2 require_once('include/Emailer.php');
3 require_once('include/email.php');
4 require_once('include/bbcode.php');
5 require_once('include/html2bbcode.php');
8 * @brief Creates a notification entry and possibly sends a mail
10 * @param array $params Array with the elements:
11 uid, item, parent, type, otype, verb, event,
12 link, subject, body, to_name, to_email, source_name,
13 source_link, activity, preamble, notify_flags,
14 language, show_in_notification_page
16 function notification($params) {
20 // from here on everything is in the recipients language
22 push_lang($params['language']);
24 $banner = t('Friendica Notification');
25 $product = FRIENDICA_PLATFORM;
26 $siteurl = $a->get_baseurl(true);
27 $thanks = t('Thank You,');
28 $sitename = $a->config['sitename'];
29 if (!x($a->config['admin_name']))
30 $site_admin = sprintf(t('%s Administrator'), $sitename);
32 $site_admin = sprintf(t('%1$s, %2$s Administrator'), $a->config['admin_name'], $sitename);
36 $sender_name = $sitename;
37 $hostname = $a->get_hostname();
38 if (strpos($hostname, ':'))
39 $hostname = substr($hostname, 0, strpos($hostname, ':'));
41 $sender_email = $a->config['sender_email'];
42 if (empty($sender_email))
43 $sender_email = t('noreply').'@'.$hostname;
45 $user = q("SELECT `nickname` FROM `user` WHERE `uid` = %d", intval($params['uid']));
47 $nickname = $user[0]["nickname"];
49 // with $params['show_in_notification_page'] == false, the notification isn't inserted into
50 // the database, and an email is sent if applicable.
51 // default, if not specified: true
52 $show_in_notification_page = ((x($params, 'show_in_notification_page')) ? $params['show_in_notification_page']:True);
54 $additional_mail_header = "";
55 $additional_mail_header .= "Precedence: list\n";
56 $additional_mail_header .= "X-Friendica-Host: ".$hostname."\n";
57 $additional_mail_header .= "X-Friendica-Account: <".$nickname."@".$hostname.">\n";
58 $additional_mail_header .= "X-Friendica-Platform: ".FRIENDICA_PLATFORM."\n";
59 $additional_mail_header .= "X-Friendica-Version: ".FRIENDICA_VERSION."\n";
60 $additional_mail_header .= "List-ID: <notification.".$hostname.">\n";
61 $additional_mail_header .= "List-Archive: <".$a->get_baseurl()."/notifications/system>\n";
63 if (array_key_exists('item', $params)) {
64 $title = $params['item']['title'];
65 $body = $params['item']['body'];
69 // e.g. "your post", "David's photo", etc.
70 $possess_desc = t('%s <!item_type!>');
72 if (isset($params['item']['id']))
73 $item_id = $params['item']['id'];
77 if (isset($params['parent']))
78 $parent_id = $params['parent'];
82 if ($params['type'] == NOTIFY_MAIL) {
83 $subject = sprintf(t('[Friendica:Notify] New mail received at %s'), $sitename);
85 $preamble = sprintf(t('%1$s sent you a new private message at %2$s.'), $params['source_name'], $sitename);
86 $epreamble = sprintf(t('%1$s sent you %2$s.'), '[url='.$params['source_link'].']'.$params['source_name'].'[/url]', '[url=$itemlink]'.t('a private message').'[/url]');
88 $sitelink = t('Please visit %s to view and/or reply to your private messages.');
89 $tsitelink = sprintf($sitelink, $siteurl.'/message/'.$params['item']['id']);
90 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'/message/'.$params['item']['id'].'">'.$sitename.'</a>');
91 $itemlink = $siteurl.'/message/'.$params['item']['id'];
94 if ($params['type'] == NOTIFY_COMMENT) {
95 $p = q("SELECT `ignored` FROM `thread` WHERE `iid` = %d AND `uid` = %d LIMIT 1",
97 intval($params['uid'])
99 if ($p AND count($p) AND ($p[0]["ignored"])) {
100 logger("Thread ".$parent_id." will be ignored", LOGGER_DEBUG);
104 // Check to see if there was already a tag notify or comment notify for this post.
105 // If so don't create a second notification
108 $p = q("SELECT `id` FROM `notify` WHERE (`type` = %d OR `type` = %d OR `type` = %d) AND `link` = '%s' AND `uid` = %d LIMIT 1",
109 intval(NOTIFY_TAGSELF),
110 intval(NOTIFY_COMMENT),
111 intval(NOTIFY_SHARE),
112 dbesc($params['link']),
113 intval($params['uid'])
115 if ($p and count($p)) {
120 // if it's a post figure out who's post it is.
124 if ($params['otype'] === 'item' && $parent_id) {
125 $p = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
127 intval($params['uid'])
131 $item_post_type = item_post_type($p[0]);
134 $dest_str = sprintf(t('%1$s commented on [url=%2$s]a %3$s[/url]'),
135 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
139 // "George Bull's post"
141 $dest_str = sprintf(t('%1$s commented on [url=%2$s]%3$s\'s %4$s[/url]'),
142 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
144 $p[0]['author-name'],
148 if ($p[0]['owner-name'] == $p[0]['author-name'] && $p[0]['wall'])
149 $dest_str = sprintf(t('%1$s commented on [url=%2$s]your %3$s[/url]'),
150 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
154 // Some mail softwares relies on subject field for threading.
155 // So, we cannot have different subjects for notifications of the same thread.
156 // Before this we have the name of the replier on the subject rendering
157 // differents subjects for messages on the same thread.
159 $subject = sprintf(t('[Friendica:Notify] Comment to conversation #%1$d by %2$s'), $parent_id, $params['source_name']);
161 $preamble = sprintf(t('%s commented on an item/conversation you have been following.'), $params['source_name']);
162 $epreamble = $dest_str;
164 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
165 $tsitelink = sprintf($sitelink, $siteurl);
166 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
167 $itemlink = $params['link'];
170 if ($params['type'] == NOTIFY_WALL) {
171 $subject = sprintf(t('[Friendica:Notify] %s posted to your profile wall'), $params['source_name']);
173 $preamble = sprintf(t('%1$s posted to your profile wall at %2$s'), $params['source_name'], $sitename);
174 $epreamble = sprintf(t('%1$s posted to [url=%2$s]your wall[/url]'),
175 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
178 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
179 $tsitelink = sprintf($sitelink, $siteurl);
180 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
181 $itemlink = $params['link'];
184 if ($params['type'] == NOTIFY_TAGSELF) {
185 $subject = sprintf(t('[Friendica:Notify] %s tagged you'), $params['source_name']);
187 $preamble = sprintf(t('%1$s tagged you at %2$s'), $params['source_name'], $sitename);
188 $epreamble = sprintf(t('%1$s [url=%2$s]tagged you[/url].'),
189 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
192 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
193 $tsitelink = sprintf($sitelink, $siteurl);
194 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
195 $itemlink = $params['link'];
198 if ($params['type'] == NOTIFY_SHARE) {
199 $subject = sprintf(t('[Friendica:Notify] %s shared a new post'), $params['source_name']);
201 $preamble = sprintf(t('%1$s shared a new post at %2$s'), $params['source_name'], $sitename);
202 $epreamble = sprintf(t('%1$s [url=%2$s]shared a post[/url].'),
203 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
206 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
207 $tsitelink = sprintf($sitelink, $siteurl);
208 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
209 $itemlink = $params['link'];
212 if ($params['type'] == NOTIFY_POKE) {
213 $subject = sprintf(t('[Friendica:Notify] %1$s poked you'), $params['source_name']);
215 $preamble = sprintf(t('%1$s poked you at %2$s'), $params['source_name'], $sitename);
216 $epreamble = sprintf(t('%1$s [url=%2$s]poked you[/url].'),
217 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
220 $subject = str_replace('poked', t($params['activity']), $subject);
221 $preamble = str_replace('poked', t($params['activity']), $preamble);
222 $epreamble = str_replace('poked', t($params['activity']), $epreamble);
224 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
225 $tsitelink = sprintf($sitelink, $siteurl);
226 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
227 $itemlink = $params['link'];
230 if ($params['type'] == NOTIFY_TAGSHARE) {
231 $subject = sprintf(t('[Friendica:Notify] %s tagged your post'), $params['source_name']);
233 $preamble = sprintf(t('%1$s tagged your post at %2$s'), $params['source_name'], $sitename);
234 $epreamble = sprintf(t('%1$s tagged [url=%2$s]your post[/url]'),
235 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
238 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
239 $tsitelink = sprintf($sitelink, $siteurl);
240 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
241 $itemlink = $params['link'];
244 if ($params['type'] == NOTIFY_INTRO) {
245 $subject = sprintf(t('[Friendica:Notify] Introduction received'));
247 $preamble = sprintf(t('You\'ve received an introduction from \'%1$s\' at %2$s'), $params['source_name'], $sitename);
248 $epreamble = sprintf(t('You\'ve received [url=%1$s]an introduction[/url] from %2$s.'),
250 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]');
252 $body = sprintf(t('You may visit their profile at %s'), $params['source_link']);
254 $sitelink = t('Please visit %s to approve or reject the introduction.');
255 $tsitelink = sprintf($sitelink, $siteurl);
256 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
257 $itemlink = $params['link'];
259 switch ($params['verb']) {
260 case ACTIVITY_FRIEND:
261 // someone started to share with user (mostly OStatus)
262 $subject = sprintf(t('[Friendica:Notify] A new person is sharing with you'));
264 $preamble = sprintf(t('%1$s is sharing with you at %2$s'), $params['source_name'], $sitename);
265 $epreamble = sprintf(t('%1$s is sharing with you at %2$s'),
266 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
269 case ACTIVITY_FOLLOW:
270 // someone started to follow the user (mostly OStatus)
271 $subject = sprintf(t('[Friendica:Notify] You have a new follower'));
273 $preamble = sprintf(t('You have a new follower at %2$s : %1$s'), $params['source_name'], $sitename);
274 $epreamble = sprintf(t('You have a new follower at %2$s : %1$s'),
275 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]',
279 // ACTIVITY_REQ_FRIEND is default activity for notifications
284 if ($params['type'] == NOTIFY_SUGGEST) {
285 $subject = sprintf(t('[Friendica:Notify] Friend suggestion received'));
287 $preamble = sprintf(t('You\'ve received a friend suggestion from \'%1$s\' at %2$s'), $params['source_name'], $sitename);
288 $epreamble = sprintf(t('You\'ve received [url=%1$s]a friend suggestion[/url] for %2$s from %3$s.'),
290 '[url='.$params['item']['url'].']'.$params['item']['name'].'[/url]',
291 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]');
293 $body = t('Name:').' '.$params['item']['name']."\n";
294 $body .= t('Photo:').' '.$params['item']['photo']."\n";
295 $body .= sprintf(t('You may visit their profile at %s'), $params['item']['url']);
297 $sitelink = t('Please visit %s to approve or reject the suggestion.');
298 $tsitelink = sprintf($sitelink, $siteurl);
299 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
300 $itemlink = $params['link'];
303 if ($params['type'] == NOTIFY_CONFIRM) {
304 if ($params['verb'] == ACTIVITY_FRIEND) { // mutual connection
305 $subject = sprintf(t('[Friendica:Notify] Connection accepted'));
307 $preamble = sprintf(t('\'%1$s\' has accepted your connection request at %2$s'), $params['source_name'], $sitename);
308 $epreamble = sprintf(t('%2$s has accepted your [url=%1$s]connection request[/url].'),
310 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]');
312 $body = t('You are now mutual friends and may exchange status updates, photos, and email without restriction.');
314 $sitelink = t('Please visit %s if you wish to make any changes to this relationship.');
315 $tsitelink = sprintf($sitelink, $siteurl);
316 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
317 $itemlink = $params['link'];
318 } else { // ACTIVITY_FOLLOW
319 $subject = sprintf(t('[Friendica:Notify] Connection accepted'));
321 $preamble = sprintf(t('\'%1$s\' has accepted your connection request at %2$s'), $params['source_name'], $sitename);
322 $epreamble = sprintf(t('%2$s has accepted your [url=%1$s]connection request[/url].'),
324 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]');
326 $body = sprintf(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']);
328 $body .= sprintf(t('\'%1$s\' may choose to extend this into a two-way or more permissive relationship in the future.'), $params['source_name']);
330 $sitelink = t('Please visit %s if you wish to make any changes to this relationship.');
331 $tsitelink = sprintf($sitelink, $siteurl);
332 $hsitelink = sprintf($sitelink, '<a href="'.$siteurl.'">'.$sitename.'</a>');
333 $itemlink = $params['link'];
337 if ($params['type'] == NOTIFY_SYSTEM) {
338 switch($params['event']) {
339 case "SYSTEM_REGISTER_REQUEST":
340 $subject = sprintf(t('[Friendica System:Notify] registration request'));
342 $preamble = sprintf(t('You\'ve received a registration request from \'%1$s\' at %2$s'), $params['source_name'], $sitename);
343 $epreamble = sprintf(t('You\'ve received a [url=%1$s]registration request[/url] from %2$s.'),
345 '[url='.$params['source_link'].']'.$params['source_name'].'[/url]');
347 $body = sprintf(t('Full Name: %1$s\nSite Location: %2$s\nLogin Name: %3$s (%4$s)'),
348 $params['source_name'], $siteurl, $params['source_mail'], $params['source_nick']);
350 $sitelink = t('Please visit %s to approve or reject the request.');
351 $tsitelink = sprintf($sitelink, $params['link']);
352 $hsitelink = sprintf($sitelink, '<a href="'.$params['link'].'">'.$sitename.'</a><br><br>');
353 $itemlink = $params['link'];
355 case "SYSTEM_DB_UPDATE_FAIL":
360 if ($params['type'] == "SYSTEM_EMAIL") {
361 // not part of the notifications.
362 // it just send a mail to the user.
363 // It will be used by the system to send emails to users (like
364 // password reset, invitations and so) using one look (but without
365 // add a notification to the user, with could be inexistent)
366 $subject = $params['subject'];
368 $preamble = $params['preamble'];
370 $body = $params['body'];
376 $show_in_notification_page = false;
379 $subject .= " (".$nickname."@".$hostname.")";
383 'subject' => $subject,
384 'preamble' => $preamble,
385 'epreamble' => $epreamble,
387 'sitelink' => $sitelink,
388 'tsitelink' => $tsitelink,
389 'hsitelink' => $hsitelink,
390 'itemlink' => $itemlink
393 call_hooks('enotify', $h);
395 $subject = $h['subject'];
397 $preamble = $h['preamble'];
398 $epreamble = $h['epreamble'];
402 $sitelink = $h['sitelink'];
403 $tsitelink = $h['tsitelink'];
404 $hsitelink = $h['hsitelink'];
405 $itemlink = $h['itemlink'];
407 if ($show_in_notification_page) {
408 logger("adding notification entry", LOGGER_DEBUG);
411 $hash = random_string();
412 $r = q("SELECT `id` FROM `notify` WHERE `hash` = '%s' LIMIT 1",
416 } while($dups == true);
419 $datarray['hash'] = $hash;
420 $datarray['name'] = $params['source_name'];
421 $datarray['url'] = $params['source_link'];
422 $datarray['photo'] = $params['source_photo'];
423 $datarray['date'] = datetime_convert();
424 $datarray['uid'] = $params['uid'];
425 $datarray['link'] = $itemlink;
426 $datarray['iid'] = $item_id;
427 $datarray['parent'] = $parent_id;
428 $datarray['type'] = $params['type'];
429 $datarray['verb'] = $params['verb'];
430 $datarray['otype'] = $params['otype'];
431 $datarray['abort'] = false;
433 call_hooks('enotify_store', $datarray);
435 if ($datarray['abort']) {
440 // create notification entry in DB
442 $r = q("INSERT INTO `notify` (`hash`, `name`, `url`, `photo`, `date`, `uid`, `link`, `iid`, `parent`, `type`, `verb`, `otype`)
443 values('%s', '%s', '%s', '%s', '%s', %d, '%s', %d, %d, %d, '%s', '%s')",
444 dbesc($datarray['hash']),
445 dbesc($datarray['name']),
446 dbesc($datarray['url']),
447 dbesc($datarray['photo']),
448 dbesc($datarray['date']),
449 intval($datarray['uid']),
450 dbesc($datarray['link']),
451 intval($datarray['iid']),
452 intval($datarray['parent']),
453 intval($datarray['type']),
454 dbesc($datarray['verb']),
455 dbesc($datarray['otype'])
458 $r = q("SELECT `id` FROM `notify` WHERE `hash` = '%s' AND `uid` = %d LIMIT 1",
460 intval($params['uid'])
463 $notify_id = $r[0]['id'];
469 // we seem to have a lot of duplicate comment notifications due to race conditions, mostly from forums
470 // After we've stored everything, look again to see if there are any duplicates and if so remove them
473 $p = q("SELECT `id` FROM `notify` WHERE (`type` = %d OR `type` = %d) AND `link` = '%s' AND `uid` = %d ORDER BY `id`",
474 intval(NOTIFY_TAGSELF),
475 intval(NOTIFY_COMMENT),
476 dbesc($params['link']),
477 intval($params['uid'])
479 if ($p && (count($p) > 1)) {
480 for ($d = 1; $d < count($p); $d ++) {
481 q("DELETE FROM `notify` WHERE `id` = %d",
486 // only continue on if we stored the first one
488 if ($notify_id != $p[0]['id']) {
495 $itemlink = $a->get_baseurl().'/notify/view/'.$notify_id;
496 $msg = replace_macros($epreamble, array('$itemlink' => $itemlink));
497 $r = q("UPDATE `notify` SET `msg` = '%s' WHERE `id` = %d AND `uid` = %d",
500 intval($params['uid'])
504 // send email notification if notification preferences permit
505 if ((intval($params['notify_flags']) & intval($params['type']))
506 || $params['type'] == NOTIFY_SYSTEM
507 || $params['type'] == "SYSTEM_EMAIL") {
509 logger('sending notification email');
511 if (isset($params['parent']) AND (intval($params['parent']) != 0)) {
512 $id_for_parent = $params['parent']."@".$hostname;
514 // Is this the first email notification for this parent item and user?
516 $r = q("SELECT `id` FROM `notify-threads` WHERE `master-parent-item` = %d AND `receiver-uid` = %d LIMIT 1",
517 intval($params['parent']),
518 intval($params['uid']));
520 // If so, create the record of it and use a message-id smtp header.
523 logger("notify_id:".intval($notify_id).", parent: ".intval($params['parent'])."uid: ".intval($params['uid']), LOGGER_DEBUG);
524 $r = q("INSERT INTO `notify-threads` (`notify-id`, `master-parent-item`, `receiver-uid`, `parent-item`)
525 values(%d, %d, %d, %d)",
527 intval($params['parent']),
528 intval($params['uid']),
531 $additional_mail_header .= "Message-ID: <${id_for_parent}>\n";
532 $log_msg = "include/enotify: No previous notification found for this parent:\n".
533 " parent: ${params['parent']}\n"." uid : ${params['uid']}\n";
534 logger($log_msg, LOGGER_DEBUG);
536 // If not, just "follow" the thread.
537 $additional_mail_header .= "References: <${id_for_parent}>\nIn-Reply-To: <${id_for_parent}>\n";
538 logger("There's already a notification for this parent:\n".print_r($r, true), LOGGER_DEBUG);
542 // textversion keeps linebreaks
543 $textversion = strip_tags(str_replace("<br>", "\n", html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",
544 $body))),ENT_QUOTES, 'UTF-8')));
545 $htmlversion = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n\\n", "\\n"),
546 "<br />\n", $body))), ENT_QUOTES, 'UTF-8');
549 $datarray['banner'] = $banner;
550 $datarray['product'] = $product;
551 $datarray['preamble'] = $preamble;
552 $datarray['sitename'] = $sitename;
553 $datarray['siteurl'] = $siteurl;
554 $datarray['type'] = $params['type'];
555 $datarray['parent'] = $params['parent'];
556 $datarray['source_name'] = $params['source_name'];
557 $datarray['source_link'] = $params['source_link'];
558 $datarray['source_photo'] = $params['source_photo'];
559 $datarray['uid'] = $params['uid'];
560 $datarray['username'] = $params['to_name'];
561 $datarray['hsitelink'] = $hsitelink;
562 $datarray['tsitelink'] = $tsitelink;
563 $datarray['hitemlink'] = '<a href="'.$itemlink.'">'.$itemlink.'</a>';
564 $datarray['titemlink'] = $itemlink;
565 $datarray['thanks'] = $thanks;
566 $datarray['site_admin'] = $site_admin;
567 $datarray['title'] = stripslashes($title);
568 $datarray['htmlversion'] = $htmlversion;
569 $datarray['textversion'] = $textversion;
570 $datarray['subject'] = $subject;
571 $datarray['headers'] = $additional_mail_header;
573 call_hooks('enotify_mail', $datarray);
575 // check whether sending post content in email notifications is allowed
576 // always true for "SYSTEM_EMAIL"
577 $content_allowed = ((!get_config('system', 'enotify_no_content')) || ($params['type'] == "SYSTEM_EMAIL"));
579 // load the template for private message notifications
580 $tpl = get_markup_template('email_notify_html.tpl');
581 $email_html_body = replace_macros($tpl, array(
582 '$banner' => $datarray['banner'],
583 '$product' => $datarray['product'],
584 '$preamble' => str_replace("\n", "<br>\n", $datarray['preamble']),
585 '$sitename' => $datarray['sitename'],
586 '$siteurl' => $datarray['siteurl'],
587 '$source_name' => $datarray['source_name'],
588 '$source_link' => $datarray['source_link'],
589 '$source_photo' => $datarray['source_photo'],
590 '$username' => $datarray['to_name'],
591 '$hsitelink' => $datarray['hsitelink'],
592 '$hitemlink' => $datarray['hitemlink'],
593 '$thanks' => $datarray['thanks'],
594 '$site_admin' => $datarray['site_admin'],
595 '$title' => $datarray['title'],
596 '$htmlversion' => $datarray['htmlversion'],
597 '$content_allowed' => $content_allowed,
600 // load the template for private message notifications
601 $tpl = get_markup_template('email_notify_text.tpl');
602 $email_text_body = replace_macros($tpl, array(
603 '$banner' => $datarray['banner'],
604 '$product' => $datarray['product'],
605 '$preamble' => $datarray['preamble'],
606 '$sitename' => $datarray['sitename'],
607 '$siteurl' => $datarray['siteurl'],
608 '$source_name' => $datarray['source_name'],
609 '$source_link' => $datarray['source_link'],
610 '$source_photo' => $datarray['source_photo'],
611 '$username' => $datarray['to_name'],
612 '$tsitelink' => $datarray['tsitelink'],
613 '$titemlink' => $datarray['titemlink'],
614 '$thanks' => $datarray['thanks'],
615 '$site_admin' => $datarray['site_admin'],
616 '$title' => $datarray['title'],
617 '$textversion' => $datarray['textversion'],
618 '$content_allowed' => $content_allowed,
621 // use the Emailer class to send the message
623 return Emailer::send(array(
624 'uid' => $params['uid'],
625 'fromName' => $sender_name,
626 'fromEmail' => $sender_email,
627 'replyTo' => $sender_email,
628 'toEmail' => $params['to_email'],
629 'messageSubject' => $datarray['subject'],
630 'htmlVersion' => $email_html_body,
631 'textVersion' => $email_text_body,
632 'additionalMailHeader' => $datarray['headers'],
640 * @brief Checks for item related notifications and sends them
642 * @param int $itemid ID of the item for which the check should be done
643 * @param int $uid User ID
644 * @param str $defaulttype (Optional) Forces a notification with this type.
646 function check_item_notification($itemid, $uid, $defaulttype = "") {
648 $notification_data = array("uid" => $uid, "profiles" => array());
649 call_hooks('check_item_notification', $notification_data);
651 $profiles = $notification_data["profiles"];
653 $user = q("SELECT `notify-flags`, `language`, `username`, `email`, `nickname` FROM `user` WHERE `uid` = %d", intval($uid));
657 $owner = q("SELECT `id`, `url` FROM `contact` WHERE `self` AND `uid` = %d LIMIT 1", intval($uid));
661 // This is our regular URL format
662 $profiles[] = $owner[0]["url"];
664 // Notifications from Diaspora are often with an URL in the Diaspora format
665 $profiles[] = App::get_baseurl()."/u/".$user[0]["nickname"];
667 $profiles2 = array();
669 foreach ($profiles AS $profile) {
670 // Check for invalid profile urls. 13 should be the shortest possible profile length:
672 // Additionally check for invalid urls that would return the normalised value "http:"
673 if ((strlen($profile) >= 13) AND (normalise_link($profile) != "http:")) {
674 if (!in_array($profile, $profiles2))
675 $profiles2[] = $profile;
677 $profile = normalise_link($profile);
678 if (!in_array($profile, $profiles2))
679 $profiles2[] = $profile;
681 $profile = str_replace("http://", "https://", $profile);
682 if (!in_array($profile, $profiles2))
683 $profiles2[] = $profile;
687 $profiles = $profiles2;
691 foreach ($profiles AS $profile) {
692 if ($profile_list != "")
693 $profile_list .= "', '";
695 $profile_list .= dbesc($profile);
698 $profile_list = "'".$profile_list."'";
700 // Only act if it is a "real" post
701 // We need the additional check for the "local_profile" because of mixed situations on connector networks
702 $item = q("SELECT `id`, `mention`, `tag`,`parent`, `title`, `body`, `author-name`, `author-link`, `author-avatar`, `guid`,
703 `parent-uri`, `uri`, `contact-id`
704 FROM `item` WHERE `id` = %d AND `verb` IN ('%s', '') AND `type` != 'activity' AND
705 NOT (`author-link` IN ($profile_list)) LIMIT 1",
706 intval($itemid), dbesc(ACTIVITY_POST));
710 // Generate the notification array
712 $params["uid"] = $uid;
713 $params["notify_flags"] = $user[0]["notify-flags"];
714 $params["language"] = $user[0]["language"];
715 $params["to_name"] = $user[0]["username"];
716 $params["to_email"] = $user[0]["email"];
717 $params["item"] = $item[0];
718 $params["parent"] = $item[0]["parent"];
719 $params["link"] = App::get_baseurl().'/display/'.urlencode($item[0]["guid"]);
720 $params["otype"] = 'item';
721 $params["source_name"] = $item[0]["author-name"];
722 $params["source_link"] = $item[0]["author-link"];
723 $params["source_photo"] = $item[0]["author-avatar"];
725 if ($item[0]["parent-uri"] === $item[0]["uri"]) {
726 // Send a notification for every new post?
727 $r = q("SELECT `notify_new_posts` FROM `contact` WHERE `id` = %d AND `uid` = %d AND `notify_new_posts` LIMIT 1",
728 intval($item[0]['contact-id']),
731 $send_notification = count($r);
733 if (!$send_notification) {
734 $tags = q("SELECT `url` FROM `term` WHERE `otype` = %d AND `oid` = %d AND `type` = %d AND `uid` = %d",
735 intval(TERM_OBJ_POST), intval($itemid), intval(TERM_MENTION), intval($uid));
738 foreach ($tags AS $tag) {
739 $r = q("SELECT `id` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d AND `notify_new_posts`",
740 normalise_link($tag["url"]), intval($uid));
742 $send_notification = true;
747 if ($send_notification) {
748 $params["type"] = NOTIFY_SHARE;
749 $params["verb"] = ACTIVITY_TAG;
753 // Is the user mentioned in this post?
756 foreach ($profiles AS $profile) {
757 if (strpos($item[0]["tag"], "=".$profile."]") OR strpos($item[0]["body"], "=".$profile."]"))
761 if ($item[0]["mention"] OR $tagged OR ($defaulttype == NOTIFY_TAGSELF)) {
762 $params["type"] = NOTIFY_TAGSELF;
763 $params["verb"] = ACTIVITY_TAG;
766 // Is it a post that the user had started or where he interacted?
767 $parent = q("SELECT `thread`.`iid` FROM `thread` INNER JOIN `item` ON `item`.`parent` = `thread`.`iid`
768 WHERE `thread`.`iid` = %d AND `thread`.`uid` = %d AND NOT `thread`.`ignored` AND
769 (`thread`.`mention` OR `item`.`author-link` IN ($profile_list))
771 intval($item[0]["parent"]), intval($uid));
773 if ($parent AND !isset($params["type"])) {
774 $params["type"] = NOTIFY_COMMENT;
775 $params["verb"] = ACTIVITY_POST;
778 if (isset($params["type"]))
779 notification($params);