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