JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
30c7798fdb7d0daf8cd5fdbc7d771ccd2462c703
[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
51 function fdb_fix_key($key) {
52         $key = ereg_replace('[^a-z0-9.-]', '_', strtolower($key));
53         $key = ereg_replace('^[-.]', '_', strtolower($key));
54         return substr($key, 0, 32);
55 }
56
57
58 function fdb_get_raw($key) {
59         $key = fdb_fix_key($key);
60         return read_whole_file_or_false(fdb_get_dir() . "/$key");
61 }
62
63 function fdb_set_raw($key, $data) {
64         $key = fdb_fix_key($key);
65         write_whole_file(fdb_get_dir() . "/$key", $data);
66 }
67
68 # like fdb_get() except it returns an array even when there's just one element
69 function fdb_geta($key) {
70         $key = fdb_fix_key($key);
71         $data = fdb_get_raw($key);
72         if($data === false) {
73                 return false;
74         }
75         return raw_to_array($data);
76 }
77
78 # returns:
79 #
80 # false if the key is not found in the database
81 #
82 # an array from the file otherwise
83 #
84 # a string if there's one field in that file (use fdb_geta() if you want an
85 # array in this case too)
86 function fdb_get($key) {
87         $ret = fdb_geta($key);
88         if($ret == false) {
89                 return false;
90         }
91         if(count($ret) == 1) {
92                 return $ret[0];
93         } else {
94                 return $ret;
95         }
96 }
97
98 # data can be a string or array
99 function fdb_set($key, $data) {
100         $key = fdb_fix_key($key);
101         if(!is_array($data)) {
102                 $data = array($data);
103         }
104         fdb_set_raw($key, array_to_raw($data));
105 }
106
107 function fdb_delete($key) {
108         $key = fdb_fix_key($key);
109         $path = fdb_get_dir() . "/$key";
110         if(file_exists($path)) {
111                 return unlink($path);
112         }
113         return false;
114 }
115
116 ?>