3 require_once('include/email.php');
5 function notification($params) {
7 logger('notification: entry', LOGGER_DEBUG);
11 // from here on everything is in the recipients language
13 push_lang($params['language']);
16 $banner = t('Friendica Notification');
17 $product = FRIENDICA_PLATFORM;
18 $siteurl = $a->get_baseurl(true);
19 $thanks = t('Thank You,');
20 $sitename = $a->config['sitename'];
21 $site_admin = sprintf( t('%s Administrator'), $sitename);
23 $sender_name = $product;
24 $hostname = $a->get_hostname();
25 if(strpos($hostname,':'))
26 $hostname = substr($hostname,0,strpos($hostname,':'));
28 $sender_email = t('noreply') . '@' . $hostname;
29 $additional_mail_header = "";
31 $additional_mail_header .= "Precedence: list\n";
32 $additional_mail_header .= "X-Friendica-Host: ".$hostname."\n";
33 $additional_mail_header .= "X-Friendica-Platform: ".FRIENDICA_PLATFORM."\n";
34 $additional_mail_header .= "X-Friendica-Version: ".FRIENDICA_VERSION."\n";
35 $additional_mail_header .= "List-ID: <notification.".$hostname.">\n";
36 $additional_mail_header .= "List-Archive: <".$a->get_baseurl()."/notifications/system>\n";
38 if(array_key_exists('item',$params)) {
39 $title = $params['item']['title'];
40 $body = $params['item']['body'];
46 // e.g. "your post", "David's photo", etc.
47 $possess_desc = t('%s <!item_type!>');
49 if($params['type'] == NOTIFY_MAIL) {
51 $subject = sprintf( t('[Friendica:Notify] New mail received at %s'),$sitename);
53 $preamble = sprintf( t('%1$s sent you a new private message at %2$s.'),$params['source_name'],$sitename);
54 $epreamble = sprintf( t('%1$s sent you %2$s.'),'[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=$itemlink]' . t('a private message') . '[/url]');
55 $sitelink = t('Please visit %s to view and/or reply to your private messages.');
56 $tsitelink = sprintf( $sitelink, $siteurl . '/message/' . $params['item']['id'] );
57 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '/message/' . $params['item']['id'] . '">' . $sitename . '</a>');
58 $itemlink = $siteurl . '/message/' . $params['item']['id'];
61 if($params['type'] == NOTIFY_COMMENT) {
62 // logger("notification: params = " . print_r($params, true), LOGGER_DEBUG);
64 $parent_id = $params['parent'];
66 $p = q("SELECT `ignored` FROM `thread` WHERE `iid` = %d AND `uid` = %d LIMIT 1",
68 intval($params['uid'])
70 if ($p AND count($p) AND ($p[0]["ignored"])) {
71 logger("Thread ".$parent_id." will be ignored", LOGGER_DEBUG);
75 // Check to see if there was already a tag notify or comment notify for this post.
76 // If so don't create a second notification
79 $p = q("select id from notify where (type = %d or type = %d or type = %d) and link = '%s' and uid = %d limit 1",
80 intval(NOTIFY_TAGSELF),
81 intval(NOTIFY_COMMENT),
83 dbesc($params['link']),
84 intval($params['uid'])
86 if($p and count($p)) {
92 // if it's a post figure out who's post it is.
96 if($params['otype'] === 'item' && $parent_id) {
97 $p = q("select * from item where id = %d and uid = %d limit 1",
99 intval($params['uid'])
103 $item_post_type = item_post_type($p[0]);
104 //$possess_desc = str_replace('<!item_type!>',$possess_desc);
107 $dest_str = sprintf(t('%1$s commented on [url=%2$s]a %3$s[/url]'),
108 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
112 // "George Bull's post"
114 $dest_str = sprintf(t('%1$s commented on [url=%2$s]%3$s\'s %4$s[/url]'),
115 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
117 $p[0]['author-name'],
121 if($p[0]['owner-name'] == $p[0]['author-name'] && $p[0]['wall'])
122 $dest_str = sprintf(t('%1$s commented on [url=%2$s]your %3$s[/url]'),
123 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
127 // Some mail softwares relies on subject field for threading.
128 // So, we cannot have different subjects for notifications of the same thread.
129 // Before this we have the name of the replier on the subject rendering
130 // differents subjects for messages on the same thread.
132 $subject = sprintf( t('[Friendica:Notify] Comment to conversation #%1$d by %2$s'), $parent_id, $params['source_name']);
133 $preamble = sprintf( t('%s commented on an item/conversation you have been following.'), $params['source_name']);
134 $epreamble = $dest_str;
136 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
137 $tsitelink = sprintf( $sitelink, $siteurl );
138 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
139 $itemlink = $params['link'];
142 if($params['type'] == NOTIFY_WALL) {
143 $subject = sprintf( t('[Friendica:Notify] %s posted to your profile wall') , $params['source_name']);
145 $preamble = sprintf( t('%1$s posted to your profile wall at %2$s') , $params['source_name'], $sitename);
147 $epreamble = sprintf( t('%1$s posted to [url=%2$s]your wall[/url]') ,
148 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
151 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
152 $tsitelink = sprintf( $sitelink, $siteurl );
153 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
154 $itemlink = $params['link'];
157 if($params['type'] == NOTIFY_TAGSELF) {
158 $subject = sprintf( t('[Friendica:Notify] %s tagged you') , $params['source_name']);
159 $preamble = sprintf( t('%1$s tagged you at %2$s') , $params['source_name'], $sitename);
160 $epreamble = sprintf( t('%1$s [url=%2$s]tagged you[/url].') ,
161 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
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_SHARE) {
171 $subject = sprintf( t('[Friendica:Notify] %s shared a new post') , $params['source_name']);
172 $preamble = sprintf( t('%1$s shared a new post at %2$s') , $params['source_name'], $sitename);
173 $epreamble = sprintf( t('%1$s [url=%2$s]shared a post[/url].') ,
174 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
177 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
178 $tsitelink = sprintf( $sitelink, $siteurl );
179 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
180 $itemlink = $params['link'];
183 if($params['type'] == NOTIFY_POKE) {
185 $subject = sprintf( t('[Friendica:Notify] %1$s poked you') , $params['source_name']);
186 $preamble = sprintf( t('%1$s poked you at %2$s') , $params['source_name'], $sitename);
187 $epreamble = sprintf( t('%1$s [url=%2$s]poked you[/url].') ,
188 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
191 $subject = str_replace('poked', t($params['activity']), $subject);
192 $preamble = str_replace('poked', t($params['activity']), $preamble);
193 $epreamble = str_replace('poked', t($params['activity']), $epreamble);
195 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
196 $tsitelink = sprintf( $sitelink, $siteurl );
197 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
198 $itemlink = $params['link'];
201 if($params['type'] == NOTIFY_TAGSHARE) {
202 $subject = sprintf( t('[Friendica:Notify] %s tagged your post') , $params['source_name']);
203 $preamble = sprintf( t('%1$s tagged your post at %2$s') , $params['source_name'], $sitename);
204 $epreamble = sprintf( t('%1$s tagged [url=%2$s]your post[/url]') ,
205 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
208 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
209 $tsitelink = sprintf( $sitelink, $siteurl );
210 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
211 $itemlink = $params['link'];
214 if($params['type'] == NOTIFY_INTRO) {
215 $subject = sprintf( t('[Friendica:Notify] Introduction received'));
216 $preamble = sprintf( t('You\'ve received an introduction from \'%1$s\' at %2$s'), $params['source_name'], $sitename);
217 $epreamble = sprintf( t('You\'ve received [url=%1$s]an introduction[/url] from %2$s.'),
219 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
220 $body = sprintf( t('You may visit their profile at %s'),$params['source_link']);
222 $sitelink = t('Please visit %s to approve or reject the introduction.');
223 $tsitelink = sprintf( $sitelink, $siteurl );
224 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
225 $itemlink = $params['link'];
228 if($params['type'] == NOTIFY_SUGGEST) {
229 $subject = sprintf( t('[Friendica:Notify] Friend suggestion received'));
230 $preamble = sprintf( t('You\'ve received a friend suggestion from \'%1$s\' at %2$s'), $params['source_name'], $sitename);
231 $epreamble = sprintf( t('You\'ve received [url=%1$s]a friend suggestion[/url] for %2$s from %3$s.'),
233 '[url=' . $params['item']['url'] . ']' . $params['item']['name'] . '[/url]',
234 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
236 $body = t('Name:') . ' ' . $params['item']['name'] . "\n";
237 $body .= t('Photo:') . ' ' . $params['item']['photo'] . "\n";
238 $body .= sprintf( t('You may visit their profile at %s'),$params['item']['url']);
240 $sitelink = t('Please visit %s to approve or reject the suggestion.');
241 $tsitelink = sprintf( $sitelink, $siteurl );
242 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
243 $itemlink = $params['link'];
246 if($params['type'] == NOTIFY_CONFIRM) {
249 if($params['type'] == NOTIFY_SYSTEM) {
255 'subject' => $subject,
256 'preamble' => $preamble,
257 'epreamble' => $epreamble,
259 'sitelink' => $sitelink,
260 'tsitelink' => $tsitelink,
261 'hsitelink' => $hsitelink,
262 'itemlink' => $itemlink
265 call_hooks('enotify',$h);
267 $subject = $h['subject'];
268 $preamble = $h['preamble'];
269 $epreamble = $h['epreamble'];
271 $sitelink = $h['sitelink'];
272 $tsitelink = $h['tsitelink'];
273 $hsitelink = $h['hsitelink'];
274 $itemlink = $h['itemlink'];
277 require_once('include/html2bbcode.php');
281 $hash = random_string();
282 $r = q("SELECT `id` FROM `notify` WHERE `hash` = '%s' LIMIT 1",
286 } while($dups == true);
290 $datarray['hash'] = $hash;
291 $datarray['name'] = $params['source_name'];
292 $datarray['url'] = $params['source_link'];
293 $datarray['photo'] = $params['source_photo'];
294 $datarray['date'] = datetime_convert();
295 $datarray['uid'] = $params['uid'];
296 $datarray['link'] = $itemlink;
297 $datarray['parent'] = $parent_id;
298 $datarray['type'] = $params['type'];
299 $datarray['verb'] = $params['verb'];
300 $datarray['otype'] = $params['otype'];
301 $datarray['abort'] = false;
303 call_hooks('enotify_store', $datarray);
305 if($datarray['abort']) {
310 // create notification entry in DB
312 $r = q("insert into notify (hash,name,url,photo,date,uid,link,parent,type,verb,otype)
313 values('%s','%s','%s','%s','%s',%d,'%s',%d,%d,'%s','%s')",
314 dbesc($datarray['hash']),
315 dbesc($datarray['name']),
316 dbesc($datarray['url']),
317 dbesc($datarray['photo']),
318 dbesc($datarray['date']),
319 intval($datarray['uid']),
320 dbesc($datarray['link']),
321 intval($datarray['parent']),
322 intval($datarray['type']),
323 dbesc($datarray['verb']),
324 dbesc($datarray['otype'])
327 $r = q("select id from notify where hash = '%s' and uid = %d limit 1",
329 intval($params['uid'])
332 $notify_id = $r[0]['id'];
338 // we seem to have a lot of duplicate comment notifications due to race conditions, mostly from forums
339 // After we've stored everything, look again to see if there are any duplicates and if so remove them
342 $p = q("select id from notify where ( type = %d or type = %d ) and link = '%s' and uid = %d order by id",
343 intval(NOTIFY_TAGSELF),
344 intval(NOTIFY_COMMENT),
345 dbesc($params['link']),
346 intval($params['uid'])
348 if($p && (count($p) > 1)) {
349 for ($d = 1; $d < count($p); $d ++) {
350 q("delete from notify where id = %d",
355 // only continue on if we stored the first one
357 if($notify_id != $p[0]['id']) {
370 $itemlink = $a->get_baseurl() . '/notify/view/' . $notify_id;
371 $msg = replace_macros($epreamble,array('$itemlink' => $itemlink));
372 $r = q("update notify set msg = '%s' where id = %d and uid = %d",
375 intval($params['uid'])
379 // send email notification if notification preferences permit
381 require_once('include/bbcode.php');
382 if((intval($params['notify_flags']) & intval($params['type'])) || $params['type'] == NOTIFY_SYSTEM) {
384 logger('notification: sending notification email');
386 if (isset($params['parent']) AND (intval($params['parent']) != 0)) {
387 $id_for_parent = $params['parent']."@".$hostname;
389 // Is this the first email notification for this parent item and user?
391 $r = q("select `id` from `notify-threads` where `master-parent-item` = %d and `receiver-uid` = %d limit 1",
392 intval($params['parent']),
393 intval($params['uid']) );
395 // If so, create the record of it and use a message-id smtp header.
398 logger("notify_id:".intval($notify_id).", parent: ".intval($params['parent'])."uid: ".intval($params['uid']), LOGGER_DEBUG);
399 $r = q("insert into `notify-threads` (`notify-id`, `master-parent-item`, `receiver-uid`, `parent-item`)
400 values(%d,%d,%d,%d)",
402 intval($params['parent']),
403 intval($params['uid']),
406 $additional_mail_header .= "Message-ID: <${id_for_parent}>\n";
407 $log_msg = "include/enotify: No previous notification found for this parent:\n" .
408 " parent: ${params['parent']}\n" . " uid : ${params['uid']}\n";
409 logger($log_msg, LOGGER_DEBUG);
411 // If not, just "follow" the thread.
412 $additional_mail_header .= "References: <${id_for_parent}>\nIn-Reply-To: <${id_for_parent}>\n";
413 logger("include/enotify: There's already a notification for this parent:\n" . print_r($r, true), LOGGER_DEBUG);
418 $textversion = strip_tags(html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",
419 $body))),ENT_QUOTES,'UTF-8'));
420 $htmlversion = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"),
421 "<br />\n",$body))),ENT_QUOTES,'UTF-8');
424 $datarray['banner'] = $banner;
425 $datarray['product'] = $product;
426 $datarray['preamble'] = $preamble;
427 $datarray['sitename'] = $sitename;
428 $datarray['siteurl'] = $siteurl;
429 $datarray['type'] = $params['type'];
430 $datarray['parent'] = $params['parent'];
431 $datarray['source_name'] = $params['source_name'];
432 $datarray['source_link'] = $params['source_link'];
433 $datarray['source_photo'] = $params['source_photo'];
434 $datarray['uid'] = $params['uid'];
435 $datarray['username'] = $params['to_name'];
436 $datarray['hsitelink'] = $hsitelink;
437 $datarray['tsitelink'] = $tsitelink;
438 $datarray['hitemlink'] = '<a href="' . $itemlink . '">' . $itemlink . '</a>';
439 $datarray['titemlink'] = $itemlink;
440 $datarray['thanks'] = $thanks;
441 $datarray['site_admin'] = $site_admin;
442 $datarray['title'] = stripslashes($title);
443 $datarray['htmlversion'] = $htmlversion;
444 $datarray['textversion'] = $textversion;
445 $datarray['subject'] = $subject;
446 $datarray['headers'] = $additional_mail_header;
448 call_hooks('enotify_mail', $datarray);
450 // check whether sending post content in email notifications is allowed
451 $content_allowed = !get_config('system','enotify_no_content');
453 // load the template for private message notifications
454 $tpl = get_markup_template('email_notify_html.tpl');
455 $email_html_body = replace_macros($tpl,array(
456 '$banner' => $datarray['banner'],
457 '$product' => $datarray['product'],
458 '$preamble' => $datarray['preamble'],
459 '$sitename' => $datarray['sitename'],
460 '$siteurl' => $datarray['siteurl'],
461 '$source_name' => $datarray['source_name'],
462 '$source_link' => $datarray['source_link'],
463 '$source_photo' => $datarray['source_photo'],
464 '$username' => $datarray['to_name'],
465 '$hsitelink' => $datarray['hsitelink'],
466 '$hitemlink' => $datarray['hitemlink'],
467 '$thanks' => $datarray['thanks'],
468 '$site_admin' => $datarray['site_admin'],
469 '$title' => $datarray['title'],
470 '$htmlversion' => $datarray['htmlversion'],
471 '$content_allowed' => $content_allowed,
474 // load the template for private message notifications
475 $tpl = get_markup_template('email_notify_text.tpl');
476 $email_text_body = replace_macros($tpl,array(
477 '$banner' => $datarray['banner'],
478 '$product' => $datarray['product'],
479 '$preamble' => $datarray['preamble'],
480 '$sitename' => $datarray['sitename'],
481 '$siteurl' => $datarray['siteurl'],
482 '$source_name' => $datarray['source_name'],
483 '$source_link' => $datarray['source_link'],
484 '$source_photo' => $datarray['source_photo'],
485 '$username' => $datarray['to_name'],
486 '$tsitelink' => $datarray['tsitelink'],
487 '$titemlink' => $datarray['titemlink'],
488 '$thanks' => $datarray['thanks'],
489 '$site_admin' => $datarray['site_admin'],
490 '$title' => $datarray['title'],
491 '$textversion' => $datarray['textversion'],
492 '$content_allowed' => $content_allowed,
495 // logger('text: ' . $email_text_body);
497 // use the EmailNotification library to send the message
500 'fromName' => $sender_name,
501 'fromEmail' => $sender_email,
502 'replyTo' => $sender_email,
503 'toEmail' => $params['to_email'],
504 'messageSubject' => $datarray['subject'],
505 'htmlVersion' => $email_html_body,
506 'textVersion' => $email_text_body,
507 'additionalMailHeader' => $datarray['headers'],
515 require_once('include/email.php');
519 * Send a multipart/alternative message with Text and HTML versions
521 * @param fromName name of the sender
522 * @param fromEmail email fo the sender
523 * @param replyTo replyTo address to direct responses
524 * @param toEmail destination email address
525 * @param messageSubject subject of the message
526 * @param htmlVersion html version of the message
527 * @param textVersion text only version of the message
528 * @param additionalMailHeader additions to the smtp mail header
530 static public function send($params) {
532 $fromName = email_header_encode(html_entity_decode($params['fromName'],ENT_QUOTES,'UTF-8'),'UTF-8');
533 $messageSubject = email_header_encode(html_entity_decode($params['messageSubject'],ENT_QUOTES,'UTF-8'),'UTF-8');
535 // generate a mime boundary
536 $mimeBoundary =rand(0,9)."-"
537 .rand(10000000000,9999999999)."-"
538 .rand(10000000000,9999999999)."=:"
541 // generate a multipart/alternative message header
543 $params['additionalMailHeader'] .
544 "From: $fromName <{$params['fromEmail']}>\n" .
545 "Reply-To: $fromName <{$params['replyTo']}>\n" .
546 "MIME-Version: 1.0\n" .
547 "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
549 // assemble the final multipart message body with the text and html types included
550 $textBody = chunk_split(base64_encode($params['textVersion']));
551 $htmlBody = chunk_split(base64_encode($params['htmlVersion']));
552 $multipartMessageBody =
553 "--" . $mimeBoundary . "\n" . // plain text section
554 "Content-Type: text/plain; charset=UTF-8\n" .
555 "Content-Transfer-Encoding: base64\n\n" .
557 "--" . $mimeBoundary . "\n" . // text/html section
558 "Content-Type: text/html; charset=UTF-8\n" .
559 "Content-Transfer-Encoding: base64\n\n" .
561 "--" . $mimeBoundary . "--\n"; // message ending
565 $params['toEmail'], // send to address
566 $messageSubject, // subject
567 $multipartMessageBody, // message body
568 $messageHeader // message headers
570 logger("notification: enotify::send header " . 'To: ' . $params['toEmail'] . "\n" . $messageHeader, LOGGER_DEBUG);
571 logger("notification: enotify::send returns " . $res, LOGGER_DEBUG);