r2836 Added EntityNotFoundException to spotEntity()
[racktables] / inc / init.php
1 <?php
2 /*
3 *
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
8 * related autotags.
9 *
10 */
11
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']), '\\', '/');
22 if (substr ($root, -1) != '/')
23 $root .= '/';
24
25 // This is the first thing we need to do.
26 require_once 'inc/exceptions.php';
27 require_once 'inc/config.php';
28
29 // What we need first is database and interface functions.
30 require_once 'inc/interface.php';
31 require_once 'inc/functions.php';
32 require_once 'inc/database.php';
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
38 if (file_exists ('inc/secret.php'))
39 require_once 'inc/secret.php';
40 else
41 {
42 showError
43 (
44 "Database connection parameters are read from inc/secret.php file, " .
45 "which cannot be found.\nYou probably need to complete the installation " .
46 "procedure by following <a href='${root}install.php'>this link</a>.",
47 __FILE__
48 );
49 exit (1);
50 }
51
52 // Now try to connect...
53 try
54 {
55 $dbxlink = new PDO ($pdo_dsn, $db_username, $db_password);
56 }
57 catch (PDOException $e)
58 {
59 showError ("Database connection failed:\n\n" . $e->getMessage(), __FILE__);
60 exit (1);
61 }
62
63 $dbxlink->exec ("set names 'utf8'");
64
65 if (get_magic_quotes_gpc())
66 foreach ($_REQUEST as $key => $value)
67 if (gettype ($value) == 'string')
68 $_REQUEST[$key] = stripslashes ($value);
69
70 if (!set_magic_quotes_runtime (0))
71 {
72 showError ('Failed to turn magic quotes off', __FILE__);
73 exit (1);
74 }
75
76 // Escape any globals before we ever try to use them, but keep a copy of originals.
77 $sic = array();
78 foreach ($_REQUEST as $key => $value)
79 {
80 $sic[$key] = dos2unix ($value);
81 if (gettype ($value) == 'string')
82 $_REQUEST[$key] = escapeString (dos2unix ($value));
83 }
84
85 if (isset ($_SERVER['PHP_AUTH_USER']))
86 $_SERVER['PHP_AUTH_USER'] = escapeString ($_SERVER['PHP_AUTH_USER']);
87 if (isset ($_SERVER['REMOTE_USER']))
88 $_SERVER['REMOTE_USER'] = escapeString ($_SERVER['REMOTE_USER']);
89
90 $dbver = getDatabaseVersion();
91 if ($dbver != CODE_VERSION)
92 {
93 echo '<p align=justify>This Racktables installation seems to be ' .
94 'just upgraded to version ' . CODE_VERSION . ', while the '.
95 'database version is ' . $dbver . '. No user will be ' .
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>';
99 exit (1);
100 }
101
102 if (!mb_internal_encoding ('UTF-8') or !mb_regex_encoding ('UTF-8'))
103 {
104 showError ('Failed setting multibyte string encoding to UTF-8', __FILE__);
105 exit (1);
106 }
107 $configCache = loadConfigCache();
108 if (!count ($configCache))
109 {
110 showError ('Failed to load configuration from the database.', __FILE__);
111 exit (1);
112 }
113
114 require_once 'inc/code.php'; // for getRackCode()
115 $rackCodeCache = loadScript ('RackCodeCache');
116 if ($rackCodeCache == NULL or !strlen ($rackCodeCache))
117 {
118 $rackCode = getRackCode (loadScript ('RackCode'));
119 saveScript ('RackCodeCache', base64_encode (serialize ($rackCode)));
120 }
121 else
122 {
123 $rackCode = unserialize (base64_decode ($rackCodeCache));
124 if ($rackCode === FALSE) // invalid cache
125 {
126 saveScript ('RackCodeCache', '');
127 $rackCode = getRackCode (loadScript ('RackCode'));
128 }
129 }
130
131 // Depending on the 'result' value the 'load' carries either the
132 // parse tree or error message.
133 if ($rackCode['result'] != 'ACK')
134 {
135 // FIXME: display a message with an option to reset RackCode text
136 showError ('Could not load the RackCode due to error: ' . $rackCode['load'], __FILE__);
137 exit (1);
138 }
139 $rackCode = $rackCode['load'];
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();
146
147 $entityCache = array();
148
149 $taglist = getTagList();
150 $tagtree = treeFromList ($taglist);
151 sortTree ($tagtree, 'taginfoCmp');
152
153 require_once 'inc/auth.php';
154 $auto_tags = array();
155 // Initial chain for the current user.
156 $user_given_tags = array();
157
158 if (!isset ($script_mode) or $script_mode !== TRUE)
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().
164 session_start();
165 }
166
167 $pageno = (isset ($_REQUEST['page'])) ? $_REQUEST['page'] : 'index';
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.
171
172 if (isset ($_REQUEST['tab']))
173 $tabno = $_REQUEST['tab'];
174 elseif (basename($_SERVER['PHP_SELF']) == 'index.php' and getConfigVar ('SHOW_LAST_TAB') == 'yes' and isset ($_SESSION['RTLT'][$pageno]))
175 {
176 $tabno = $_SESSION['RTLT'][$pageno];
177 $url = "${root}?page=$pageno&tab=$tabno";
178 foreach ($_GET as $name=>$value)
179 {
180 if ($name == 'page' or $name == 'tab') continue;
181 if (gettype($value) == 'array')
182 foreach($value as $v)
183 $url .= '&'.urlencode($name.'[]').'='.urlencode($v);
184 else
185 $url .= '&'.urlencode($name).'='.urlencode($value);
186 }
187 header('Location: '.$url);
188 exit();
189 }
190 else
191 $tabno = 'default';
192
193 $op = (isset ($_REQUEST['op'])) ? $_REQUEST['op'] : '';
194
195 require_once 'inc/navigation.php';
196 require_once 'inc/triggers.php';
197 require_once 'inc/gateways.php';
198 if (file_exists ('inc/local.php'))
199 require_once 'inc/local.php';
200
201 // These will be filled in by fixContext()
202 $expl_tags = array();
203 $impl_tags = array();
204 // Initial chain for the current target.
205 $target_given_tags = array();
206
207 ?>