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 public function getSection($name) {
54 if(array_key_exists($name, $this->database)) {
55 return $this->database[$name];
61 public function setSection($name, $value) {
62 $this->database[$name] = $value;
65 private function parseDB($db) {
71 for($i = 0; $i < strlen($db); $i++) {
72 if(($parserflags & self::PARSER_ISBLOCK) && $db[$i] != "{" && $db[$i] != "}") {
76 if(($parserflags & self::PARSER_ISSTRING) && $db[$i] != "\"") {
80 if($parserflags & self::PARSER_ISESCAPED) {
82 $parserflags &= ~self::PARSER_ISESCAPED;
87 $parserflags |= self::PARSER_ISESCAPED;
90 if($parserflags & self::PARSER_ISSTRING) {
91 $parserflags &= ~self::PARSER_ISSTRING;
92 if($parserflags & self::PARSER_STRING_LIST) {
93 $cache['list'][] = $buffer;
94 } else if($parserflags & self::PARSER_EXPECT_VALUE) {
95 //we've got a full entry
96 $output[$cache['name']] = $buffer;
97 $parserflags &= ~self::PARSER_EXPECT_VALUE;
99 //we've only got the name of the next entry
100 $cache['name'] = $buffer;
101 $parserflags |= self::PARSER_EXPECT_VALUE;
104 $buffer = ""; //clear the buffer
105 $parserflags |= self::PARSER_ISSTRING;
109 //block (it must be a value)
110 if($parserflags & self::PARSER_ISBLOCK) {
114 $parserflags |= self::PARSER_ISBLOCK;
119 if($parserflags & self::PARSER_ISBLOCK) {
121 if($openblocks == -1) {
122 $parserflags &= ~self::PARSER_ISBLOCK;
123 if($parserflags & self::PARSER_EXPECT_VALUE) {
124 $output[$cache['name']] = $this->parseDB($buffer);
125 $parserflags &= ~self::PARSER_EXPECT_VALUE;
133 if(!($parserflags & self::PARSER_STRING_LIST)) {
134 $cache['list'] = array();
135 $parserflags |= self::PARSER_STRING_LIST;
139 if(($parserflags & self::PARSER_STRING_LIST)) {
140 $parserflags &= ~self::PARSER_STRING_LIST;
141 if($parserflags & self::PARSER_EXPECT_VALUE) {
142 $output[$cache['name']] = $cache['list'];
143 $parserflags &= ~self::PARSER_EXPECT_VALUE;
155 private function serializeDB($db, $dbstring = "") {
156 foreach($db as $name => $value) {
157 if(!is_array($value) && !is_string($value) && !is_numeric($value)) continue;
158 $dbstring .= "\"".str_replace("\"","\\\"", $name)."\"";
159 if(is_array($value)) {
161 $dbstring .= $this->serializeDB($value, $dbstring);
164 $dbstring .= "\"".str_replace("\"","\\\"", $value)."\"";