1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
<?php
/*
* Xibo - Digital Signage - http://www.xibo.org.uk
* Copyright (C) 2006-2015 Daniel Garner
*
* This file (Upgrade.php) is part of Xibo.
*
* Xibo is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* Xibo is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Xibo. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Xibo\Controller;
use Config;
use Exception;
use FormManager;
use Media;
use Xibo\Helper\Install;
use Xibo\Helper\Theme;
class Upgrade extends Base
{
public $errorMessage;
public function displayPage()
{
if (DBVERSION == WEBSITE_VERSION) {
Theme::Set('message', sprintf(__('Sorry you have arrived at this page in error, please try to navigate away.'), Theme::GetConfig('app_name')));
$this->getState()->html .= Theme::RenderReturn('message_box');
return;
}
if ($this->getUser()->userTypeId != 1) {
// Make sure we actually need to do an upgrade
Theme::Set('message', sprintf(__('The CMS is temporarily off-line as an upgrade is in progress. Please check with your system administrator for updates or refresh your page in a few minutes.'), Theme::GetConfig('app_name')));
$this->getState()->html .= Theme::RenderReturn('message_box');
return;
} else {
// We want a static form (traditional rather than ajax)
Theme::Set('form_class', 'StaticForm');
// What step are we on
$xibo_step = \Kit::GetParam('step', _REQUEST, _INT, 1);
$content = '';
switch ($xibo_step) {
case 1:
// Checks environment
$content = $this->Step1();
break;
case 2:
// Collect upgrade details
$content = $this->Step2();
break;
case 3:
// Execute upgrade
try {
$content = $this->Step3();
} catch (Exception $e) {
$this->errorMessage = $e->getMessage();
// Reload step 2
$content = $this->Step2();
}
break;
}
Theme::Set('step', $xibo_step);
Theme::Set('page_content', $content);
$this->getState()->html .= Theme::RenderReturn('upgrade_page');
}
}
public function Step1()
{
Theme::Set('form_action', 'index.php?p=upgrade');
// Check environment
$config = new Config();
$environment = $config->CheckEnvironment();
$formFields = array();
$formButtons = array();
$formFields[] = FormManager::AddMessage(sprintf(__('First we need to re-check if your server meets %s\'s requirements. The CMS requirements may change from release to release. If this is the case there will be further information in the release notes.'), Theme::GetConfig('app_name')));
$formFields[] = FormManager::AddRaw($environment);
if ($config->EnvironmentFault()) {
$formFields[] = FormManager::AddHidden('step', 1);
$formButtons[] = FormManager::AddButton(__('Retest'));
} else if ($config->EnvironmentWarning()) {
$formFields[] = FormManager::AddHidden('step', 2);
$formButtons[] = FormManager::AddButton(__('Retest'), 'link', 'index.php?p=upgrade&step=1');
$formButtons[] = FormManager::AddButton(__('Next'));
} else {
$formFields[] = FormManager::AddHidden('step', 2);
$formButtons[] = FormManager::AddButton(__('Next'));
}
// Return a rendered form
Theme::Set('form_fields', $formFields);
Theme::Set('form_buttons', $formButtons);
return Theme::RenderReturn('form_render');
}
public function Step2()
{
// Work out what is involved in this upgrade
$_SESSION['upgradeFrom'] = Config::Version('DBVersion');
if ($_SESSION['upgradeFrom'] < 1) {
$_SESSION['upgradeFrom'] = 1;
}
// Get a list of .sql and .php files for the upgrade
$sql_files = Install::ls('*.sql', 'install/database', false, array('return_files'));
$php_files = Install::ls('*.php', 'install/database', false, array('return_files'));
// Sort by natural filename (eg 10 is bigger than 2)
natcasesort($sql_files);
natcasesort($php_files);
$_SESSION['phpFiles'] = $php_files;
$_SESSION['sqlFiles'] = $sql_files;
$max_sql = \Kit::ValidateParam(substr(end($sql_files), 0, -4), _INT);
$max_php = \Kit::ValidateParam(substr(end($php_files), 0, -4), _INT);
$_SESSION['upgradeTo'] = max($max_sql, $max_php);
if (!$_SESSION['upgradeTo'])
throw new Exception(__('Unable to calculate the upgradeTo value. Check for non-numeric SQL and PHP files in the "install / database" directory.'));
if ($_SESSION['upgradeTo'] < $_SESSION['upgradeFrom'])
$_SESSION['upgradeTo'] = $_SESSION['upgradeFrom'];
// Form to collect some information.
$formFields = array();
$formButtons = array();
// Put up an error message if one has been set (and then unset it)
if ($this->errorMessage != '') {
Theme::Set('message', $this->errorMessage);
Theme::Set('prepend', Theme::RenderReturn('message_box'));
$this->errorMessage == '';
}
$formFields[] = FormManager::AddHidden('step', 3);
$formFields[] = FormManager::AddHidden('upgradeFrom', $_SESSION['upgradeFrom']);
$formFields[] = FormManager::AddHidden('upgradeTo', $_SESSION['upgradeTo']);
$formFields[] = FormManager::AddHidden('includes', true);
$formFields[] = FormManager::AddMessage(sprintf(__('Upgrading from database version %d to %d'), $_SESSION['upgradeFrom'], $_SESSION['upgradeTo']));
// Loop for $i between upgradeFrom + 1 and upgradeTo.
// If a php file exists for that upgrade, make an instance of it and call Questions so we can
// Ask the user for input.
for ($i = $_SESSION['upgradeFrom'] + 1; $i <= $_SESSION['upgradeTo']; $i++) {
if (file_exists('install/database/' . $i . '.php')) {
include_once('install/database/' . $i . '.php');
$stepName = 'Step' . $i;
// Check that a class called Step$i exists
if (class_exists($stepName)) {
$_SESSION['Step' . $i] = new $stepName($this->db);
// Call Questions on the object and send the resulting hash to createQuestions routine
$questionFields = $this->createQuestions($i, $_SESSION['Step' . $i]->Questions());
$formFields = array_merge($formFields, $questionFields);
} else {
$formFields[] = FormManager::AddMessage(sprintf(__('Warning: We included %s.php, but it did not include a class of appropriate name.'), $i));
}
}
}
$formFields[] = FormManager::AddCheckbox('doBackup', 'I agree I have a valid database backup and can restore it should the upgrade process fail', 0, __('It is important to take a database backup before running the upgrade wizard. A backup is essential for recovering your CMS should there be a problem with the upgrade.'), 'b');
// Return a rendered form
Theme::Set('form_action', 'index.php?p=upgrade');
Theme::Set('form_fields', $formFields);
Theme::Set('form_buttons', array(FormManager::AddButton(__('Next'))));
return Theme::RenderReturn('form_render');
}
public function Step3()
{
set_time_limit(0);
$fault = false;
$fault_string = '';
foreach ($_POST as $key => $post) {
// $key should be like 1-2, 1-3 etc
// Split $key on - character.
$parts = explode('-', $key);
if (count($parts) == 2) {
$step_num = 'Step' . $parts[0];
include_once('install/database/' . $parts[0] . '.php');
$response = $_SESSION[$step_num]->ValidateQuestion($parts[1], $post);
if (!$response == true) {
// The upgrade routine for this step wasn't happy.
$fault = true;
$fault_string .= $response . "<br />\n";
}
}
}
if ($fault)
throw new Exception($fault_string);
$doBackup = \Xibo\Helper\Sanitize::getCheckbox('doBackup');
if ($doBackup == 0)
throw new Exception(__('You MUST have a valid database backup to continue. Please take and verify a backup and upgrade again.'));
$sql_file = '';
$sql = '';
$i = 0;
// Now loop over the entire upgrade. Run the SQLs and PHP interleaved.
try {
$dbh = \Xibo\Storage\PDOConnect::init();
//$dbh->beginTransaction();
for ($i = $_SESSION['upgradeFrom'] + 1; $i <= $_SESSION['upgradeTo']; $i++) {
if (file_exists('install/database/' . $i . '.sql')) {
$delimiter = ';';
$sql_file = @file_get_contents('install/database/' . $i . '.sql');
$sql_file = Install::remove_remarks($sql_file);
$sql_file = Install::split_sql_file($sql_file, $delimiter);
foreach ($sql_file as $sql) {
$dbh->exec($sql);
}
}
if (file_exists('install/database/' . $i . '.php')) {
$stepName = 'Step' . $i;
if (!$_SESSION[$stepName]->Boot())
throw new Exception(__('Failed with %s', $stepName));
}
}
//$dbh->commit();
} catch (Exception $e) {
//$dbh->rollBack();
throw new Exception(sprintf(__('An error occurred running the upgrade. Please take a screen shot of this page and seek help. Statement number: %d. Error Message = [%s]. File = [%s]. SQL = [%s].'), $i, $e->getMessage(), $sql_file, $sql));
}
// Install files
Media::installAllModuleFiles();
// Delete install
if (!unlink('install.php'))
$formFields[] = FormManager::AddMessage(__("Unable to delete install.php. Please ensure the webserver has permission to unlink this file and retry"));
$formFields[] = FormManager::AddMessage(__('The upgrade was a success!'));
// Return a rendered form
Theme::Set('form_fields', $formFields);
return Theme::RenderReturn('form_render');
}
private function createQuestions($step, $questions)
{
// Takes a multi-dimensional array eg:
// $q[0]['question'] = "May we collect anonymous usage statistics?";
// $q[0]['type'] = _CHECKBOX;
// $q[0]['default'] = true;
$formFields = array();
foreach ($questions as $qnum => $question) {
$title = ($step < 80) ? __('Question %d of Step %s', $qnum + 1, $step) : $question['title'];
if ($question['type'] == _INPUTBOX) {
$formFields[] = FormManager::AddText($step . '-' . $qnum, $title, $question['default'],
$question['question'], 'q');
} elseif ($question['type'] == _PASSWORD) {
$formFields[] = FormManager::AddPassword($step . '-' . $qnum, $title, $question['default'],
$question['question'], 'q');
} elseif ($question['type'] == _CHECKBOX) {
$formFields[] = FormManager::AddCheckbox($step . '-' . $qnum, $title, (($question['default']) ? 1 : 0),
$question['question'], 'q');
}
}
return $formFields;
}
}
?>
|