Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 170 additions & 0 deletions core/Controller/UpdateController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH
* SPDX-FileContributor: Carl Schwan
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OC\Core\Controller;

use OC\Core\Listener\FeedBackHandler;
use OC\DB\MigratorExecuteSqlEvent;
use OC\Repair\Events\RepairAdvanceEvent;
use OC\Repair\Events\RepairErrorEvent;
use OC\Repair\Events\RepairFinishEvent;
use OC\Repair\Events\RepairInfoEvent;
use OC\Repair\Events\RepairStartEvent;
use OC\Repair\Events\RepairStepEvent;
use OC\Repair\Events\RepairWarningEvent;
use OC\Updater;
use OCP\AppFramework\Http\Attribute\ApiRoute;
use OCP\AppFramework\Http\Attribute\PublicPage;
use OCP\AppFramework\Http\DataResponse;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
use OCP\IEventSourceFactory;
use OCP\IL10N;
use OCP\IRequest;
use OCP\Util;
use Psr\Log\LoggerInterface;

class UpdateController extends \OCP\AppFramework\OCSController {
public function __construct(
string $appName,
IRequest $request,
private readonly IEventSourceFactory $eventSourceFactory,
private readonly IL10N $l,
private readonly IConfig $config,
private readonly Updater $updater,
private readonly IEventDispatcher $dispatcher,
private readonly LoggerInterface $logger,
) {
parent::__construct($appName, $request);
}

#[ApiRoute(verb: 'GET', url: '/update', root: '/core')]
#[PublicPage]
public function update(): DataResponse {
if (!str_contains(@ini_get('disable_functions'), 'set_time_limit')) {
@set_time_limit(0);
}

\OC_User::setIncognitoMode(true);

$eventSource = $this->eventSourceFactory->create();
// need to send an initial message to force-init the event source,
// which will then trigger its own CSRF check and produces its own CSRF error
// message
$eventSource->send('success', $this->l->t('Preparing update'));
if (!Util::needUpgrade()) {
$eventSource->send('notice', $this->l->t('Already up to date'));
$eventSource->send('done', '');
$eventSource->close();
return new DataResponse([]);
}

if ($this->config->getSystemValueBool('upgrade.disable-web', false)) {
$eventSource->send('failure', $this->l->t('Please use the command line updater because updating via browser is disabled in your config.php.'));
$eventSource->close();
return new DataResponse([]);
}

// if a user is currently logged in, their session must be ignored to
// avoid side effects
\OC_User::setIncognitoMode(true);

$incompatibleApps = [];
$incompatibleOverwrites = $this->config->getSystemValue('app_install_overwrite', []);

$this->dispatcher->addListener(
MigratorExecuteSqlEvent::class,
function (MigratorExecuteSqlEvent $event) use ($eventSource): void {
$eventSource->send('success', $this->l->t('[%d / %d]: %s', [$event->getCurrentStep(), $event->getMaxStep(), $event->getSql()]));
}
);
$feedBack = new FeedBackHandler($eventSource, $this->l);
$this->dispatcher->addListener(RepairStartEvent::class, $feedBack->handleRepairFeedback(...));
$this->dispatcher->addListener(RepairAdvanceEvent::class, $feedBack->handleRepairFeedback(...));
$this->dispatcher->addListener(RepairFinishEvent::class, $feedBack->handleRepairFeedback(...));
$this->dispatcher->addListener(RepairStepEvent::class, $feedBack->handleRepairFeedback(...));
$this->dispatcher->addListener(RepairInfoEvent::class, $feedBack->handleRepairFeedback(...));
$this->dispatcher->addListener(RepairWarningEvent::class, $feedBack->handleRepairFeedback(...));
$this->dispatcher->addListener(RepairErrorEvent::class, $feedBack->handleRepairFeedback(...));

$this->updater->listen('\OC\Updater', 'maintenanceEnabled', function () use ($eventSource): void {

Check failure on line 97 in core/Controller/UpdateController.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

DeprecatedMethod

core/Controller/UpdateController.php:97:19: DeprecatedMethod: The method OC\Hooks\EmitterTrait::listen has been marked as deprecated (see https://psalm.dev/001)
$eventSource->send('success', $this->l->t('Turned on maintenance mode'));
});
$this->updater->listen('\OC\Updater', 'maintenanceDisabled', function () use ($eventSource): void {

Check failure on line 100 in core/Controller/UpdateController.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

DeprecatedMethod

core/Controller/UpdateController.php:100:19: DeprecatedMethod: The method OC\Hooks\EmitterTrait::listen has been marked as deprecated (see https://psalm.dev/001)
$eventSource->send('success', $this->l->t('Turned off maintenance mode'));
});
$this->updater->listen('\OC\Updater', 'maintenanceActive', function () use ($eventSource): void {

Check failure on line 103 in core/Controller/UpdateController.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

DeprecatedMethod

core/Controller/UpdateController.php:103:19: DeprecatedMethod: The method OC\Hooks\EmitterTrait::listen has been marked as deprecated (see https://psalm.dev/001)
$eventSource->send('success', $this->l->t('Maintenance mode is kept active'));
});
$this->updater->listen('\OC\Updater', 'dbUpgradeBefore', function () use ($eventSource): void {

Check failure on line 106 in core/Controller/UpdateController.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

DeprecatedMethod

core/Controller/UpdateController.php:106:19: DeprecatedMethod: The method OC\Hooks\EmitterTrait::listen has been marked as deprecated (see https://psalm.dev/001)
$eventSource->send('success', $this->l->t('Updating database schema'));
});
$this->updater->listen('\OC\Updater', 'dbUpgrade', function () use ($eventSource): void {

Check failure on line 109 in core/Controller/UpdateController.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

DeprecatedMethod

core/Controller/UpdateController.php:109:19: DeprecatedMethod: The method OC\Hooks\EmitterTrait::listen has been marked as deprecated (see https://psalm.dev/001)
$eventSource->send('success', $this->l->t('Updated database'));
});
$this->updater->listen('\OC\Updater', 'upgradeAppStoreApp', function ($app) use ($eventSource): void {

Check failure on line 112 in core/Controller/UpdateController.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

DeprecatedMethod

core/Controller/UpdateController.php:112:19: DeprecatedMethod: The method OC\Hooks\EmitterTrait::listen has been marked as deprecated (see https://psalm.dev/001)
$eventSource->send('success', $this->l->t('Update app "%s" from App Store', [$app]));
});
$this->updater->listen('\OC\Updater', 'appSimulateUpdate', function ($app) use ($eventSource): void {

Check failure on line 115 in core/Controller/UpdateController.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

DeprecatedMethod

core/Controller/UpdateController.php:115:19: DeprecatedMethod: The method OC\Hooks\EmitterTrait::listen has been marked as deprecated (see https://psalm.dev/001)
$eventSource->send('success', $this->l->t('Checking whether the database schema for %s can be updated (this can take a long time depending on the database size)', [$app]));
});
$this->updater->listen('\OC\Updater', 'appUpgrade', function ($app, $version) use ($eventSource): void {

Check failure on line 118 in core/Controller/UpdateController.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

DeprecatedMethod

core/Controller/UpdateController.php:118:19: DeprecatedMethod: The method OC\Hooks\EmitterTrait::listen has been marked as deprecated (see https://psalm.dev/001)
$eventSource->send('success', $this->l->t('Updated "%1$s" to %2$s', [$app, $version]));
});
$this->updater->listen('\OC\Updater', 'incompatibleAppDisabled', function ($app) use (&$incompatibleApps, &$incompatibleOverwrites): void {

Check failure on line 121 in core/Controller/UpdateController.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

DeprecatedMethod

core/Controller/UpdateController.php:121:19: DeprecatedMethod: The method OC\Hooks\EmitterTrait::listen has been marked as deprecated (see https://psalm.dev/001)
if (!in_array($app, $incompatibleOverwrites)) {
$incompatibleApps[] = $app;
}
});
$this->updater->listen('\OC\Updater', 'failure', function ($message) use ($eventSource): void {

Check failure on line 126 in core/Controller/UpdateController.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

DeprecatedMethod

core/Controller/UpdateController.php:126:19: DeprecatedMethod: The method OC\Hooks\EmitterTrait::listen has been marked as deprecated (see https://psalm.dev/001)
$eventSource->send('failure', $message);
$this->config->setSystemValue('maintenance', false);
});
$this->updater->listen('\OC\Updater', 'setDebugLogLevel', function ($logLevel, $logLevelName) use ($eventSource): void {
$eventSource->send('success', $this->l->t('Set log level to debug'));
});
$this->updater->listen('\OC\Updater', 'resetLogLevel', function ($logLevel, $logLevelName) use ($eventSource): void {
$eventSource->send('success', $this->l->t('Reset log level'));
});
$this->updater->listen('\OC\Updater', 'startCheckCodeIntegrity', function () use ($eventSource): void {
$eventSource->send('success', $this->l->t('Starting code integrity check'));
});
$this->updater->listen('\OC\Updater', 'finishedCheckCodeIntegrity', function () use ($eventSource): void {
$eventSource->send('success', $this->l->t('Finished code integrity check'));
});

try {
$this->updater->upgrade();
} catch (\Exception $e) {
$this->logger->error(
$e->getMessage(),
[
'exception' => $e,
'app' => 'update',
]);
$eventSource->send('failure', get_class($e) . ': ' . $e->getMessage());
$eventSource->close();
return new DataResponse([]);
}

$disabledApps = [];
foreach ($incompatibleApps as $app) {
$disabledApps[$app] = $this->l->t('%s (incompatible)', [$app]);
}

if (!empty($disabledApps)) {
$eventSource->send('notice', $this->l->t('The following apps have been disabled: %s', [implode(', ', $disabledApps)]));
}

$eventSource->send('done', '');
$eventSource->close();
return new DataResponse([]);
}
}
151 changes: 0 additions & 151 deletions core/ajax/update.php

This file was deleted.

5 changes: 0 additions & 5 deletions core/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,4 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
/** @var Router $this */
// Core ajax actions
// Routing
$this->create('core_ajax_update', '/core/ajax/update.php')
->actionInclude('core/ajax/update.php');

$this->create('heartbeat', '/heartbeat')->get();
4 changes: 2 additions & 2 deletions core/src/views/UpdaterAdmin.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from '@mdi/js'
import { loadState } from '@nextcloud/initial-state'
import { t } from '@nextcloud/l10n'
import { generateFilePath } from '@nextcloud/router'
import { generateOcsUrl } from '@nextcloud/router'
import { NcButton, NcIconSvgWrapper, NcLoadingIcon } from '@nextcloud/vue'
import { computed, onMounted, onUnmounted, ref } from 'vue'
import NcGuestContent from '@nextcloud/vue/components/NcGuestContent'
Expand Down Expand Up @@ -92,7 +92,7 @@ async function onStartUpdate() {
}

isUpdateRunning.value = true
const eventSource = new OCEventSource(generateFilePath('core', '', 'ajax/update.php'))
const eventSource = new OCEventSource(generateOcsUrl('/core/update'))
eventSource.listen('success', (message) => {
messages.value.push({ message, type: 'success' })
})
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1485,6 +1485,7 @@
'OC\\Core\\Controller\\TwoFactorChallengeController' => $baseDir . '/core/Controller/TwoFactorChallengeController.php',
'OC\\Core\\Controller\\UnifiedSearchController' => $baseDir . '/core/Controller/UnifiedSearchController.php',
'OC\\Core\\Controller\\UnsupportedBrowserController' => $baseDir . '/core/Controller/UnsupportedBrowserController.php',
'OC\\Core\\Controller\\UpdateController' => $baseDir . '/core/Controller/UpdateController.php',
'OC\\Core\\Controller\\UserController' => $baseDir . '/core/Controller/UserController.php',
'OC\\Core\\Controller\\WalledGardenController' => $baseDir . '/core/Controller/WalledGardenController.php',
'OC\\Core\\Controller\\WebAuthnController' => $baseDir . '/core/Controller/WebAuthnController.php',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -1526,6 +1526,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Core\\Controller\\TwoFactorChallengeController' => __DIR__ . '/../../..' . '/core/Controller/TwoFactorChallengeController.php',
'OC\\Core\\Controller\\UnifiedSearchController' => __DIR__ . '/../../..' . '/core/Controller/UnifiedSearchController.php',
'OC\\Core\\Controller\\UnsupportedBrowserController' => __DIR__ . '/../../..' . '/core/Controller/UnsupportedBrowserController.php',
'OC\\Core\\Controller\\UpdateController' => __DIR__ . '/../../..' . '/core/Controller/UpdateController.php',
'OC\\Core\\Controller\\UserController' => __DIR__ . '/../../..' . '/core/Controller/UserController.php',
'OC\\Core\\Controller\\WalledGardenController' => __DIR__ . '/../../..' . '/core/Controller/WalledGardenController.php',
'OC\\Core\\Controller\\WebAuthnController' => __DIR__ . '/../../..' . '/core/Controller/WebAuthnController.php',
Expand Down
Loading
Loading