5050bc75647f8772fbdc0bbdc80c65b1828e526b
[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 // Escape any globals before we ever try to use them, but keep a copy of originals.
84 $sic = array();
85 foreach ($_REQUEST as $key => $value)
86 {
87 $sic[$key] = dos2unix ($value);
88 if (gettype ($value) == 'string')
89 $_REQUEST[$key] = escapeString (dos2unix ($value));
90 }
91
92 if (isset ($_SERVER['PHP_AUTH_USER']))
93 $_SERVER['PHP_AUTH_USER'] = escapeString ($_SERVER['PHP_AUTH_USER']);
94 if (isset ($_SERVER['REMOTE_USER']))
95 $_SERVER['REMOTE_USER'] = escapeString ($_SERVER['REMOTE_USER']);
96
97 $dbver = getDatabaseVersion();
98 if ($dbver != 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 ' . $dbver . '. No user will be ' .
103 'either authenticated or shown any page until the upgrade is ' .
104 "finished. Follow <a href='upgrade.php'>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') or !mb_regex_encoding ('UTF-8'))
110 throw new Exception ('Failed setting multibyte string encoding to UTF-8', E_INTERNAL);
111
112 loadConfigDefaults();
113
114 require_once 'inc/code.php'; // for getRackCode()
115 $rackCodeCache = loadScript ('RackCodeCache');
116 if ($rackCodeCache == NULL or !strlen ($rackCodeCache))
117 {
118 $rackCode = getRackCode (loadScript ('RackCode'));
119 saveScript ('RackCodeCache', base64_encode (serialize ($rackCode)));
120 }
121 else
122 {
123 $rackCode = unserialize (base64_decode ($rackCodeCache));
124 if ($rackCode === FALSE) // invalid cache
125 {
126 saveScript ('RackCodeCache', '');
127 $rackCode = getRackCode (loadScript ('RackCode'));
128 }
129 }
130
131 // Depending on the 'result' value the 'load' carries either the
132 // parse tree or error message.
133 if ($rackCode['result'] != 'ACK')
134 throw new Exception ($rackCode['load'], E_BAD_RACKCODE);
135 $rackCode = $rackCode['load'];
136 // Only call buildPredicateTable() once and save the result, because it will remain
137 // constant during one execution for constraints processing.
138 $pTable = buildPredicateTable ($rackCode);
139 // Constraints parse trees aren't cached in the database, so the least to keep
140 // things running is to maintain application cache for them.
141 $parseCache = array();
142 $entityCache = array();
143 // used by getExplicitTagsOnly()
144 $tagRelCache = array();
145
146 $taglist = getTagList();
147 $tagtree = treeFromList ($taglist);
148 sortTree ($tagtree, 'taginfoCmp');
149
150 require_once 'inc/auth.php';
151 $auto_tags = array();
152 // Initial chain for the current user.
153 $user_given_tags = array();
154
155 // This also can be modified in local.php.
156 $pageheaders = array
157 (
158 100 => "<link rel='STYLESHEET' type='text/css' href='css/pi.css' />",
159 101 => "<link rel='STYLESHEET' type='text/css' href='css/dhtmlxcombo.css' />",
160 102 => "<link rel='ICON' type='image/x-icon' href='pix/racktables.ico' />",
161 200 => "<script language='javascript' type='text/javascript' src='js/racktables.js'></script>",
162 201 => "<script language='javascript' type='text/javascript' src='js/jquery-1.3.1.min.js'></script>",
163 202 => "<script language='javascript' type='text/javascript' src='js/live_validation.js'></script>",
164 203 => "<script language='javascript' type='text/javascript' src='js/codepress/codepress.js'></script>",
165 204 => "<script language='javascript' type='text/javascript' src='js/dhtmlxcommon.js'></script>",
166 205 => "<script language='javascript' type='text/javascript' src='js/dhtmlxcombo.js'></script>",
167 206 => "<script language='javascript' type='text/javascript' src='js/dhtmlxcombo_whp.js'></script>",
168 207 => "<script language='javascript'>window.dhx_globalImgPath='pix/'</script>",
169
170 );
171
172 if (!isset ($script_mode) or $script_mode !== TRUE)
173 {
174 // A successful call to authenticate() always generates autotags and somethimes
175 // even given/implicit tags. It also sets remote_username and remote_displayname.
176 authenticate();
177 // Authentication passed.
178 // Note that we don't perform autorization here, so each 1st level page
179 // has to do it in its way, e.g. by calling authorize() after fixContext().
180 session_start();
181 }
182 else
183 {
184 // Some functions require remote_username to be set to something to act correctly,
185 // even though they don't use the value itself.
186 $admin_account = spotEntity ('user', 1);
187 $remote_username = $admin_account['user_name'];
188 unset ($admin_account);
189 }
190
191 alterConfigWithUserPreferences();
192
193 require_once 'inc/navigation.php';
194 require_once 'inc/triggers.php';
195
196
197 $op = '';
198 require_once 'inc/gateways.php';
199 if (file_exists ('inc/local.php'))
200 require_once 'inc/local.php';
201
202 // These will be filled in by fixContext()
203 $expl_tags = array();
204 $impl_tags = array();
205 // Initial chain for the current target.
206 $target_given_tags = array();
207
208 ?>