added error message for missing modules (send_raw, blockusers) and znc_block, znc_unb...
[ZNCAdmin.git] / zncadmin / ZNCServer.class.php
index 35724a1c1994628cfa283a2a8c4b84872a71ce1c..d5d4badbc6ff85050cb1eaf12abfebaaada96d7d 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,14 @@ class ZNCServer {
             $content = $content[0];
             $post[$name] = $content;
         }
+    }
+    
+    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 +242,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 +271,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) {