fb1ce9cda9b220891c49dfec1db180198d8f3c2f
[PHP-P10.git] / Uplink / P10_Server.class.php
1 <?php
2 /******************************* PHP-P10 v2 *****************************
3  * Copyright (C) 2011-2012  Philipp Kreil (pk910)                       *
4  *                                                                      *
5  * This program is free software: you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation, either version 3 of the License, or    *
8  * (at your option) any later version.                                  *
9  *                                                                      * 
10  * This program is distributed in the hope that it will be useful,      *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
13  * GNU General Public License for more details.                         *
14  *                                                                      *
15  * You should have received a copy of the GNU General Public License    *
16  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17  *                                                                      *
18  ************************************************************************
19  * 
20  *  Uplink/P10_Server.class.php
21  *
22  * This class represents a IRC Server
23  *
24  */
25
26 class P10_Server {
27         private static $static_servers = array();
28         
29         public static function getServerByNum($numeric) {
30                 if(array_key_exists($numeric, self::$static_servers)) {
31                         return self::$static_servers[$numeric];
32                 }
33                 return NULL;
34         }
35         
36         public static function getServerByName($name) {
37                 $name = strtolower($name);
38                 foreach(self::$static_servers as $server) {
39                         if(strtolower($server->getName()) == $name) {
40                                 return $server;
41                         }
42                 }
43                 return NULL;
44         }
45         
46         public static function getServerCount() {
47                 return count(self::$static_servers);
48         }
49         
50         public static function getServers() {
51                 return self::$static_servers;
52         }
53         
54         
55         private $name;
56         private $numeric;
57         private $parent_server;
58         private $start_time;
59         private $link_time;
60         private $description;
61         private $servers = array(); //all Servers connected to this Server
62         private $users = array(); //all Users connected to this Server
63         
64         public function __construct($name, $numeric, $parent_server, $start_time, $link_time, $description) {
65                 $this->name = $name;
66                 $this->numeric = $numeric;
67                 $this->parent_server = $parent_server;
68                 $this->start_time = $start_time;
69                 $this->link_time = $link_time;
70                 $this->description = $description;
71                 
72                 self::$static_servers[$numeric] = $this;
73         }
74         
75         public function disconnectServer($eventHandler, $linked_only = false) {
76                 if(!$linked_only) {
77                         if($eventHandler)
78                                 $eventHandler->event_squit($this);
79                         if($this->parent_server) {
80                                 $this->parent_server->delServer($this);
81                         }
82                         $this->disconnectUsers();
83                         unset(self::$static_servers[$this->numeric]);
84                 }
85                 foreach($this->servers as $server) {
86                         $server->disconnectServer($eventHandler);
87                 }
88         }
89         
90         public function disconnectUsers() {
91                 //disconnect all Users connected to the actual Server
92                 foreach($this->users as $user) {
93                         $user->quit("*.net *.split");
94                 }
95         }
96         
97         public function getNumeric() {
98                 return $this->numeric;
99         }
100         
101         public function getName() {
102                 return $this->name;
103         }
104         
105         public function getStartTime() {
106                 return $this->start_time;
107         }
108         
109         public function getLinkTime() {
110                 return $this->link_time;
111         }
112         
113         public function getDescription() {
114                 return $this->description;
115         }
116         
117         public function addServer($server) {
118                 $this->servers[$server->getNumeric()] = $server;
119         }
120         
121         public function delServer($server) {
122                 if(array_key_exists($server->getNumeric(), $this->servers)) {
123                         unset($this->servers[$server->getNumeric()]);
124                 } else {
125                         trigger_error("Tried to remove a Server, that does NOT exist.", E_USER_WARNING);
126                 }
127         }
128         
129         public function addUser($user) {
130                 $this->users[$user->getNumeric()] = $user;
131         }
132         
133         public function delUser($user) {
134                 if(array_key_exists($user->getNumeric(), $this->users)) {
135                         unset($this->users[$user->getNumeric()]);
136                 } else {
137                         trigger_error("Tried to remove a User, that does NOT exist.", E_USER_WARNING);
138                 }
139         }
140         
141         public function getUsers() {
142                 return $this->users;
143         }
144         
145         public function getUserCount() {
146                 return count($this->users);
147         }
148 }
149
150 ?>