r3203 bugfix for a broken process.php
[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.
026a79ee 14define ('CODE_VERSION', '0.17.7');
7fa7047a
DO
15define ('CHAP_OBJTYPE', 1);
16define ('CHAP_PORTTYPE', 2);
e673ee24 17
ca3d68bd
DO
18$max_dict_key = array
19(
20 '0.17.0' => 988,
21 '0.17.1' => 988,
22 '0.17.2' => 1150,
70992855 23 '0.17.3' => 1150,
681f2138 24 '0.17.4' => 1150,
2803fbe6 25 '0.17.5' => 1322,
63811a09 26 '0.17.6' => 1326,
026a79ee 27 '0.17.7' => 1326,
ca3d68bd
DO
28);
29
23818dde
DO
30define ('TAGNAME_REGEXP', '^[[:alnum:]]([\. _~-]?[[:alnum:]])*$');
31define ('AUTOTAGNAME_REGEXP', '^\$[[:alnum:]]([\. _~-]?[[:alnum:]])*$');
05771508
DO
32// The latter matches both SunOS and Linux-styled formats.
33define ('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');
34define ('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');
35define ('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 36define ('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
37define ('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');
38define ('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');
39define ('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
40define ('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');
41define ('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 42
9c0b0016
DO
43function getConfigVar ($varname = '')
44{
45 global $configCache;
46 // We assume the only point of cache init, and it is init.php. If it
47 // has failed, we don't retry loading.
48 if (!isset ($configCache))
49 {
0cc24e9a 50 throw new RuntimeException ("Configuration cache is unavailable");
9c0b0016
DO
51 }
52 if ($varname == '')
53 {
0cc24e9a 54 throw new InvalidArgException('$varname', $varname, 'Empty variable name');
9c0b0016
DO
55 }
56 if (isset ($configCache[$varname]))
57 {
58 // Try casting to int, if possible.
c461c579 59 if ($configCache[$varname]['vartype'] == 'uint')
9c0b0016
DO
60 return 0 + $configCache[$varname]['varvalue'];
61 else
62 return $configCache[$varname]['varvalue'];
63 }
64 return NULL;
65}
66
c461c579
DO
67// In softfail mode die only on fatal errors, letting the user check
68// and resubmit his input.
69function setConfigVar ($varname = '', $varvalue = '', $softfail = FALSE)
9c0b0016
DO
70{
71 global $configCache;
72 if (!isset ($configCache))
73 {
0cc24e9a 74 throw new RuntimeException ("Configuration cache is unavailable");
9c0b0016 75 }
59a83bd8 76 if (!strlen ($varname))
9c0b0016 77 {
0cc24e9a 78 throw new InvalidArgException('$varname', $varname, 'Empty variable name');
9c0b0016
DO
79 }
80 // We don't operate on unknown data.
81 if (!isset ($configCache[$varname]))
82 {
0cc24e9a 83 throw new InvalidArgException('$varname', $varname, "Don't know how to handle '${varname}'");
9c0b0016
DO
84 die;
85 }
dc2f1801
DO
86 if ($configCache[$varname]['is_hidden'] != 'no')
87 {
88 $errormsg = "'${varname}' is a system variable and cannot be changed by user.";
89 if ($softfail)
90 return $errormsg;
0cc24e9a 91 throw new InvalidArgException('$varname', $varname, $errormsg);
dc2f1801 92 }
59a83bd8 93 if (!strlen ($varvalue) && $configCache[$varname]['emptyok'] != 'yes')
9c0b0016 94 {
c461c579
DO
95 $errormsg = "'${varname}' is configured to take non-empty value. Perhaps there was a reason to do so.";
96 if ($softfail)
97 return $errormsg;
0cc24e9a 98 throw new InvalidArgException('$varname', $varname, $errormsg);
c461c579 99 }
59a83bd8 100 if (strlen ($varvalue) && $configCache[$varname]['vartype'] == 'uint' && (!is_numeric ($varvalue) or $varvalue < 0 ))
c461c579
DO
101 {
102 $errormsg = "'${varname}' can accept UINT values only";
103 if ($softfail)
104 return $errormsg;
0cc24e9a 105 throw new InvalidArgException('$varname', $varname, $errormsg);
9c0b0016
DO
106 }
107 // Update cache only if the changes went into DB.
108 if (storeConfigVar ($varname, $varvalue))
c461c579 109 {
9c0b0016 110 $configCache[$varname]['varvalue'] = $varvalue;
c461c579
DO
111 if ($softfail)
112 return '';
113 }
114 elseif ($softfail)
f1e7f725 115 return "storeConfigVar ('${varname}', '${varvalue}') failed in setConfigVar()";
9c0b0016 116}
8d07e3e0 117
e673ee24 118?>