r3035 - replace record 731 with record 1018 (ticket:239)
[racktables] / inc / config.php
CommitLineData
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.
f7bd644b 14define ('CODE_VERSION', '0.17.2');
4e60a63b 15define ('MAX_DICT_KEY', 1150);
7fa7047a
DO
16define ('CHAP_OBJTYPE', 1);
17define ('CHAP_PORTTYPE', 2);
e673ee24 18
23818dde
DO
19define ('TAGNAME_REGEXP', '^[[:alnum:]]([\. _~-]?[[:alnum:]])*$');
20define ('AUTOTAGNAME_REGEXP', '^\$[[:alnum:]]([\. _~-]?[[:alnum:]])*$');
05771508
DO
21// The latter matches both SunOS and Linux-styled formats.
22define ('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');
23define ('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');
24define ('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');
1930dc7f 25define ('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');
d516d719
DO
26define ('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');
27define ('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');
28define ('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');
05771508
DO
29define ('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');
30define ('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');
26131670 31
9c0b0016
DO
32function 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 {
61e269b5 39 showError ("Configuration cache is unavailable", __FUNCTION__);
9c0b0016
DO
40 die;
41 }
42 if ($varname == '')
43 {
61e269b5 44 showError ("Missing argument", __FUNCTION__);
9c0b0016
DO
45 die;
46 }
47 if (isset ($configCache[$varname]))
48 {
49 // Try casting to int, if possible.
c461c579 50 if ($configCache[$varname]['vartype'] == 'uint')
9c0b0016
DO
51 return 0 + $configCache[$varname]['varvalue'];
52 else
53 return $configCache[$varname]['varvalue'];
54 }
55 return NULL;
56}
57
c461c579
DO
58// In softfail mode die only on fatal errors, letting the user check
59// and resubmit his input.
60function setConfigVar ($varname = '', $varvalue = '', $softfail = FALSE)
9c0b0016
DO
61{
62 global $configCache;
63 if (!isset ($configCache))
64 {
61e269b5 65 showError ('Configuration cache is unavailable', __FUNCTION__);
9c0b0016
DO
66 die;
67 }
59a83bd8 68 if (!strlen ($varname))
9c0b0016 69 {
61e269b5 70 showError ("Empty argument", __FUNCTION__);
9c0b0016
DO
71 die;
72 }
73 // We don't operate on unknown data.
74 if (!isset ($configCache[$varname]))
75 {
61e269b5 76 showError ("don't know how to handle '${varname}'", __FUNCTION__);
9c0b0016
DO
77 die;
78 }
dc2f1801
DO
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 }
59a83bd8 87 if (!strlen ($varvalue) && $configCache[$varname]['emptyok'] != 'yes')
9c0b0016 88 {
c461c579
DO
89 $errormsg = "'${varname}' is configured to take non-empty value. Perhaps there was a reason to do so.";
90 if ($softfail)
91 return $errormsg;
61e269b5 92 showError ($errormsg, __FUNCTION__);
c461c579
DO
93 die;
94 }
59a83bd8 95 if (strlen ($varvalue) && $configCache[$varname]['vartype'] == 'uint' && (!is_numeric ($varvalue) or $varvalue < 0 ))
c461c579
DO
96 {
97 $errormsg = "'${varname}' can accept UINT values only";
98 if ($softfail)
99 return $errormsg;
61e269b5 100 showError ($errormsg, __FUNCTION__);
9c0b0016
DO
101 die;
102 }
103 // Update cache only if the changes went into DB.
104 if (storeConfigVar ($varname, $varvalue))
c461c579 105 {
9c0b0016 106 $configCache[$varname]['varvalue'] = $varvalue;
c461c579
DO
107 if ($softfail)
108 return '';
109 }
110 elseif ($softfail)
f1e7f725 111 return "storeConfigVar ('${varname}', '${varvalue}') failed in setConfigVar()";
9c0b0016 112}
8d07e3e0 113
e673ee24 114?>