nextcloud /
announcementcenter
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | declare(strict_types=1); |
||
| 3 | /** |
||
| 4 | * @copyright Copyright (c) 2016 Joas Schilling <[email protected]> |
||
| 5 | * |
||
| 6 | * @license GNU AGPL version 3 or any later version |
||
| 7 | * |
||
| 8 | * This program is free software: you can redistribute it and/or modify |
||
| 9 | * it under the terms of the GNU Affero General Public License as |
||
| 10 | * published by the Free Software Foundation, either version 3 of the |
||
| 11 | * License, or (at your option) any later version. |
||
| 12 | * |
||
| 13 | * This program is distributed in the hope that it will be useful, |
||
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 16 | * GNU Affero General Public License for more details. |
||
| 17 | * |
||
| 18 | * You should have received a copy of the GNU Affero General Public License |
||
| 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
| 20 | * |
||
| 21 | */ |
||
| 22 | |||
| 23 | namespace OCA\AnnouncementCenter\Activity; |
||
| 24 | |||
| 25 | use OCA\AnnouncementCenter\Manager; |
||
| 26 | use OCA\AnnouncementCenter\Model\Announcement; |
||
| 27 | use OCA\AnnouncementCenter\Model\AnnouncementDoesNotExistException; |
||
| 28 | use OCP\Activity\IEvent; |
||
| 29 | use OCP\Activity\IManager as IActivityManager; |
||
| 30 | use OCP\Activity\IProvider; |
||
| 31 | use OCP\IURLGenerator; |
||
| 32 | use OCP\IUser; |
||
| 33 | use OCP\IUserManager; |
||
| 34 | use OCP\L10N\IFactory; |
||
| 35 | |||
| 36 | class Provider implements IProvider { |
||
| 37 | |||
| 38 | /** @var IFactory */ |
||
| 39 | protected $languageFactory; |
||
| 40 | |||
| 41 | /** @var IURLGenerator */ |
||
| 42 | protected $url; |
||
| 43 | |||
| 44 | /** @var IActivityManager */ |
||
| 45 | protected $activityManager; |
||
| 46 | |||
| 47 | /** @var IUserManager */ |
||
| 48 | protected $userManager; |
||
| 49 | |||
| 50 | /** @var Manager */ |
||
| 51 | protected $manager; |
||
| 52 | |||
| 53 | /** @var string[] */ |
||
| 54 | protected $displayNames = []; |
||
| 55 | |||
| 56 | 1 | View Code Duplication | public function __construct( |
|
0 ignored issues
–
show
|
|||
| 57 | IFactory $languageFactory, |
||
| 58 | IURLGenerator $url, |
||
| 59 | IActivityManager $activityManager, |
||
| 60 | IUserManager $userManager, |
||
| 61 | Manager $manager |
||
| 62 | ) { |
||
| 63 | 1 | $this->languageFactory = $languageFactory; |
|
| 64 | 1 | $this->url = $url; |
|
| 65 | 1 | $this->activityManager = $activityManager; |
|
| 66 | 1 | $this->userManager = $userManager; |
|
| 67 | 1 | $this->manager = $manager; |
|
| 68 | 1 | } |
|
| 69 | |||
| 70 | /** |
||
| 71 | * @param string $language |
||
| 72 | * @param IEvent $event |
||
| 73 | * @param IEvent|null $previousEvent |
||
| 74 | * @return IEvent |
||
| 75 | * @throws \InvalidArgumentException |
||
| 76 | * @since 11.0.0 |
||
| 77 | */ |
||
| 78 | public function parse($language, IEvent $event, IEvent $previousEvent = null): IEvent { |
||
| 79 | if ($event->getApp() !== 'announcementcenter' || ( |
||
| 80 | $event->getSubject() !== 'announcementsubject' && // 3.1 and later |
||
| 81 | strpos($event->getSubject(), 'announcementsubject#') !== 0) // 3.0 and before |
||
| 82 | ) { |
||
| 83 | throw new \InvalidArgumentException('Unknown subject'); |
||
| 84 | } |
||
| 85 | |||
| 86 | $l = $this->languageFactory->get('announcementcenter', $language); |
||
| 87 | |||
| 88 | if (method_exists($this->activityManager, 'getRequirePNG') && $this->activityManager->getRequirePNG()) { |
||
| 89 | $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('announcementcenter', 'announcementcenter-dark.png'))); |
||
| 90 | } else { |
||
| 91 | $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('announcementcenter', 'announcementcenter-dark.svg'))); |
||
| 92 | } |
||
| 93 | |||
| 94 | $parameters = $this->getParameters($event); |
||
| 95 | |||
| 96 | try { |
||
| 97 | $announcement = $this->manager->getAnnouncement($parameters['announcement']); |
||
| 98 | |||
| 99 | $parsedParameters = $this->getParsedParameters($parameters, $announcement); |
||
| 100 | View Code Duplication | if ($parsedParameters['actor']['id'] === $this->activityManager->getCurrentUserId()) { |
|
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. Loading history...
|
|||
| 101 | $subject = $l->t('You announced “{announcement}”'); |
||
| 102 | unset($parsedParameters['actor']); |
||
| 103 | } else { |
||
| 104 | $subject = $l->t('{actor} announced “{announcement}”'); |
||
| 105 | } |
||
| 106 | $event->setParsedMessage($announcement->getMessage()); |
||
| 107 | } catch (AnnouncementDoesNotExistException $e) { |
||
| 108 | $parsedParameters = $this->getParsedParameters($parameters); |
||
| 109 | View Code Duplication | if ($parsedParameters['actor']['id'] === $this->activityManager->getCurrentUserId()) { |
|
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. Loading history...
|
|||
| 110 | $subject = $l->t('You posted an announcement'); |
||
| 111 | unset($parsedParameters['actor']); |
||
| 112 | } else { |
||
| 113 | $subject = $l->t('{actor} posted an announcement'); |
||
| 114 | } |
||
| 115 | |||
| 116 | $event->setParsedMessage($l->t('The announcement does not exist anymore')); |
||
| 117 | } |
||
| 118 | |||
| 119 | |||
| 120 | $this->setSubjects($event, $subject, $parsedParameters); |
||
| 121 | |||
| 122 | return $event; |
||
| 123 | } |
||
| 124 | |||
| 125 | protected function getParameters(IEvent $event): array { |
||
| 126 | $parameters = $event->getSubjectParameters(); |
||
| 127 | if (isset($parameters['announcement'])) { |
||
| 128 | return $parameters; |
||
| 129 | } |
||
| 130 | |||
| 131 | // Legacy fallback from before 3.4.0 |
||
| 132 | return [ |
||
| 133 | 'author' => $parameters[0] ?? '', |
||
| 134 | 'announcement' => $event->getObjectId(), |
||
| 135 | ]; |
||
| 136 | } |
||
| 137 | |||
| 138 | protected function setSubjects(IEvent $event, string $subject, array $parameters) { |
||
| 139 | $placeholders = $replacements = []; |
||
| 140 | foreach ($parameters as $placeholder => $parameter) { |
||
| 141 | $placeholders[] = '{' . $placeholder . '}'; |
||
| 142 | $replacements[] = $parameter['name']; |
||
| 143 | } |
||
| 144 | |||
| 145 | $event->setParsedSubject(str_replace($placeholders, $replacements, $subject)) |
||
| 146 | ->setRichSubject($subject, $parameters); |
||
| 147 | } |
||
| 148 | |||
| 149 | protected function getParsedParameters(array $parameters, Announcement $announcement = null): array { |
||
| 150 | if ($announcement !== null) { |
||
| 151 | return [ |
||
| 152 | 'actor' => $this->generateUserParameter($parameters['author']), |
||
| 153 | 'announcement' => $this->generateAnnouncementParameter($announcement), |
||
| 154 | ]; |
||
| 155 | } |
||
| 156 | |||
| 157 | return [ |
||
| 158 | 'actor' => $this->generateUserParameter($parameters['author']), |
||
| 159 | ]; |
||
| 160 | } |
||
| 161 | |||
| 162 | protected function generateAnnouncementParameter(Announcement $announcement): array { |
||
| 163 | return [ |
||
| 164 | 'type' => 'announcement', |
||
| 165 | 'id' => $announcement->getId(), |
||
| 166 | 'name' => $announcement->getSubject(), |
||
| 167 | 'link' => $this->url->linkToRouteAbsolute('announcementcenter.page.index', [ |
||
| 168 | 'announcement' => $announcement->getId(), |
||
| 169 | ]), |
||
| 170 | ]; |
||
| 171 | } |
||
| 172 | |||
| 173 | protected function generateUserParameter(string $uid): array { |
||
| 174 | if (!isset($this->displayNames[$uid])) { |
||
| 175 | $this->displayNames[$uid] = $this->getDisplayName($uid); |
||
| 176 | } |
||
| 177 | |||
| 178 | return [ |
||
| 179 | 'type' => 'user', |
||
| 180 | 'id' => $uid, |
||
| 181 | 'name' => $this->displayNames[$uid], |
||
| 182 | ]; |
||
| 183 | } |
||
| 184 | |||
| 185 | protected function getDisplayName(string $uid): string { |
||
| 186 | $user = $this->userManager->get($uid); |
||
| 187 | if ($user instanceof IUser) { |
||
|
0 ignored issues
–
show
The class
OCP\IUser does not exist. Did you forget a USE statement, or did you not list all dependencies?
This error could be the result of: 1. Missing dependenciesPHP Analyzer uses your Are you sure this class is defined by one of your dependencies, or did you maybe
not list a dependency in either the 2. Missing use statementPHP does not complain about undefined classes in if ($x instanceof DoesNotExist) {
// Do something.
}
If you have not tested against this specific condition, such errors might go unnoticed. Loading history...
|
|||
| 188 | return $user->getDisplayName(); |
||
| 189 | } |
||
| 190 | return $uid; |
||
| 191 | } |
||
| 192 | } |
||
| 193 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.