]> jfr.im git - irc/evilnet/x3.git/blame - tools/db.php
Fix for issue with SASL account names
[irc/evilnet/x3.git] / tools / db.php
CommitLineData
14c04a50 1<?php
2/*
55a256b7 3 This is a php class & demo util which parses the x3.db and x3.conf files for use in web programs.
14c04a50 4
5 Written by Jobe:
6<Jobe> reads in the DB, parses it then print_r's it
7<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
14c04a50 8
55a256b7
MB
9See bottom of this file for examples of how to use this class
10*/
14c04a50 11
55a256b7
MB
12class X3SaxDB {
13 public $data;
14 private $raw = "";
15
16 function X3SaxDB($filename = "") {
17 $this->data = Array();
18 if ($filename != "") {
19 $this->parse($filename);
20 }
21 }
22
23 function parse($raw = "") {
24 $temp = Array();
25 $temparray = Array();
26 $key = "";
27 $array = false;
28
29 if ($raw != "") {
30 $this->raw = $raw;
31 }
32
33 while ($this->raw != "") {
34 if (preg_match("/^[\s\r\n]*#[^\r\n]*[\r\n]+[\s\r\n]*/s", $this->raw, $matches)) {
35 // Remove "#<stuff>" comments
36 $this->raw = substr($this->raw, strLen($matches[0]));
37 } elseif (preg_match("/^[\s\r\n]*\/\/[^\r\n]*[\r\n]+[\s\r\n]*/s", $this->raw, $matches)) {
38 // Remove "//<stuff>" comments
39 $this->raw = substr($this->raw, strLen($matches[0]));
40 } elseif (preg_match("/^\/\*.*?\*\/[\r\n]+[\s\r\n]*/s", $this->raw, $matches)) {
41 // Remove "/*<stuff>*/" comments
42 $this->raw = substr($this->raw, strLen($matches[0]));
43 } elseif (preg_match("/^\}[\s\r\n]*;[\s\r\n]*/s", $this->raw, $matches)) {
44 // Block End
45 $this->raw = substr($this->raw, strLen($matches[0]));
46 break;
47 } elseif (preg_match("/^\)[\s\r\n]*;[\s\r\n]*/s", $this->raw, $matches)) {
48 // Array End
49 $this->raw = substr($this->raw, strLen($matches[0]));
50 $temp[$key][] = $temparray;
51 $temparray = Array();
52 $array = false;
53 $key = "";
54 } elseif (($key != "") and preg_match("/^\([\s\r\n]*/s", $this->raw, $matches)) {
55 // Array Start
56 $this->raw = substr($this->raw, strLen($matches[0]));
57 $array = true;
58 } elseif (($key != "") and preg_match("/^\{[\s\r\n]*/s", $this->raw, $matches)) {
59 // Block
60 $this->raw = substr($this->raw, strLen($matches[0]));
61 $temp[$key][] = $this->parse();
62 $key = "";
63 } elseif ($array and ($key != "") and preg_match("/^(?:(?:\"(.*?)(?<!\\\\)\")|(?:([^\s]*)))[\s\r\n]*,?[\s\r\n]*/s", $this->raw, $matches)) {
64 // Array Value
65 $this->raw = substr($this->raw, strLen($matches[0]));
66 $val = $matches[1];
67 if ($val == "") {
68 $val = $matches[2];
69 }
70 $temparray[] = stripslashes($val);
71 } elseif (($key != "") and preg_match("/^(?:=[\s\r\n]*)?(?:(?:\"(.*?)(?<!\\\\)\")|(?:([^\s]*)))[\s\r\n]*;[\s\r\n]*/s", $this->raw, $matches)) {
72 // Value
73 $this->raw = substr($this->raw, strLen($matches[0]));
74 $val = $matches[1];
75 if ($val == "") {
76 $val = $matches[2];
77 }
78 $temp[$key][] = stripslashes($val);
79 $key = "";
80 } elseif (preg_match("/^(?:(?:\"(.+?)(?<!\\\\)\")|(?:([^\s]*)))[\s\r\n]*/s", $this->raw, $matches)) {
81 // Key
82 $this->raw = substr($this->raw, strLen($matches[0]));
83 $key = $matches[1];
84 if ($key == "") {
85 $key = $matches[2];
86 }
87 } else {
88 // Error
89 $temp["parse_error"] = $this->raw;
90 $this->raw = "";
91 break;
92 }
93 }
94
95 foreach (array_keys($temp) as $key) {
96 if (count($temp[$key]) == 1) {
97 $temp[$key] = $temp[$key][0];
98 }
99 }
100
101 if ($raw != "") {
102 $this->data = $temp;
103 return $this->data;
104 } else {
105 return $temp;
106 }
107 }
108
109 function parsefile($filename = "") {
110
111 if ($filename != "") {
112 if (file_exists($filename) and is_readable($filename)) {
113 $this->raw = file_get_contents($filename);
114 }
115 }
116
117 $this->parse($this->raw);
118 return $this->data;
119 }
120
121 function getval($path, $array = null) {
122 $temp = $path;
123 $parts = Array();
124 $ret = Array();
125
126 if (is_null($array)) {
127 $ret = $this->data;
128 } else {
129 $ret = $array;
130 }
131
132 if (substr($temp, 0, 1) == "/") {
133 $temp = substr($temp, 1);
134 }
135 if (substr($temp, -1) != "/") {
136 $temp = $temp . "/";
137 }
138
139 while ($temp != "") {
140 if (preg_match("/(?:(?:\"(.*?)(?<!\\\\)\")|(?:([^\/\r\n\s]*)))\//s", $temp, $matches)) {
141 $temp = substr($temp, strLen($matches[0]));
142 if ($matches[1] != "") {
143 $parts[] = $matches[1];
144 } else {
145 $parts[] = $matches[2];
146 }
147 } else {
148 $parts['error'] = $temp;
149 break;
150 }
151 }
152
153 for ($i=0; $i<count($parts); $i++) {
154 $found = false;
155 if (!is_array($ret)) { unset($ret); break; }
156 foreach (array_keys($ret) as $key) {
157 if (strtolower($key) == strtolower($parts[$i])) {
158 $parts[$i] = $key;
159 $found = true;
160 }
161 }
162 if (!$found) { unset($ret); break; }
163 $ret = $ret[$parts[$i]];
164 if (($i < count($parts) - 1) and isset($ret[0])) {
165 $ret = $ret[0];
166 }
167 }
168
169 return $ret;
170 }
14c04a50 171}
172
55a256b7
MB
173$x3db = new X3SaxDB();
174$data = $x3db->parsefile("data/x3.db");
175// $data == copy of $x3db->data
176var_dump($x3db->data);
177var_dump($x3db->getval("/NickServ/Jobe/email_addr"));
14c04a50 178?>