diff --git a/core/ajax/update.php b/core/ajax/update.php index dae08ad0882f9..1678e30c02ecb 100644 --- a/core/ajax/update.php +++ b/core/ajax/update.php @@ -33,6 +33,7 @@ use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventDispatcher; use OCP\IEventSource; +use OCP\IEventSourceFactory; use OCP\IL10N; use OCP\ILogger; use OC\DB\MigratorExecuteSqlEvent; @@ -43,6 +44,7 @@ use OC\Repair\Events\RepairStartEvent; use OC\Repair\Events\RepairStepEvent; use OC\Repair\Events\RepairWarningEvent; +use OCP\L10N\IFactory; if (strpos(@ini_get('disable_functions'), 'set_time_limit') === false) { @set_time_limit(0); @@ -50,9 +52,10 @@ require_once '../../lib/base.php'; -$l = \OC::$server->getL10N('core'); +/** @var \OCP\IL10N $l */ +$l = \OC::$server->get(IFactory::class)->get('core'); -$eventSource = \OC::$server->createEventSource(); +$eventSource = \OC::$server->get(IEventSourceFactory::class)->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 diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index f35b9c94fbc45..9f9ff8f60f39d 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -453,6 +453,7 @@ 'OCP\\IDateTimeZone' => $baseDir . '/lib/public/IDateTimeZone.php', 'OCP\\IEmojiHelper' => $baseDir . '/lib/public/IEmojiHelper.php', 'OCP\\IEventSource' => $baseDir . '/lib/public/IEventSource.php', + 'OCP\\IEventSourceFactory' => $baseDir . '/lib/public/IEventSourceFactory.php', 'OCP\\IGroup' => $baseDir . '/lib/public/IGroup.php', 'OCP\\IGroupManager' => $baseDir . '/lib/public/IGroupManager.php', 'OCP\\IImage' => $baseDir . '/lib/public/IImage.php', @@ -1196,6 +1197,7 @@ 'OC\\EventDispatcher\\GenericEventWrapper' => $baseDir . '/lib/private/EventDispatcher/GenericEventWrapper.php', 'OC\\EventDispatcher\\ServiceEventListener' => $baseDir . '/lib/private/EventDispatcher/ServiceEventListener.php', 'OC\\EventDispatcher\\SymfonyAdapter' => $baseDir . '/lib/private/EventDispatcher/SymfonyAdapter.php', + 'OC\\EventSourceFactory' => $baseDir . '/lib/private/EventSourceFactory.php', 'OC\\Federation\\CloudFederationFactory' => $baseDir . '/lib/private/Federation/CloudFederationFactory.php', 'OC\\Federation\\CloudFederationNotification' => $baseDir . '/lib/private/Federation/CloudFederationNotification.php', 'OC\\Federation\\CloudFederationProviderManager' => $baseDir . '/lib/private/Federation/CloudFederationProviderManager.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 8de7ef99d0216..546fa72c32541 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -486,6 +486,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OCP\\IDateTimeZone' => __DIR__ . '/../../..' . '/lib/public/IDateTimeZone.php', 'OCP\\IEmojiHelper' => __DIR__ . '/../../..' . '/lib/public/IEmojiHelper.php', 'OCP\\IEventSource' => __DIR__ . '/../../..' . '/lib/public/IEventSource.php', + 'OCP\\IEventSourceFactory' => __DIR__ . '/../../..' . '/lib/public/IEventSourceFactory.php', 'OCP\\IGroup' => __DIR__ . '/../../..' . '/lib/public/IGroup.php', 'OCP\\IGroupManager' => __DIR__ . '/../../..' . '/lib/public/IGroupManager.php', 'OCP\\IImage' => __DIR__ . '/../../..' . '/lib/public/IImage.php', @@ -1229,6 +1230,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OC\\EventDispatcher\\GenericEventWrapper' => __DIR__ . '/../../..' . '/lib/private/EventDispatcher/GenericEventWrapper.php', 'OC\\EventDispatcher\\ServiceEventListener' => __DIR__ . '/../../..' . '/lib/private/EventDispatcher/ServiceEventListener.php', 'OC\\EventDispatcher\\SymfonyAdapter' => __DIR__ . '/../../..' . '/lib/private/EventDispatcher/SymfonyAdapter.php', + 'OC\\EventSourceFactory' => __DIR__ . '/../../..' . '/lib/private/EventSourceFactory.php', 'OC\\Federation\\CloudFederationFactory' => __DIR__ . '/../../..' . '/lib/private/Federation/CloudFederationFactory.php', 'OC\\Federation\\CloudFederationNotification' => __DIR__ . '/../../..' . '/lib/private/Federation/CloudFederationNotification.php', 'OC\\Federation\\CloudFederationProviderManager' => __DIR__ . '/../../..' . '/lib/private/Federation/CloudFederationProviderManager.php', diff --git a/lib/private/EventSourceFactory.php b/lib/private/EventSourceFactory.php new file mode 100644 index 0000000000000..197c8bf9e6ca7 --- /dev/null +++ b/lib/private/EventSourceFactory.php @@ -0,0 +1,46 @@ + + * + * @author Daniel Kesselberg + * + * @license AGPL-3.0-or-later + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program 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, version 3, + * along with this program. If not, see + * + */ + +namespace OC; + +use OCP\IEventSource; +use OCP\IEventSourceFactory; +use OCP\IRequest; + +class EventSourceFactory implements IEventSourceFactory { + private IRequest $request; + + + public function __construct(IRequest $request) { + $this->request = $request; + } + + /** + * Create a new event source + * + * @return IEventSource + * @since 28.0.0 + */ + public function create(): IEventSource { + return new \OC_EventSource($this->request); + } +} diff --git a/lib/private/Server.php b/lib/private/Server.php index 84860f8c494ee..f98ee051a3230 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -211,6 +211,7 @@ use OCP\IDateTimeFormatter; use OCP\IDateTimeZone; use OCP\IDBConnection; +use OCP\IEventSourceFactory; use OCP\IGroupManager; use OCP\IInitialStateService; use OCP\IL10N; @@ -1467,6 +1468,8 @@ public function __construct($webRoot, \OC\Config $config) { $this->registerAlias(ISpeechToTextManager::class, SpeechToTextManager::class); + $this->registerAlias(IEventSourceFactory::class, EventSourceFactory::class); + $this->connectDispatcher(); } @@ -1928,16 +1931,6 @@ public function getHTTPClientService() { return $this->get(IClientService::class); } - /** - * Create a new event source - * - * @return \OCP\IEventSource - * @deprecated 20.0.0 - */ - public function createEventSource() { - return new \OC_EventSource(); - } - /** * Get the active event logger * diff --git a/lib/private/legacy/OC_EventSource.php b/lib/private/legacy/OC_EventSource.php index c733316050f49..cd72ba1f2d558 100644 --- a/lib/private/legacy/OC_EventSource.php +++ b/lib/private/legacy/OC_EventSource.php @@ -1,4 +1,7 @@ request = $request; + } + protected function init() { if ($this->started) { return; @@ -71,11 +80,11 @@ protected function init() { } else { header("Content-Type: text/event-stream"); } - if (!\OC::$server->getRequest()->passesStrictCookieCheck()) { + if (!$this->request->passesStrictCookieCheck()) { header('Location: '.\OC::$WEBROOT); exit(); } - if (!\OC::$server->getRequest()->passesCSRFCheck()) { + if (!$this->request->passesCSRFCheck()) { $this->send('error', 'Possible CSRF attack. Connection will be closed.'); $this->close(); exit(); diff --git a/lib/public/IEventSourceFactory.php b/lib/public/IEventSourceFactory.php new file mode 100644 index 0000000000000..5876189cebb9d --- /dev/null +++ b/lib/public/IEventSourceFactory.php @@ -0,0 +1,38 @@ + + * + * @author Daniel Kesselberg + * + * @license AGPL-3.0-or-later + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program 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, version 3, + * along with this program. If not, see + * + */ +namespace OCP; + +/** + * @since 28.0.0 + */ +interface IEventSourceFactory { + /** + * Create a new event source + * + * @return IEventSource + * @since 28.0.0 + */ + public function create(): IEventSource; +} diff --git a/lib/public/IServerContainer.php b/lib/public/IServerContainer.php index c69dab8b3c6fd..f438838b98e6c 100644 --- a/lib/public/IServerContainer.php +++ b/lib/public/IServerContainer.php @@ -395,15 +395,6 @@ public function getSearch(); */ public function getCertificateManager(); - /** - * Create a new event source - * - * @return \OCP\IEventSource - * @since 8.0.0 - * @deprecated 20.0.0 have it injected or fetch it through \Psr\Container\ContainerInterface::get - */ - public function createEventSource(); - /** * Returns an instance of the HTTP client service * diff --git a/tests/lib/EventSourceFactoryTest.php b/tests/lib/EventSourceFactoryTest.php new file mode 100644 index 0000000000000..67bc900bd4c03 --- /dev/null +++ b/tests/lib/EventSourceFactoryTest.php @@ -0,0 +1,37 @@ + + * + * @author Daniel Kesselberg + * + * @license AGPL-3.0-or-later + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program 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, version 3, + * along with this program. If not, see + * + */ + +namespace Test; + +use OC\EventSourceFactory; +use OCP\IEventSource; +use OCP\IRequest; + +class EventSourceFactoryTest extends TestCase { + public function testCreate(): void { + $request = $this->createMock(IRequest::class); + $factory = new EventSourceFactory($request); + + $instance = $factory->create(); + $this->assertInstanceOf(IEventSource::class, $instance); + } +} diff --git a/tests/lib/ServerTest.php b/tests/lib/ServerTest.php index cbaa0e0762ad1..d9f95e9eab172 100644 --- a/tests/lib/ServerTest.php +++ b/tests/lib/ServerTest.php @@ -179,11 +179,6 @@ public function testGetCertificateManager() { $this->assertInstanceOf('\OCP\ICertificateManager', $this->server->getCertificateManager(), 'service returned by "getCertificateManager" did not return the right class'); } - public function testCreateEventSource() { - $this->assertInstanceOf('\OC_EventSource', $this->server->createEventSource(), 'service returned by "createEventSource" did not return the right class'); - $this->assertInstanceOf('\OCP\IEventSource', $this->server->createEventSource(), 'service returned by "createEventSource" did not return the right class'); - } - public function testOverwriteDefaultCommentsManager() { $config = $this->server->getConfig(); $defaultManagerFactory = $config->getSystemValue('comments.managerFactory', '\OC\Comments\ManagerFactory');