]>
jfr.im git - irc/evilnet/x3.git/blob - tools/db.php
4 This is a php function & demo util which parses the x3.db and x3.conf files for use in web programs.
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
11 $conf [ 'dbfile' ] = "x3.db" ;
13 $dh = fopen ( $conf [ 'dbfile' ], "r" );
15 $contents = fread ( $dh , filesize ( $conf [ 'dbfile' ]));
19 function parse_data ( $data = "" ) {
20 static $passback = "" ;
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 ) ) {
38 $passback = $matches [ 1 ];
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 ]);
45 $return [ strtolower ( $matches [ 1 ])] = parse_data ( $matches [ 7 ]);
48 } else if ( preg_match ( "/^\s*(( \" ((( \\\\\" )|[^ \" ])+) \" )|([^\s]+))\s*\(\s*(((( \" ((( \\\\\" )|[^ \" ])+) \" )|([^\s,]+))\s*(,\s*(( \" ((( \\\\\" )|[^ \" ])+) \" )|([^\s,]+))\s*)*)?)\s*\)\s*;\s*(.*) \$ /s" , $working , $matches ) ) {
50 $arraycontents = $matches [ 7 ];
52 while ( preg_match ( "/[^\s]+/" , $arraycontents ) ) {
53 preg_match ( "/^\s*,?\s*(( \" ((( \\\\\" )|[^ \" ])+) \" )|([^\s,]+))\s*(.*)/s" , $arraycontents , $arraymatches );
54 if ( $arraymatches [ 3 ] != "" ) {
55 $array [] = $arraymatches [ 3 ];
57 $array [] = $arraymatches [ 1 ];
59 $arraycontents = $arraymatches [ 7 ];
61 if ( $matches [ 3 ] != "" ) {
62 $return [ strtolower ( $matches [ 3 ])] = $array ;
64 $return [ strtolower ( $matches [ 1 ])] = $array ;
66 $working = $matches [ 22 ];
67 } else if ( preg_match ( "/^\s*(( \" ((( \\\\\" )|[^ \" ])+) \" )|([^\s,]+))\s*((( \" ((( \\\\\" )|[^ \" ])+) \" )|([^\s,]+)))?\s*;\s*(.*) \$ /s" , $working , $matches ) ) {
69 if ( $matches [ 3 ] != "" ) {
70 $key = strtolower ( $matches [ 3 ]);
72 $key = strtolower ( $matches [ 1 ]);
74 if ( $matches [ 7 ] != "" ) {
75 if ( $matches [ 10 ] != "" ) {
80 if ( isset ( $return [ $key ]) ) {
81 if ( ! is_array ( $return [ $key ]) ) {
82 $temp = $return [ $key ];
84 $return [ $key ][] = $temp ;
85 $return [ $key ][] = $val ;
87 $return [ $key ][] = $val ;
93 $return [ $key ] = array ();
95 $working = $matches [ 14 ];
97 if ( $working != "" ) {
98 $return [ 'parseerror' ] = $working ;
109 $db = parse_data ( $contents );