r3010 - maintenance->trunk sync of changesets 3008~3009
[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.
e673ee24
DO
30require_once 'inc/functions.php';
31require_once 'inc/database.php';
2c9fde3a
DO
32// Always have default values for these options, so if a user didn't
33// care to set, something would be working anyway.
34$user_auth_src = 'database';
35$require_local_account = TRUE;
36
f77323f1
DO
37function showError ($info = '', $location = 'N/A')
38{
39 if (preg_match ('/\.php$/', $location))
40 $location = basename ($location);
41 elseif ($location != 'N/A')
42 $location = $location . '()';
43 echo "<div class=msg_error>An error has occured in [${location}]. ";
44 if (!strlen ($info))
45 echo 'No additional information is available.';
46 else
47 echo "Additional information:<br><p>\n<pre>\n${info}\n</pre></p>";
48 echo "Go back or try starting from <a href='".makeHref()."'>index page</a>.<br></div>\n";
49}
50
80138748
DO
51if (file_exists ('inc/secret.php'))
52 require_once 'inc/secret.php';
e673ee24
DO
53else
54{
55 showError
56 (
80138748 57 "Database connection parameters are read from inc/secret.php file, " .
e6093686 58 "which cannot be found.\nYou probably need to complete the installation " .
b0348307
DO
59 "procedure by following <a href='${root}install.php'>this link</a>.",
60 __FILE__
e673ee24 61 );
7ab82114 62 exit (1);
e673ee24
DO
63}
64
65// Now try to connect...
66try
67{
68 $dbxlink = new PDO ($pdo_dsn, $db_username, $db_password);
69}
70catch (PDOException $e)
71{
b0348307 72 showError ("Database connection failed:\n\n" . $e->getMessage(), __FILE__);
7ab82114 73 exit (1);
e673ee24
DO
74}
75
4d55392a
DO
76$dbxlink->exec ("set names 'utf8'");
77
b8d1ab66
DO
78if (get_magic_quotes_gpc())
79 foreach ($_REQUEST as $key => $value)
f4d511df
DO
80 if (gettype ($value) == 'string')
81 $_REQUEST[$key] = stripslashes ($value);
b8d1ab66
DO
82
83if (!set_magic_quotes_runtime (0))
84{
b0348307 85 showError ('Failed to turn magic quotes off', __FILE__);
7ab82114 86 exit (1);
b8d1ab66 87}
6bae5fbb 88
f3d274bf 89// Escape any globals before we ever try to use them, but keep a copy of originals.
f8874cdb 90$sic = array();
e673ee24 91foreach ($_REQUEST as $key => $value)
f8874cdb
DO
92{
93 $sic[$key] = dos2unix ($value);
f4d511df 94 if (gettype ($value) == 'string')
f8874cdb
DO
95 $_REQUEST[$key] = escapeString (dos2unix ($value));
96}
6bae5fbb 97
e673ee24
DO
98if (isset ($_SERVER['PHP_AUTH_USER']))
99 $_SERVER['PHP_AUTH_USER'] = escapeString ($_SERVER['PHP_AUTH_USER']);
dc9ea133
DO
100if (isset ($_SERVER['REMOTE_USER']))
101 $_SERVER['REMOTE_USER'] = escapeString ($_SERVER['REMOTE_USER']);
e673ee24 102
fbbb74fb
DO
103$dbver = getDatabaseVersion();
104if ($dbver != CODE_VERSION)
105{
106 echo '<p align=justify>This Racktables installation seems to be ' .
107 'just upgraded to version ' . CODE_VERSION . ', while the '.
db914a6c 108 'database version is ' . $dbver . '. No user will be ' .
fbbb74fb
DO
109 'either authenticated or shown any page until the upgrade is ' .
110 "finished. Follow <a href='${root}upgrade.php'>this link</a> and " .
111 'authenticate as administrator to finish the upgrade.</p>';
7ab82114 112 exit (1);
fbbb74fb
DO
113}
114
26131670
DO
115if (!mb_internal_encoding ('UTF-8') or !mb_regex_encoding ('UTF-8'))
116{
b0348307 117 showError ('Failed setting multibyte string encoding to UTF-8', __FILE__);
7ab82114 118 exit (1);
26131670 119}
06f23fd4
DO
120$configCache = loadConfigCache();
121if (!count ($configCache))
122{
b0348307 123 showError ('Failed to load configuration from the database.', __FILE__);
7ab82114 124 exit (1);
06f23fd4
DO
125}
126
204284ba 127require_once 'inc/code.php'; // for getRackCode()
4a6a28f1 128$rackCodeCache = loadScript ('RackCodeCache');
59a83bd8 129if ($rackCodeCache == NULL or !strlen ($rackCodeCache))
4a6a28f1 130{
4a6a28f1 131 $rackCode = getRackCode (loadScript ('RackCode'));
4a6a28f1
DO
132 saveScript ('RackCodeCache', base64_encode (serialize ($rackCode)));
133}
134else
135{
4a6a28f1 136 $rackCode = unserialize (base64_decode ($rackCodeCache));
4a6a28f1
DO
137 if ($rackCode === FALSE) // invalid cache
138 {
139 saveScript ('RackCodeCache', '');
4a6a28f1 140 $rackCode = getRackCode (loadScript ('RackCode'));
4a6a28f1
DO
141 }
142}
143
cf25e649
DO
144// Depending on the 'result' value the 'load' carries either the
145// parse tree or error message.
cf25e649
DO
146if ($rackCode['result'] != 'ACK')
147{
148 // FIXME: display a message with an option to reset RackCode text
b0348307 149 showError ('Could not load the RackCode due to error: ' . $rackCode['load'], __FILE__);
7ab82114 150 exit (1);
cf25e649
DO
151}
152$rackCode = $rackCode['load'];
7ddbcf59
DO
153// Only call buildPredicateTable() once and save the result, because it will remain
154// constant during one execution for constraints processing.
155$pTable = buildPredicateTable ($rackCode);
156// Constraints parse trees aren't cached in the database, so the least to keep
157// things running is to maintain application cache for them.
158$parseCache = array();
7cfc98b1 159$entityCache = array();
5fd2a004
DO
160// used by getExplicitTagsOnly()
161$tagRelCache = array();
7cfc98b1 162
573214e0
DO
163$taglist = getTagList();
164$tagtree = treeFromList ($taglist);
165sortTree ($tagtree, 'taginfoCmp');
166
e673ee24 167require_once 'inc/auth.php';
d5262485 168$auto_tags = array();
b82cce3f
DO
169// Initial chain for the current user.
170$user_given_tags = array();
e673ee24 171
deb18b61 172if (!isset ($script_mode) or $script_mode !== TRUE)
b82cce3f 173{
39b0408a
DO
174 // A successful call to authenticate() always generates autotags and somethimes
175 // even given/implicit tags. It also sets remote_username and remote_displayname.
176 authenticate();
b82cce3f
DO
177 // Authentication passed.
178 // Note that we don't perform autorization here, so each 1st level page
179 // has to do it in its way, e.g. by calling authorize() after fixContext().
deb18b61 180 session_start();
b82cce3f 181}
39b0408a
DO
182else
183{
184 // Some functions require remote_username to be set to something to act correctly,
185 // even though they don't use the value itself.
186 $admin_account = spotEntity ('user', 1);
187 $remote_username = $admin_account['user_name'];
188 unset ($admin_account);
189}
83555c04 190
e673ee24 191$pageno = (isset ($_REQUEST['page'])) ? $_REQUEST['page'] : 'index';
b0348307
DO
192// Special handling of tab number to substitute the "last" index where applicable.
193// Always show explicitly requested tab, substitute the last used name in case
194// it is awailable, fall back to the default one.
da111736 195
b0348307
DO
196if (isset ($_REQUEST['tab']))
197 $tabno = $_REQUEST['tab'];
da111736 198elseif (basename($_SERVER['PHP_SELF']) == 'index.php' and getConfigVar ('SHOW_LAST_TAB') == 'yes' and isset ($_SESSION['RTLT'][$pageno]))
050b68b5 199{
3b34b607 200 $tabno = $_SESSION['RTLT'][$pageno];
050b68b5
DY
201 $url = "${root}?page=$pageno&tab=$tabno";
202 foreach ($_GET as $name=>$value)
203 {
204 if ($name == 'page' or $name == 'tab') continue;
58652ac1
DY
205 if (gettype($value) == 'array')
206 foreach($value as $v)
207 $url .= '&'.urlencode($name.'[]').'='.urlencode($v);
208 else
209 $url .= '&'.urlencode($name).'='.urlencode($value);
050b68b5
DY
210 }
211 header('Location: '.$url);
212 exit();
213}
b0348307
DO
214else
215 $tabno = 'default';
050b68b5 216
da958e52 217$op = (isset ($_REQUEST['op'])) ? $_REQUEST['op'] : '';
b0348307 218
e673ee24 219require_once 'inc/navigation.php';
641fe9b0 220require_once 'inc/triggers.php';
d33645ff 221require_once 'inc/gateways.php';
80138748
DO
222if (file_exists ('inc/local.php'))
223 require_once 'inc/local.php';
e673ee24 224
4c9b513a 225// These will be filled in by fixContext()
2fb24351
DO
226$expl_tags = array();
227$impl_tags = array();
4c9b513a
DO
228// Initial chain for the current target.
229$target_given_tags = array();
2fb24351 230
e673ee24 231?>