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 | /** |
||
3 | * @author Joas Schilling <[email protected]> |
||
4 | * |
||
5 | * @copyright Copyright (c) 2016, ownCloud, Inc. |
||
6 | * @license AGPL-3.0 |
||
7 | * |
||
8 | * This code is free software: you can redistribute it and/or modify |
||
9 | * it under the terms of the GNU Affero General Public License, version 3, |
||
10 | * as published by the Free Software Foundation. |
||
11 | * |
||
12 | * This program is distributed in the hope that it will be useful, |
||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
15 | * GNU Affero General Public License for more details. |
||
16 | * |
||
17 | * You should have received a copy of the GNU Affero General Public License, version 3, |
||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/> |
||
19 | * |
||
20 | */ |
||
21 | |||
22 | namespace OCA\Activity; |
||
23 | |||
24 | use OCP\Activity\IManager; |
||
25 | use OCP\Defaults; |
||
26 | use OCP\IDateTimeFormatter; |
||
27 | use OCP\IDBConnection; |
||
28 | use OCP\IURLGenerator; |
||
29 | use OCP\IUser; |
||
30 | use OCP\IUserManager; |
||
31 | use OCP\Mail\IMailer; |
||
32 | use OCP\Template; |
||
33 | use OCP\Util; |
||
34 | |||
35 | /** |
||
36 | * Class MailQueueHandler |
||
37 | * Gets the users from the database and |
||
38 | * |
||
39 | * @package OCA\Activity |
||
40 | */ |
||
41 | class MailQueueHandler { |
||
42 | /** Number of entries we want to list in the email */ |
||
43 | const ENTRY_LIMIT = 200; |
||
44 | |||
45 | /** @var array */ |
||
46 | protected $languages; |
||
47 | |||
48 | /** @var string */ |
||
49 | protected $senderAddress; |
||
50 | |||
51 | /** @var string */ |
||
52 | protected $senderName; |
||
53 | |||
54 | /** @var IDateTimeFormatter */ |
||
55 | protected $dateFormatter; |
||
56 | |||
57 | /** @var DataHelper */ |
||
58 | protected $dataHelper; |
||
59 | |||
60 | /** @var IDBConnection */ |
||
61 | protected $connection; |
||
62 | |||
63 | /** @var IMailer */ |
||
64 | protected $mailer; |
||
65 | |||
66 | /** @var IURLGenerator */ |
||
67 | protected $urlGenerator; |
||
68 | |||
69 | /** @var IUserManager */ |
||
70 | protected $userManager; |
||
71 | |||
72 | /** @var IManager */ |
||
73 | protected $activityManager; |
||
74 | |||
75 | /** |
||
76 | * Constructor |
||
77 | * |
||
78 | * @param IDateTimeFormatter $dateFormatter |
||
79 | * @param IDBConnection $connection |
||
80 | * @param DataHelper $dataHelper |
||
81 | * @param IMailer $mailer |
||
82 | * @param IURLGenerator $urlGenerator |
||
83 | * @param IUserManager $userManager |
||
84 | * @param IManager $activityManager |
||
85 | */ |
||
86 | 10 | View Code Duplication | public function __construct(IDateTimeFormatter $dateFormatter, |
0 ignored issues
–
show
|
|||
87 | IDBConnection $connection, |
||
88 | DataHelper $dataHelper, |
||
89 | IMailer $mailer, |
||
90 | IURLGenerator $urlGenerator, |
||
91 | IUserManager $userManager, |
||
92 | IManager $activityManager) { |
||
93 | 10 | $this->dateFormatter = $dateFormatter; |
|
94 | 10 | $this->connection = $connection; |
|
95 | 10 | $this->dataHelper = $dataHelper; |
|
96 | 10 | $this->mailer = $mailer; |
|
97 | 10 | $this->urlGenerator = $urlGenerator; |
|
98 | 10 | $this->userManager = $userManager; |
|
99 | 10 | $this->activityManager = $activityManager; |
|
100 | 10 | } |
|
101 | |||
102 | /** |
||
103 | * Get the users we want to send an email to |
||
104 | * |
||
105 | * @param int|null $limit |
||
106 | * @param int $latestSend |
||
107 | * @return array |
||
108 | */ |
||
109 | 7 | public function getAffectedUsers($limit, $latestSend) { |
|
110 | 7 | $limit = (!$limit) ? null : (int) $limit; |
|
111 | |||
112 | 7 | $query = $this->connection->prepare( |
|
113 | 'SELECT `amq_affecteduser`, `email`, MIN(`amq_latest_send`) AS `amq_trigger_time` ' |
||
114 | . ' FROM `*PREFIX*activity_mq` ' |
||
115 | . ' LEFT JOIN `*PREFIX*accounts` ON `user_id` = `amq_affecteduser` ' |
||
116 | . ' WHERE `amq_latest_send` < ? ' |
||
117 | . ' GROUP BY `amq_affecteduser`, `email` ' |
||
118 | 7 | . ' ORDER BY `amq_trigger_time` ASC', |
|
119 | 7 | $limit); |
|
120 | 7 | $query->execute([$latestSend]); |
|
121 | |||
122 | 7 | $affectedUsers = []; |
|
123 | 7 | while ($row = $query->fetch()) { |
|
124 | 6 | $affectedUsers[] = [ |
|
125 | 6 | 'uid' => $row['amq_affecteduser'], |
|
126 | 6 | 'email' => $row['email'] |
|
127 | ]; |
||
128 | } |
||
129 | |||
130 | 7 | return $affectedUsers; |
|
131 | } |
||
132 | |||
133 | /** |
||
134 | * Get all items for the user we want to send an email to |
||
135 | * |
||
136 | * @param string $affectedUser |
||
137 | * @param int $maxTime |
||
138 | * @param int $maxNumItems |
||
139 | * @return array [data of the first max. 200 entries, total number of entries] |
||
140 | */ |
||
141 | 7 | protected function getItemsForUser($affectedUser, $maxTime, $maxNumItems = self::ENTRY_LIMIT) { |
|
142 | 7 | $query = $this->connection->prepare( |
|
143 | 'SELECT * ' |
||
144 | . ' FROM `*PREFIX*activity_mq` ' |
||
145 | . ' WHERE `amq_timestamp` <= ? ' |
||
146 | . ' AND `amq_affecteduser` = ? ' |
||
147 | 7 | . ' ORDER BY `amq_timestamp` ASC', |
|
148 | 7 | $maxNumItems |
|
149 | ); |
||
150 | 7 | $query->execute([(int) $maxTime, $affectedUser]); |
|
151 | |||
152 | 7 | $activities = []; |
|
153 | 7 | while ($row = $query->fetch()) { |
|
154 | 7 | $activities[] = $row; |
|
155 | } |
||
156 | |||
157 | 7 | if (isset($activities[$maxNumItems - 1])) { |
|
158 | // Reached the limit, run a query to get the actual count. |
||
159 | 1 | $query = $this->connection->prepare( |
|
160 | 'SELECT COUNT(*) AS `actual_count`' |
||
161 | . ' FROM `*PREFIX*activity_mq` ' |
||
162 | . ' WHERE `amq_timestamp` <= ? ' |
||
163 | 1 | . ' AND `amq_affecteduser` = ?' |
|
164 | ); |
||
165 | 1 | $query->execute([(int) $maxTime, $affectedUser]); |
|
166 | |||
167 | 1 | $row = $query->fetch(); |
|
168 | 1 | return [$activities, $row['actual_count'] - $maxNumItems]; |
|
169 | } else { |
||
170 | 7 | return [$activities, 0]; |
|
171 | } |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Get a language object for a specific language |
||
176 | * |
||
177 | * @param string $lang Language identifier |
||
178 | * @return \OCP\IL10N Language object of $lang |
||
179 | */ |
||
180 | 1 | protected function getLanguage($lang) { |
|
181 | 1 | if (!isset($this->languages[$lang])) { |
|
182 | 1 | $this->languages[$lang] = Util::getL10N('activity', $lang); |
|
183 | } |
||
184 | |||
185 | 1 | return $this->languages[$lang]; |
|
186 | } |
||
187 | |||
188 | /** |
||
189 | * Get the sender data |
||
190 | * @param string $setting Either `email` or `name` |
||
191 | * @return string |
||
192 | */ |
||
193 | 1 | protected function getSenderData($setting) { |
|
194 | 1 | if (empty($this->senderAddress)) { |
|
195 | 1 | $this->senderAddress = Util::getDefaultEmailAddress('no-reply'); |
|
196 | } |
||
197 | 1 | if (empty($this->senderName)) { |
|
198 | 1 | $defaults = new Defaults(); |
|
199 | 1 | $this->senderName = $defaults->getName(); |
|
200 | } |
||
201 | |||
202 | 1 | if ($setting === 'email') { |
|
203 | 1 | return $this->senderAddress; |
|
204 | } |
||
205 | 1 | return $this->senderName; |
|
206 | } |
||
207 | |||
208 | /** |
||
209 | * Send a notification to one user |
||
210 | * |
||
211 | * @param string $userName Username of the recipient |
||
212 | * @param string $email Email address of the recipient |
||
213 | * @param string $lang Selected language of the recipient |
||
214 | * @param string $timezone Selected timezone of the recipient |
||
215 | * @param int $maxTime |
||
216 | */ |
||
217 | 1 | public function sendEmailToUser($userName, $email, $lang, $timezone, $maxTime) { |
|
218 | 1 | $user = $this->userManager->get($userName); |
|
219 | 1 | 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. ![]() |
|||
220 | 1 | return; |
|
221 | } |
||
222 | |||
223 | 1 | list($mailData, $skippedCount) = $this->getItemsForUser($userName, $maxTime); |
|
224 | |||
225 | 1 | $l = $this->getLanguage($lang); |
|
226 | 1 | $parser = new PlainTextParser($l); |
|
227 | 1 | $this->dataHelper->setUser($userName); |
|
228 | 1 | $this->dataHelper->setL10n($l); |
|
229 | 1 | $this->activityManager->setCurrentUserId($userName); |
|
230 | |||
231 | 1 | $activityList = []; |
|
232 | 1 | foreach ($mailData as $activity) { |
|
233 | 1 | $event = $this->activityManager->generateEvent(); |
|
234 | 1 | $event->setApp($activity['amq_appid']) |
|
235 | 1 | ->setType($activity['amq_type']) |
|
236 | 1 | ->setTimestamp($activity['amq_timestamp']) |
|
237 | 1 | ->setSubject($activity['amq_subject'], []); |
|
238 | |||
239 | 1 | $relativeDateTime = $this->dateFormatter->formatDateTimeRelativeDay( |
|
240 | 1 | $activity['amq_timestamp'], |
|
241 | 1 | 'long', 'medium', |
|
242 | 1 | new \DateTimeZone($timezone), $l |
|
243 | ); |
||
244 | |||
245 | 1 | $activityList[] = [ |
|
246 | 1 | $parser->parseMessage( |
|
247 | 1 | $this->dataHelper->translation( |
|
248 | 1 | $activity['amq_appid'], $activity['amq_subject'], $this->dataHelper->getParameters($event, 'subject', $activity['amq_subjectparams']) |
|
249 | ) |
||
250 | ), |
||
251 | 1 | $relativeDateTime, |
|
252 | ]; |
||
253 | } |
||
254 | |||
255 | 1 | $alttext = new Template('activity', 'email.notification', '', false); |
|
256 | 1 | $alttext->assign('username', $user->getDisplayName()); |
|
257 | 1 | $alttext->assign('activities', $activityList); |
|
258 | 1 | $alttext->assign('skippedCount', $skippedCount); |
|
259 | 1 | $alttext->assign('owncloud_installation', $this->urlGenerator->getAbsoluteURL('/')); |
|
260 | 1 | $alttext->assign('overwriteL10N', $l); |
|
261 | 1 | $emailText = $alttext->fetchPage(); |
|
262 | |||
263 | 1 | $htmltext = new Template('activity', 'html.notification', '', false); |
|
264 | 1 | $htmltext->assign('username', $user->getDisplayName()); |
|
265 | 1 | $htmltext->assign('activities', $activityList); |
|
266 | 1 | $htmltext->assign('skippedCount', $skippedCount); |
|
267 | 1 | $htmltext->assign('owncloud_installation', $this->urlGenerator->getAbsoluteURL('/')); |
|
268 | 1 | $htmltext->assign('overwriteL10N', $l); |
|
269 | 1 | $htmlText = $htmltext->fetchPage(); |
|
270 | |||
271 | 1 | $message = $this->mailer->createMessage(); |
|
272 | 1 | $message->setTo([$email => $user->getDisplayName()]); |
|
273 | 1 | $message->setSubject((string) $l->t('Activity notification')); |
|
274 | 1 | $message->setHtmlBody($htmlText); |
|
275 | 1 | $message->setPlainBody($emailText); |
|
276 | 1 | $message->setFrom([$this->getSenderData('email') => $this->getSenderData('name')]); |
|
277 | 1 | $this->mailer->send($message); |
|
278 | |||
279 | 1 | $this->activityManager->setCurrentUserId(null); |
|
280 | 1 | } |
|
281 | |||
282 | /** |
||
283 | * Delete all entries we dealt with |
||
284 | * |
||
285 | * @param array $affectedUsers |
||
286 | * @param int $maxTime |
||
287 | */ |
||
288 | 6 | public function deleteSentItems(array $affectedUsers, $maxTime) { |
|
289 | // Don't try to delete if we are not considering any users |
||
290 | 6 | if (\count($affectedUsers) === 0) { |
|
291 | 1 | return; |
|
292 | } |
||
293 | 5 | $placeholders = \implode(',', \array_fill(0, \sizeof($affectedUsers), '?')); |
|
294 | 5 | $queryParams = $affectedUsers; |
|
295 | 5 | \array_unshift($queryParams, (int) $maxTime); |
|
296 | |||
297 | 5 | $query = $this->connection->prepare( |
|
298 | 'DELETE FROM `*PREFIX*activity_mq` ' |
||
299 | . ' WHERE `amq_timestamp` <= ? ' |
||
300 | 5 | . ' AND `amq_affecteduser` IN (' . $placeholders . ')'); |
|
301 | 5 | $query->execute($queryParams); |
|
302 | 5 | } |
|
303 | } |
||
304 |
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.