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