]>
jfr.im git - irc/evilnet/x3.git/blob - tools/db.php
3 This is a php class & demo util which parses the x3.db and x3.conf files for use in web programs.
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
9 See bottom of this file for examples of how to use this class
16 function X3SaxDB ( $filename = "" ) {
17 $this- > data
= Array ();
18 if ( $filename != "" ) {
19 $this- > parse ( $filename );
23 function parse ( $raw = "" ) {
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 )) {
45 $this- > raw
= substr ( $this- > raw
, strLen ( $matches [ 0 ]));
47 } elseif ( preg_match ( "/^\)[\s \r\n ]*;[\s \r\n ]*/s" , $this- > raw
, $matches )) {
49 $this- > raw
= substr ( $this- > raw
, strLen ( $matches [ 0 ]));
50 $temp [ $key ][] = $temparray ;
54 } elseif (( $key != "" ) and preg_match ( "/^\([\s \r\n ]*/s" , $this- > raw
, $matches )) {
56 $this- > raw
= substr ( $this- > raw
, strLen ( $matches [ 0 ]));
58 } elseif (( $key != "" ) and preg_match ( "/^\{[\s \r\n ]*/s" , $this- > raw
, $matches )) {
60 $this- > raw
= substr ( $this- > raw
, strLen ( $matches [ 0 ]));
61 $temp [ $key ][] = $this- > parse ();
63 } elseif ( $array and ( $key != "" ) and preg_match ( "/^(?:(?: \" (.*?)(?<! \\\\ ) \" )|(?:([^\s]*)))[\s \r\n ]*,?[\s \r\n ]*/s" , $this- > raw
, $matches )) {
65 $this- > raw
= substr ( $this- > raw
, strLen ( $matches [ 0 ]));
70 $temparray [] = stripslashes ( $val );
71 } elseif (( $key != "" ) and preg_match ( "/^(?:=[\s \r\n ]*)?(?:(?: \" (.*?)(?<! \\\\ ) \" )|(?:([^\s]*)))[\s \r\n ]*;[\s \r\n ]*/s" , $this- > raw
, $matches )) {
73 $this- > raw
= substr ( $this- > raw
, strLen ( $matches [ 0 ]));
78 $temp [ $key ][] = stripslashes ( $val );
80 } elseif ( preg_match ( "/^(?:(?: \" (.+?)(?<! \\\\ ) \" )|(?:([^\s]*)))[\s \r\n ]*/s" , $this- > raw
, $matches )) {
82 $this- > raw
= substr ( $this- > raw
, strLen ( $matches [ 0 ]));
89 $temp [ "parse_error" ] = $this- > raw
;
95 foreach ( array_keys ( $temp ) as $key ) {
96 if ( count ( $temp [ $key ]) == 1 ) {
97 $temp [ $key ] = $temp [ $key ][ 0 ];
109 function parsefile ( $filename = "" ) {
111 if ( $filename != "" ) {
112 if ( file_exists ( $filename ) and is_readable ( $filename )) {
113 $this- > raw
= file_get_contents ( $filename );
117 $this- > parse ( $this- > raw
);
121 function getval ( $path , $array = null ) {
126 if ( is_null ( $array )) {
132 if ( substr ( $temp , 0 , 1 ) == "/" ) {
133 $temp = substr ( $temp , 1 );
135 if ( substr ( $temp , - 1 ) != "/" ) {
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 ];
145 $parts [] = $matches [ 2 ];
148 $parts [ 'error' ] = $temp ;
153 for ( $i = 0 ; $i < count ( $parts ); $i ++
) {
155 if (! is_array ( $ret )) { unset ( $ret
); break ; }
156 foreach ( array_keys ( $ret ) as $key ) {
157 if ( strtolower ( $key ) == strtolower ( $parts [ $i ])) {
162 if (! $found ) { unset ( $ret
); break ; }
163 $ret = $ret [ $parts [ $i ]];
164 if (( $i < count ( $parts ) - 1 ) and isset ( $ret [ 0 ])) {
173 $x3db = new X3SaxDB ();
174 $data = $x3db- > parsefile ( "data/x3.db" );
175 // $data == copy of $x3db->data
176 var_dump ( $x3db- > data
);
177 var_dump ( $x3db- > getval ( "/NickServ/Jobe/email_addr" ));