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