r2906 - maintenance->trunk sync of changesets 2886~2894
[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');
3fb336f6 15define ('MAX_DICT_KEY', 1058);
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');
23define ('RE_L2_FDRYSTP', '/^[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');
1930dc7f 24define ('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');
05771508
DO
25define ('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');
26define ('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 27
9c0b0016
DO
28function getConfigVar ($varname = '')
29{
30 global $configCache;
31 // We assume the only point of cache init, and it is init.php. If it
32 // has failed, we don't retry loading.
33 if (!isset ($configCache))
34 {
61e269b5 35 showError ("Configuration cache is unavailable", __FUNCTION__);
9c0b0016
DO
36 die;
37 }
38 if ($varname == '')
39 {
61e269b5 40 showError ("Missing argument", __FUNCTION__);
9c0b0016
DO
41 die;
42 }
43 if (isset ($configCache[$varname]))
44 {
45 // Try casting to int, if possible.
c461c579 46 if ($configCache[$varname]['vartype'] == 'uint')
9c0b0016
DO
47 return 0 + $configCache[$varname]['varvalue'];
48 else
49 return $configCache[$varname]['varvalue'];
50 }
51 return NULL;
52}
53
c461c579
DO
54// In softfail mode die only on fatal errors, letting the user check
55// and resubmit his input.
56function setConfigVar ($varname = '', $varvalue = '', $softfail = FALSE)
9c0b0016
DO
57{
58 global $configCache;
59 if (!isset ($configCache))
60 {
61e269b5 61 showError ('Configuration cache is unavailable', __FUNCTION__);
9c0b0016
DO
62 die;
63 }
59a83bd8 64 if (!strlen ($varname))
9c0b0016 65 {
61e269b5 66 showError ("Empty argument", __FUNCTION__);
9c0b0016
DO
67 die;
68 }
69 // We don't operate on unknown data.
70 if (!isset ($configCache[$varname]))
71 {
61e269b5 72 showError ("don't know how to handle '${varname}'", __FUNCTION__);
9c0b0016
DO
73 die;
74 }
dc2f1801
DO
75 if ($configCache[$varname]['is_hidden'] != 'no')
76 {
77 $errormsg = "'${varname}' is a system variable and cannot be changed by user.";
78 if ($softfail)
79 return $errormsg;
80 showError ($errormsg, __FUNCTION__);
81 die;
82 }
59a83bd8 83 if (!strlen ($varvalue) && $configCache[$varname]['emptyok'] != 'yes')
9c0b0016 84 {
c461c579
DO
85 $errormsg = "'${varname}' is configured to take non-empty value. Perhaps there was a reason to do so.";
86 if ($softfail)
87 return $errormsg;
61e269b5 88 showError ($errormsg, __FUNCTION__);
c461c579
DO
89 die;
90 }
59a83bd8 91 if (strlen ($varvalue) && $configCache[$varname]['vartype'] == 'uint' && (!is_numeric ($varvalue) or $varvalue < 0 ))
c461c579
DO
92 {
93 $errormsg = "'${varname}' can accept UINT values only";
94 if ($softfail)
95 return $errormsg;
61e269b5 96 showError ($errormsg, __FUNCTION__);
9c0b0016
DO
97 die;
98 }
99 // Update cache only if the changes went into DB.
100 if (storeConfigVar ($varname, $varvalue))
c461c579 101 {
9c0b0016 102 $configCache[$varname]['varvalue'] = $varvalue;
c461c579
DO
103 if ($softfail)
104 return '';
105 }
106 elseif ($softfail)
f1e7f725 107 return "storeConfigVar ('${varname}', '${varvalue}') failed in setConfigVar()";
9c0b0016 108}
8d07e3e0 109
e673ee24 110?>