3 function notification($params) {
5 logger('notification: entry', LOGGER_DEBUG);
9 // from here on everything is in the recipients language
11 push_lang($params['language']);
14 $banner = t('Friendica Notification');
15 $product = FRIENDICA_PLATFORM;
16 $siteurl = $a->get_baseurl(true);
17 $thanks = t('Thank You,');
18 $sitename = get_config('config','sitename');
19 $site_admin = sprintf( t('%s Administrator'), $sitename);
21 $sender_name = $product;
22 $hostname = $a->get_hostname();
23 if(strpos($hostname,':'))
24 $hostname = substr($hostname,0,strpos($hostname,':'));
26 $sender_email = t('noreply') . '@' . $hostname;
27 $additional_mail_header = "";
29 if(array_key_exists('item',$params)) {
30 $title = $params['item']['title'];
31 $body = $params['item']['body'];
37 // e.g. "your post", "David's photo", etc.
38 $possess_desc = t('%s <!item_type!>');
40 if($params['type'] == NOTIFY_MAIL) {
42 $subject = sprintf( t('[Friendica:Notify] New mail received at %s'),$sitename);
44 $preamble = sprintf( t('%1$s sent you a new private message at %2$s.'),$params['source_name'],$sitename);
45 $epreamble = sprintf( t('%1$s sent you %2$s.'),'[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=$itemlink]' . t('a private message') . '[/url]');
46 $sitelink = t('Please visit %s to view and/or reply to your private messages.');
47 $tsitelink = sprintf( $sitelink, $siteurl . '/message/' . $params['item']['id'] );
48 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '/message/' . $params['item']['id'] . '">' . $sitename . '</a>');
49 $itemlink = $siteurl . '/message/' . $params['item']['id'];
52 if($params['type'] == NOTIFY_COMMENT) {
53 // logger("notification: params = " . print_r($params, true), LOGGER_DEBUG);
55 $parent_id = $params['parent'];
57 // Check to see if there was already a tag notify for this post.
58 // If so don't create a second notification
61 $p = q("select id from notify where type = %d and link = '%s' and uid = %d limit 1",
62 intval(NOTIFY_TAGSELF),
63 dbesc($params['link']),
64 intval($params['uid'])
66 if($p and count($p)) {
72 // if it's a post figure out who's post it is.
76 if($params['otype'] === 'item' && $parent_id) {
77 $p = q("select * from item where id = %d and uid = %d limit 1",
79 intval($params['uid'])
83 $item_post_type = item_post_type($p[0]);
84 //$possess_desc = str_replace('<!item_type!>',$possess_desc);
87 $dest_str = sprintf(t('%1$s commented on [url=%2$s]a %3$s[/url]'),
88 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
92 // "George Bull's post"
94 $dest_str = sprintf(t('%1$s commented on [url=%2$s]%3$s\'s %4$s[/url]'),
95 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
101 if($p[0]['owner-name'] == $p[0]['author-name'] && $p[0]['wall'])
102 $dest_str = sprintf(t('%1$s commented on [url=%2$s]your %3$s[/url]'),
103 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
107 // Some mail softwares relies on subject field for threading.
108 // So, we cannot have different subjects for notifications of the same thread.
109 // Before this we have the name of the replier on the subject rendering
110 // differents subjects for messages on the same thread.
112 $subject = sprintf( t('[Friendica:Notify] Comment to conversation #%1$d by %2$s'), $parent_id, $params['source_name']);
113 $preamble = sprintf( t('%s commented on an item/conversation you have been following.'), $params['source_name']);
114 $epreamble = $dest_str;
116 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
117 $tsitelink = sprintf( $sitelink, $siteurl );
118 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
119 $itemlink = $params['link'];
122 if($params['type'] == NOTIFY_WALL) {
123 $subject = sprintf( t('[Friendica:Notify] %s posted to your profile wall') , $params['source_name']);
125 $preamble = sprintf( t('%1$s posted to your profile wall at %2$s') , $params['source_name'], $sitename);
127 $epreamble = sprintf( t('%1$s posted to [url=%2$s]your wall[/url]') ,
128 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
131 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
132 $tsitelink = sprintf( $sitelink, $siteurl );
133 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
134 $itemlink = $params['link'];
137 if($params['type'] == NOTIFY_TAGSELF) {
138 $subject = sprintf( t('[Friendica:Notify] %s tagged you') , $params['source_name']);
139 $preamble = sprintf( t('%1$s tagged you at %2$s') , $params['source_name'], $sitename);
140 $epreamble = sprintf( t('%1$s [url=%2$s]tagged you[/url].') ,
141 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
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_POKE) {
152 $subject = sprintf( t('[Friendica:Notify] %1$s poked you') , $params['source_name']);
153 $preamble = sprintf( t('%1$s poked you at %2$s') , $params['source_name'], $sitename);
154 $epreamble = sprintf( t('%1$s [url=%2$s]poked you[/url].') ,
155 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
158 $subject = str_replace('poked', t($params['activity']), $subject);
159 $preamble = str_replace('poked', t($params['activity']), $preamble);
160 $epreamble = str_replace('poked', t($params['activity']), $epreamble);
162 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
163 $tsitelink = sprintf( $sitelink, $siteurl );
164 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
165 $itemlink = $params['link'];
168 if($params['type'] == NOTIFY_TAGSHARE) {
169 $subject = sprintf( t('[Friendica:Notify] %s tagged your post') , $params['source_name']);
170 $preamble = sprintf( t('%1$s tagged your post at %2$s') , $params['source_name'], $sitename);
171 $epreamble = sprintf( t('%1$s tagged [url=%2$s]your post[/url]') ,
172 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
175 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
176 $tsitelink = sprintf( $sitelink, $siteurl );
177 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
178 $itemlink = $params['link'];
181 if($params['type'] == NOTIFY_INTRO) {
182 $subject = sprintf( t('[Friendica:Notify] Introduction received'));
183 $preamble = sprintf( t('You\'ve received an introduction from \'%1$s\' at %2$s'), $params['source_name'], $sitename);
184 $epreamble = sprintf( t('You\'ve received [url=%1$s]an introduction[/url] from %2$s.'),
186 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
187 $body = sprintf( t('You may visit their profile at %s'),$params['source_link']);
189 $sitelink = t('Please visit %s to approve or reject the introduction.');
190 $tsitelink = sprintf( $sitelink, $siteurl );
191 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
192 $itemlink = $params['link'];
195 if($params['type'] == NOTIFY_SUGGEST) {
196 $subject = sprintf( t('[Friendica:Notify] Friend suggestion received'));
197 $preamble = sprintf( t('You\'ve received a friend suggestion from \'%1$s\' at %2$s'), $params['source_name'], $sitename);
198 $epreamble = sprintf( t('You\'ve received [url=%1$s]a friend suggestion[/url] for %2$s from %3$s.'),
200 '[url=' . $params['item']['url'] . ']' . $params['item']['name'] . '[/url]',
201 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
203 $body = t('Name:') . ' ' . $params['item']['name'] . "\n";
204 $body .= t('Photo:') . ' ' . $params['item']['photo'] . "\n";
205 $body .= sprintf( t('You may visit their profile at %s'),$params['item']['url']);
207 $sitelink = t('Please visit %s to approve or reject the suggestion.');
208 $tsitelink = sprintf( $sitelink, $siteurl );
209 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
210 $itemlink = $params['link'];
213 if($params['type'] == NOTIFY_CONFIRM) {
217 if($params['type'] == NOTIFY_SYSTEM) {
223 'subject' => $subject,
224 'preamble' => $preamble,
225 'epreamble' => $epreamble,
227 'sitelink' => $sitelink,
228 'tsitelink' => $tsitelink,
229 'hsitelink' => $hsitelink,
230 'itemlink' => $itemlink
233 call_hooks('enotify',$h);
235 $subject = $h['subject'];
236 $preamble = $h['preamble'];
237 $epreamble = $h['epreamble'];
239 $sitelink = $h['sitelink'];
240 $tsitelink = $h['tsitelink'];
241 $hsitelink = $h['hsitelink'];
242 $itemlink = $h['itemlink'];
245 require_once('include/html2bbcode.php');
249 $hash = random_string();
250 $r = q("SELECT `id` FROM `notify` WHERE `hash` = '%s' LIMIT 1",
254 } while($dups == true);
258 $datarray['hash'] = $hash;
259 $datarray['name'] = $params['source_name'];
260 $datarray['url'] = $params['source_link'];
261 $datarray['photo'] = $params['source_photo'];
262 $datarray['date'] = datetime_convert();
263 $datarray['uid'] = $params['uid'];
264 $datarray['link'] = $itemlink;
265 $datarray['parent'] = $parent_id;
266 $datarray['type'] = $params['type'];
267 $datarray['verb'] = $params['verb'];
268 $datarray['otype'] = $params['otype'];
270 call_hooks('enotify_store', $datarray);
272 // create notification entry in DB
274 $r = q("insert into notify (hash,name,url,photo,date,uid,link,parent,type,verb,otype)
275 values('%s','%s','%s','%s','%s',%d,'%s',%d,%d,'%s','%s')",
276 dbesc($datarray['hash']),
277 dbesc($datarray['name']),
278 dbesc($datarray['url']),
279 dbesc($datarray['photo']),
280 dbesc($datarray['date']),
281 intval($datarray['uid']),
282 dbesc($datarray['link']),
283 intval($datarray['parent']),
284 intval($datarray['type']),
285 dbesc($datarray['verb']),
286 dbesc($datarray['otype'])
289 $r = q("select id from notify where hash = '%s' and uid = %d limit 1",
291 intval($params['uid'])
294 $notify_id = $r[0]['id'];
300 $itemlink = $a->get_baseurl() . '/notify/view/' . $notify_id;
301 $msg = replace_macros($epreamble,array('$itemlink' => $itemlink));
302 $r = q("update notify set msg = '%s' where id = %d and uid = %d limit 1",
305 intval($params['uid'])
309 // send email notification if notification preferences permit
311 require_once('bbcode.php');
312 if((intval($params['notify_flags']) & intval($params['type'])) || $params['type'] == NOTIFY_SYSTEM) {
314 logger('notification: sending notification email');
316 $id_for_parent = "${params['parent']}@${hostname}";
318 // Is this the first email notification for this parent item and user?
320 $r = q("select `id` from `notify-threads` where `master-parent-item` = %d and `receiver-uid` = %d limit 1",
321 intval($params['parent']),
322 intval($params['uid']) );
324 // If so, create the record of it and use a message-id smtp header.
327 logger("norify_id:" . intval($notify_id). ", parent: " . intval($params['parent']) . "uid: " .
328 intval($params['uid']), LOGGER_DEBUG);
329 $r = q("insert into `notify-threads` (`notify-id`, `master-parent-item`, `receiver-uid`, `parent-item`)
330 values(%d,%d,%d,%d)",
332 intval($params['parent']),
333 intval($params['uid']),
336 $additional_mail_header .= "Message-ID: <${id_for_parent}>\n";
337 $log_msg = "include/enotify: No previous notification found for this parent:\n" .
338 " parent: ${params['parent']}\n" . " uid : ${params['uid']}\n";
339 logger($log_msg, LOGGER_DEBUG);
342 // If not, just "follow" the thread.
345 $additional_mail_header = "References: <${id_for_parent}>\nIn-Reply-To: <${id_for_parent}>\n";
346 logger("include/enotify: There's already a notification for this parent:\n" . print_r($r, true), LOGGER_DEBUG);
351 $textversion = strip_tags(html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",
352 $body))),ENT_QUOTES,'UTF-8'));
353 $htmlversion = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"),
354 "<br />\n",$body))));
357 $datarray['banner'] = $banner;
358 $datarray['product'] = $product;
359 $datarray['preamble'] = $preamble;
360 $datarray['sitename'] = $sitename;
361 $datarray['siteurl'] = $siteurl;
362 $datarray['type'] = $params['type'];
363 $datarray['parent'] = $params['parent'];
364 $datarray['source_name'] = $params['source_name'];
365 $datarray['source_link'] = $params['source_link'];
366 $datarray['source_photo'] = $params['source_photo'];
367 $datarray['uid'] = $params['uid'];
368 $datarray['username'] = $params['to_name'];
369 $datarray['hsitelink'] = $hsitelink;
370 $datarray['tsitelink'] = $tsitelink;
371 $datarray['hitemlink'] = '<a href="' . $itemlink . '">' . $itemlink . '</a>';
372 $datarray['titemlink'] = $itemlink;
373 $datarray['thanks'] = $thanks;
374 $datarray['site_admin'] = $site_admin;
375 $datarray['title'] = stripslashes($title);
376 $datarray['htmlversion'] = $htmlversion;
377 $datarray['textversion'] = $textversion;
378 $datarray['subject'] = $subject;
379 $datarray['headers'] = $additional_mail_header;
381 call_hooks('enotify_mail', $datarray);
383 // load the template for private message notifications
384 $tpl = get_markup_template('email_notify_html.tpl');
385 $email_html_body = replace_macros($tpl,array(
386 '$banner' => $datarray['banner'],
387 '$product' => $datarray['product'],
388 '$preamble' => $datarray['preamble'],
389 '$sitename' => $datarray['sitename'],
390 '$siteurl' => $datarray['siteurl'],
391 '$source_name' => $datarray['source_name'],
392 '$source_link' => $datarray['source_link'],
393 '$source_photo' => $datarray['source_photo'],
394 '$username' => $datarray['to_name'],
395 '$hsitelink' => $datarray['hsitelink'],
396 '$hitemlink' => $datarray['hitemlink'],
397 '$thanks' => $datarray['thanks'],
398 '$site_admin' => $datarray['site_admin'],
399 '$title' => $datarray['title'],
400 '$htmlversion' => $datarray['htmlversion'],
403 // load the template for private message notifications
404 $tpl = get_markup_template('email_notify_text.tpl');
405 $email_text_body = replace_macros($tpl,array(
406 '$banner' => $datarray['banner'],
407 '$product' => $datarray['product'],
408 '$preamble' => $datarray['preamble'],
409 '$sitename' => $datarray['sitename'],
410 '$siteurl' => $datarray['siteurl'],
411 '$source_name' => $datarray['source_name'],
412 '$source_link' => $datarray['source_link'],
413 '$source_photo' => $datarray['source_photo'],
414 '$username' => $datarray['to_name'],
415 '$tsitelink' => $datarray['tsitelink'],
416 '$titemlink' => $datarray['titemlink'],
417 '$thanks' => $datarray['thanks'],
418 '$site_admin' => $datarray['site_admin'],
419 '$title' => $datarray['title'],
420 '$textversion' => $datarray['textversion'],
423 // logger('text: ' . $email_text_body);
425 // use the EmailNotification library to send the message
428 'fromName' => $sender_name,
429 'fromEmail' => $sender_email,
430 'replyTo' => $sender_email,
431 'toEmail' => $params['to_email'],
432 'messageSubject' => $datarray['subject'],
433 'htmlVersion' => $email_html_body,
434 'textVersion' => $email_text_body,
435 'additionalMailHeader' => $datarray['headers'],
443 require_once('include/email.php');
447 * Send a multipart/alternative message with Text and HTML versions
449 * @param fromName name of the sender
450 * @param fromEmail email fo the sender
451 * @param replyTo replyTo address to direct responses
452 * @param toEmail destination email address
453 * @param messageSubject subject of the message
454 * @param htmlVersion html version of the message
455 * @param textVersion text only version of the message
456 * @param additionalMailHeader additions to the smtp mail header
458 static public function send($params) {
460 $fromName = email_header_encode(html_entity_decode($params['fromName'],ENT_QUOTES,'UTF-8'),'UTF-8');
461 $messageSubject = email_header_encode(html_entity_decode($params['messageSubject'],ENT_QUOTES,'UTF-8'),'UTF-8');
463 // generate a mime boundary
464 $mimeBoundary =rand(0,9)."-"
465 .rand(10000000000,9999999999)."-"
466 .rand(10000000000,9999999999)."=:"
469 // generate a multipart/alternative message header
471 $params['additionalMailHeader'] .
472 "From: {$params['fromName']} <{$params['fromEmail']}>\n" .
473 "Reply-To: {$params['fromName']} <{$params['replyTo']}>\n" .
474 "MIME-Version: 1.0\n" .
475 "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
477 // assemble the final multipart message body with the text and html types included
478 $textBody = chunk_split(base64_encode($params['textVersion']));
479 $htmlBody = chunk_split(base64_encode($params['htmlVersion']));
480 $multipartMessageBody =
481 "--" . $mimeBoundary . "\n" . // plain text section
482 "Content-Type: text/plain; charset=UTF-8\n" .
483 "Content-Transfer-Encoding: base64\n\n" .
485 "--" . $mimeBoundary . "\n" . // text/html section
486 "Content-Type: text/html; charset=UTF-8\n" .
487 "Content-Transfer-Encoding: base64\n\n" .
489 "--" . $mimeBoundary . "--\n"; // message ending
493 $params['toEmail'], // send to address
494 $params['messageSubject'], // subject
495 $multipartMessageBody, // message body
496 $messageHeader // message headers
498 logger("notification: enotify::send returns " . $res, LOGGER_DEBUG);