]>
Commit | Line | Data |
---|---|---|
14c04a50 MB |
1 | <pre> |
2 | <?php | |
3 | /* | |
4 | This is a php function & demo util which parses the x3.db and x3.conf files for use in web programs. | |
5 | ||
6 | Written by Jobe: | |
7 | <Jobe> reads in the DB, parses it then print_r's it | |
8 | <Jobe> if it comes across a syntax it doesnt know it stops parsing and puts the remaining data, all of it in ['parserror'] in whatever array its working in | |
9 | */ | |
10 | ||
11 | $conf['dbfile'] = "x3.db"; | |
12 | ||
13 | $dh = fopen($conf['dbfile'], "r"); | |
14 | ||
15 | $contents = fread($dh, filesize($conf['dbfile'])); | |
16 | ||
17 | fclose($dh); | |
18 | ||
19 | function parse_data ($data = "") { | |
20 | static $passback = ""; | |
21 | $working = $data; | |
22 | $return = array(); | |
23 | $loop = true; | |
24 | ||
25 | if ( $data != "" ) { | |
26 | while ( $loop ) { | |
27 | if ( preg_match("/^\s*\/\/([^\n\r]*)[\n\r]*(.*)\$/s", $working, $matches) ) { | |
28 | /* ignore // comments */ | |
29 | $working = $matches[2]; | |
30 | } else if ( preg_match("/^\s*#([^\n\r]*)[\n\r]*(.*)\$/s", $working, $matches) ) { | |
31 | /* ignore # comments */ | |
32 | $working = $matches[2]; | |
33 | } else if ( preg_match("/^\s*\/\*(.*?)\*\/\s*(.*)\$/s", $working, $matches) ) { | |
34 | // ignore /* */ comments | |
35 | $working = $matches[2]; | |
36 | } else if ( preg_match("/^\s*\}\s*;\s*(.*)\$/s", $working, $matches) ) { | |
37 | /* section end */ | |
38 | $passback = $matches[1]; | |
39 | $loop = false; | |
40 | } else if ( preg_match("/^\s*((\"(((\\\\\")|[^\"])+)\")|([^\s]+))\s*\{\s*(.*)\$/s", $working, $matches) ) { | |
41 | /* section start (name quoted) */ | |
42 | if ( $matches[3] != "" ) { | |
43 | $return[strtolower($matches[3])] = parse_data($matches[7]); | |
44 | } else { | |
45 | $return[strtolower($matches[1])] = parse_data($matches[7]); | |
46 | } | |
47 | $working = $passback; | |
48 | } else if ( preg_match("/^\s*((\"(((\\\\\")|[^\"])+)\")|([^\s]+))\s*\(\s*((((\"(((\\\\\")|[^\"])+)\")|([^\s,]+))\s*(,\s*((\"(((\\\\\")|[^\"])+)\")|([^\s,]+))\s*)*)?)\s*\)\s*;\s*(.*)\$/s", $working, $matches) ) { | |
49 | /* array */ | |
50 | $arraycontents = $matches[7]; | |
51 | $array = array(); | |
52 | while ( preg_match("/[^\s]+/", $arraycontents) ) { | |
53 | preg_match("/^\s*,?\s*((\"(((\\\\\")|[^\"])+)\")|([^\s,]+))\s*(.*)/s", $arraycontents, $arraymatches); | |
54 | if ( $arraymatches[3] != "" ) { | |
55 | $array[] = $arraymatches[3]; | |
56 | } else { | |
57 | $array[] = $arraymatches[1]; | |
58 | } | |
59 | $arraycontents = $arraymatches[7]; | |
60 | } | |
61 | if ( $matches[3] != "" ) { | |
62 | $return[strtolower($matches[3])] = $array; | |
63 | } else { | |
64 | $return[strtolower($matches[1])] = $array; | |
65 | } | |
66 | $working = $matches[22]; | |
67 | } else if ( preg_match("/^\s*((\"(((\\\\\")|[^\"])+)\")|([^\s,]+))\s*(((\"(((\\\\\")|[^\"])+)\")|([^\s,]+)))?\s*;\s*(.*)\$/s", $working, $matches) ) { | |
68 | /* name value pair */ | |
69 | if ( $matches[3] != "" ) { | |
70 | $key = strtolower($matches[3]); | |
71 | } else { | |
72 | $key = strtolower($matches[1]); | |
73 | } | |
74 | if ( $matches[7] != "" ) { | |
75 | if ( $matches[10] != "" ) { | |
76 | $val = $matches[10]; | |
77 | } else { | |
78 | $val = $matches[7]; | |
79 | } | |
80 | if ( isset($return[$key]) ) { | |
81 | if ( !is_array($return[$key]) ) { | |
82 | $temp = $return[$key]; | |
83 | unset($return[$key]); | |
84 | $return[$key][] = $temp; | |
85 | $return[$key][] = $val; | |
86 | } else { | |
87 | $return[$key][] = $val; | |
88 | } | |
89 | } else { | |
90 | $return[$key] = $val; | |
91 | } | |
92 | } else { | |
93 | $return[$key] = array(); | |
94 | } | |
95 | $working = $matches[14]; | |
96 | } else { | |
97 | if ( $working != "" ) { | |
98 | $return['parseerror'] = $working; | |
99 | } | |
100 | $passback = ""; | |
101 | $loop = false; | |
102 | } | |
103 | } | |
104 | } | |
105 | ||
106 | return $return; | |
107 | } | |
108 | ||
109 | $db = parse_data($contents); | |
110 | ||
111 | print_r($db); | |
112 | ||
113 | ?> | |
114 | </pre> | |
115 |