2 /********************************* PHP-P10 ******************************
3 * P10 uplink class by pk910 (c)2011 pk910 *
4 ************************************************************************
7 * PHP-P10 is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
17 * You should have received a copy of the GNU General Public License *
18 * along with PHP-P10; if not, write to the Free Software Foundation, *
19 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
21 ************************************************************************
23 * BotLoader/saxdb.class.php
25 * Simple PHP P10 database.
30 const PARSER_ISSTRING = 0x0001;
31 const PARSER_ISESCAPED = 0x0002;
32 const PARSER_ISBLOCK = 0x0004;
33 const PARSER_EXPECT_VALUE = 0x0008;
34 const PARSER_STRING_LIST = 0x0010;
35 private $database = array();
37 public function loadDB($name) {
38 if(file_exists($name)) {
39 $fp = fopen($name, "r");
40 $db = fread($fp, filesize($name));
42 $this->database = $this->parseDB($db);
46 public function writeDB($name) {
47 $fp = fopen($name, "w");
48 $db = $this->serializeDB($this->database);
53 private function parseDB($db) {
59 for($i = 0; $i < strlen($db); $i++) {
60 if(($parserflags & self::PARSER_ISBLOCK) && $db[$i] != "{" && $db[$i] != "}") {
64 if(($parserflags & self::PARSER_ISSTRING) && $db[$i] != "\"") {
68 if($parserflags & self::PARSER_ISESCAPED) {
70 $parserflags &= ~self::PARSER_ISESCAPED;
75 $parserflags |= self::PARSER_ISESCAPED;
78 if($parserflags & self::PARSER_ISSTRING) {
79 $parserflags &= ~self::PARSER_ISSTRING;
80 if($parserflags & self::PARSER_STRING_LIST) {
81 $cache['list'][] = $buffer;
82 } else if($parserflags & self::PARSER_EXPECT_VALUE) {
83 //we've got a full entry
84 $output[$cache['name']] = $buffer;
85 $parserflags &= ~self::PARSER_EXPECT_VALUE;
87 //we've only got the name of the next entry
88 $cache['name'] = $buffer;
89 $parserflags |= self::PARSER_EXPECT_VALUE;
92 $buffer = ""; //clear the buffer
93 $parserflags |= self::PARSER_ISSTRING;
97 //block (it must be a value)
98 if($parserflags & self::PARSER_ISBLOCK) {
102 $parserflags |= self::PARSER_ISBLOCK;
107 if($parserflags & self::PARSER_ISBLOCK) {
109 if($openblocks == -1) {
110 $parserflags &= ~self::PARSER_ISBLOCK;
111 if($parserflags & self::PARSER_EXPECT_VALUE) {
112 $output[$cache['name']] = $this->parseDB($buffer);
113 $parserflags &= ~self::PARSER_EXPECT_VALUE;
121 if(!($parserflags & self::PARSER_STRING_LIST)) {
122 $cache['list'] = array();
123 $parserflags |= self::PARSER_STRING_LIST;
127 if(($parserflags & self::PARSER_STRING_LIST)) {
128 $parserflags &= ~self::PARSER_STRING_LIST;
129 if($parserflags & self::PARSER_EXPECT_VALUE) {
130 $output[$cache['name']] = $cache['list'];
131 $parserflags &= ~self::PARSER_EXPECT_VALUE;
143 private function serializeDB($db, $dbstring = "") {
144 foreach($db as $name => $value) {
145 if(!is_array($value) && !is_string($value) && !is_numeric($value)) continue;
146 $dbstring .= "\"".str_replace("\"","\\\"", $name)."\"";
147 if(is_array($value)) {
149 $dbstring .= $this->serializeDB($value, $dbstring);
152 $dbstring .= "\"".str_replace("\"","\\\"", $value)."\"";