r1994 + rework RackCode parsing error handling to avoid premature script termination
[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 form security context.
8 *
9 */
10
11 $root = (empty($_SERVER['HTTPS'])?'http':'https').
12 '://'.
13 (isset($_SERVER['HTTP_HOST'])?$_SERVER['HTTP_HOST']:($_SERVER['SERVER_NAME'].($_SERVER['SERVER_PORT']=='80'?'':$_SERVER['SERVER_PORT']))).
14 dirname($_SERVER['PHP_SELF']);
15 if (substr ($root, -1) != '/')
16 $root .= '/';
17
18 // This is the first thing we need to do.
19 require_once 'inc/config.php';
20
21 // What we need first is database and interface functions.
22 require_once 'inc/interface.php';
23 require_once 'inc/functions.php';
24 require_once 'inc/database.php';
25 if (file_exists ('inc/secret.php'))
26 require_once 'inc/secret.php';
27 else
28 {
29 showError
30 (
31 "Database connection parameters are read from inc/secret.php file, " .
32 "which cannot be found.\nYou probably need to complete the installation " .
33 "procedure by following <a href='${root}install.php'>this link</a>."
34 );
35 die;
36 }
37
38 // Now try to connect...
39 try
40 {
41 $dbxlink = new PDO ($pdo_dsn, $db_username, $db_password);
42 }
43 catch (PDOException $e)
44 {
45 showError ("Database connection failed:\n\n" . $e->getMessage());
46 die();
47 }
48
49 $dbxlink->exec ("set names 'utf8'");
50 mb_internal_encoding ("UTF-8");
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 $configCache = loadConfigCache();
86 if (!count ($configCache))
87 {
88 showError ('Failed to load configuration from the database.');
89 die();
90 }
91
92 require_once 'inc/code.php';
93 // Depending on the 'result' value the 'load' carries either the
94 // parse tree or error message.
95 $rackCode = getRackCode (loadScript ('RackCode'));
96 if ($rackCode['result'] != 'ACK')
97 {
98 // FIXME: display a message with an option to reset RackCode text
99 showError ('Could not load the RackCode due to error: ' . $rackCode['load'], __FUNCTION__);
100 die;
101 }
102 $rackCode = $rackCode['load'];
103
104 // Now init authentication.
105
106 require_once 'inc/auth.php';
107 // Load access database once.
108 $accounts = getUserAccounts();
109 if ($accounts === NULL)
110 {
111 showError ('Failed to initialize access database.');
112 die();
113 }
114
115 authenticate();
116
117 // Authentication passed.
118 // Note that we don't perform autorization here, so each 1st level page
119 // has to do it in its way, e.g. to call authorize().
120
121 $remote_username = $_SERVER['PHP_AUTH_USER'];
122 $pageno = (isset ($_REQUEST['page'])) ? $_REQUEST['page'] : 'index';
123 $tabno = (isset ($_REQUEST['tab'])) ? $_REQUEST['tab'] : 'default';
124 $op = (isset ($_REQUEST['op'])) ? $_REQUEST['op'] : '';
125 // Order matters here.
126 $taglist = getTagList();
127 $tagtree = getTagTree();
128
129 require_once 'inc/navigation.php';
130 require_once 'inc/pagetitles.php';
131 require_once 'inc/ophandlers.php';
132 require_once 'inc/triggers.php';
133 require_once 'inc/gateways.php';
134 require_once 'inc/snmp.php';
135
136 // These will be filled in by fixContext()
137 $auto_tags = array();
138 $expl_tags = array();
139 $impl_tags = array();
140 // and this will remain constant
141 $user_tags = loadUserTags ($accounts[$remote_username]['user_id']);
142 $user_tags = array_merge ($user_tags, getImplicitTags ($user_tags), getUserAutoTags());
143
144 ?>