added P10_Server.class.php & recv_server
[PHP-P10.git] / Uplink / P10_Server.class.php
1 <?php
2 /********************************* PHP-P10 ******************************
3  *    P10 uplink class by pk910   (c)2011 pk910                         *
4  ************************************************************************
5  *                          Version 2 (OOP)                             *
6  *                                                                      *
7  * PHP-P10 is free software; you can redistribute it and/or modify      *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or    *
10  * (at your option) any later version.                                  *
11  *                                                                      *
12  * This program is distributed in the hope that it will be useful,      *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
15  * GNU General Public License for more details.                         *
16  *                                                                      *
17  * You should have received a copy of the GNU General Public License    *
18  * along with PHP-P10; if not, write to the Free Software Foundation,   *
19  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.       *
20  *                                                                      *
21  ************************************************************************
22  * 
23  *  Uplink/P10_Server.class.php
24  *
25  * This class represents a IRC Server
26  *
27  ************************************************************************
28  * accessable methods:
29  *
30  * static P10_Server getServerByNum(String $numeric) 
31  *     searches and returns the Server with the provided Numeric
32  *
33  * __construct(String $name, String $numeric, P10_Server $parent_server, int $start_time, $link_time, $description)
34  *     *** nothing to say here ***
35  *
36  * void disconnectServer(bool $linked_only = false)
37  *     DISCONNECT EVENT (if $linked_only is true, only the Servers connected to the actual Server get disconnected.)
38  *
39  * String getNumeric()
40  *     returns the Numeric of the Server
41  *
42  * void addServer(P10_Server $server)
43  *     adds a Server to the server's "slave" list
44  *
45  * void delServer(P10_Server $server)
46  *     removes a Server to the server's "slave" list
47  */
48
49 class P10_Server {
50         private static $static_servers = array();
51         
52         public static getServerByNum($numeric) {
53                 if(array_key_exists($numeric, self::$static_servers)) {
54                         return self::$static_servers[$numeric];
55                 }
56                 return NULL;
57         }
58         
59         
60         private $name;
61         private $numeric;
62         private $parent_server;
63         private $start_time;
64         private $link_time;
65         private $description;
66         private $servers = array(); //all Servers connected to this Server
67         
68         public function __construct($name, $numeric, $parent_server, $start_time, $link_time, $description) {
69                 $this->name = $name;
70                 $this->numeric = $numeric;
71                 $this->parent_server = $parent_server;
72                 $this->start_time = $start_time;
73                 $this->link_time = $link_time;
74                 $this->description = $description;
75                 
76                 self::$static_servers[$numeric] = $this;
77         }
78         
79         public function disconnectServer($linked_only = false) {
80                 if(!$linked_only) {
81                         if($this->parent_server) {
82                                 $this->parent_server->delServer($this);
83                         }
84                         $this->disconnectUsers();
85                         unset(self::$static_servers[$this->numeric]);
86                 }
87                 foreach($this->servers as $server) {
88                         $server->disconnectServer();
89                 }
90         }
91         
92         public function disconnectUsers() {
93                 //disconnect all Users connected to the actual Server
94         }
95         
96         public function getNumeric() {
97                 return $this->numeric;
98         }
99         
100         public function addServer($server) {
101                 $this->servers[$server->getNumeric()] = $server;
102         }
103         
104         public function delServer($server) {
105                 if(array_key_exists($server->getNumeric(), $this->servers)) {
106                         unset($this->servers[$server->getNumeric()]);
107                 } else {
108                         trigger_error("Tried to remove a Server, that does NOT exist.", E_USER_WARNING);
109                 }
110         }
111 }
112
113 ?>