]> git.mxchange.org Git - friendica.git/blob - include/enotify.php
Move "App::getSenderEmailAddress()" to "Emailer::getSiteEmailAddress()"
[friendica.git] / include / enotify.php
1 <?php
2 /**
3  * @file include/enotify.php
4  */
5
6 use Friendica\Content\Text\BBCode;
7 use Friendica\Core\Hook;
8 use Friendica\Core\Logger;
9 use Friendica\Core\Renderer;
10 use Friendica\Core\System;
11 use Friendica\Database\DBA;
12 use Friendica\DI;
13 use Friendica\Model\Item;
14 use Friendica\Model\ItemContent;
15 use Friendica\Model\Notify;
16 use Friendica\Model\User;
17 use Friendica\Model\UserItem;
18 use Friendica\Object\Email;
19 use Friendica\Protocol\Activity;
20
21 /**
22  * Creates a notification entry and possibly sends a mail
23  *
24  * @param array $params Array with the elements:
25  *                      uid, item, parent, type, otype, verb, event,
26  *                      link, subject, body, to_name, to_email, source_name,
27  *                      source_link, activity, preamble, notify_flags,
28  *                      language, show_in_notification_page
29  * @return bool
30  * @throws \Friendica\Network\HTTPException\InternalServerErrorException
31  */
32 function notification($params)
33 {
34         $a = DI::app();
35
36         // Temporary logging for finding the origin
37         if (!isset($params['uid'])) {
38                 Logger::notice('Missing parameters "uid".', ['params' => $params, 'callstack' => System::callstack()]);
39         }
40
41         // Ensure that the important fields are set at any time
42         $fields = ['notify-flags', 'language', 'username', 'email'];
43         $user = DBA::selectFirst('user', $fields, ['uid' => $params['uid']]);
44
45         if (!DBA::isResult($user)) {
46                 Logger::error('Unknown user', ['uid' =>  $params['uid']]);
47                 return false;
48         }
49
50         $params['notify_flags'] = ($params['notify_flags'] ?? '') ?: $user['notify-flags'];
51         $params['language']     = ($params['language']     ?? '') ?: $user['language'];
52         $params['to_name']      = ($params['to_name']      ?? '') ?: $user['username'];
53         $params['to_email']     = ($params['to_email']     ?? '') ?: $user['email'];
54
55         // from here on everything is in the recipients language
56         $l10n = DI::l10n()->withLang($params['language']);
57
58         $banner = $l10n->t('Friendica Notification');
59         $product = FRIENDICA_PLATFORM;
60         $siteurl = DI::baseUrl()->get(true);
61         $thanks = $l10n->t('Thank You,');
62         $sitename = DI::config()->get('config', 'sitename');
63         if (DI::config()->get('config', 'admin_name')) {
64                 $site_admin = $l10n->t('%1$s, %2$s Administrator', DI::config()->get('config', 'admin_name'), $sitename);
65         } else {
66                 $site_admin = $l10n->t('%s Administrator', $sitename);
67         }
68
69         $sender_name = $sitename;
70         $hostname = DI::baseUrl()->getHostname();
71         if (strpos($hostname, ':')) {
72                 $hostname = substr($hostname, 0, strpos($hostname, ':'));
73         }
74
75         $sender_email = DI::emailer()->getSiteEmailAddress();
76
77         $user = DBA::selectFirst('user', ['nickname', 'page-flags'],
78                 ['uid' => $params['uid']]);
79
80         // There is no need to create notifications for forum accounts
81         if (!DBA::isResult($user) || in_array($user["page-flags"], [User::PAGE_FLAGS_COMMUNITY, User::PAGE_FLAGS_PRVGROUP])) {
82                 return false;
83         }
84         $nickname = $user["nickname"];
85
86         // with $params['show_in_notification_page'] == false, the notification isn't inserted into
87         // the database, and an email is sent if applicable.
88         // default, if not specified: true
89         $show_in_notification_page = isset($params['show_in_notification_page']) ? $params['show_in_notification_page'] : true;
90
91         $additional_mail_header = "";
92         $additional_mail_header .= "Precedence: list\n";
93         $additional_mail_header .= "X-Friendica-Host: ".$hostname."\n";
94         $additional_mail_header .= "X-Friendica-Account: <".$nickname."@".$hostname.">\n";
95         $additional_mail_header .= "X-Friendica-Platform: ".FRIENDICA_PLATFORM."\n";
96         $additional_mail_header .= "X-Friendica-Version: ".FRIENDICA_VERSION."\n";
97         $additional_mail_header .= "List-ID: <notification.".$hostname.">\n";
98         $additional_mail_header .= "List-Archive: <".DI::baseUrl()."/notifications/system>\n";
99
100         if (array_key_exists('item', $params)) {
101                 $title = $params['item']['title'];
102                 $body = $params['item']['body'];
103         } else {
104                 $title = $body = '';
105         }
106
107         if (isset($params['item']['id'])) {
108                 $item_id = $params['item']['id'];
109         } else {
110                 $item_id = 0;
111         }
112
113         if (isset($params['parent'])) {
114                 $parent_id = $params['parent'];
115         } else {
116                 $parent_id = 0;
117         }
118
119         $epreamble = '';
120         $preamble  = '';
121         $subject   = '';
122         $sitelink  = '';
123         $tsitelink = '';
124         $hsitelink = '';
125         $itemlink  = '';
126
127         if ($params['type'] == NOTIFY_MAIL) {
128                 $itemlink = $siteurl.'/message/'.$params['item']['id'];
129                 $params["link"] = $itemlink;
130
131                 $subject = $l10n->t('[Friendica:Notify] New mail received at %s', $sitename);
132
133                 $preamble = $l10n->t('%1$s sent you a new private message at %2$s.', $params['source_name'], $sitename);
134                 $epreamble = $l10n->t('%1$s sent you %2$s.', '[url='.$params['source_link'].']'.$params['source_name'].'[/url]', '[url=' . $itemlink . ']' . $l10n->t('a private message').'[/url]');
135
136                 $sitelink = $l10n->t('Please visit %s to view and/or reply to your private messages.');
137                 $tsitelink = sprintf($sitelink, $siteurl.'/message/'.$params['item']['id']);
138                 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'/message/'.$params['item']['id'].'">'.$sitename.'</a>');
139         }
140
141         if ($params['type'] == NOTIFY_COMMENT || $params['type'] == NOTIFY_TAGSELF) {
142                 $thread = Item::selectFirstThreadForUser($params['uid'], ['ignored'], ['iid' => $parent_id, 'deleted' => false]);
143                 if (DBA::isResult($thread) && $thread['ignored']) {
144                         Logger::log('Thread ' . $parent_id . ' will be ignored', Logger::DEBUG);
145                         return false;
146                 }
147
148                 // Check to see if there was already a tag notify or comment notify for this post.
149                 // If so don't create a second notification
150                 /// @todo In the future we should store the notification with the highest "value" and replace notifications
151                 $condition = ['type' => [NOTIFY_TAGSELF, NOTIFY_COMMENT, NOTIFY_SHARE],
152                         'link' => $params['link'], 'uid' => $params['uid']];
153                 if (DBA::exists('notify', $condition)) {
154                         return false;
155                 }
156
157                 // if it's a post figure out who's post it is.
158                 $item = null;
159                 if ($params['otype'] === Notify::OTYPE_ITEM && $parent_id) {
160                         $item = Item::selectFirstForUser($params['uid'], Item::ITEM_FIELDLIST, ['id' => $parent_id, 'deleted' => false]);
161                 }
162
163                 if (empty($item)) {
164                         return false;
165                 }
166
167                 $item_post_type = Item::postType($item);
168
169                 $content = ItemContent::getPlaintextPost($item, 70);
170                 if (!empty($content['text'])) {
171                         $title = '"' . trim(str_replace("\n", " ", $content['text'])) . '"';
172                 } else {
173                         $title = '';
174                 }
175
176                 // First go for the general message
177
178                 // "George Bull's post"
179                 if ($params['activity']['origin_comment']) {
180                         $message = '%1$s replied to you on %2$s\'s %3$s %4$s';
181                 } elseif ($params['activity']['explicit_tagged']) {
182                         $message = '%1$s tagged you on %2$s\'s %3$s %4$s';
183                 } else {
184                         $message = '%1$s commented on %2$s\'s %3$s %4$s';
185                 }
186
187                 $dest_str = $l10n->t($message, $params['source_name'], $item['author-name'], $item_post_type, $title);
188
189                 // Then look for the special cases
190
191                 // "your post"
192                 if ($params['activity']['origin_thread']) {
193                         if ($params['activity']['origin_comment']) {
194                                 $message = '%1$s replied to you on your %2$s %3$s';
195                         } elseif ($params['activity']['explicit_tagged']) {
196                                 $message = '%1$s tagged you on your %2$s %3$s';
197                         } else {
198                                 $message = '%1$s commented on your %2$s %3$s';
199                         }
200
201                         $dest_str = $l10n->t($message, $params['source_name'], $item_post_type, $title);
202                 // "their post"
203                 } elseif ($item['author-link'] == $params['source_link']) {
204                         if ($params['activity']['origin_comment']) {
205                                 $message = '%1$s replied to you on their %2$s %3$s';
206                         } elseif ($params['activity']['explicit_tagged']) {
207                                 $message = '%1$s tagged you on their %2$s %3$s';
208                         } else {
209                                 $message = '%1$s commented on their %2$s %3$s';
210                         }
211
212                         $dest_str = $l10n->t($message, $params['source_name'], $item_post_type, $title);
213                 }
214
215                 // Some mail software relies on subject field for threading.
216                 // So, we cannot have different subjects for notifications of the same thread.
217                 // Before this we have the name of the replier on the subject rendering
218                 // different subjects for messages on the same thread.
219                 if ($params['activity']['explicit_tagged']) {
220                         $subject = $l10n->t('[Friendica:Notify] %s tagged you', $params['source_name']);
221
222                         $preamble = $l10n->t('%1$s tagged you at %2$s', $params['source_name'], $sitename);
223                 } else {
224                         $subject = $l10n->t('[Friendica:Notify] Comment to conversation #%1$d by %2$s', $parent_id, $params['source_name']);
225
226                         $preamble = $l10n->t('%s commented on an item/conversation you have been following.', $params['source_name']);
227                 }
228
229                 $epreamble = $dest_str;
230
231                 $sitelink = $l10n->t('Please visit %s to view and/or reply to the conversation.');
232                 $tsitelink = sprintf($sitelink, $siteurl);
233                 $hsitelink = sprintf($sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
234                 $itemlink =  $params['link'];
235         }
236
237         if ($params['type'] == NOTIFY_WALL) {
238                 $subject = $l10n->t('[Friendica:Notify] %s posted to your profile wall', $params['source_name']);
239
240                 $preamble = $l10n->t('%1$s posted to your profile wall at %2$s', $params['source_name'], $sitename);
241                 $epreamble = $l10n->t('%1$s posted to [url=%2$s]your wall[/url]',
242                         '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
243                         $params['link']
244                 );
245
246                 $sitelink = $l10n->t('Please visit %s to view and/or reply to the conversation.');
247                 $tsitelink = sprintf($sitelink, $siteurl);
248                 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
249                 $itemlink =  $params['link'];
250         }
251
252         if ($params['type'] == NOTIFY_SHARE) {
253                 $subject = $l10n->t('[Friendica:Notify] %s shared a new post', $params['source_name']);
254
255                 $preamble = $l10n->t('%1$s shared a new post at %2$s', $params['source_name'], $sitename);
256                 $epreamble = $l10n->t('%1$s [url=%2$s]shared a post[/url].',
257                         '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
258                         $params['link']
259                 );
260
261                 $sitelink = $l10n->t('Please visit %s to view and/or reply to the conversation.');
262                 $tsitelink = sprintf($sitelink, $siteurl);
263                 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
264                 $itemlink =  $params['link'];
265         }
266
267         if ($params['type'] == NOTIFY_POKE) {
268                 $subject = $l10n->t('[Friendica:Notify] %1$s poked you', $params['source_name']);
269
270                 $preamble = $l10n->t('%1$s poked you at %2$s', $params['source_name'], $sitename);
271                 $epreamble = $l10n->t('%1$s [url=%2$s]poked you[/url].',
272                         '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
273                         $params['link']
274                 );
275
276                 $subject = str_replace('poked', $l10n->t($params['activity']), $subject);
277                 $preamble = str_replace('poked', $l10n->t($params['activity']), $preamble);
278                 $epreamble = str_replace('poked', $l10n->t($params['activity']), $epreamble);
279
280                 $sitelink = $l10n->t('Please visit %s to view and/or reply to the conversation.');
281                 $tsitelink = sprintf($sitelink, $siteurl);
282                 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
283                 $itemlink =  $params['link'];
284         }
285
286         if ($params['type'] == NOTIFY_TAGSHARE) {
287                 $itemlink =  $params['link'];
288                 $subject = $l10n->t('[Friendica:Notify] %s tagged your post', $params['source_name']);
289
290                 $preamble = $l10n->t('%1$s tagged your post at %2$s', $params['source_name'], $sitename);
291                 $epreamble = $l10n->t('%1$s tagged [url=%2$s]your post[/url]',
292                         '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
293                         $itemlink
294                 );
295
296                 $sitelink = $l10n->t('Please visit %s to view and/or reply to the conversation.');
297                 $tsitelink = sprintf($sitelink, $siteurl);
298                 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
299         }
300
301         if ($params['type'] == NOTIFY_INTRO) {
302                 $itemlink = $params['link'];
303                 $subject = $l10n->t('[Friendica:Notify] Introduction received');
304
305                 $preamble = $l10n->t('You\'ve received an introduction from \'%1$s\' at %2$s', $params['source_name'], $sitename);
306                 $epreamble = $l10n->t('You\'ve received [url=%1$s]an introduction[/url] from %2$s.',
307                         $itemlink,
308                         '[url='.$params['source_link'].']'.$params['source_name'].'[/url]'
309                 );
310
311                 $body = $l10n->t('You may visit their profile at %s', $params['source_link']);
312
313                 $sitelink = $l10n->t('Please visit %s to approve or reject the introduction.');
314                 $tsitelink = sprintf($sitelink, $siteurl);
315                 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
316
317                 switch ($params['verb']) {
318                         case Activity::FRIEND:
319                                 // someone started to share with user (mostly OStatus)
320                                 $subject = $l10n->t('[Friendica:Notify] A new person is sharing with you');
321
322                                 $preamble = $l10n->t('%1$s is sharing with you at %2$s', $params['source_name'], $sitename);
323                                 $epreamble = $l10n->t('%1$s is sharing with you at %2$s',
324                                         '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
325                                         $sitename
326                                 );
327                                 break;
328                         case Activity::FOLLOW:
329                                 // someone started to follow the user (mostly OStatus)
330                                 $subject = $l10n->t('[Friendica:Notify] You have a new follower');
331
332                                 $preamble = $l10n->t('You have a new follower at %2$s : %1$s', $params['source_name'], $sitename);
333                                 $epreamble = $l10n->t('You have a new follower at %2$s : %1$s',
334                                         '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
335                                         $sitename
336                                 );
337                                 break;
338                         default:
339                                 // ACTIVITY_REQ_FRIEND is default activity for notifications
340                                 break;
341                 }
342         }
343
344         if ($params['type'] == NOTIFY_SUGGEST) {
345                 $itemlink =  $params['link'];
346                 $subject = $l10n->t('[Friendica:Notify] Friend suggestion received');
347
348                 $preamble = $l10n->t('You\'ve received a friend suggestion from \'%1$s\' at %2$s', $params['source_name'], $sitename);
349                 $epreamble = $l10n->t('You\'ve received [url=%1$s]a friend suggestion[/url] for %2$s from %3$s.',
350                         $itemlink,
351                         '[url='.$params['item']['url'].']'.$params['item']['name'].'[/url]',
352                         '[url='.$params['source_link'].']'.$params['source_name'].'[/url]'
353                 );
354
355                 $body = $l10n->t('Name:').' '.$params['item']['name']."\n";
356                 $body .= $l10n->t('Photo:').' '.$params['item']['photo']."\n";
357                 $body .= $l10n->t('You may visit their profile at %s', $params['item']['url']);
358
359                 $sitelink = $l10n->t('Please visit %s to approve or reject the suggestion.');
360                 $tsitelink = sprintf($sitelink, $siteurl);
361                 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
362         }
363
364         if ($params['type'] == NOTIFY_CONFIRM) {
365                 if ($params['verb'] == Activity::FRIEND) { // mutual connection
366                         $itemlink =  $params['link'];
367                         $subject = $l10n->t('[Friendica:Notify] Connection accepted');
368
369                         $preamble = $l10n->t('\'%1$s\' has accepted your connection request at %2$s', $params['source_name'], $sitename);
370                         $epreamble = $l10n->t('%2$s has accepted your [url=%1$s]connection request[/url].',
371                                 $itemlink,
372                                 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]'
373                         );
374
375                         $body =  $l10n->t('You are now mutual friends and may exchange status updates, photos, and email without restriction.');
376
377                         $sitelink = $l10n->t('Please visit %s if you wish to make any changes to this relationship.');
378                         $tsitelink = sprintf($sitelink, $siteurl);
379                         $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
380                 } else { // ACTIVITY_FOLLOW
381                         $itemlink =  $params['link'];
382                         $subject = $l10n->t('[Friendica:Notify] Connection accepted');
383
384                         $preamble = $l10n->t('\'%1$s\' has accepted your connection request at %2$s', $params['source_name'], $sitename);
385                         $epreamble = $l10n->t('%2$s has accepted your [url=%1$s]connection request[/url].',
386                                 $itemlink,
387                                 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]'
388                         );
389
390                         $body =  $l10n->t('\'%1$s\' has chosen to accept you a fan, which restricts some forms of communication - such as private messaging and some profile interactions. If this is a celebrity or community page, these settings were applied automatically.', $params['source_name']);
391                         $body .= "\n\n";
392                         $body .= $l10n->t('\'%1$s\' may choose to extend this into a two-way or more permissive relationship in the future.', $params['source_name']);
393
394                         $sitelink = $l10n->t('Please visit %s  if you wish to make any changes to this relationship.');
395                         $tsitelink = sprintf($sitelink, $siteurl);
396                         $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
397                 }
398         }
399
400         if ($params['type'] == NOTIFY_SYSTEM) {
401                 switch($params['event']) {
402                         case "SYSTEM_REGISTER_REQUEST":
403                                 $itemlink =  $params['link'];
404                                 $subject = $l10n->t('[Friendica System Notify]') . ' ' . $l10n->t('registration request');
405
406                                 $preamble = $l10n->t('You\'ve received a registration request from \'%1$s\' at %2$s', $params['source_name'], $sitename);
407                                 $epreamble = $l10n->t('You\'ve received a [url=%1$s]registration request[/url] from %2$s.',
408                                         $itemlink,
409                                         '[url='.$params['source_link'].']'.$params['source_name'].'[/url]'
410                                 );
411
412                                 $body = $l10n->t("Full Name:    %s\nSite Location:      %s\nLogin Name: %s (%s)",
413                                         $params['source_name'],
414                                         $siteurl, $params['source_mail'],
415                                         $params['source_nick']
416                                 );
417
418                                 $sitelink = $l10n->t('Please visit %s to approve or reject the request.');
419                                 $tsitelink = sprintf($sitelink, $params['link']);
420                                 $hsitelink = sprintf($sitelink, '<a href="'.$params['link'].'">'.$sitename.'</a><br><br>');
421                                 break;
422                         case "SYSTEM_DB_UPDATE_FAIL":
423                                 break;
424                 }
425         }
426
427         $subject .= " (".$nickname."@".$hostname.")";
428
429         $h = [
430                 'params'    => $params,
431                 'subject'   => $subject,
432                 'preamble'  => $preamble,
433                 'epreamble' => $epreamble,
434                 'body'      => $body,
435                 'sitelink'  => $sitelink,
436                 'tsitelink' => $tsitelink,
437                 'hsitelink' => $hsitelink,
438                 'itemlink'  => $itemlink
439         ];
440
441         Hook::callAll('enotify', $h);
442
443         $subject   = $h['subject'];
444
445         $preamble  = $h['preamble'];
446         $epreamble = $h['epreamble'];
447
448         $body      = $h['body'];
449
450         $tsitelink = $h['tsitelink'];
451         $hsitelink = $h['hsitelink'];
452         $itemlink  = $h['itemlink'];
453
454         $notify_id = 0;
455
456         if ($show_in_notification_page) {
457                 $notification = DI::notify()->insert([
458                         'name'       => $params['source_name'] ?? '',
459                         'name_cache' => strip_tags(BBCode::convert($params['source_name'] ?? '')),
460                         'url'        => $params['source_link'] ?? '',
461                         'photo'      => $params['source_photo'] ?? '',
462                         'link'       => $itemlink ?? '',
463                         'uid'        => $params['uid'] ?? 0,
464                         'iid'        => $item_id ?? 0,
465                         'parent'     => $parent_id ?? 0,
466                         'type'       => $params['type'] ?? '',
467                         'verb'       => $params['verb'] ?? '',
468                         'otype'      => $params['otype'] ?? '',
469                 ]);
470
471                 $notification->msg = Renderer::replaceMacros($epreamble, ['$itemlink' => $notification->link]);
472
473                 DI::notify()->update($notification);
474
475                 $itemlink  = DI::baseUrl() . '/notification/' . $notification->id;
476                 $notify_id = $notification->id;
477         }
478
479         // send email notification if notification preferences permit
480         if ((intval($params['notify_flags']) & intval($params['type']))
481                 || $params['type'] == NOTIFY_SYSTEM) {
482
483                 Logger::log('sending notification email');
484
485                 if (isset($params['parent']) && (intval($params['parent']) != 0)) {
486                         $id_for_parent = $params['parent'] . "@" . $hostname;
487
488                         // Is this the first email notification for this parent item and user?
489                         if (!DBA::exists('notify-threads', ['master-parent-item' => $params['parent'], 'receiver-uid' => $params['uid']])) {
490                                 Logger::log("notify_id:" . intval($notify_id) . ", parent: " . intval($params['parent']) . "uid: " . intval($params['uid']), Logger::DEBUG);
491
492                                 $fields = ['notify-id'    => $notify_id, 'master-parent-item' => $params['parent'],
493                                            'receiver-uid' => $params['uid'], 'parent-item' => 0];
494                                 DBA::insert('notify-threads', $fields);
495
496                                 $additional_mail_header .= "Message-ID: <${id_for_parent}>\n";
497                                 $log_msg                = "include/enotify: No previous notification found for this parent:\n" .
498                                                           "  parent: ${params['parent']}\n" . "  uid   : ${params['uid']}\n";
499                                 Logger::log($log_msg, Logger::DEBUG);
500                         } else {
501                                 // If not, just "follow" the thread.
502                                 $additional_mail_header .= "References: <${id_for_parent}>\nIn-Reply-To: <${id_for_parent}>\n";
503                                 Logger::log("There's already a notification for this parent.", Logger::DEBUG);
504                         }
505                 }
506
507                 $textversion = BBCode::toPlaintext($body);
508                 $htmlversion = BBCode::convert($body);
509
510                 $datarray                 = [];
511                 $datarray['banner']       = $banner;
512                 $datarray['product']      = $product;
513                 $datarray['preamble']     = $preamble;
514                 $datarray['sitename']     = $sitename;
515                 $datarray['siteurl']      = $siteurl;
516                 $datarray['type']         = $params['type'];
517                 $datarray['parent']       = $parent_id;
518                 $datarray['source_name']  = $params['source_name'] ?? '';
519                 $datarray['source_link']  = $params['source_link'] ?? '';
520                 $datarray['source_photo'] = $params['source_photo'] ?? '';
521                 $datarray['uid']          = $params['uid'];
522                 $datarray['hsitelink']    = $hsitelink;
523                 $datarray['tsitelink']    = $tsitelink;
524                 $datarray['hitemlink']    = '<a href="' . $itemlink . '">' . $itemlink . '</a>';
525                 $datarray['titemlink']    = $itemlink;
526                 $datarray['thanks']       = $thanks;
527                 $datarray['site_admin']   = $site_admin;
528                 $datarray['title']        = stripslashes($title);
529                 $datarray['htmlversion']  = $htmlversion;
530                 $datarray['textversion']  = $textversion;
531                 $datarray['subject']      = $subject;
532                 $datarray['headers']      = $additional_mail_header;
533
534                 Hook::callAll('enotify_mail', $datarray);
535
536                 // check whether sending post content in email notifications is allowed
537                 $content_allowed = (!DI::config()->get('system', 'enotify_no_content'));
538
539                 // load the template for private message notifications
540                 $tpl             = Renderer::getMarkupTemplate('email/notify/html.tpl');
541                 $email_html_body = Renderer::replaceMacros($tpl, [
542                         '$banner'          => $datarray['banner'],
543                         '$product'         => $datarray['product'],
544                         '$preamble'        => str_replace("\n", "<br>\n", $datarray['preamble']),
545                         '$sitename'        => $datarray['sitename'],
546                         '$siteurl'         => $datarray['siteurl'],
547                         '$source_name'     => $datarray['source_name'],
548                         '$source_link'     => $datarray['source_link'],
549                         '$source_photo'    => $datarray['source_photo'],
550                         '$hsitelink'       => $datarray['hsitelink'],
551                         '$hitemlink'       => $datarray['hitemlink'],
552                         '$thanks'          => $datarray['thanks'],
553                         '$site_admin'      => $datarray['site_admin'],
554                         '$title'           => $datarray['title'],
555                         '$htmlversion'     => $datarray['htmlversion'],
556                         '$content_allowed' => $content_allowed,
557                 ]);
558
559                 // load the template for private message notifications
560                 $tpl             = Renderer::getMarkupTemplate('email/notify/text.tpl');
561                 $email_text_body = Renderer::replaceMacros($tpl, [
562                         '$preamble'        => $datarray['preamble'],
563                         '$tsitelink'       => $datarray['tsitelink'],
564                         '$titemlink'       => $datarray['titemlink'],
565                         '$thanks'          => $datarray['thanks'],
566                         '$site_admin'      => $datarray['site_admin'],
567                         '$title'           => $datarray['title'],
568                         '$textversion'     => $datarray['textversion'],
569                         '$content_allowed' => $content_allowed,
570                 ]);
571
572                 $email = new Email($sender_name, $sender_email, $sender_email, $params['to_email'],
573                         $datarray['subject'], $email_html_body, $email_text_body,
574                         $datarray['headers'], $params['uid']);
575
576                 // use the Emailer class to send the message
577                 return DI::emailer()->send($email);
578         }
579
580         return false;
581 }
582
583 /**
584  * Checks for users who should be notified
585  *
586  * @param int $itemid ID of the item for which the check should be done
587  * @throws \Friendica\Network\HTTPException\InternalServerErrorException
588  */
589 function check_user_notification($itemid) {
590         // fetch all users with notifications
591         $useritems = DBA::select('user-item', ['uid', 'notification-type'], ['iid' => $itemid]);
592         while ($useritem = DBA::fetch($useritems)) {
593                 check_item_notification($itemid, $useritem['uid'], $useritem['notification-type']);
594         }
595         DBA::close($useritems);
596 }
597
598 /**
599  * Checks for item related notifications and sends them
600  *
601  * @param int    $itemid            ID of the item for which the check should be done
602  * @param int    $uid               User ID
603  * @param int    $notification_type Notification bits
604  * @return bool
605  * @throws \Friendica\Network\HTTPException\InternalServerErrorException
606  */
607 function check_item_notification($itemid, $uid, $notification_type) {
608         $fields = ['id', 'mention', 'tag', 'parent', 'title', 'body',
609                 'author-link', 'author-name', 'author-avatar', 'author-id',
610                 'guid', 'parent-uri', 'uri', 'contact-id', 'network'];
611         $condition = ['id' => $itemid, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT], 'deleted' => false];
612         $item = Item::selectFirstForUser($uid, $fields, $condition);
613         if (!DBA::isResult($item)) {
614                 return false;
615         }
616
617         // Generate the notification array
618         $params = [];
619         $params['uid'] = $uid;
620         $params['item'] = $item;
621         $params['parent'] = $item['parent'];
622         $params['link'] = DI::baseUrl() . '/display/' . urlencode($item['guid']);
623         $params['otype'] = 'item';
624         $params['source_name'] = $item['author-name'];
625         $params['source_link'] = $item['author-link'];
626         $params['source_photo'] = $item['author-avatar'];
627
628         // Set the activity flags
629         $params['activity']['explicit_tagged'] = ($notification_type & UserItem::NOTIF_EXPLICIT_TAGGED);
630         $params['activity']['implicit_tagged'] = ($notification_type & UserItem::NOTIF_IMPLICIT_TAGGED);
631         $params['activity']['origin_comment'] = ($notification_type & UserItem::NOTIF_DIRECT_COMMENT);
632         $params['activity']['origin_thread'] = ($notification_type & UserItem::NOTIF_THREAD_COMMENT);
633         $params['activity']['thread_comment'] = ($notification_type & UserItem::NOTIF_COMMENT_PARTICIPATION);
634         $params['activity']['thread_activity'] = ($notification_type & UserItem::NOTIF_ACTIVITY_PARTICIPATION);
635
636         // Tagging a user in a direct post (first comment level) means a direct comment
637         if ($params['activity']['explicit_tagged'] && ($notification_type & UserItem::NOTIF_DIRECT_THREAD_COMMENT)) {
638                 $params['activity']['origin_comment'] = true;
639         }
640
641         if ($notification_type & UserItem::NOTIF_SHARED) {
642                 $params['type'] = NOTIFY_SHARE;
643                 $params['verb'] = Activity::POST;
644         } elseif ($notification_type & UserItem::NOTIF_EXPLICIT_TAGGED) {
645                 $params['type'] = NOTIFY_TAGSELF;
646                 $params['verb'] = Activity::TAG;
647         } elseif ($notification_type & UserItem::NOTIF_IMPLICIT_TAGGED) {
648                 $params['type'] = NOTIFY_COMMENT;
649                 $params['verb'] = Activity::POST;
650         } elseif ($notification_type & UserItem::NOTIF_THREAD_COMMENT) {
651                 $params['type'] = NOTIFY_COMMENT;
652                 $params['verb'] = Activity::POST;
653         } elseif ($notification_type & UserItem::NOTIF_DIRECT_COMMENT) {
654                 $params['type'] = NOTIFY_COMMENT;
655                 $params['verb'] = Activity::POST;
656         } elseif ($notification_type & UserItem::NOTIF_COMMENT_PARTICIPATION) {
657                 $params['type'] = NOTIFY_COMMENT;
658                 $params['verb'] = Activity::POST;
659         } elseif ($notification_type & UserItem::NOTIF_ACTIVITY_PARTICIPATION) {
660                 $params['type'] = NOTIFY_COMMENT;
661                 $params['verb'] = Activity::POST;
662         } else {
663                 return false;
664         }
665
666         notification($params);
667 }