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 for this post.
60 // If so don't create a second notification
63 $p = q("select id from notify where type = %d and link = '%s' and uid = %d limit 1",
64 intval(NOTIFY_TAGSELF),
65 dbesc($params['link']),
66 intval($params['uid'])
68 if($p and count($p)) {
74 // if it's a post figure out who's post it is.
78 if($params['otype'] === 'item' && $parent_id) {
79 $p = q("select * from item where id = %d and uid = %d limit 1",
81 intval($params['uid'])
85 $item_post_type = item_post_type($p[0]);
86 //$possess_desc = str_replace('<!item_type!>',$possess_desc);
89 $dest_str = sprintf(t('%1$s commented on [url=%2$s]a %3$s[/url]'),
90 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
94 // "George Bull's post"
96 $dest_str = sprintf(t('%1$s commented on [url=%2$s]%3$s\'s %4$s[/url]'),
97 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
103 if($p[0]['owner-name'] == $p[0]['author-name'] && $p[0]['wall'])
104 $dest_str = sprintf(t('%1$s commented on [url=%2$s]your %3$s[/url]'),
105 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
109 // Some mail softwares relies on subject field for threading.
110 // So, we cannot have different subjects for notifications of the same thread.
111 // Before this we have the name of the replier on the subject rendering
112 // differents subjects for messages on the same thread.
114 $subject = sprintf( t('[Friendica:Notify] Comment to conversation #%1$d by %2$s'), $parent_id, $params['source_name']);
115 $preamble = sprintf( t('%s commented on an item/conversation you have been following.'), $params['source_name']);
116 $epreamble = $dest_str;
118 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
119 $tsitelink = sprintf( $sitelink, $siteurl );
120 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
121 $itemlink = $params['link'];
124 if($params['type'] == NOTIFY_WALL) {
125 $subject = sprintf( t('[Friendica:Notify] %s posted to your profile wall') , $params['source_name']);
127 $preamble = sprintf( t('%1$s posted to your profile wall at %2$s') , $params['source_name'], $sitename);
129 $epreamble = sprintf( t('%1$s posted to [url=%2$s]your wall[/url]') ,
130 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
133 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
134 $tsitelink = sprintf( $sitelink, $siteurl );
135 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
136 $itemlink = $params['link'];
139 if($params['type'] == NOTIFY_TAGSELF) {
140 $subject = sprintf( t('[Friendica:Notify] %s tagged you') , $params['source_name']);
141 $preamble = sprintf( t('%1$s tagged you at %2$s') , $params['source_name'], $sitename);
142 $epreamble = sprintf( t('%1$s [url=%2$s]tagged you[/url].') ,
143 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
146 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
147 $tsitelink = sprintf( $sitelink, $siteurl );
148 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
149 $itemlink = $params['link'];
152 if($params['type'] == NOTIFY_POKE) {
154 $subject = sprintf( t('[Friendica:Notify] %1$s poked you') , $params['source_name']);
155 $preamble = sprintf( t('%1$s poked you at %2$s') , $params['source_name'], $sitename);
156 $epreamble = sprintf( t('%1$s [url=%2$s]poked you[/url].') ,
157 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
160 $subject = str_replace('poked', t($params['activity']), $subject);
161 $preamble = str_replace('poked', t($params['activity']), $preamble);
162 $epreamble = str_replace('poked', t($params['activity']), $epreamble);
164 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
165 $tsitelink = sprintf( $sitelink, $siteurl );
166 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
167 $itemlink = $params['link'];
170 if($params['type'] == NOTIFY_TAGSHARE) {
171 $subject = sprintf( t('[Friendica:Notify] %s tagged your post') , $params['source_name']);
172 $preamble = sprintf( t('%1$s tagged your post at %2$s') , $params['source_name'], $sitename);
173 $epreamble = sprintf( t('%1$s tagged [url=%2$s]your post[/url]') ,
174 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
177 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
178 $tsitelink = sprintf( $sitelink, $siteurl );
179 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
180 $itemlink = $params['link'];
183 if($params['type'] == NOTIFY_INTRO) {
184 $subject = sprintf( t('[Friendica:Notify] Introduction received'));
185 $preamble = sprintf( t('You\'ve received an introduction from \'%1$s\' at %2$s'), $params['source_name'], $sitename);
186 $epreamble = sprintf( t('You\'ve received [url=%1$s]an introduction[/url] from %2$s.'),
188 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
189 $body = sprintf( t('You may visit their profile at %s'),$params['source_link']);
191 $sitelink = t('Please visit %s to approve or reject the introduction.');
192 $tsitelink = sprintf( $sitelink, $siteurl );
193 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
194 $itemlink = $params['link'];
197 if($params['type'] == NOTIFY_SUGGEST) {
198 $subject = sprintf( t('[Friendica:Notify] Friend suggestion received'));
199 $preamble = sprintf( t('You\'ve received a friend suggestion from \'%1$s\' at %2$s'), $params['source_name'], $sitename);
200 $epreamble = sprintf( t('You\'ve received [url=%1$s]a friend suggestion[/url] for %2$s from %3$s.'),
202 '[url=' . $params['item']['url'] . ']' . $params['item']['name'] . '[/url]',
203 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
205 $body = t('Name:') . ' ' . $params['item']['name'] . "\n";
206 $body .= t('Photo:') . ' ' . $params['item']['photo'] . "\n";
207 $body .= sprintf( t('You may visit their profile at %s'),$params['item']['url']);
209 $sitelink = t('Please visit %s to approve or reject the suggestion.');
210 $tsitelink = sprintf( $sitelink, $siteurl );
211 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
212 $itemlink = $params['link'];
215 if($params['type'] == NOTIFY_CONFIRM) {
219 if($params['type'] == NOTIFY_SYSTEM) {
225 'subject' => $subject,
226 'preamble' => $preamble,
227 'epreamble' => $epreamble,
229 'sitelink' => $sitelink,
230 'tsitelink' => $tsitelink,
231 'hsitelink' => $hsitelink,
232 'itemlink' => $itemlink
235 call_hooks('enotify',$h);
237 $subject = $h['subject'];
238 $preamble = $h['preamble'];
239 $epreamble = $h['epreamble'];
241 $sitelink = $h['sitelink'];
242 $tsitelink = $h['tsitelink'];
243 $hsitelink = $h['hsitelink'];
244 $itemlink = $h['itemlink'];
247 require_once('include/html2bbcode.php');
251 $hash = random_string();
252 $r = q("SELECT `id` FROM `notify` WHERE `hash` = '%s' LIMIT 1",
256 } while($dups == true);
260 $datarray['hash'] = $hash;
261 $datarray['name'] = $params['source_name'];
262 $datarray['url'] = $params['source_link'];
263 $datarray['photo'] = $params['source_photo'];
264 $datarray['date'] = datetime_convert();
265 $datarray['uid'] = $params['uid'];
266 $datarray['link'] = $itemlink;
267 $datarray['parent'] = $parent_id;
268 $datarray['type'] = $params['type'];
269 $datarray['verb'] = $params['verb'];
270 $datarray['otype'] = $params['otype'];
272 call_hooks('enotify_store', $datarray);
274 // create notification entry in DB
276 $r = q("insert into notify (hash,name,url,photo,date,uid,link,parent,type,verb,otype)
277 values('%s','%s','%s','%s','%s',%d,'%s',%d,%d,'%s','%s')",
278 dbesc($datarray['hash']),
279 dbesc($datarray['name']),
280 dbesc($datarray['url']),
281 dbesc($datarray['photo']),
282 dbesc($datarray['date']),
283 intval($datarray['uid']),
284 dbesc($datarray['link']),
285 intval($datarray['parent']),
286 intval($datarray['type']),
287 dbesc($datarray['verb']),
288 dbesc($datarray['otype'])
291 $r = q("select id from notify where hash = '%s' and uid = %d limit 1",
293 intval($params['uid'])
296 $notify_id = $r[0]['id'];
302 $itemlink = $a->get_baseurl() . '/notify/view/' . $notify_id;
303 $msg = replace_macros($epreamble,array('$itemlink' => $itemlink));
304 $r = q("update notify set msg = '%s' where id = %d and uid = %d limit 1",
307 intval($params['uid'])
311 // send email notification if notification preferences permit
313 require_once('bbcode.php');
314 if((intval($params['notify_flags']) & intval($params['type'])) || $params['type'] == NOTIFY_SYSTEM) {
316 logger('notification: sending notification email');
318 $id_for_parent = "${params['parent']}@${hostname}";
320 // Is this the first email notification for this parent item and user?
322 $r = q("select `id` from `notify-threads` where `master-parent-item` = %d and `receiver-uid` = %d limit 1",
323 intval($params['parent']),
324 intval($params['uid']) );
326 // If so, create the record of it and use a message-id smtp header.
329 logger("notify_id:" . intval($notify_id). ", parent: " . intval($params['parent']) . "uid: " .
330 intval($params['uid']), LOGGER_DEBUG);
331 $r = q("insert into `notify-threads` (`notify-id`, `master-parent-item`, `receiver-uid`, `parent-item`)
332 values(%d,%d,%d,%d)",
334 intval($params['parent']),
335 intval($params['uid']),
338 $additional_mail_header .= "Message-ID: <${id_for_parent}>\n";
339 $log_msg = "include/enotify: No previous notification found for this parent:\n" .
340 " parent: ${params['parent']}\n" . " uid : ${params['uid']}\n";
341 logger($log_msg, LOGGER_DEBUG);
344 // If not, just "follow" the thread.
347 $additional_mail_header = "References: <${id_for_parent}>\nIn-Reply-To: <${id_for_parent}>\n";
348 logger("include/enotify: There's already a notification for this parent:\n" . print_r($r, true), LOGGER_DEBUG);
353 $textversion = strip_tags(html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",
354 $body))),ENT_QUOTES,'UTF-8'));
355 $htmlversion = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"),
356 "<br />\n",$body))));
359 $datarray['banner'] = $banner;
360 $datarray['product'] = $product;
361 $datarray['preamble'] = $preamble;
362 $datarray['sitename'] = $sitename;
363 $datarray['siteurl'] = $siteurl;
364 $datarray['type'] = $params['type'];
365 $datarray['parent'] = $params['parent'];
366 $datarray['source_name'] = $params['source_name'];
367 $datarray['source_link'] = $params['source_link'];
368 $datarray['source_photo'] = $params['source_photo'];
369 $datarray['uid'] = $params['uid'];
370 $datarray['username'] = $params['to_name'];
371 $datarray['hsitelink'] = $hsitelink;
372 $datarray['tsitelink'] = $tsitelink;
373 $datarray['hitemlink'] = '<a href="' . $itemlink . '">' . $itemlink . '</a>';
374 $datarray['titemlink'] = $itemlink;
375 $datarray['thanks'] = $thanks;
376 $datarray['site_admin'] = $site_admin;
377 $datarray['title'] = stripslashes($title);
378 $datarray['htmlversion'] = $htmlversion;
379 $datarray['textversion'] = $textversion;
380 $datarray['subject'] = $subject;
381 $datarray['headers'] = $additional_mail_header;
383 call_hooks('enotify_mail', $datarray);
385 // load the template for private message notifications
386 $tpl = get_markup_template('email_notify_html.tpl');
387 $email_html_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 '$hsitelink' => $datarray['hsitelink'],
398 '$hitemlink' => $datarray['hitemlink'],
399 '$thanks' => $datarray['thanks'],
400 '$site_admin' => $datarray['site_admin'],
401 '$title' => $datarray['title'],
402 '$htmlversion' => $datarray['htmlversion'],
405 // load the template for private message notifications
406 $tpl = get_markup_template('email_notify_text.tpl');
407 $email_text_body = replace_macros($tpl,array(
408 '$banner' => $datarray['banner'],
409 '$product' => $datarray['product'],
410 '$preamble' => $datarray['preamble'],
411 '$sitename' => $datarray['sitename'],
412 '$siteurl' => $datarray['siteurl'],
413 '$source_name' => $datarray['source_name'],
414 '$source_link' => $datarray['source_link'],
415 '$source_photo' => $datarray['source_photo'],
416 '$username' => $datarray['to_name'],
417 '$tsitelink' => $datarray['tsitelink'],
418 '$titemlink' => $datarray['titemlink'],
419 '$thanks' => $datarray['thanks'],
420 '$site_admin' => $datarray['site_admin'],
421 '$title' => $datarray['title'],
422 '$textversion' => $datarray['textversion'],
425 // logger('text: ' . $email_text_body);
427 // use the EmailNotification library to send the message
430 'fromName' => $sender_name,
431 'fromEmail' => $sender_email,
432 'replyTo' => $sender_email,
433 'toEmail' => $params['to_email'],
434 'messageSubject' => $datarray['subject'],
435 'htmlVersion' => $email_html_body,
436 'textVersion' => $email_text_body,
437 'additionalMailHeader' => $datarray['headers'],
445 require_once('include/email.php');
449 * Send a multipart/alternative message with Text and HTML versions
451 * @param fromName name of the sender
452 * @param fromEmail email fo the sender
453 * @param replyTo replyTo address to direct responses
454 * @param toEmail destination email address
455 * @param messageSubject subject of the message
456 * @param htmlVersion html version of the message
457 * @param textVersion text only version of the message
458 * @param additionalMailHeader additions to the smtp mail header
460 static public function send($params) {
462 $fromName = email_header_encode(html_entity_decode($params['fromName'],ENT_QUOTES,'UTF-8'),'UTF-8');
463 $messageSubject = email_header_encode(html_entity_decode($params['messageSubject'],ENT_QUOTES,'UTF-8'),'UTF-8');
465 // generate a mime boundary
466 $mimeBoundary =rand(0,9)."-"
467 .rand(10000000000,9999999999)."-"
468 .rand(10000000000,9999999999)."=:"
471 // generate a multipart/alternative message header
473 $params['additionalMailHeader'] .
474 "From: {$params['fromName']} <{$params['fromEmail']}>\n" .
475 "Reply-To: {$params['fromName']} <{$params['replyTo']}>\n" .
476 "MIME-Version: 1.0\n" .
477 "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
479 // assemble the final multipart message body with the text and html types included
480 $textBody = chunk_split(base64_encode($params['textVersion']));
481 $htmlBody = chunk_split(base64_encode($params['htmlVersion']));
482 $multipartMessageBody =
483 "--" . $mimeBoundary . "\n" . // plain text section
484 "Content-Type: text/plain; charset=UTF-8\n" .
485 "Content-Transfer-Encoding: base64\n\n" .
487 "--" . $mimeBoundary . "\n" . // text/html section
488 "Content-Type: text/html; charset=UTF-8\n" .
489 "Content-Transfer-Encoding: base64\n\n" .
491 "--" . $mimeBoundary . "--\n"; // message ending
495 $params['toEmail'], // send to address
496 $params['messageSubject'], // subject
497 $multipartMessageBody, // message body
498 $messageHeader // message headers
500 logger("notification: enotify::send header " . 'To: ' . $params['toEmail'] . "\n" . $messageHeader, LOGGER_DEBUG);
501 logger("notification: enotify::send returns " . $res, LOGGER_DEBUG);