JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Fix db_get_value after mysql->mysqli upgrade
[wfpl.git] / fdb.php
1 <?php
2
3 # This program is in the public domain within the United States. Additionally,
4 # we waive copyright and related rights in the work worldwide through the CC0
5 # 1.0 Universal public domain dedication, which can be found at
6 # http://creativecommons.org/publicdomain/zero/1.0/
7
8
9 # This file contains code to use a web-writeable directory full of files as a
10 # very simple database.
11
12 # Keys are truncated to 32 bytes, made lowercase, and all characters that are
13 # not alpha/numeric are replaced with underscores. Periods and hyphens are only
14 # replaced if they are at the begining.
15
16 # Data can be either a string or an array.
17
18 # To set up the database, make a directory that's writeable by PHP and call
19 # fdb_set_dir() passing the path to that directory.
20
21
22 require_once(__DIR__.'/'.'file.php');
23 require_once(__DIR__.'/'.'binary.php');
24
25 # call this to set what directory is used to store the files
26 function fdb_set_dir($dir) {
27         $GLOBALS['fdb_dir'] = $dir;
28 }
29
30 function fdb_get_dir() {
31         if(!isset($GLOBALS['fdb_dir'])) {
32                 die('you must call fdb_set_dir() before calling other functions in wfpl/fdb.php');
33         }
34         return $GLOBALS['fdb_dir'];
35 }
36
37
38 function fdb_fix_key($key) {
39         $key = preg_replace('|[^a-z0-9.-]|', '_', strtolower($key));
40         $key = preg_replace('|^[-.]|', '_', strtolower($key));
41         return substr($key, 0, 32);
42 }
43
44
45 function fdb_get_raw($key) {
46         $key = fdb_fix_key($key);
47         return read_whole_file_or_false(fdb_get_dir() . "/$key");
48 }
49
50 function fdb_set_raw($key, $data) {
51         $key = fdb_fix_key($key);
52         write_whole_file(fdb_get_dir() . "/$key", $data);
53 }
54
55 # like fdb_get() except it returns an array even when there's just one element
56 function fdb_geta($key) {
57         $key = fdb_fix_key($key);
58         $data = fdb_get_raw($key);
59         if($data === false) {
60                 return false;
61         }
62         return raw_to_array($data);
63 }
64
65 # returns:
66 #
67 # false if the key is not found in the database
68 #
69 # an array from the file otherwise
70 #
71 # a string if there's one field in that file (use fdb_geta() if you want an
72 # array in this case too)
73 function fdb_get($key) {
74         $ret = fdb_geta($key);
75         if($ret == false) {
76                 return false;
77         }
78         if(count($ret) == 1) {
79                 return $ret[0];
80         } else {
81                 return $ret;
82         }
83 }
84
85 # data can be a string or array
86 function fdb_set($key, $data) {
87         $key = fdb_fix_key($key);
88         if(!is_array($data)) {
89                 $data = array($data);
90         }
91         fdb_set_raw($key, array_to_raw($data));
92 }
93
94 function fdb_delete($key) {
95         $key = fdb_fix_key($key);
96         $path = fdb_get_dir() . "/$key";
97         if(file_exists($path)) {
98                 return unlink($path);
99         }
100         return false;
101 }