3 * @file include/enotify.php
6 use Friendica\Content\Text\BBCode;
7 use Friendica\Core\Config;
8 use Friendica\Core\Hook;
9 use Friendica\Core\L10n;
10 use Friendica\Core\Logger;
11 use Friendica\Core\Renderer;
12 use Friendica\Core\System;
13 use Friendica\Database\DBA;
14 use Friendica\Model\Contact;
16 use Friendica\Model\Item;
17 use Friendica\Model\User;
18 use Friendica\Model\UserItem;
19 use Friendica\Protocol\Activity;
20 use Friendica\Util\DateTimeFormat;
21 use Friendica\Util\Emailer;
22 use Friendica\Util\Strings;
25 * @brief Creates a notification entry and possibly sends a mail
27 * @param array $params Array with the elements:
28 * uid, item, parent, type, otype, verb, event,
29 * link, subject, body, to_name, to_email, source_name,
30 * source_link, activity, preamble, notify_flags,
31 * language, show_in_notification_page
33 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
35 function notification($params)
39 // Temporary logging for finding the origin
40 if (!isset($params['uid'])) {
41 Logger::notice('Missing parameters "uid".', ['params' => $params, 'callstack' => System::callstack()]);
44 // Ensure that the important fields are set at any time
45 $fields = ['notify-flags', 'language', 'username', 'email'];
46 $user = DBA::selectFirst('user', $fields, ['uid' => $params['uid']]);
48 if (!DBA::isResult($user)) {
49 Logger::error('Unknown user', ['uid' => $params['uid']]);
53 $params['notify_flags'] = ($params['notify_flags'] ?? '') ?: $user['notify-flags'];
54 $params['language'] = ($params['language'] ?? '') ?: $user['language'];
55 $params['to_name'] = ($params['to_name'] ?? '') ?: $user['username'];
56 $params['to_email'] = ($params['to_email'] ?? '') ?: $user['email'];
58 // from here on everything is in the recipients language
59 $l10n = L10n::withLang($params['language']);
61 $banner = $l10n->t('Friendica Notification');
62 $product = FRIENDICA_PLATFORM;
63 $siteurl = DI::baseUrl()->get(true);
64 $thanks = $l10n->t('Thank You,');
65 $sitename = Config::get('config', 'sitename');
66 if (Config::get('config', 'admin_name')) {
67 $site_admin = $l10n->t('%1$s, %2$s Administrator', Config::get('config', 'admin_name'), $sitename);
69 $site_admin = $l10n->t('%s Administrator', $sitename);
72 $sender_name = $sitename;
73 $hostname = DI::baseUrl()->getHostname();
74 if (strpos($hostname, ':')) {
75 $hostname = substr($hostname, 0, strpos($hostname, ':'));
78 $sender_email = $a->getSenderEmailAddress();
80 if ($params['type'] != SYSTEM_EMAIL) {
81 $user = DBA::selectFirst('user', ['nickname', 'page-flags'],
82 ['uid' => $params['uid']]);
84 // There is no need to create notifications for forum accounts
85 if (!DBA::isResult($user) || in_array($user["page-flags"], [User::PAGE_FLAGS_COMMUNITY, User::PAGE_FLAGS_PRVGROUP])) {
88 $nickname = $user["nickname"];
93 // with $params['show_in_notification_page'] == false, the notification isn't inserted into
94 // the database, and an email is sent if applicable.
95 // default, if not specified: true
96 $show_in_notification_page = isset($params['show_in_notification_page']) ? $params['show_in_notification_page'] : true;
98 $additional_mail_header = "";
99 $additional_mail_header .= "Precedence: list\n";
100 $additional_mail_header .= "X-Friendica-Host: ".$hostname."\n";
101 $additional_mail_header .= "X-Friendica-Account: <".$nickname."@".$hostname.">\n";
102 $additional_mail_header .= "X-Friendica-Platform: ".FRIENDICA_PLATFORM."\n";
103 $additional_mail_header .= "X-Friendica-Version: ".FRIENDICA_VERSION."\n";
104 $additional_mail_header .= "List-ID: <notification.".$hostname.">\n";
105 $additional_mail_header .= "List-Archive: <".DI::baseUrl()."/notifications/system>\n";
107 if (array_key_exists('item', $params)) {
108 $title = $params['item']['title'];
109 $body = $params['item']['body'];
114 if (isset($params['item']['id'])) {
115 $item_id = $params['item']['id'];
120 if (isset($params['parent'])) {
121 $parent_id = $params['parent'];
134 if ($params['type'] == NOTIFY_MAIL) {
135 $itemlink = $siteurl.'/message/'.$params['item']['id'];
136 $params["link"] = $itemlink;
138 $subject = $l10n->t('[Friendica:Notify] New mail received at %s', $sitename);
140 $preamble = $l10n->t('%1$s sent you a new private message at %2$s.', $params['source_name'], $sitename);
141 $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]');
143 $sitelink = $l10n->t('Please visit %s to view and/or reply to your private messages.');
144 $tsitelink = sprintf($sitelink, $siteurl.'/message/'.$params['item']['id']);
145 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'/message/'.$params['item']['id'].'">'.$sitename.'</a>');
148 if ($params['type'] == NOTIFY_COMMENT || $params['type'] == NOTIFY_TAGSELF) {
149 $thread = Item::selectFirstThreadForUser($params['uid'], ['ignored'], ['iid' => $parent_id, 'deleted' => false]);
150 if (DBA::isResult($thread) && $thread['ignored']) {
151 Logger::log('Thread ' . $parent_id . ' will be ignored', Logger::DEBUG);
155 // Check to see if there was already a tag notify or comment notify for this post.
156 // If so don't create a second notification
157 /// @todo In the future we should store the notification with the highest "value" and replace notifications
158 $condition = ['type' => [NOTIFY_TAGSELF, NOTIFY_COMMENT, NOTIFY_SHARE],
159 'link' => $params['link'], 'uid' => $params['uid']];
160 if (DBA::exists('notify', $condition)) {
164 // if it's a post figure out who's post it is.
166 if ($params['otype'] === 'item' && $parent_id) {
167 $item = Item::selectFirstForUser($params['uid'], Item::ITEM_FIELDLIST, ['id' => $parent_id, 'deleted' => false]);
174 $item_post_type = Item::postType($item);
175 $itemlink = $item['plink'];
178 if ($item['author-link'] == $params['source_link']) {
179 if ($params['activity']['explicit_tagged']) {
180 $dest_str = $l10n->t('%1$s tagged you on [url=%2$s]their %3$s[/url]',
181 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
185 } elseif ($params['activity']['origin_comment']) {
186 $dest_str = $l10n->t('%1$s answered you on [url=%2$s]their %3$s[/url]',
187 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
192 $dest_str = $l10n->t('%1$s commented on [url=%2$s]their %3$s[/url]',
193 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
199 } elseif ($params['activity']['origin_thread']) {
200 if ($params['activity']['explicit_tagged']) {
201 $dest_str = $l10n->t('%1$s tagged you on [url=%2$s]your %3$s[/url]',
202 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
206 } elseif ($params['activity']['origin_comment']) {
207 $dest_str = $l10n->t('%1$s answered you on [url=%2$s]your %3$s[/url]',
208 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
213 $dest_str = $l10n->t('%1$s commented on [url=%2$s]your %3$s[/url]',
214 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
220 } elseif ($params['activity']['explicit_tagged']) {
221 $dest_str = $l10n->t('%1$s tagged you on [url=%2$s]a %3$s[/url]',
222 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
226 } elseif ($params['activity']['origin_comment']) {
227 $dest_str = $l10n->t('%1$s answered you on [url=%2$s]a %3$s[/url]',
228 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
233 $dest_str = $l10n->t('%1$s commented on [url=%2$s]a %3$s[/url]',
234 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
240 // Some mail software relies on subject field for threading.
241 // So, we cannot have different subjects for notifications of the same thread.
242 // Before this we have the name of the replier on the subject rendering
243 // different subjects for messages on the same thread.
244 if ($params['activity']['explicit_tagged']) {
245 $subject = $l10n->t('[Friendica:Notify] %s tagged you', $params['source_name']);
247 $preamble = $l10n->t('%1$s tagged you at %2$s', $params['source_name'], $sitename);
249 $subject = $l10n->t('[Friendica:Notify] Comment to conversation #%1$d by %2$s', $parent_id, $params['source_name']);
251 $preamble = $l10n->t('%s commented on an item/conversation you have been following.', $params['source_name']);
254 $epreamble = $dest_str;
256 $sitelink = $l10n->t('Please visit %s to view and/or reply to the conversation.');
257 $tsitelink = sprintf($sitelink, $siteurl);
258 $hsitelink = sprintf($sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
259 $itemlink = $params['link'];
262 if ($params['type'] == NOTIFY_WALL) {
263 $subject = $l10n->t('[Friendica:Notify] %s posted to your profile wall', $params['source_name']);
265 $preamble = $l10n->t('%1$s posted to your profile wall at %2$s', $params['source_name'], $sitename);
266 $epreamble = $l10n->t('%1$s posted to [url=%2$s]your wall[/url]',
267 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
271 $sitelink = $l10n->t('Please visit %s to view and/or reply to the conversation.');
272 $tsitelink = sprintf($sitelink, $siteurl);
273 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
274 $itemlink = $params['link'];
277 if ($params['type'] == NOTIFY_SHARE) {
278 $subject = $l10n->t('[Friendica:Notify] %s shared a new post', $params['source_name']);
280 $preamble = $l10n->t('%1$s shared a new post at %2$s', $params['source_name'], $sitename);
281 $epreamble = $l10n->t('%1$s [url=%2$s]shared a post[/url].',
282 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
286 $sitelink = $l10n->t('Please visit %s to view and/or reply to the conversation.');
287 $tsitelink = sprintf($sitelink, $siteurl);
288 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
289 $itemlink = $params['link'];
292 if ($params['type'] == NOTIFY_POKE) {
293 $subject = $l10n->t('[Friendica:Notify] %1$s poked you', $params['source_name']);
295 $preamble = $l10n->t('%1$s poked you at %2$s', $params['source_name'], $sitename);
296 $epreamble = $l10n->t('%1$s [url=%2$s]poked you[/url].',
297 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
301 $subject = str_replace('poked', $l10n->t($params['activity']), $subject);
302 $preamble = str_replace('poked', $l10n->t($params['activity']), $preamble);
303 $epreamble = str_replace('poked', $l10n->t($params['activity']), $epreamble);
305 $sitelink = $l10n->t('Please visit %s to view and/or reply to the conversation.');
306 $tsitelink = sprintf($sitelink, $siteurl);
307 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
308 $itemlink = $params['link'];
311 if ($params['type'] == NOTIFY_TAGSHARE) {
312 $itemlink = $params['link'];
313 $subject = $l10n->t('[Friendica:Notify] %s tagged your post', $params['source_name']);
315 $preamble = $l10n->t('%1$s tagged your post at %2$s', $params['source_name'], $sitename);
316 $epreamble = $l10n->t('%1$s tagged [url=%2$s]your post[/url]',
317 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
321 $sitelink = $l10n->t('Please visit %s to view and/or reply to the conversation.');
322 $tsitelink = sprintf($sitelink, $siteurl);
323 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
326 if ($params['type'] == NOTIFY_INTRO) {
327 $itemlink = $params['link'];
328 $subject = $l10n->t('[Friendica:Notify] Introduction received');
330 $preamble = $l10n->t('You\'ve received an introduction from \'%1$s\' at %2$s', $params['source_name'], $sitename);
331 $epreamble = $l10n->t('You\'ve received [url=%1$s]an introduction[/url] from %2$s.',
333 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]'
336 $body = $l10n->t('You may visit their profile at %s', $params['source_link']);
338 $sitelink = $l10n->t('Please visit %s to approve or reject the introduction.');
339 $tsitelink = sprintf($sitelink, $siteurl);
340 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
342 switch ($params['verb']) {
343 case Activity::FRIEND:
344 // someone started to share with user (mostly OStatus)
345 $subject = $l10n->t('[Friendica:Notify] A new person is sharing with you');
347 $preamble = $l10n->t('%1$s is sharing with you at %2$s', $params['source_name'], $sitename);
348 $epreamble = $l10n->t('%1$s is sharing with you at %2$s',
349 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
353 case Activity::FOLLOW:
354 // someone started to follow the user (mostly OStatus)
355 $subject = $l10n->t('[Friendica:Notify] You have a new follower');
357 $preamble = $l10n->t('You have a new follower at %2$s : %1$s', $params['source_name'], $sitename);
358 $epreamble = $l10n->t('You have a new follower at %2$s : %1$s',
359 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
364 // ACTIVITY_REQ_FRIEND is default activity for notifications
369 if ($params['type'] == NOTIFY_SUGGEST) {
370 $itemlink = $params['link'];
371 $subject = $l10n->t('[Friendica:Notify] Friend suggestion received');
373 $preamble = $l10n->t('You\'ve received a friend suggestion from \'%1$s\' at %2$s', $params['source_name'], $sitename);
374 $epreamble = $l10n->t('You\'ve received [url=%1$s]a friend suggestion[/url] for %2$s from %3$s.',
376 '[url='.$params['item']['url'].']'.$params['item']['name'].'[/url]',
377 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]'
380 $body = $l10n->t('Name:').' '.$params['item']['name']."\n";
381 $body .= $l10n->t('Photo:').' '.$params['item']['photo']."\n";
382 $body .= $l10n->t('You may visit their profile at %s', $params['item']['url']);
384 $sitelink = $l10n->t('Please visit %s to approve or reject the suggestion.');
385 $tsitelink = sprintf($sitelink, $siteurl);
386 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
389 if ($params['type'] == NOTIFY_CONFIRM) {
390 if ($params['verb'] == Activity::FRIEND) { // mutual connection
391 $itemlink = $params['link'];
392 $subject = $l10n->t('[Friendica:Notify] Connection accepted');
394 $preamble = $l10n->t('\'%1$s\' has accepted your connection request at %2$s', $params['source_name'], $sitename);
395 $epreamble = $l10n->t('%2$s has accepted your [url=%1$s]connection request[/url].',
397 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]'
400 $body = $l10n->t('You are now mutual friends and may exchange status updates, photos, and email without restriction.');
402 $sitelink = $l10n->t('Please visit %s if you wish to make any changes to this relationship.');
403 $tsitelink = sprintf($sitelink, $siteurl);
404 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
405 } else { // ACTIVITY_FOLLOW
406 $itemlink = $params['link'];
407 $subject = $l10n->t('[Friendica:Notify] Connection accepted');
409 $preamble = $l10n->t('\'%1$s\' has accepted your connection request at %2$s', $params['source_name'], $sitename);
410 $epreamble = $l10n->t('%2$s has accepted your [url=%1$s]connection request[/url].',
412 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]'
415 $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']);
417 $body .= $l10n->t('\'%1$s\' may choose to extend this into a two-way or more permissive relationship in the future.', $params['source_name']);
419 $sitelink = $l10n->t('Please visit %s if you wish to make any changes to this relationship.');
420 $tsitelink = sprintf($sitelink, $siteurl);
421 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
425 if ($params['type'] == NOTIFY_SYSTEM) {
426 switch($params['event']) {
427 case "SYSTEM_REGISTER_REQUEST":
428 $itemlink = $params['link'];
429 $subject = $l10n->t('[Friendica System Notify]') . ' ' . $l10n->t('registration request');
431 $preamble = $l10n->t('You\'ve received a registration request from \'%1$s\' at %2$s', $params['source_name'], $sitename);
432 $epreamble = $l10n->t('You\'ve received a [url=%1$s]registration request[/url] from %2$s.',
434 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]'
437 $body = $l10n->t("Full Name: %s\nSite Location: %s\nLogin Name: %s (%s)",
438 $params['source_name'],
439 $siteurl, $params['source_mail'],
440 $params['source_nick']
443 $sitelink = $l10n->t('Please visit %s to approve or reject the request.');
444 $tsitelink = sprintf($sitelink, $params['link']);
445 $hsitelink = sprintf($sitelink, '<a href="'.$params['link'].'">'.$sitename.'</a><br><br>');
447 case "SYSTEM_DB_UPDATE_FAIL":
452 if ($params['type'] == SYSTEM_EMAIL) {
453 // not part of the notifications.
454 // it just send a mail to the user.
455 // It will be used by the system to send emails to users (like
456 // password reset, invitations and so) using one look (but without
457 // add a notification to the user, with could be inexistent)
458 if (!isset($params['subject'])) {
459 Logger::warning('subject isn\'t set.', ['type' => $params['type']]);
461 $subject = $params['subject'] ?? '';
463 if (!isset($params['preamble'])) {
464 Logger::warning('preamble isn\'t set.', ['type' => $params['type'], 'subject' => $subject]);
466 $preamble = $params['preamble'] ?? '';
468 if (!isset($params['body'])) {
469 Logger::warning('body isn\'t set.', ['type' => $params['type'], 'subject' => $subject, 'preamble' => $preamble]);
471 $body = $params['body'] ?? '';
473 $show_in_notification_page = false;
476 $subject .= " (".$nickname."@".$hostname.")";
480 'subject' => $subject,
481 'preamble' => $preamble,
482 'epreamble' => $epreamble,
484 'sitelink' => $sitelink,
485 'tsitelink' => $tsitelink,
486 'hsitelink' => $hsitelink,
487 'itemlink' => $itemlink
490 Hook::callAll('enotify', $h);
492 $subject = $h['subject'];
494 $preamble = $h['preamble'];
495 $epreamble = $h['epreamble'];
499 $tsitelink = $h['tsitelink'];
500 $hsitelink = $h['hsitelink'];
501 $itemlink = $h['itemlink'];
505 if ($show_in_notification_page) {
506 Logger::log("adding notification entry", Logger::DEBUG);
508 /// @TODO One statement is enough
510 $datarray['name'] = $params['source_name'];
511 $datarray['name_cache'] = strip_tags(BBCode::convert($params['source_name']));
512 $datarray['url'] = $params['source_link'];
513 $datarray['photo'] = $params['source_photo'];
514 $datarray['date'] = DateTimeFormat::utcNow();
515 $datarray['uid'] = $params['uid'];
516 $datarray['link'] = $itemlink;
517 $datarray['iid'] = $item_id;
518 $datarray['parent'] = $parent_id;
519 $datarray['type'] = $params['type'];
520 $datarray['verb'] = $params['verb'];
521 $datarray['otype'] = $params['otype'];
522 $datarray['abort'] = false;
524 Hook::callAll('enotify_store', $datarray);
526 if ($datarray['abort']) {
530 // create notification entry in DB
531 $fields = ['name' => $datarray['name'], 'url' => $datarray['url'],
532 'photo' => $datarray['photo'], 'date' => $datarray['date'], 'uid' => $datarray['uid'],
533 'link' => $datarray['link'], 'iid' => $datarray['iid'], 'parent' => $datarray['parent'],
534 'type' => $datarray['type'], 'verb' => $datarray['verb'], 'otype' => $datarray['otype'],
535 'name_cache' => $datarray["name_cache"]];
536 DBA::insert('notify', $fields);
538 $notify_id = DBA::lastInsertId();
540 $itemlink = DI::baseUrl().'/notify/view/'.$notify_id;
541 $msg = Renderer::replaceMacros($epreamble, ['$itemlink' => $itemlink]);
542 $msg_cache = format_notification_message($datarray['name_cache'], strip_tags(BBCode::convert($msg)));
544 $fields = ['msg' => $msg, 'msg_cache' => $msg_cache];
545 $condition = ['id' => $notify_id, 'uid' => $params['uid']];
546 DBA::update('notify', $fields, $condition);
549 // send email notification if notification preferences permit
550 if ((intval($params['notify_flags']) & intval($params['type']))
551 || $params['type'] == NOTIFY_SYSTEM
552 || $params['type'] == SYSTEM_EMAIL) {
554 Logger::log('sending notification email');
556 if (isset($params['parent']) && (intval($params['parent']) != 0)) {
557 $id_for_parent = $params['parent']."@".$hostname;
559 // Is this the first email notification for this parent item and user?
560 if (!DBA::exists('notify-threads', ['master-parent-item' => $params['parent'], 'receiver-uid' => $params['uid']])) {
561 Logger::log("notify_id:".intval($notify_id).", parent: ".intval($params['parent'])."uid: ".intval($params['uid']), Logger::DEBUG);
563 $fields = ['notify-id' => $notify_id, 'master-parent-item' => $params['parent'],
564 'receiver-uid' => $params['uid'], 'parent-item' => 0];
565 DBA::insert('notify-threads', $fields);
567 $additional_mail_header .= "Message-ID: <${id_for_parent}>\n";
568 $log_msg = "include/enotify: No previous notification found for this parent:\n".
569 " parent: ${params['parent']}\n"." uid : ${params['uid']}\n";
570 Logger::log($log_msg, Logger::DEBUG);
572 // If not, just "follow" the thread.
573 $additional_mail_header .= "References: <${id_for_parent}>\nIn-Reply-To: <${id_for_parent}>\n";
574 Logger::log("There's already a notification for this parent.", Logger::DEBUG);
578 $textversion = BBCode::toPlaintext($body);
579 $htmlversion = BBCode::convert($body);
582 $datarray['banner'] = $banner;
583 $datarray['product'] = $product;
584 $datarray['preamble'] = $preamble;
585 $datarray['sitename'] = $sitename;
586 $datarray['siteurl'] = $siteurl;
587 $datarray['type'] = $params['type'];
588 $datarray['parent'] = $parent_id;
589 $datarray['source_name'] = $params['source_name'] ?? '';
590 $datarray['source_link'] = $params['source_link'] ?? '';
591 $datarray['source_photo'] = $params['source_photo'] ?? '';
592 $datarray['uid'] = $params['uid'];
593 $datarray['username'] = $params['to_name'] ?? '';
594 $datarray['hsitelink'] = $hsitelink;
595 $datarray['tsitelink'] = $tsitelink;
596 $datarray['hitemlink'] = '<a href="'.$itemlink.'">'.$itemlink.'</a>';
597 $datarray['titemlink'] = $itemlink;
598 $datarray['thanks'] = $thanks;
599 $datarray['site_admin'] = $site_admin;
600 $datarray['title'] = stripslashes($title);
601 $datarray['htmlversion'] = $htmlversion;
602 $datarray['textversion'] = $textversion;
603 $datarray['subject'] = $subject;
604 $datarray['headers'] = $additional_mail_header;
606 Hook::callAll('enotify_mail', $datarray);
608 // check whether sending post content in email notifications is allowed
609 // always true for SYSTEM_EMAIL
610 $content_allowed = ((!Config::get('system', 'enotify_no_content')) || ($params['type'] == SYSTEM_EMAIL));
612 // load the template for private message notifications
613 $tpl = Renderer::getMarkupTemplate('email_notify_html.tpl');
614 $email_html_body = Renderer::replaceMacros($tpl, [
615 '$banner' => $datarray['banner'],
616 '$product' => $datarray['product'],
617 '$preamble' => str_replace("\n", "<br>\n", $datarray['preamble']),
618 '$sitename' => $datarray['sitename'],
619 '$siteurl' => $datarray['siteurl'],
620 '$source_name' => $datarray['source_name'],
621 '$source_link' => $datarray['source_link'],
622 '$source_photo' => $datarray['source_photo'],
623 '$username' => $datarray['username'],
624 '$hsitelink' => $datarray['hsitelink'],
625 '$hitemlink' => $datarray['hitemlink'],
626 '$thanks' => $datarray['thanks'],
627 '$site_admin' => $datarray['site_admin'],
628 '$title' => $datarray['title'],
629 '$htmlversion' => $datarray['htmlversion'],
630 '$content_allowed' => $content_allowed,
633 // load the template for private message notifications
634 $tpl = Renderer::getMarkupTemplate('email_notify_text.tpl');
635 $email_text_body = Renderer::replaceMacros($tpl, [
636 '$banner' => $datarray['banner'],
637 '$product' => $datarray['product'],
638 '$preamble' => $datarray['preamble'],
639 '$sitename' => $datarray['sitename'],
640 '$siteurl' => $datarray['siteurl'],
641 '$source_name' => $datarray['source_name'],
642 '$source_link' => $datarray['source_link'],
643 '$source_photo' => $datarray['source_photo'],
644 '$username' => $datarray['username'],
645 '$tsitelink' => $datarray['tsitelink'],
646 '$titemlink' => $datarray['titemlink'],
647 '$thanks' => $datarray['thanks'],
648 '$site_admin' => $datarray['site_admin'],
649 '$title' => $datarray['title'],
650 '$textversion' => $datarray['textversion'],
651 '$content_allowed' => $content_allowed,
654 // use the Emailer class to send the message
655 return Emailer::send([
656 'uid' => $params['uid'],
657 'fromName' => $sender_name,
658 'fromEmail' => $sender_email,
659 'replyTo' => $sender_email,
660 'toEmail' => $params['to_email'],
661 'messageSubject' => $datarray['subject'],
662 'htmlVersion' => $email_html_body,
663 'textVersion' => $email_text_body,
664 'additionalMailHeader' => $datarray['headers']
672 * @brief Checks for users who should be notified
674 * @param int $itemid ID of the item for which the check should be done
675 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
677 function check_user_notification($itemid) {
678 // fetch all users with notifications
679 $useritems = DBA::select('user-item', ['uid', 'notification-type'], ['iid' => $itemid]);
680 while ($useritem = DBA::fetch($useritems)) {
681 check_item_notification($itemid, $useritem['uid'], $useritem['notification-type']);
683 DBA::close($useritems);
687 * @brief Checks for item related notifications and sends them
689 * @param int $itemid ID of the item for which the check should be done
690 * @param int $uid User ID
691 * @param int $notification_type Notification bits
693 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
695 function check_item_notification($itemid, $uid, $notification_type) {
696 $fields = ['id', 'mention', 'tag', 'parent', 'title', 'body',
697 'author-link', 'author-name', 'author-avatar', 'author-id',
698 'guid', 'parent-uri', 'uri', 'contact-id', 'network'];
699 $condition = ['id' => $itemid, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT], 'deleted' => false];
700 $item = Item::selectFirstForUser($uid, $fields, $condition);
701 if (!DBA::isResult($item)) {
705 // Generate the notification array
707 $params['uid'] = $uid;
708 $params['item'] = $item;
709 $params['parent'] = $item['parent'];
710 $params['link'] = DI::baseUrl() . '/display/' . urlencode($item['guid']);
711 $params['otype'] = 'item';
712 $params['source_name'] = $item['author-name'];
713 $params['source_link'] = $item['author-link'];
714 $params['source_photo'] = $item['author-avatar'];
716 // Set the activity flags
717 $params['activity']['explicit_tagged'] = ($notification_type & UserItem::NOTIF_EXPLICIT_TAGGED);
718 $params['activity']['implicit_tagged'] = ($notification_type & UserItem::NOTIF_IMPLICIT_TAGGED);
719 $params['activity']['origin_comment'] = ($notification_type & UserItem::NOTIF_DIRECT_COMMENT);
720 $params['activity']['origin_thread'] = ($notification_type & UserItem::NOTIF_THREAD_COMMENT);
721 $params['activity']['thread_comment'] = ($notification_type & UserItem::NOTIF_COMMENT_PARTICIPATION);
722 $params['activity']['thread_activity'] = ($notification_type & UserItem::NOTIF_ACTIVITY_PARTICIPATION);
724 if ($notification_type & UserItem::NOTIF_SHARED) {
725 $params['type'] = NOTIFY_SHARE;
726 $params['verb'] = Activity::TAG;
727 } elseif ($notification_type & UserItem::NOTIF_EXPLICIT_TAGGED) {
728 $params['type'] = NOTIFY_TAGSELF;
729 $params['verb'] = Activity::TAG;
730 } elseif ($notification_type & UserItem::NOTIF_IMPLICIT_TAGGED) {
731 $params['type'] = NOTIFY_COMMENT;
732 $params['verb'] = Activity::POST;
733 } elseif ($notification_type & UserItem::NOTIF_THREAD_COMMENT) {
734 $params['type'] = NOTIFY_COMMENT;
735 $params['verb'] = Activity::POST;
736 } elseif ($notification_type & UserItem::NOTIF_DIRECT_COMMENT) {
737 $params['type'] = NOTIFY_COMMENT;
738 $params['verb'] = Activity::POST;
739 } elseif ($notification_type & UserItem::NOTIF_COMMENT_PARTICIPATION) {
740 $params['type'] = NOTIFY_COMMENT;
741 $params['verb'] = Activity::POST;
742 } elseif ($notification_type & UserItem::NOTIF_ACTIVITY_PARTICIPATION) {
743 $params['type'] = NOTIFY_COMMENT;
744 $params['verb'] = Activity::POST;
749 notification($params);
753 * @brief Formats a notification message with the notification author
755 * Replace the name with {0} but ensure to make that only once. The {0} is used
756 * later and prints the name in bold.
758 * @param string $name
759 * @param string $message
760 * @return string Formatted message
762 function format_notification_message($name, $message) {
764 $pos = strpos($message, $name);
769 if ($pos !== false) {
770 $message = substr_replace($message, '{0}', $pos, strlen($name));