added QServer for external cache access
[NeonServV5.git] / QServer / NeonServ_QServer.class.php
diff --git a/QServer/NeonServ_QServer.class.php b/QServer/NeonServ_QServer.class.php
new file mode 100644 (file)
index 0000000..e3fb4b0
--- /dev/null
@@ -0,0 +1,150 @@
+<?php
+/* NeonServ_QServer.class.php - NeonServ v5
+ * 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/>. 
+ */
+
+class NeonServ_QServer {
+    private $admin, $socket, $numeric;
+    
+    function NeonServ_QServer() {
+        $this->admin['pass']='*'; // QServer Passwort
+        $this->admin['host']='127.0.0.1';
+        $this->admin['port']=7499;
+    }
+    
+    public function disconnect() {
+        fclose($this->socket);
+        $this->socket=false;
+    }
+    
+    public function connect() {
+        if($this->socket) {
+            $this->disconnect();
+        }
+        $this->socket=@fsockopen($this->admin['host'], $this->admin['port'], $errno, $errstr, 3);
+        if ($this->socket) {
+            stream_set_timeout($this->socket, 2);
+            fputs($this->socket, "A ".$this->admin['pass']."\n");
+            $data = $this->parseLine(@fgets($this->socket));
+            if($data[0] == "A")
+                return true;
+            $this->disconnect(); //not logged in...
+            return false;
+        }
+    }
+    
+    public function connected() {
+        if($this->socket) return true;
+        return false;
+    }
+    
+    private function parseLine($line) {
+        $line = str_replace(array("\r", "\n"), array("", ""), $line);
+        $highExplode = explode(" :", $line, 2);
+        $tokens = explode(" ", $highExplode[0]);
+        if(count($highExplode) > 1)
+            $tokens[] = $highExplode[1];
+        return $tokens;
+    }
+    
+    /* Get IRC User Information
+    * returns array: nick, ident, host, auth, realname
+    */
+    public function getUser($nick) {
+        fputs($this->socket, "U ".$nick."\n");
+        $data = $this->parseLine(@fgets($this->socket));
+        if($data[0] != "U") return NULL;
+        if($data[1] == "0") return NULL; //User not found
+        $user = array();
+        $user['nick'] = $data[2];
+        $user['ident'] = $data[3];
+        $user['host'] = $data[4];
+        $user['auth'] = ($data[5] == "0" ? NULL : $data[5]);
+        $user['realname'] = $data[6];
+        return $user;
+    }
+    
+    /* Get IRC Channel Information
+    * returns array: name, usercount, modes, topic
+    */
+    public function getChannel($name) {
+        fputs($this->socket, "C ".$name."\n");
+        $data = $this->parseLine(@fgets($this->socket));
+        if($data[0] != "C") return NULL;
+        if($data[1] == "0") return NULL; //Channel not found
+        $chan = array();
+        $chan['name'] = $data[2];
+        $chan['usercount'] = $data[3];
+        $chan['modes'] = implode(" ", array_slice($data, 4, (count($data) - 5)));
+        $chan['topic'] = $data[count($data)-1];
+        return $chan;
+    }
+    
+    /* Get All IRC Channels
+    * returns array with subarrays: name, usercount, modes, topic
+    */
+    public function getChannels() {
+        fputs($this->socket, "AC\n");
+        $channels = array();
+        while(true) {
+            $data = $this->parseLine(@fgets($this->socket));
+            if($data[0] == "E") return NULL;
+            if($data[0] == "ACE") break;
+            if($data[0] == "AC") {
+                $chan = array();
+                $chan['name'] = $data[1];
+                $chan['usercount'] = $data[2];
+                $chan['modes'] = implode(" ", array_slice($data, 3, (count($data) - 4)));
+                $chan['topic'] = $data[count($data)-1];
+                $channels[] = $chan;
+            }
+        }
+        return $channels;
+    }
+    
+    /* Get IRC Channel Users
+    * returns array with subarrays: nick, auth, flags
+    */
+    public function getChannelUsers($name, $invisibles = false) {
+        fputs($this->socket, "ACU ".$name." ".($invisibles ? "1" : "0")."\n");
+        $chanusers = array();
+        while(true) {
+            $data = $this->parseLine(@fgets($this->socket));
+            if($data[0] == "E") return NULL;
+            if($data[0] == "ACUE") break;
+            if($data[0] == "ACU") {
+                $chanuser = array();
+                $chanuser['nick'] = $data[1];
+                $chanuser['auth'] = ($data[2] == "0" ? NULL : $data[2]);
+                $chanuser['flags'] = $data[3];
+                $chanusers[] = $chanuser;
+            }
+        }
+        return $chanusers;
+    }
+    
+    /* send IRC RAW
+    * returns true / false
+    */
+    public function sendRAW($class, $raw, $classIsNick = false) {
+        fputs($this->socket, "R ".($classIsNick ? "1" : "0")." ".$class." :".$raw."\n");
+        $data = $this->parseLine(@fgets($this->socket));
+        if($data[0] == "R" && $data[1] == "1") return true;
+        return false;
+    }
+}
+
+?>