update.php
4.48 KB
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
<?php
/**
* ownCloud - Updater plugin
*
* @author Victor Dubiniuk
* @copyright 2013 Victor Dubiniuk victor.dubiniuk@gmail.com
*
* This file is licensed under the Affero General Public License version 3 or
* later.
*/
namespace OCA\Updater;
\OCP\JSON::checkAdminUser();
\OCP\JSON::callCheck();
set_time_limit(0);
$updateEventSource = new \OC_EventSource();
$watcher = new UpdateWatcher($updateEventSource);
\OC_Hook::connect('update', 'success', $watcher, 'success');
\OC_Hook::connect('update', 'failure', $watcher, 'failure');
$watcher->success((string) App::$l10n->t('Checking your installation...'));
// Check if we have enough permissions
$installed = Helper::getDirectories();
$thirdPartyUpdater = new Location_3rdparty($installed[Helper::THIRDPARTY_DIRNAME], '');
$coreUpdater = new Location_Core($installed[Helper::CORE_DIRNAME], '');
$appsUpdater = new Location_Apps('', '');
$errors = array_merge(
$thirdPartyUpdater->check(), $coreUpdater->check(), $appsUpdater->check()
);
if (count($errors)) {
$message = App::$l10n->t('Upgrade is not possible. Make sure that your webserver has write access to the following files and directories:');
$message .= '<br /><br />' . implode('<br />', $errors);
$watcher->error($message);
$watcher->done();
die();
}
// Download package
// Url to download package e.g. http://download.owncloud.org/releases/owncloud-4.0.5.tar.bz2
$packageUrl = 'https://download.owncloud.com/download/community/owncloud-latest.zip';
//Package version e.g. 4.0.4
$packageVersion = '';
$updateData = \OC_Updater::check();
if (isset($updateData['version'])) {
$packageVersion = $updateData['version'];
}
if (isset($updateData['url']) && extension_loaded('bz2')) {
$packageUrl = $updateData['url'];
}
if (!strlen($packageVersion) || !strlen($packageUrl)) {
App::log('Invalid response from update feed.');
$watcher->failure((string) App::$l10n->t('Version not found'));
}
//Some cleanup first
Downloader::cleanUp($packageVersion);
if (!Downloader::isClean($packageVersion)){
$message = App::$l10n->t('Upgrade is not possible. Your webserver has not enough permissions to remove the following directory:');
$message .= '<br />' . Downloader::getPackageDir($packageVersion);
$message .= '<br />' . App::$l10n->t('Update permissions on this directory and its content or remove it manually first.');
$watcher->error($message);
$watcher->done();
}
Updater::cleanUp();
if (!Updater::isClean()){
$message = App::$l10n->t('Upgrade is not possible. Your webserver has not enough permissions to remove the following directory:');
$message .= '<br />' . Updater::getTempDir();
$message .= '<br />' . App::$l10n->t('Update permissions on this directory and its content or remove it manually first.');
$watcher->error($message);
$watcher->done();
}
// Downloading new version
try {
$watcher->success((string) App::$l10n->t('Downloading package...'));
Downloader::getPackage($packageUrl, $packageVersion);
} catch (\Exception $e) {
App::log($e->getMessage());
$watcher->failure((string) App::$l10n->t('Unable to fetch package'));
}
// Create Backup
try {
$watcher->success((string) App::$l10n->t('Creating backup...'));
$backupPath = Backup::create();
$watcher->success((string) App::$l10n->t('Here is your backup: ') . $backupPath . '.zip');
} catch (\Exception $e){
App::log($e->getMessage());
$watcher->failure((string) App::$l10n->t('Failed to create backup'));
}
try {
$watcher->success((string) App::$l10n->t('Moving files...'));
Updater::update($packageVersion, $backupPath);
// We are done. Some cleanup
Downloader::cleanUp($packageVersion);
Updater::cleanUp();
$watcher->done((string) App::$l10n->t('All done. Click to the link below to start database upgrade.'));
} catch (\Exception $e){
App::log($e->getMessage());
$watcher->failure((string) App::$l10n->t('Update failed'));
}
$watcher->done();
class UpdateWatcher {
/**
* @var \OC_EventSource $eventSource;
*/
private $eventSource;
public function __construct($eventSource) {
$this->eventSource = $eventSource;
}
public function error($message) {
\OC_Util::obEnd();
$this->eventSource->send('error', $message);
ob_start();
}
public function success($message) {
\OC_Util::obEnd();
$this->eventSource->send('success', $message);
ob_start();
}
public function failure($message) {
\OC_Util::obEnd();
$this->eventSource->send('failure', $message);
$this->eventSource->close();
die();
}
public function done() {
\OC_Util::obEnd();
$this->eventSource->send('done', '');
$this->eventSource->close();
}
}