Merge branch 'ZNCv1' into MixedVersions
authorpk910 <philipp@zoelle1.de>
Sat, 29 Jun 2013 22:37:32 +0000 (00:37 +0200)
committerpk910 <philipp@zoelle1.de>
Sat, 29 Jun 2013 22:37:32 +0000 (00:37 +0200)
.gitignore
config.example.php
zncadmin.php
zncadmin/ZNCServer.class.php
zncadmin/ZNCServerV1.class.php [new file with mode: 0644]

index 2b9ce0a56ef7ae68838c63b5fb44ef30b620d379..3b725d3a2fe47e547ce5cafd31656a63b80c6e46 100644 (file)
@@ -1 +1,2 @@
-config.inc.php
\ No newline at end of file
+config.inc.php
+.project
\ No newline at end of file
index 0e76bc529f60985928041ad8d06977b5e4086f46..d3139eaed28a4d4f9ca11dcc708fc430a326ab6e 100644 (file)
@@ -57,6 +57,16 @@ $add_settings = array(
     "servers" => array(
         "irc.onlinegamesnet.net 6667"
     ),
+    "channel" => array(
+        "#Krypton"
+    ),
+    "mirccmds" => array(
+        "hosts" => array(
+            "free-bnc.de",
+            "krypton-bouncer.de"
+        ),
+        "groupname" => "KryptonZNC"
+    ),
     "modules" => array(
         "autocycle" => "",
         "awaynick" => "%nick%|ZNC",
index 78937925953b5c40ba70e4cfc35f10bebecceef9..4949a10ccebc7022c73c737ac4b4c6c0877991ca 100644 (file)
@@ -159,7 +159,7 @@ function zncadmin_search() {
     }
     $mask = preg_prepare($mask);
     $table = new Table(5);
-    $table->add("Server", "User", "Clients", "Server", "Nick");
+    $table->add("Server", "User", "Clients", "Server");
     $count = 0;
     foreach($zncservers as $zncserver) {
         $zncserver['conn'] = new ZNCServer($zncserver['host'], $zncserver['port']);
@@ -209,7 +209,7 @@ function zncadmin_search() {
             }
             if($skip) continue;
             if(preg_match("#^".$mask."$#i", $user['user'])) {
-                $table->add($zncserver['name'], $user['user'], $user['clients'], $user['server'], $user['nick']);
+                $table->add($zncserver['name'], $user['user'], $user['clients'], $user['server']);
                 $count++;
             }
         }
@@ -316,6 +316,10 @@ function zncadmin_add() {
     foreach($zncservers as $zncserver) {
         if($argv[3] && (strtolower($argv[3]) != strtolower($zncserver['name']))) continue;
         if(!$argv[3] && $zncserver['priority'] < $priority) continue;
+        if($zncserver['protected'] === true && !$force) {
+            error("Access denied\n");
+            return;
+        }
         $zncserver['conn'] = new ZNCServer($zncserver['host'], $zncserver['port']);
         $zncserver['conn']->login($zncserver['auser'], $zncserver['apass']);
         $zncserver['users'] = $zncserver['conn']->getUserList();
@@ -359,12 +363,15 @@ function zncadmin_add() {
         echo "  Server Host: ".$addserv['public']."  Port: ".$addserv['port']."  SSL Port: ".$addserv['sslport']."\n";
         echo "  Password: ".$password."\n";
         echo "[mIRC]\n";
-        echo " /server -a ".$addserv['public']." -p ".$addserv['port']." -g KryptonZNC -w ".$argv[2].":".$password." -d KryptonZNC\n";
-        echo " /AS addmask *@*.free-bnc.de\n";
-        echo " /AS addmask *@*.krypton-bouncer.de\n";
-        echo " /server -m KryptonZNC\n";
+        echo " /server -a ".$addserv['public']." -p ".$addserv['port']." -g ".$add_settings['mirccmds']['groupname']." -w ".$argv[2].":".$password." -d ".$add_settings['mirccmds']['groupname']."\n";
+        foreach($add_settings['mirccmds']['hosts'] AS $host) {
+            echo " /AS addmask *@*.".$host."\n";
+        }
+        echo " /server -m ".$add_settings['mirccmds']['groupname']."\n";
         echo"/log\n";
-        $addserv['conn']->addChan($argv[2], "#Krypton");
+        foreach($add_settings["channel"] AS $channel) {
+            $addserv['conn']->addChan($argv[2], $channel);
+        }
     } else
         error("Error while adding user.");
 }
@@ -558,8 +565,8 @@ function zncadmin_unblock() {
 //SUBCOMMAND: stats
 function zncadmin_stats() {
     global $argv, $zncservers;
-    $table = new Table(5);
-    $table->add("Server", "Port / SSL Port", "Total ZNC's", "Connected (IRC)", "Online (User)");
+    $table = new Table(6);
+    $table->add("Server", "Port / SSL Port", "Total ZNC's", "Connected (IRC)", "Online (User)", "Protected?");
     $count = 0;
     foreach($zncservers as $zncserver) {
         $total = 0;
@@ -572,7 +579,7 @@ function zncadmin_stats() {
             if($user['clients'] > 0) $online++;
             $total++;
         }
-        $table->add($zncserver['name'], $zncserver['port'].($zncserver['sslport'] ? "/".$zncserver['sslport'] : ""), $total.($zncserver['maxznc'] ? "/".$zncserver['maxznc'] : ""), $connected, $online);
+        $table->add($zncserver['name'], $zncserver['port'].($zncserver['sslport'] ? "/".$zncserver['sslport'] : ""), $total.($zncserver['maxznc'] ? "/".$zncserver['maxznc'] : ""), $connected, $online, ($zncserver['protected'] === true ? "X" : ""));
         $count++;
     }
     if($count) {
index f85dd5607848db4c340006978b1f0ef6e9fd9f0a..8af0194188fc6687affcae0c52db3a3c274382e0 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);
@@ -24,6 +24,7 @@ define("ERR_UNKNOWN_ZNC_VERSION_OR_METHOD", 4);
 
 $zncserver_classes = array(
        "ZNCServerV0.class.php",
+       "ZNCServerV1.class.php",
 );
 
 class ZNCServer {
diff --git a/zncadmin/ZNCServerV1.class.php b/zncadmin/ZNCServerV1.class.php
new file mode 100644 (file)
index 0000000..1cac8e1
--- /dev/null
@@ -0,0 +1,391 @@
+<?php
+/* ZNCServerV1.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/>. 
+ */
+
+$classinfo = array(
+       "name" => "ZNCServerV1",
+       "version" => 1.000,
+);
+
+class ZNCServerV1 {
+       private $host, $port;
+       private $connector;
+       
+       public function ZNCServerV1($host, $port, $connector = NULL) {
+               $this->host = $host;
+               $this->port = $port;
+               if($connector == NULL)
+                       $connector = new HTTPConnector();
+               $this->connector = $connector;
+       }
+
+    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];
+            $userdata['seen_unix'] = strtotime($exp2[0]);
+            $exp2 = explode('</td>', $exp[3]);
+            $userdata['info'] = $exp2[0];
+            $seenlist[] = $userdata;
+        }
+        return $seenlist;
+    }
+
+    /*
+    $settings array elements:
+        nick
+        altnick
+        ident
+    */
+    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('"', $exp[1]);
+        $csrf = $exp[0];
+        $post = array();
+        $post['_CSRF_Check'] = $csrf;
+        $post['submitted'] = '1';
+        //$post['doconnect'] = '1';
+        $post['user'] = $username;
+        $post['password'] = $password;
+        $post['password2'] = $password;
+        $post['nick'] = $settings['nick'];
+        $post['altnick'] = $settings['altnick'];
+        $post['ident'] = $settings['ident'];
+        $post['statusprefix'] = '*';
+        $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/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/global/webadmin/deluser?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['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) {
+            $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];
+            }
+            $name = $cfields['name'];
+            switch($cfields['type']) {
+                case "text":
+                case "hidden":
+                    $value = $cfields['value'];
+                    break;
+                case "checkbox":
+                    if($cfields['checked']) {
+                        $value = ($cfields['value'] ? $cfields['value'] : "1");
+                    } else
+                        $value = null;
+                    break;
+                default:
+                    $value = null;
+            }
+            if($name && $value) {
+                if(array_key_exists($name, $post)) {
+                    if(!is_array($post[$name])) {
+                        $cvalue = $post[$name];
+                        $post[$name] = array();
+                        $post[$name][] = $cvalue;
+                    }
+                    $post[$name][] = $value;
+                } else
+                    $post[$name] = $value;
+            }
+        }
+        //textareas
+        $textareas = explode("<textarea ", $html);
+        for($i = 1; $i < count($textareas); $i++) {
+            $exp = explode(">", $textareas[$i]);
+            $name = explode("name=\"", $exp[0]);
+            $name = explode("\"", $name[1]);
+            $name = $name[0];
+            $content = explode("</textarea", $exp[1]);
+            $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;
+        }
+        if($new_servers) {
+            $post['servers'] = implode("\n", $new_servers);
+        }
+        foreach($add_modules as $name => $args) {
+            $post['loadmod'][] = $name;
+            if($args != "")
+                $post['modargs_'.$name] = $args;
+        }
+        foreach($del_modules as $name => $args) {
+            foreach($post['loadmod'] as $index => $mod) {
+                if(strtolower($mod) == strtolower($name))
+                    unset($post['loadmod'][$index]);
+            }
+        }
+        foreach($others as $name => $value) {
+            $post[$name] = $value;
+        }
+        $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');
+        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['network'] = $username.'/default';
+        $post['send_to'] = ($server ? "server" : "client");
+        $post['line'] = $raw;
+        $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.'&network=default');
+        if(preg_match("/No such username/i", $html)) return false;
+        $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);
+        $exp = explode('"', $exp[1]);
+        $default_key = $exp[0];
+        $post['key'] = ($key ? $key : $default_key);
+        $exp = explode('name="buffercount" value="', $html);
+        $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.'&network=default', $post);
+    }
+       
+}
+
+?>
\ No newline at end of file