Merge branch 'ZNCv1' into MixedVersions
[ZNCAdmin.git] / zncadmin / ZNCServer.class.php
1 <?php
2 /* ZNCServer.class.php - ZNC Server Class - ZNCAdmin
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 define("ERR_OK", 0);
20 define("ERR_UNKNOWN", 1);
21 define("ERR_MODULE_NOT_FOUND", 2);
22 define("ERR_USER_NOT_FOUND", 3);
23 define("ERR_UNKNOWN_ZNC_VERSION_OR_METHOD", 4);
24
25 $zncserver_classes = array(
26         "ZNCServerV0.class.php",
27         "ZNCServerV1.class.php",
28 );
29
30 class ZNCServer {
31         private static $zncserver_classes = array();
32         private $host, $port, $server = NULL;
33         
34         private static function load_classes() {
35                 global $zncserver_classes;
36                 foreach($zncserver_classes as $classfile) {
37                         if(isset($classinfo))
38                                 unset($classinfo);
39                         include($classfile);
40                         if(isset($classinfo))
41                                 self::$zncserver_classes[] = $classinfo;
42                 }
43         }
44         
45         public function ZNCServer($host, $port, $version = null) {
46                 $this->host = $host;
47                 $this->port = $port;
48                 if(is_empty(self::$zncserver_classes))
49                         self::load_classes();
50                 if($version !== null) {
51                         $this->loadServerObject($version);
52                 }
53         }
54         
55         private function loadServerObject($version, $http_connector = null) {
56                 $preferred_class = null;
57                 foreach(self::$zncserver_classes as $classinfo) {
58                         if($version < $classinfo['version']) 
59                                 continue;
60                         if($preferred_class && $classinfo['version'] < $preferred_class['version'])
61                                 continue;
62                         $preferred_class = $classinfo;
63                 }
64                 if($preferred_class) {
65                         $classname = $preferred_class['name'];
66                         $this->server = new $classname($this->host, $this->port, $http_connector);
67                 }
68         }
69         
70         public function login($user, $pass) { /* Version independent login, maybe there's a better way? */
71                 if($this->server)
72                         return $this->server->login($user, $pass);
73                 
74                 $post = array(
75                         "user" => $user,
76                         "pass" => $pass,
77                         "submitted" => "1"
78                 );
79                 $connector = new HTTPConnector();
80                 $connector->post("http://".$this->host.":".$this->port."/login", $post);
81                 $http = $connector->get("http://".$this->host.":".$this->port."/?cookie_check=true");
82                 $logged_in = !preg_match("/errorbar/i", $http);
83                 
84                 /* Version detection */
85                 if(preg_match("ZNC ([0-9]+\.[0-9]+)", $http, $version_match))
86                         $this->loadServerObject($version_match[1], $connector);
87                 
88                 return $logged_in;
89         }
90         
91         
92         
93         public function __call($name, $arguments) {
94                 if(!$this->server)
95                         return ERR_UNKNOWN_ZNC_VERSION_OR_METHOD;
96                 if(!method_exists($this->server, $name))
97                         return ERR_UNKNOWN_ZNC_VERSION_OR_METHOD;
98                 return call_user_func_array(array($this->server, $name), $arguments);
99         }
100
101 }
102
103 ?>