fixed ZNCServer::parseHTMLFields(): parse select boxes
[ZNCAdmin.git] / zncadmin / ZNCServer.class.php
index 35724a1c1994628cfa283a2a8c4b84872a71ce1c..d001947fcf7c2f67f9a088919c805c99c870846a 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;
@@ -135,11 +140,7 @@ class ZNCServer {
         $this->connector->post("http://".$this->host.":".$this->port."/mods/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 +209,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/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;
@@ -233,9 +286,25 @@ class ZNCServer {
         return !preg_match("/Invalid Submission/i", $html);
     }
     
-    public function simulZNC($username, $raw, $server = true) {
+    public function blockZNC($username, $block) {
+        $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/webadmin/edituser?user=".$username);
+        if(preg_match("/No such username/i", $html)) return ERR_USER_NOT_FOUND;
+        $post = array();
+        $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/webadmin/edituser?user=".$username, $post);
+        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/send_raw/");
-        if(preg_match("/Not Found/i", $html)) return false;
+        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];
@@ -246,6 +315,7 @@ class ZNCServer {
         $post['send_to'] = ($server ? "server" : "client");
         $post['line'] = $raw;
         $this->connector->post("http://".$this->host.":".$this->port."/mods/send_raw/", $post);
+        return ERR_OK;
     }
     
     public function addChan($username, $channel, $key = NULL, $buffer = NULL) {