bugfix
[ZNCAdmin.git] / zncadmin / ZNCServer.class.php
index 3964875a3bc9904cfb2de10c29c2bdf3f3410e75..fdf3c1e02daa86fc6f9c6cfb4c3bc20f01eea5ee 100644 (file)
@@ -1,19 +1,19 @@
 <?php
 /* ZNCServer.class.php - ZNC Server Class - ZNCAdmin
  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
- * 
+ *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
- * 
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * 
- * You should have received a copy of the GNU General Public License 
- * along with this program. If not, see <http://www.gnu.org/licenses/>. 
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 
 define("ERR_OK", 0);
@@ -22,74 +22,74 @@ define("ERR_MODULE_NOT_FOUND", 2);
 define("ERR_USER_NOT_FOUND", 3);
 
 class ZNCServer {
-       private $host, $port;
-       private $connector;
-       
-       public function ZNCServer($host, $port) {
-               $this->host = $host;
-               $this->port = $port;
-               $this->connector = new HTTPConnector();
-       }
-       
-       public function login($user, $pass) {
-               $post = array(
-                       "user" => $user,
-                       "pass" => $pass,
-                       "submitted" => "1"
-               );
-               $this->connector->post("http://".$this->host.":".$this->port."/login", $post);
-               $http = $this->connector->get("http://".$this->host.":".$this->port."/?cookie_check=true");
-               $logged_in = !preg_match("/errorbar/i", $http);
-               
-               return $logged_in;
-       }
-       
-       public function getUserList() {
-               $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/global/webadmin/listusers");
-               $exp = explode('<div class="toptable">', $html);
-               $exp = explode('</div>', $exp[1]);
-               $exp = explode('<tbody>', $exp[0]);
-               $exp = explode('</tbody>', $exp[1]);
-               $list = explode('<tr class=', $exp[0]);
-               $userlist = array();
-               for($i = 1; $i < count($list); $i++) {
-                       $userdata = array();
-                       $exp = explode('<td>', $list[$i]);
-                       $exp2 = explode('</td>', $exp[2]);
-                       $userdata['user'] = $exp2[0];
-                       $exp2 = explode('</td>', $exp[3]);
-                       $userdata['server'] = $exp2[0];
-                       $exp2 = explode('</td>', $exp[4]);
-                       $userdata['clients'] = $exp2[0];
-                       $userlist[] = $userdata;
-               }
-               return $userlist;
-       }
-    
+    private $host, $port;
+    private $connector;
+
+    public function ZNCServer($host, $port) {
+        $this->host = $host;
+        $this->port = $port;
+        $this->connector = new HTTPConnector();
+    }
+
+    public function login($user, $pass) {
+        $post = array(
+            "user" => $user,
+            "pass" => $pass,
+            "submitted" => "1"
+        );
+        $this->connector->post("http://".$this->host.":".$this->port."/login", $post);
+        $http = $this->connector->get("http://".$this->host.":".$this->port."/?cookie_check=true");
+        $logged_in = !preg_match("/errorbar/i", $http);
+
+        return $logged_in;
+    }
+
+    public function getUserList() {
+        $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/global/webadmin/listusers");
+        $exp = explode('<div class="toptable">', $html);
+        $exp = explode('</div>', $exp[1]);
+        $exp = explode('<tbody>', $exp[0]);
+        $exp = explode('</tbody>', $exp[1]);
+        $list = explode('<tr class=', $exp[0]);
+        $userlist = array();
+        for($i = 1; $i < count($list); $i++) {
+            $userdata = array();
+            $exp = explode('<td>', $list[$i]);
+            $exp2 = explode('</td>', $exp[2]);
+            $userdata['user'] = $exp2[0];
+            $exp2 = explode('</td>', $exp[3]);
+            $userdata['server'] = $exp2[0];
+            $exp2 = explode('</td>', $exp[4]);
+            $userdata['clients'] = $exp2[0];
+            $userlist[] = $userdata;
+        }
+        return $userlist;
+    }
+
     public function getSeenList() {
-               $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/global/lastseen/");
-               $exp = explode('<div class="toptable">', $html);
-               $exp = explode('</div>', $exp[1]);
-               $exp = explode('<tbody>', $exp[0]);
-               $exp = explode('</tbody>', $exp[1]);
-               $list = explode('<tr class=', $exp[0]);
-               $seenlist = array();
-               for($i = 1; $i < count($list); $i++) {
-                       $userdata = array();
-                       $exp = explode('<td>', $list[$i]);
-            
-                       $exp2 = explode('</td>', $exp[1]);
-                       $userdata['user'] = $exp2[0];
-                       $exp2 = explode('</td>', $exp[2]);
-                       $userdata['seen'] = $exp2[0];
+        $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/global/lastseen/");
+        $exp = explode('<div class="toptable">', $html);
+        $exp = explode('</div>', $exp[1]);
+        $exp = explode('<tbody>', $exp[0]);
+        $exp = explode('</tbody>', $exp[1]);
+        $list = explode('<tr class=', $exp[0]);
+        $seenlist = array();
+        for($i = 1; $i < count($list); $i++) {
+            $userdata = array();
+            $exp = explode('<td>', $list[$i]);
+
+            $exp2 = explode('</td>', $exp[1]);
+            $userdata['user'] = $exp2[0];
+            $exp2 = explode('</td>', $exp[2]);
+            $userdata['seen'] = $exp2[0];
             $userdata['seen_unix'] = strtotime($exp2[0]);
-                       $exp2 = explode('</td>', $exp[3]);
-                       $userdata['info'] = $exp2[0];
-                       $seenlist[] = $userdata;
-               }
-               return $seenlist;
-       }
-    
+            $exp2 = explode('</td>', $exp[3]);
+            $userdata['info'] = $exp2[0];
+            $seenlist[] = $userdata;
+        }
+        return $seenlist;
+    }
+
     /*
     $settings array elements:
         nick
@@ -98,7 +98,7 @@ class ZNCServer {
     */
     public function addZNC($username, $password, $settings, $servers, $modules, $others) {
         $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/global/webadmin/adduser");
-               $exp = explode('name="_CSRF_Check" value="', $html);
+        $exp = explode('name="_CSRF_Check" value="', $html);
         $exp = explode('"', $exp[1]);
         $csrf = $exp[0];
         $post = array();
@@ -118,13 +118,13 @@ class ZNCServer {
             if($args != "")
                 $post['modargs_'.$name] = $args;
         }
-        
+
         foreach($others as $name => $value) {
             $post[$name] = $value;
         }
-        
+
         $html = $this->connector->post("http://".$this->host.":".$this->port."/mods/global/webadmin/adduser", $post);
-        
+
         if(!preg_match("/Invalid Submission/i", $html)) {
             if(!$this->addNetwork($username, $settings, $servers, $other)) {
                 return false;
@@ -133,7 +133,7 @@ class ZNCServer {
         }
         return !preg_match("/Invalid Submission/i", $html);
     }
-    
+
     public function addNetwork($username, $settings, $servers, $other) {
         $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/global/webadmin/addnetwork?user=".$username);
         $exp = explode('name="_CSRF_Check" value="', $html);
@@ -152,18 +152,18 @@ class ZNCServer {
         $post['floodrate'] = '1.0';
         $post['floodburst'] = '4';
         $post['user'] = $username;
-        
+
         foreach($others as $name => $value) {
             $post[$name] = $value;
         }
-        
+
         $html = $this->connector->post("http://".$this->host.":".$this->port."/mods/global/webadmin/addnetwork", $post);
         return !preg_match("/Invalid Submission/i", $html);
     }
-       
+
     public function delZNC($username) {
         $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/global/webadmin/deluser?user=".$username);
-               $exp = explode('name="_CSRF_Check" value="', $html);
+        $exp = explode('name="_CSRF_Check" value="', $html);
         $exp = explode('"', $exp[1]);
         $csrf = $exp[0];
         $post = array();
@@ -172,7 +172,7 @@ class ZNCServer {
         $post['user'] = $username;
         $this->connector->post("http://".$this->host.":".$this->port."/mods/global/webadmin/deluser", $post);
     }
-    
+
     private function parseHTMLFields($html, &$post) {
         preg_match_all("<input ([^\>]*)>", $html, $matches);
         foreach($matches[0] as $input) {
@@ -213,7 +213,7 @@ class ZNCServer {
                 case "checkbox":
                     if($cfields['checked']) {
                         $value = ($cfields['value'] ? $cfields['value'] : "1");
-                    } else 
+                    } else
                         $value = null;
                     break;
                 default:
@@ -287,7 +287,7 @@ class ZNCServer {
             }
         }
     }
-    
+
     public function editZNC($username, $password = NULL, $new_servers = NULL, $add_modules = NULL, $del_modules = NULL, $others = NULL) {
         $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/global/webadmin/edituser?user=".$username);
         if(preg_match("/No such username/i", $html)) return false;
@@ -318,7 +318,7 @@ class ZNCServer {
         $html = $this->connector->post("http://".$this->host.":".$this->port."/mods/global/webadmin/edituser?user=".$username, $post);
         return !preg_match("/Invalid Submission/i", $html);
     }
-    
+
     public function blockZNC($username, $block) {
         $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/global/webadmin/edituser?user=".$username);
         $handle = fopen('/home/srvx/neonserv/log.txt', 'a');
@@ -341,11 +341,11 @@ class ZNCServer {
         fclose($handle);
         return (preg_match("/Invalid Submission/i", $html) ? ERR_UNKNOWN : ERR_OK);
     }
-    
+
     public function simulZNC($username, $raw, &$errmsg, $server = true) {
         $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/user/send_raw/");
         if(preg_match("/Not Found/i", $html)) return ERR_MODULE_NOT_FOUND;
-               $exp = explode('name="_CSRF_Check" value="', $html);
+        $exp = explode('name="_CSRF_Check" value="', $html);
         $exp = explode('"', $exp[1]);
         $csrf = $exp[0];
         $post = array();
@@ -357,16 +357,17 @@ class ZNCServer {
         $html = $this->connector->post("http://".$this->host.":".$this->port."/mods/user/send_raw/", $post);
         return ERR_OK;
     }
-    
+
     public function addChan($username, $channel, $key = NULL, $buffer = NULL) {
-        $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/global/webadmin/addchan?user=".$username);
+        $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/global/webadmin/addchan?user=".$username.'&network=default');
         if(preg_match("/No such username/i", $html)) return false;
-               $exp = explode('name="_CSRF_Check" value="', $html);
+        $exp = explode('name="_CSRF_Check" value="', $html);
         $exp = explode('"', $exp[1]);
         $csrf = $exp[0];
         $post = array();
         $post['_CSRF_Check'] = $csrf;
         $post['submitted'] = '1';
+        $post['network'] = 'default';
         $post['user'] = $username;
         $post['name'] = $channel;
         $exp = explode('name="key" value="', $html);
@@ -377,10 +378,12 @@ class ZNCServer {
         $exp = explode('"', $exp[1]);
         $default_buffer = $exp[0];
         $post['buffercount'] = ($buffer ? $buffer : $default_buffer);
+        $post['defmodes'] = '';
         $post['save'] = 'true';
+        $post['autoclearchanbuffer'] = 'true';
         $this->connector->post("http://".$this->host.":".$this->port."/mods/global/webadmin/addchan?user=".$username, $post);
     }
-    
+
 }
 
 ?>
\ No newline at end of file