ba9200e24c796905eecd53aa0504f9ab35b38aae
[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  * String getName()
43  *     returns the Name of the Server
44  *
45  * String getStartTime()
46  *     returns the startup time of the Server
47  *
48  * String getLinkTime()
49  *     returns the link time of the Server
50  *
51  * String getDescription()
52  *     returns the Description of the Server
53  *
54  * void addServer(P10_Server $server)
55  *     adds a Server to the server's "slave" list
56  *
57  * void delServer(P10_Server $server)
58  *     removes a Server from the server's "slave" list
59  *
60  * void addUser(P10_User $user)
61  *     adds a User to the server's userlist
62  *
63  * void delUser(P10_User $user)
64  *     removes a User from the server's userlist
65  *
66  * P10_User[] getUsers()
67  *     returns the server's userlist
68  */
69
70 class P10_Server {
71         private static $static_servers = array();
72         
73         public static function getServerByNum($numeric) {
74                 if(array_key_exists($numeric, self::$static_servers)) {
75                         return self::$static_servers[$numeric];
76                 }
77                 return NULL;
78         }
79         
80         public static function getServerByName($name) {
81                 $name = strtolower($name);
82                 foreach(self::$static_servers as $server) {
83                         if(strtolower($server->getName()) == $name) {
84                                 return $server;
85                         }
86                 }
87                 return NULL;
88         }
89         
90         public static function getServerCount() {
91                 return count(self::$static_servers);
92         }
93         
94         public static function getServers() {
95                 return self::$static_servers;
96         }
97         
98         
99         private $name;
100         private $numeric;
101         private $parent_server;
102         private $start_time;
103         private $link_time;
104         private $description;
105         private $servers = array(); //all Servers connected to this Server
106         private $users = array(); //all Users connected to this Server
107         
108         public function __construct($name, $numeric, $parent_server, $start_time, $link_time, $description) {
109                 $this->name = $name;
110                 $this->numeric = $numeric;
111                 $this->parent_server = $parent_server;
112                 $this->start_time = $start_time;
113                 $this->link_time = $link_time;
114                 $this->description = $description;
115                 
116                 self::$static_servers[$numeric] = $this;
117         }
118         
119         public function disconnectServer($eventHandler, $linked_only = false) {
120                 if(!$linked_only) {
121                         if($eventHandler)
122                                 $eventHandler->event_squit($this);
123                         if($this->parent_server) {
124                                 $this->parent_server->delServer($this);
125                         }
126                         $this->disconnectUsers();
127                         unset(self::$static_servers[$this->numeric]);
128                 }
129                 foreach($this->servers as $server) {
130                         $server->disconnectServer($eventHandler);
131                 }
132         }
133         
134         public function disconnectUsers() {
135                 //disconnect all Users connected to the actual Server
136                 foreach($this->users as $user) {
137                         $user->quit("*.net *.split");
138                 }
139         }
140         
141         public function getNumeric() {
142                 return $this->numeric;
143         }
144         
145         public function getName() {
146                 return $this->name;
147         }
148         
149         public function getStartTime() {
150                 return $this->start_time;
151         }
152         
153         public function getLinkTime() {
154                 return $this->link_time;
155         }
156         
157         public function getDescription() {
158                 return $this->description;
159         }
160         
161         public function addServer($server) {
162                 $this->servers[$server->getNumeric()] = $server;
163         }
164         
165         public function delServer($server) {
166                 if(array_key_exists($server->getNumeric(), $this->servers)) {
167                         unset($this->servers[$server->getNumeric()]);
168                 } else {
169                         trigger_error("Tried to remove a Server, that does NOT exist.", E_USER_WARNING);
170                 }
171         }
172         
173         public function addUser($user) {
174                 $this->users[$user->getNumeric()] = $user;
175         }
176         
177         public function delUser($user) {
178                 if(array_key_exists($user->getNumeric(), $this->users)) {
179                         unset($this->users[$user->getNumeric()]);
180                 } else {
181                         trigger_error("Tried to remove a User, that does NOT exist.", E_USER_WARNING);
182                 }
183         }
184         
185         public function getUsers() {
186                 return $this->users;
187         }
188         
189         public function getUserCount() {
190                 return count($this->users);
191         }
192 }
193
194 ?>