r3014 - getPortTypes(): sayonara
[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', 1150);
16 define ('CHAP_OBJTYPE', 1);
17 define ('CHAP_PORTTYPE', 2);
18
19 define ('TAGNAME_REGEXP', '^[[:alnum:]]([\. _~-]?[[:alnum:]])*$');
20 define ('AUTOTAGNAME_REGEXP', '^\$[[:alnum:]]([\. _~-]?[[:alnum:]])*$');
21 // The latter matches both SunOS and Linux-styled formats.
22 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');
23 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');
24 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');
25 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');
26 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');
27 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');
28 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');
29 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');
30 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');
31
32 function getConfigVar ($varname = '')
33 {
34 global $configCache;
35 // We assume the only point of cache init, and it is init.php. If it
36 // has failed, we don't retry loading.
37 if (!isset ($configCache))
38 {
39 showError ("Configuration cache is unavailable", __FUNCTION__);
40 die;
41 }
42 if ($varname == '')
43 {
44 showError ("Missing argument", __FUNCTION__);
45 die;
46 }
47 if (isset ($configCache[$varname]))
48 {
49 // Try casting to int, if possible.
50 if ($configCache[$varname]['vartype'] == 'uint')
51 return 0 + $configCache[$varname]['varvalue'];
52 else
53 return $configCache[$varname]['varvalue'];
54 }
55 return NULL;
56 }
57
58 // In softfail mode die only on fatal errors, letting the user check
59 // and resubmit his input.
60 function setConfigVar ($varname = '', $varvalue = '', $softfail = FALSE)
61 {
62 global $configCache;
63 if (!isset ($configCache))
64 {
65 showError ('Configuration cache is unavailable', __FUNCTION__);
66 die;
67 }
68 if (!strlen ($varname))
69 {
70 showError ("Empty argument", __FUNCTION__);
71 die;
72 }
73 // We don't operate on unknown data.
74 if (!isset ($configCache[$varname]))
75 {
76 showError ("don't know how to handle '${varname}'", __FUNCTION__);
77 die;
78 }
79 if ($configCache[$varname]['is_hidden'] != 'no')
80 {
81 $errormsg = "'${varname}' is a system variable and cannot be changed by user.";
82 if ($softfail)
83 return $errormsg;
84 showError ($errormsg, __FUNCTION__);
85 die;
86 }
87 if (!strlen ($varvalue) && $configCache[$varname]['emptyok'] != 'yes')
88 {
89 $errormsg = "'${varname}' is configured to take non-empty value. Perhaps there was a reason to do so.";
90 if ($softfail)
91 return $errormsg;
92 showError ($errormsg, __FUNCTION__);
93 die;
94 }
95 if (strlen ($varvalue) && $configCache[$varname]['vartype'] == 'uint' && (!is_numeric ($varvalue) or $varvalue < 0 ))
96 {
97 $errormsg = "'${varname}' can accept UINT values only";
98 if ($softfail)
99 return $errormsg;
100 showError ($errormsg, __FUNCTION__);
101 die;
102 }
103 // Update cache only if the changes went into DB.
104 if (storeConfigVar ($varname, $varvalue))
105 {
106 $configCache[$varname]['varvalue'] = $varvalue;
107 if ($softfail)
108 return '';
109 }
110 elseif ($softfail)
111 return "storeConfigVar ('${varname}', '${varvalue}') failed in setConfigVar()";
112 }
113
114 ?>