r2275 - cut the upgrade path at 0.16.4 for now
[racktables] / upgrade.php
CommitLineData
6dc745d2 1<?php
fbbb74fb
DO
2
3// At the moment we assume, that for any two releases we can
4// sequentally execute all batches, that separate them, and
5// nothing will break. If this changes one day, the function
6// below will have to generate smarter upgrade paths, while
7// the upper layer will remain the same.
8// Returning an empty array means that no upgrade is necessary.
9function getDBUpgradePath ($v1, $v2)
10{
a6f83a72
DO
11 $versionhistory = array
12 (
b3f866fc 13 '0.16.4',
30d0a2a3 14 '0.17.0',
a6f83a72 15 );
fbbb74fb
DO
16 if (!in_array ($v1, $versionhistory) || !in_array ($v2, $versionhistory))
17 {
18 showError ("An upgrade path has been requested for versions '${v1}' and '${v2}', " .
b0348307 19 "and at least one of those isn't known to me.", __FILE__);
fbbb74fb
DO
20 die;
21 }
22 $skip = TRUE;
23 $path = array();
24 // Now collect all versions > $v1 and <= $v2
25 foreach ($versionhistory as $v)
26 {
27 if ($v == $v1)
28 {
29 $skip = FALSE;
30 continue;
31 }
32 if ($skip)
33 continue;
34 $path[] = $v;
35 if ($v == $v2)
36 break;
37 }
38 return $path;
39}
40
5f4027b8
DO
41function printReleaseNotes ($batchid)
42{
43 switch ($batchid)
44 {
5f4027b8
DO
45 default:
46 break;
47 }
48}
49
fbbb74fb
DO
50// Upgrade batches are name exactly as the release where they first appear.
51// That simple, but seems sufficient for beginning.
52function executeUpgradeBatch ($batchid)
53{
54 $query = array();
ce109ff2 55 global $dbxlink;
fbbb74fb
DO
56 switch ($batchid)
57 {
30d0a2a3
DO
58 case '0.17.0':
59 $query[] = "update Config set varvalue = '0.17.0' where varname = 'DB_VERSION'";
b3f866fc 60 break;
fbbb74fb 61 default:
b0348307 62 showError ("executeUpgradeBatch () failed, because batch '${batchid}' isn't defined", __FILE__);
fbbb74fb
DO
63 die;
64 break;
65 }
fbbb74fb 66 $failures = array();
ce109ff2 67 $ndots = 0;
22e40283 68 echo "<pre>Executing database upgrade batch '${batchid}':\n";
fbbb74fb
DO
69 foreach ($query as $q)
70 {
71 $result = $dbxlink->query ($q);
72 if ($result != NULL)
fbbb74fb 73 echo '.';
758fe24c
DO
74 else
75 {
76 echo '!';
77 $errorInfo = $dbxlink->errorInfo();
78 $failures[] = array ($q, $errorInfo[2]);
79 }
80 if (++$ndots == 50)
81 {
82 echo "\n";
15a50768 83 flush();
758fe24c 84 $ndots = 0;
fbbb74fb 85 }
fbbb74fb
DO
86 }
87 echo '<br>';
88 if (!count ($failures))
89 echo "No errors!\n";
90 else
91 {
7fc5565c 92 echo "The following queries failed:\n<font color=red>";
fbbb74fb
DO
93 foreach ($failures as $f)
94 {
95 list ($q, $i) = $f;
96 echo "${q} // ${i}\n";
97 }
98 }
7fc5565c 99 echo '</font></pre>';
fbbb74fb
DO
100}
101
102// ******************************************************************
103//
104// Execution starts here
105//
106// ******************************************************************
107
108$root = (empty($_SERVER['HTTPS'])?'http':'https').
109 '://'.
110 (isset($_SERVER['HTTP_HOST'])?$_SERVER['HTTP_HOST']:($_SERVER['SERVER_NAME'].($_SERVER['SERVER_PORT']=='80'?'':$_SERVER['SERVER_PORT']))).
54c2a7a8
DO
111 dirname($_SERVER['PHP_SELF']);
112if (substr ($root, -1) != '/')
113 $root .= '/';
fbbb74fb
DO
114
115// The below will be necessary as long as we rely on showError()
116require_once 'inc/interface.php';
117
118require_once 'inc/config.php';
119require_once 'inc/database.php';
120if (file_exists ('inc/secret.php'))
121 require_once 'inc/secret.php';
122else
123 die ("Database connection parameters are read from inc/secret.php file, " .
124 "which cannot be found.\nCopy provided inc/secret-sample.php to " .
125 "inc/secret.php and modify to your setup.\n\nThen reload the page.");
126
127try
128{
129 $dbxlink = new PDO ($pdo_dsn, $db_username, $db_password);
130}
131catch (PDOException $e)
132{
133 die ("Database connection failed:\n\n" . $e->getMessage());
134}
135
fbbb74fb
DO
136// Now we need to be sure that the current user is the administrator.
137// The rest doesn't matter within this context.
138// We still continue to use the current authenticator though, but this will
139// last only till the UserAccounts remains the same. After that this file
140// will have to dig into the DB for the user accounts.
141require_once 'inc/auth.php';
142
a1f3710a
DO
143// 1. This didn't fail sanely, because getUserAccounts() depended on showError()
144// 2. getUserAccounts() doesn't work for old DBs since 0.16.0. Let's have own
145// copy until it breaks too.
146
147function getUserAccounts_local ()
148{
149 global $dbxlink;
150 $query = 'select user_id, user_name, user_password_hash from UserAccount order by user_name';
151 if (($result = $dbxlink->query ($query)) == NULL)
152 die ('SQL query failed in ' . __FUNCTION__);
153 $ret = array();
154 while ($row = $result->fetch (PDO::FETCH_ASSOC))
155 foreach (array ('user_id', 'user_name', 'user_password_hash') as $cname)
156 $ret[$row['user_name']][$cname] = $row[$cname];
157 return $ret;
158}
159
160$accounts = getUserAccounts_local();
fbbb74fb 161
d78fdaea
DO
162// Only administrator is always authenticated locally, so reject others
163// for authenticate() to succeed.
99ee5479
DO
164
165if
166(
167 !isset ($_SERVER['PHP_AUTH_USER']) or
168 !isset ($_SERVER['PHP_AUTH_PW']) or
169 $accounts[$_SERVER['PHP_AUTH_USER']]['user_id'] != 1 or
170 !authenticated_via_database (escapeString ($_SERVER['PHP_AUTH_USER']), escapeString ($_SERVER['PHP_AUTH_PW']))
171)
172{
c0142c01
DO
173 header ('WWW-Authenticate: Basic realm="RackTables upgrade"');
174 header ('HTTP/1.0 401 Unauthorized');
b0348307 175 showError ('You must be authenticated as an administrator to complete the upgrade.', __FILE__);
99ee5479
DO
176 die;
177}
fbbb74fb
DO
178
179$dbver = getDatabaseVersion();
5f4027b8
DO
180echo 'Code version: ' . CODE_VERSION . '<br>';
181echo 'Database version: ' . $dbver . '<br>';
fbbb74fb 182if ($dbver == CODE_VERSION)
758fe24c 183{
5f4027b8
DO
184 die ("<p align=justify>No action is necessary. " .
185 "Proceed to the <a href='${root}'>main page</a>, " .
186 "check your data and have a nice day.</p>");
758fe24c 187}
fbbb74fb
DO
188
189foreach (getDBUpgradePath ($dbver, CODE_VERSION) as $batchid)
5f4027b8 190{
fbbb74fb 191 executeUpgradeBatch ($batchid);
5f4027b8
DO
192 printReleaseNotes ($batchid);
193}
fbbb74fb
DO
194
195echo '<br>Database version == ' . getDatabaseVersion();
196echo "<p align=justify>Your database seems to be up-to-date. " .
197 "Now the best thing to do would be to follow to the <a href='${root}'>main page</a> " .
198 "and explore your data. Have a nice day.</p>";
199
200?>