added saxdb & continued BotLoader
[PHP-P10.git] / BotLoader / saxdb.php
1 <?php
2 /********************************* PHP-P10 ******************************
3  *    P10 uplink class by pk910   (c)2011 pk910                         *
4  ************************************************************************
5  *                          Version 2 (OOP)                             *
6  *                                                                      *
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.                                  *
11  *                                                                      *
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.                         *
16  *                                                                      *
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.       *
20  *                                                                      *
21  ************************************************************************
22  * 
23  *  BotLoader/saxdb.class.php
24  *
25  * Simple PHP P10 database.
26  *
27  */
28
29 class saxdb {
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();
36         
37         public function loadDB($name) {
38                 if(file_exists($name)) {
39                         $fp = fopen($name, "r");
40                         $db = fread($fp, filesize($name));
41                         fclose($fp);
42                         $this->database = $this->parseDB($db);
43                 }
44         }
45         
46         public function writeDB($name) {
47                 $fp = fopen($name, "w");
48                 $db = $this->serializeDB($this->database);
49                 fwrite($fp, $db);
50                 fclose($fp);
51         }
52         
53         private function parseDB($db) {
54                 $parserflags = 0;
55                 $openblocks = 0;
56                 $buffer = "";
57                 $cache = array();
58                 $output = array();
59                 for($i = 0; $i < strlen($db); $i++) {
60                         if(($parserflags & self::PARSER_ISBLOCK) && $db[$i] != "{" && $db[$i] != "}") {
61                                 $buffer .= $db[$i];
62                                 continue;
63                         }
64                         if(($parserflags & self::PARSER_ISSTRING) && $db[$i] != "\"") {
65                                 $buffer .= $db[$i];
66                                 continue;
67                         }
68                         if($parserflags & self::PARSER_ISESCAPED) {
69                                 $buffer .= $db[$i];
70                                 $parserflags &= ~self::PARSER_ISESCAPED;
71                                 continue;
72                         }
73                         switch($db[$i]) {
74                                 case "\\":
75                                         $parserflags |= self::PARSER_ISESCAPED;
76                                         break;
77                                 case "\"":
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;
86                                                 } else {
87                                                         //we've only got the name of the next entry
88                                                         $cache['name'] = $buffer;
89                                                         $parserflags |= self::PARSER_EXPECT_VALUE;
90                                                 }
91                                         } else {
92                                                 $buffer = ""; //clear the buffer
93                                                 $parserflags |= self::PARSER_ISSTRING;
94                                         }
95                                         break;
96                                 case "{":
97                                         //block (it must be a value)
98                                         if($parserflags & self::PARSER_ISBLOCK) {
99                                                 $openblocks++;
100                                                 $buffer .= $db[$i];
101                                         } else {
102                                                 $parserflags |= self::PARSER_ISBLOCK;
103                                                 $buffer = "";
104                                         }
105                                         break;
106                                 case "}":
107                                         if($parserflags & self::PARSER_ISBLOCK) {
108                                                 $openblocks--;
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;
114                                                         }
115                                                 } else {
116                                                         $buffer .= $db[$i];
117                                                 }
118                                         }
119                                         break;
120                                 case "(":
121                                         if(!($parserflags & self::PARSER_STRING_LIST)) {
122                                                 $cache['list'] = array();
123                                                 $parserflags |= self::PARSER_STRING_LIST;
124                                         }
125                                         break;
126                                 case ")":
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;
132                                                 }
133                                         }
134                                         break;
135                                 default:
136                                         $buffer .= $db[$i];
137                                         break;
138                         }
139                 }
140                 return $output;
141         }
142         
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)) {
148                                 $dbstring .= "{";
149                                 $dbstring .= $this->serializeDB($value, $dbstring);
150                                 $dbstring .= "}";
151                         } else {
152                                 $dbstring .= "\"".str_replace("\"","\\\"", $value)."\"";
153                         }
154                 }
155                 return $dbstring;
156         }
157         
158 }
159
160 ?>