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'];
58 // if it's a post figure out who's post it is.
62 if($params['otype'] === 'item' && $parent_id) {
63 $p = q("select * from item where id = %d and uid = %d limit 1",
65 intval($params['uid'])
69 $item_post_type = item_post_type($p[0]);
70 //$possess_desc = str_replace('<!item_type!>',$possess_desc);
73 $dest_str = sprintf(t('%1$s commented on [url=%2$s]a %3$s[/url]'),
74 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
78 // "George Bull's post"
80 $dest_str = sprintf(t('%1$s commented on [url=%2$s]%3$s\'s %4$s[/url]'),
81 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
87 if($p[0]['owner-name'] == $p[0]['author-name'] && $p[0]['wall'])
88 $dest_str = sprintf(t('%1$s commented on [url=%2$s]your %3$s[/url]'),
89 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
93 // Some mail softwares relies on subject field for threading.
94 // So, we cannot have different subjects for notifications of the same thread.
95 // Before this we have the name of the replier on the subject rendering
96 // differents subjects for messages on the same thread.
98 $subject = sprintf( t('[Friendica:Notify] Comment to conversation #%1$d by %2$s'), $parent_id, $params['source_name']);
99 $preamble = sprintf( t('%s commented on an item/conversation you have been following.'), $params['source_name']);
100 $epreamble = $dest_str;
102 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
103 $tsitelink = sprintf( $sitelink, $siteurl );
104 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
105 $itemlink = $params['link'];
108 if($params['type'] == NOTIFY_WALL) {
109 $subject = sprintf( t('[Friendica:Notify] %s posted to your profile wall') , $params['source_name']);
111 $preamble = sprintf( t('%1$s posted to your profile wall at %2$s') , $params['source_name'], $sitename);
113 $epreamble = sprintf( t('%1$s posted to [url=%2s]your wall[/url]') ,
114 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
117 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
118 $tsitelink = sprintf( $sitelink, $siteurl );
119 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
120 $itemlink = $params['link'];
123 if($params['type'] == NOTIFY_TAGSELF) {
124 $subject = sprintf( t('[Friendica:Notify] %s tagged you') , $params['source_name']);
125 $preamble = sprintf( t('%1$s tagged you at %2$s') , $params['source_name'], $sitename);
126 $epreamble = sprintf( t('%1$s [url=%2$s]tagged you[/url].') ,
127 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
130 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
131 $tsitelink = sprintf( $sitelink, $siteurl );
132 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
133 $itemlink = $params['link'];
136 if($params['type'] == NOTIFY_TAGSHARE) {
137 $subject = sprintf( t('[Friendica:Notify] %s tagged your post') , $params['source_name']);
138 $preamble = sprintf( t('%1$s tagged your post at %2$s') , $params['source_name'], $sitename);
139 $epreamble = sprintf( t('%1$s tagged [url=%2$s]your post[/url]') ,
140 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
143 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
144 $tsitelink = sprintf( $sitelink, $siteurl );
145 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
146 $itemlink = $params['link'];
149 if($params['type'] == NOTIFY_INTRO) {
150 $subject = sprintf( t('[Friendica:Notify] Introduction received'));
151 $preamble = sprintf( t('You\'ve received an introduction from \'%1$s\' at %2$s'), $params['source_name'], $sitename);
152 $epreamble = sprintf( t('You\'ve received [url=%1$s]an introduction[/url] from %2$s.'),
154 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
155 $body = sprintf( t('You may visit their profile at %s'),$params['source_link']);
157 $sitelink = t('Please visit %s to approve or reject the introduction.');
158 $tsitelink = sprintf( $sitelink, $siteurl );
159 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
160 $itemlink = $params['link'];
163 if($params['type'] == NOTIFY_SUGGEST) {
164 $subject = sprintf( t('[Friendica:Notify] Friend suggestion received'));
165 $preamble = sprintf( t('You\'ve received a friend suggestion from \'%1$s\' at %2$s'), $params['source_name'], $sitename);
166 $epreamble = sprintf( t('You\'ve received [url=%1$s]a friend suggestion[/url] for %2$s from %3$s.'),
168 '[url=' . $params['item']['url'] . ']' . $params['item']['name'] . '[/url]',
169 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
171 $body = t('Name:') . ' ' . $params['item']['name'] . "\n";
172 $body .= t('Photo:') . ' ' . $params['item']['photo'] . "\n";
173 $body .= sprintf( t('You may visit their profile at %s'),$params['item']['url']);
175 $sitelink = t('Please visit %s to approve or reject the suggestion.');
176 $tsitelink = sprintf( $sitelink, $siteurl );
177 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
178 $itemlink = $params['link'];
181 if($params['type'] == NOTIFY_CONFIRM) {
185 if($params['type'] == NOTIFY_SYSTEM) {
191 'subject' => $subject,
192 'preamble' => $preamble,
193 'epreamble' => $epreamble,
195 'sitelink' => $sitelink,
196 'tsitelink' => $tsitelink,
197 'hsitelink' => $hsitelink,
198 'itemlink' => $itemlink
201 call_hooks('enotify',$h);
203 $subject = $h['subject'];
204 $preamble = $h['preamble'];
205 $epreamble = $h['epreamble'];
207 $sitelink = $h['sitelink'];
208 $tsitelink = $h['tsitelink'];
209 $hsitelink = $h['hsitelink'];
210 $itemlink = $h['itemlink'];
213 require_once('include/html2bbcode.php');
217 $hash = random_string();
218 $r = q("SELECT `id` FROM `notify` WHERE `hash` = '%s' LIMIT 1",
222 } while($dups == true);
226 $datarray['hash'] = $hash;
227 $datarray['name'] = $params['source_name'];
228 $datarray['url'] = $params['source_link'];
229 $datarray['photo'] = $params['source_photo'];
230 $datarray['date'] = datetime_convert();
231 $datarray['uid'] = $params['uid'];
232 $datarray['link'] = $itemlink;
233 $datarray['parent'] = $parent_id;
234 $datarray['type'] = $params['type'];
235 $datarray['verb'] = $params['verb'];
236 $datarray['otype'] = $params['otype'];
238 call_hooks('enotify_store', $datarray);
240 // create notification entry in DB
242 $r = q("insert into notify (hash,name,url,photo,date,uid,link,parent,type,verb,otype)
243 values('%s','%s','%s','%s','%s',%d,'%s',%d,%d,'%s','%s')",
244 dbesc($datarray['hash']),
245 dbesc($datarray['name']),
246 dbesc($datarray['url']),
247 dbesc($datarray['photo']),
248 dbesc($datarray['date']),
249 intval($datarray['uid']),
250 dbesc($datarray['link']),
251 intval($datarray['parent']),
252 intval($datarray['type']),
253 dbesc($datarray['verb']),
254 dbesc($datarray['otype'])
257 $r = q("select id from notify where hash = '%s' and uid = %d limit 1",
259 intval($params['uid'])
262 $notify_id = $r[0]['id'];
268 $itemlink = $a->get_baseurl() . '/notify/view/' . $notify_id;
269 $msg = replace_macros($epreamble,array('$itemlink' => $itemlink));
270 $r = q("update notify set msg = '%s' where id = %d and uid = %d limit 1",
273 intval($params['uid'])
277 // send email notification if notification preferences permit
279 require_once('bbcode.php');
280 if((intval($params['notify_flags']) & intval($params['type'])) || $params['type'] == NOTIFY_SYSTEM) {
282 logger('notification: sending notification email');
284 $id_for_parent = "${params['parent']}@${hostname}";
286 // Is this the first email notification for this parent item and user?
288 $r = q("select `id` from `notify-threads` where `master-parent-item` = %d and `receiver-uid` = %d limit 1",
289 intval($params['parent']),
290 intval($params['uid']) );
292 // If so, create the record of it and use a message-id smtp header.
295 logger("norify_id:" . intval($notify_id). ", parent: " . intval($params['parent']) . "uid: " .
296 intval($params['uid']), LOGGER_DEBUG);
297 $r = q("insert into `notify-threads` (`notify-id`, `master-parent-item`, `receiver-uid`, `parent-item`)
298 values(%d,%d,%d,%d)",
300 intval($params['parent']),
301 intval($params['uid']),
304 $additional_mail_header .= "Message-ID: <${id_for_parent}>\n";
305 $log_msg = "include/enotify: No previous notification found for this parent:\n" .
306 " parent: ${params['parent']}\n" . " uid : ${params['uid']}\n";
307 logger($log_msg, LOGGER_DEBUG);
310 // If not, just "follow" the thread.
313 $additional_mail_header = "References: <${id_for_parent}>\nIn-Reply-To: <${id_for_parent}>\n";
314 logger("include/enotify: There's already a notification for this parent:\n" . print_r($r, true), LOGGER_DEBUG);
319 $textversion = strip_tags(html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",
320 $body))),ENT_QUOTES,'UTF-8'));
321 $htmlversion = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"),
322 "<br />\n",$body))));
325 $datarray['banner'] = $banner;
326 $datarray['product'] = $product;
327 $datarray['preamble'] = $preamble;
328 $datarray['sitename'] = $sitename;
329 $datarray['siteurl'] = $siteurl;
330 $datarray['type'] = $params['type'];
331 $datarray['parent'] = $params['parent'];
332 $datarray['source_name'] = $params['source_name'];
333 $datarray['source_link'] = $params['source_link'];
334 $datarray['source_photo'] = $params['source_photo'];
335 $datarray['uid'] = $params['uid'];
336 $datarray['username'] = $params['to_name'];
337 $datarray['hsitelink'] = $hsitelink;
338 $datarray['tsitelink'] = $tsitelink;
339 $datarray['hitemlink'] = '<a href="' . $itemlink . '">' . $itemlink . '</a>';
340 $datarray['titemlink'] = $itemlink;
341 $datarray['thanks'] = $thanks;
342 $datarray['site_admin'] = $site_admin;
343 $datarray['title'] = stripslashes($title);
344 $datarray['htmlversion'] = $htmlversion;
345 $datarray['textversion'] = $textversion;
346 $datarray['subject'] = $subject;
347 $datarray['headers'] = $additional_mail_header;
349 call_hooks('enotify_mail', $datarray);
351 // load the template for private message notifications
352 $tpl = get_markup_template('email_notify_html.tpl');
353 $email_html_body = replace_macros($tpl,array(
354 '$banner' => $datarray['banner'],
355 '$product' => $datarray['product'],
356 '$preamble' => $datarray['preamble'],
357 '$sitename' => $datarray['sitename'],
358 '$siteurl' => $datarray['siteurl'],
359 '$source_name' => $datarray['source_name'],
360 '$source_link' => $datarray['source_link'],
361 '$source_photo' => $datarray['source_photo'],
362 '$username' => $datarray['to_name'],
363 '$hsitelink' => $datarray['hsitelink'],
364 '$hitemlink' => $datarray['hitemlink'],
365 '$thanks' => $datarray['thanks'],
366 '$site_admin' => $datarray['site_admin'],
367 '$title' => $datarray['title'],
368 '$htmlversion' => $datarray['htmlversion'],
371 // load the template for private message notifications
372 $tpl = get_markup_template('email_notify_text.tpl');
373 $email_text_body = replace_macros($tpl,array(
374 '$banner' => $datarray['banner'],
375 '$product' => $datarray['product'],
376 '$preamble' => $datarray['preamble'],
377 '$sitename' => $datarray['sitename'],
378 '$siteurl' => $datarray['siteurl'],
379 '$source_name' => $datarray['source_name'],
380 '$source_link' => $datarray['source_link'],
381 '$source_photo' => $datarray['source_photo'],
382 '$username' => $datarray['to_name'],
383 '$tsitelink' => $datarray['tsitelink'],
384 '$titemlink' => $datarray['titemlink'],
385 '$thanks' => $datarray['thanks'],
386 '$site_admin' => $datarray['site_admin'],
387 '$title' => $datarray['title'],
388 '$textversion' => $datarray['textversion'],
391 // logger('text: ' . $email_text_body);
393 // use the EmailNotification library to send the message
396 'fromName' => $sender_name,
397 'fromEmail' => $sender_email,
398 'replyTo' => $sender_email,
399 'toEmail' => $params['to_email'],
400 'messageSubject' => $datarray['subject'],
401 'htmlVersion' => $email_html_body,
402 'textVersion' => $email_text_body,
403 'additionalMailHeader' => $datarray['headers'],
411 require_once('include/email.php');
415 * Send a multipart/alternative message with Text and HTML versions
417 * @param fromName name of the sender
418 * @param fromEmail email fo the sender
419 * @param replyTo replyTo address to direct responses
420 * @param toEmail destination email address
421 * @param messageSubject subject of the message
422 * @param htmlVersion html version of the message
423 * @param textVersion text only version of the message
424 * @param additionalMailHeader additions to the smtp mail header
426 static public function send($params) {
428 $fromName = email_header_encode(html_entity_decode($params['fromName'],ENT_QUOTES,'UTF-8'),'UTF-8');
429 $messageSubject = email_header_encode(html_entity_decode($params['messageSubject'],ENT_QUOTES,'UTF-8'),'UTF-8');
431 // generate a mime boundary
432 $mimeBoundary =rand(0,9)."-"
433 .rand(10000000000,9999999999)."-"
434 .rand(10000000000,9999999999)."=:"
437 // generate a multipart/alternative message header
439 $params['additionalMailHeader'] .
440 "From: {$params['fromName']} <{$params['fromEmail']}>\n" .
441 "Reply-To: {$params['fromName']} <{$params['replyTo']}>\n" .
442 "MIME-Version: 1.0\n" .
443 "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
445 // assemble the final multipart message body with the text and html types included
446 $textBody = chunk_split(base64_encode($params['textVersion']));
447 $htmlBody = chunk_split(base64_encode($params['htmlVersion']));
448 $multipartMessageBody =
449 "--" . $mimeBoundary . "\n" . // plain text section
450 "Content-Type: text/plain; charset=UTF-8\n" .
451 "Content-Transfer-Encoding: base64\n\n" .
453 "--" . $mimeBoundary . "\n" . // text/html section
454 "Content-Type: text/html; charset=UTF-8\n" .
455 "Content-Transfer-Encoding: base64\n\n" .
457 "--" . $mimeBoundary . "--\n"; // message ending
461 $params['toEmail'], // send to address
462 $params['messageSubject'], // subject
463 $multipartMessageBody, // message body
464 $messageHeader // message headers
466 logger("notification: enotify::send returns " . $res, LOGGER_DEBUG);