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