JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
metaform only sends .sql file if you check 'db'. added string_array()
[wfpl.git] / fdb.php
1 <?php
2
3 #  Copyright (C) 2007 Jason Woofenden
4 #
5 #  This file is part of wfpl.
6 #
7 #  wfpl is free software; you can redistribute it and/or modify it under the
8 #  terms of the GNU Lesser General Public License as published by the Free
9 #  Software Foundation; either version 2.1 of the License, or (at your option)
10 #  any later version.
11 #
12 #  wfpl is distributed in the hope that it will be useful, but WITHOUT ANY
13 #  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 #  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
15 #  more details.
16 #
17 #  You should have received a copy of the GNU Lesser General Public License
18 #  along with wfpl; if not, write to the Free Software Foundation, Inc., 51
19 #  Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20
21
22 # This file contains code to use a web-writeable directory full of files as a
23 # very simple database.
24
25 # Keys are truncated to 32 bytes, made lowercase, and all characters that are
26 # not alpha/numeric are replaced with underscores. Periods and hyphens are only
27 # replaced if they are at the begining.
28
29 # Data can be either a string or an array.
30
31 # To set up the database, make a directory that's writeable by PHP and call
32 # fdb_set_dir() passing the path to that directory.
33
34
35 require_once('code/wfpl/file.php');
36 require_once('code/wfpl/binary.php');
37
38 # call this to set what directory is used to store the files
39 function fdb_set_dir($dir) {
40         $GLOBALS['fdb_dir'] = $dir;
41 }
42
43 function fdb_get_dir() {
44         if(!isset($GLOBALS['fdb_dir'])) {
45                 die('you must call fdb_set_dir() before calling other functions in code/wfpl/fdb.php');
46         }
47         return $GLOBALS['fdb_dir'];
48 }
49
50 # return a 4 bytes that represent the passed integer as a big-endian binary number
51 function to_raw_int($int) {
52         return chr($int >> 24) . chr(($int >> 16) & 0xff) . chr(($int >> 8) & 0xff) . chr($int & 0xff);
53 }
54
55 # return a php number from the string you pass in. The first 4 bytes of the
56 # string are read in as a binary value in big-endian format.
57 function from_raw_int($quad) {
58         return (ord(substr($quad, 0, 1)) << 24) + (ord(substr($quad, 1, 1)) << 16) + (ord(substr($quad, 2, 1)) << 8) + ord(substr($quad, 3, 1));
59 }
60
61 function int_at($string, $index) {
62         return from_raw_int(substr($string, $index * 4, 4));
63 }
64
65 # remove the first 4 bytes of the string, and return them as an int
66 function pop_int(&$string) {
67         $int = from_raw_int(substr($string, 0, 4));
68         $string = substr($string, 4);
69         return $int;
70 }
71
72
73 function fdb_fix_key($key) {
74         $key = ereg_replace('[^a-z0-9.-]', '_', strtolower($key));
75         $key = ereg_replace('^[-.]', '_', strtolower($key));
76         return substr($key, 0, 32);
77 }
78
79
80 function fdb_get_raw($key) {
81         $key = fdb_fix_key($key);
82         return read_whole_file_or_false(fdb_get_dir() . "/$key");
83 }
84
85 function fdb_set_raw($key, $data) {
86         $key = fdb_fix_key($key);
87         write_whole_file(fdb_get_dir() . "/$key", $data);
88 }
89
90 # like fdb_get() except it returns an array even when there's just one element
91 function fdb_geta($key) {
92         $key = fdb_fix_key($key);
93         $data = fdb_get_raw($key);
94         if($data === false) {
95                 return false;
96         }
97         $header_count = pop_int($data);
98         $out = array();
99         while($header_count--) {
100                 $size = pop_int($data);
101                 $out[] = substr($data, 0, $size);
102                 $data = substr($data, $size);
103         }
104         return $out;
105 }
106
107 # returns:
108 #
109 # false if the key is not found in the database
110 #
111 # an array from the file otherwise
112 #
113 # a string if there's one field in that file (use fdb_geta() if you want an
114 # array in this case too)
115 function fdb_get($key) {
116         $ret = fdb_geta($key);
117         if($ret == false) {
118                 return false;
119         }
120         if(count($ret) == 1) {
121                 return $ret[0];
122         } else {
123                 return $ret;
124         }
125 }
126
127 # data can be a string or array
128 function fdb_set($key, $data) {
129         $key = fdb_fix_key($key);
130         if(!is_array($data)) {
131                 $data = array($data);
132         }
133         $out = to_raw_int(count($data));
134         foreach($data as $dat) {
135                 $out .= to_raw_int(strlen($dat));
136                 $out .= $dat;
137         }
138         fdb_set_raw($key, $out);
139 }
140
141 function fdb_delete($key) {
142         $key = fdb_fix_key($key);
143         $path = fdb_get_dir() . "/$key";
144         if(file_exists($path)) {
145                 return unlink($path);
146         }
147         return false;
148 }
149
150 ?>