r4170 getDatabaseVersion(): move to upgrade.php, where it fits better
[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 loadConfigDefaults();
75
76 if (getConfigVar ('DB_VERSION') != CODE_VERSION)
77 {
78 echo '<p align=justify>This Racktables installation seems to be ' .
79 'just upgraded to version ' . CODE_VERSION . ', while the '.
80 'database version is ' . getConfigVar ('DB_VERSION') . '.<br>No user will be ' .
81 'either authenticated or shown any page until the upgrade is ' .
82 "finished.<br>Follow <a href='upgrade.php'>this link</a> and " .
83 'authenticate as administrator to finish the upgrade.</p>';
84 exit (1);
85 }
86
87 if (!mb_internal_encoding ('UTF-8'))
88 throw new RackTablesError ('Failed setting multibyte string encoding to UTF-8', RackTablesError::INTERNAL);
89
90 $rackCodeCache = loadScript ('RackCodeCache');
91 if ($rackCodeCache == NULL or !strlen ($rackCodeCache))
92 {
93 $rackCode = getRackCode (loadScript ('RackCode'));
94 saveScript ('RackCodeCache', base64_encode (serialize ($rackCode)));
95 }
96 else
97 {
98 $rackCode = unserialize (base64_decode ($rackCodeCache));
99 if ($rackCode === FALSE) // invalid cache
100 {
101 saveScript ('RackCodeCache', '');
102 $rackCode = getRackCode (loadScript ('RackCode'));
103 }
104 }
105
106 // Depending on the 'result' value the 'load' carries either the
107 // parse tree or error message. The latter case is a bug, because
108 // RackCode saving function was supposed to validate its input.
109 if ($rackCode['result'] != 'ACK')
110 throw new RackTablesError ($rackCode['load'], RackTablesError::INTERNAL);
111 $rackCode = $rackCode['load'];
112 // Only call buildPredicateTable() once and save the result, because it will remain
113 // constant during one execution for constraints processing.
114 $pTable = buildPredicateTable ($rackCode);
115 // Constraints parse trees aren't cached in the database, so the least to keep
116 // things running is to maintain application cache for them.
117 $parseCache = array();
118 $entityCache = array();
119 // used by getExplicitTagsOnly()
120 $tagRelCache = array();
121
122 $taglist = getTagList();
123 $tagtree = treeFromList ($taglist);
124 sortTree ($tagtree, 'taginfoCmp');
125
126 require_once 'auth.php';
127 $auto_tags = array();
128 // Initial chain for the current user.
129 $user_given_tags = array();
130
131 // This also can be modified in local.php.
132 $pageheaders = array
133 (
134 100 => "<link rel='ICON' type='image/x-icon' href='favicon.ico' />",
135 );
136 addCSS ('css/pi.css');
137
138 if (!isset ($script_mode) or $script_mode !== TRUE)
139 {
140 // A successful call to authenticate() always generates autotags and somethimes
141 // even given/implicit tags. It also sets remote_username and remote_displayname.
142 authenticate();
143 // Authentication passed.
144 // Note that we don't perform autorization here, so each 1st level page
145 // has to do it in its way, e.g. by calling authorize() after fixContext().
146 session_start();
147 }
148 else
149 {
150 // Some functions require remote_username to be set to something to act correctly,
151 // even though they don't use the value itself.
152 $admin_account = spotEntity ('user', 1);
153 $remote_username = $admin_account['user_name'];
154 unset ($admin_account);
155 }
156
157 alterConfigWithUserPreferences();
158
159 require_once 'navigation.php';
160 require_once 'triggers.php';
161
162
163 $op = '';
164 require_once 'gateways.php';
165 if (file_exists ('inc/local.php'))
166 require_once 'inc/local.php';
167
168 // These will be filled in by fixContext()
169 $expl_tags = array();
170 $impl_tags = array();
171 // Initial chain for the current target.
172 $target_given_tags = array();
173
174 require_once 'IPv6.php';
175
176 ?>