diff options
Diffstat (limited to 'lib/private')
-rw-r--r-- | lib/private/Activity/Event.php | 40 | ||||
-rw-r--r-- | lib/private/Activity/Manager.php | 32 | ||||
-rw-r--r-- | lib/private/Files/Cache/Cache.php | 3 | ||||
-rw-r--r-- | lib/private/Files/Cache/SearchBuilder.php | 2 | ||||
-rw-r--r-- | lib/private/Files/ObjectStore/ObjectStoreStorage.php | 22 | ||||
-rw-r--r-- | lib/private/Files/ObjectStore/StorageObjectStore.php | 4 | ||||
-rw-r--r-- | lib/private/Notification/Manager.php | 4 | ||||
-rw-r--r-- | lib/private/Notification/Notification.php | 30 | ||||
-rw-r--r-- | lib/private/RichObjectStrings/RichTextFormatter.php | 36 | ||||
-rw-r--r-- | lib/private/Server.php | 4 |
10 files changed, 82 insertions, 95 deletions
diff --git a/lib/private/Activity/Event.php b/lib/private/Activity/Event.php index 02cfd758058..810af5c0612 100644 --- a/lib/private/Activity/Event.php +++ b/lib/private/Activity/Event.php @@ -12,6 +12,7 @@ namespace OC\Activity; use OCP\Activity\Exceptions\InvalidValueException; use OCP\Activity\IEvent; use OCP\RichObjectStrings\InvalidObjectExeption; +use OCP\RichObjectStrings\IRichTextFormatter; use OCP\RichObjectStrings\IValidator; class Event implements IEvent { @@ -60,14 +61,11 @@ class Event implements IEvent { /** @var IEvent|null */ protected $child; - /** @var IValidator */ - protected $richValidator; - /** - * @param IValidator $richValidator - */ - public function __construct(IValidator $richValidator) { - $this->richValidator = $richValidator; + public function __construct( + protected IValidator $richValidator, + protected IRichTextFormatter $richTextFormatter, + ) { } /** @@ -217,7 +215,7 @@ class Event implements IEvent { if ($this->subjectParsed === '') { try { - $this->subjectParsed = $this->richToParsed($subject, $parameters); + $this->subjectParsed = $this->richTextFormatter->richToParsed($subject, $parameters); } catch (\InvalidArgumentException $e) { throw new InvalidValueException('richSubjectParameters', $e); } @@ -227,30 +225,6 @@ class Event implements IEvent { } /** - * @throws \InvalidArgumentException if a parameter has no name or no type - */ - private function richToParsed(string $message, array $parameters): string { - $placeholders = []; - $replacements = []; - foreach ($parameters as $placeholder => $parameter) { - $placeholders[] = '{' . $placeholder . '}'; - foreach (['name','type'] as $requiredField) { - if (!isset($parameter[$requiredField]) || !is_string($parameter[$requiredField])) { - throw new \InvalidArgumentException("Invalid rich object, {$requiredField} field is missing"); - } - } - if ($parameter['type'] === 'user') { - $replacements[] = '@' . $parameter['name']; - } elseif ($parameter['type'] === 'file') { - $replacements[] = $parameter['path'] ?? $parameter['name']; - } else { - $replacements[] = $parameter['name']; - } - } - return str_replace($placeholders, $replacements, $message); - } - - /** * @return string * @since 11.0.0 */ @@ -317,7 +291,7 @@ class Event implements IEvent { if ($this->messageParsed === '') { try { - $this->messageParsed = $this->richToParsed($message, $parameters); + $this->messageParsed = $this->richTextFormatter->richToParsed($message, $parameters); } catch (\InvalidArgumentException $e) { throw new InvalidValueException('richMessageParameters', $e); } diff --git a/lib/private/Activity/Manager.php b/lib/private/Activity/Manager.php index 54d50a47dcb..5c306fe6399 100644 --- a/lib/private/Activity/Manager.php +++ b/lib/private/Activity/Manager.php @@ -21,20 +21,10 @@ use OCP\IL10N; use OCP\IRequest; use OCP\IUser; use OCP\IUserSession; +use OCP\RichObjectStrings\IRichTextFormatter; use OCP\RichObjectStrings\IValidator; class Manager implements IManager { - /** @var IRequest */ - protected $request; - - /** @var IUserSession */ - protected $session; - - /** @var IConfig */ - protected $config; - - /** @var IValidator */ - protected $validator; /** @var string */ protected $formattingObjectType; @@ -48,20 +38,14 @@ class Manager implements IManager { /** @var string */ protected $currentUserId; - protected $l10n; - public function __construct( - IRequest $request, - IUserSession $session, - IConfig $config, - IValidator $validator, - IL10N $l10n + protected IRequest $request, + protected IUserSession $session, + protected IConfig $config, + protected IValidator $validator, + protected IRichTextFormatter $richTextFormatter, + protected IL10N $l10n, ) { - $this->request = $request; - $this->session = $session; - $this->config = $config; - $this->validator = $validator; - $this->l10n = $l10n; } /** @var \Closure[] */ @@ -104,7 +88,7 @@ class Manager implements IManager { * @return IEvent */ public function generateEvent(): IEvent { - return new Event($this->validator); + return new Event($this->validator, $this->richTextFormatter); } /** diff --git a/lib/private/Files/Cache/Cache.php b/lib/private/Files/Cache/Cache.php index e1547fefe1c..de66e8924a5 100644 --- a/lib/private/Files/Cache/Cache.php +++ b/lib/private/Files/Cache/Cache.php @@ -265,6 +265,9 @@ class Cache implements ICache { if (!isset($data['parent'])) { $data['parent'] = $this->getParentId($file); } + if ($data['parent'] === -1 && $file !== '') { + throw new \Exception('Parent folder not in filecache for ' . $file); + } $data['name'] = basename($file); [$values, $extensionValues] = $this->normalizeData($data); diff --git a/lib/private/Files/Cache/SearchBuilder.php b/lib/private/Files/Cache/SearchBuilder.php index 748844b9e1b..41f942cab03 100644 --- a/lib/private/Files/Cache/SearchBuilder.php +++ b/lib/private/Files/Cache/SearchBuilder.php @@ -64,7 +64,7 @@ class SearchBuilder { 'owner' => 'string', ]; - /** @var array<string, int> */ + /** @var array<string, int|string> */ protected static $paramTypeMap = [ 'string' => IQueryBuilder::PARAM_STR, 'integer' => IQueryBuilder::PARAM_INT, diff --git a/lib/private/Files/ObjectStore/ObjectStoreStorage.php b/lib/private/Files/ObjectStore/ObjectStoreStorage.php index 1b55b4b3b44..60affa4b89e 100644 --- a/lib/private/Files/ObjectStore/ObjectStoreStorage.php +++ b/lib/private/Files/ObjectStore/ObjectStoreStorage.php @@ -459,13 +459,10 @@ class ObjectStoreStorage extends \OC\Files\Storage\Common implements IChunkedFil } public function file_put_contents($path, $data) { - $handle = $this->fopen($path, 'w+'); - if (!$handle) { - return false; - } - $result = fwrite($handle, $data); - fclose($handle); - return $result; + $fh = fopen('php://temp', 'w+'); + fwrite($fh, $data); + rewind($fh); + return $this->writeStream($path, $fh, strlen($data)); } public function writeStream(string $path, $stream, ?int $size = null): int { @@ -495,6 +492,10 @@ class ObjectStoreStorage extends \OC\Files\Storage\Common implements IChunkedFil if ($exists) { $fileId = $stat['fileid']; } else { + $parent = $this->normalizePath(dirname($path)); + if (!$this->is_dir($parent)) { + throw new \InvalidArgumentException("trying to upload a file ($path) inside a non-directory ($parent)"); + } $fileId = $this->getCache()->put($uploadPath, $stat); } @@ -602,15 +603,20 @@ class ObjectStoreStorage extends \OC\Files\Storage\Common implements IChunkedFil $sourceCacheEntry = $sourceCache->get($sourceInternalPath); } if ($sourceCacheEntry->getMimeType() === FileInfo::MIMETYPE_FOLDER) { + $this->mkdir($targetInternalPath); foreach ($sourceCache->getFolderContents($sourceInternalPath) as $child) { $this->moveFromStorage($sourceStorage, $child->getPath(), $targetInternalPath . '/' . $child->getName()); } $sourceStorage->rmdir($sourceInternalPath); } else { + $sourceStream = $sourceStorage->fopen($sourceInternalPath, 'r'); + if (!$sourceStream) { + return false; + } // move the cache entry before the contents so that we have the correct fileid/urn for the target $this->getCache()->moveFromCache($sourceCache, $sourceInternalPath, $targetInternalPath); try { - $this->writeStream($targetInternalPath, $sourceStorage->fopen($sourceInternalPath, 'r'), $sourceCacheEntry->getSize()); + $this->writeStream($targetInternalPath, $sourceStream, $sourceCacheEntry->getSize()); } catch (\Exception $e) { // restore the cache entry $sourceCache->moveFromCache($this->getCache(), $targetInternalPath, $sourceInternalPath); diff --git a/lib/private/Files/ObjectStore/StorageObjectStore.php b/lib/private/Files/ObjectStore/StorageObjectStore.php index 5e7125e18a6..4361795ec45 100644 --- a/lib/private/Files/ObjectStore/StorageObjectStore.php +++ b/lib/private/Files/ObjectStore/StorageObjectStore.php @@ -27,8 +27,8 @@ class StorageObjectStore implements IObjectStore { * @return string the container or bucket name where objects are stored * @since 7.0.0 */ - public function getStorageId() { - $this->storage->getId(); + public function getStorageId(): string { + return $this->storage->getId(); } /** diff --git a/lib/private/Notification/Manager.php b/lib/private/Notification/Manager.php index 8edbca0380d..ac287e97048 100644 --- a/lib/private/Notification/Manager.php +++ b/lib/private/Notification/Manager.php @@ -22,6 +22,7 @@ use OCP\Notification\IncompleteParsedNotificationException; use OCP\Notification\INotification; use OCP\Notification\INotifier; use OCP\Notification\UnknownNotificationException; +use OCP\RichObjectStrings\IRichTextFormatter; use OCP\RichObjectStrings\IValidator; use OCP\Support\Subscription\IRegistry; use Psr\Container\ContainerExceptionInterface; @@ -55,6 +56,7 @@ class Manager implements IManager { protected IRegistry $subscription, protected LoggerInterface $logger, private Coordinator $coordinator, + private IRichTextFormatter $richTextFormatter, ) { $this->cache = $cacheFactory->createDistributed('notifications'); @@ -199,7 +201,7 @@ class Manager implements IManager { * @since 8.2.0 */ public function createNotification(): INotification { - return new Notification($this->validator); + return new Notification($this->validator, $this->richTextFormatter); } /** diff --git a/lib/private/Notification/Notification.php b/lib/private/Notification/Notification.php index 19c836c2044..1c624b50d44 100644 --- a/lib/private/Notification/Notification.php +++ b/lib/private/Notification/Notification.php @@ -12,6 +12,7 @@ use OCP\Notification\IAction; use OCP\Notification\INotification; use OCP\Notification\InvalidValueException; use OCP\RichObjectStrings\InvalidObjectExeption; +use OCP\RichObjectStrings\IRichTextFormatter; use OCP\RichObjectStrings\IValidator; class Notification implements INotification { @@ -39,6 +40,7 @@ class Notification implements INotification { public function __construct( protected IValidator $richValidator, + protected IRichTextFormatter $richTextFormatter, ) { $this->dateTime = new \DateTime(); $this->dateTime->setTimestamp(0); @@ -187,7 +189,7 @@ class Notification implements INotification { if ($this->subjectParsed === '') { try { - $this->subjectParsed = $this->richToParsed($subject, $parameters); + $this->subjectParsed = $this->richTextFormatter->richToParsed($subject, $parameters); } catch (\InvalidArgumentException $e) { throw new InvalidValueException('richSubjectParameters', $e); } @@ -197,30 +199,6 @@ class Notification implements INotification { } /** - * @throws \InvalidArgumentException if a parameter has no name or no type - */ - private function richToParsed(string $message, array $parameters): string { - $placeholders = []; - $replacements = []; - foreach ($parameters as $placeholder => $parameter) { - $placeholders[] = '{' . $placeholder . '}'; - foreach (['name','type'] as $requiredField) { - if (!isset($parameter[$requiredField]) || !is_string($parameter[$requiredField])) { - throw new \InvalidArgumentException("Invalid rich object, {$requiredField} field is missing"); - } - } - if ($parameter['type'] === 'user') { - $replacements[] = '@' . $parameter['name']; - } elseif ($parameter['type'] === 'file') { - $replacements[] = $parameter['path'] ?? $parameter['name']; - } else { - $replacements[] = $parameter['name']; - } - } - return str_replace($placeholders, $replacements, $message); - } - - /** * {@inheritDoc} */ public function getRichSubject(): string { @@ -293,7 +271,7 @@ class Notification implements INotification { if ($this->messageParsed === '') { try { - $this->messageParsed = $this->richToParsed($message, $parameters); + $this->messageParsed = $this->richTextFormatter->richToParsed($message, $parameters); } catch (\InvalidArgumentException $e) { throw new InvalidValueException('richMessageParameters', $e); } diff --git a/lib/private/RichObjectStrings/RichTextFormatter.php b/lib/private/RichObjectStrings/RichTextFormatter.php new file mode 100644 index 00000000000..9c9ddf94fa9 --- /dev/null +++ b/lib/private/RichObjectStrings/RichTextFormatter.php @@ -0,0 +1,36 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace OC\RichObjectStrings; + +use OCP\RichObjectStrings\IRichTextFormatter; + +class RichTextFormatter implements IRichTextFormatter { + /** + * @throws \InvalidArgumentException if a parameter has no name or no type + */ + public function richToParsed(string $message, array $parameters): string { + $placeholders = []; + $replacements = []; + foreach ($parameters as $placeholder => $parameter) { + $placeholders[] = '{' . $placeholder . '}'; + foreach (['name','type'] as $requiredField) { + if (!isset($parameter[$requiredField]) || !is_string($parameter[$requiredField])) { + throw new \InvalidArgumentException("Invalid rich object, {$requiredField} field is missing"); + } + } + $replacements[] = match($parameter['type']) { + 'user' => '@' . $parameter['name'], + 'file' => $parameter['path'] ?? $parameter['name'], + default => $parameter['name'], + }; + } + return str_replace($placeholders, $replacements, $message); + } +} diff --git a/lib/private/Server.php b/lib/private/Server.php index 7ecafdcd66c..f48cc0d0a48 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -204,6 +204,7 @@ use OCP\Profile\IProfileManager; use OCP\Profiler\IProfiler; use OCP\Remote\Api\IApiFactory; use OCP\Remote\IInstanceFactory; +use OCP\RichObjectStrings\IRichTextFormatter; use OCP\RichObjectStrings\IValidator; use OCP\Route\IRouter; use OCP\Security\Bruteforce\IThrottler; @@ -648,6 +649,7 @@ class Server extends ServerContainer implements IServerContainer { $c->get(IUserSession::class), $c->get(\OCP\IConfig::class), $c->get(IValidator::class), + $c->get(IRichTextFormatter::class), $l10n ); }); @@ -1285,6 +1287,8 @@ class Server extends ServerContainer implements IServerContainer { $this->registerAlias(\OCP\Security\Ip\IFactory::class, \OC\Security\Ip\Factory::class); + $this->registerAlias(IRichTextFormatter::class, \OC\RichObjectStrings\RichTextFormatter::class); + $this->connectDispatcher(); } |