diff options
Diffstat (limited to 'lib/private')
-rw-r--r-- | lib/private/AppFramework/DependencyInjection/DIContainer.php | 6 | ||||
-rw-r--r-- | lib/private/Notification/Manager.php | 5 | ||||
-rw-r--r-- | lib/private/Notification/Notification.php | 64 | ||||
-rw-r--r-- | lib/private/RichObjectStrings/Validator.php | 108 |
4 files changed, 181 insertions, 2 deletions
diff --git a/lib/private/AppFramework/DependencyInjection/DIContainer.php b/lib/private/AppFramework/DependencyInjection/DIContainer.php index 8fe9b4dca03..671093ff08b 100644 --- a/lib/private/AppFramework/DependencyInjection/DIContainer.php +++ b/lib/private/AppFramework/DependencyInjection/DIContainer.php @@ -44,10 +44,12 @@ use OC\AppFramework\Middleware\Security\SecurityMiddleware; use OC\AppFramework\Middleware\SessionMiddleware; use OC\AppFramework\Utility\SimpleContainer; use OC\Core\Middleware\TwoFactorMiddleware; +use OC\RichObjectStrings\Validator; use OCP\AppFramework\IApi; use OCP\AppFramework\IAppContainer; use OCP\Files\IAppData; use OCP\Files\Mount\IMountManager; +use OCP\RichObjectStrings\IValidator; class DIContainer extends SimpleContainer implements IAppContainer { @@ -330,6 +332,10 @@ class DIContainer extends SimpleContainer implements IAppContainer { return $this->getServer()->getEncryptionManager(); }); + $this->registerService(IValidator::class, function($c) { + return $c->query(Validator::class); + }); + /** * App Framework APIs diff --git a/lib/private/Notification/Manager.php b/lib/private/Notification/Manager.php index ec831cf9793..4b57db4bac9 100644 --- a/lib/private/Notification/Manager.php +++ b/lib/private/Notification/Manager.php @@ -24,6 +24,7 @@ namespace OC\Notification; +use OC\RichObjectStrings\Validator; use OCP\Notification\IApp; use OCP\Notification\IManager; use OCP\Notification\INotification; @@ -149,7 +150,9 @@ class Manager implements IManager { * @since 8.2.0 */ public function createNotification() { - return new Notification(); + return new Notification( + new Validator() + ); } /** diff --git a/lib/private/Notification/Notification.php b/lib/private/Notification/Notification.php index 7bf4b9a74cf..7346f35f966 100644 --- a/lib/private/Notification/Notification.php +++ b/lib/private/Notification/Notification.php @@ -26,8 +26,14 @@ namespace OC\Notification; use OCP\Notification\IAction; use OCP\Notification\INotification; +use OCP\RichObjectStrings\InvalidObjectExeption; +use OCP\RichObjectStrings\IValidator; class Notification implements INotification { + + /** @var IValidator */ + protected $richValidator; + /** @var string */ protected $app; @@ -53,6 +59,12 @@ class Notification implements INotification { protected $subjectParsed; /** @var string */ + protected $subjectRich; + + /** @var array */ + protected $subjectRichParameters; + + /** @var string */ protected $message; /** @var array */ @@ -81,8 +93,11 @@ class Notification implements INotification { /** * Constructor + * + * @param IValidator $richValidator */ - public function __construct() { + public function __construct(IValidator $richValidator) { + $this->richValidator = $richValidator; $this->app = ''; $this->user = ''; $this->dateTime = new \DateTime(); @@ -92,6 +107,8 @@ class Notification implements INotification { $this->subject = ''; $this->subjectParameters = []; $this->subjectParsed = ''; + $this->subjectRich = ''; + $this->subjectRichParameters = []; $this->message = ''; $this->messageParameters = []; $this->messageParsed = ''; @@ -262,6 +279,43 @@ class Notification implements INotification { } /** + * @param string $subject + * @param array $parameters + * @return $this + * @throws \InvalidArgumentException if the subject or parameters are invalid + * @since 9.2.0 + */ + public function setRichSubject($subject, array $parameters = []) { + if (!is_string($subject) || $subject === '') { + throw new \InvalidArgumentException('The given parsed subject is invalid'); + } + $this->subjectRich = $subject; + + if (!is_array($parameters)) { + throw new \InvalidArgumentException('The given subject parameters are invalid'); + } + $this->subjectRichParameters = $parameters; + + return $this; + } + + /** + * @return string + * @since 9.2.0 + */ + public function getRichSubject() { + return $this->subjectRich; + } + + /** + * @return array[] + * @since 9.2.0 + */ + public function getRichSubjectParameters() { + return $this->subjectRichParameters; + } + + /** * @param string $message * @param array $parameters * @return $this @@ -454,6 +508,14 @@ class Notification implements INotification { * @since 8.2.0 */ public function isValidParsed() { + if ($this->getRichSubject() !== '' || !empty($this->getRichSubjectParameters())) { + try { + $this->richValidator->validate($this->getRichSubject(), $this->getRichSubjectParameters()); + } catch (InvalidObjectExeption $e) { + return false; + } + } + return $this->isValidCommon() && diff --git a/lib/private/RichObjectStrings/Validator.php b/lib/private/RichObjectStrings/Validator.php new file mode 100644 index 00000000000..2729b4a3f1b --- /dev/null +++ b/lib/private/RichObjectStrings/Validator.php @@ -0,0 +1,108 @@ +<?php +/** + * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * 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 + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OC\RichObjectStrings; + + +use OCP\RichObjectStrings\InvalidObjectExeption; +use OCP\RichObjectStrings\IValidator; + +/** + * Class Validator + * + * @package OCP\RichObjectStrings + * @since 9.2.0 + */ +class Validator implements IValidator { + + /** @var array[] */ + protected $definitions; + + /** @var array[] */ + protected $requiredParameters = []; + + /** + * Constructor + */ + public function __construct() { + $this->definitions = json_decode(file_get_contents(__DIR__ . '/../../public/RichObjectStrings/definitions.json'), true); + } + + /** + * @param string $subject + * @param array[] $parameters + * @throws InvalidObjectExeption + * @since 9.2.0 + */ + public function validate($subject, array $parameters) { + $matches = []; + $result = preg_match_all('/\{([a-z0-9]+)\}/i', $subject, $matches); + + if ($result === false) { + throw new InvalidObjectExeption(); + } + + if (!empty($matches[1])) { + foreach ($matches[1] as $parameter) { + if (!isset($parameters[$parameter])) { + throw new InvalidObjectExeption('Parameter is undefined'); + } else { + $this->validateParameter($parameters[$parameter]); + } + } + } + } + + /** + * @param array $parameter + * @throws InvalidObjectExeption + */ + protected function validateParameter(array $parameter) { + if (!isset($parameter['type']) || !isset($this->definitions[$parameter['type']])) { + throw new InvalidObjectExeption('Object type is undefined'); + } + + $requiredParameters = $this->getRequiredParameters($parameter['type']); + $missingKeys = array_diff($requiredParameters, array_keys($parameter)); + if (!empty($missingKeys)) { + throw new InvalidObjectExeption('Object is invalid'); + } + } + + /** + * @param string $type + * @return string[] + */ + protected function getRequiredParameters($type) { + if (isset($this->requiredParameters[$type])) { + return $this->requiredParameters[$type]; + } + + $this->requiredParameters[$type] = []; + foreach ($this->definitions[$type]['parameters'] as $parameter => $data) { + if ($data['required']) { + $this->requiredParameters[$type][] = $parameter; + } + } + + return $this->requiredParameters[$type]; + } +} |