Commit | Line | Data |
---|---|---|
b325120a | 1 | <?php |
e673ee24 | 2 | /* |
9c0b0016 DO |
3 | * |
4 | * This file used to hold a collection of constants, variables and arrays, | |
5 | * which drived the way misc RackTables functions performed. Now most of | |
201056a2 | 6 | * them have gone into the database, and there is a user interface |
9c0b0016 DO |
7 | * for changing them. This file now provides a couple of functions to |
8 | * access the new config storage. | |
9 | * | |
8d07e3e0 DO |
10 | */ |
11 | ||
8d07e3e0 | 12 | |
9c0b0016 | 13 | // Current code version is subject to change with each new release. |
8608b285 | 14 | define ('CODE_VERSION', '0.16.1'); |
e673ee24 | 15 | |
9c0b0016 DO |
16 | // The name of hash used to store account password hashes |
17 | // in the database. I think, we are happy with this one forever. | |
18 | define ('PASSWORD_HASH', 'sha1'); | |
75d3818d | 19 | |
23818dde DO |
20 | define ('TAGNAME_REGEXP', '^[[:alnum:]]([\. _~-]?[[:alnum:]])*$'); |
21 | define ('AUTOTAGNAME_REGEXP', '^\$[[:alnum:]]([\. _~-]?[[:alnum:]])*$'); | |
26131670 | 22 | |
9c0b0016 DO |
23 | function getConfigVar ($varname = '') |
24 | { | |
25 | global $configCache; | |
26 | // We assume the only point of cache init, and it is init.php. If it | |
27 | // has failed, we don't retry loading. | |
28 | if (!isset ($configCache)) | |
29 | { | |
61e269b5 | 30 | showError ("Configuration cache is unavailable", __FUNCTION__); |
9c0b0016 DO |
31 | die; |
32 | } | |
33 | if ($varname == '') | |
34 | { | |
61e269b5 | 35 | showError ("Missing argument", __FUNCTION__); |
9c0b0016 DO |
36 | die; |
37 | } | |
38 | if (isset ($configCache[$varname])) | |
39 | { | |
40 | // Try casting to int, if possible. | |
c461c579 | 41 | if ($configCache[$varname]['vartype'] == 'uint') |
9c0b0016 DO |
42 | return 0 + $configCache[$varname]['varvalue']; |
43 | else | |
44 | return $configCache[$varname]['varvalue']; | |
45 | } | |
46 | return NULL; | |
47 | } | |
48 | ||
c461c579 DO |
49 | // In softfail mode die only on fatal errors, letting the user check |
50 | // and resubmit his input. | |
51 | function setConfigVar ($varname = '', $varvalue = '', $softfail = FALSE) | |
9c0b0016 DO |
52 | { |
53 | global $configCache; | |
54 | if (!isset ($configCache)) | |
55 | { | |
61e269b5 | 56 | showError ('Configuration cache is unavailable', __FUNCTION__); |
9c0b0016 DO |
57 | die; |
58 | } | |
59 | if (empty ($varname)) | |
60 | { | |
61e269b5 | 61 | showError ("Empty argument", __FUNCTION__); |
9c0b0016 DO |
62 | die; |
63 | } | |
64 | // We don't operate on unknown data. | |
65 | if (!isset ($configCache[$varname])) | |
66 | { | |
61e269b5 | 67 | showError ("don't know how to handle '${varname}'", __FUNCTION__); |
9c0b0016 DO |
68 | die; |
69 | } | |
dc2f1801 DO |
70 | if ($configCache[$varname]['is_hidden'] != 'no') |
71 | { | |
72 | $errormsg = "'${varname}' is a system variable and cannot be changed by user."; | |
73 | if ($softfail) | |
74 | return $errormsg; | |
75 | showError ($errormsg, __FUNCTION__); | |
76 | die; | |
77 | } | |
9c0b0016 DO |
78 | if (empty ($varvalue) && $configCache[$varname]['emptyok'] != 'yes') |
79 | { | |
c461c579 DO |
80 | $errormsg = "'${varname}' is configured to take non-empty value. Perhaps there was a reason to do so."; |
81 | if ($softfail) | |
82 | return $errormsg; | |
61e269b5 | 83 | showError ($errormsg, __FUNCTION__); |
c461c579 DO |
84 | die; |
85 | } | |
f1e7f725 | 86 | if (!empty ($varvalue) && $configCache[$varname]['vartype'] == 'uint' && (!is_numeric ($varvalue) or $varvalue < 0 )) |
c461c579 DO |
87 | { |
88 | $errormsg = "'${varname}' can accept UINT values only"; | |
89 | if ($softfail) | |
90 | return $errormsg; | |
61e269b5 | 91 | showError ($errormsg, __FUNCTION__); |
9c0b0016 DO |
92 | die; |
93 | } | |
94 | // Update cache only if the changes went into DB. | |
95 | if (storeConfigVar ($varname, $varvalue)) | |
c461c579 | 96 | { |
9c0b0016 | 97 | $configCache[$varname]['varvalue'] = $varvalue; |
c461c579 DO |
98 | if ($softfail) |
99 | return ''; | |
100 | } | |
101 | elseif ($softfail) | |
f1e7f725 | 102 | return "storeConfigVar ('${varname}', '${varvalue}') failed in setConfigVar()"; |
9c0b0016 | 103 | } |
8d07e3e0 | 104 | |
e673ee24 | 105 | ?> |