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 // Check to see if there was already a tag notify or comment notify for this post.
67 // If so don't create a second notification
70 $p = q("select id from notify where (type = %d or type = %d or type = %d) and link = '%s' and uid = %d limit 1",
71 intval(NOTIFY_TAGSELF),
72 intval(NOTIFY_COMMENT),
74 dbesc($params['link']),
75 intval($params['uid'])
77 if($p and count($p)) {
83 // if it's a post figure out who's post it is.
87 if($params['otype'] === 'item' && $parent_id) {
88 $p = q("select * from item where id = %d and uid = %d limit 1",
90 intval($params['uid'])
94 $item_post_type = item_post_type($p[0]);
95 //$possess_desc = str_replace('<!item_type!>',$possess_desc);
98 $dest_str = sprintf(t('%1$s commented on [url=%2$s]a %3$s[/url]'),
99 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
103 // "George Bull's post"
105 $dest_str = sprintf(t('%1$s commented on [url=%2$s]%3$s\'s %4$s[/url]'),
106 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
108 $p[0]['author-name'],
112 if($p[0]['owner-name'] == $p[0]['author-name'] && $p[0]['wall'])
113 $dest_str = sprintf(t('%1$s commented on [url=%2$s]your %3$s[/url]'),
114 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
118 // Some mail softwares relies on subject field for threading.
119 // So, we cannot have different subjects for notifications of the same thread.
120 // Before this we have the name of the replier on the subject rendering
121 // differents subjects for messages on the same thread.
123 $subject = sprintf( t('[Friendica:Notify] Comment to conversation #%1$d by %2$s'), $parent_id, $params['source_name']);
124 $preamble = sprintf( t('%s commented on an item/conversation you have been following.'), $params['source_name']);
125 $epreamble = $dest_str;
127 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
128 $tsitelink = sprintf( $sitelink, $siteurl );
129 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
130 $itemlink = $params['link'];
133 if($params['type'] == NOTIFY_WALL) {
134 $subject = sprintf( t('[Friendica:Notify] %s posted to your profile wall') , $params['source_name']);
136 $preamble = sprintf( t('%1$s posted to your profile wall at %2$s') , $params['source_name'], $sitename);
138 $epreamble = sprintf( t('%1$s posted to [url=%2$s]your wall[/url]') ,
139 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
142 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
143 $tsitelink = sprintf( $sitelink, $siteurl );
144 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
145 $itemlink = $params['link'];
148 if($params['type'] == NOTIFY_TAGSELF) {
149 $subject = sprintf( t('[Friendica:Notify] %s tagged you') , $params['source_name']);
150 $preamble = sprintf( t('%1$s tagged you at %2$s') , $params['source_name'], $sitename);
151 $epreamble = sprintf( t('%1$s [url=%2$s]tagged you[/url].') ,
152 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
155 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
156 $tsitelink = sprintf( $sitelink, $siteurl );
157 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
158 $itemlink = $params['link'];
161 if($params['type'] == NOTIFY_SHARE) {
162 $subject = sprintf( t('[Friendica:Notify] %s shared a new post') , $params['source_name']);
163 $preamble = sprintf( t('%1$s shared a new post at %2$s') , $params['source_name'], $sitename);
164 $epreamble = sprintf( t('%1$s [url=%2$s]shared a post[/url].') ,
165 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
168 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
169 $tsitelink = sprintf( $sitelink, $siteurl );
170 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
171 $itemlink = $params['link'];
174 if($params['type'] == NOTIFY_POKE) {
176 $subject = sprintf( t('[Friendica:Notify] %1$s poked you') , $params['source_name']);
177 $preamble = sprintf( t('%1$s poked you at %2$s') , $params['source_name'], $sitename);
178 $epreamble = sprintf( t('%1$s [url=%2$s]poked you[/url].') ,
179 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
182 $subject = str_replace('poked', t($params['activity']), $subject);
183 $preamble = str_replace('poked', t($params['activity']), $preamble);
184 $epreamble = str_replace('poked', t($params['activity']), $epreamble);
186 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
187 $tsitelink = sprintf( $sitelink, $siteurl );
188 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
189 $itemlink = $params['link'];
192 if($params['type'] == NOTIFY_TAGSHARE) {
193 $subject = sprintf( t('[Friendica:Notify] %s tagged your post') , $params['source_name']);
194 $preamble = sprintf( t('%1$s tagged your post at %2$s') , $params['source_name'], $sitename);
195 $epreamble = sprintf( t('%1$s tagged [url=%2$s]your post[/url]') ,
196 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
199 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
200 $tsitelink = sprintf( $sitelink, $siteurl );
201 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
202 $itemlink = $params['link'];
205 if($params['type'] == NOTIFY_INTRO) {
206 $subject = sprintf( t('[Friendica:Notify] Introduction received'));
207 $preamble = sprintf( t('You\'ve received an introduction from \'%1$s\' at %2$s'), $params['source_name'], $sitename);
208 $epreamble = sprintf( t('You\'ve received [url=%1$s]an introduction[/url] from %2$s.'),
210 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
211 $body = sprintf( t('You may visit their profile at %s'),$params['source_link']);
213 $sitelink = t('Please visit %s to approve or reject the introduction.');
214 $tsitelink = sprintf( $sitelink, $siteurl );
215 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
216 $itemlink = $params['link'];
219 if($params['type'] == NOTIFY_SUGGEST) {
220 $subject = sprintf( t('[Friendica:Notify] Friend suggestion received'));
221 $preamble = sprintf( t('You\'ve received a friend suggestion from \'%1$s\' at %2$s'), $params['source_name'], $sitename);
222 $epreamble = sprintf( t('You\'ve received [url=%1$s]a friend suggestion[/url] for %2$s from %3$s.'),
224 '[url=' . $params['item']['url'] . ']' . $params['item']['name'] . '[/url]',
225 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
227 $body = t('Name:') . ' ' . $params['item']['name'] . "\n";
228 $body .= t('Photo:') . ' ' . $params['item']['photo'] . "\n";
229 $body .= sprintf( t('You may visit their profile at %s'),$params['item']['url']);
231 $sitelink = t('Please visit %s to approve or reject the suggestion.');
232 $tsitelink = sprintf( $sitelink, $siteurl );
233 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
234 $itemlink = $params['link'];
237 if($params['type'] == NOTIFY_CONFIRM) {
240 if($params['type'] == NOTIFY_SYSTEM) {
246 'subject' => $subject,
247 'preamble' => $preamble,
248 'epreamble' => $epreamble,
250 'sitelink' => $sitelink,
251 'tsitelink' => $tsitelink,
252 'hsitelink' => $hsitelink,
253 'itemlink' => $itemlink
256 call_hooks('enotify',$h);
258 $subject = $h['subject'];
259 $preamble = $h['preamble'];
260 $epreamble = $h['epreamble'];
262 $sitelink = $h['sitelink'];
263 $tsitelink = $h['tsitelink'];
264 $hsitelink = $h['hsitelink'];
265 $itemlink = $h['itemlink'];
268 require_once('include/html2bbcode.php');
272 $hash = random_string();
273 $r = q("SELECT `id` FROM `notify` WHERE `hash` = '%s' LIMIT 1",
277 } while($dups == true);
281 $datarray['hash'] = $hash;
282 $datarray['name'] = $params['source_name'];
283 $datarray['url'] = $params['source_link'];
284 $datarray['photo'] = $params['source_photo'];
285 $datarray['date'] = datetime_convert();
286 $datarray['uid'] = $params['uid'];
287 $datarray['link'] = $itemlink;
288 $datarray['parent'] = $parent_id;
289 $datarray['type'] = $params['type'];
290 $datarray['verb'] = $params['verb'];
291 $datarray['otype'] = $params['otype'];
292 $datarray['abort'] = false;
294 call_hooks('enotify_store', $datarray);
296 if($datarray['abort']) {
301 // create notification entry in DB
303 $r = q("insert into notify (hash,name,url,photo,date,uid,link,parent,type,verb,otype)
304 values('%s','%s','%s','%s','%s',%d,'%s',%d,%d,'%s','%s')",
305 dbesc($datarray['hash']),
306 dbesc($datarray['name']),
307 dbesc($datarray['url']),
308 dbesc($datarray['photo']),
309 dbesc($datarray['date']),
310 intval($datarray['uid']),
311 dbesc($datarray['link']),
312 intval($datarray['parent']),
313 intval($datarray['type']),
314 dbesc($datarray['verb']),
315 dbesc($datarray['otype'])
318 $r = q("select id from notify where hash = '%s' and uid = %d limit 1",
320 intval($params['uid'])
323 $notify_id = $r[0]['id'];
329 // we seem to have a lot of duplicate comment notifications due to race conditions, mostly from forums
330 // After we've stored everything, look again to see if there are any duplicates and if so remove them
333 $p = q("select id from notify where ( type = %d or type = %d ) and link = '%s' and uid = %d order by id",
334 intval(NOTIFY_TAGSELF),
335 intval(NOTIFY_COMMENT),
336 dbesc($params['link']),
337 intval($params['uid'])
339 if($p && (count($p) > 1)) {
340 for ($d = 1; $d < count($p); $d ++) {
341 q("delete from notify where id = %d",
346 // only continue on if we stored the first one
348 if($notify_id != $p[0]['id']) {
361 $itemlink = $a->get_baseurl() . '/notify/view/' . $notify_id;
362 $msg = replace_macros($epreamble,array('$itemlink' => $itemlink));
363 $r = q("update notify set msg = '%s' where id = %d and uid = %d",
366 intval($params['uid'])
370 // send email notification if notification preferences permit
372 require_once('include/bbcode.php');
373 if((intval($params['notify_flags']) & intval($params['type'])) || $params['type'] == NOTIFY_SYSTEM) {
375 logger('notification: sending notification email');
377 if (isset($params['parent']) AND (intval($params['parent']) != 0)) {
378 $id_for_parent = $params['parent']."@".$hostname;
380 // Is this the first email notification for this parent item and user?
382 $r = q("select `id` from `notify-threads` where `master-parent-item` = %d and `receiver-uid` = %d limit 1",
383 intval($params['parent']),
384 intval($params['uid']) );
386 // If so, create the record of it and use a message-id smtp header.
389 logger("notify_id:".intval($notify_id).", parent: ".intval($params['parent'])."uid: ".intval($params['uid']), LOGGER_DEBUG);
390 $r = q("insert into `notify-threads` (`notify-id`, `master-parent-item`, `receiver-uid`, `parent-item`)
391 values(%d,%d,%d,%d)",
393 intval($params['parent']),
394 intval($params['uid']),
397 $additional_mail_header .= "Message-ID: <${id_for_parent}>\n";
398 $log_msg = "include/enotify: No previous notification found for this parent:\n" .
399 " parent: ${params['parent']}\n" . " uid : ${params['uid']}\n";
400 logger($log_msg, LOGGER_DEBUG);
402 // If not, just "follow" the thread.
403 $additional_mail_header .= "References: <${id_for_parent}>\nIn-Reply-To: <${id_for_parent}>\n";
404 logger("include/enotify: There's already a notification for this parent:\n" . print_r($r, true), LOGGER_DEBUG);
409 $textversion = strip_tags(html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",
410 $body))),ENT_QUOTES,'UTF-8'));
411 $htmlversion = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"),
412 "<br />\n",$body))),ENT_QUOTES,'UTF-8');
415 $datarray['banner'] = $banner;
416 $datarray['product'] = $product;
417 $datarray['preamble'] = $preamble;
418 $datarray['sitename'] = $sitename;
419 $datarray['siteurl'] = $siteurl;
420 $datarray['type'] = $params['type'];
421 $datarray['parent'] = $params['parent'];
422 $datarray['source_name'] = $params['source_name'];
423 $datarray['source_link'] = $params['source_link'];
424 $datarray['source_photo'] = $params['source_photo'];
425 $datarray['uid'] = $params['uid'];
426 $datarray['username'] = $params['to_name'];
427 $datarray['hsitelink'] = $hsitelink;
428 $datarray['tsitelink'] = $tsitelink;
429 $datarray['hitemlink'] = '<a href="' . $itemlink . '">' . $itemlink . '</a>';
430 $datarray['titemlink'] = $itemlink;
431 $datarray['thanks'] = $thanks;
432 $datarray['site_admin'] = $site_admin;
433 $datarray['title'] = stripslashes($title);
434 $datarray['htmlversion'] = $htmlversion;
435 $datarray['textversion'] = $textversion;
436 $datarray['subject'] = $subject;
437 $datarray['headers'] = $additional_mail_header;
439 call_hooks('enotify_mail', $datarray);
441 // check whether sending post content in email notifications is allowed
442 $content_allowed = !get_config('system','enotify_no_content');
444 // load the template for private message notifications
445 $tpl = get_markup_template('email_notify_html.tpl');
446 $email_html_body = replace_macros($tpl,array(
447 '$banner' => $datarray['banner'],
448 '$product' => $datarray['product'],
449 '$preamble' => $datarray['preamble'],
450 '$sitename' => $datarray['sitename'],
451 '$siteurl' => $datarray['siteurl'],
452 '$source_name' => $datarray['source_name'],
453 '$source_link' => $datarray['source_link'],
454 '$source_photo' => $datarray['source_photo'],
455 '$username' => $datarray['to_name'],
456 '$hsitelink' => $datarray['hsitelink'],
457 '$hitemlink' => $datarray['hitemlink'],
458 '$thanks' => $datarray['thanks'],
459 '$site_admin' => $datarray['site_admin'],
460 '$title' => $datarray['title'],
461 '$htmlversion' => $datarray['htmlversion'],
462 '$content_allowed' => $content_allowed,
465 // load the template for private message notifications
466 $tpl = get_markup_template('email_notify_text.tpl');
467 $email_text_body = replace_macros($tpl,array(
468 '$banner' => $datarray['banner'],
469 '$product' => $datarray['product'],
470 '$preamble' => $datarray['preamble'],
471 '$sitename' => $datarray['sitename'],
472 '$siteurl' => $datarray['siteurl'],
473 '$source_name' => $datarray['source_name'],
474 '$source_link' => $datarray['source_link'],
475 '$source_photo' => $datarray['source_photo'],
476 '$username' => $datarray['to_name'],
477 '$tsitelink' => $datarray['tsitelink'],
478 '$titemlink' => $datarray['titemlink'],
479 '$thanks' => $datarray['thanks'],
480 '$site_admin' => $datarray['site_admin'],
481 '$title' => $datarray['title'],
482 '$textversion' => $datarray['textversion'],
483 '$content_allowed' => $content_allowed,
486 // logger('text: ' . $email_text_body);
488 // use the EmailNotification library to send the message
491 'fromName' => $sender_name,
492 'fromEmail' => $sender_email,
493 'replyTo' => $sender_email,
494 'toEmail' => $params['to_email'],
495 'messageSubject' => $datarray['subject'],
496 'htmlVersion' => $email_html_body,
497 'textVersion' => $email_text_body,
498 'additionalMailHeader' => $datarray['headers'],
506 require_once('include/email.php');
510 * Send a multipart/alternative message with Text and HTML versions
512 * @param fromName name of the sender
513 * @param fromEmail email fo the sender
514 * @param replyTo replyTo address to direct responses
515 * @param toEmail destination email address
516 * @param messageSubject subject of the message
517 * @param htmlVersion html version of the message
518 * @param textVersion text only version of the message
519 * @param additionalMailHeader additions to the smtp mail header
521 static public function send($params) {
523 $fromName = email_header_encode(html_entity_decode($params['fromName'],ENT_QUOTES,'UTF-8'),'UTF-8');
524 $messageSubject = email_header_encode(html_entity_decode($params['messageSubject'],ENT_QUOTES,'UTF-8'),'UTF-8');
526 // generate a mime boundary
527 $mimeBoundary =rand(0,9)."-"
528 .rand(10000000000,9999999999)."-"
529 .rand(10000000000,9999999999)."=:"
532 // generate a multipart/alternative message header
534 $params['additionalMailHeader'] .
535 "From: $fromName <{$params['fromEmail']}>\n" .
536 "Reply-To: $fromName <{$params['replyTo']}>\n" .
537 "MIME-Version: 1.0\n" .
538 "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
540 // assemble the final multipart message body with the text and html types included
541 $textBody = chunk_split(base64_encode($params['textVersion']));
542 $htmlBody = chunk_split(base64_encode($params['htmlVersion']));
543 $multipartMessageBody =
544 "--" . $mimeBoundary . "\n" . // plain text section
545 "Content-Type: text/plain; charset=UTF-8\n" .
546 "Content-Transfer-Encoding: base64\n\n" .
548 "--" . $mimeBoundary . "\n" . // text/html section
549 "Content-Type: text/html; charset=UTF-8\n" .
550 "Content-Transfer-Encoding: base64\n\n" .
552 "--" . $mimeBoundary . "--\n"; // message ending
556 $params['toEmail'], // send to address
557 $messageSubject, // subject
558 $multipartMessageBody, // message body
559 $messageHeader // message headers
561 logger("notification: enotify::send header " . 'To: ' . $params['toEmail'] . "\n" . $messageHeader, LOGGER_DEBUG);
562 logger("notification: enotify::send returns " . $res, LOGGER_DEBUG);