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 if(array_key_exists('item',$params)) {
32 $title = $params['item']['title'];
33 $body = $params['item']['body'];
39 // e.g. "your post", "David's photo", etc.
40 $possess_desc = t('%s <!item_type!>');
42 if($params['type'] == NOTIFY_MAIL) {
44 $subject = sprintf( t('[Friendica:Notify] New mail received at %s'),$sitename);
46 $preamble = sprintf( t('%1$s sent you a new private message at %2$s.'),$params['source_name'],$sitename);
47 $epreamble = sprintf( t('%1$s sent you %2$s.'),'[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=$itemlink]' . t('a private message') . '[/url]');
48 $sitelink = t('Please visit %s to view and/or reply to your private messages.');
49 $tsitelink = sprintf( $sitelink, $siteurl . '/message/' . $params['item']['id'] );
50 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '/message/' . $params['item']['id'] . '">' . $sitename . '</a>');
51 $itemlink = $siteurl . '/message/' . $params['item']['id'];
54 if($params['type'] == NOTIFY_COMMENT) {
55 // logger("notification: params = " . print_r($params, true), LOGGER_DEBUG);
57 $parent_id = $params['parent'];
59 // Check to see if there was already a tag notify or comment notify for this post.
60 // If so don't create a second notification
63 $p = q("select id from notify where (type = %d or type = %d or type = %d) and link = '%s' and uid = %d limit 1",
64 intval(NOTIFY_TAGSELF),
65 intval(NOTIFY_COMMENT),
67 dbesc($params['link']),
68 intval($params['uid'])
70 if($p and count($p)) {
76 // if it's a post figure out who's post it is.
80 if($params['otype'] === 'item' && $parent_id) {
81 $p = q("select * from item where id = %d and uid = %d limit 1",
83 intval($params['uid'])
87 $item_post_type = item_post_type($p[0]);
88 //$possess_desc = str_replace('<!item_type!>',$possess_desc);
91 $dest_str = sprintf(t('%1$s commented on [url=%2$s]a %3$s[/url]'),
92 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
96 // "George Bull's post"
98 $dest_str = sprintf(t('%1$s commented on [url=%2$s]%3$s\'s %4$s[/url]'),
99 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
101 $p[0]['author-name'],
105 if($p[0]['owner-name'] == $p[0]['author-name'] && $p[0]['wall'])
106 $dest_str = sprintf(t('%1$s commented on [url=%2$s]your %3$s[/url]'),
107 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
111 // Some mail softwares relies on subject field for threading.
112 // So, we cannot have different subjects for notifications of the same thread.
113 // Before this we have the name of the replier on the subject rendering
114 // differents subjects for messages on the same thread.
116 $subject = sprintf( t('[Friendica:Notify] Comment to conversation #%1$d by %2$s'), $parent_id, $params['source_name']);
117 $preamble = sprintf( t('%s commented on an item/conversation you have been following.'), $params['source_name']);
118 $epreamble = $dest_str;
120 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
121 $tsitelink = sprintf( $sitelink, $siteurl );
122 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
123 $itemlink = $params['link'];
126 if($params['type'] == NOTIFY_WALL) {
127 $subject = sprintf( t('[Friendica:Notify] %s posted to your profile wall') , $params['source_name']);
129 $preamble = sprintf( t('%1$s posted to your profile wall at %2$s') , $params['source_name'], $sitename);
131 $epreamble = sprintf( t('%1$s posted to [url=%2$s]your wall[/url]') ,
132 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
135 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
136 $tsitelink = sprintf( $sitelink, $siteurl );
137 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
138 $itemlink = $params['link'];
141 if($params['type'] == NOTIFY_TAGSELF) {
142 $subject = sprintf( t('[Friendica:Notify] %s tagged you') , $params['source_name']);
143 $preamble = sprintf( t('%1$s tagged you at %2$s') , $params['source_name'], $sitename);
144 $epreamble = sprintf( t('%1$s [url=%2$s]tagged you[/url].') ,
145 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
148 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
149 $tsitelink = sprintf( $sitelink, $siteurl );
150 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
151 $itemlink = $params['link'];
154 if($params['type'] == NOTIFY_SHARE) {
155 $subject = sprintf( t('[Friendica:Notify] %s shared a new post') , $params['source_name']);
156 $preamble = sprintf( t('%1$s shared a new post at %2$s') , $params['source_name'], $sitename);
157 $epreamble = sprintf( t('%1$s [url=%2$s]shared a post[/url].') ,
158 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
161 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
162 $tsitelink = sprintf( $sitelink, $siteurl );
163 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
164 $itemlink = $params['link'];
167 if($params['type'] == NOTIFY_POKE) {
169 $subject = sprintf( t('[Friendica:Notify] %1$s poked you') , $params['source_name']);
170 $preamble = sprintf( t('%1$s poked you at %2$s') , $params['source_name'], $sitename);
171 $epreamble = sprintf( t('%1$s [url=%2$s]poked you[/url].') ,
172 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
175 $subject = str_replace('poked', t($params['activity']), $subject);
176 $preamble = str_replace('poked', t($params['activity']), $preamble);
177 $epreamble = str_replace('poked', t($params['activity']), $epreamble);
179 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
180 $tsitelink = sprintf( $sitelink, $siteurl );
181 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
182 $itemlink = $params['link'];
185 if($params['type'] == NOTIFY_TAGSHARE) {
186 $subject = sprintf( t('[Friendica:Notify] %s tagged your post') , $params['source_name']);
187 $preamble = sprintf( t('%1$s tagged your post at %2$s') , $params['source_name'], $sitename);
188 $epreamble = sprintf( t('%1$s tagged [url=%2$s]your post[/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_INTRO) {
199 $subject = sprintf( t('[Friendica:Notify] Introduction received'));
200 $preamble = sprintf( t('You\'ve received an introduction from \'%1$s\' at %2$s'), $params['source_name'], $sitename);
201 $epreamble = sprintf( t('You\'ve received [url=%1$s]an introduction[/url] from %2$s.'),
203 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
204 $body = sprintf( t('You may visit their profile at %s'),$params['source_link']);
206 $sitelink = t('Please visit %s to approve or reject the introduction.');
207 $tsitelink = sprintf( $sitelink, $siteurl );
208 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
209 $itemlink = $params['link'];
212 if($params['type'] == NOTIFY_SUGGEST) {
213 $subject = sprintf( t('[Friendica:Notify] Friend suggestion received'));
214 $preamble = sprintf( t('You\'ve received a friend suggestion from \'%1$s\' at %2$s'), $params['source_name'], $sitename);
215 $epreamble = sprintf( t('You\'ve received [url=%1$s]a friend suggestion[/url] for %2$s from %3$s.'),
217 '[url=' . $params['item']['url'] . ']' . $params['item']['name'] . '[/url]',
218 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
220 $body = t('Name:') . ' ' . $params['item']['name'] . "\n";
221 $body .= t('Photo:') . ' ' . $params['item']['photo'] . "\n";
222 $body .= sprintf( t('You may visit their profile at %s'),$params['item']['url']);
224 $sitelink = t('Please visit %s to approve or reject the suggestion.');
225 $tsitelink = sprintf( $sitelink, $siteurl );
226 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
227 $itemlink = $params['link'];
230 if($params['type'] == NOTIFY_CONFIRM) {
233 if($params['type'] == NOTIFY_SYSTEM) {
239 'subject' => $subject,
240 'preamble' => $preamble,
241 'epreamble' => $epreamble,
243 'sitelink' => $sitelink,
244 'tsitelink' => $tsitelink,
245 'hsitelink' => $hsitelink,
246 'itemlink' => $itemlink
249 call_hooks('enotify',$h);
251 $subject = $h['subject'];
252 $preamble = $h['preamble'];
253 $epreamble = $h['epreamble'];
255 $sitelink = $h['sitelink'];
256 $tsitelink = $h['tsitelink'];
257 $hsitelink = $h['hsitelink'];
258 $itemlink = $h['itemlink'];
261 require_once('include/html2bbcode.php');
265 $hash = random_string();
266 $r = q("SELECT `id` FROM `notify` WHERE `hash` = '%s' LIMIT 1",
270 } while($dups == true);
274 $datarray['hash'] = $hash;
275 $datarray['name'] = $params['source_name'];
276 $datarray['url'] = $params['source_link'];
277 $datarray['photo'] = $params['source_photo'];
278 $datarray['date'] = datetime_convert();
279 $datarray['uid'] = $params['uid'];
280 $datarray['link'] = $itemlink;
281 $datarray['parent'] = $parent_id;
282 $datarray['type'] = $params['type'];
283 $datarray['verb'] = $params['verb'];
284 $datarray['otype'] = $params['otype'];
285 $datarray['abort'] = false;
287 call_hooks('enotify_store', $datarray);
289 if($datarray['abort']) {
294 // create notification entry in DB
296 $r = q("insert into notify (hash,name,url,photo,date,uid,link,parent,type,verb,otype)
297 values('%s','%s','%s','%s','%s',%d,'%s',%d,%d,'%s','%s')",
298 dbesc($datarray['hash']),
299 dbesc($datarray['name']),
300 dbesc($datarray['url']),
301 dbesc($datarray['photo']),
302 dbesc($datarray['date']),
303 intval($datarray['uid']),
304 dbesc($datarray['link']),
305 intval($datarray['parent']),
306 intval($datarray['type']),
307 dbesc($datarray['verb']),
308 dbesc($datarray['otype'])
311 $r = q("select id from notify where hash = '%s' and uid = %d limit 1",
313 intval($params['uid'])
316 $notify_id = $r[0]['id'];
322 // we seem to have a lot of duplicate comment notifications due to race conditions, mostly from forums
323 // After we've stored everything, look again to see if there are any duplicates and if so remove them
326 $p = q("select id from notify where ( type = %d or type = %d ) and link = '%s' and uid = %d order by id",
327 intval(NOTIFY_TAGSELF),
328 intval(NOTIFY_COMMENT),
329 dbesc($params['link']),
330 intval($params['uid'])
332 if($p && (count($p) > 1)) {
333 for ($d = 1; $d < count($p); $d ++) {
334 q("delete from notify where id = %d",
339 // only continue on if we stored the first one
341 if($notify_id != $p[0]['id']) {
354 $itemlink = $a->get_baseurl() . '/notify/view/' . $notify_id;
355 $msg = replace_macros($epreamble,array('$itemlink' => $itemlink));
356 $r = q("update notify set msg = '%s' where id = %d and uid = %d",
359 intval($params['uid'])
363 // send email notification if notification preferences permit
365 require_once('include/bbcode.php');
366 if((intval($params['notify_flags']) & intval($params['type'])) || $params['type'] == NOTIFY_SYSTEM) {
368 logger('notification: sending notification email');
370 $id_for_parent = "${params['parent']}@${hostname}";
372 // Is this the first email notification for this parent item and user?
374 $r = q("select `id` from `notify-threads` where `master-parent-item` = %d and `receiver-uid` = %d limit 1",
375 intval($params['parent']),
376 intval($params['uid']) );
378 // If so, create the record of it and use a message-id smtp header.
381 logger("notify_id:" . intval($notify_id). ", parent: " . intval($params['parent']) . "uid: " .
382 intval($params['uid']), LOGGER_DEBUG);
383 $r = q("insert into `notify-threads` (`notify-id`, `master-parent-item`, `receiver-uid`, `parent-item`)
384 values(%d,%d,%d,%d)",
386 intval($params['parent']),
387 intval($params['uid']),
390 $additional_mail_header .= "Message-ID: <${id_for_parent}>\n";
391 $log_msg = "include/enotify: No previous notification found for this parent:\n" .
392 " parent: ${params['parent']}\n" . " uid : ${params['uid']}\n";
393 logger($log_msg, LOGGER_DEBUG);
396 // If not, just "follow" the thread.
399 $additional_mail_header = "References: <${id_for_parent}>\nIn-Reply-To: <${id_for_parent}>\n";
400 logger("include/enotify: There's already a notification for this parent:\n" . print_r($r, true), LOGGER_DEBUG);
405 $textversion = strip_tags(html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",
406 $body))),ENT_QUOTES,'UTF-8'));
407 $htmlversion = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"),
408 "<br />\n",$body))));
411 $datarray['banner'] = $banner;
412 $datarray['product'] = $product;
413 $datarray['preamble'] = $preamble;
414 $datarray['sitename'] = $sitename;
415 $datarray['siteurl'] = $siteurl;
416 $datarray['type'] = $params['type'];
417 $datarray['parent'] = $params['parent'];
418 $datarray['source_name'] = $params['source_name'];
419 $datarray['source_link'] = $params['source_link'];
420 $datarray['source_photo'] = $params['source_photo'];
421 $datarray['uid'] = $params['uid'];
422 $datarray['username'] = $params['to_name'];
423 $datarray['hsitelink'] = $hsitelink;
424 $datarray['tsitelink'] = $tsitelink;
425 $datarray['hitemlink'] = '<a href="' . $itemlink . '">' . $itemlink . '</a>';
426 $datarray['titemlink'] = $itemlink;
427 $datarray['thanks'] = $thanks;
428 $datarray['site_admin'] = $site_admin;
429 $datarray['title'] = stripslashes($title);
430 $datarray['htmlversion'] = $htmlversion;
431 $datarray['textversion'] = $textversion;
432 $datarray['subject'] = $subject;
433 $datarray['headers'] = $additional_mail_header;
435 call_hooks('enotify_mail', $datarray);
437 // check whether sending post content in email notifications is allowed
438 $content_allowed = !get_config('system','enotify_no_content');
440 // load the template for private message notifications
441 $tpl = get_markup_template('email_notify_html.tpl');
442 $email_html_body = replace_macros($tpl,array(
443 '$banner' => $datarray['banner'],
444 '$product' => $datarray['product'],
445 '$preamble' => $datarray['preamble'],
446 '$sitename' => $datarray['sitename'],
447 '$siteurl' => $datarray['siteurl'],
448 '$source_name' => $datarray['source_name'],
449 '$source_link' => $datarray['source_link'],
450 '$source_photo' => $datarray['source_photo'],
451 '$username' => $datarray['to_name'],
452 '$hsitelink' => $datarray['hsitelink'],
453 '$hitemlink' => $datarray['hitemlink'],
454 '$thanks' => $datarray['thanks'],
455 '$site_admin' => $datarray['site_admin'],
456 '$title' => $datarray['title'],
457 '$htmlversion' => $datarray['htmlversion'],
458 '$content_allowed' => $content_allowed,
461 // load the template for private message notifications
462 $tpl = get_markup_template('email_notify_text.tpl');
463 $email_text_body = replace_macros($tpl,array(
464 '$banner' => $datarray['banner'],
465 '$product' => $datarray['product'],
466 '$preamble' => $datarray['preamble'],
467 '$sitename' => $datarray['sitename'],
468 '$siteurl' => $datarray['siteurl'],
469 '$source_name' => $datarray['source_name'],
470 '$source_link' => $datarray['source_link'],
471 '$source_photo' => $datarray['source_photo'],
472 '$username' => $datarray['to_name'],
473 '$tsitelink' => $datarray['tsitelink'],
474 '$titemlink' => $datarray['titemlink'],
475 '$thanks' => $datarray['thanks'],
476 '$site_admin' => $datarray['site_admin'],
477 '$title' => $datarray['title'],
478 '$textversion' => $datarray['textversion'],
479 '$content_allowed' => $content_allowed,
482 // logger('text: ' . $email_text_body);
484 // use the EmailNotification library to send the message
487 'fromName' => $sender_name,
488 'fromEmail' => $sender_email,
489 'replyTo' => $sender_email,
490 'toEmail' => $params['to_email'],
491 'messageSubject' => $datarray['subject'],
492 'htmlVersion' => $email_html_body,
493 'textVersion' => $email_text_body,
494 'additionalMailHeader' => $datarray['headers'],
502 require_once('include/email.php');
506 * Send a multipart/alternative message with Text and HTML versions
508 * @param fromName name of the sender
509 * @param fromEmail email fo the sender
510 * @param replyTo replyTo address to direct responses
511 * @param toEmail destination email address
512 * @param messageSubject subject of the message
513 * @param htmlVersion html version of the message
514 * @param textVersion text only version of the message
515 * @param additionalMailHeader additions to the smtp mail header
517 static public function send($params) {
519 $fromName = email_header_encode(html_entity_decode($params['fromName'],ENT_QUOTES,'UTF-8'),'UTF-8');
520 $messageSubject = email_header_encode(html_entity_decode($params['messageSubject'],ENT_QUOTES,'UTF-8'),'UTF-8');
522 // generate a mime boundary
523 $mimeBoundary =rand(0,9)."-"
524 .rand(10000000000,9999999999)."-"
525 .rand(10000000000,9999999999)."=:"
528 // generate a multipart/alternative message header
530 $params['additionalMailHeader'] .
531 "From: $fromName <{$params['fromEmail']}>\n" .
532 "Reply-To: $fromName <{$params['replyTo']}>\n" .
533 "MIME-Version: 1.0\n" .
534 "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
536 // assemble the final multipart message body with the text and html types included
537 $textBody = chunk_split(base64_encode($params['textVersion']));
538 $htmlBody = chunk_split(base64_encode($params['htmlVersion']));
539 $multipartMessageBody =
540 "--" . $mimeBoundary . "\n" . // plain text section
541 "Content-Type: text/plain; charset=UTF-8\n" .
542 "Content-Transfer-Encoding: base64\n\n" .
544 "--" . $mimeBoundary . "\n" . // text/html section
545 "Content-Type: text/html; charset=UTF-8\n" .
546 "Content-Transfer-Encoding: base64\n\n" .
548 "--" . $mimeBoundary . "--\n"; // message ending
552 $params['toEmail'], // send to address
553 $messageSubject, // subject
554 $multipartMessageBody, // message body
555 $messageHeader // message headers
557 logger("notification: enotify::send header " . 'To: ' . $params['toEmail'] . "\n" . $messageHeader, LOGGER_DEBUG);
558 logger("notification: enotify::send returns " . $res, LOGGER_DEBUG);