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 = get_config('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 ) and link = '%s' and uid = %d limit 1",
64 intval(NOTIFY_TAGSELF),
65 intval(NOTIFY_COMMENT),
66 dbesc($params['link']),
67 intval($params['uid'])
69 if($p and count($p)) {
75 // if it's a post figure out who's post it is.
79 if($params['otype'] === 'item' && $parent_id) {
80 $p = q("select * from item where id = %d and uid = %d limit 1",
82 intval($params['uid'])
86 $item_post_type = item_post_type($p[0]);
87 //$possess_desc = str_replace('<!item_type!>',$possess_desc);
90 $dest_str = sprintf(t('%1$s commented on [url=%2$s]a %3$s[/url]'),
91 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
95 // "George Bull's post"
97 $dest_str = sprintf(t('%1$s commented on [url=%2$s]%3$s\'s %4$s[/url]'),
98 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
100 $p[0]['author-name'],
104 if($p[0]['owner-name'] == $p[0]['author-name'] && $p[0]['wall'])
105 $dest_str = sprintf(t('%1$s commented on [url=%2$s]your %3$s[/url]'),
106 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
110 // Some mail softwares relies on subject field for threading.
111 // So, we cannot have different subjects for notifications of the same thread.
112 // Before this we have the name of the replier on the subject rendering
113 // differents subjects for messages on the same thread.
115 $subject = sprintf( t('[Friendica:Notify] Comment to conversation #%1$d by %2$s'), $parent_id, $params['source_name']);
116 $preamble = sprintf( t('%s commented on an item/conversation you have been following.'), $params['source_name']);
117 $epreamble = $dest_str;
119 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
120 $tsitelink = sprintf( $sitelink, $siteurl );
121 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
122 $itemlink = $params['link'];
125 if($params['type'] == NOTIFY_WALL) {
126 $subject = sprintf( t('[Friendica:Notify] %s posted to your profile wall') , $params['source_name']);
128 $preamble = sprintf( t('%1$s posted to your profile wall at %2$s') , $params['source_name'], $sitename);
130 $epreamble = sprintf( t('%1$s posted to [url=%2$s]your wall[/url]') ,
131 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
134 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
135 $tsitelink = sprintf( $sitelink, $siteurl );
136 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
137 $itemlink = $params['link'];
140 if($params['type'] == NOTIFY_TAGSELF) {
141 $subject = sprintf( t('[Friendica:Notify] %s tagged you') , $params['source_name']);
142 $preamble = sprintf( t('%1$s tagged you at %2$s') , $params['source_name'], $sitename);
143 $epreamble = sprintf( t('%1$s [url=%2$s]tagged you[/url].') ,
144 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
147 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
148 $tsitelink = sprintf( $sitelink, $siteurl );
149 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
150 $itemlink = $params['link'];
153 if($params['type'] == NOTIFY_POKE) {
155 $subject = sprintf( t('[Friendica:Notify] %1$s poked you') , $params['source_name']);
156 $preamble = sprintf( t('%1$s poked you at %2$s') , $params['source_name'], $sitename);
157 $epreamble = sprintf( t('%1$s [url=%2$s]poked you[/url].') ,
158 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
161 $subject = str_replace('poked', t($params['activity']), $subject);
162 $preamble = str_replace('poked', t($params['activity']), $preamble);
163 $epreamble = str_replace('poked', t($params['activity']), $epreamble);
165 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
166 $tsitelink = sprintf( $sitelink, $siteurl );
167 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
168 $itemlink = $params['link'];
171 if($params['type'] == NOTIFY_TAGSHARE) {
172 $subject = sprintf( t('[Friendica:Notify] %s tagged your post') , $params['source_name']);
173 $preamble = sprintf( t('%1$s tagged your post at %2$s') , $params['source_name'], $sitename);
174 $epreamble = sprintf( t('%1$s tagged [url=%2$s]your post[/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_INTRO) {
185 $subject = sprintf( t('[Friendica:Notify] Introduction received'));
186 $preamble = sprintf( t('You\'ve received an introduction from \'%1$s\' at %2$s'), $params['source_name'], $sitename);
187 $epreamble = sprintf( t('You\'ve received [url=%1$s]an introduction[/url] from %2$s.'),
189 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
190 $body = sprintf( t('You may visit their profile at %s'),$params['source_link']);
192 $sitelink = t('Please visit %s to approve or reject the introduction.');
193 $tsitelink = sprintf( $sitelink, $siteurl );
194 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
195 $itemlink = $params['link'];
198 if($params['type'] == NOTIFY_SUGGEST) {
199 $subject = sprintf( t('[Friendica:Notify] Friend suggestion received'));
200 $preamble = sprintf( t('You\'ve received a friend suggestion from \'%1$s\' at %2$s'), $params['source_name'], $sitename);
201 $epreamble = sprintf( t('You\'ve received [url=%1$s]a friend suggestion[/url] for %2$s from %3$s.'),
203 '[url=' . $params['item']['url'] . ']' . $params['item']['name'] . '[/url]',
204 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
206 $body = t('Name:') . ' ' . $params['item']['name'] . "\n";
207 $body .= t('Photo:') . ' ' . $params['item']['photo'] . "\n";
208 $body .= sprintf( t('You may visit their profile at %s'),$params['item']['url']);
210 $sitelink = t('Please visit %s to approve or reject the suggestion.');
211 $tsitelink = sprintf( $sitelink, $siteurl );
212 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
213 $itemlink = $params['link'];
216 if($params['type'] == NOTIFY_CONFIRM) {
220 if($params['type'] == NOTIFY_SYSTEM) {
226 'subject' => $subject,
227 'preamble' => $preamble,
228 'epreamble' => $epreamble,
230 'sitelink' => $sitelink,
231 'tsitelink' => $tsitelink,
232 'hsitelink' => $hsitelink,
233 'itemlink' => $itemlink
236 call_hooks('enotify',$h);
238 $subject = $h['subject'];
239 $preamble = $h['preamble'];
240 $epreamble = $h['epreamble'];
242 $sitelink = $h['sitelink'];
243 $tsitelink = $h['tsitelink'];
244 $hsitelink = $h['hsitelink'];
245 $itemlink = $h['itemlink'];
248 require_once('include/html2bbcode.php');
252 $hash = random_string();
253 $r = q("SELECT `id` FROM `notify` WHERE `hash` = '%s' LIMIT 1",
257 } while($dups == true);
261 $datarray['hash'] = $hash;
262 $datarray['name'] = $params['source_name'];
263 $datarray['url'] = $params['source_link'];
264 $datarray['photo'] = $params['source_photo'];
265 $datarray['date'] = datetime_convert();
266 $datarray['uid'] = $params['uid'];
267 $datarray['link'] = $itemlink;
268 $datarray['parent'] = $parent_id;
269 $datarray['type'] = $params['type'];
270 $datarray['verb'] = $params['verb'];
271 $datarray['otype'] = $params['otype'];
273 call_hooks('enotify_store', $datarray);
275 // create notification entry in DB
277 $r = q("insert into notify (hash,name,url,photo,date,uid,link,parent,type,verb,otype)
278 values('%s','%s','%s','%s','%s',%d,'%s',%d,%d,'%s','%s')",
279 dbesc($datarray['hash']),
280 dbesc($datarray['name']),
281 dbesc($datarray['url']),
282 dbesc($datarray['photo']),
283 dbesc($datarray['date']),
284 intval($datarray['uid']),
285 dbesc($datarray['link']),
286 intval($datarray['parent']),
287 intval($datarray['type']),
288 dbesc($datarray['verb']),
289 dbesc($datarray['otype'])
292 $r = q("select id from notify where hash = '%s' and uid = %d limit 1",
294 intval($params['uid'])
297 $notify_id = $r[0]['id'];
303 // we seem to have a lot of duplicate comment notifications due to race conditions, mostly from forums
304 // After we've stored everything, look again to see if there are any duplicates and if so remove them
307 $p = q("select id from notify where ( type = %d or type = %d ) and link = '%s' and uid = %d order by id",
308 intval(NOTIFY_TAGSELF),
309 intval(NOTIFY_COMMENT),
310 dbesc($params['link']),
311 intval($params['uid'])
313 if($p && (count($p) > 1)) {
314 for ($d = 1; $d < count($p); $d ++) {
315 q("delete from notify where id = %d limit 1",
320 // only continue on if we stored the first one
322 if($notify_id != $p[0]['id']) {
335 $itemlink = $a->get_baseurl() . '/notify/view/' . $notify_id;
336 $msg = replace_macros($epreamble,array('$itemlink' => $itemlink));
337 $r = q("update notify set msg = '%s' where id = %d and uid = %d limit 1",
340 intval($params['uid'])
344 // send email notification if notification preferences permit
346 require_once('include/bbcode.php');
347 if((intval($params['notify_flags']) & intval($params['type'])) || $params['type'] == NOTIFY_SYSTEM) {
349 logger('notification: sending notification email');
351 $id_for_parent = "${params['parent']}@${hostname}";
353 // Is this the first email notification for this parent item and user?
355 $r = q("select `id` from `notify-threads` where `master-parent-item` = %d and `receiver-uid` = %d limit 1",
356 intval($params['parent']),
357 intval($params['uid']) );
359 // If so, create the record of it and use a message-id smtp header.
362 logger("notify_id:" . intval($notify_id). ", parent: " . intval($params['parent']) . "uid: " .
363 intval($params['uid']), LOGGER_DEBUG);
364 $r = q("insert into `notify-threads` (`notify-id`, `master-parent-item`, `receiver-uid`, `parent-item`)
365 values(%d,%d,%d,%d)",
367 intval($params['parent']),
368 intval($params['uid']),
371 $additional_mail_header .= "Message-ID: <${id_for_parent}>\n";
372 $log_msg = "include/enotify: No previous notification found for this parent:\n" .
373 " parent: ${params['parent']}\n" . " uid : ${params['uid']}\n";
374 logger($log_msg, LOGGER_DEBUG);
377 // If not, just "follow" the thread.
380 $additional_mail_header = "References: <${id_for_parent}>\nIn-Reply-To: <${id_for_parent}>\n";
381 logger("include/enotify: There's already a notification for this parent:\n" . print_r($r, true), LOGGER_DEBUG);
386 $textversion = strip_tags(html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",
387 $body))),ENT_QUOTES,'UTF-8'));
388 $htmlversion = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"),
389 "<br />\n",$body))));
392 $datarray['banner'] = $banner;
393 $datarray['product'] = $product;
394 $datarray['preamble'] = $preamble;
395 $datarray['sitename'] = $sitename;
396 $datarray['siteurl'] = $siteurl;
397 $datarray['type'] = $params['type'];
398 $datarray['parent'] = $params['parent'];
399 $datarray['source_name'] = $params['source_name'];
400 $datarray['source_link'] = $params['source_link'];
401 $datarray['source_photo'] = $params['source_photo'];
402 $datarray['uid'] = $params['uid'];
403 $datarray['username'] = $params['to_name'];
404 $datarray['hsitelink'] = $hsitelink;
405 $datarray['tsitelink'] = $tsitelink;
406 $datarray['hitemlink'] = '<a href="' . $itemlink . '">' . $itemlink . '</a>';
407 $datarray['titemlink'] = $itemlink;
408 $datarray['thanks'] = $thanks;
409 $datarray['site_admin'] = $site_admin;
410 $datarray['title'] = stripslashes($title);
411 $datarray['htmlversion'] = $htmlversion;
412 $datarray['textversion'] = $textversion;
413 $datarray['subject'] = $subject;
414 $datarray['headers'] = $additional_mail_header;
416 call_hooks('enotify_mail', $datarray);
418 // load the template for private message notifications
419 $tpl = get_markup_template('email_notify_html.tpl');
420 $email_html_body = replace_macros($tpl,array(
421 '$banner' => $datarray['banner'],
422 '$product' => $datarray['product'],
423 '$preamble' => $datarray['preamble'],
424 '$sitename' => $datarray['sitename'],
425 '$siteurl' => $datarray['siteurl'],
426 '$source_name' => $datarray['source_name'],
427 '$source_link' => $datarray['source_link'],
428 '$source_photo' => $datarray['source_photo'],
429 '$username' => $datarray['to_name'],
430 '$hsitelink' => $datarray['hsitelink'],
431 '$hitemlink' => $datarray['hitemlink'],
432 '$thanks' => $datarray['thanks'],
433 '$site_admin' => $datarray['site_admin'],
434 '$title' => $datarray['title'],
435 '$htmlversion' => $datarray['htmlversion'],
438 // load the template for private message notifications
439 $tpl = get_markup_template('email_notify_text.tpl');
440 $email_text_body = replace_macros($tpl,array(
441 '$banner' => $datarray['banner'],
442 '$product' => $datarray['product'],
443 '$preamble' => $datarray['preamble'],
444 '$sitename' => $datarray['sitename'],
445 '$siteurl' => $datarray['siteurl'],
446 '$source_name' => $datarray['source_name'],
447 '$source_link' => $datarray['source_link'],
448 '$source_photo' => $datarray['source_photo'],
449 '$username' => $datarray['to_name'],
450 '$tsitelink' => $datarray['tsitelink'],
451 '$titemlink' => $datarray['titemlink'],
452 '$thanks' => $datarray['thanks'],
453 '$site_admin' => $datarray['site_admin'],
454 '$title' => $datarray['title'],
455 '$textversion' => $datarray['textversion'],
458 // logger('text: ' . $email_text_body);
460 // use the EmailNotification library to send the message
463 'fromName' => $sender_name,
464 'fromEmail' => $sender_email,
465 'replyTo' => $sender_email,
466 'toEmail' => $params['to_email'],
467 'messageSubject' => $datarray['subject'],
468 'htmlVersion' => $email_html_body,
469 'textVersion' => $email_text_body,
470 'additionalMailHeader' => $datarray['headers'],
478 require_once('include/email.php');
482 * Send a multipart/alternative message with Text and HTML versions
484 * @param fromName name of the sender
485 * @param fromEmail email fo the sender
486 * @param replyTo replyTo address to direct responses
487 * @param toEmail destination email address
488 * @param messageSubject subject of the message
489 * @param htmlVersion html version of the message
490 * @param textVersion text only version of the message
491 * @param additionalMailHeader additions to the smtp mail header
493 static public function send($params) {
495 $fromName = email_header_encode(html_entity_decode($params['fromName'],ENT_QUOTES,'UTF-8'),'UTF-8');
496 $messageSubject = email_header_encode(html_entity_decode($params['messageSubject'],ENT_QUOTES,'UTF-8'),'UTF-8');
498 // generate a mime boundary
499 $mimeBoundary =rand(0,9)."-"
500 .rand(10000000000,9999999999)."-"
501 .rand(10000000000,9999999999)."=:"
504 // generate a multipart/alternative message header
506 $params['additionalMailHeader'] .
507 "From: $fromName <{$params['fromEmail']}>\n" .
508 "Reply-To: $fromName <{$params['replyTo']}>\n" .
509 "MIME-Version: 1.0\n" .
510 "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
512 // assemble the final multipart message body with the text and html types included
513 $textBody = chunk_split(base64_encode($params['textVersion']));
514 $htmlBody = chunk_split(base64_encode($params['htmlVersion']));
515 $multipartMessageBody =
516 "--" . $mimeBoundary . "\n" . // plain text section
517 "Content-Type: text/plain; charset=UTF-8\n" .
518 "Content-Transfer-Encoding: base64\n\n" .
520 "--" . $mimeBoundary . "\n" . // text/html section
521 "Content-Type: text/html; charset=UTF-8\n" .
522 "Content-Transfer-Encoding: base64\n\n" .
524 "--" . $mimeBoundary . "--\n"; // message ending
528 $params['toEmail'], // send to address
529 $messageSubject, // subject
530 $multipartMessageBody, // message body
531 $messageHeader // message headers
533 logger("notification: enotify::send header " . 'To: ' . $params['toEmail'] . "\n" . $messageHeader, LOGGER_DEBUG);
534 logger("notification: enotify::send returns " . $res, LOGGER_DEBUG);