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