-
Notifications
You must be signed in to change notification settings - Fork 612
/
UpdateController.php
454 lines (372 loc) · 12 KB
/
UpdateController.php
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
<?php
namespace Craft;
/**
* The UpdateController class is a controller that handles various update related tasks such as checking for available
* updates and running manual and auto-updates.
*
* Note that all actions in the controller, except for {@link actionPrepare}, {@link actionBackupDatabase},
* {@link actionUpdateDatabase}, {@link actionCleanUp} and {@link actionRollback} require an authenticated Craft session
* via {@link BaseController::allowAnonymous}.
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @copyright Copyright (c) 2014, Pixel & Tonic, Inc.
* @license http://craftcms.com/license Craft License Agreement
* @see http://craftcms.com
* @package craft.app.controllers
* @since 1.0
*/
class UpdateController extends BaseController
{
// Properties
// =========================================================================
/**
* If set to false, you are required to be logged in to execute any of the given controller's actions.
*
* If set to true, anonymous access is allowed for all of the given controller's actions.
*
* If the value is an array of action names, then you must be logged in for any action method except for the ones in
* the array list.
*
* If you have a controller that where the majority of action methods will be anonymous, but you only want require
* login on a few, it's best to use {@link UserSessionService::requireLogin() craft()->userSession->requireLogin()}
* in the individual methods.
*
* @var bool
*/
protected $allowAnonymous = array('actionPrepare', 'actionBackupDatabase', 'actionUpdateDatabase', 'actionCleanUp', 'actionRollback');
// Public Methods
// =========================================================================
// Auto Updates
// -------------------------------------------------------------------------
/**
* Returns the available updates.
*
* @return null
*/
public function actionGetAvailableUpdates()
{
craft()->userSession->requirePermission('performUpdates');
try
{
$updates = craft()->updates->getUpdates(true);
}
catch (EtException $e)
{
if ($e->getCode() == 10001)
{
$this->returnErrorJson($e->getMessage());
}
}
if ($updates)
{
$response = $updates->getAttributes();
$response['allowAutoUpdates'] = craft()->config->allowAutoUpdates();
$this->returnJson($response);
}
else
{
$this->returnErrorJson(Craft::t('Could not fetch available updates at this time.'));
}
}
/**
* Called during both a manual and auto-update.
*
* @return null
*/
public function actionPrepare()
{
$this->requirePostRequest();
$this->requireAjaxRequest();
$data = craft()->request->getRequiredPost('data');
$handle = $this->_getFixedHandle($data);
$manual = false;
if (!$this->_isManualUpdate($data))
{
// If it's not a manual update, make sure they have auto-update permissions.
craft()->userSession->requirePermission('performUpdates');
if (!craft()->config->allowAutoUpdates())
{
$this->returnJson(array('alive' => true, 'errorDetails' => Craft::t('Auto-updating is disabled on this system.'), 'finished' => true));
}
}
else
{
$manual = true;
}
$return = craft()->updates->prepareUpdate($manual, $handle);
if (!$return['success'])
{
$this->returnJson(array('alive' => true, 'errorDetails' => $return['message'], 'finished' => true));
}
if ($manual)
{
$this->returnJson(array('alive' => true, 'nextStatus' => Craft::t('Backing-up database…'), 'nextAction' => 'update/backupDatabase', 'data' => $data));
}
else
{
$data['md5'] = craft()->security->hashData($return['md5']);
$this->returnJson(array('alive' => true, 'nextStatus' => Craft::t('Downloading update…'), 'nextAction' => 'update/processDownload', 'data' => $data));
}
}
/**
* Called during an auto-update.
*
* @return null
* @throws Exception
*/
public function actionProcessDownload()
{
// This method should never be called in a manual update.
craft()->userSession->requirePermission('performUpdates');
$this->requirePostRequest();
$this->requireAjaxRequest();
if (!craft()->config->allowAutoUpdates())
{
$this->returnJson(array('alive' => true, 'errorDetails' => Craft::t('Auto-updating is disabled on this system.'), 'finished' => true));
}
$data = craft()->request->getRequiredPost('data');
$handle = $this->_getFixedHandle($data);
$md5 = craft()->security->validateData($data['md5']);
if (!$md5)
{
throw new Exception('Could not validate MD5.');
}
$return = craft()->updates->processUpdateDownload($md5, $handle);
if (!$return['success'])
{
$this->returnJson(array('alive' => true, 'errorDetails' => $return['message'], 'finished' => true));
}
$data = array(
'handle' => craft()->security->hashData($handle),
'uid' => craft()->security->hashData($return['uid']),
);
$this->returnJson(array('alive' => true, 'nextStatus' => Craft::t('Backing-up files…'), 'nextAction' => 'update/backupFiles', 'data' => $data));
}
/**
* Called during an auto-update.
*
* @return null
* @throws Exception
*/
public function actionBackupFiles()
{
// This method should never be called in a manual update.
craft()->userSession->requirePermission('performUpdates');
$this->requirePostRequest();
$this->requireAjaxRequest();
if (!craft()->config->allowAutoUpdates())
{
$this->returnJson(array('alive' => true, 'errorDetails' => Craft::t('Auto-updating is disabled on this system.'), 'finished' => true));
}
$data = craft()->request->getRequiredPost('data');
$handle = $this->_getFixedHandle($data);
$uid = craft()->security->validateData($data['uid']);
if (!$uid)
{
throw new Exception('Could not validate UID');
}
$return = craft()->updates->backupFiles($uid, $handle);
if (!$return['success'])
{
$this->returnJson(array('alive' => true, 'errorDetails' => $return['message'], 'finished' => true));
}
$this->returnJson(array('alive' => true, 'nextStatus' => Craft::t('Updating files…'), 'nextAction' => 'update/updateFiles', 'data' => $data));
}
/**
* Called during an auto-update.
*
* @return null
* @throws Exception
*/
public function actionUpdateFiles()
{
// This method should never be called in a manual update.
craft()->userSession->requirePermission('performUpdates');
$this->requirePostRequest();
$this->requireAjaxRequest();
if (!craft()->config->allowAutoUpdates())
{
$this->returnJson(array('alive' => true, 'errorDetails' => Craft::t('Auto-updating is disabled on this system.'), 'finished' => true));
}
$data = craft()->request->getRequiredPost('data');
$handle = $this->_getFixedHandle($data);
$uid = craft()->security->validateData($data['uid']);
if (!$uid)
{
throw new Exception('Could not validate UID');
}
$return = craft()->updates->updateFiles($uid, $handle);
if (!$return['success'])
{
$this->returnJson(array('alive' => true, 'errorDetails' => $return['message'], 'nextStatus' => Craft::t('An error was encountered. Rolling back…'), 'nextAction' => 'update/rollback'));
}
$this->returnJson(array('alive' => true, 'nextStatus' => Craft::t('Backing-up database…'), 'nextAction' => 'update/backupDatabase', 'data' => $data));
}
/**
* Called during both a manual and auto-update.
*
* @return null
*/
public function actionBackupDatabase()
{
$this->requirePostRequest();
$this->requireAjaxRequest();
$data = craft()->request->getRequiredPost('data');
$handle = $this->_getFixedHandle($data);
if (craft()->config->get('backupDbOnUpdate'))
{
$plugin = craft()->plugins->getPlugin($handle);
// If this a plugin, make sure it actually has new migrations before backing up the database.
if ($handle == 'craft' || ($plugin && craft()->migrations->getNewMigrations($plugin)))
{
$return = craft()->updates->backupDatabase();
if (!$return['success'])
{
$this->returnJson(array('alive' => true, 'errorDetails' => $return['message'], 'nextStatus' => Craft::t('An error was encountered. Rolling back…'), 'nextAction' => 'update/rollback'));
}
if (isset($return['dbBackupPath']))
{
$data['dbBackupPath'] = craft()->security->hashData($return['dbBackupPath']);
}
}
}
$this->returnJson(array('alive' => true, 'nextStatus' => Craft::t('Updating database…'), 'nextAction' => 'update/updateDatabase', 'data' => $data));
}
/**
* Called during both a manual and auto-update.
*
* @return null
*/
public function actionUpdateDatabase()
{
$this->requirePostRequest();
$this->requireAjaxRequest();
$data = craft()->request->getRequiredPost('data');
$handle = $this->_getFixedHandle($data);
$return = craft()->updates->updateDatabase($handle);
if (!$return['success'])
{
$this->returnJson(array('alive' => true, 'errorDetails' => $return['message'], 'nextStatus' => Craft::t('An error was encountered. Rolling back…'), 'nextAction' => 'update/rollback'));
}
$this->returnJson(array('alive' => true, 'nextStatus' => Craft::t('Cleaning up…'), 'nextAction' => 'update/cleanUp', 'data' => $data));
}
/**
* Performs maintenance and clean up tasks after an update.
*
* Called during both a manual and auto-update.
*
* @return null
* @throws Exception
*/
public function actionCleanUp()
{
$this->requirePostRequest();
$this->requireAjaxRequest();
$data = craft()->request->getRequiredPost('data');
if ($this->_isManualUpdate($data))
{
$uid = false;
}
else
{
$uid = craft()->security->validateData($data['uid']);
if (!$uid)
{
throw new Exception(('Could not validate UID'));
}
}
$handle = $this->_getFixedHandle($data);
$oldVersion = false;
// Grab the old version from the manifest data before we nuke it.
$manifestData = UpdateHelper::getManifestData(UpdateHelper::getUnzipFolderFromUID($uid), $handle);
if ($manifestData && $handle == 'craft')
{
$oldVersion = UpdateHelper::getLocalVersionFromManifest($manifestData);
}
craft()->updates->updateCleanUp($uid, $handle);
// New major Craft CMS version?
if ($handle == 'craft' && $oldVersion && AppHelper::getMajorVersion($oldVersion) < AppHelper::getMajorVersion(craft()->getVersion()))
{
$returnUrl = UrlHelper::getUrl('whats-new');
}
else
{
$returnUrl = craft()->config->get('postCpLoginRedirect');
}
$this->returnJson(array('alive' => true, 'finished' => true, 'returnUrl' => $returnUrl));
}
/**
* Can be called during both a manual and auto-update.
*
* @throws Exception
* @return null
*/
public function actionRollback()
{
$this->requirePostRequest();
$this->requireAjaxRequest();
$data = craft()->request->getRequiredPost('data');
$handle = $this->_getFixedHandle($data);
if ($this->_isManualUpdate($data))
{
$uid = false;
}
else
{
$uid = craft()->security->validateData($data['uid']);
if (!$uid)
{
throw new Exception(('Could not validate UID'));
}
}
if (isset($data['dbBackupPath']))
{
$dbBackupPath = craft()->security->validateData($data['dbBackupPath']);
if (!$dbBackupPath)
{
throw new Exception('Could not validate database backup path.');
}
$return = craft()->updates->rollbackUpdate($uid, $handle, $dbBackupPath);
}
else
{
$return = craft()->updates->rollbackUpdate($uid, $handle);
}
if (!$return['success'])
{
// Let the JS handle the exception response.
throw new Exception($return['message']);
}
$this->returnJson(array('alive' => true, 'finished' => true, 'rollBack' => true));
}
// Private Methods
// =========================================================================
/**
* @param $data
*
* @return bool
* @throws Exception
*/
private function _isManualUpdate($data)
{
if (isset($data['manualUpdate']) && $data['manualUpdate'] == 1)
{
return true;
}
return false;
}
/**
* @param $data
*
* @return string
* @throws Exception
*/
private function _getFixedHandle($data)
{
if ($handle = craft()->security->validateData($data['handle']))
{
return $handle;
}
throw new Exception('Could not validate update handle.');
}
}