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