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