some changes
[ZNCAdmin.git] / zncadmin / ZNCServer.class.php
index 35724a1c1994628cfa283a2a8c4b84872a71ce1c..3964875a3bc9904cfb2de10c29c2bdf3f3410e75 100644 (file)
  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
  */
 
+define("ERR_OK", 0);
+define("ERR_UNKNOWN", 1);
+define("ERR_MODULE_NOT_FOUND", 2);
+define("ERR_USER_NOT_FOUND", 3);
+
 class ZNCServer {
        private $host, $port;
        private $connector;
@@ -40,7 +45,7 @@ class ZNCServer {
        }
        
        public function getUserList() {
-               $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/webadmin/listusers");
+               $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]);
@@ -53,18 +58,16 @@ class ZNCServer {
                        $exp2 = explode('</td>', $exp[2]);
                        $userdata['user'] = $exp2[0];
                        $exp2 = explode('</td>', $exp[3]);
-                       $userdata['clients'] = $exp2[0];
-                       $exp2 = explode('</td>', $exp[4]);
                        $userdata['server'] = $exp2[0];
-                       $exp2 = explode('</td>', $exp[5]);
-                       $userdata['nick'] = $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/lastseen/");
+               $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]);
@@ -94,14 +97,14 @@ class ZNCServer {
         ident
     */
     public function addZNC($username, $password, $settings, $servers, $modules, $others) {
-        $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/webadmin/adduser");
+        $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/global/webadmin/adduser");
                $exp = explode('name="_CSRF_Check" value="', $html);
         $exp = explode('"', $exp[1]);
         $csrf = $exp[0];
         $post = array();
         $post['_CSRF_Check'] = $csrf;
         $post['submitted'] = '1';
-        $post['doconnect'] = '1';
+        //$post['doconnect'] = '1';
         $post['user'] = $username;
         $post['password'] = $password;
         $post['password2'] = $password;
@@ -109,22 +112,57 @@ class ZNCServer {
         $post['altnick'] = $settings['altnick'];
         $post['ident'] = $settings['ident'];
         $post['statusprefix'] = '*';
-        $post['servers'] = implode("\n", $servers);
         $post['loadmod'] = array();
         foreach($modules as $name => $args) {
             $post['loadmod'][] = $name;
             if($args != "")
                 $post['modargs_'.$name] = $args;
         }
+        
         foreach($others as $name => $value) {
             $post[$name] = $value;
         }
-        $html = $this->connector->post("http://".$this->host.":".$this->port."/mods/webadmin/adduser", $post);
+        
+        $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;
+            }
+            return !preg_match("/Invalid Submission/i", $html);
+        }
+        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);
+        $exp = explode('"', $exp[1]);
+        $csrf = $exp[0];
+        $post = array();
+        $post['_CSRF_Check'] = $csrf;
+        $post['submitted'] = '1';
+        $post['network'] = 'default';
+        $post['nick'] = $settings['nick'];
+        $post['altnick'] = $settings['altnick'];
+        $post['ident'] = $settings['ident'];
+        $post['servers'] = implode("\n", $servers);
+        $post['doconnect'] = '1';
+        $post['floodprotection'] = '1';
+        $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/webadmin/deluser?user=".$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('"', $exp[1]);
         $csrf = $exp[0];
@@ -132,14 +170,10 @@ class ZNCServer {
         $post['_CSRF_Check'] = $csrf;
         $post['submitted'] = '1';
         $post['user'] = $username;
-        $this->connector->post("http://".$this->host.":".$this->port."/mods/webadmin/deluser", $post);
+        $this->connector->post("http://".$this->host.":".$this->port."/mods/global/webadmin/deluser", $post);
     }
     
-    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/webadmin/edituser?user=".$username);
-        if(preg_match("/No such username/i", $html)) return false;
-        $post = array();
-        $post['loadmod'] = array();
+    private function parseHTMLFields($html, &$post) {
         preg_match_all("<input ([^\>]*)>", $html, $matches);
         foreach($matches[0] as $input) {
             $fields = array(0 => "");
@@ -208,6 +242,58 @@ class ZNCServer {
             $content = $content[0];
             $post[$name] = $content;
         }
+        //select boxes
+        $selectboxes = explode("<select ", $html);
+        for($j = 1; $j < count($selectboxes); $j++) {
+            $exp = explode(">", $selectboxes[$j], 2);
+            $name = explode("name=\"", $exp[0]);
+            $name = explode("\"", $name[1]);
+            $name = $name[0];
+            $content = explode("</select", $exp[1]);
+            $content = $content[0];
+            preg_match_all("<option ([^\>]*)>", $content, $matches);
+            foreach($matches[0] as $input) {
+                $fields = array(0 => "");
+                $esc = false; $str = false;
+                $fieldid = 0;
+                for($i = 0; $i < strlen($input); $i++) {
+                    if($esc) {
+                        $esc = false;
+                        $fields[$fieldid] .= $input[$i];
+                        continue;
+                    }
+                    if($input[$i] == "\\") {
+                        $esc = true;
+                    }
+                    if($input[$i] == "\"") {
+                        $str = !$str;
+                        continue;
+                    }
+                    if($input[$i] == " " && !$str) {
+                        $fields[++$fieldid] = "";
+                        continue;
+                    }
+                    $fields[$fieldid] .= $input[$i];
+                }
+                $cfields = array();
+                foreach($fields as $field) {
+                    $fieldexp = explode("=", $field, 2);
+                    if(count($fieldexp) != 2) continue;
+                    $cfields[$fieldexp[0]] = $fieldexp[1];
+                }
+                if(!$post[$name] || $cfields['selected']) {
+                    $post[$name] = $cfields['value'];
+                }
+            }
+        }
+    }
+    
+    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;
+        $post = array();
+        $post['loadmod'] = array();
+        $this->parseHTMLFields($html, $post);
         if($password) {
             $post['password'] = $password;
             $post['password2'] = $password;
@@ -229,27 +315,51 @@ class ZNCServer {
         foreach($others as $name => $value) {
             $post[$name] = $value;
         }
-        $html = $this->connector->post("http://".$this->host.":".$this->port."/mods/webadmin/edituser?user=".$username, $post);
+        $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 simulZNC($username, $raw, $server = true) {
-        $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/send_raw/");
-        if(preg_match("/Not Found/i", $html)) return false;
+    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');
+        fwrite($handle, "$html\r\n");
+        fclose($handle);
+        if(preg_match("/No such username/i", $html)) return ERR_USER_NOT_FOUND;
+        $post = array();
+        $post['user'] = $username;
+        $post['loadmod'] = array();
+        $this->parseHTMLFields($html, $post);
+        if(!$post['embed_blockuser_presented']) return ERR_MODULE_NOT_FOUND;
+        if(!($post['embed_blockuser_block'] xor $block)) return ERR_OK;
+        $post['embed_blockuser_block'] = ($block ? "1" : null);
+        if(!$block) {
+            $post['doconnect'] = 1;
+        }
+        $html = $this->connector->post("http://".$this->host.":".$this->port."/mods/global/webadmin/edituser", $post);
+        $handle = fopen('/home/srvx/neonserv/log.txt', 'a');
+        fwrite($handle, "$html\r\n");
+        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('"', $exp[1]);
         $csrf = $exp[0];
         $post = array();
         $post['_CSRF_Check'] = $csrf;
         $post['submitted'] = '1';
-        $post['user'] = $username;
+        $post['network'] = $username.'/default';
         $post['send_to'] = ($server ? "server" : "client");
         $post['line'] = $raw;
-        $this->connector->post("http://".$this->host.":".$this->port."/mods/send_raw/", $post);
+        $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/webadmin/addchan?user=".$username);
+        $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/global/webadmin/addchan?user=".$username);
         if(preg_match("/No such username/i", $html)) return false;
                $exp = explode('name="_CSRF_Check" value="', $html);
         $exp = explode('"', $exp[1]);
@@ -268,7 +378,7 @@ class ZNCServer {
         $default_buffer = $exp[0];
         $post['buffercount'] = ($buffer ? $buffer : $default_buffer);
         $post['save'] = 'true';
-        $this->connector->post("http://".$this->host.":".$this->port."/mods/webadmin/addchan?user=".$username, $post);
+        $this->connector->post("http://".$this->host.":".$this->port."/mods/global/webadmin/addchan?user=".$username, $post);
     }
     
 }