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",
27 "ZNCServerV1.class.php",
31 private static $zncserver_classes = array();
32 private $host, $port, $server = NULL;
34 private static function load_classes() {
35 global $zncserver_classes;
36 foreach($zncserver_classes as $classfile) {
41 self::$zncserver_classes[] = $classinfo;
45 public function ZNCServer($host, $port, $version = null) {
48 if(empty(self::$zncserver_classes))
50 if($version !== null) {
51 $this->loadServerObject($version);
55 private function loadServerObject($version, $http_connector = null) {
56 $preferred_class = null;
57 foreach(self::$zncserver_classes as $classinfo) {
58 if($version < $classinfo['version'])
60 if($preferred_class && $classinfo['version'] < $preferred_class['version'])
62 $preferred_class = $classinfo;
64 if($preferred_class) {
65 $classname = $preferred_class['name'];
66 $this->server = new $classname($this->host, $this->port, $http_connector);
70 public function login($user, $pass) { /* Version independent login, maybe there's a better way? */
72 return $this->server->login($user, $pass);
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);
84 /* Version detection */
85 if(preg_match("#ZNC ([0-9]+\.[0-9]+)#", $http, $version_match))
86 $this->loadServerObject($version_match[1], $connector);
93 public function __call($name, $arguments) {
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);