added saxdb & continued BotLoader
[PHP-P10.git] / BotLoader / saxdb.class.php
diff --git a/BotLoader/saxdb.class.php b/BotLoader/saxdb.class.php
new file mode 100644 (file)
index 0000000..15b36c2
--- /dev/null
@@ -0,0 +1,172 @@
+<?php
+/********************************* PHP-P10 ******************************
+ *    P10 uplink class by pk910   (c)2011 pk910                         *
+ ************************************************************************
+ *                          Version 2 (OOP)                             *
+ *                                                                      *
+ * PHP-P10 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 2 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 PHP-P10; if not, write to the Free Software Foundation,   *
+ * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.       *
+ *                                                                      *
+ ************************************************************************
+ * 
+ *  BotLoader/saxdb.class.php
+ *
+ * Simple PHP P10 database.
+ *
+ */
+
+class saxdb {
+       const PARSER_ISSTRING  = 0x0001;
+       const PARSER_ISESCAPED = 0x0002;
+       const PARSER_ISBLOCK   = 0x0004;
+       const PARSER_EXPECT_VALUE = 0x0008;
+       const PARSER_STRING_LIST  = 0x0010;
+       private $database = array();
+       
+       public function loadDB($name) {
+               if(file_exists($name)) {
+                       $fp = fopen($name, "r");
+                       $db = fread($fp, filesize($name));
+                       fclose($fp);
+                       $this->database = $this->parseDB($db);
+               }
+       }
+       
+       public function writeDB($name) {
+               $fp = fopen($name, "w");
+               $db = $this->serializeDB($this->database);
+               fwrite($fp, $db);
+               fclose($fp);
+       }
+       
+       public function getSection($name) {
+               if(array_key_exists($name, $this->database)) {
+                       return $this->database[$name];
+               } else {
+                       return array();
+               }
+       }
+       
+       public function setSection($name, $value) {
+               $this->database[$name] = $value;
+       }
+       
+       private function parseDB($db) {
+               $parserflags = 0;
+               $openblocks = 0;
+               $buffer = "";
+               $cache = array();
+               $output = array();
+               for($i = 0; $i < strlen($db); $i++) {
+                       if(($parserflags & self::PARSER_ISBLOCK) && $db[$i] != "{" && $db[$i] != "}") {
+                               $buffer .= $db[$i];
+                               continue;
+                       }
+                       if(($parserflags & self::PARSER_ISSTRING) && $db[$i] != "\"") {
+                               $buffer .= $db[$i];
+                               continue;
+                       }
+                       if($parserflags & self::PARSER_ISESCAPED) {
+                               $buffer .= $db[$i];
+                               $parserflags &= ~self::PARSER_ISESCAPED;
+                               continue;
+                       }
+                       switch($db[$i]) {
+                               case "\\":
+                                       $parserflags |= self::PARSER_ISESCAPED;
+                                       break;
+                               case "\"":
+                                       if($parserflags & self::PARSER_ISSTRING) {
+                                               $parserflags &= ~self::PARSER_ISSTRING;
+                                               if($parserflags & self::PARSER_STRING_LIST) {
+                                                       $cache['list'][] = $buffer;
+                                               } else if($parserflags & self::PARSER_EXPECT_VALUE) {
+                                                       //we've got a full entry
+                                                       $output[$cache['name']] = $buffer;
+                                                       $parserflags &= ~self::PARSER_EXPECT_VALUE;
+                                               } else {
+                                                       //we've only got the name of the next entry
+                                                       $cache['name'] = $buffer;
+                                                       $parserflags |= self::PARSER_EXPECT_VALUE;
+                                               }
+                                       } else {
+                                               $buffer = ""; //clear the buffer
+                                               $parserflags |= self::PARSER_ISSTRING;
+                                       }
+                                       break;
+                               case "{":
+                                       //block (it must be a value)
+                                       if($parserflags & self::PARSER_ISBLOCK) {
+                                               $openblocks++;
+                                               $buffer .= $db[$i];
+                                       } else {
+                                               $parserflags |= self::PARSER_ISBLOCK;
+                                               $buffer = "";
+                                       }
+                                       break;
+                               case "}":
+                                       if($parserflags & self::PARSER_ISBLOCK) {
+                                               $openblocks--;
+                                               if($openblocks == -1) {
+                                                       $parserflags &= ~self::PARSER_ISBLOCK;
+                                                       if($parserflags & self::PARSER_EXPECT_VALUE) {
+                                                               $output[$cache['name']] = $this->parseDB($buffer);
+                                                               $parserflags &= ~self::PARSER_EXPECT_VALUE;
+                                                       }
+                                               } else {
+                                                       $buffer .= $db[$i];
+                                               }
+                                       }
+                                       break;
+                               case "(":
+                                       if(!($parserflags & self::PARSER_STRING_LIST)) {
+                                               $cache['list'] = array();
+                                               $parserflags |= self::PARSER_STRING_LIST;
+                                       }
+                                       break;
+                               case ")":
+                                       if(($parserflags & self::PARSER_STRING_LIST)) {
+                                               $parserflags &= ~self::PARSER_STRING_LIST;
+                                               if($parserflags & self::PARSER_EXPECT_VALUE) {
+                                                       $output[$cache['name']] = $cache['list'];
+                                                       $parserflags &= ~self::PARSER_EXPECT_VALUE;
+                                               }
+                                       }
+                                       break;
+                               default:
+                                       $buffer .= $db[$i];
+                                       break;
+                       }
+               }
+               return $output;
+       }
+       
+       private function serializeDB($db, $dbstring = "") {
+               foreach($db as $name => $value) {
+                       if(!is_array($value) && !is_string($value) && !is_numeric($value)) continue;
+                       $dbstring .= "\"".str_replace("\"","\\\"", $name)."\"";
+                       if(is_array($value)) {
+                               $dbstring .= "{";
+                               $dbstring .= $this->serializeDB($value, $dbstring);
+                               $dbstring .= "}";
+                       } else {
+                               $dbstring .= "\"".str_replace("\"","\\\"", $value)."\"";
+                       }
+               }
+               return $dbstring;
+       }
+       
+}
+
+?>
\ No newline at end of file