4 * This file performs RackTables initialisation. After you include it
5 * from 1st-level page, don't forget to call fixContext(). This is done
6 * to enable override of of pageno and tabno variables. pageno and tabno
7 * together participate in forming security context by generating
12 require_once 'exceptions.php';
13 require_once 'config.php';
14 require_once 'functions.php';
15 require_once 'database.php';
16 require_once 'auth.php';
17 require_once 'navigation.php';
18 require_once 'triggers.php';
19 require_once 'gateways.php';
20 require_once 'IPv6.php';
21 require_once 'interface-lib.php';
22 require_once 'caching.php';
23 // Always have default values for these options, so if a user didn't
24 // care to set, something would be working anyway.
25 $user_auth_src = 'database';
26 $require_local_account = TRUE;
27 # Below are default values for two paths. The right way to change these
28 # is to add respective line(s) to secret.php, unless this is a "shared
29 # code, multiple instances" deploy.
30 $racktables_gwdir = '../gateways';
31 $racktables_staticdir = '.';
32 # Set both paths at once before actually including secret.php, this way
33 # both files will always be included from the same directory.
34 $path_to_secret_php = $path_to_local_php = isset ($racktables_confdir) ?
35 "${racktables_confdir}/" : '';
36 $path_to_secret_php .= 'secret.php';
37 $path_to_local_php .= 'local.php';
39 // (re)connects to DB, stores PDO object in $dbxlink global var
42 global $dbxlink, $pdo_dsn, $db_username, $db_password;
44 // Now try to connect...
47 $dbxlink = new PDO ($pdo_dsn, $db_username, $db_password);
49 catch (PDOException
$e)
51 throw new RackTablesError ("Database connection failed:\n\n" . $e->getMessage(), RackTablesError
::INTERNAL
);
53 $dbxlink->setAttribute(PDO
::ATTR_ERRMODE
, PDO
::ERRMODE_EXCEPTION
);
54 $dbxlink->exec ("set names 'utf8'");
57 // secret.php may be missing, in which case this is a special fatal error
59 if (FALSE === @include_once
$path_to_secret_php)
62 throw new RackTablesError
64 "Database connection parameters are read from ${path_to_secret_php} file, " .
65 "which cannot be found.<br>You probably need to complete the installation " .
66 "procedure by following <a href='?module=installer'>this link</a>.",
67 RackTablesError
::MISCONFIGURED
70 $tmp = ob_get_clean();
71 if ($tmp != '' and ! preg_match ("/^\n+$/D", $tmp))
75 // Magic quotes feature is deprecated, but just in case the local system
76 // still has it activated, reverse its effect.
77 if (function_exists ('get_magic_quotes_gpc') and get_magic_quotes_gpc())
78 foreach ($_REQUEST as $key => $value)
79 if (gettype ($value) == 'string')
80 $_REQUEST[$key] = stripslashes ($value);
82 // Escape any globals before we ever try to use them, but keep a copy of originals.
84 foreach ($_REQUEST as $key => $value)
86 $sic[$key] = dos2unix ($value);
87 if (gettype ($value) == 'string')
88 $_REQUEST[$key] = escapeString (dos2unix ($value));
91 if (isset ($_SERVER['PHP_AUTH_USER']))
92 $_SERVER['PHP_AUTH_USER'] = escapeString ($_SERVER['PHP_AUTH_USER']);
93 if (isset ($_SERVER['REMOTE_USER']))
94 $_SERVER['REMOTE_USER'] = escapeString ($_SERVER['REMOTE_USER']);
97 $tab['reports']['local'] = getConfigVar ('enterprise');
99 if (getConfigVar ('DB_VERSION') != CODE_VERSION
)
101 echo '<p align=justify>This Racktables installation seems to be ' .
102 'just upgraded to version ' . CODE_VERSION
. ', while the '.
103 'database version is ' . getConfigVar ('DB_VERSION') . '.<br>No user will be ' .
104 'either authenticated or shown any page until the upgrade is ' .
105 "finished.<br>Follow <a href='?module=upgrade'>this link</a> and " .
106 'authenticate as administrator to finish the upgrade.</p>';
110 if (!mb_internal_encoding ('UTF-8'))
111 throw new RackTablesError ('Failed setting multibyte string encoding to UTF-8', RackTablesError
::INTERNAL
);
113 $rackCodeCache = loadScript ('RackCodeCache');
114 if ($rackCodeCache == NULL or !strlen ($rackCodeCache))
116 $rackCode = getRackCode (loadScript ('RackCode'));
117 saveScript ('RackCodeCache', base64_encode (serialize ($rackCode)));
121 $rackCode = unserialize (base64_decode ($rackCodeCache));
122 if ($rackCode === FALSE) // invalid cache
124 saveScript ('RackCodeCache', '');
125 $rackCode = getRackCode (loadScript ('RackCode'));
129 // Depending on the 'result' value the 'load' carries either the
130 // parse tree or error message. The latter case is a bug, because
131 // RackCode saving function was supposed to validate its input.
132 if ($rackCode['result'] != 'ACK')
133 throw new RackTablesError ($rackCode['load'], RackTablesError
::INTERNAL
);
134 $rackCode = $rackCode['load'];
135 // Only call buildPredicateTable() once and save the result, because it will remain
136 // constant during one execution for constraints processing.
137 $pTable = buildPredicateTable ($rackCode);
138 // Constraints parse trees aren't cached in the database, so the least to keep
139 // things running is to maintain application cache for them.
140 $parseCache = array();
141 $entityCache = array();
142 // used by getExplicitTagsOnly()
143 $tagRelCache = array();
145 $taglist = getTagList();
146 $tagtree = treeFromList ($taglist);
147 sortTree ($tagtree, 'taginfoCmp');
149 $auto_tags = array();
150 // Initial chain for the current user.
151 $user_given_tags = array();
153 // This also can be modified in local.php.
156 100 => "<link rel='ICON' type='image/x-icon' href='?module=chrome&uri=pix/favicon.ico' />",
158 addCSS ('css/pi.css');
160 if (!isset ($script_mode) or $script_mode !== TRUE)
162 // A successful call to authenticate() always generates autotags and somethimes
163 // even given/implicit tags. It also sets remote_username and remote_displayname.
165 // Authentication passed.
166 // Note that we don't perform autorization here, so each 1st level page
167 // has to do it in its way, e.g. by calling authorize() after fixContext().
172 // Some functions require remote_username to be set to something to act correctly,
173 // even though they don't use the value itself.
174 $admin_account = spotEntity ('user', 1);
175 $remote_username = $admin_account['user_name'];
176 unset ($admin_account);
179 alterConfigWithUserPreferences();
181 // local.php may be missing, this case requires no special treatment
182 // and must not generate any warnings
184 @include_once
$path_to_local_php;
185 $tmp = ob_get_clean();
186 if ($tmp != '' and ! preg_match ("/^\n+$/D", $tmp))
190 // These will be filled in by fixContext()
191 $expl_tags = array();
192 $impl_tags = array();
193 // Initial chain for the current target.
194 $target_given_tags = array();