1e309a8b9665532c83c1fb4198b6d803468de162
[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 allow reloading of pageno and tabno variables. pageno and tabno
7 * together participate in forming security context by generating
8 * related autotags.
9 *
10 */
11
12 $root = (empty($_SERVER['HTTPS'])?'http':'https').
13 '://'.
14 (isset($_SERVER['HTTP_HOST'])?$_SERVER['HTTP_HOST']:($_SERVER['SERVER_NAME'].($_SERVER['SERVER_PORT']=='80'?'':$_SERVER['SERVER_PORT']))).
15 dirname($_SERVER['PHP_SELF']);
16 if (substr ($root, -1) != '/')
17 $root .= '/';
18
19 // This is the first thing we need to do.
20 require_once 'inc/config.php';
21
22 // What we need first is database and interface functions.
23 require_once 'inc/interface.php';
24 require_once 'inc/functions.php';
25 require_once 'inc/database.php';
26 if (file_exists ('inc/secret.php'))
27 require_once 'inc/secret.php';
28 else
29 {
30 showError
31 (
32 "Database connection parameters are read from inc/secret.php file, " .
33 "which cannot be found.\nYou probably need to complete the installation " .
34 "procedure by following <a href='${root}install.php'>this link</a>."
35 );
36 die;
37 }
38
39 // Now try to connect...
40 try
41 {
42 $dbxlink = new PDO ($pdo_dsn, $db_username, $db_password);
43 }
44 catch (PDOException $e)
45 {
46 showError ("Database connection failed:\n\n" . $e->getMessage());
47 die();
48 }
49
50 $dbxlink->exec ("set names 'utf8'");
51
52 if (get_magic_quotes_gpc())
53 foreach ($_REQUEST as $key => $value)
54 if (gettype ($value) == 'string')
55 $_REQUEST[$key] = stripslashes ($value);
56
57 if (!set_magic_quotes_runtime (0))
58 {
59 showError ('Failed to turn magic quotes off');
60 die;
61 }
62
63 // Escape any globals before we ever try to use them.
64 foreach ($_REQUEST as $key => $value)
65 if (gettype ($value) == 'string')
66 $_REQUEST[$key] = escapeString ($value);
67
68 if (isset ($_SERVER['PHP_AUTH_USER']))
69 $_SERVER['PHP_AUTH_USER'] = escapeString ($_SERVER['PHP_AUTH_USER']);
70 if (isset ($_SERVER['PHP_AUTH_PW']))
71 $_SERVER['PHP_AUTH_PW'] = escapeString ($_SERVER['PHP_AUTH_PW']);
72
73 $dbver = getDatabaseVersion();
74 if ($dbver != CODE_VERSION)
75 {
76 echo '<p align=justify>This Racktables installation seems to be ' .
77 'just upgraded to version ' . CODE_VERSION . ', while the '.
78 'database version is ' . $dbver . '. No user will be ' .
79 'either authenticated or shown any page until the upgrade is ' .
80 "finished. Follow <a href='${root}upgrade.php'>this link</a> and " .
81 'authenticate as administrator to finish the upgrade.</p>';
82 die;
83 }
84
85 if (!mb_internal_encoding ('UTF-8') or !mb_regex_encoding ('UTF-8'))
86 {
87 showError ('Failed setting multibyte string encoding to UTF-8');
88 die;
89 }
90 $configCache = loadConfigCache();
91 if (!count ($configCache))
92 {
93 showError ('Failed to load configuration from the database.');
94 die();
95 }
96
97 require_once 'inc/code.php';
98 $rackCodeCache = loadScript ('RackCodeCache');
99 if ($rackCodeCache == NULL or empty ($rackCodeCache))
100 {
101 // $t1 = microtime (TRUE);
102 $rackCode = getRackCode (loadScript ('RackCode'));
103 // $t2 = microtime (TRUE);
104 // echo 'DEBUG: parsed RackCode tree from scratch in ' . ($t2 - $t1) . ' second(s)<br>';
105 saveScript ('RackCodeCache', base64_encode (serialize ($rackCode)));
106 }
107 else
108 {
109 // $t1 = microtime (TRUE);
110 $rackCode = unserialize (base64_decode ($rackCodeCache));
111 // $t2 = microtime (TRUE);
112 // echo 'DEBUG: loaded RackCode cache in ' . ($t2 - $t1) . ' second(s)<br>';
113 if ($rackCode === FALSE) // invalid cache
114 {
115 saveScript ('RackCodeCache', '');
116 // $t1 = microtime (TRUE);
117 $rackCode = getRackCode (loadScript ('RackCode'));
118 // $t2 = microtime (TRUE);
119 // echo 'DEBUG: discarded RackCode cache and parsed tree from scratch in ' . ($t2 - $t1) . ' second(s)<br>';
120 }
121 }
122
123 // Depending on the 'result' value the 'load' carries either the
124 // parse tree or error message.
125 if ($rackCode['result'] != 'ACK')
126 {
127 // FIXME: display a message with an option to reset RackCode text
128 showError ('Could not load the RackCode due to error: ' . $rackCode['load'], __FUNCTION__);
129 die;
130 }
131 $rackCode = $rackCode['load'];
132
133 // Now init authentication.
134
135 require_once 'inc/auth.php';
136 // Load access database once.
137 $accounts = getUserAccounts();
138 if ($accounts === NULL)
139 {
140 showError ('Failed to initialize access database.');
141 die();
142 }
143
144 authenticate();
145
146 // Authentication passed.
147 // Note that we don't perform autorization here, so each 1st level page
148 // has to do it in its way, e.g. to call authorize().
149
150 $remote_username = $_SERVER['PHP_AUTH_USER'];
151 $pageno = (isset ($_REQUEST['page'])) ? $_REQUEST['page'] : 'index';
152 $tabno = (isset ($_REQUEST['tab'])) ? $_REQUEST['tab'] : 'default';
153 $op = (isset ($_REQUEST['op'])) ? $_REQUEST['op'] : '';
154 // Order matters here.
155 $taglist = getTagList();
156 $tagtree = getTagTree();
157
158 require_once 'inc/navigation.php';
159 require_once 'inc/pagetitles.php';
160 require_once 'inc/ophandlers.php';
161 require_once 'inc/triggers.php';
162 require_once 'inc/gateways.php';
163 require_once 'inc/snmp.php';
164
165 // These will be filled in by fixContext()
166 $auto_tags = array();
167 $expl_tags = array();
168 $impl_tags = array();
169 // and this will remain constant
170 $user_tags = loadUserTags ($accounts[$remote_username]['user_id']);
171 $user_tags = array_merge ($user_tags, getImplicitTags ($user_tags), getUserAutoTags());
172
173 ?>