e3e5413dd634fa114c483492fe216f2aea2502b2
[PHP-P10.git] / BotLoader / saxdb.class.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         public function getSection($name) {
54                 if(array_key_exists($name, $this->database)) {
55                         return $this->database[$name];
56                 } else {
57                         return array();
58                 }
59         }
60         
61         public function setSection($name, $value) {
62                 $this->database[$name] = $value;
63         }
64         
65         private function parseDB($db) {
66                 $parserflags = 0;
67                 $openblocks = 0;
68                 $buffer = "";
69                 $cache = array();
70                 $output = array();
71                 for($i = 0; $i < strlen($db); $i++) {
72                         if(($parserflags & self::PARSER_ISBLOCK) && $db[$i] != "{" && $db[$i] != "}") {
73                                 $buffer .= $db[$i];
74                                 continue;
75                         }
76                         if(($parserflags & self::PARSER_ISSTRING) && $db[$i] != "\"") {
77                                 $buffer .= $db[$i];
78                                 continue;
79                         }
80                         if($parserflags & self::PARSER_ISESCAPED) {
81                                 $buffer .= $db[$i];
82                                 $parserflags &= ~self::PARSER_ISESCAPED;
83                                 continue;
84                         }
85                         switch($db[$i]) {
86                                 case "\\":
87                                         $parserflags |= self::PARSER_ISESCAPED;
88                                         break;
89                                 case "\"":
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;
98                                                 } else {
99                                                         //we've only got the name of the next entry
100                                                         $cache['name'] = $buffer;
101                                                         $parserflags |= self::PARSER_EXPECT_VALUE;
102                                                 }
103                                         } else {
104                                                 $buffer = ""; //clear the buffer
105                                                 $parserflags |= self::PARSER_ISSTRING;
106                                         }
107                                         break;
108                                 case "{":
109                                         //block (it must be a value)
110                                         if($parserflags & self::PARSER_ISBLOCK) {
111                                                 $openblocks++;
112                                                 $buffer .= $db[$i];
113                                         } else {
114                                                 $parserflags |= self::PARSER_ISBLOCK;
115                                                 $buffer = "";
116                                         }
117                                         break;
118                                 case "}":
119                                         if($parserflags & self::PARSER_ISBLOCK) {
120                                                 $openblocks--;
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;
126                                                         }
127                                                 } else {
128                                                         $buffer .= $db[$i];
129                                                 }
130                                         }
131                                         break;
132                                 case "(":
133                                         if(!($parserflags & self::PARSER_STRING_LIST)) {
134                                                 $cache['list'] = array();
135                                                 $parserflags |= self::PARSER_STRING_LIST;
136                                         }
137                                         break;
138                                 case ")":
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;
144                                                 }
145                                         }
146                                         break;
147                                 default:
148                                         $buffer .= $db[$i];
149                                         break;
150                         }
151                 }
152                 return $output;
153         }
154         
155         private function serializeDB($db) {
156                 $dbstring = "";
157                 foreach($db as $name => $value) {
158                         if(!is_array($value) && !is_string($value) && !is_numeric($value)) continue;
159                         $dbstring .= "\"".str_replace("\"","\\\"", $name)."\"";
160                         if(is_array($value)) {
161                                 $dbstring .= "{";
162                                 $dbstring .= $this->serializeDB($value);
163                                 $dbstring .= "}";
164                         } else {
165                                 $dbstring .= "\"".str_replace("\"","\\\"", $value)."\"";
166                         }
167                 }
168                 return $dbstring;
169         }
170         
171 }
172
173 ?>