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_TAGSHARE) {
151 $subject = sprintf( t('[Friendica:Notify] %s tagged your post') , $params['source_name']);
152 $preamble = sprintf( t('%1$s tagged your post at %2$s') , $params['source_name'], $sitename);
153 $epreamble = sprintf( t('%1$s tagged [url=%2$s]your post[/url]') ,
154 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
157 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
158 $tsitelink = sprintf( $sitelink, $siteurl );
159 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
160 $itemlink = $params['link'];
163 if($params['type'] == NOTIFY_INTRO) {
164 $subject = sprintf( t('[Friendica:Notify] Introduction received'));
165 $preamble = sprintf( t('You\'ve received an introduction from \'%1$s\' at %2$s'), $params['source_name'], $sitename);
166 $epreamble = sprintf( t('You\'ve received [url=%1$s]an introduction[/url] from %2$s.'),
168 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
169 $body = sprintf( t('You may visit their profile at %s'),$params['source_link']);
171 $sitelink = t('Please visit %s to approve or reject the introduction.');
172 $tsitelink = sprintf( $sitelink, $siteurl );
173 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
174 $itemlink = $params['link'];
177 if($params['type'] == NOTIFY_SUGGEST) {
178 $subject = sprintf( t('[Friendica:Notify] Friend suggestion received'));
179 $preamble = sprintf( t('You\'ve received a friend suggestion from \'%1$s\' at %2$s'), $params['source_name'], $sitename);
180 $epreamble = sprintf( t('You\'ve received [url=%1$s]a friend suggestion[/url] for %2$s from %3$s.'),
182 '[url=' . $params['item']['url'] . ']' . $params['item']['name'] . '[/url]',
183 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
185 $body = t('Name:') . ' ' . $params['item']['name'] . "\n";
186 $body .= t('Photo:') . ' ' . $params['item']['photo'] . "\n";
187 $body .= sprintf( t('You may visit their profile at %s'),$params['item']['url']);
189 $sitelink = t('Please visit %s to approve or reject the suggestion.');
190 $tsitelink = sprintf( $sitelink, $siteurl );
191 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
192 $itemlink = $params['link'];
195 if($params['type'] == NOTIFY_CONFIRM) {
199 if($params['type'] == NOTIFY_SYSTEM) {
205 'subject' => $subject,
206 'preamble' => $preamble,
207 'epreamble' => $epreamble,
209 'sitelink' => $sitelink,
210 'tsitelink' => $tsitelink,
211 'hsitelink' => $hsitelink,
212 'itemlink' => $itemlink
215 call_hooks('enotify',$h);
217 $subject = $h['subject'];
218 $preamble = $h['preamble'];
219 $epreamble = $h['epreamble'];
221 $sitelink = $h['sitelink'];
222 $tsitelink = $h['tsitelink'];
223 $hsitelink = $h['hsitelink'];
224 $itemlink = $h['itemlink'];
227 require_once('include/html2bbcode.php');
231 $hash = random_string();
232 $r = q("SELECT `id` FROM `notify` WHERE `hash` = '%s' LIMIT 1",
236 } while($dups == true);
240 $datarray['hash'] = $hash;
241 $datarray['name'] = $params['source_name'];
242 $datarray['url'] = $params['source_link'];
243 $datarray['photo'] = $params['source_photo'];
244 $datarray['date'] = datetime_convert();
245 $datarray['uid'] = $params['uid'];
246 $datarray['link'] = $itemlink;
247 $datarray['parent'] = $parent_id;
248 $datarray['type'] = $params['type'];
249 $datarray['verb'] = $params['verb'];
250 $datarray['otype'] = $params['otype'];
252 call_hooks('enotify_store', $datarray);
254 // create notification entry in DB
256 $r = q("insert into notify (hash,name,url,photo,date,uid,link,parent,type,verb,otype)
257 values('%s','%s','%s','%s','%s',%d,'%s',%d,%d,'%s','%s')",
258 dbesc($datarray['hash']),
259 dbesc($datarray['name']),
260 dbesc($datarray['url']),
261 dbesc($datarray['photo']),
262 dbesc($datarray['date']),
263 intval($datarray['uid']),
264 dbesc($datarray['link']),
265 intval($datarray['parent']),
266 intval($datarray['type']),
267 dbesc($datarray['verb']),
268 dbesc($datarray['otype'])
271 $r = q("select id from notify where hash = '%s' and uid = %d limit 1",
273 intval($params['uid'])
276 $notify_id = $r[0]['id'];
282 $itemlink = $a->get_baseurl() . '/notify/view/' . $notify_id;
283 $msg = replace_macros($epreamble,array('$itemlink' => $itemlink));
284 $r = q("update notify set msg = '%s' where id = %d and uid = %d limit 1",
287 intval($params['uid'])
291 // send email notification if notification preferences permit
293 require_once('bbcode.php');
294 if((intval($params['notify_flags']) & intval($params['type'])) || $params['type'] == NOTIFY_SYSTEM) {
296 logger('notification: sending notification email');
298 $id_for_parent = "${params['parent']}@${hostname}";
300 // Is this the first email notification for this parent item and user?
302 $r = q("select `id` from `notify-threads` where `master-parent-item` = %d and `receiver-uid` = %d limit 1",
303 intval($params['parent']),
304 intval($params['uid']) );
306 // If so, create the record of it and use a message-id smtp header.
309 logger("norify_id:" . intval($notify_id). ", parent: " . intval($params['parent']) . "uid: " .
310 intval($params['uid']), LOGGER_DEBUG);
311 $r = q("insert into `notify-threads` (`notify-id`, `master-parent-item`, `receiver-uid`, `parent-item`)
312 values(%d,%d,%d,%d)",
314 intval($params['parent']),
315 intval($params['uid']),
318 $additional_mail_header .= "Message-ID: <${id_for_parent}>\n";
319 $log_msg = "include/enotify: No previous notification found for this parent:\n" .
320 " parent: ${params['parent']}\n" . " uid : ${params['uid']}\n";
321 logger($log_msg, LOGGER_DEBUG);
324 // If not, just "follow" the thread.
327 $additional_mail_header = "References: <${id_for_parent}>\nIn-Reply-To: <${id_for_parent}>\n";
328 logger("include/enotify: There's already a notification for this parent:\n" . print_r($r, true), LOGGER_DEBUG);
333 $textversion = strip_tags(html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",
334 $body))),ENT_QUOTES,'UTF-8'));
335 $htmlversion = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"),
336 "<br />\n",$body))));
339 $datarray['banner'] = $banner;
340 $datarray['product'] = $product;
341 $datarray['preamble'] = $preamble;
342 $datarray['sitename'] = $sitename;
343 $datarray['siteurl'] = $siteurl;
344 $datarray['type'] = $params['type'];
345 $datarray['parent'] = $params['parent'];
346 $datarray['source_name'] = $params['source_name'];
347 $datarray['source_link'] = $params['source_link'];
348 $datarray['source_photo'] = $params['source_photo'];
349 $datarray['uid'] = $params['uid'];
350 $datarray['username'] = $params['to_name'];
351 $datarray['hsitelink'] = $hsitelink;
352 $datarray['tsitelink'] = $tsitelink;
353 $datarray['hitemlink'] = '<a href="' . $itemlink . '">' . $itemlink . '</a>';
354 $datarray['titemlink'] = $itemlink;
355 $datarray['thanks'] = $thanks;
356 $datarray['site_admin'] = $site_admin;
357 $datarray['title'] = stripslashes($title);
358 $datarray['htmlversion'] = $htmlversion;
359 $datarray['textversion'] = $textversion;
360 $datarray['subject'] = $subject;
361 $datarray['headers'] = $additional_mail_header;
363 call_hooks('enotify_mail', $datarray);
365 // load the template for private message notifications
366 $tpl = get_markup_template('email_notify_html.tpl');
367 $email_html_body = replace_macros($tpl,array(
368 '$banner' => $datarray['banner'],
369 '$product' => $datarray['product'],
370 '$preamble' => $datarray['preamble'],
371 '$sitename' => $datarray['sitename'],
372 '$siteurl' => $datarray['siteurl'],
373 '$source_name' => $datarray['source_name'],
374 '$source_link' => $datarray['source_link'],
375 '$source_photo' => $datarray['source_photo'],
376 '$username' => $datarray['to_name'],
377 '$hsitelink' => $datarray['hsitelink'],
378 '$hitemlink' => $datarray['hitemlink'],
379 '$thanks' => $datarray['thanks'],
380 '$site_admin' => $datarray['site_admin'],
381 '$title' => $datarray['title'],
382 '$htmlversion' => $datarray['htmlversion'],
385 // load the template for private message notifications
386 $tpl = get_markup_template('email_notify_text.tpl');
387 $email_text_body = replace_macros($tpl,array(
388 '$banner' => $datarray['banner'],
389 '$product' => $datarray['product'],
390 '$preamble' => $datarray['preamble'],
391 '$sitename' => $datarray['sitename'],
392 '$siteurl' => $datarray['siteurl'],
393 '$source_name' => $datarray['source_name'],
394 '$source_link' => $datarray['source_link'],
395 '$source_photo' => $datarray['source_photo'],
396 '$username' => $datarray['to_name'],
397 '$tsitelink' => $datarray['tsitelink'],
398 '$titemlink' => $datarray['titemlink'],
399 '$thanks' => $datarray['thanks'],
400 '$site_admin' => $datarray['site_admin'],
401 '$title' => $datarray['title'],
402 '$textversion' => $datarray['textversion'],
405 // logger('text: ' . $email_text_body);
407 // use the EmailNotification library to send the message
410 'fromName' => $sender_name,
411 'fromEmail' => $sender_email,
412 'replyTo' => $sender_email,
413 'toEmail' => $params['to_email'],
414 'messageSubject' => $datarray['subject'],
415 'htmlVersion' => $email_html_body,
416 'textVersion' => $email_text_body,
417 'additionalMailHeader' => $datarray['headers'],
425 require_once('include/email.php');
429 * Send a multipart/alternative message with Text and HTML versions
431 * @param fromName name of the sender
432 * @param fromEmail email fo the sender
433 * @param replyTo replyTo address to direct responses
434 * @param toEmail destination email address
435 * @param messageSubject subject of the message
436 * @param htmlVersion html version of the message
437 * @param textVersion text only version of the message
438 * @param additionalMailHeader additions to the smtp mail header
440 static public function send($params) {
442 $fromName = email_header_encode(html_entity_decode($params['fromName'],ENT_QUOTES,'UTF-8'),'UTF-8');
443 $messageSubject = email_header_encode(html_entity_decode($params['messageSubject'],ENT_QUOTES,'UTF-8'),'UTF-8');
445 // generate a mime boundary
446 $mimeBoundary =rand(0,9)."-"
447 .rand(10000000000,9999999999)."-"
448 .rand(10000000000,9999999999)."=:"
451 // generate a multipart/alternative message header
453 $params['additionalMailHeader'] .
454 "From: {$params['fromName']} <{$params['fromEmail']}>\n" .
455 "Reply-To: {$params['fromName']} <{$params['replyTo']}>\n" .
456 "MIME-Version: 1.0\n" .
457 "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
459 // assemble the final multipart message body with the text and html types included
460 $textBody = chunk_split(base64_encode($params['textVersion']));
461 $htmlBody = chunk_split(base64_encode($params['htmlVersion']));
462 $multipartMessageBody =
463 "--" . $mimeBoundary . "\n" . // plain text section
464 "Content-Type: text/plain; charset=UTF-8\n" .
465 "Content-Transfer-Encoding: base64\n\n" .
467 "--" . $mimeBoundary . "\n" . // text/html section
468 "Content-Type: text/html; charset=UTF-8\n" .
469 "Content-Transfer-Encoding: base64\n\n" .
471 "--" . $mimeBoundary . "--\n"; // message ending
475 $params['toEmail'], // send to address
476 $params['messageSubject'], // subject
477 $multipartMessageBody, // message body
478 $messageHeader // message headers
480 logger("notification: enotify::send returns " . $res, LOGGER_DEBUG);