2 require_once('include/Emailer.php');
3 require_once('include/email.php');
4 require_once('include/bbcode.php');
5 require_once('include/html2bbcode.php');
7 function notification($params) {
9 #logger('notification()', LOGGER_DEBUG);
13 // from here on everything is in the recipients language
15 push_lang($params['language']);
18 $banner = t('Friendica Notification');
19 $product = FRIENDICA_PLATFORM;
20 $siteurl = $a->get_baseurl(true);
21 $thanks = t('Thank You,');
22 $sitename = $a->config['sitename'];
23 $site_admin = sprintf( t('%s Administrator'), $sitename);
25 $sender_name = $product;
26 $hostname = $a->get_hostname();
27 if(strpos($hostname,':'))
28 $hostname = substr($hostname,0,strpos($hostname,':'));
30 $sender_email = t('noreply') . '@' . $hostname;
32 // with $params['show_in_notification_page'] == false, the notification isn't inserted into
33 // the database, and an email is sent if applicable.
34 // default, if not specified: true
35 $show_in_notification_page = ((x($params,'show_in_notification_page')) ? $params['show_in_notification_page']:True);
37 $additional_mail_header = "";
38 $additional_mail_header .= "Precedence: list\n";
39 $additional_mail_header .= "X-Friendica-Host: ".$hostname."\n";
40 $additional_mail_header .= "X-Friendica-Platform: ".FRIENDICA_PLATFORM."\n";
41 $additional_mail_header .= "X-Friendica-Version: ".FRIENDICA_VERSION."\n";
42 $additional_mail_header .= "List-ID: <notification.".$hostname.">\n";
43 $additional_mail_header .= "List-Archive: <".$a->get_baseurl()."/notifications/system>\n";
46 if(array_key_exists('item',$params)) {
47 $title = $params['item']['title'];
48 $body = $params['item']['body'];
54 // e.g. "your post", "David's photo", etc.
55 $possess_desc = t('%s <!item_type!>');
57 if($params['type'] == NOTIFY_MAIL) {
59 $subject = sprintf( t('[Friendica:Notify] New mail received at %s'),$sitename);
61 $preamble = sprintf( t('%1$s sent you a new private message at %2$s.'),$params['source_name'],$sitename);
62 $epreamble = sprintf( t('%1$s sent you %2$s.'),'[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=$itemlink]' . t('a private message') . '[/url]');
63 $sitelink = t('Please visit %s to view and/or reply to your private messages.');
64 $tsitelink = sprintf( $sitelink, $siteurl . '/message/' . $params['item']['id'] );
65 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '/message/' . $params['item']['id'] . '">' . $sitename . '</a>');
66 $itemlink = $siteurl . '/message/' . $params['item']['id'];
69 if($params['type'] == NOTIFY_COMMENT) {
70 // logger("notification: params = " . print_r($params, true), LOGGER_DEBUG);
72 $parent_id = $params['parent'];
74 $p = q("SELECT `ignored` FROM `thread` WHERE `iid` = %d AND `uid` = %d LIMIT 1",
76 intval($params['uid'])
78 if ($p AND count($p) AND ($p[0]["ignored"])) {
79 logger("Thread ".$parent_id." will be ignored", LOGGER_DEBUG);
83 // Check to see if there was already a tag notify or comment notify for this post.
84 // If so don't create a second notification
87 $p = q("select id from notify where (type = %d or type = %d or type = %d) and link = '%s' and uid = %d limit 1",
88 intval(NOTIFY_TAGSELF),
89 intval(NOTIFY_COMMENT),
91 dbesc($params['link']),
92 intval($params['uid'])
94 if($p and count($p)) {
100 // if it's a post figure out who's post it is.
104 if($params['otype'] === 'item' && $parent_id) {
105 $p = q("select * from item where id = %d and uid = %d limit 1",
107 intval($params['uid'])
111 $item_post_type = item_post_type($p[0]);
112 //$possess_desc = str_replace('<!item_type!>',$possess_desc);
115 $dest_str = sprintf(t('%1$s commented on [url=%2$s]a %3$s[/url]'),
116 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
120 // "George Bull's post"
122 $dest_str = sprintf(t('%1$s commented on [url=%2$s]%3$s\'s %4$s[/url]'),
123 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
125 $p[0]['author-name'],
129 if($p[0]['owner-name'] == $p[0]['author-name'] && $p[0]['wall'])
130 $dest_str = sprintf(t('%1$s commented on [url=%2$s]your %3$s[/url]'),
131 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
135 // Some mail softwares relies on subject field for threading.
136 // So, we cannot have different subjects for notifications of the same thread.
137 // Before this we have the name of the replier on the subject rendering
138 // differents subjects for messages on the same thread.
140 $subject = sprintf( t('[Friendica:Notify] Comment to conversation #%1$d by %2$s'), $parent_id, $params['source_name']);
141 $preamble = sprintf( t('%s commented on an item/conversation you have been following.'), $params['source_name']);
142 $epreamble = $dest_str;
144 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
145 $tsitelink = sprintf( $sitelink, $siteurl );
146 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
147 $itemlink = $params['link'];
150 if($params['type'] == NOTIFY_WALL) {
151 $subject = sprintf( t('[Friendica:Notify] %s posted to your profile wall') , $params['source_name']);
153 $preamble = sprintf( t('%1$s posted to your profile wall at %2$s') , $params['source_name'], $sitename);
155 $epreamble = sprintf( t('%1$s posted to [url=%2$s]your wall[/url]') ,
156 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
159 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
160 $tsitelink = sprintf( $sitelink, $siteurl );
161 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
162 $itemlink = $params['link'];
165 if($params['type'] == NOTIFY_TAGSELF) {
166 $subject = sprintf( t('[Friendica:Notify] %s tagged you') , $params['source_name']);
167 $preamble = sprintf( t('%1$s tagged you at %2$s') , $params['source_name'], $sitename);
168 $epreamble = sprintf( t('%1$s [url=%2$s]tagged you[/url].') ,
169 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
172 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
173 $tsitelink = sprintf( $sitelink, $siteurl );
174 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
175 $itemlink = $params['link'];
178 if($params['type'] == NOTIFY_SHARE) {
179 $subject = sprintf( t('[Friendica:Notify] %s shared a new post') , $params['source_name']);
180 $preamble = sprintf( t('%1$s shared a new post at %2$s') , $params['source_name'], $sitename);
181 $epreamble = sprintf( t('%1$s [url=%2$s]shared a post[/url].') ,
182 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
185 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
186 $tsitelink = sprintf( $sitelink, $siteurl );
187 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
188 $itemlink = $params['link'];
191 if($params['type'] == NOTIFY_POKE) {
193 $subject = sprintf( t('[Friendica:Notify] %1$s poked you') , $params['source_name']);
194 $preamble = sprintf( t('%1$s poked you at %2$s') , $params['source_name'], $sitename);
195 $epreamble = sprintf( t('%1$s [url=%2$s]poked you[/url].') ,
196 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
199 $subject = str_replace('poked', t($params['activity']), $subject);
200 $preamble = str_replace('poked', t($params['activity']), $preamble);
201 $epreamble = str_replace('poked', t($params['activity']), $epreamble);
203 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
204 $tsitelink = sprintf( $sitelink, $siteurl );
205 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
206 $itemlink = $params['link'];
209 if($params['type'] == NOTIFY_TAGSHARE) {
210 $subject = sprintf( t('[Friendica:Notify] %s tagged your post') , $params['source_name']);
211 $preamble = sprintf( t('%1$s tagged your post at %2$s') , $params['source_name'], $sitename);
212 $epreamble = sprintf( t('%1$s tagged [url=%2$s]your post[/url]') ,
213 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
216 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
217 $tsitelink = sprintf( $sitelink, $siteurl );
218 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
219 $itemlink = $params['link'];
222 if($params['type'] == NOTIFY_INTRO) {
223 $subject = sprintf( t('[Friendica:Notify] Introduction received'));
224 $preamble = sprintf( t('You\'ve received an introduction from \'%1$s\' at %2$s'), $params['source_name'], $sitename);
225 $epreamble = sprintf( t('You\'ve received [url=%1$s]an introduction[/url] from %2$s.'),
227 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
228 $body = sprintf( t('You may visit their profile at %s'),$params['source_link']);
230 $sitelink = t('Please visit %s to approve or reject the introduction.');
231 $tsitelink = sprintf( $sitelink, $siteurl );
232 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
233 $itemlink = $params['link'];
235 switch ($params['verb']) {
236 case ACTIVITY_FRIEND:
237 // someone started to share with user (mostly OStatus)
238 $subject = sprintf( t('[Friendica:Notify] A new person is sharing with you'));
239 $preamble = sprintf( t('%1$s is sharing with you at %2$s'), $params['source_name'], $sitename);
240 $epreamble = sprintf( t('%1$s is sharing with you at %2$s'),
241 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
244 case ACTIVITY_FOLLOW:
245 // someone started to follow the user (mostly OStatus)
246 $subject = sprintf( t('[Friendica:Notify] You have a new follower'));
247 $preamble = sprintf( t('You have a new follower at %2$s : %1$s'), $params['source_name'], $sitename);
248 $epreamble = sprintf( t('You have a new follower at %2$s : %1$s'),
249 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
259 if($params['type'] == NOTIFY_SUGGEST) {
260 $subject = sprintf( t('[Friendica:Notify] Friend suggestion received'));
261 $preamble = sprintf( t('You\'ve received a friend suggestion from \'%1$s\' at %2$s'), $params['source_name'], $sitename);
262 $epreamble = sprintf( t('You\'ve received [url=%1$s]a friend suggestion[/url] for %2$s from %3$s.'),
264 '[url=' . $params['item']['url'] . ']' . $params['item']['name'] . '[/url]',
265 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
267 $body = t('Name:') . ' ' . $params['item']['name'] . "\n";
268 $body .= t('Photo:') . ' ' . $params['item']['photo'] . "\n";
269 $body .= sprintf( t('You may visit their profile at %s'),$params['item']['url']);
271 $sitelink = t('Please visit %s to approve or reject the suggestion.');
272 $tsitelink = sprintf( $sitelink, $siteurl );
273 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
274 $itemlink = $params['link'];
277 if($params['type'] == NOTIFY_CONFIRM) {
278 if ($params['verb'] == ACTIVITY_FRIEND ){ // mutual connection
279 $subject = sprintf( t('[Friendica:Notify] Connection accepted'));
280 $preamble = sprintf( t('\'%1$s\' has acepted your connection request at %2$s'), $params['source_name'], $sitename);
281 $epreamble = sprintf( t('%2$s has accepted your [url=%1$s]connection request[/url].'),
283 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
284 $body = t('You are now mutual friends and may exchange status updates, photos, and email
285 without restriction.');
287 $sitelink = t('Please visit %s if you wish to make any changes to this relationship.');
288 $tsitelink = sprintf( $sitelink, $siteurl );
289 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
290 $itemlink = $params['link'];
291 } else { // ACTIVITY_FOLLOW
292 $subject = sprintf( t('[Friendica:Notify] Connection accepted'));
293 $preamble = sprintf( t('\'%1$s\' has acepted your connection request at %2$s'), $params['source_name'], $sitename);
294 $epreamble = sprintf( t('%2$s has accepted your [url=%1$s]connection request[/url].'),
296 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
297 $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']);
299 $body .= sprintf(t('\'%1$s\' may choose to extend this into a two-way or more permissive relationship in the future. '), $params['source_name']);
301 $sitelink = t('Please visit %s if you wish to make any changes to this relationship.');
302 $tsitelink = sprintf( $sitelink, $siteurl );
303 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
304 $itemlink = $params['link'];
309 if($params['type'] == NOTIFY_SYSTEM) {
310 $subject = $params['subject'];
311 $preamble = $params['preamble'];
312 $epreamble = $params['epreamble'];
313 $body = $params['body'];
324 'subject' => $subject,
325 'preamble' => $preamble,
326 'epreamble' => $epreamble,
328 'sitelink' => $sitelink,
329 'tsitelink' => $tsitelink,
330 'hsitelink' => $hsitelink,
331 'itemlink' => $itemlink
334 call_hooks('enotify',$h);
336 $subject = $h['subject'];
337 $preamble = $h['preamble'];
338 $epreamble = $h['epreamble'];
340 $sitelink = $h['sitelink'];
341 $tsitelink = $h['tsitelink'];
342 $hsitelink = $h['hsitelink'];
343 $itemlink = $h['itemlink'];
346 if ($show_in_notification_page) {
350 $hash = random_string();
351 $r = q("SELECT `id` FROM `notify` WHERE `hash` = '%s' LIMIT 1",
355 } while($dups == true);
359 $datarray['hash'] = $hash;
360 $datarray['name'] = $params['source_name'];
361 $datarray['url'] = $params['source_link'];
362 $datarray['photo'] = $params['source_photo'];
363 $datarray['date'] = datetime_convert();
364 $datarray['uid'] = $params['uid'];
365 $datarray['link'] = $itemlink;
366 $datarray['parent'] = $parent_id;
367 $datarray['type'] = $params['type'];
368 $datarray['verb'] = $params['verb'];
369 $datarray['otype'] = $params['otype'];
370 $datarray['abort'] = false;
372 call_hooks('enotify_store', $datarray);
374 if($datarray['abort']) {
379 // create notification entry in DB
381 $r = q("insert into notify (hash,name,url,photo,date,uid,link,parent,type,verb,otype)
382 values('%s','%s','%s','%s','%s',%d,'%s',%d,%d,'%s','%s')",
383 dbesc($datarray['hash']),
384 dbesc($datarray['name']),
385 dbesc($datarray['url']),
386 dbesc($datarray['photo']),
387 dbesc($datarray['date']),
388 intval($datarray['uid']),
389 dbesc($datarray['link']),
390 intval($datarray['parent']),
391 intval($datarray['type']),
392 dbesc($datarray['verb']),
393 dbesc($datarray['otype'])
396 $r = q("select id from notify where hash = '%s' and uid = %d limit 1",
398 intval($params['uid'])
401 $notify_id = $r[0]['id'];
407 // we seem to have a lot of duplicate comment notifications due to race conditions, mostly from forums
408 // After we've stored everything, look again to see if there are any duplicates and if so remove them
411 $p = q("select id from notify where ( type = %d or type = %d ) and link = '%s' and uid = %d order by id",
412 intval(NOTIFY_TAGSELF),
413 intval(NOTIFY_COMMENT),
414 dbesc($params['link']),
415 intval($params['uid'])
417 if($p && (count($p) > 1)) {
418 for ($d = 1; $d < count($p); $d ++) {
419 q("delete from notify where id = %d",
424 // only continue on if we stored the first one
426 if($notify_id != $p[0]['id']) {
433 $itemlink = $a->get_baseurl() . '/notify/view/' . $notify_id;
434 $msg = replace_macros($epreamble,array('$itemlink' => $itemlink));
435 $r = q("update notify set msg = '%s' where id = %d and uid = %d",
438 intval($params['uid'])
443 // send email notification if notification preferences permit
444 if((intval($params['notify_flags']) & intval($params['type'])) || $params['type'] == NOTIFY_SYSTEM) {
446 logger('notification: sending notification email');
448 if (isset($params['parent']) AND (intval($params['parent']) != 0)) {
449 $id_for_parent = $params['parent']."@".$hostname;
451 // Is this the first email notification for this parent item and user?
453 $r = q("select `id` from `notify-threads` where `master-parent-item` = %d and `receiver-uid` = %d limit 1",
454 intval($params['parent']),
455 intval($params['uid']) );
457 // If so, create the record of it and use a message-id smtp header.
460 logger("notify_id:".intval($notify_id).", parent: ".intval($params['parent'])."uid: ".intval($params['uid']), LOGGER_DEBUG);
461 $r = q("insert into `notify-threads` (`notify-id`, `master-parent-item`, `receiver-uid`, `parent-item`)
462 values(%d,%d,%d,%d)",
464 intval($params['parent']),
465 intval($params['uid']),
468 $additional_mail_header .= "Message-ID: <${id_for_parent}>\n";
469 $log_msg = "include/enotify: No previous notification found for this parent:\n" .
470 " parent: ${params['parent']}\n" . " uid : ${params['uid']}\n";
471 logger($log_msg, LOGGER_DEBUG);
473 // If not, just "follow" the thread.
474 $additional_mail_header .= "References: <${id_for_parent}>\nIn-Reply-To: <${id_for_parent}>\n";
475 logger("There's already a notification for this parent:\n" . print_r($r, true), LOGGER_DEBUG);
480 $textversion = strip_tags(html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",
481 $body))),ENT_QUOTES,'UTF-8'));
482 $htmlversion = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"),
483 "<br />\n",$body))),ENT_QUOTES,'UTF-8');
486 $datarray['banner'] = $banner;
487 $datarray['product'] = $product;
488 $datarray['preamble'] = $preamble;
489 $datarray['sitename'] = $sitename;
490 $datarray['siteurl'] = $siteurl;
491 $datarray['type'] = $params['type'];
492 $datarray['parent'] = $params['parent'];
493 $datarray['source_name'] = $params['source_name'];
494 $datarray['source_link'] = $params['source_link'];
495 $datarray['source_photo'] = $params['source_photo'];
496 $datarray['uid'] = $params['uid'];
497 $datarray['username'] = $params['to_name'];
498 $datarray['hsitelink'] = $hsitelink;
499 $datarray['tsitelink'] = $tsitelink;
500 $datarray['hitemlink'] = '<a href="' . $itemlink . '">' . $itemlink . '</a>';
501 $datarray['titemlink'] = $itemlink;
502 $datarray['thanks'] = $thanks;
503 $datarray['site_admin'] = $site_admin;
504 $datarray['title'] = stripslashes($title);
505 $datarray['htmlversion'] = $htmlversion;
506 $datarray['textversion'] = $textversion;
507 $datarray['subject'] = $subject;
508 $datarray['headers'] = $additional_mail_header;
510 call_hooks('enotify_mail', $datarray);
512 // check whether sending post content in email notifications is allowed
513 $content_allowed = !get_config('system','enotify_no_content');
515 // load the template for private message notifications
516 $tpl = get_markup_template('email_notify_html.tpl');
517 $email_html_body = replace_macros($tpl,array(
518 '$banner' => $datarray['banner'],
519 '$product' => $datarray['product'],
520 '$preamble' => $datarray['preamble'],
521 '$sitename' => $datarray['sitename'],
522 '$siteurl' => $datarray['siteurl'],
523 '$source_name' => $datarray['source_name'],
524 '$source_link' => $datarray['source_link'],
525 '$source_photo' => $datarray['source_photo'],
526 '$username' => $datarray['to_name'],
527 '$hsitelink' => $datarray['hsitelink'],
528 '$hitemlink' => $datarray['hitemlink'],
529 '$thanks' => $datarray['thanks'],
530 '$site_admin' => $datarray['site_admin'],
531 '$title' => $datarray['title'],
532 '$htmlversion' => $datarray['htmlversion'],
533 '$content_allowed' => $content_allowed,
536 // load the template for private message notifications
537 $tpl = get_markup_template('email_notify_text.tpl');
538 $email_text_body = replace_macros($tpl,array(
539 '$banner' => $datarray['banner'],
540 '$product' => $datarray['product'],
541 '$preamble' => $datarray['preamble'],
542 '$sitename' => $datarray['sitename'],
543 '$siteurl' => $datarray['siteurl'],
544 '$source_name' => $datarray['source_name'],
545 '$source_link' => $datarray['source_link'],
546 '$source_photo' => $datarray['source_photo'],
547 '$username' => $datarray['to_name'],
548 '$tsitelink' => $datarray['tsitelink'],
549 '$titemlink' => $datarray['titemlink'],
550 '$thanks' => $datarray['thanks'],
551 '$site_admin' => $datarray['site_admin'],
552 '$title' => $datarray['title'],
553 '$textversion' => $datarray['textversion'],
554 '$content_allowed' => $content_allowed,
557 // logger('text: ' . $email_text_body);
559 // use the Emailer class to send the message
562 'fromName' => $sender_name,
563 'fromEmail' => $sender_email,
564 'replyTo' => $sender_email,
565 'toEmail' => $params['to_email'],
566 'messageSubject' => $datarray['subject'],
567 'htmlVersion' => $email_html_body,
568 'textVersion' => $email_text_body,
569 'additionalMailHeader' => $datarray['headers'],