@@ -26,83 +26,83 @@ |
||
| 26 | 26 | |
| 27 | 27 | class LegacyFilter implements IFilter { |
| 28 | 28 | |
| 29 | - /** @var IManager */ |
|
| 30 | - protected $manager; |
|
| 31 | - /** @var string */ |
|
| 32 | - protected $identifier; |
|
| 33 | - /** @var string */ |
|
| 34 | - protected $name; |
|
| 35 | - /** @var bool */ |
|
| 36 | - protected $isTopFilter; |
|
| 29 | + /** @var IManager */ |
|
| 30 | + protected $manager; |
|
| 31 | + /** @var string */ |
|
| 32 | + protected $identifier; |
|
| 33 | + /** @var string */ |
|
| 34 | + protected $name; |
|
| 35 | + /** @var bool */ |
|
| 36 | + protected $isTopFilter; |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * LegacySetting constructor. |
|
| 40 | - * |
|
| 41 | - * @param IManager $manager |
|
| 42 | - * @param string $identifier |
|
| 43 | - * @param string $name |
|
| 44 | - * @param bool $isTopFilter |
|
| 45 | - */ |
|
| 46 | - public function __construct(IManager $manager, |
|
| 47 | - $identifier, |
|
| 48 | - $name, |
|
| 49 | - $isTopFilter) { |
|
| 50 | - $this->manager = $manager; |
|
| 51 | - $this->identifier = $identifier; |
|
| 52 | - $this->name = $name; |
|
| 53 | - $this->isTopFilter = $isTopFilter; |
|
| 54 | - } |
|
| 38 | + /** |
|
| 39 | + * LegacySetting constructor. |
|
| 40 | + * |
|
| 41 | + * @param IManager $manager |
|
| 42 | + * @param string $identifier |
|
| 43 | + * @param string $name |
|
| 44 | + * @param bool $isTopFilter |
|
| 45 | + */ |
|
| 46 | + public function __construct(IManager $manager, |
|
| 47 | + $identifier, |
|
| 48 | + $name, |
|
| 49 | + $isTopFilter) { |
|
| 50 | + $this->manager = $manager; |
|
| 51 | + $this->identifier = $identifier; |
|
| 52 | + $this->name = $name; |
|
| 53 | + $this->isTopFilter = $isTopFilter; |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * @return string Lowercase a-z and underscore only identifier |
|
| 58 | - * @since 11.0.0 |
|
| 59 | - */ |
|
| 60 | - public function getIdentifier() { |
|
| 61 | - return $this->identifier; |
|
| 62 | - } |
|
| 56 | + /** |
|
| 57 | + * @return string Lowercase a-z and underscore only identifier |
|
| 58 | + * @since 11.0.0 |
|
| 59 | + */ |
|
| 60 | + public function getIdentifier() { |
|
| 61 | + return $this->identifier; |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * @return string A translated string |
|
| 66 | - * @since 11.0.0 |
|
| 67 | - */ |
|
| 68 | - public function getName() { |
|
| 69 | - return $this->name; |
|
| 70 | - } |
|
| 64 | + /** |
|
| 65 | + * @return string A translated string |
|
| 66 | + * @since 11.0.0 |
|
| 67 | + */ |
|
| 68 | + public function getName() { |
|
| 69 | + return $this->name; |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * @return int whether the filter should be rather on the top or bottom of |
|
| 74 | - * the admin section. The filters are arranged in ascending order of the |
|
| 75 | - * priority values. It is required to return a value between 0 and 100. |
|
| 76 | - * @since 11.0.0 |
|
| 77 | - */ |
|
| 78 | - public function getPriority() { |
|
| 79 | - return $this->isTopFilter ? 40 : 50; |
|
| 80 | - } |
|
| 72 | + /** |
|
| 73 | + * @return int whether the filter should be rather on the top or bottom of |
|
| 74 | + * the admin section. The filters are arranged in ascending order of the |
|
| 75 | + * priority values. It is required to return a value between 0 and 100. |
|
| 76 | + * @since 11.0.0 |
|
| 77 | + */ |
|
| 78 | + public function getPriority() { |
|
| 79 | + return $this->isTopFilter ? 40 : 50; |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * @return string Full URL to an icon, empty string when none is given |
|
| 84 | - * @since 11.0.0 |
|
| 85 | - */ |
|
| 86 | - public function getIcon() { |
|
| 87 | - // Old API was CSS class, so we can not use this... |
|
| 88 | - return ''; |
|
| 89 | - } |
|
| 82 | + /** |
|
| 83 | + * @return string Full URL to an icon, empty string when none is given |
|
| 84 | + * @since 11.0.0 |
|
| 85 | + */ |
|
| 86 | + public function getIcon() { |
|
| 87 | + // Old API was CSS class, so we can not use this... |
|
| 88 | + return ''; |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - /** |
|
| 92 | - * @param string[] $types |
|
| 93 | - * @return string[] An array of allowed apps from which activities should be displayed |
|
| 94 | - * @since 11.0.0 |
|
| 95 | - */ |
|
| 96 | - public function filterTypes(array $types) { |
|
| 97 | - return $this->manager->filterNotificationTypes($types, $this->getIdentifier()); |
|
| 98 | - } |
|
| 91 | + /** |
|
| 92 | + * @param string[] $types |
|
| 93 | + * @return string[] An array of allowed apps from which activities should be displayed |
|
| 94 | + * @since 11.0.0 |
|
| 95 | + */ |
|
| 96 | + public function filterTypes(array $types) { |
|
| 97 | + return $this->manager->filterNotificationTypes($types, $this->getIdentifier()); |
|
| 98 | + } |
|
| 99 | 99 | |
| 100 | - /** |
|
| 101 | - * @return string[] An array of allowed apps from which activities should be displayed |
|
| 102 | - * @since 11.0.0 |
|
| 103 | - */ |
|
| 104 | - public function allowedApps() { |
|
| 105 | - return []; |
|
| 106 | - } |
|
| 100 | + /** |
|
| 101 | + * @return string[] An array of allowed apps from which activities should be displayed |
|
| 102 | + * @since 11.0.0 |
|
| 103 | + */ |
|
| 104 | + public function allowedApps() { |
|
| 105 | + return []; |
|
| 106 | + } |
|
| 107 | 107 | } |
| 108 | 108 | |
@@ -112,7 +112,7 @@ discard block |
||
| 112 | 112 | } |
| 113 | 113 | |
| 114 | 114 | $this->consumers = []; |
| 115 | - foreach($this->consumersClosures as $consumer) { |
|
| 115 | + foreach ($this->consumersClosures as $consumer) { |
|
| 116 | 116 | $c = $consumer(); |
| 117 | 117 | if ($c instanceof IConsumer) { |
| 118 | 118 | $this->consumers[] = $c; |
@@ -133,7 +133,7 @@ discard block |
||
| 133 | 133 | } |
| 134 | 134 | |
| 135 | 135 | $this->extensions = []; |
| 136 | - foreach($this->extensionsClosures as $extension) { |
|
| 136 | + foreach ($this->extensionsClosures as $extension) { |
|
| 137 | 137 | $e = $extension(); |
| 138 | 138 | if ($e instanceof IExtension) { |
| 139 | 139 | $this->extensions[] = $e; |
@@ -669,7 +669,7 @@ discard block |
||
| 669 | 669 | return array(null, null); |
| 670 | 670 | } |
| 671 | 671 | |
| 672 | - return array(' and ((' . implode(') or (', $conditions) . '))', $parameters); |
|
| 672 | + return array(' and (('.implode(') or (', $conditions).'))', $parameters); |
|
| 673 | 673 | } |
| 674 | 674 | |
| 675 | 675 | /** |
@@ -39,688 +39,688 @@ |
||
| 39 | 39 | use OCP\RichObjectStrings\IValidator; |
| 40 | 40 | |
| 41 | 41 | class Manager implements IManager { |
| 42 | - /** @var IRequest */ |
|
| 43 | - protected $request; |
|
| 44 | - |
|
| 45 | - /** @var IUserSession */ |
|
| 46 | - protected $session; |
|
| 47 | - |
|
| 48 | - /** @var IConfig */ |
|
| 49 | - protected $config; |
|
| 50 | - |
|
| 51 | - /** @var IValidator */ |
|
| 52 | - protected $validator; |
|
| 53 | - |
|
| 54 | - /** @var string */ |
|
| 55 | - protected $formattingObjectType; |
|
| 56 | - |
|
| 57 | - /** @var int */ |
|
| 58 | - protected $formattingObjectId; |
|
| 59 | - |
|
| 60 | - /** @var bool */ |
|
| 61 | - protected $requirePNG; |
|
| 62 | - |
|
| 63 | - /** @var string */ |
|
| 64 | - protected $currentUserId; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * constructor of the controller |
|
| 68 | - * |
|
| 69 | - * @param IRequest $request |
|
| 70 | - * @param IUserSession $session |
|
| 71 | - * @param IConfig $config |
|
| 72 | - * @param IValidator $validator |
|
| 73 | - */ |
|
| 74 | - public function __construct(IRequest $request, |
|
| 75 | - IUserSession $session, |
|
| 76 | - IConfig $config, |
|
| 77 | - IValidator $validator) { |
|
| 78 | - $this->request = $request; |
|
| 79 | - $this->session = $session; |
|
| 80 | - $this->config = $config; |
|
| 81 | - $this->validator = $validator; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** @var \Closure[] */ |
|
| 85 | - private $consumersClosures = array(); |
|
| 86 | - |
|
| 87 | - /** @var IConsumer[] */ |
|
| 88 | - private $consumers = array(); |
|
| 89 | - |
|
| 90 | - /** @var \Closure[] */ |
|
| 91 | - private $extensionsClosures = array(); |
|
| 92 | - |
|
| 93 | - /** @var IExtension[] */ |
|
| 94 | - private $extensions = array(); |
|
| 95 | - |
|
| 96 | - /** @var array list of filters "name" => "is valid" */ |
|
| 97 | - protected $validFilters = array( |
|
| 98 | - 'all' => true, |
|
| 99 | - 'by' => true, |
|
| 100 | - 'self' => true, |
|
| 101 | - ); |
|
| 102 | - |
|
| 103 | - /** @var array list of type icons "type" => "css class" */ |
|
| 104 | - protected $typeIcons = array(); |
|
| 105 | - |
|
| 106 | - /** @var array list of special parameters "app" => ["text" => ["parameter" => "type"]] */ |
|
| 107 | - protected $specialParameters = array(); |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * @return \OCP\Activity\IConsumer[] |
|
| 111 | - */ |
|
| 112 | - protected function getConsumers() { |
|
| 113 | - if (!empty($this->consumers)) { |
|
| 114 | - return $this->consumers; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - $this->consumers = []; |
|
| 118 | - foreach($this->consumersClosures as $consumer) { |
|
| 119 | - $c = $consumer(); |
|
| 120 | - if ($c instanceof IConsumer) { |
|
| 121 | - $this->consumers[] = $c; |
|
| 122 | - } else { |
|
| 123 | - throw new \InvalidArgumentException('The given consumer does not implement the \OCP\Activity\IConsumer interface'); |
|
| 124 | - } |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - return $this->consumers; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * @return \OCP\Activity\IExtension[] |
|
| 132 | - */ |
|
| 133 | - protected function getExtensions() { |
|
| 134 | - if (!empty($this->extensions)) { |
|
| 135 | - return $this->extensions; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - $this->extensions = []; |
|
| 139 | - foreach($this->extensionsClosures as $extension) { |
|
| 140 | - $e = $extension(); |
|
| 141 | - if ($e instanceof IExtension) { |
|
| 142 | - $this->extensions[] = $e; |
|
| 143 | - } else { |
|
| 144 | - throw new \InvalidArgumentException('The given extension does not implement the \OCP\Activity\IExtension interface'); |
|
| 145 | - } |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - return $this->extensions; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * Generates a new IEvent object |
|
| 153 | - * |
|
| 154 | - * Make sure to call at least the following methods before sending it to the |
|
| 155 | - * app with via the publish() method: |
|
| 156 | - * - setApp() |
|
| 157 | - * - setType() |
|
| 158 | - * - setAffectedUser() |
|
| 159 | - * - setSubject() |
|
| 160 | - * |
|
| 161 | - * @return IEvent |
|
| 162 | - */ |
|
| 163 | - public function generateEvent() { |
|
| 164 | - return new Event($this->validator); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * Publish an event to the activity consumers |
|
| 169 | - * |
|
| 170 | - * Make sure to call at least the following methods before sending an Event: |
|
| 171 | - * - setApp() |
|
| 172 | - * - setType() |
|
| 173 | - * - setAffectedUser() |
|
| 174 | - * - setSubject() |
|
| 175 | - * |
|
| 176 | - * @param IEvent $event |
|
| 177 | - * @throws \BadMethodCallException if required values have not been set |
|
| 178 | - */ |
|
| 179 | - public function publish(IEvent $event) { |
|
| 180 | - if ($event->getAuthor() === '') { |
|
| 181 | - if ($this->session->getUser() instanceof IUser) { |
|
| 182 | - $event->setAuthor($this->session->getUser()->getUID()); |
|
| 183 | - } |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - if (!$event->getTimestamp()) { |
|
| 187 | - $event->setTimestamp(time()); |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - if (!$event->isValid()) { |
|
| 191 | - throw new \BadMethodCallException('The given event is invalid'); |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - foreach ($this->getConsumers() as $c) { |
|
| 195 | - $c->receive($event); |
|
| 196 | - } |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * @param string $app The app where this event is associated with |
|
| 201 | - * @param string $subject A short description of the event |
|
| 202 | - * @param array $subjectParams Array with parameters that are filled in the subject |
|
| 203 | - * @param string $message A longer description of the event |
|
| 204 | - * @param array $messageParams Array with parameters that are filled in the message |
|
| 205 | - * @param string $file The file including path where this event is associated with |
|
| 206 | - * @param string $link A link where this event is associated with |
|
| 207 | - * @param string $affectedUser Recipient of the activity |
|
| 208 | - * @param string $type Type of the notification |
|
| 209 | - * @param int $priority Priority of the notification |
|
| 210 | - */ |
|
| 211 | - public function publishActivity($app, $subject, $subjectParams, $message, $messageParams, $file, $link, $affectedUser, $type, $priority) { |
|
| 212 | - $event = $this->generateEvent(); |
|
| 213 | - $event->setApp($app) |
|
| 214 | - ->setType($type) |
|
| 215 | - ->setAffectedUser($affectedUser) |
|
| 216 | - ->setSubject($subject, $subjectParams) |
|
| 217 | - ->setMessage($message, $messageParams) |
|
| 218 | - ->setObject('', 0, $file) |
|
| 219 | - ->setLink($link); |
|
| 220 | - |
|
| 221 | - $this->publish($event); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * In order to improve lazy loading a closure can be registered which will be called in case |
|
| 226 | - * activity consumers are actually requested |
|
| 227 | - * |
|
| 228 | - * $callable has to return an instance of OCA\Activity\IConsumer |
|
| 229 | - * |
|
| 230 | - * @param \Closure $callable |
|
| 231 | - */ |
|
| 232 | - public function registerConsumer(\Closure $callable) { |
|
| 233 | - array_push($this->consumersClosures, $callable); |
|
| 234 | - $this->consumers = []; |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - /** |
|
| 238 | - * In order to improve lazy loading a closure can be registered which will be called in case |
|
| 239 | - * activity consumers are actually requested |
|
| 240 | - * |
|
| 241 | - * $callable has to return an instance of OCA\Activity\IExtension |
|
| 242 | - * |
|
| 243 | - * @param \Closure $callable |
|
| 244 | - */ |
|
| 245 | - public function registerExtension(\Closure $callable) { |
|
| 246 | - array_push($this->extensionsClosures, $callable); |
|
| 247 | - $this->extensions = []; |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - /** @var string[] */ |
|
| 251 | - protected $filterClasses = []; |
|
| 252 | - |
|
| 253 | - /** @var IFilter[] */ |
|
| 254 | - protected $filters = []; |
|
| 255 | - |
|
| 256 | - /** @var bool */ |
|
| 257 | - protected $loadedLegacyFilters = false; |
|
| 258 | - |
|
| 259 | - /** |
|
| 260 | - * @param string $filter Class must implement OCA\Activity\IFilter |
|
| 261 | - * @return void |
|
| 262 | - */ |
|
| 263 | - public function registerFilter($filter) { |
|
| 264 | - $this->filterClasses[$filter] = false; |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - /** |
|
| 268 | - * @return IFilter[] |
|
| 269 | - * @throws \InvalidArgumentException |
|
| 270 | - */ |
|
| 271 | - public function getFilters() { |
|
| 272 | - if (!$this->loadedLegacyFilters) { |
|
| 273 | - $legacyFilters = $this->getNavigation(); |
|
| 274 | - |
|
| 275 | - foreach ($legacyFilters['top'] as $filter => $data) { |
|
| 276 | - $this->filters[$filter] = new LegacyFilter( |
|
| 277 | - $this, $filter, $data['name'], true |
|
| 278 | - ); |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - foreach ($legacyFilters['apps'] as $filter => $data) { |
|
| 282 | - $this->filters[$filter] = new LegacyFilter( |
|
| 283 | - $this, $filter, $data['name'], false |
|
| 284 | - ); |
|
| 285 | - } |
|
| 286 | - $this->loadedLegacyFilters = true; |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - foreach ($this->filterClasses as $class => $false) { |
|
| 290 | - /** @var IFilter $filter */ |
|
| 291 | - $filter = \OC::$server->query($class); |
|
| 292 | - |
|
| 293 | - if (!$filter instanceof IFilter) { |
|
| 294 | - throw new \InvalidArgumentException('Invalid activity filter registered'); |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - $this->filters[$filter->getIdentifier()] = $filter; |
|
| 298 | - |
|
| 299 | - unset($this->filterClasses[$class]); |
|
| 300 | - } |
|
| 301 | - return $this->filters; |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - /** |
|
| 305 | - * @param string $id |
|
| 306 | - * @return IFilter |
|
| 307 | - * @throws \InvalidArgumentException when the filter was not found |
|
| 308 | - * @since 11.0.0 |
|
| 309 | - */ |
|
| 310 | - public function getFilterById($id) { |
|
| 311 | - $filters = $this->getFilters(); |
|
| 312 | - |
|
| 313 | - if (isset($filters[$id])) { |
|
| 314 | - return $filters[$id]; |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - throw new \InvalidArgumentException('Requested filter does not exist'); |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - /** @var string[] */ |
|
| 321 | - protected $providerClasses = []; |
|
| 322 | - |
|
| 323 | - /** @var IProvider[] */ |
|
| 324 | - protected $providers = []; |
|
| 325 | - |
|
| 326 | - /** |
|
| 327 | - * @param string $provider Class must implement OCA\Activity\IProvider |
|
| 328 | - * @return void |
|
| 329 | - */ |
|
| 330 | - public function registerProvider($provider) { |
|
| 331 | - $this->providerClasses[$provider] = false; |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - /** |
|
| 335 | - * @return IProvider[] |
|
| 336 | - * @throws \InvalidArgumentException |
|
| 337 | - */ |
|
| 338 | - public function getProviders() { |
|
| 339 | - foreach ($this->providerClasses as $class => $false) { |
|
| 340 | - /** @var IProvider $provider */ |
|
| 341 | - $provider = \OC::$server->query($class); |
|
| 342 | - |
|
| 343 | - if (!$provider instanceof IProvider) { |
|
| 344 | - throw new \InvalidArgumentException('Invalid activity provider registered'); |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - $this->providers[] = $provider; |
|
| 348 | - |
|
| 349 | - unset($this->providerClasses[$class]); |
|
| 350 | - } |
|
| 351 | - return $this->providers; |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - /** @var string[] */ |
|
| 355 | - protected $settingsClasses = []; |
|
| 356 | - |
|
| 357 | - /** @var ISetting[] */ |
|
| 358 | - protected $settings = []; |
|
| 359 | - |
|
| 360 | - /** @var bool */ |
|
| 361 | - protected $loadedLegacyTypes = false; |
|
| 362 | - |
|
| 363 | - /** |
|
| 364 | - * @param string $setting Class must implement OCA\Activity\ISetting |
|
| 365 | - * @return void |
|
| 366 | - */ |
|
| 367 | - public function registerSetting($setting) { |
|
| 368 | - $this->settingsClasses[$setting] = false; |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - /** |
|
| 372 | - * @return ISetting[] |
|
| 373 | - * @throws \InvalidArgumentException |
|
| 374 | - */ |
|
| 375 | - public function getSettings() { |
|
| 376 | - if (!$this->loadedLegacyTypes) { |
|
| 377 | - $l = \OC::$server->getL10N('core'); |
|
| 378 | - $legacyTypes = $this->getNotificationTypes($l->getLanguageCode()); |
|
| 379 | - $streamTypes = $this->getDefaultTypes(IExtension::METHOD_STREAM); |
|
| 380 | - $mailTypes = $this->getDefaultTypes(IExtension::METHOD_MAIL); |
|
| 381 | - foreach ($legacyTypes as $type => $data) { |
|
| 382 | - if (is_string($data)) { |
|
| 383 | - $desc = $data; |
|
| 384 | - $canChangeStream = true; |
|
| 385 | - $canChangeMail = true; |
|
| 386 | - } else { |
|
| 387 | - $desc = $data['desc']; |
|
| 388 | - $canChangeStream = in_array(IExtension::METHOD_STREAM, $data['methods']); |
|
| 389 | - $canChangeMail = in_array(IExtension::METHOD_MAIL, $data['methods']); |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - $this->settings[$type] = new LegacySetting( |
|
| 393 | - $type, $desc, |
|
| 394 | - $canChangeStream, in_array($type, $streamTypes), |
|
| 395 | - $canChangeMail, in_array($type, $mailTypes) |
|
| 396 | - ); |
|
| 397 | - } |
|
| 398 | - $this->loadedLegacyTypes = true; |
|
| 399 | - } |
|
| 400 | - |
|
| 401 | - foreach ($this->settingsClasses as $class => $false) { |
|
| 402 | - /** @var ISetting $setting */ |
|
| 403 | - $setting = \OC::$server->query($class); |
|
| 404 | - |
|
| 405 | - if (!$setting instanceof ISetting) { |
|
| 406 | - throw new \InvalidArgumentException('Invalid activity filter registered'); |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - $this->settings[$setting->getIdentifier()] = $setting; |
|
| 410 | - |
|
| 411 | - unset($this->settingsClasses[$class]); |
|
| 412 | - } |
|
| 413 | - return $this->settings; |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - /** |
|
| 417 | - * @param string $id |
|
| 418 | - * @return ISetting |
|
| 419 | - * @throws \InvalidArgumentException when the setting was not found |
|
| 420 | - * @since 11.0.0 |
|
| 421 | - */ |
|
| 422 | - public function getSettingById($id) { |
|
| 423 | - $settings = $this->getSettings(); |
|
| 424 | - |
|
| 425 | - if (isset($settings[$id])) { |
|
| 426 | - return $settings[$id]; |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - throw new \InvalidArgumentException('Requested setting does not exist'); |
|
| 430 | - } |
|
| 431 | - |
|
| 432 | - /** |
|
| 433 | - * @param string $type |
|
| 434 | - * @return string |
|
| 435 | - */ |
|
| 436 | - public function getTypeIcon($type) { |
|
| 437 | - if (isset($this->typeIcons[$type])) { |
|
| 438 | - return $this->typeIcons[$type]; |
|
| 439 | - } |
|
| 440 | - |
|
| 441 | - foreach ($this->getExtensions() as $c) { |
|
| 442 | - $icon = $c->getTypeIcon($type); |
|
| 443 | - if (is_string($icon)) { |
|
| 444 | - $this->typeIcons[$type] = $icon; |
|
| 445 | - return $icon; |
|
| 446 | - } |
|
| 447 | - } |
|
| 448 | - |
|
| 449 | - $this->typeIcons[$type] = ''; |
|
| 450 | - return ''; |
|
| 451 | - } |
|
| 452 | - |
|
| 453 | - /** |
|
| 454 | - * @param string $type |
|
| 455 | - * @param string $id |
|
| 456 | - */ |
|
| 457 | - public function setFormattingObject($type, $id) { |
|
| 458 | - $this->formattingObjectType = $type; |
|
| 459 | - $this->formattingObjectId = (string) $id; |
|
| 460 | - } |
|
| 461 | - |
|
| 462 | - /** |
|
| 463 | - * @return bool |
|
| 464 | - */ |
|
| 465 | - public function isFormattingFilteredObject() { |
|
| 466 | - return $this->formattingObjectType !== null && $this->formattingObjectId !== null |
|
| 467 | - && $this->formattingObjectType === $this->request->getParam('object_type') |
|
| 468 | - && $this->formattingObjectId === $this->request->getParam('object_id'); |
|
| 469 | - } |
|
| 470 | - |
|
| 471 | - /** |
|
| 472 | - * @param bool $status Set to true, when parsing events should not use SVG icons |
|
| 473 | - */ |
|
| 474 | - public function setRequirePNG($status) { |
|
| 475 | - $this->requirePNG = $status; |
|
| 476 | - } |
|
| 477 | - |
|
| 478 | - /** |
|
| 479 | - * @return bool |
|
| 480 | - */ |
|
| 481 | - public function getRequirePNG() { |
|
| 482 | - return $this->requirePNG; |
|
| 483 | - } |
|
| 484 | - |
|
| 485 | - /** |
|
| 486 | - * @param string $app |
|
| 487 | - * @param string $text |
|
| 488 | - * @param array $params |
|
| 489 | - * @param boolean $stripPath |
|
| 490 | - * @param boolean $highlightParams |
|
| 491 | - * @param string $languageCode |
|
| 492 | - * @return string|false |
|
| 493 | - */ |
|
| 494 | - public function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode) { |
|
| 495 | - foreach ($this->getExtensions() as $c) { |
|
| 496 | - $translation = $c->translate($app, $text, $params, $stripPath, $highlightParams, $languageCode); |
|
| 497 | - if (is_string($translation)) { |
|
| 498 | - return $translation; |
|
| 499 | - } |
|
| 500 | - } |
|
| 501 | - |
|
| 502 | - return false; |
|
| 503 | - } |
|
| 504 | - |
|
| 505 | - /** |
|
| 506 | - * @param string $app |
|
| 507 | - * @param string $text |
|
| 508 | - * @return array|false |
|
| 509 | - */ |
|
| 510 | - public function getSpecialParameterList($app, $text) { |
|
| 511 | - if (isset($this->specialParameters[$app][$text])) { |
|
| 512 | - return $this->specialParameters[$app][$text]; |
|
| 513 | - } |
|
| 514 | - |
|
| 515 | - if (!isset($this->specialParameters[$app])) { |
|
| 516 | - $this->specialParameters[$app] = array(); |
|
| 517 | - } |
|
| 518 | - |
|
| 519 | - foreach ($this->getExtensions() as $c) { |
|
| 520 | - $specialParameter = $c->getSpecialParameterList($app, $text); |
|
| 521 | - if (is_array($specialParameter)) { |
|
| 522 | - $this->specialParameters[$app][$text] = $specialParameter; |
|
| 523 | - return $specialParameter; |
|
| 524 | - } |
|
| 525 | - } |
|
| 526 | - |
|
| 527 | - $this->specialParameters[$app][$text] = false; |
|
| 528 | - return false; |
|
| 529 | - } |
|
| 530 | - |
|
| 531 | - /** |
|
| 532 | - * @param array $activity |
|
| 533 | - * @return integer|false |
|
| 534 | - */ |
|
| 535 | - public function getGroupParameter($activity) { |
|
| 536 | - foreach ($this->getExtensions() as $c) { |
|
| 537 | - $parameter = $c->getGroupParameter($activity); |
|
| 538 | - if ($parameter !== false) { |
|
| 539 | - return $parameter; |
|
| 540 | - } |
|
| 541 | - } |
|
| 542 | - |
|
| 543 | - return false; |
|
| 544 | - } |
|
| 545 | - |
|
| 546 | - /** |
|
| 547 | - * Set the user we need to use |
|
| 548 | - * |
|
| 549 | - * @param string|null $currentUserId |
|
| 550 | - * @throws \UnexpectedValueException If the user is invalid |
|
| 551 | - */ |
|
| 552 | - public function setCurrentUserId($currentUserId) { |
|
| 553 | - if (!is_string($currentUserId) && $currentUserId !== null) { |
|
| 554 | - throw new \UnexpectedValueException('The given current user is invalid'); |
|
| 555 | - } |
|
| 556 | - $this->currentUserId = $currentUserId; |
|
| 557 | - } |
|
| 558 | - |
|
| 559 | - /** |
|
| 560 | - * Get the user we need to use |
|
| 561 | - * |
|
| 562 | - * Either the user is logged in, or we try to get it from the token |
|
| 563 | - * |
|
| 564 | - * @return string |
|
| 565 | - * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique |
|
| 566 | - */ |
|
| 567 | - public function getCurrentUserId() { |
|
| 568 | - if ($this->currentUserId !== null) { |
|
| 569 | - return $this->currentUserId; |
|
| 570 | - } else if (!$this->session->isLoggedIn()) { |
|
| 571 | - return $this->getUserFromToken(); |
|
| 572 | - } else { |
|
| 573 | - return $this->session->getUser()->getUID(); |
|
| 574 | - } |
|
| 575 | - } |
|
| 576 | - |
|
| 577 | - /** |
|
| 578 | - * Get the user for the token |
|
| 579 | - * |
|
| 580 | - * @return string |
|
| 581 | - * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique |
|
| 582 | - */ |
|
| 583 | - protected function getUserFromToken() { |
|
| 584 | - $token = (string) $this->request->getParam('token', ''); |
|
| 585 | - if (strlen($token) !== 30) { |
|
| 586 | - throw new \UnexpectedValueException('The token is invalid'); |
|
| 587 | - } |
|
| 588 | - |
|
| 589 | - $users = $this->config->getUsersForUserValue('activity', 'rsstoken', $token); |
|
| 590 | - |
|
| 591 | - if (count($users) !== 1) { |
|
| 592 | - // No unique user found |
|
| 593 | - throw new \UnexpectedValueException('The token is invalid'); |
|
| 594 | - } |
|
| 595 | - |
|
| 596 | - // Token found login as that user |
|
| 597 | - return array_shift($users); |
|
| 598 | - } |
|
| 599 | - |
|
| 600 | - /** |
|
| 601 | - * @return array |
|
| 602 | - * @deprecated 11.0.0 - Use getFilters() instead |
|
| 603 | - */ |
|
| 604 | - public function getNavigation() { |
|
| 605 | - $entries = array( |
|
| 606 | - 'apps' => array(), |
|
| 607 | - 'top' => array(), |
|
| 608 | - ); |
|
| 609 | - foreach ($this->getExtensions() as $c) { |
|
| 610 | - $additionalEntries = $c->getNavigation(); |
|
| 611 | - if (is_array($additionalEntries)) { |
|
| 612 | - $entries['apps'] = array_merge($entries['apps'], $additionalEntries['apps']); |
|
| 613 | - $entries['top'] = array_merge($entries['top'], $additionalEntries['top']); |
|
| 614 | - } |
|
| 615 | - } |
|
| 616 | - |
|
| 617 | - return $entries; |
|
| 618 | - } |
|
| 619 | - |
|
| 620 | - /** |
|
| 621 | - * @param string $filterValue |
|
| 622 | - * @return boolean |
|
| 623 | - * @deprecated 11.0.0 - Use getFilterById() instead |
|
| 624 | - */ |
|
| 625 | - public function isFilterValid($filterValue) { |
|
| 626 | - if (isset($this->validFilters[$filterValue])) { |
|
| 627 | - return $this->validFilters[$filterValue]; |
|
| 628 | - } |
|
| 629 | - |
|
| 630 | - foreach ($this->getExtensions() as $c) { |
|
| 631 | - if ($c->isFilterValid($filterValue) === true) { |
|
| 632 | - $this->validFilters[$filterValue] = true; |
|
| 633 | - return true; |
|
| 634 | - } |
|
| 635 | - } |
|
| 636 | - |
|
| 637 | - $this->validFilters[$filterValue] = false; |
|
| 638 | - return false; |
|
| 639 | - } |
|
| 640 | - |
|
| 641 | - /** |
|
| 642 | - * @param array $types |
|
| 643 | - * @param string $filter |
|
| 644 | - * @return array |
|
| 645 | - * @deprecated 11.0.0 - Use getFilterById()->filterTypes() instead |
|
| 646 | - */ |
|
| 647 | - public function filterNotificationTypes($types, $filter) { |
|
| 648 | - if (!$this->isFilterValid($filter)) { |
|
| 649 | - return $types; |
|
| 650 | - } |
|
| 651 | - |
|
| 652 | - foreach ($this->getExtensions() as $c) { |
|
| 653 | - $result = $c->filterNotificationTypes($types, $filter); |
|
| 654 | - if (is_array($result)) { |
|
| 655 | - $types = $result; |
|
| 656 | - } |
|
| 657 | - } |
|
| 658 | - return $types; |
|
| 659 | - } |
|
| 660 | - |
|
| 661 | - /** |
|
| 662 | - * @param string $filter |
|
| 663 | - * @return array |
|
| 664 | - * @deprecated 11.0.0 - Use getFilterById() instead |
|
| 665 | - */ |
|
| 666 | - public function getQueryForFilter($filter) { |
|
| 667 | - if (!$this->isFilterValid($filter)) { |
|
| 668 | - return [null, null]; |
|
| 669 | - } |
|
| 670 | - |
|
| 671 | - $conditions = array(); |
|
| 672 | - $parameters = array(); |
|
| 673 | - |
|
| 674 | - foreach ($this->getExtensions() as $c) { |
|
| 675 | - $result = $c->getQueryForFilter($filter); |
|
| 676 | - if (is_array($result)) { |
|
| 677 | - list($condition, $parameter) = $result; |
|
| 678 | - if ($condition && is_array($parameter)) { |
|
| 679 | - $conditions[] = $condition; |
|
| 680 | - $parameters = array_merge($parameters, $parameter); |
|
| 681 | - } |
|
| 682 | - } |
|
| 683 | - } |
|
| 684 | - |
|
| 685 | - if (empty($conditions)) { |
|
| 686 | - return array(null, null); |
|
| 687 | - } |
|
| 688 | - |
|
| 689 | - return array(' and ((' . implode(') or (', $conditions) . '))', $parameters); |
|
| 690 | - } |
|
| 691 | - |
|
| 692 | - /** |
|
| 693 | - * Will return additional notification types as specified by other apps |
|
| 694 | - * |
|
| 695 | - * @param string $languageCode |
|
| 696 | - * @return array |
|
| 697 | - * @deprecated 11.0.0 - Use getSettings() instead |
|
| 698 | - */ |
|
| 699 | - public function getNotificationTypes($languageCode) { |
|
| 700 | - $notificationTypes = $sharingNotificationTypes = []; |
|
| 701 | - foreach ($this->getExtensions() as $c) { |
|
| 702 | - $result = $c->getNotificationTypes($languageCode); |
|
| 703 | - if (is_array($result)) { |
|
| 704 | - $notificationTypes = array_merge($notificationTypes, $result); |
|
| 705 | - } |
|
| 706 | - } |
|
| 707 | - |
|
| 708 | - return array_merge($sharingNotificationTypes, $notificationTypes); |
|
| 709 | - } |
|
| 710 | - |
|
| 711 | - /** |
|
| 712 | - * @param string $method |
|
| 713 | - * @return array |
|
| 714 | - * @deprecated 11.0.0 - Use getSettings()->isDefaulEnabled<method>() instead |
|
| 715 | - */ |
|
| 716 | - public function getDefaultTypes($method) { |
|
| 717 | - $defaultTypes = array(); |
|
| 718 | - foreach ($this->getExtensions() as $c) { |
|
| 719 | - $types = $c->getDefaultTypes($method); |
|
| 720 | - if (is_array($types)) { |
|
| 721 | - $defaultTypes = array_merge($types, $defaultTypes); |
|
| 722 | - } |
|
| 723 | - } |
|
| 724 | - return $defaultTypes; |
|
| 725 | - } |
|
| 42 | + /** @var IRequest */ |
|
| 43 | + protected $request; |
|
| 44 | + |
|
| 45 | + /** @var IUserSession */ |
|
| 46 | + protected $session; |
|
| 47 | + |
|
| 48 | + /** @var IConfig */ |
|
| 49 | + protected $config; |
|
| 50 | + |
|
| 51 | + /** @var IValidator */ |
|
| 52 | + protected $validator; |
|
| 53 | + |
|
| 54 | + /** @var string */ |
|
| 55 | + protected $formattingObjectType; |
|
| 56 | + |
|
| 57 | + /** @var int */ |
|
| 58 | + protected $formattingObjectId; |
|
| 59 | + |
|
| 60 | + /** @var bool */ |
|
| 61 | + protected $requirePNG; |
|
| 62 | + |
|
| 63 | + /** @var string */ |
|
| 64 | + protected $currentUserId; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * constructor of the controller |
|
| 68 | + * |
|
| 69 | + * @param IRequest $request |
|
| 70 | + * @param IUserSession $session |
|
| 71 | + * @param IConfig $config |
|
| 72 | + * @param IValidator $validator |
|
| 73 | + */ |
|
| 74 | + public function __construct(IRequest $request, |
|
| 75 | + IUserSession $session, |
|
| 76 | + IConfig $config, |
|
| 77 | + IValidator $validator) { |
|
| 78 | + $this->request = $request; |
|
| 79 | + $this->session = $session; |
|
| 80 | + $this->config = $config; |
|
| 81 | + $this->validator = $validator; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** @var \Closure[] */ |
|
| 85 | + private $consumersClosures = array(); |
|
| 86 | + |
|
| 87 | + /** @var IConsumer[] */ |
|
| 88 | + private $consumers = array(); |
|
| 89 | + |
|
| 90 | + /** @var \Closure[] */ |
|
| 91 | + private $extensionsClosures = array(); |
|
| 92 | + |
|
| 93 | + /** @var IExtension[] */ |
|
| 94 | + private $extensions = array(); |
|
| 95 | + |
|
| 96 | + /** @var array list of filters "name" => "is valid" */ |
|
| 97 | + protected $validFilters = array( |
|
| 98 | + 'all' => true, |
|
| 99 | + 'by' => true, |
|
| 100 | + 'self' => true, |
|
| 101 | + ); |
|
| 102 | + |
|
| 103 | + /** @var array list of type icons "type" => "css class" */ |
|
| 104 | + protected $typeIcons = array(); |
|
| 105 | + |
|
| 106 | + /** @var array list of special parameters "app" => ["text" => ["parameter" => "type"]] */ |
|
| 107 | + protected $specialParameters = array(); |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * @return \OCP\Activity\IConsumer[] |
|
| 111 | + */ |
|
| 112 | + protected function getConsumers() { |
|
| 113 | + if (!empty($this->consumers)) { |
|
| 114 | + return $this->consumers; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + $this->consumers = []; |
|
| 118 | + foreach($this->consumersClosures as $consumer) { |
|
| 119 | + $c = $consumer(); |
|
| 120 | + if ($c instanceof IConsumer) { |
|
| 121 | + $this->consumers[] = $c; |
|
| 122 | + } else { |
|
| 123 | + throw new \InvalidArgumentException('The given consumer does not implement the \OCP\Activity\IConsumer interface'); |
|
| 124 | + } |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + return $this->consumers; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * @return \OCP\Activity\IExtension[] |
|
| 132 | + */ |
|
| 133 | + protected function getExtensions() { |
|
| 134 | + if (!empty($this->extensions)) { |
|
| 135 | + return $this->extensions; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + $this->extensions = []; |
|
| 139 | + foreach($this->extensionsClosures as $extension) { |
|
| 140 | + $e = $extension(); |
|
| 141 | + if ($e instanceof IExtension) { |
|
| 142 | + $this->extensions[] = $e; |
|
| 143 | + } else { |
|
| 144 | + throw new \InvalidArgumentException('The given extension does not implement the \OCP\Activity\IExtension interface'); |
|
| 145 | + } |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + return $this->extensions; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * Generates a new IEvent object |
|
| 153 | + * |
|
| 154 | + * Make sure to call at least the following methods before sending it to the |
|
| 155 | + * app with via the publish() method: |
|
| 156 | + * - setApp() |
|
| 157 | + * - setType() |
|
| 158 | + * - setAffectedUser() |
|
| 159 | + * - setSubject() |
|
| 160 | + * |
|
| 161 | + * @return IEvent |
|
| 162 | + */ |
|
| 163 | + public function generateEvent() { |
|
| 164 | + return new Event($this->validator); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * Publish an event to the activity consumers |
|
| 169 | + * |
|
| 170 | + * Make sure to call at least the following methods before sending an Event: |
|
| 171 | + * - setApp() |
|
| 172 | + * - setType() |
|
| 173 | + * - setAffectedUser() |
|
| 174 | + * - setSubject() |
|
| 175 | + * |
|
| 176 | + * @param IEvent $event |
|
| 177 | + * @throws \BadMethodCallException if required values have not been set |
|
| 178 | + */ |
|
| 179 | + public function publish(IEvent $event) { |
|
| 180 | + if ($event->getAuthor() === '') { |
|
| 181 | + if ($this->session->getUser() instanceof IUser) { |
|
| 182 | + $event->setAuthor($this->session->getUser()->getUID()); |
|
| 183 | + } |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + if (!$event->getTimestamp()) { |
|
| 187 | + $event->setTimestamp(time()); |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + if (!$event->isValid()) { |
|
| 191 | + throw new \BadMethodCallException('The given event is invalid'); |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + foreach ($this->getConsumers() as $c) { |
|
| 195 | + $c->receive($event); |
|
| 196 | + } |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * @param string $app The app where this event is associated with |
|
| 201 | + * @param string $subject A short description of the event |
|
| 202 | + * @param array $subjectParams Array with parameters that are filled in the subject |
|
| 203 | + * @param string $message A longer description of the event |
|
| 204 | + * @param array $messageParams Array with parameters that are filled in the message |
|
| 205 | + * @param string $file The file including path where this event is associated with |
|
| 206 | + * @param string $link A link where this event is associated with |
|
| 207 | + * @param string $affectedUser Recipient of the activity |
|
| 208 | + * @param string $type Type of the notification |
|
| 209 | + * @param int $priority Priority of the notification |
|
| 210 | + */ |
|
| 211 | + public function publishActivity($app, $subject, $subjectParams, $message, $messageParams, $file, $link, $affectedUser, $type, $priority) { |
|
| 212 | + $event = $this->generateEvent(); |
|
| 213 | + $event->setApp($app) |
|
| 214 | + ->setType($type) |
|
| 215 | + ->setAffectedUser($affectedUser) |
|
| 216 | + ->setSubject($subject, $subjectParams) |
|
| 217 | + ->setMessage($message, $messageParams) |
|
| 218 | + ->setObject('', 0, $file) |
|
| 219 | + ->setLink($link); |
|
| 220 | + |
|
| 221 | + $this->publish($event); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * In order to improve lazy loading a closure can be registered which will be called in case |
|
| 226 | + * activity consumers are actually requested |
|
| 227 | + * |
|
| 228 | + * $callable has to return an instance of OCA\Activity\IConsumer |
|
| 229 | + * |
|
| 230 | + * @param \Closure $callable |
|
| 231 | + */ |
|
| 232 | + public function registerConsumer(\Closure $callable) { |
|
| 233 | + array_push($this->consumersClosures, $callable); |
|
| 234 | + $this->consumers = []; |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + /** |
|
| 238 | + * In order to improve lazy loading a closure can be registered which will be called in case |
|
| 239 | + * activity consumers are actually requested |
|
| 240 | + * |
|
| 241 | + * $callable has to return an instance of OCA\Activity\IExtension |
|
| 242 | + * |
|
| 243 | + * @param \Closure $callable |
|
| 244 | + */ |
|
| 245 | + public function registerExtension(\Closure $callable) { |
|
| 246 | + array_push($this->extensionsClosures, $callable); |
|
| 247 | + $this->extensions = []; |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + /** @var string[] */ |
|
| 251 | + protected $filterClasses = []; |
|
| 252 | + |
|
| 253 | + /** @var IFilter[] */ |
|
| 254 | + protected $filters = []; |
|
| 255 | + |
|
| 256 | + /** @var bool */ |
|
| 257 | + protected $loadedLegacyFilters = false; |
|
| 258 | + |
|
| 259 | + /** |
|
| 260 | + * @param string $filter Class must implement OCA\Activity\IFilter |
|
| 261 | + * @return void |
|
| 262 | + */ |
|
| 263 | + public function registerFilter($filter) { |
|
| 264 | + $this->filterClasses[$filter] = false; |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + /** |
|
| 268 | + * @return IFilter[] |
|
| 269 | + * @throws \InvalidArgumentException |
|
| 270 | + */ |
|
| 271 | + public function getFilters() { |
|
| 272 | + if (!$this->loadedLegacyFilters) { |
|
| 273 | + $legacyFilters = $this->getNavigation(); |
|
| 274 | + |
|
| 275 | + foreach ($legacyFilters['top'] as $filter => $data) { |
|
| 276 | + $this->filters[$filter] = new LegacyFilter( |
|
| 277 | + $this, $filter, $data['name'], true |
|
| 278 | + ); |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + foreach ($legacyFilters['apps'] as $filter => $data) { |
|
| 282 | + $this->filters[$filter] = new LegacyFilter( |
|
| 283 | + $this, $filter, $data['name'], false |
|
| 284 | + ); |
|
| 285 | + } |
|
| 286 | + $this->loadedLegacyFilters = true; |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + foreach ($this->filterClasses as $class => $false) { |
|
| 290 | + /** @var IFilter $filter */ |
|
| 291 | + $filter = \OC::$server->query($class); |
|
| 292 | + |
|
| 293 | + if (!$filter instanceof IFilter) { |
|
| 294 | + throw new \InvalidArgumentException('Invalid activity filter registered'); |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + $this->filters[$filter->getIdentifier()] = $filter; |
|
| 298 | + |
|
| 299 | + unset($this->filterClasses[$class]); |
|
| 300 | + } |
|
| 301 | + return $this->filters; |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + /** |
|
| 305 | + * @param string $id |
|
| 306 | + * @return IFilter |
|
| 307 | + * @throws \InvalidArgumentException when the filter was not found |
|
| 308 | + * @since 11.0.0 |
|
| 309 | + */ |
|
| 310 | + public function getFilterById($id) { |
|
| 311 | + $filters = $this->getFilters(); |
|
| 312 | + |
|
| 313 | + if (isset($filters[$id])) { |
|
| 314 | + return $filters[$id]; |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + throw new \InvalidArgumentException('Requested filter does not exist'); |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + /** @var string[] */ |
|
| 321 | + protected $providerClasses = []; |
|
| 322 | + |
|
| 323 | + /** @var IProvider[] */ |
|
| 324 | + protected $providers = []; |
|
| 325 | + |
|
| 326 | + /** |
|
| 327 | + * @param string $provider Class must implement OCA\Activity\IProvider |
|
| 328 | + * @return void |
|
| 329 | + */ |
|
| 330 | + public function registerProvider($provider) { |
|
| 331 | + $this->providerClasses[$provider] = false; |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + /** |
|
| 335 | + * @return IProvider[] |
|
| 336 | + * @throws \InvalidArgumentException |
|
| 337 | + */ |
|
| 338 | + public function getProviders() { |
|
| 339 | + foreach ($this->providerClasses as $class => $false) { |
|
| 340 | + /** @var IProvider $provider */ |
|
| 341 | + $provider = \OC::$server->query($class); |
|
| 342 | + |
|
| 343 | + if (!$provider instanceof IProvider) { |
|
| 344 | + throw new \InvalidArgumentException('Invalid activity provider registered'); |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + $this->providers[] = $provider; |
|
| 348 | + |
|
| 349 | + unset($this->providerClasses[$class]); |
|
| 350 | + } |
|
| 351 | + return $this->providers; |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + /** @var string[] */ |
|
| 355 | + protected $settingsClasses = []; |
|
| 356 | + |
|
| 357 | + /** @var ISetting[] */ |
|
| 358 | + protected $settings = []; |
|
| 359 | + |
|
| 360 | + /** @var bool */ |
|
| 361 | + protected $loadedLegacyTypes = false; |
|
| 362 | + |
|
| 363 | + /** |
|
| 364 | + * @param string $setting Class must implement OCA\Activity\ISetting |
|
| 365 | + * @return void |
|
| 366 | + */ |
|
| 367 | + public function registerSetting($setting) { |
|
| 368 | + $this->settingsClasses[$setting] = false; |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + /** |
|
| 372 | + * @return ISetting[] |
|
| 373 | + * @throws \InvalidArgumentException |
|
| 374 | + */ |
|
| 375 | + public function getSettings() { |
|
| 376 | + if (!$this->loadedLegacyTypes) { |
|
| 377 | + $l = \OC::$server->getL10N('core'); |
|
| 378 | + $legacyTypes = $this->getNotificationTypes($l->getLanguageCode()); |
|
| 379 | + $streamTypes = $this->getDefaultTypes(IExtension::METHOD_STREAM); |
|
| 380 | + $mailTypes = $this->getDefaultTypes(IExtension::METHOD_MAIL); |
|
| 381 | + foreach ($legacyTypes as $type => $data) { |
|
| 382 | + if (is_string($data)) { |
|
| 383 | + $desc = $data; |
|
| 384 | + $canChangeStream = true; |
|
| 385 | + $canChangeMail = true; |
|
| 386 | + } else { |
|
| 387 | + $desc = $data['desc']; |
|
| 388 | + $canChangeStream = in_array(IExtension::METHOD_STREAM, $data['methods']); |
|
| 389 | + $canChangeMail = in_array(IExtension::METHOD_MAIL, $data['methods']); |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + $this->settings[$type] = new LegacySetting( |
|
| 393 | + $type, $desc, |
|
| 394 | + $canChangeStream, in_array($type, $streamTypes), |
|
| 395 | + $canChangeMail, in_array($type, $mailTypes) |
|
| 396 | + ); |
|
| 397 | + } |
|
| 398 | + $this->loadedLegacyTypes = true; |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + foreach ($this->settingsClasses as $class => $false) { |
|
| 402 | + /** @var ISetting $setting */ |
|
| 403 | + $setting = \OC::$server->query($class); |
|
| 404 | + |
|
| 405 | + if (!$setting instanceof ISetting) { |
|
| 406 | + throw new \InvalidArgumentException('Invalid activity filter registered'); |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + $this->settings[$setting->getIdentifier()] = $setting; |
|
| 410 | + |
|
| 411 | + unset($this->settingsClasses[$class]); |
|
| 412 | + } |
|
| 413 | + return $this->settings; |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + /** |
|
| 417 | + * @param string $id |
|
| 418 | + * @return ISetting |
|
| 419 | + * @throws \InvalidArgumentException when the setting was not found |
|
| 420 | + * @since 11.0.0 |
|
| 421 | + */ |
|
| 422 | + public function getSettingById($id) { |
|
| 423 | + $settings = $this->getSettings(); |
|
| 424 | + |
|
| 425 | + if (isset($settings[$id])) { |
|
| 426 | + return $settings[$id]; |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + throw new \InvalidArgumentException('Requested setting does not exist'); |
|
| 430 | + } |
|
| 431 | + |
|
| 432 | + /** |
|
| 433 | + * @param string $type |
|
| 434 | + * @return string |
|
| 435 | + */ |
|
| 436 | + public function getTypeIcon($type) { |
|
| 437 | + if (isset($this->typeIcons[$type])) { |
|
| 438 | + return $this->typeIcons[$type]; |
|
| 439 | + } |
|
| 440 | + |
|
| 441 | + foreach ($this->getExtensions() as $c) { |
|
| 442 | + $icon = $c->getTypeIcon($type); |
|
| 443 | + if (is_string($icon)) { |
|
| 444 | + $this->typeIcons[$type] = $icon; |
|
| 445 | + return $icon; |
|
| 446 | + } |
|
| 447 | + } |
|
| 448 | + |
|
| 449 | + $this->typeIcons[$type] = ''; |
|
| 450 | + return ''; |
|
| 451 | + } |
|
| 452 | + |
|
| 453 | + /** |
|
| 454 | + * @param string $type |
|
| 455 | + * @param string $id |
|
| 456 | + */ |
|
| 457 | + public function setFormattingObject($type, $id) { |
|
| 458 | + $this->formattingObjectType = $type; |
|
| 459 | + $this->formattingObjectId = (string) $id; |
|
| 460 | + } |
|
| 461 | + |
|
| 462 | + /** |
|
| 463 | + * @return bool |
|
| 464 | + */ |
|
| 465 | + public function isFormattingFilteredObject() { |
|
| 466 | + return $this->formattingObjectType !== null && $this->formattingObjectId !== null |
|
| 467 | + && $this->formattingObjectType === $this->request->getParam('object_type') |
|
| 468 | + && $this->formattingObjectId === $this->request->getParam('object_id'); |
|
| 469 | + } |
|
| 470 | + |
|
| 471 | + /** |
|
| 472 | + * @param bool $status Set to true, when parsing events should not use SVG icons |
|
| 473 | + */ |
|
| 474 | + public function setRequirePNG($status) { |
|
| 475 | + $this->requirePNG = $status; |
|
| 476 | + } |
|
| 477 | + |
|
| 478 | + /** |
|
| 479 | + * @return bool |
|
| 480 | + */ |
|
| 481 | + public function getRequirePNG() { |
|
| 482 | + return $this->requirePNG; |
|
| 483 | + } |
|
| 484 | + |
|
| 485 | + /** |
|
| 486 | + * @param string $app |
|
| 487 | + * @param string $text |
|
| 488 | + * @param array $params |
|
| 489 | + * @param boolean $stripPath |
|
| 490 | + * @param boolean $highlightParams |
|
| 491 | + * @param string $languageCode |
|
| 492 | + * @return string|false |
|
| 493 | + */ |
|
| 494 | + public function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode) { |
|
| 495 | + foreach ($this->getExtensions() as $c) { |
|
| 496 | + $translation = $c->translate($app, $text, $params, $stripPath, $highlightParams, $languageCode); |
|
| 497 | + if (is_string($translation)) { |
|
| 498 | + return $translation; |
|
| 499 | + } |
|
| 500 | + } |
|
| 501 | + |
|
| 502 | + return false; |
|
| 503 | + } |
|
| 504 | + |
|
| 505 | + /** |
|
| 506 | + * @param string $app |
|
| 507 | + * @param string $text |
|
| 508 | + * @return array|false |
|
| 509 | + */ |
|
| 510 | + public function getSpecialParameterList($app, $text) { |
|
| 511 | + if (isset($this->specialParameters[$app][$text])) { |
|
| 512 | + return $this->specialParameters[$app][$text]; |
|
| 513 | + } |
|
| 514 | + |
|
| 515 | + if (!isset($this->specialParameters[$app])) { |
|
| 516 | + $this->specialParameters[$app] = array(); |
|
| 517 | + } |
|
| 518 | + |
|
| 519 | + foreach ($this->getExtensions() as $c) { |
|
| 520 | + $specialParameter = $c->getSpecialParameterList($app, $text); |
|
| 521 | + if (is_array($specialParameter)) { |
|
| 522 | + $this->specialParameters[$app][$text] = $specialParameter; |
|
| 523 | + return $specialParameter; |
|
| 524 | + } |
|
| 525 | + } |
|
| 526 | + |
|
| 527 | + $this->specialParameters[$app][$text] = false; |
|
| 528 | + return false; |
|
| 529 | + } |
|
| 530 | + |
|
| 531 | + /** |
|
| 532 | + * @param array $activity |
|
| 533 | + * @return integer|false |
|
| 534 | + */ |
|
| 535 | + public function getGroupParameter($activity) { |
|
| 536 | + foreach ($this->getExtensions() as $c) { |
|
| 537 | + $parameter = $c->getGroupParameter($activity); |
|
| 538 | + if ($parameter !== false) { |
|
| 539 | + return $parameter; |
|
| 540 | + } |
|
| 541 | + } |
|
| 542 | + |
|
| 543 | + return false; |
|
| 544 | + } |
|
| 545 | + |
|
| 546 | + /** |
|
| 547 | + * Set the user we need to use |
|
| 548 | + * |
|
| 549 | + * @param string|null $currentUserId |
|
| 550 | + * @throws \UnexpectedValueException If the user is invalid |
|
| 551 | + */ |
|
| 552 | + public function setCurrentUserId($currentUserId) { |
|
| 553 | + if (!is_string($currentUserId) && $currentUserId !== null) { |
|
| 554 | + throw new \UnexpectedValueException('The given current user is invalid'); |
|
| 555 | + } |
|
| 556 | + $this->currentUserId = $currentUserId; |
|
| 557 | + } |
|
| 558 | + |
|
| 559 | + /** |
|
| 560 | + * Get the user we need to use |
|
| 561 | + * |
|
| 562 | + * Either the user is logged in, or we try to get it from the token |
|
| 563 | + * |
|
| 564 | + * @return string |
|
| 565 | + * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique |
|
| 566 | + */ |
|
| 567 | + public function getCurrentUserId() { |
|
| 568 | + if ($this->currentUserId !== null) { |
|
| 569 | + return $this->currentUserId; |
|
| 570 | + } else if (!$this->session->isLoggedIn()) { |
|
| 571 | + return $this->getUserFromToken(); |
|
| 572 | + } else { |
|
| 573 | + return $this->session->getUser()->getUID(); |
|
| 574 | + } |
|
| 575 | + } |
|
| 576 | + |
|
| 577 | + /** |
|
| 578 | + * Get the user for the token |
|
| 579 | + * |
|
| 580 | + * @return string |
|
| 581 | + * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique |
|
| 582 | + */ |
|
| 583 | + protected function getUserFromToken() { |
|
| 584 | + $token = (string) $this->request->getParam('token', ''); |
|
| 585 | + if (strlen($token) !== 30) { |
|
| 586 | + throw new \UnexpectedValueException('The token is invalid'); |
|
| 587 | + } |
|
| 588 | + |
|
| 589 | + $users = $this->config->getUsersForUserValue('activity', 'rsstoken', $token); |
|
| 590 | + |
|
| 591 | + if (count($users) !== 1) { |
|
| 592 | + // No unique user found |
|
| 593 | + throw new \UnexpectedValueException('The token is invalid'); |
|
| 594 | + } |
|
| 595 | + |
|
| 596 | + // Token found login as that user |
|
| 597 | + return array_shift($users); |
|
| 598 | + } |
|
| 599 | + |
|
| 600 | + /** |
|
| 601 | + * @return array |
|
| 602 | + * @deprecated 11.0.0 - Use getFilters() instead |
|
| 603 | + */ |
|
| 604 | + public function getNavigation() { |
|
| 605 | + $entries = array( |
|
| 606 | + 'apps' => array(), |
|
| 607 | + 'top' => array(), |
|
| 608 | + ); |
|
| 609 | + foreach ($this->getExtensions() as $c) { |
|
| 610 | + $additionalEntries = $c->getNavigation(); |
|
| 611 | + if (is_array($additionalEntries)) { |
|
| 612 | + $entries['apps'] = array_merge($entries['apps'], $additionalEntries['apps']); |
|
| 613 | + $entries['top'] = array_merge($entries['top'], $additionalEntries['top']); |
|
| 614 | + } |
|
| 615 | + } |
|
| 616 | + |
|
| 617 | + return $entries; |
|
| 618 | + } |
|
| 619 | + |
|
| 620 | + /** |
|
| 621 | + * @param string $filterValue |
|
| 622 | + * @return boolean |
|
| 623 | + * @deprecated 11.0.0 - Use getFilterById() instead |
|
| 624 | + */ |
|
| 625 | + public function isFilterValid($filterValue) { |
|
| 626 | + if (isset($this->validFilters[$filterValue])) { |
|
| 627 | + return $this->validFilters[$filterValue]; |
|
| 628 | + } |
|
| 629 | + |
|
| 630 | + foreach ($this->getExtensions() as $c) { |
|
| 631 | + if ($c->isFilterValid($filterValue) === true) { |
|
| 632 | + $this->validFilters[$filterValue] = true; |
|
| 633 | + return true; |
|
| 634 | + } |
|
| 635 | + } |
|
| 636 | + |
|
| 637 | + $this->validFilters[$filterValue] = false; |
|
| 638 | + return false; |
|
| 639 | + } |
|
| 640 | + |
|
| 641 | + /** |
|
| 642 | + * @param array $types |
|
| 643 | + * @param string $filter |
|
| 644 | + * @return array |
|
| 645 | + * @deprecated 11.0.0 - Use getFilterById()->filterTypes() instead |
|
| 646 | + */ |
|
| 647 | + public function filterNotificationTypes($types, $filter) { |
|
| 648 | + if (!$this->isFilterValid($filter)) { |
|
| 649 | + return $types; |
|
| 650 | + } |
|
| 651 | + |
|
| 652 | + foreach ($this->getExtensions() as $c) { |
|
| 653 | + $result = $c->filterNotificationTypes($types, $filter); |
|
| 654 | + if (is_array($result)) { |
|
| 655 | + $types = $result; |
|
| 656 | + } |
|
| 657 | + } |
|
| 658 | + return $types; |
|
| 659 | + } |
|
| 660 | + |
|
| 661 | + /** |
|
| 662 | + * @param string $filter |
|
| 663 | + * @return array |
|
| 664 | + * @deprecated 11.0.0 - Use getFilterById() instead |
|
| 665 | + */ |
|
| 666 | + public function getQueryForFilter($filter) { |
|
| 667 | + if (!$this->isFilterValid($filter)) { |
|
| 668 | + return [null, null]; |
|
| 669 | + } |
|
| 670 | + |
|
| 671 | + $conditions = array(); |
|
| 672 | + $parameters = array(); |
|
| 673 | + |
|
| 674 | + foreach ($this->getExtensions() as $c) { |
|
| 675 | + $result = $c->getQueryForFilter($filter); |
|
| 676 | + if (is_array($result)) { |
|
| 677 | + list($condition, $parameter) = $result; |
|
| 678 | + if ($condition && is_array($parameter)) { |
|
| 679 | + $conditions[] = $condition; |
|
| 680 | + $parameters = array_merge($parameters, $parameter); |
|
| 681 | + } |
|
| 682 | + } |
|
| 683 | + } |
|
| 684 | + |
|
| 685 | + if (empty($conditions)) { |
|
| 686 | + return array(null, null); |
|
| 687 | + } |
|
| 688 | + |
|
| 689 | + return array(' and ((' . implode(') or (', $conditions) . '))', $parameters); |
|
| 690 | + } |
|
| 691 | + |
|
| 692 | + /** |
|
| 693 | + * Will return additional notification types as specified by other apps |
|
| 694 | + * |
|
| 695 | + * @param string $languageCode |
|
| 696 | + * @return array |
|
| 697 | + * @deprecated 11.0.0 - Use getSettings() instead |
|
| 698 | + */ |
|
| 699 | + public function getNotificationTypes($languageCode) { |
|
| 700 | + $notificationTypes = $sharingNotificationTypes = []; |
|
| 701 | + foreach ($this->getExtensions() as $c) { |
|
| 702 | + $result = $c->getNotificationTypes($languageCode); |
|
| 703 | + if (is_array($result)) { |
|
| 704 | + $notificationTypes = array_merge($notificationTypes, $result); |
|
| 705 | + } |
|
| 706 | + } |
|
| 707 | + |
|
| 708 | + return array_merge($sharingNotificationTypes, $notificationTypes); |
|
| 709 | + } |
|
| 710 | + |
|
| 711 | + /** |
|
| 712 | + * @param string $method |
|
| 713 | + * @return array |
|
| 714 | + * @deprecated 11.0.0 - Use getSettings()->isDefaulEnabled<method>() instead |
|
| 715 | + */ |
|
| 716 | + public function getDefaultTypes($method) { |
|
| 717 | + $defaultTypes = array(); |
|
| 718 | + foreach ($this->getExtensions() as $c) { |
|
| 719 | + $types = $c->getDefaultTypes($method); |
|
| 720 | + if (is_array($types)) { |
|
| 721 | + $defaultTypes = array_merge($types, $defaultTypes); |
|
| 722 | + } |
|
| 723 | + } |
|
| 724 | + return $defaultTypes; |
|
| 725 | + } |
|
| 726 | 726 | } |
@@ -25,99 +25,99 @@ |
||
| 25 | 25 | |
| 26 | 26 | class LegacySetting implements ISetting { |
| 27 | 27 | |
| 28 | - /** @var string */ |
|
| 29 | - protected $identifier; |
|
| 30 | - /** @var string */ |
|
| 31 | - protected $name; |
|
| 32 | - /** @var bool */ |
|
| 33 | - protected $canChangeStream; |
|
| 34 | - /** @var bool */ |
|
| 35 | - protected $isDefaultEnabledStream; |
|
| 36 | - /** @var bool */ |
|
| 37 | - protected $canChangeMail; |
|
| 38 | - /** @var bool */ |
|
| 39 | - protected $isDefaultEnabledMail; |
|
| 28 | + /** @var string */ |
|
| 29 | + protected $identifier; |
|
| 30 | + /** @var string */ |
|
| 31 | + protected $name; |
|
| 32 | + /** @var bool */ |
|
| 33 | + protected $canChangeStream; |
|
| 34 | + /** @var bool */ |
|
| 35 | + protected $isDefaultEnabledStream; |
|
| 36 | + /** @var bool */ |
|
| 37 | + protected $canChangeMail; |
|
| 38 | + /** @var bool */ |
|
| 39 | + protected $isDefaultEnabledMail; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * LegacySetting constructor. |
|
| 43 | - * |
|
| 44 | - * @param string $identifier |
|
| 45 | - * @param string $name |
|
| 46 | - * @param bool $canChangeStream |
|
| 47 | - * @param bool $isDefaultEnabledStream |
|
| 48 | - * @param bool $canChangeMail |
|
| 49 | - * @param bool $isDefaultEnabledMail |
|
| 50 | - */ |
|
| 51 | - public function __construct($identifier, |
|
| 52 | - $name, |
|
| 53 | - $canChangeStream, |
|
| 54 | - $isDefaultEnabledStream, |
|
| 55 | - $canChangeMail, |
|
| 56 | - $isDefaultEnabledMail) { |
|
| 57 | - $this->identifier = $identifier; |
|
| 58 | - $this->name = $name; |
|
| 59 | - $this->canChangeStream = $canChangeStream; |
|
| 60 | - $this->isDefaultEnabledStream = $isDefaultEnabledStream; |
|
| 61 | - $this->canChangeMail = $canChangeMail; |
|
| 62 | - $this->isDefaultEnabledMail = $isDefaultEnabledMail; |
|
| 63 | - } |
|
| 41 | + /** |
|
| 42 | + * LegacySetting constructor. |
|
| 43 | + * |
|
| 44 | + * @param string $identifier |
|
| 45 | + * @param string $name |
|
| 46 | + * @param bool $canChangeStream |
|
| 47 | + * @param bool $isDefaultEnabledStream |
|
| 48 | + * @param bool $canChangeMail |
|
| 49 | + * @param bool $isDefaultEnabledMail |
|
| 50 | + */ |
|
| 51 | + public function __construct($identifier, |
|
| 52 | + $name, |
|
| 53 | + $canChangeStream, |
|
| 54 | + $isDefaultEnabledStream, |
|
| 55 | + $canChangeMail, |
|
| 56 | + $isDefaultEnabledMail) { |
|
| 57 | + $this->identifier = $identifier; |
|
| 58 | + $this->name = $name; |
|
| 59 | + $this->canChangeStream = $canChangeStream; |
|
| 60 | + $this->isDefaultEnabledStream = $isDefaultEnabledStream; |
|
| 61 | + $this->canChangeMail = $canChangeMail; |
|
| 62 | + $this->isDefaultEnabledMail = $isDefaultEnabledMail; |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * @return string Lowercase a-z and underscore only identifier |
|
| 67 | - * @since 11.0.0 |
|
| 68 | - */ |
|
| 69 | - public function getIdentifier() { |
|
| 70 | - return $this->identifier; |
|
| 71 | - } |
|
| 65 | + /** |
|
| 66 | + * @return string Lowercase a-z and underscore only identifier |
|
| 67 | + * @since 11.0.0 |
|
| 68 | + */ |
|
| 69 | + public function getIdentifier() { |
|
| 70 | + return $this->identifier; |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - /** |
|
| 74 | - * @return string A translated string |
|
| 75 | - * @since 11.0.0 |
|
| 76 | - */ |
|
| 77 | - public function getName() { |
|
| 78 | - return $this->name; |
|
| 79 | - } |
|
| 73 | + /** |
|
| 74 | + * @return string A translated string |
|
| 75 | + * @since 11.0.0 |
|
| 76 | + */ |
|
| 77 | + public function getName() { |
|
| 78 | + return $this->name; |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - /** |
|
| 82 | - * @return int whether the filter should be rather on the top or bottom of |
|
| 83 | - * the admin section. The filters are arranged in ascending order of the |
|
| 84 | - * priority values. It is required to return a value between 0 and 100. |
|
| 85 | - * @since 11.0.0 |
|
| 86 | - */ |
|
| 87 | - public function getPriority() { |
|
| 88 | - return 70; |
|
| 89 | - } |
|
| 81 | + /** |
|
| 82 | + * @return int whether the filter should be rather on the top or bottom of |
|
| 83 | + * the admin section. The filters are arranged in ascending order of the |
|
| 84 | + * priority values. It is required to return a value between 0 and 100. |
|
| 85 | + * @since 11.0.0 |
|
| 86 | + */ |
|
| 87 | + public function getPriority() { |
|
| 88 | + return 70; |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - /** |
|
| 92 | - * @return bool True when the option can be changed for the stream |
|
| 93 | - * @since 11.0.0 |
|
| 94 | - */ |
|
| 95 | - public function canChangeStream() { |
|
| 96 | - return $this->canChangeStream; |
|
| 97 | - } |
|
| 91 | + /** |
|
| 92 | + * @return bool True when the option can be changed for the stream |
|
| 93 | + * @since 11.0.0 |
|
| 94 | + */ |
|
| 95 | + public function canChangeStream() { |
|
| 96 | + return $this->canChangeStream; |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - /** |
|
| 100 | - * @return bool True when the option can be changed for the stream |
|
| 101 | - * @since 11.0.0 |
|
| 102 | - */ |
|
| 103 | - public function isDefaultEnabledStream() { |
|
| 104 | - return $this->isDefaultEnabledStream; |
|
| 105 | - } |
|
| 99 | + /** |
|
| 100 | + * @return bool True when the option can be changed for the stream |
|
| 101 | + * @since 11.0.0 |
|
| 102 | + */ |
|
| 103 | + public function isDefaultEnabledStream() { |
|
| 104 | + return $this->isDefaultEnabledStream; |
|
| 105 | + } |
|
| 106 | 106 | |
| 107 | - /** |
|
| 108 | - * @return bool True when the option can be changed for the mail |
|
| 109 | - * @since 11.0.0 |
|
| 110 | - */ |
|
| 111 | - public function canChangeMail() { |
|
| 112 | - return $this->canChangeMail; |
|
| 113 | - } |
|
| 107 | + /** |
|
| 108 | + * @return bool True when the option can be changed for the mail |
|
| 109 | + * @since 11.0.0 |
|
| 110 | + */ |
|
| 111 | + public function canChangeMail() { |
|
| 112 | + return $this->canChangeMail; |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - /** |
|
| 116 | - * @return bool True when the option can be changed for the stream |
|
| 117 | - * @since 11.0.0 |
|
| 118 | - */ |
|
| 119 | - public function isDefaultEnabledMail() { |
|
| 120 | - return $this->isDefaultEnabledMail; |
|
| 121 | - } |
|
| 115 | + /** |
|
| 116 | + * @return bool True when the option can be changed for the stream |
|
| 117 | + * @since 11.0.0 |
|
| 118 | + */ |
|
| 119 | + public function isDefaultEnabledMail() { |
|
| 120 | + return $this->isDefaultEnabledMail; |
|
| 121 | + } |
|
| 122 | 122 | } |
| 123 | 123 | |
@@ -29,52 +29,52 @@ |
||
| 29 | 29 | * @package OC\Security\CSP |
| 30 | 30 | */ |
| 31 | 31 | class ContentSecurityPolicyNonceManager { |
| 32 | - /** @var CsrfTokenManager */ |
|
| 33 | - private $csrfTokenManager; |
|
| 34 | - /** @var IRequest */ |
|
| 35 | - private $request; |
|
| 36 | - /** @var string */ |
|
| 37 | - private $nonce = ''; |
|
| 32 | + /** @var CsrfTokenManager */ |
|
| 33 | + private $csrfTokenManager; |
|
| 34 | + /** @var IRequest */ |
|
| 35 | + private $request; |
|
| 36 | + /** @var string */ |
|
| 37 | + private $nonce = ''; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * @param CsrfTokenManager $csrfTokenManager |
|
| 41 | - * @param IRequest $request |
|
| 42 | - */ |
|
| 43 | - public function __construct(CsrfTokenManager $csrfTokenManager, |
|
| 44 | - IRequest $request) { |
|
| 45 | - $this->csrfTokenManager = $csrfTokenManager; |
|
| 46 | - $this->request = $request; |
|
| 47 | - } |
|
| 39 | + /** |
|
| 40 | + * @param CsrfTokenManager $csrfTokenManager |
|
| 41 | + * @param IRequest $request |
|
| 42 | + */ |
|
| 43 | + public function __construct(CsrfTokenManager $csrfTokenManager, |
|
| 44 | + IRequest $request) { |
|
| 45 | + $this->csrfTokenManager = $csrfTokenManager; |
|
| 46 | + $this->request = $request; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Returns the current CSP nounce |
|
| 51 | - * |
|
| 52 | - * @return string |
|
| 53 | - */ |
|
| 54 | - public function getNonce() { |
|
| 55 | - if($this->nonce === '') { |
|
| 56 | - $this->nonce = base64_encode($this->csrfTokenManager->getToken()->getEncryptedValue()); |
|
| 57 | - } |
|
| 49 | + /** |
|
| 50 | + * Returns the current CSP nounce |
|
| 51 | + * |
|
| 52 | + * @return string |
|
| 53 | + */ |
|
| 54 | + public function getNonce() { |
|
| 55 | + if($this->nonce === '') { |
|
| 56 | + $this->nonce = base64_encode($this->csrfTokenManager->getToken()->getEncryptedValue()); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - return $this->nonce; |
|
| 60 | - } |
|
| 59 | + return $this->nonce; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Check if the browser supports CSP v3 |
|
| 64 | - * |
|
| 65 | - * @return bool |
|
| 66 | - */ |
|
| 67 | - public function browserSupportsCspV3() { |
|
| 68 | - $browserWhitelist = [ |
|
| 69 | - Request::USER_AGENT_CHROME, |
|
| 70 | - // Firefox 45+ |
|
| 71 | - '/^Mozilla\/5\.0 \([^)]+\) Gecko\/[0-9.]+ Firefox\/(4[5-9]|[5-9][0-9])\.[0-9.]+$/', |
|
| 72 | - ]; |
|
| 62 | + /** |
|
| 63 | + * Check if the browser supports CSP v3 |
|
| 64 | + * |
|
| 65 | + * @return bool |
|
| 66 | + */ |
|
| 67 | + public function browserSupportsCspV3() { |
|
| 68 | + $browserWhitelist = [ |
|
| 69 | + Request::USER_AGENT_CHROME, |
|
| 70 | + // Firefox 45+ |
|
| 71 | + '/^Mozilla\/5\.0 \([^)]+\) Gecko\/[0-9.]+ Firefox\/(4[5-9]|[5-9][0-9])\.[0-9.]+$/', |
|
| 72 | + ]; |
|
| 73 | 73 | |
| 74 | - if($this->request->isUserAgent($browserWhitelist)) { |
|
| 75 | - return true; |
|
| 76 | - } |
|
| 74 | + if($this->request->isUserAgent($browserWhitelist)) { |
|
| 75 | + return true; |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - return false; |
|
| 79 | - } |
|
| 78 | + return false; |
|
| 79 | + } |
|
| 80 | 80 | } |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | * @return string |
| 53 | 53 | */ |
| 54 | 54 | public function getNonce() { |
| 55 | - if($this->nonce === '') { |
|
| 55 | + if ($this->nonce === '') { |
|
| 56 | 56 | $this->nonce = base64_encode($this->csrfTokenManager->getToken()->getEncryptedValue()); |
| 57 | 57 | } |
| 58 | 58 | |
@@ -71,7 +71,7 @@ discard block |
||
| 71 | 71 | '/^Mozilla\/5\.0 \([^)]+\) Gecko\/[0-9.]+ Firefox\/(4[5-9]|[5-9][0-9])\.[0-9.]+$/', |
| 72 | 72 | ]; |
| 73 | 73 | |
| 74 | - if($this->request->isUserAgent($browserWhitelist)) { |
|
| 74 | + if ($this->request->isUserAgent($browserWhitelist)) { |
|
| 75 | 75 | return true; |
| 76 | 76 | } |
| 77 | 77 | |
@@ -27,48 +27,48 @@ |
||
| 27 | 27 | use OCP\Security\IContentSecurityPolicyManager; |
| 28 | 28 | |
| 29 | 29 | class ContentSecurityPolicyManager implements IContentSecurityPolicyManager { |
| 30 | - /** @var ContentSecurityPolicy[] */ |
|
| 31 | - private $policies = []; |
|
| 30 | + /** @var ContentSecurityPolicy[] */ |
|
| 31 | + private $policies = []; |
|
| 32 | 32 | |
| 33 | - /** {@inheritdoc} */ |
|
| 34 | - public function addDefaultPolicy(EmptyContentSecurityPolicy $policy) { |
|
| 35 | - $this->policies[] = $policy; |
|
| 36 | - } |
|
| 33 | + /** {@inheritdoc} */ |
|
| 34 | + public function addDefaultPolicy(EmptyContentSecurityPolicy $policy) { |
|
| 35 | + $this->policies[] = $policy; |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * Get the configured default policy. This is not in the public namespace |
|
| 40 | - * as it is only supposed to be used by core itself. |
|
| 41 | - * |
|
| 42 | - * @return ContentSecurityPolicy |
|
| 43 | - */ |
|
| 44 | - public function getDefaultPolicy() { |
|
| 45 | - $defaultPolicy = new \OC\Security\CSP\ContentSecurityPolicy(); |
|
| 46 | - foreach($this->policies as $policy) { |
|
| 47 | - $defaultPolicy = $this->mergePolicies($defaultPolicy, $policy); |
|
| 48 | - } |
|
| 49 | - return $defaultPolicy; |
|
| 50 | - } |
|
| 38 | + /** |
|
| 39 | + * Get the configured default policy. This is not in the public namespace |
|
| 40 | + * as it is only supposed to be used by core itself. |
|
| 41 | + * |
|
| 42 | + * @return ContentSecurityPolicy |
|
| 43 | + */ |
|
| 44 | + public function getDefaultPolicy() { |
|
| 45 | + $defaultPolicy = new \OC\Security\CSP\ContentSecurityPolicy(); |
|
| 46 | + foreach($this->policies as $policy) { |
|
| 47 | + $defaultPolicy = $this->mergePolicies($defaultPolicy, $policy); |
|
| 48 | + } |
|
| 49 | + return $defaultPolicy; |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Merges the first given policy with the second one |
|
| 54 | - * |
|
| 55 | - * @param ContentSecurityPolicy $defaultPolicy |
|
| 56 | - * @param EmptyContentSecurityPolicy $originalPolicy |
|
| 57 | - * @return ContentSecurityPolicy |
|
| 58 | - */ |
|
| 59 | - public function mergePolicies(ContentSecurityPolicy $defaultPolicy, |
|
| 60 | - EmptyContentSecurityPolicy $originalPolicy) { |
|
| 61 | - foreach((object)(array)$originalPolicy as $name => $value) { |
|
| 62 | - $setter = 'set'.ucfirst($name); |
|
| 63 | - if(is_array($value)) { |
|
| 64 | - $getter = 'get'.ucfirst($name); |
|
| 65 | - $currentValues = is_array($defaultPolicy->$getter()) ? $defaultPolicy->$getter() : []; |
|
| 66 | - $defaultPolicy->$setter(array_values(array_unique(array_merge($currentValues, $value)))); |
|
| 67 | - } elseif (is_bool($value)) { |
|
| 68 | - $defaultPolicy->$setter($value); |
|
| 69 | - } |
|
| 70 | - } |
|
| 52 | + /** |
|
| 53 | + * Merges the first given policy with the second one |
|
| 54 | + * |
|
| 55 | + * @param ContentSecurityPolicy $defaultPolicy |
|
| 56 | + * @param EmptyContentSecurityPolicy $originalPolicy |
|
| 57 | + * @return ContentSecurityPolicy |
|
| 58 | + */ |
|
| 59 | + public function mergePolicies(ContentSecurityPolicy $defaultPolicy, |
|
| 60 | + EmptyContentSecurityPolicy $originalPolicy) { |
|
| 61 | + foreach((object)(array)$originalPolicy as $name => $value) { |
|
| 62 | + $setter = 'set'.ucfirst($name); |
|
| 63 | + if(is_array($value)) { |
|
| 64 | + $getter = 'get'.ucfirst($name); |
|
| 65 | + $currentValues = is_array($defaultPolicy->$getter()) ? $defaultPolicy->$getter() : []; |
|
| 66 | + $defaultPolicy->$setter(array_values(array_unique(array_merge($currentValues, $value)))); |
|
| 67 | + } elseif (is_bool($value)) { |
|
| 68 | + $defaultPolicy->$setter($value); |
|
| 69 | + } |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - return $defaultPolicy; |
|
| 73 | - } |
|
| 72 | + return $defaultPolicy; |
|
| 73 | + } |
|
| 74 | 74 | } |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | */ |
| 44 | 44 | public function getDefaultPolicy() { |
| 45 | 45 | $defaultPolicy = new \OC\Security\CSP\ContentSecurityPolicy(); |
| 46 | - foreach($this->policies as $policy) { |
|
| 46 | + foreach ($this->policies as $policy) { |
|
| 47 | 47 | $defaultPolicy = $this->mergePolicies($defaultPolicy, $policy); |
| 48 | 48 | } |
| 49 | 49 | return $defaultPolicy; |
@@ -58,9 +58,9 @@ discard block |
||
| 58 | 58 | */ |
| 59 | 59 | public function mergePolicies(ContentSecurityPolicy $defaultPolicy, |
| 60 | 60 | EmptyContentSecurityPolicy $originalPolicy) { |
| 61 | - foreach((object)(array)$originalPolicy as $name => $value) { |
|
| 61 | + foreach ((object) (array) $originalPolicy as $name => $value) { |
|
| 62 | 62 | $setter = 'set'.ucfirst($name); |
| 63 | - if(is_array($value)) { |
|
| 63 | + if (is_array($value)) { |
|
| 64 | 64 | $getter = 'get'.ucfirst($name); |
| 65 | 65 | $currentValues = is_array($defaultPolicy->$getter()) ? $defaultPolicy->$getter() : []; |
| 66 | 66 | $defaultPolicy->$setter(array_values(array_unique(array_merge($currentValues, $value)))); |
@@ -34,93 +34,93 @@ |
||
| 34 | 34 | */ |
| 35 | 35 | class CredentialsManager implements ICredentialsManager { |
| 36 | 36 | |
| 37 | - const DB_TABLE = 'credentials'; |
|
| 37 | + const DB_TABLE = 'credentials'; |
|
| 38 | 38 | |
| 39 | - /** @var ICrypto */ |
|
| 40 | - protected $crypto; |
|
| 39 | + /** @var ICrypto */ |
|
| 40 | + protected $crypto; |
|
| 41 | 41 | |
| 42 | - /** @var IDBConnection */ |
|
| 43 | - protected $dbConnection; |
|
| 42 | + /** @var IDBConnection */ |
|
| 43 | + protected $dbConnection; |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * @param ICrypto $crypto |
|
| 47 | - * @param IDBConnection $dbConnection |
|
| 48 | - */ |
|
| 49 | - public function __construct(ICrypto $crypto, IDBConnection $dbConnection) { |
|
| 50 | - $this->crypto = $crypto; |
|
| 51 | - $this->dbConnection = $dbConnection; |
|
| 52 | - } |
|
| 45 | + /** |
|
| 46 | + * @param ICrypto $crypto |
|
| 47 | + * @param IDBConnection $dbConnection |
|
| 48 | + */ |
|
| 49 | + public function __construct(ICrypto $crypto, IDBConnection $dbConnection) { |
|
| 50 | + $this->crypto = $crypto; |
|
| 51 | + $this->dbConnection = $dbConnection; |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Store a set of credentials |
|
| 56 | - * |
|
| 57 | - * @param string|null $userId Null for system-wide credentials |
|
| 58 | - * @param string $identifier |
|
| 59 | - * @param mixed $credentials |
|
| 60 | - */ |
|
| 61 | - public function store($userId, $identifier, $credentials) { |
|
| 62 | - $value = $this->crypto->encrypt(json_encode($credentials)); |
|
| 54 | + /** |
|
| 55 | + * Store a set of credentials |
|
| 56 | + * |
|
| 57 | + * @param string|null $userId Null for system-wide credentials |
|
| 58 | + * @param string $identifier |
|
| 59 | + * @param mixed $credentials |
|
| 60 | + */ |
|
| 61 | + public function store($userId, $identifier, $credentials) { |
|
| 62 | + $value = $this->crypto->encrypt(json_encode($credentials)); |
|
| 63 | 63 | |
| 64 | - $this->dbConnection->setValues(self::DB_TABLE, [ |
|
| 65 | - 'user' => $userId, |
|
| 66 | - 'identifier' => $identifier, |
|
| 67 | - ], [ |
|
| 68 | - 'credentials' => $value, |
|
| 69 | - ]); |
|
| 70 | - } |
|
| 64 | + $this->dbConnection->setValues(self::DB_TABLE, [ |
|
| 65 | + 'user' => $userId, |
|
| 66 | + 'identifier' => $identifier, |
|
| 67 | + ], [ |
|
| 68 | + 'credentials' => $value, |
|
| 69 | + ]); |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * Retrieve a set of credentials |
|
| 74 | - * |
|
| 75 | - * @param string|null $userId Null for system-wide credentials |
|
| 76 | - * @param string $identifier |
|
| 77 | - * @return mixed |
|
| 78 | - */ |
|
| 79 | - public function retrieve($userId, $identifier) { |
|
| 80 | - $qb = $this->dbConnection->getQueryBuilder(); |
|
| 81 | - $qb->select('credentials') |
|
| 82 | - ->from(self::DB_TABLE) |
|
| 83 | - ->where($qb->expr()->eq('user', $qb->createNamedParameter($userId))) |
|
| 84 | - ->andWhere($qb->expr()->eq('identifier', $qb->createNamedParameter($identifier))) |
|
| 85 | - ; |
|
| 86 | - $result = $qb->execute()->fetch(); |
|
| 72 | + /** |
|
| 73 | + * Retrieve a set of credentials |
|
| 74 | + * |
|
| 75 | + * @param string|null $userId Null for system-wide credentials |
|
| 76 | + * @param string $identifier |
|
| 77 | + * @return mixed |
|
| 78 | + */ |
|
| 79 | + public function retrieve($userId, $identifier) { |
|
| 80 | + $qb = $this->dbConnection->getQueryBuilder(); |
|
| 81 | + $qb->select('credentials') |
|
| 82 | + ->from(self::DB_TABLE) |
|
| 83 | + ->where($qb->expr()->eq('user', $qb->createNamedParameter($userId))) |
|
| 84 | + ->andWhere($qb->expr()->eq('identifier', $qb->createNamedParameter($identifier))) |
|
| 85 | + ; |
|
| 86 | + $result = $qb->execute()->fetch(); |
|
| 87 | 87 | |
| 88 | - if (!$result) { |
|
| 89 | - return null; |
|
| 90 | - } |
|
| 91 | - $value = $result['credentials']; |
|
| 88 | + if (!$result) { |
|
| 89 | + return null; |
|
| 90 | + } |
|
| 91 | + $value = $result['credentials']; |
|
| 92 | 92 | |
| 93 | - return json_decode($this->crypto->decrypt($value), true); |
|
| 94 | - } |
|
| 93 | + return json_decode($this->crypto->decrypt($value), true); |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | - /** |
|
| 97 | - * Delete a set of credentials |
|
| 98 | - * |
|
| 99 | - * @param string|null $userId Null for system-wide credentials |
|
| 100 | - * @param string $identifier |
|
| 101 | - * @return int rows removed |
|
| 102 | - */ |
|
| 103 | - public function delete($userId, $identifier) { |
|
| 104 | - $qb = $this->dbConnection->getQueryBuilder(); |
|
| 105 | - $qb->delete(self::DB_TABLE) |
|
| 106 | - ->where($qb->expr()->eq('user', $qb->createNamedParameter($userId))) |
|
| 107 | - ->andWhere($qb->expr()->eq('identifier', $qb->createNamedParameter($identifier))) |
|
| 108 | - ; |
|
| 109 | - return $qb->execute(); |
|
| 110 | - } |
|
| 96 | + /** |
|
| 97 | + * Delete a set of credentials |
|
| 98 | + * |
|
| 99 | + * @param string|null $userId Null for system-wide credentials |
|
| 100 | + * @param string $identifier |
|
| 101 | + * @return int rows removed |
|
| 102 | + */ |
|
| 103 | + public function delete($userId, $identifier) { |
|
| 104 | + $qb = $this->dbConnection->getQueryBuilder(); |
|
| 105 | + $qb->delete(self::DB_TABLE) |
|
| 106 | + ->where($qb->expr()->eq('user', $qb->createNamedParameter($userId))) |
|
| 107 | + ->andWhere($qb->expr()->eq('identifier', $qb->createNamedParameter($identifier))) |
|
| 108 | + ; |
|
| 109 | + return $qb->execute(); |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | - /** |
|
| 113 | - * Erase all credentials stored for a user |
|
| 114 | - * |
|
| 115 | - * @param string $userId |
|
| 116 | - * @return int rows removed |
|
| 117 | - */ |
|
| 118 | - public function erase($userId) { |
|
| 119 | - $qb = $this->dbConnection->getQueryBuilder(); |
|
| 120 | - $qb->delete(self::DB_TABLE) |
|
| 121 | - ->where($qb->expr()->eq('user', $qb->createNamedParameter($userId))) |
|
| 122 | - ; |
|
| 123 | - return $qb->execute(); |
|
| 124 | - } |
|
| 112 | + /** |
|
| 113 | + * Erase all credentials stored for a user |
|
| 114 | + * |
|
| 115 | + * @param string $userId |
|
| 116 | + * @return int rows removed |
|
| 117 | + */ |
|
| 118 | + public function erase($userId) { |
|
| 119 | + $qb = $this->dbConnection->getQueryBuilder(); |
|
| 120 | + $qb->delete(self::DB_TABLE) |
|
| 121 | + ->where($qb->expr()->eq('user', $qb->createNamedParameter($userId))) |
|
| 122 | + ; |
|
| 123 | + return $qb->execute(); |
|
| 124 | + } |
|
| 125 | 125 | |
| 126 | 126 | } |
@@ -50,9 +50,9 @@ |
||
| 50 | 50 | * @return string |
| 51 | 51 | */ |
| 52 | 52 | public function getEncryptedValue() { |
| 53 | - if($this->encryptedValue === '') { |
|
| 53 | + if ($this->encryptedValue === '') { |
|
| 54 | 54 | $sharedSecret = random_bytes(strlen($this->value)); |
| 55 | - $this->encryptedValue = base64_encode($this->value ^ $sharedSecret) . ':' . base64_encode($sharedSecret); |
|
| 55 | + $this->encryptedValue = base64_encode($this->value ^ $sharedSecret).':'.base64_encode($sharedSecret); |
|
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | return $this->encryptedValue; |
@@ -31,46 +31,46 @@ |
||
| 31 | 31 | * @package OC\Security\CSRF |
| 32 | 32 | */ |
| 33 | 33 | class CsrfToken { |
| 34 | - /** @var string */ |
|
| 35 | - private $value; |
|
| 36 | - /** @var string */ |
|
| 37 | - private $encryptedValue = ''; |
|
| 34 | + /** @var string */ |
|
| 35 | + private $value; |
|
| 36 | + /** @var string */ |
|
| 37 | + private $encryptedValue = ''; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * @param string $value Value of the token. Can be encrypted or not encrypted. |
|
| 41 | - */ |
|
| 42 | - public function __construct($value) { |
|
| 43 | - $this->value = $value; |
|
| 44 | - } |
|
| 39 | + /** |
|
| 40 | + * @param string $value Value of the token. Can be encrypted or not encrypted. |
|
| 41 | + */ |
|
| 42 | + public function __construct($value) { |
|
| 43 | + $this->value = $value; |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * Encrypted value of the token. This is used to mitigate BREACH alike |
|
| 48 | - * vulnerabilities. For display measures do use this functionality. |
|
| 49 | - * |
|
| 50 | - * @return string |
|
| 51 | - */ |
|
| 52 | - public function getEncryptedValue() { |
|
| 53 | - if($this->encryptedValue === '') { |
|
| 54 | - $sharedSecret = random_bytes(strlen($this->value)); |
|
| 55 | - $this->encryptedValue = base64_encode($this->value ^ $sharedSecret) . ':' . base64_encode($sharedSecret); |
|
| 56 | - } |
|
| 46 | + /** |
|
| 47 | + * Encrypted value of the token. This is used to mitigate BREACH alike |
|
| 48 | + * vulnerabilities. For display measures do use this functionality. |
|
| 49 | + * |
|
| 50 | + * @return string |
|
| 51 | + */ |
|
| 52 | + public function getEncryptedValue() { |
|
| 53 | + if($this->encryptedValue === '') { |
|
| 54 | + $sharedSecret = random_bytes(strlen($this->value)); |
|
| 55 | + $this->encryptedValue = base64_encode($this->value ^ $sharedSecret) . ':' . base64_encode($sharedSecret); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - return $this->encryptedValue; |
|
| 59 | - } |
|
| 58 | + return $this->encryptedValue; |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * The unencrypted value of the token. Used for decrypting an already |
|
| 63 | - * encrypted token. |
|
| 64 | - * |
|
| 65 | - * @return string |
|
| 66 | - */ |
|
| 67 | - public function getDecryptedValue() { |
|
| 68 | - $token = explode(':', $this->value); |
|
| 69 | - if (count($token) !== 2) { |
|
| 70 | - return ''; |
|
| 71 | - } |
|
| 72 | - $obfuscatedToken = $token[0]; |
|
| 73 | - $secret = $token[1]; |
|
| 74 | - return base64_decode($obfuscatedToken) ^ base64_decode($secret); |
|
| 75 | - } |
|
| 61 | + /** |
|
| 62 | + * The unencrypted value of the token. Used for decrypting an already |
|
| 63 | + * encrypted token. |
|
| 64 | + * |
|
| 65 | + * @return string |
|
| 66 | + */ |
|
| 67 | + public function getDecryptedValue() { |
|
| 68 | + $token = explode(':', $this->value); |
|
| 69 | + if (count($token) !== 2) { |
|
| 70 | + return ''; |
|
| 71 | + } |
|
| 72 | + $obfuscatedToken = $token[0]; |
|
| 73 | + $secret = $token[1]; |
|
| 74 | + return base64_decode($obfuscatedToken) ^ base64_decode($secret); |
|
| 75 | + } |
|
| 76 | 76 | } |
@@ -30,59 +30,59 @@ |
||
| 30 | 30 | * @package OC\Security\CSRF\TokenStorage |
| 31 | 31 | */ |
| 32 | 32 | class SessionStorage { |
| 33 | - /** @var ISession */ |
|
| 34 | - private $session; |
|
| 33 | + /** @var ISession */ |
|
| 34 | + private $session; |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * @param ISession $session |
|
| 38 | - */ |
|
| 39 | - public function __construct(ISession $session) { |
|
| 40 | - $this->session = $session; |
|
| 41 | - } |
|
| 36 | + /** |
|
| 37 | + * @param ISession $session |
|
| 38 | + */ |
|
| 39 | + public function __construct(ISession $session) { |
|
| 40 | + $this->session = $session; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * @param ISession $session |
|
| 45 | - */ |
|
| 46 | - public function setSession(ISession $session) { |
|
| 47 | - $this->session = $session; |
|
| 48 | - } |
|
| 43 | + /** |
|
| 44 | + * @param ISession $session |
|
| 45 | + */ |
|
| 46 | + public function setSession(ISession $session) { |
|
| 47 | + $this->session = $session; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Returns the current token or throws an exception if none is found. |
|
| 52 | - * |
|
| 53 | - * @return string |
|
| 54 | - * @throws \Exception |
|
| 55 | - */ |
|
| 56 | - public function getToken() { |
|
| 57 | - $token = $this->session->get('requesttoken'); |
|
| 58 | - if(empty($token)) { |
|
| 59 | - throw new \Exception('Session does not contain a requesttoken'); |
|
| 60 | - } |
|
| 50 | + /** |
|
| 51 | + * Returns the current token or throws an exception if none is found. |
|
| 52 | + * |
|
| 53 | + * @return string |
|
| 54 | + * @throws \Exception |
|
| 55 | + */ |
|
| 56 | + public function getToken() { |
|
| 57 | + $token = $this->session->get('requesttoken'); |
|
| 58 | + if(empty($token)) { |
|
| 59 | + throw new \Exception('Session does not contain a requesttoken'); |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - return $token; |
|
| 63 | - } |
|
| 62 | + return $token; |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Set the valid current token to $value. |
|
| 67 | - * |
|
| 68 | - * @param string $value |
|
| 69 | - */ |
|
| 70 | - public function setToken($value) { |
|
| 71 | - $this->session->set('requesttoken', $value); |
|
| 72 | - } |
|
| 65 | + /** |
|
| 66 | + * Set the valid current token to $value. |
|
| 67 | + * |
|
| 68 | + * @param string $value |
|
| 69 | + */ |
|
| 70 | + public function setToken($value) { |
|
| 71 | + $this->session->set('requesttoken', $value); |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * Removes the current token. |
|
| 76 | - */ |
|
| 77 | - public function removeToken() { |
|
| 78 | - $this->session->remove('requesttoken'); |
|
| 79 | - } |
|
| 80 | - /** |
|
| 81 | - * Whether the storage has a storage. |
|
| 82 | - * |
|
| 83 | - * @return bool |
|
| 84 | - */ |
|
| 85 | - public function hasToken() { |
|
| 86 | - return $this->session->exists('requesttoken'); |
|
| 87 | - } |
|
| 74 | + /** |
|
| 75 | + * Removes the current token. |
|
| 76 | + */ |
|
| 77 | + public function removeToken() { |
|
| 78 | + $this->session->remove('requesttoken'); |
|
| 79 | + } |
|
| 80 | + /** |
|
| 81 | + * Whether the storage has a storage. |
|
| 82 | + * |
|
| 83 | + * @return bool |
|
| 84 | + */ |
|
| 85 | + public function hasToken() { |
|
| 86 | + return $this->session->exists('requesttoken'); |
|
| 87 | + } |
|
| 88 | 88 | } |
@@ -55,7 +55,7 @@ |
||
| 55 | 55 | */ |
| 56 | 56 | public function getToken() { |
| 57 | 57 | $token = $this->session->get('requesttoken'); |
| 58 | - if(empty($token)) { |
|
| 58 | + if (empty($token)) { |
|
| 59 | 59 | throw new \Exception('Session does not contain a requesttoken'); |
| 60 | 60 | } |
| 61 | 61 | |
@@ -30,78 +30,78 @@ |
||
| 30 | 30 | * @package OC\Security\CSRF |
| 31 | 31 | */ |
| 32 | 32 | class CsrfTokenManager { |
| 33 | - /** @var CsrfTokenGenerator */ |
|
| 34 | - private $tokenGenerator; |
|
| 35 | - /** @var SessionStorage */ |
|
| 36 | - private $sessionStorage; |
|
| 37 | - /** @var CsrfToken|null */ |
|
| 38 | - private $csrfToken = null; |
|
| 33 | + /** @var CsrfTokenGenerator */ |
|
| 34 | + private $tokenGenerator; |
|
| 35 | + /** @var SessionStorage */ |
|
| 36 | + private $sessionStorage; |
|
| 37 | + /** @var CsrfToken|null */ |
|
| 38 | + private $csrfToken = null; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * @param CsrfTokenGenerator $tokenGenerator |
|
| 42 | - * @param SessionStorage $storageInterface |
|
| 43 | - */ |
|
| 44 | - public function __construct(CsrfTokenGenerator $tokenGenerator, |
|
| 45 | - SessionStorage $storageInterface) { |
|
| 46 | - $this->tokenGenerator = $tokenGenerator; |
|
| 47 | - $this->sessionStorage = $storageInterface; |
|
| 48 | - } |
|
| 40 | + /** |
|
| 41 | + * @param CsrfTokenGenerator $tokenGenerator |
|
| 42 | + * @param SessionStorage $storageInterface |
|
| 43 | + */ |
|
| 44 | + public function __construct(CsrfTokenGenerator $tokenGenerator, |
|
| 45 | + SessionStorage $storageInterface) { |
|
| 46 | + $this->tokenGenerator = $tokenGenerator; |
|
| 47 | + $this->sessionStorage = $storageInterface; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Returns the current CSRF token, if none set it will create a new one. |
|
| 52 | - * |
|
| 53 | - * @return CsrfToken |
|
| 54 | - */ |
|
| 55 | - public function getToken() { |
|
| 56 | - if(!is_null($this->csrfToken)) { |
|
| 57 | - return $this->csrfToken; |
|
| 58 | - } |
|
| 50 | + /** |
|
| 51 | + * Returns the current CSRF token, if none set it will create a new one. |
|
| 52 | + * |
|
| 53 | + * @return CsrfToken |
|
| 54 | + */ |
|
| 55 | + public function getToken() { |
|
| 56 | + if(!is_null($this->csrfToken)) { |
|
| 57 | + return $this->csrfToken; |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - if($this->sessionStorage->hasToken()) { |
|
| 61 | - $value = $this->sessionStorage->getToken(); |
|
| 62 | - } else { |
|
| 63 | - $value = $this->tokenGenerator->generateToken(); |
|
| 64 | - $this->sessionStorage->setToken($value); |
|
| 65 | - } |
|
| 60 | + if($this->sessionStorage->hasToken()) { |
|
| 61 | + $value = $this->sessionStorage->getToken(); |
|
| 62 | + } else { |
|
| 63 | + $value = $this->tokenGenerator->generateToken(); |
|
| 64 | + $this->sessionStorage->setToken($value); |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - $this->csrfToken = new CsrfToken($value); |
|
| 68 | - return $this->csrfToken; |
|
| 69 | - } |
|
| 67 | + $this->csrfToken = new CsrfToken($value); |
|
| 68 | + return $this->csrfToken; |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * Invalidates any current token and sets a new one. |
|
| 73 | - * |
|
| 74 | - * @return CsrfToken |
|
| 75 | - */ |
|
| 76 | - public function refreshToken() { |
|
| 77 | - $value = $this->tokenGenerator->generateToken(); |
|
| 78 | - $this->sessionStorage->setToken($value); |
|
| 79 | - $this->csrfToken = new CsrfToken($value); |
|
| 80 | - return $this->csrfToken; |
|
| 81 | - } |
|
| 71 | + /** |
|
| 72 | + * Invalidates any current token and sets a new one. |
|
| 73 | + * |
|
| 74 | + * @return CsrfToken |
|
| 75 | + */ |
|
| 76 | + public function refreshToken() { |
|
| 77 | + $value = $this->tokenGenerator->generateToken(); |
|
| 78 | + $this->sessionStorage->setToken($value); |
|
| 79 | + $this->csrfToken = new CsrfToken($value); |
|
| 80 | + return $this->csrfToken; |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * Remove the current token from the storage. |
|
| 85 | - */ |
|
| 86 | - public function removeToken() { |
|
| 87 | - $this->csrfToken = null; |
|
| 88 | - $this->sessionStorage->removeToken(); |
|
| 89 | - } |
|
| 83 | + /** |
|
| 84 | + * Remove the current token from the storage. |
|
| 85 | + */ |
|
| 86 | + public function removeToken() { |
|
| 87 | + $this->csrfToken = null; |
|
| 88 | + $this->sessionStorage->removeToken(); |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - /** |
|
| 92 | - * Verifies whether the provided token is valid. |
|
| 93 | - * |
|
| 94 | - * @param CsrfToken $token |
|
| 95 | - * @return bool |
|
| 96 | - */ |
|
| 97 | - public function isTokenValid(CsrfToken $token) { |
|
| 98 | - if(!$this->sessionStorage->hasToken()) { |
|
| 99 | - return false; |
|
| 100 | - } |
|
| 91 | + /** |
|
| 92 | + * Verifies whether the provided token is valid. |
|
| 93 | + * |
|
| 94 | + * @param CsrfToken $token |
|
| 95 | + * @return bool |
|
| 96 | + */ |
|
| 97 | + public function isTokenValid(CsrfToken $token) { |
|
| 98 | + if(!$this->sessionStorage->hasToken()) { |
|
| 99 | + return false; |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - return hash_equals( |
|
| 103 | - $this->sessionStorage->getToken(), |
|
| 104 | - $token->getDecryptedValue() |
|
| 105 | - ); |
|
| 106 | - } |
|
| 102 | + return hash_equals( |
|
| 103 | + $this->sessionStorage->getToken(), |
|
| 104 | + $token->getDecryptedValue() |
|
| 105 | + ); |
|
| 106 | + } |
|
| 107 | 107 | } |
@@ -53,11 +53,11 @@ discard block |
||
| 53 | 53 | * @return CsrfToken |
| 54 | 54 | */ |
| 55 | 55 | public function getToken() { |
| 56 | - if(!is_null($this->csrfToken)) { |
|
| 56 | + if (!is_null($this->csrfToken)) { |
|
| 57 | 57 | return $this->csrfToken; |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | - if($this->sessionStorage->hasToken()) { |
|
| 60 | + if ($this->sessionStorage->hasToken()) { |
|
| 61 | 61 | $value = $this->sessionStorage->getToken(); |
| 62 | 62 | } else { |
| 63 | 63 | $value = $this->tokenGenerator->generateToken(); |
@@ -95,7 +95,7 @@ discard block |
||
| 95 | 95 | * @return bool |
| 96 | 96 | */ |
| 97 | 97 | public function isTokenValid(CsrfToken $token) { |
| 98 | - if(!$this->sessionStorage->hasToken()) { |
|
| 98 | + if (!$this->sessionStorage->hasToken()) { |
|
| 99 | 99 | return false; |
| 100 | 100 | } |
| 101 | 101 | |