r2836 Added EntityNotFoundException to spotEntity()
[racktables] / inc / init.php
CommitLineData
b325120a 1<?php
e673ee24
DO
2/*
3*
4* This file performs RackTables initialisation. After you include it
da958e52 5* from 1st-level page, don't forget to call fixContext(). This is done
204284ba 6* to enable override of of pageno and tabno variables. pageno and tabno
c3a8284b
DO
7* together participate in forming security context by generating
8* related autotags.
e673ee24
DO
9*
10*/
11
fff18256
DO
12// "Note that when using ISAPI with IIS, the value will be 'off' if the
13// request was not made through the HTTPS protocol."
14$root = (empty($_SERVER['HTTPS']) or $_SERVER['HTTPS'] == 'off') ? 'http://' : 'https://';
15$root .= isset ($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ($_SERVER['SERVER_NAME'].($_SERVER['SERVER_PORT']=='80'?'':$_SERVER['SERVER_PORT']));
16// "Since PHP 4.3.0, you will often get a slash or a dot back from
17// dirname() in situations where the older functionality would have given
18// you the empty string."
19// "On Windows, both slash (/) and backslash (\) are used as directory
20// separator character."
21$root .= strtr (dirname ($_SERVER['PHP_SELF']), '\\', '/');
9c6e7a97
DO
22if (substr ($root, -1) != '/')
23 $root .= '/';
e673ee24
DO
24
25// This is the first thing we need to do.
b135a49d 26require_once 'inc/exceptions.php';
e673ee24
DO
27require_once 'inc/config.php';
28
29// What we need first is database and interface functions.
30require_once 'inc/interface.php';
31require_once 'inc/functions.php';
32require_once 'inc/database.php';
2c9fde3a
DO
33// Always have default values for these options, so if a user didn't
34// care to set, something would be working anyway.
35$user_auth_src = 'database';
36$require_local_account = TRUE;
37
80138748
DO
38if (file_exists ('inc/secret.php'))
39 require_once 'inc/secret.php';
e673ee24
DO
40else
41{
42 showError
43 (
80138748 44 "Database connection parameters are read from inc/secret.php file, " .
e6093686 45 "which cannot be found.\nYou probably need to complete the installation " .
b0348307
DO
46 "procedure by following <a href='${root}install.php'>this link</a>.",
47 __FILE__
e673ee24 48 );
7ab82114 49 exit (1);
e673ee24
DO
50}
51
52// Now try to connect...
53try
54{
55 $dbxlink = new PDO ($pdo_dsn, $db_username, $db_password);
56}
57catch (PDOException $e)
58{
b0348307 59 showError ("Database connection failed:\n\n" . $e->getMessage(), __FILE__);
7ab82114 60 exit (1);
e673ee24
DO
61}
62
4d55392a
DO
63$dbxlink->exec ("set names 'utf8'");
64
b8d1ab66
DO
65if (get_magic_quotes_gpc())
66 foreach ($_REQUEST as $key => $value)
f4d511df
DO
67 if (gettype ($value) == 'string')
68 $_REQUEST[$key] = stripslashes ($value);
b8d1ab66
DO
69
70if (!set_magic_quotes_runtime (0))
71{
b0348307 72 showError ('Failed to turn magic quotes off', __FILE__);
7ab82114 73 exit (1);
b8d1ab66 74}
6bae5fbb 75
f3d274bf 76// Escape any globals before we ever try to use them, but keep a copy of originals.
f8874cdb 77$sic = array();
e673ee24 78foreach ($_REQUEST as $key => $value)
f8874cdb
DO
79{
80 $sic[$key] = dos2unix ($value);
f4d511df 81 if (gettype ($value) == 'string')
f8874cdb
DO
82 $_REQUEST[$key] = escapeString (dos2unix ($value));
83}
6bae5fbb 84
e673ee24
DO
85if (isset ($_SERVER['PHP_AUTH_USER']))
86 $_SERVER['PHP_AUTH_USER'] = escapeString ($_SERVER['PHP_AUTH_USER']);
dc9ea133
DO
87if (isset ($_SERVER['REMOTE_USER']))
88 $_SERVER['REMOTE_USER'] = escapeString ($_SERVER['REMOTE_USER']);
e673ee24 89
fbbb74fb
DO
90$dbver = getDatabaseVersion();
91if ($dbver != CODE_VERSION)
92{
93 echo '<p align=justify>This Racktables installation seems to be ' .
94 'just upgraded to version ' . CODE_VERSION . ', while the '.
db914a6c 95 'database version is ' . $dbver . '. No user will be ' .
fbbb74fb
DO
96 'either authenticated or shown any page until the upgrade is ' .
97 "finished. Follow <a href='${root}upgrade.php'>this link</a> and " .
98 'authenticate as administrator to finish the upgrade.</p>';
7ab82114 99 exit (1);
fbbb74fb
DO
100}
101
26131670
DO
102if (!mb_internal_encoding ('UTF-8') or !mb_regex_encoding ('UTF-8'))
103{
b0348307 104 showError ('Failed setting multibyte string encoding to UTF-8', __FILE__);
7ab82114 105 exit (1);
26131670 106}
06f23fd4
DO
107$configCache = loadConfigCache();
108if (!count ($configCache))
109{
b0348307 110 showError ('Failed to load configuration from the database.', __FILE__);
7ab82114 111 exit (1);
06f23fd4
DO
112}
113
204284ba 114require_once 'inc/code.php'; // for getRackCode()
4a6a28f1 115$rackCodeCache = loadScript ('RackCodeCache');
59a83bd8 116if ($rackCodeCache == NULL or !strlen ($rackCodeCache))
4a6a28f1 117{
4a6a28f1 118 $rackCode = getRackCode (loadScript ('RackCode'));
4a6a28f1
DO
119 saveScript ('RackCodeCache', base64_encode (serialize ($rackCode)));
120}
121else
122{
4a6a28f1 123 $rackCode = unserialize (base64_decode ($rackCodeCache));
4a6a28f1
DO
124 if ($rackCode === FALSE) // invalid cache
125 {
126 saveScript ('RackCodeCache', '');
4a6a28f1 127 $rackCode = getRackCode (loadScript ('RackCode'));
4a6a28f1
DO
128 }
129}
130
cf25e649
DO
131// Depending on the 'result' value the 'load' carries either the
132// parse tree or error message.
cf25e649
DO
133if ($rackCode['result'] != 'ACK')
134{
135 // FIXME: display a message with an option to reset RackCode text
b0348307 136 showError ('Could not load the RackCode due to error: ' . $rackCode['load'], __FILE__);
7ab82114 137 exit (1);
cf25e649
DO
138}
139$rackCode = $rackCode['load'];
7ddbcf59
DO
140// Only call buildPredicateTable() once and save the result, because it will remain
141// constant during one execution for constraints processing.
142$pTable = buildPredicateTable ($rackCode);
143// Constraints parse trees aren't cached in the database, so the least to keep
144// things running is to maintain application cache for them.
145$parseCache = array();
bcd37231 146
7cfc98b1
DO
147$entityCache = array();
148
573214e0
DO
149$taglist = getTagList();
150$tagtree = treeFromList ($taglist);
151sortTree ($tagtree, 'taginfoCmp');
152
e673ee24 153require_once 'inc/auth.php';
d5262485 154$auto_tags = array();
b82cce3f
DO
155// Initial chain for the current user.
156$user_given_tags = array();
e673ee24 157
deb18b61 158if (!isset ($script_mode) or $script_mode !== TRUE)
b82cce3f
DO
159{
160 authenticate(); // this call always generates autotags and somethimes --- given tags
161 // Authentication passed.
162 // Note that we don't perform autorization here, so each 1st level page
163 // has to do it in its way, e.g. by calling authorize() after fixContext().
deb18b61 164 session_start();
b82cce3f 165}
83555c04 166
e673ee24 167$pageno = (isset ($_REQUEST['page'])) ? $_REQUEST['page'] : 'index';
b0348307
DO
168// Special handling of tab number to substitute the "last" index where applicable.
169// Always show explicitly requested tab, substitute the last used name in case
170// it is awailable, fall back to the default one.
da111736 171
b0348307
DO
172if (isset ($_REQUEST['tab']))
173 $tabno = $_REQUEST['tab'];
da111736 174elseif (basename($_SERVER['PHP_SELF']) == 'index.php' and getConfigVar ('SHOW_LAST_TAB') == 'yes' and isset ($_SESSION['RTLT'][$pageno]))
050b68b5 175{
3b34b607 176 $tabno = $_SESSION['RTLT'][$pageno];
050b68b5
DY
177 $url = "${root}?page=$pageno&tab=$tabno";
178 foreach ($_GET as $name=>$value)
179 {
180 if ($name == 'page' or $name == 'tab') continue;
58652ac1
DY
181 if (gettype($value) == 'array')
182 foreach($value as $v)
183 $url .= '&'.urlencode($name.'[]').'='.urlencode($v);
184 else
185 $url .= '&'.urlencode($name).'='.urlencode($value);
050b68b5
DY
186 }
187 header('Location: '.$url);
188 exit();
189}
b0348307
DO
190else
191 $tabno = 'default';
050b68b5 192
da958e52 193$op = (isset ($_REQUEST['op'])) ? $_REQUEST['op'] : '';
b0348307 194
e673ee24 195require_once 'inc/navigation.php';
641fe9b0 196require_once 'inc/triggers.php';
d33645ff 197require_once 'inc/gateways.php';
80138748
DO
198if (file_exists ('inc/local.php'))
199 require_once 'inc/local.php';
e673ee24 200
4c9b513a 201// These will be filled in by fixContext()
2fb24351
DO
202$expl_tags = array();
203$impl_tags = array();
4c9b513a
DO
204// Initial chain for the current target.
205$target_given_tags = array();
2fb24351 206
e673ee24 207?>