1a39468c5cb112e3034b7a6ff0d4ca34b77f0ff2
[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 require_once 'exceptions.php';
13 require_once 'config.php';
14 require_once 'functions.php';
15 require_once 'database.php';
16 // Always have default values for these options, so if a user didn't
17 // care to set, something would be working anyway.
18 $user_auth_src = 'database';
19 $require_local_account = TRUE;
20
21 // (re)connects to DB, stores PDO object in $dbxlink global var
22 function connectDB()
23 {
24 global $dbxlink, $pdo_dsn, $db_username, $db_password;
25 $dbxlink = NULL;
26 // Now try to connect...
27 try
28 {
29 $dbxlink = new PDO ($pdo_dsn, $db_username, $db_password);
30 }
31 catch (PDOException $e)
32 {
33 throw new RackTablesError ("Database connection failed:\n\n" . $e->getMessage(), RackTablesError::INTERNAL);
34 }
35 $dbxlink->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
36 $dbxlink->exec ("set names 'utf8'");
37 }
38
39 if (file_exists ('inc/secret.php'))
40 require_once 'inc/secret.php';
41 else
42 {
43 throw new RackTablesError
44 (
45 "Database connection parameters are read from inc/secret.php file, " .
46 "which cannot be found.<br>You probably need to complete the installation " .
47 "procedure by following <a href='install.php'>this link</a>.",
48 RackTablesError::MISCONFIGURED
49 );
50 }
51 connectDB();
52
53 // Magic quotes feature is deprecated, but just in case the local system
54 // still has it activated, reverse its effect.
55 if (function_exists ('get_magic_quotes_gpc') and get_magic_quotes_gpc())
56 foreach ($_REQUEST as $key => $value)
57 if (gettype ($value) == 'string')
58 $_REQUEST[$key] = stripslashes ($value);
59
60 // Escape any globals before we ever try to use them, but keep a copy of originals.
61 $sic = array();
62 foreach ($_REQUEST as $key => $value)
63 {
64 $sic[$key] = dos2unix ($value);
65 if (gettype ($value) == 'string')
66 $_REQUEST[$key] = escapeString (dos2unix ($value));
67 }
68
69 if (isset ($_SERVER['PHP_AUTH_USER']))
70 $_SERVER['PHP_AUTH_USER'] = escapeString ($_SERVER['PHP_AUTH_USER']);
71 if (isset ($_SERVER['REMOTE_USER']))
72 $_SERVER['REMOTE_USER'] = escapeString ($_SERVER['REMOTE_USER']);
73
74 $dbver = getDatabaseVersion();
75 if ($dbver != CODE_VERSION)
76 {
77 echo '<p align=justify>This Racktables installation seems to be ' .
78 'just upgraded to version ' . CODE_VERSION . ', while the '.
79 'database version is ' . $dbver . '.<br>No user will be ' .
80 'either authenticated or shown any page until the upgrade is ' .
81 "finished.<br>Follow <a href='upgrade.php'>this link</a> and " .
82 'authenticate as administrator to finish the upgrade.</p>';
83 exit (1);
84 }
85
86 if (!mb_internal_encoding ('UTF-8'))
87 throw new RackTablesError ('Failed setting multibyte string encoding to UTF-8', RackTablesError::INTERNAL);
88
89 loadConfigDefaults();
90
91 $rackCodeCache = loadScript ('RackCodeCache');
92 if ($rackCodeCache == NULL or !strlen ($rackCodeCache))
93 {
94 $rackCode = getRackCode (loadScript ('RackCode'));
95 saveScript ('RackCodeCache', base64_encode (serialize ($rackCode)));
96 }
97 else
98 {
99 $rackCode = unserialize (base64_decode ($rackCodeCache));
100 if ($rackCode === FALSE) // invalid cache
101 {
102 saveScript ('RackCodeCache', '');
103 $rackCode = getRackCode (loadScript ('RackCode'));
104 }
105 }
106
107 // Depending on the 'result' value the 'load' carries either the
108 // parse tree or error message. The latter case is a bug, because
109 // RackCode saving function was supposed to validate its input.
110 if ($rackCode['result'] != 'ACK')
111 throw new RackTablesError ($rackCode['load'], RackTablesError::INTERNAL);
112 $rackCode = $rackCode['load'];
113 // Only call buildPredicateTable() once and save the result, because it will remain
114 // constant during one execution for constraints processing.
115 $pTable = buildPredicateTable ($rackCode);
116 // Constraints parse trees aren't cached in the database, so the least to keep
117 // things running is to maintain application cache for them.
118 $parseCache = array();
119 $entityCache = array();
120 // used by getExplicitTagsOnly()
121 $tagRelCache = array();
122
123 $taglist = getTagList();
124 $tagtree = treeFromList ($taglist);
125 sortTree ($tagtree, 'taginfoCmp');
126
127 require_once 'auth.php';
128 $auto_tags = array();
129 // Initial chain for the current user.
130 $user_given_tags = array();
131
132 // This also can be modified in local.php.
133 $pageheaders = array
134 (
135 100 => "<link rel='ICON' type='image/x-icon' href='favicon.ico' />",
136 );
137 addCSS ('css/pi.css');
138
139 if (!isset ($script_mode) or $script_mode !== TRUE)
140 {
141 // A successful call to authenticate() always generates autotags and somethimes
142 // even given/implicit tags. It also sets remote_username and remote_displayname.
143 authenticate();
144 // Authentication passed.
145 // Note that we don't perform autorization here, so each 1st level page
146 // has to do it in its way, e.g. by calling authorize() after fixContext().
147 session_start();
148 }
149 else
150 {
151 // Some functions require remote_username to be set to something to act correctly,
152 // even though they don't use the value itself.
153 $admin_account = spotEntity ('user', 1);
154 $remote_username = $admin_account['user_name'];
155 unset ($admin_account);
156 }
157
158 alterConfigWithUserPreferences();
159
160 require_once 'navigation.php';
161 require_once 'triggers.php';
162
163
164 $op = '';
165 require_once 'gateways.php';
166 if (file_exists ('inc/local.php'))
167 require_once 'inc/local.php';
168
169 // These will be filled in by fixContext()
170 $expl_tags = array();
171 $impl_tags = array();
172 // Initial chain for the current target.
173 $target_given_tags = array();
174
175 require_once 'IPv6.php';
176
177 ?>