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