r3728 Returned a GET/POST/COOKIE part of magic_quotes trick, now
[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 // This is the first thing we need to do.
13 require_once 'inc/exceptions.php';
14 require_once 'inc/config.php';
15
16 // What we need first is database and interface functions.
17 require_once 'inc/functions.php';
18 require_once 'inc/database.php';
19 // Always have default values for these options, so if a user didn't
20 // care to set, something would be working anyway.
21 $user_auth_src = 'database';
22 $require_local_account = TRUE;
23
24 function showError ($info = '', $location = 'N/A')
25 {
26 if (preg_match ('/\.php$/', $location))
27 $location = basename ($location);
28 elseif ($location != 'N/A')
29 $location = $location . '()';
30 echo "<div class=msg_error>An error has occured in [${location}]. ";
31 if (!strlen ($info))
32 echo 'No additional information is available.';
33 else
34 echo "Additional information:<br><p>\n<pre>\n${info}\n</pre></p>";
35 echo "Go back or try starting from <a href='".makeHref()."'>index page</a>.<br></div>\n";
36 }
37
38 /*
39 * This is almost a clone of showError(). This is added to get rid of
40 * cases when script dies after showError() is shown.
41 */
42
43 function showWarning ($info = '', $location = 'N/A')
44 {
45 if (preg_match ('/\.php$/', $location))
46 $location = basename ($location);
47 elseif ($location != 'N/A')
48 $location = $location . '()';
49 echo "<div class=msg_error>Warning event at [${location}]. ";
50 if (!strlen ($info))
51 echo 'No additional information is available.';
52 else
53 echo "Additional information:<br><p>\n<pre>\n${info}\n</pre></p>";
54 }
55
56
57
58 if (file_exists ('inc/secret.php'))
59 require_once 'inc/secret.php';
60 else
61 {
62 throw new Exception
63 (
64 "Database connection parameters are read from inc/secret.php file, " .
65 "which cannot be found.\nYou probably need to complete the installation " .
66 "procedure by following <a href='install.php'>this link</a>.",
67 E_INTERNAL
68 );
69 }
70
71 // Now try to connect...
72 try
73 {
74 $dbxlink = new PDO ($pdo_dsn, $db_username, $db_password);
75 }
76 catch (PDOException $e)
77 {
78 throw new Exception ("Database connection failed:\n\n" . $e->getMessage(), E_INTERNAL);
79 }
80 $dbxlink->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
81 $dbxlink->exec ("set names 'utf8'");
82
83 // Magic quotes feature is deprecated, but just in case the local system
84 // still has it activated, reverse its effect.
85 if (function_exists ('get_magic_quotes_gpc') and get_magic_quotes_gpc())
86 foreach ($_REQUEST as $key => $value)
87 if (gettype ($value) == 'string')
88 $_REQUEST[$key] = stripslashes ($value);
89
90 // Escape any globals before we ever try to use them, but keep a copy of originals.
91 $sic = array();
92 foreach ($_REQUEST as $key => $value)
93 {
94 $sic[$key] = dos2unix ($value);
95 if (gettype ($value) == 'string')
96 $_REQUEST[$key] = escapeString (dos2unix ($value));
97 }
98
99 if (isset ($_SERVER['PHP_AUTH_USER']))
100 $_SERVER['PHP_AUTH_USER'] = escapeString ($_SERVER['PHP_AUTH_USER']);
101 if (isset ($_SERVER['REMOTE_USER']))
102 $_SERVER['REMOTE_USER'] = escapeString ($_SERVER['REMOTE_USER']);
103
104 $dbver = getDatabaseVersion();
105 if ($dbver != CODE_VERSION)
106 {
107 echo '<p align=justify>This Racktables installation seems to be ' .
108 'just upgraded to version ' . CODE_VERSION . ', while the '.
109 'database version is ' . $dbver . '. No user will be ' .
110 'either authenticated or shown any page until the upgrade is ' .
111 "finished. Follow <a href='upgrade.php'>this link</a> and " .
112 'authenticate as administrator to finish the upgrade.</p>';
113 exit (1);
114 }
115
116 if (!mb_internal_encoding ('UTF-8') or !mb_regex_encoding ('UTF-8'))
117 throw new Exception ('Failed setting multibyte string encoding to UTF-8', E_INTERNAL);
118
119 loadConfigDefaults();
120
121 require_once 'inc/code.php'; // for getRackCode()
122 $rackCodeCache = loadScript ('RackCodeCache');
123 if ($rackCodeCache == NULL or !strlen ($rackCodeCache))
124 {
125 $rackCode = getRackCode (loadScript ('RackCode'));
126 saveScript ('RackCodeCache', base64_encode (serialize ($rackCode)));
127 }
128 else
129 {
130 $rackCode = unserialize (base64_decode ($rackCodeCache));
131 if ($rackCode === FALSE) // invalid cache
132 {
133 saveScript ('RackCodeCache', '');
134 $rackCode = getRackCode (loadScript ('RackCode'));
135 }
136 }
137
138 // Depending on the 'result' value the 'load' carries either the
139 // parse tree or error message.
140 if ($rackCode['result'] != 'ACK')
141 throw new Exception ($rackCode['load'], E_BAD_RACKCODE);
142 $rackCode = $rackCode['load'];
143 // Only call buildPredicateTable() once and save the result, because it will remain
144 // constant during one execution for constraints processing.
145 $pTable = buildPredicateTable ($rackCode);
146 // Constraints parse trees aren't cached in the database, so the least to keep
147 // things running is to maintain application cache for them.
148 $parseCache = array();
149 $entityCache = array();
150 // used by getExplicitTagsOnly()
151 $tagRelCache = array();
152
153 $taglist = getTagList();
154 $tagtree = treeFromList ($taglist);
155 sortTree ($tagtree, 'taginfoCmp');
156
157 require_once 'inc/auth.php';
158 $auto_tags = array();
159 // Initial chain for the current user.
160 $user_given_tags = array();
161
162 // This also can be modified in local.php.
163 $pageheaders = array
164 (
165 100 => "<link rel='STYLESHEET' type='text/css' href='css/pi.css' />",
166 101 => "<link rel='STYLESHEET' type='text/css' href='css/dhtmlxcombo.css' />",
167 102 => "<link rel='ICON' type='image/x-icon' href='pix/racktables.ico' />",
168 200 => "<script language='javascript' type='text/javascript' src='js/racktables.js'></script>",
169 201 => "<script language='javascript' type='text/javascript' src='js/jquery-1.3.1.min.js'></script>",
170 202 => "<script language='javascript' type='text/javascript' src='js/live_validation.js'></script>",
171 203 => "<script language='javascript' type='text/javascript' src='js/codepress/codepress.js'></script>",
172 204 => "<script language='javascript' type='text/javascript' src='js/dhtmlxcommon.js'></script>",
173 205 => "<script language='javascript' type='text/javascript' src='js/dhtmlxcombo.js'></script>",
174 206 => "<script language='javascript' type='text/javascript' src='js/dhtmlxcombo_whp.js'></script>",
175 207 => "<script language='javascript'>window.dhx_globalImgPath='pix/'</script>",
176
177 );
178
179 if (!isset ($script_mode) or $script_mode !== TRUE)
180 {
181 // A successful call to authenticate() always generates autotags and somethimes
182 // even given/implicit tags. It also sets remote_username and remote_displayname.
183 authenticate();
184 // Authentication passed.
185 // Note that we don't perform autorization here, so each 1st level page
186 // has to do it in its way, e.g. by calling authorize() after fixContext().
187 session_start();
188 }
189 else
190 {
191 // Some functions require remote_username to be set to something to act correctly,
192 // even though they don't use the value itself.
193 $admin_account = spotEntity ('user', 1);
194 $remote_username = $admin_account['user_name'];
195 unset ($admin_account);
196 }
197
198 alterConfigWithUserPreferences();
199
200 require_once 'inc/navigation.php';
201 require_once 'inc/triggers.php';
202
203
204 $op = '';
205 require_once 'inc/gateways.php';
206 if (file_exists ('inc/local.php'))
207 require_once 'inc/local.php';
208
209 // These will be filled in by fixContext()
210 $expl_tags = array();
211 $impl_tags = array();
212 // Initial chain for the current target.
213 $target_given_tags = array();
214
215 ?>