2 /* ZNCServer.class.php - ZNC Server Class - ZNCAdmin
3 * Copyright (C) 2011-2012 Philipp Kreil (pk910)
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.
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.
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/>.
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);
25 $zncserver_classes = array(
26 "ZNCServerV0.class.php",
30 private static $zncserver_classes = array();
31 private $host, $port, $server = NULL;
33 private static function load_classes() {
34 global $zncserver_classes;
35 foreach($zncserver_classes as $classfile) {
40 self::$zncserver_classes[] = $classinfo;
44 public function ZNCServer($host, $port, $version = null) {
47 if(is_empty(self::$zncserver_classes))
49 if($version !== null) {
50 $this->loadServerObject($version);
54 private function loadServerObject($version, $http_connector = null) {
55 $preferred_class = null;
56 foreach(self::$zncserver_classes as $classinfo) {
57 if($version < $classinfo['version'])
59 if($preferred_class && $classinfo['version'] < $preferred_class['version'])
61 $preferred_class = $classinfo;
63 if($preferred_class) {
64 $classname = $preferred_class['name'];
65 $this->server = new $classname($this->host, $this->port, $http_connector);
69 public function login($user, $pass) { /* Version independent login, maybe there's a better way? */
71 return $this->server->login($user, $pass);
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);
83 /* Version detection */
84 if(preg_match("ZNC ([0-9]+\.[0-9]+)", $http, $version_match))
85 $this->loadServerObject($version_match[1], $connector);
92 public function __call($name, $arguments) {
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);