752e8be6f856cc1ca0680447c5d948cb3b7b3d63
[PHP-P10.git] / Uplink / P10_User.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_User.class.php
21  *
22  * This class represents a IRC User
23  *
24  */
25
26 class P10_User {
27         private static $static_users = array();
28         
29         public static function getUserByNum($numeric) {
30                 if(array_key_exists($numeric, self::$static_users)) {
31                         return self::$static_users[$numeric];
32                 }
33                 return NULL;
34         }
35         
36         public static function getUserByNick($nick) {
37                 $nick = strtolower($nick);
38                 foreach(self::$static_users as $user) {
39                         if(strtolower($user->getNick()) == $nick) {
40                                 return $user;
41                         }
42                 }
43                 return NULL;
44         }
45         
46         public static function getAllUsers() {
47                 return self::$static_users;
48         }
49         
50         
51         private $numeric;
52         private $server;
53         private $nick;
54         private $ident;
55         private $host;
56         private $ip;
57         private $connect_time;
58         private $modes;
59         private $realname;
60         private $channels = array();
61         private $away = null;
62         
63         public function __construct($nick, $numeric, $server, $connect_time, $ident, $host, $ip, $realname, $modes) {
64                 $this->nick = $nick;
65                 $this->numeric = $numeric;
66                 $this->server = $server;
67                 $this->connect_time = $connect_time;
68                 $this->ident = $ident;
69                 $this->host = $host;
70                 $this->ip = $ip;
71                 $this->realname = $realname;
72                 $this->modes = $modes;
73                 $server->addUser($this);
74                 self::$static_users[$numeric] = $this;
75         }
76         
77         public function getNumeric() {
78                 return $this->numeric;
79         }
80         
81         public function getServer() {
82                 return $this->server;
83         }
84         
85         public function setNick($nick) {
86                 $this->nick = $nick;
87         }
88         
89         public function getNick() {
90                 return $this->nick;
91         }
92         
93         public function setIdent($ident) {
94                 $this->ident = $ident;
95         }
96         
97         public function getIdent() {
98                 return $this->ident;
99         }
100         
101         public function getHost() {
102                 return $this->host;
103         }
104         
105         public function getIP() {
106                 return $this->ip;
107         }
108         
109         public function getConnectTime() {
110                 return $this->connect_time;
111         }
112         
113         public function getModes() {
114                 return $this->modes;
115         }
116         
117         public function getRealname() {
118                 return $this->realname;
119         }
120         
121         public function setAway($away) {
122                 $this->away = $away;
123         }
124         
125         public function getAway() {
126                 return $this->away;
127         }
128         
129         public function isAway() {
130                 return ($this->away != null);
131         }
132         
133         public function quit($reason) {
134                 $this->server->delUser($this);
135                 unset(self::$static_users[$this->numeric]);
136                 foreach($this->channels as $channel) {
137                         $channel->quitUser($this);
138                 }
139         }
140         
141         public function addChannel($channel) {
142                 $this->channels[strtolower($channel->getName())] = $channel;
143         }
144         
145         public function delChannel($channel) {
146                 if(array_key_exists(strtolower($channel->getName()), $this->channels)) {
147                         unset($this->channels[strtolower($channel->getName())]);
148                 } else {
149                         trigger_error("Tried to remove a Channel, that does NOT exist.", E_USER_WARNING);
150                 }
151         }
152         
153         public function getChannels() {
154                 return $this->channels;
155         }
156         
157         public function getChannelCount() {
158                 return count($this->channels);
159         }
160         
161         public function isOnChannel($channel) {
162                 return array_key_exists(strtolower($channel->getName()),$this->channels);
163         }
164 }
165
166 ?>