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