r1208 + now we can detect an old database and upgrade it ourselves
[racktables] / inc / init.php
1 <?
2 /*
3 *
4 * This file performs RackTables initialisation. After you include it
5 * from 1st-level page, don't forget to call authorize(). This is done
6 * to allow reloading of pageno and tabno variables. pageno and tabno
7 * together form security context.
8 *
9 */
10
11 $root = (empty($_SERVER['HTTPS'])?'http':'https').
12 '://'.
13 (isset($_SERVER['HTTP_HOST'])?$_SERVER['HTTP_HOST']:($_SERVER['SERVER_NAME'].($_SERVER['SERVER_PORT']=='80'?'':$_SERVER['SERVER_PORT']))).
14 dirname($_SERVER['PHP_SELF']).'/';
15
16 // This is the first thing we need to do.
17 require_once 'inc/config.php';
18
19 // What we need first is database and interface functions.
20 require_once 'inc/interface.php';
21 require_once 'inc/functions.php';
22 require_once 'inc/database.php';
23 if (file_exists ('inc/secret.php'))
24 require_once 'inc/secret.php';
25 else
26 {
27 showError
28 (
29 "Database connection parameters are read from inc/secret.php file, " .
30 "which cannot be found.\nCopy provided inc/secret-sample.php to " .
31 "inc/secret.php and modify to your setup.\n\nThen reload the page."
32 );
33 die;
34 }
35
36 // Now try to connect...
37 try
38 {
39 $dbxlink = new PDO ($pdo_dsn, $db_username, $db_password);
40 }
41 catch (PDOException $e)
42 {
43 showError ("Database connection failed:\n\n" . $e->getMessage());
44 die();
45 }
46
47 // Escape any globals before we ever try to use them.
48 foreach ($_REQUEST as $key => $value)
49 if (gettype ($value) == 'string')
50 $_REQUEST[$key] = escapeString ($value);
51 if (isset ($_SERVER['PHP_AUTH_USER']))
52 $_SERVER['PHP_AUTH_USER'] = escapeString ($_SERVER['PHP_AUTH_USER']);
53 if (isset ($_SERVER['PHP_AUTH_PW']))
54 $_SERVER['PHP_AUTH_PW'] = escapeString ($_SERVER['PHP_AUTH_PW']);
55
56 $dbver = getDatabaseVersion();
57 if ($dbver != CODE_VERSION)
58 {
59 echo '<p align=justify>This Racktables installation seems to be ' .
60 'just upgraded to version ' . CODE_VERSION . ', while the '.
61 'database is still of version ' . $dbver . '. No user will be ' .
62 'either authenticated or shown any page until the upgrade is ' .
63 "finished. Follow <a href='${root}upgrade.php'>this link</a> and " .
64 'authenticate as administrator to finish the upgrade.</p>';
65 die;
66 }
67
68 $configCache = loadConfigCache();
69 if (!count ($configCache))
70 {
71 showError ('Failed to load configuration from the database.');
72 die();
73 }
74
75 // Now init authentication.
76
77 require_once 'inc/auth.php';
78 // Load access database once.
79 $accounts = getUserAccounts();
80 $perms = getUserPermissions();
81 if ($accounts === NULL or $perms === NULL)
82 {
83 showError ('Failed to initialize access database.');
84 die();
85 }
86
87 authenticate();
88
89 // Authentication passed.
90 // Note that we don't perform autorization here, so each 1st level page
91 // has to do it in its way, e.g. to call authorize().
92
93
94
95 $remote_username = $_SERVER['PHP_AUTH_USER'];
96 $pageno = (isset ($_REQUEST['page'])) ? $_REQUEST['page'] : 'index';
97 $tabno = (isset ($_REQUEST['tab'])) ? $_REQUEST['tab'] : 'default';
98
99 require_once 'inc/navigation.php';
100 require_once 'inc/pagetitles.php';
101 require_once 'inc/pagehandlers.php';
102 require_once 'inc/ophandlers.php';
103 require_once 'inc/triggers.php';
104 require_once 'inc/gateways.php';
105 require_once 'inc/help.php';
106
107 ?>