2 /******************************* PHP-P10 v2 *****************************
3 * Copyright (C) 2011 Philipp Kreil (pk910) *
5 * This program is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 3 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
18 ************************************************************************
20 * BotLoader/saxdb.class.php
22 * Simple PHP P10 database.
27 const PARSER_ISSTRING = 0x0001;
28 const PARSER_ISESCAPED = 0x0002;
29 const PARSER_ISBLOCK = 0x0004;
30 const PARSER_EXPECT_VALUE = 0x0008;
31 const PARSER_STRING_LIST = 0x0010;
32 private $database = array();
34 public function loadDB($name) {
35 if(file_exists($name)) {
36 $fp = fopen($name, "r");
37 $db = fread($fp, filesize($name));
39 $this->database = $this->parseDB($db);
43 public function writeDB($name) {
44 $fp = fopen($name, "w");
45 $db = $this->serializeDB($this->database);
50 public function getSection($name) {
51 if(array_key_exists($name, $this->database)) {
52 return $this->database[$name];
58 public function setSection($name, $value) {
59 $this->database[$name] = $value;
62 private function parseDB($db) {
68 for($i = 0; $i < strlen($db); $i++) {
69 if(($parserflags & self::PARSER_ISBLOCK) && $db[$i] != "{" && $db[$i] != "}") {
73 if(($parserflags & self::PARSER_ISSTRING) && $db[$i] != "\"") {
77 if($parserflags & self::PARSER_ISESCAPED) {
79 $parserflags &= ~self::PARSER_ISESCAPED;
84 $parserflags |= self::PARSER_ISESCAPED;
87 if($parserflags & self::PARSER_ISSTRING) {
88 $parserflags &= ~self::PARSER_ISSTRING;
89 if($parserflags & self::PARSER_STRING_LIST) {
90 $cache['list'][] = $buffer;
91 } else if($parserflags & self::PARSER_EXPECT_VALUE) {
92 //we've got a full entry
93 $output[$cache['name']] = $buffer;
94 $parserflags &= ~self::PARSER_EXPECT_VALUE;
96 //we've only got the name of the next entry
97 $cache['name'] = $buffer;
98 $parserflags |= self::PARSER_EXPECT_VALUE;
101 $buffer = ""; //clear the buffer
102 $parserflags |= self::PARSER_ISSTRING;
106 //block (it must be a value)
107 if($parserflags & self::PARSER_ISBLOCK) {
111 $parserflags |= self::PARSER_ISBLOCK;
116 if($parserflags & self::PARSER_ISBLOCK) {
118 if($openblocks == -1) {
119 $parserflags &= ~self::PARSER_ISBLOCK;
120 if($parserflags & self::PARSER_EXPECT_VALUE) {
121 $output[$cache['name']] = $this->parseDB($buffer);
122 $parserflags &= ~self::PARSER_EXPECT_VALUE;
130 if(!($parserflags & self::PARSER_STRING_LIST)) {
131 $cache['list'] = array();
132 $parserflags |= self::PARSER_STRING_LIST;
136 if(($parserflags & self::PARSER_STRING_LIST)) {
137 $parserflags &= ~self::PARSER_STRING_LIST;
138 if($parserflags & self::PARSER_EXPECT_VALUE) {
139 $output[$cache['name']] = $cache['list'];
140 $parserflags &= ~self::PARSER_EXPECT_VALUE;
152 private function serializeDB($db) {
154 foreach($db as $name => $value) {
155 if(!is_array($value) && !is_string($value) && !is_numeric($value)) continue;
156 $dbstring .= "\"".str_replace("\"","\\\"", $name)."\"";
157 if(is_array($value)) {
159 $dbstring .= $this->serializeDB($value);
162 $dbstring .= "\"".str_replace("\"","\\\"", $value)."\"";