r2946 - renderSearchResults(): discard detection of L2 addresses, it is now a sub...
[racktables] / inc / config.php
1 <?php
2 /*
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
6 * them have gone into the database, and there is a user interface
7 * for changing them. This file now provides a couple of functions to
8 * access the new config storage.
9 *
10 */
11
12
13 // Current code version is subject to change with each new release.
14 define ('CODE_VERSION', '0.17.2');
15 define ('MAX_DICT_KEY', 1076);
16
17 define ('TAGNAME_REGEXP', '^[[:alnum:]]([\. _~-]?[[:alnum:]])*$');
18 define ('AUTOTAGNAME_REGEXP', '^\$[[:alnum:]]([\. _~-]?[[:alnum:]])*$');
19 // The latter matches both SunOS and Linux-styled formats.
20 define ('RE_L2_IFCFG', '/^[0-9a-f][0-9a-f]?:[0-9a-f][0-9a-f]?:[0-9a-f][0-9a-f]?:[0-9a-f][0-9a-f]?:[0-9a-f][0-9a-f]?:[0-9a-f][0-9a-f]?$/i');
21 define ('RE_L2_CISCO', '/^[0-9a-f][0-9a-f][0-9a-f][0-9a-f].[0-9a-f][0-9a-f][0-9a-f][0-9a-f].[0-9a-f][0-9a-f][0-9a-f][0-9a-f]$/i');
22 define ('RE_L2_SOLID', '/^[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]$/i');
23 define ('RE_L2_IPCFG', '/^[0-9a-f][0-9a-f]-[0-9a-f][0-9a-f]-[0-9a-f][0-9a-f]-[0-9a-f][0-9a-f]-[0-9a-f][0-9a-f]-[0-9a-f][0-9a-f]$/i');
24 define ('RE_L2_WWN_COLON', '/^[0-9a-f][0-9a-f]?:[0-9a-f][0-9a-f]?:[0-9a-f][0-9a-f]?:[0-9a-f][0-9a-f]?:[0-9a-f][0-9a-f]?:[0-9a-f][0-9a-f]?:[0-9a-f][0-9a-f]?:[0-9a-f][0-9a-f]$/i');
25 define ('RE_L2_WWN_HYPHEN', '/^[0-9a-f][0-9a-f]-[0-9a-f][0-9a-f]-[0-9a-f][0-9a-f]-[0-9a-f][0-9a-f]-[0-9a-f][0-9a-f]-[0-9a-f][0-9a-f]-[0-9a-f][0-9a-f]-[0-9a-f][0-9a-f]$/i');
26 define ('RE_L2_WWN_SOLID', '/^[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]$/i');
27 define ('RE_IP4_ADDR', '/^[0-9][0-9]?[0-9]?\.[0-9]?[0-9]?[0-9]?\.[0-9][0-9]?[0-9]?\.[0-9][0-9]?[0-9]?$/i');
28 define ('RE_IP4_NET', '/^[0-9][0-9]?[0-9]?\.[0-9]?[0-9]?[0-9]?\.[0-9][0-9]?[0-9]?\.[0-9][0-9]?[0-9]?\/[0-9][0-9]?$/i');
29
30 function getConfigVar ($varname = '')
31 {
32 global $configCache;
33 // We assume the only point of cache init, and it is init.php. If it
34 // has failed, we don't retry loading.
35 if (!isset ($configCache))
36 {
37 showError ("Configuration cache is unavailable", __FUNCTION__);
38 die;
39 }
40 if ($varname == '')
41 {
42 showError ("Missing argument", __FUNCTION__);
43 die;
44 }
45 if (isset ($configCache[$varname]))
46 {
47 // Try casting to int, if possible.
48 if ($configCache[$varname]['vartype'] == 'uint')
49 return 0 + $configCache[$varname]['varvalue'];
50 else
51 return $configCache[$varname]['varvalue'];
52 }
53 return NULL;
54 }
55
56 // In softfail mode die only on fatal errors, letting the user check
57 // and resubmit his input.
58 function setConfigVar ($varname = '', $varvalue = '', $softfail = FALSE)
59 {
60 global $configCache;
61 if (!isset ($configCache))
62 {
63 showError ('Configuration cache is unavailable', __FUNCTION__);
64 die;
65 }
66 if (!strlen ($varname))
67 {
68 showError ("Empty argument", __FUNCTION__);
69 die;
70 }
71 // We don't operate on unknown data.
72 if (!isset ($configCache[$varname]))
73 {
74 showError ("don't know how to handle '${varname}'", __FUNCTION__);
75 die;
76 }
77 if ($configCache[$varname]['is_hidden'] != 'no')
78 {
79 $errormsg = "'${varname}' is a system variable and cannot be changed by user.";
80 if ($softfail)
81 return $errormsg;
82 showError ($errormsg, __FUNCTION__);
83 die;
84 }
85 if (!strlen ($varvalue) && $configCache[$varname]['emptyok'] != 'yes')
86 {
87 $errormsg = "'${varname}' is configured to take non-empty value. Perhaps there was a reason to do so.";
88 if ($softfail)
89 return $errormsg;
90 showError ($errormsg, __FUNCTION__);
91 die;
92 }
93 if (strlen ($varvalue) && $configCache[$varname]['vartype'] == 'uint' && (!is_numeric ($varvalue) or $varvalue < 0 ))
94 {
95 $errormsg = "'${varname}' can accept UINT values only";
96 if ($softfail)
97 return $errormsg;
98 showError ($errormsg, __FUNCTION__);
99 die;
100 }
101 // Update cache only if the changes went into DB.
102 if (storeConfigVar ($varname, $varvalue))
103 {
104 $configCache[$varname]['varvalue'] = $varvalue;
105 if ($softfail)
106 return '';
107 }
108 elseif ($softfail)
109 return "storeConfigVar ('${varname}', '${varvalue}') failed in setConfigVar()";
110 }
111
112 ?>