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