Issues (2553)

core/ajax/update.php (1 issue)

1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 * @author Christoph Wurst <[email protected]>
7
 * @author Joas Schilling <[email protected]>
8
 * @author Ko- <[email protected]>
9
 * @author Lukas Reschke <[email protected]>
10
 * @author Michael Gapczynski <[email protected]>
11
 * @author Morris Jobke <[email protected]>
12
 * @author Robin Appelman <[email protected]>
13
 * @author Thomas Müller <[email protected]>
14
 * @author Valdnet <[email protected]>
15
 * @author Victor Dubiniuk <[email protected]>
16
 * @author Vincent Petry <[email protected]>
17
 *
18
 * @license AGPL-3.0
19
 *
20
 * This code is free software: you can redistribute it and/or modify
21
 * it under the terms of the GNU Affero General Public License, version 3,
22
 * as published by the Free Software Foundation.
23
 *
24
 * This program is distributed in the hope that it will be useful,
25
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27
 * GNU Affero General Public License for more details.
28
 *
29
 * You should have received a copy of the GNU Affero General Public License, version 3,
30
 * along with this program. If not, see <http://www.gnu.org/licenses/>
31
 *
32
 */
33
use OCP\EventDispatcher\Event;
34
use OCP\EventDispatcher\IEventDispatcher;
35
use OCP\IEventSource;
36
use OCP\IEventSourceFactory;
37
use OCP\IL10N;
38
use OCP\ILogger;
39
use OC\DB\MigratorExecuteSqlEvent;
40
use OC\Repair\Events\RepairAdvanceEvent;
41
use OC\Repair\Events\RepairErrorEvent;
42
use OC\Repair\Events\RepairFinishEvent;
43
use OC\Repair\Events\RepairInfoEvent;
44
use OC\Repair\Events\RepairStartEvent;
45
use OC\Repair\Events\RepairStepEvent;
46
use OC\Repair\Events\RepairWarningEvent;
47
use OCP\L10N\IFactory;
48
49
if (!str_contains(@ini_get('disable_functions'), 'set_time_limit')) {
50
	@set_time_limit(0);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for set_time_limit(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

50
	/** @scrutinizer ignore-unhandled */ @set_time_limit(0);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
51
}
52
53
require_once '../../lib/base.php';
54
55
/** @var \OCP\IL10N $l */
56
$l = \OC::$server->get(IFactory::class)->get('core');
57
58
$eventSource = \OC::$server->get(IEventSourceFactory::class)->create();
59
// need to send an initial message to force-init the event source,
60
// which will then trigger its own CSRF check and produces its own CSRF error
61
// message
62
$eventSource->send('success', $l->t('Preparing update'));
63
64
class FeedBackHandler {
65
	private int $progressStateMax = 100;
66
	private int $progressStateStep = 0;
67
	private string $currentStep = '';
68
	private IEventSource $eventSource;
69
	private IL10N $l10n;
70
71
	public function __construct(IEventSource $eventSource, IL10N $l10n) {
72
		$this->eventSource = $eventSource;
73
		$this->l10n = $l10n;
74
	}
75
76
	public function handleRepairFeedback(Event $event): void {
77
		if ($event instanceof RepairStartEvent) {
78
			$this->progressStateMax = $event->getMaxStep();
79
			$this->progressStateStep = 0;
80
			$this->currentStep = $event->getCurrentStepName();
81
		} elseif ($event instanceof RepairAdvanceEvent) {
82
			$this->progressStateStep += $event->getIncrement();
83
			$desc = $event->getDescription();
84
			if (empty($desc)) {
85
				$desc = $this->currentStep;
86
			}
87
			$this->eventSource->send('success', $this->l10n->t('[%d / %d]: %s', [$this->progressStateStep, $this->progressStateMax, $desc]));
88
		} elseif ($event instanceof RepairFinishEvent) {
89
			$this->progressStateMax = $this->progressStateStep;
90
			$this->eventSource->send('success', $this->l10n->t('[%d / %d]: %s', [$this->progressStateStep, $this->progressStateMax, $this->currentStep]));
91
		} elseif ($event instanceof RepairStepEvent) {
92
			$this->eventSource->send('success', $this->l10n->t('Repair step:') . ' ' . $event->getStepName());
93
		} elseif ($event instanceof RepairInfoEvent) {
94
			$this->eventSource->send('success', $this->l10n->t('Repair info:') . ' ' . $event->getMessage());
95
		} elseif ($event instanceof RepairWarningEvent) {
96
			$this->eventSource->send('notice', $this->l10n->t('Repair warning:') . ' ' . $event->getMessage());
97
		} elseif ($event instanceof RepairErrorEvent) {
98
			$this->eventSource->send('error', $this->l10n->t('Repair error:') . ' ' . $event->getMessage());
99
		}
100
	}
101
}
102
103
if (\OCP\Util::needUpgrade()) {
104
	$config = \OC::$server->getSystemConfig();
105
	if ($config->getValue('upgrade.disable-web', false)) {
106
		$eventSource->send('failure', $l->t('Please use the command line updater because updating via browser is disabled in your config.php.'));
107
		$eventSource->close();
108
		exit();
109
	}
110
111
	// if a user is currently logged in, their session must be ignored to
112
	// avoid side effects
113
	\OC_User::setIncognitoMode(true);
114
115
	$logger = \OC::$server->get(\Psr\Log\LoggerInterface::class);
116
	$config = \OC::$server->getConfig();
117
	$updater = new \OC\Updater(
118
		$config,
119
		\OC::$server->getIntegrityCodeChecker(),
120
		$logger,
121
		\OC::$server->query(\OC\Installer::class)
122
	);
123
	$incompatibleApps = [];
124
125
	/** @var IEventDispatcher $dispatcher */
126
	$dispatcher = \OC::$server->get(IEventDispatcher::class);
127
	$dispatcher->addListener(
128
		MigratorExecuteSqlEvent::class,
129
		function (MigratorExecuteSqlEvent $event) use ($eventSource, $l): void {
130
			$eventSource->send('success', $l->t('[%d / %d]: %s', [$event->getCurrentStep(), $event->getMaxStep(), $event->getSql()]));
131
		}
132
	);
133
	$feedBack = new FeedBackHandler($eventSource, $l);
134
	$dispatcher->addListener(RepairStartEvent::class, [$feedBack, 'handleRepairFeedback']);
135
	$dispatcher->addListener(RepairAdvanceEvent::class, [$feedBack, 'handleRepairFeedback']);
136
	$dispatcher->addListener(RepairFinishEvent::class, [$feedBack, 'handleRepairFeedback']);
137
	$dispatcher->addListener(RepairStepEvent::class, [$feedBack, 'handleRepairFeedback']);
138
	$dispatcher->addListener(RepairInfoEvent::class, [$feedBack, 'handleRepairFeedback']);
139
	$dispatcher->addListener(RepairWarningEvent::class, [$feedBack, 'handleRepairFeedback']);
140
	$dispatcher->addListener(RepairErrorEvent::class, [$feedBack, 'handleRepairFeedback']);
141
142
	$updater->listen('\OC\Updater', 'maintenanceEnabled', function () use ($eventSource, $l) {
143
		$eventSource->send('success', $l->t('Turned on maintenance mode'));
144
	});
145
	$updater->listen('\OC\Updater', 'maintenanceDisabled', function () use ($eventSource, $l) {
146
		$eventSource->send('success', $l->t('Turned off maintenance mode'));
147
	});
148
	$updater->listen('\OC\Updater', 'maintenanceActive', function () use ($eventSource, $l) {
149
		$eventSource->send('success', $l->t('Maintenance mode is kept active'));
150
	});
151
	$updater->listen('\OC\Updater', 'dbUpgradeBefore', function () use ($eventSource, $l) {
152
		$eventSource->send('success', $l->t('Updating database schema'));
153
	});
154
	$updater->listen('\OC\Updater', 'dbUpgrade', function () use ($eventSource, $l) {
155
		$eventSource->send('success', $l->t('Updated database'));
156
	});
157
	$updater->listen('\OC\Updater', 'upgradeAppStoreApp', function ($app) use ($eventSource, $l) {
158
		$eventSource->send('success', $l->t('Update app "%s" from App Store', [$app]));
159
	});
160
	$updater->listen('\OC\Updater', 'appSimulateUpdate', function ($app) use ($eventSource, $l) {
161
		$eventSource->send('success', $l->t('Checking whether the database schema for %s can be updated (this can take a long time depending on the database size)', [$app]));
162
	});
163
	$updater->listen('\OC\Updater', 'appUpgrade', function ($app, $version) use ($eventSource, $l) {
164
		$eventSource->send('success', $l->t('Updated "%1$s" to %2$s', [$app, $version]));
165
	});
166
	$updater->listen('\OC\Updater', 'incompatibleAppDisabled', function ($app) use (&$incompatibleApps) {
167
		$incompatibleApps[] = $app;
168
	});
169
	$updater->listen('\OC\Updater', 'failure', function ($message) use ($eventSource, $config) {
170
		$eventSource->send('failure', $message);
171
		$eventSource->close();
172
		$config->setSystemValue('maintenance', false);
173
	});
174
	$updater->listen('\OC\Updater', 'setDebugLogLevel', function ($logLevel, $logLevelName) use ($eventSource, $l) {
175
		$eventSource->send('success', $l->t('Set log level to debug'));
176
	});
177
	$updater->listen('\OC\Updater', 'resetLogLevel', function ($logLevel, $logLevelName) use ($eventSource, $l) {
178
		$eventSource->send('success', $l->t('Reset log level'));
179
	});
180
	$updater->listen('\OC\Updater', 'startCheckCodeIntegrity', function () use ($eventSource, $l) {
181
		$eventSource->send('success', $l->t('Starting code integrity check'));
182
	});
183
	$updater->listen('\OC\Updater', 'finishedCheckCodeIntegrity', function () use ($eventSource, $l) {
184
		$eventSource->send('success', $l->t('Finished code integrity check'));
185
	});
186
187
	try {
188
		$updater->upgrade();
189
	} catch (\Exception $e) {
190
		\OC::$server->getLogger()->logException($e, [
191
			'level' => ILogger::ERROR,
192
			'app' => 'update',
193
		]);
194
		$eventSource->send('failure', get_class($e) . ': ' . $e->getMessage());
195
		$eventSource->close();
196
		exit();
197
	}
198
199
	$disabledApps = [];
200
	foreach ($incompatibleApps as $app) {
201
		$disabledApps[$app] = $l->t('%s (incompatible)', [$app]);
202
	}
203
204
	if (!empty($disabledApps)) {
205
		$eventSource->send('notice', $l->t('The following apps have been disabled: %s', [implode(', ', $disabledApps)]));
206
	}
207
} else {
208
	$eventSource->send('notice', $l->t('Already up to date'));
209
}
210
211
$eventSource->send('done', '');
212
$eventSource->close();
213