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;
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 $sender_email = t('noreply') . '@' . $hostname;
24 $additional_mail_header = "";
26 if(array_key_exists('item',$params)) {
27 $title = $params['item']['title'];
28 $body = $params['item']['body'];
34 // e.g. "your post", "David's photo", etc.
35 $possess_desc = t('%s <!item_type!>');
37 if($params['type'] == NOTIFY_MAIL) {
39 $subject = sprintf( t('[Friendica:Notify] New mail received at %s'),$sitename);
41 $preamble = sprintf( t('%s sent you a new private message at %s.'),$params['source_name'],$sitename);
42 $epreamble = sprintf( t('%s sent you %s.'),'[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=$itemlink]' . t('a private message') . '[/url]');
43 $sitelink = t('Please visit %s to view and/or reply to your private messages.');
44 $tsitelink = sprintf( $sitelink, $siteurl . '/message/' . $params['item']['id'] );
45 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '/message/' . $params['item']['id'] . '">' . $sitename . '</a>');
46 $itemlink = $siteurl . '/message/' . $params['item']['id'];
49 if($params['type'] == NOTIFY_COMMENT) {
50 // logger("notification: params = " . print_r($params, true), LOGGER_DEBUG);
52 $parent_id = $params['parent'];
55 // if it's a post figure out who's post it is.
59 if($params['otype'] === 'item' && $parent_id) {
60 $p = q("select * from item where id = %d and uid = %d limit 1",
62 intval($params['uid'])
66 $possess_desc = str_replace('<!item_type!>',item_post_type($p[0]),$possess_desc);
69 $dest_str = sprintf($possess_desc,'a');
71 // "George Bull's post"
73 $dest_str = sprintf($possess_desc,sprintf( t("%s's"),$p[0]['author-name']));
76 if($p[0]['owner-name'] == $p[0]['author-name'] && $p[0]['wall'])
77 $dest_str = sprintf($possess_desc, t('your') );
79 // Some mail softwares relies on subject field for threading.
80 // So, we cannot have different subjects for notifications of the same thread.
81 // Before this we have the name of the replier on the subject rendering
82 // differents subjects for messages on the same thread.
84 $subject = sprintf( t('[Friendica:Notify] Comment to conversation #%d by %s'), $parent_id, $params['source_name']);
85 $preamble = sprintf( t('%s commented on an item/conversation you have been following.'), $params['source_name']);
86 $epreamble = sprintf( t('%s commented on %s.'), '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=$itemlink]' . $dest_str . '[/url]');
88 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
89 $tsitelink = sprintf( $sitelink, $siteurl );
90 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
91 $itemlink = $params['link'];
94 if($params['type'] == NOTIFY_WALL) {
95 $subject = sprintf( t('[Friendica:Notify] %s posted to your profile wall') , $params['source_name']);
97 $preamble = sprintf( t('%s posted to your profile wall at %s') , $params['source_name'], $sitename);
99 $epreamble = sprintf( t('%s posted to %s') , '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=$itemlink]' . t('your profile wall.') . '[/url]');
101 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
102 $tsitelink = sprintf( $sitelink, $siteurl );
103 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
104 $itemlink = $params['link'];
107 if($params['type'] == NOTIFY_TAGSELF) {
108 $subject = sprintf( t('[Friendica:Notify] %s tagged you') , $params['source_name']);
109 $preamble = sprintf( t('%s tagged you at %s') , $params['source_name'], $sitename);
110 $epreamble = sprintf( t('%s %s.') , '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=' . $params['link'] . ']' . t('tagged you') . '[/url]');
112 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
113 $tsitelink = sprintf( $sitelink, $siteurl );
114 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
115 $itemlink = $params['link'];
118 if($params['type'] == NOTIFY_TAGSHARE) {
119 $subject = sprintf( t('[Friendica:Notify] %s tagged your post') , $params['source_name']);
120 $preamble = sprintf( t('%s tagged your post at %s') , $params['source_name'], $sitename);
121 $epreamble = sprintf( t('%s tagged %s') , '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=$itemlink]' . t('your post') . '[/url]' );
123 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
124 $tsitelink = sprintf( $sitelink, $siteurl );
125 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
126 $itemlink = $params['link'];
129 if($params['type'] == NOTIFY_INTRO) {
130 $subject = sprintf( t('[Friendica:Notify] Introduction received'));
131 $preamble = sprintf( t('You\'ve received an introduction from \'%s\' at %s'), $params['source_name'], $sitename);
132 $epreamble = sprintf( t('You\'ve received %s from %s.'), '[url=$itemlink]' . t('an introduction') . '[/url]' , '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
133 $body = sprintf( t('You may visit their profile at %s'),$params['source_link']);
135 $sitelink = t('Please visit %s to approve or reject the introduction.');
136 $tsitelink = sprintf( $sitelink, $siteurl );
137 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
138 $itemlink = $params['link'];
141 if($params['type'] == NOTIFY_SUGGEST) {
142 $subject = sprintf( t('[Friendica:Notify] Friend suggestion received'));
143 $preamble = sprintf( t('You\'ve received a friend suggestion from \'%s\' at %s'), $params['source_name'], $sitename);
144 $epreamble = sprintf( t('You\'ve received %s for %s from %s.'),
145 '[url=$itemlink]' . t('a friend suggestion') . '[/url]',
146 '[url=' . $params['item']['url'] . ']' . $params['item']['name'] . '[/url]',
147 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
148 $body = t('Name:') . ' ' . $params['item']['name'] . "\n";
149 $body .= t('Photo:') . ' ' . $params['item']['photo'] . "\n";
150 $body .= sprintf( t('You may visit their profile at %s'),$params['item']['url']);
152 $sitelink = t('Please visit %s to approve or reject the suggestion.');
153 $tsitelink = sprintf( $sitelink, $siteurl );
154 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
155 $itemlink = $params['link'];
158 if($params['type'] == NOTIFY_CONFIRM) {
162 if($params['type'] == NOTIFY_SYSTEM) {
168 'subject' => $subject,
169 'preamble' => $preamble,
170 'epreamble' => $epreamble,
172 'sitelink' => $sitelink,
173 'tsitelink' => $tsitelink,
174 'hsitelink' => $hsitelink,
175 'itemlink' => $itemlink
178 call_hooks('enotify',$h);
180 $subject = $h['subject'];
181 $preamble = $h['preamble'];
182 $epreamble = $h['epreamble'];
184 $sitelink = $h['sitelink'];
185 $tsitelink = $h['tsitelink'];
186 $hsitelink = $h['hsitelink'];
187 $itemlink = $h['itemlink'];
190 require_once('include/html2bbcode.php');
194 $hash = random_string();
195 $r = q("SELECT `id` FROM `notify` WHERE `hash` = '%s' LIMIT 1",
199 } while($dups == true);
203 $datarray['hash'] = $hash;
204 $datarray['name'] = $params['source_name'];
205 $datarray['url'] = $params['source_link'];
206 $datarray['photo'] = $params['source_photo'];
207 $datarray['date'] = datetime_convert();
208 $datarray['uid'] = $params['uid'];
209 $datarray['link'] = $itemlink;
210 $datarray['parent'] = $parent_id;
211 $datarray['type'] = $params['type'];
212 $datarray['verb'] = $params['verb'];
213 $datarray['otype'] = $params['otype'];
215 call_hooks('enotify_store', $datarray);
217 // create notification entry in DB
219 $r = q("insert into notify (hash,name,url,photo,date,uid,link,parent,type,verb,otype)
220 values('%s','%s','%s','%s','%s',%d,'%s',%d,%d,'%s','%s')",
221 dbesc($datarray['hash']),
222 dbesc($datarray['name']),
223 dbesc($datarray['url']),
224 dbesc($datarray['photo']),
225 dbesc($datarray['date']),
226 intval($datarray['uid']),
227 dbesc($datarray['link']),
228 intval($datarray['parent']),
229 intval($datarray['type']),
230 dbesc($datarray['verb']),
231 dbesc($datarray['otype'])
234 $r = q("select id from notify where hash = '%s' and uid = %d limit 1",
236 intval($params['uid'])
239 $notify_id = $r[0]['id'];
245 $itemlink = $a->get_baseurl() . '/notify/view/' . $notify_id;
246 $msg = replace_macros($epreamble,array('$itemlink' => $itemlink));
247 $r = q("update notify set msg = '%s' where id = %d and uid = %d limit 1",
250 intval($params['uid'])
254 // send email notification if notification preferences permit
256 require_once('bbcode.php');
257 if((intval($params['notify_flags']) & intval($params['type'])) || $params['type'] == NOTIFY_SYSTEM) {
259 logger('notification: sending notification email');
261 $id_for_parent = "${params['parent']}@${hostname}";
263 // Is this the first email notification for this parent item and user?
265 $r = q("select `id` from `notify-threads` where `master-parent-item` = %d and `receiver-uid` = %d limit 1",
266 intval($params['parent']),
267 intval($params['uid']) );
269 // If so, create the record of it and use a message-id smtp header.
272 logger("norify_id:" . intval($notify_id). ", parent: " . intval($params['parent']) . "uid: " .
273 intval($params['uid']), LOGGER_DEBUG);
274 $r = q("insert into `notify-threads` (`notify-id`, `master-parent-item`, `receiver-uid`, `parent-item`)
275 values(%d,%d,%d,%d)",
277 intval($params['parent']),
278 intval($params['uid']),
281 $additional_mail_header .= "Message-ID: <${id_for_parent}>\n";
282 $log_msg = "include/enotify: No previous notification found for this parent:\n" .
283 " parent: ${params['parent']}\n" . " uid : ${params['uid']}\n";
284 logger($log_msg, LOGGER_DEBUG);
287 // If not, just "follow" the thread.
290 $additional_mail_header = "References: <${id_for_parent}>\nIn-Reply-To: <${id_for_parent}>\n";
291 logger("include/enotify: There's already a notification for this parent:\n" . print_r($r, true), LOGGER_DEBUG);
296 $textversion = strip_tags(html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",
297 $body))),ENT_QUOTES,'UTF-8'));
298 $htmlversion = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"),
299 "<br />\n",$body))));
302 $datarray['banner'] = $banner;
303 $datarray['product'] = $product;
304 $datarray['preamble'] = $preamble;
305 $datarray['sitename'] = $sitename;
306 $datarray['siteurl'] = $siteurl;
307 $datarray['type'] = $params['type'];
308 $datarray['parent'] = $params['parent'];
309 $datarray['source_name'] = $params['source_name'];
310 $datarray['source_link'] = $params['source_link'];
311 $datarray['source_photo'] = $params['source_photo'];
312 $datarray['uid'] = $params['uid'];
313 $datarray['username'] = $params['to_name'];
314 $datarray['hsitelink'] = $hsitelink;
315 $datarray['tsitelink'] = $tsitelink;
316 $datarray['hitemlink'] = '<a href="' . $itemlink . '">' . $itemlink . '</a>';
317 $datarray['titemlink'] = $itemlink;
318 $datarray['thanks'] = $thanks;
319 $datarray['site_admin'] = $site_admin;
320 $datarray['title'] = stripslashes($title);
321 $datarray['htmlversion'] = $htmlversion;
322 $datarray['textversion'] = $textversion;
323 $datarray['subject'] = $subject;
324 $datarray['headers'] = $additional_mail_header;
326 call_hooks('enotify_mail', $datarray);
328 // load the template for private message notifications
329 $tpl = get_markup_template('email_notify_html.tpl');
330 $email_html_body = replace_macros($tpl,array(
331 '$banner' => $datarray['banner'],
332 '$product' => $datarray['product'],
333 '$preamble' => $datarray['preamble'],
334 '$sitename' => $datarray['sitename'],
335 '$siteurl' => $datarray['siteurl'],
336 '$source_name' => $datarray['source_name'],
337 '$source_link' => $datarray['source_link'],
338 '$source_photo' => $datarray['source_photo'],
339 '$username' => $datarray['to_name'],
340 '$hsitelink' => $datarray['hsitelink'],
341 '$hitemlink' => $datarray['hitemlink'],
342 '$thanks' => $datarray['thanks'],
343 '$site_admin' => $datarray['site_admin'],
344 '$title' => $datarray['title'],
345 '$htmlversion' => $datarray['htmlversion'],
348 // load the template for private message notifications
349 $tpl = get_markup_template('email_notify_text.tpl');
350 $email_text_body = replace_macros($tpl,array(
351 '$banner' => $datarray['banner'],
352 '$product' => $datarray['product'],
353 '$preamble' => $datarray['preamble'],
354 '$sitename' => $datarray['sitename'],
355 '$siteurl' => $datarray['siteurl'],
356 '$source_name' => $datarray['source_name'],
357 '$source_link' => $datarray['source_link'],
358 '$source_photo' => $datarray['source_photo'],
359 '$username' => $datarray['to_name'],
360 '$tsitelink' => $datarray['tsitelink'],
361 '$titemlink' => $datarray['titemlink'],
362 '$thanks' => $datarray['thanks'],
363 '$site_admin' => $datarray['site_admin'],
364 '$title' => $datarray['title'],
365 '$textversion' => $datarray['textversion'],
368 // logger('text: ' . $email_text_body);
370 // use the EmailNotification library to send the message
373 'fromName' => $sender_name,
374 'fromEmail' => $sender_email,
375 'replyTo' => $sender_email,
376 'toEmail' => $params['to_email'],
377 'messageSubject' => $datarray['subject'],
378 'htmlVersion' => $email_html_body,
379 'textVersion' => $email_text_body,
380 'additionalMailHeader' => $datarray['headers'],
388 require_once('include/email.php');
392 * Send a multipart/alternative message with Text and HTML versions
394 * @param fromName name of the sender
395 * @param fromEmail email fo the sender
396 * @param replyTo replyTo address to direct responses
397 * @param toEmail destination email address
398 * @param messageSubject subject of the message
399 * @param htmlVersion html version of the message
400 * @param textVersion text only version of the message
401 * @param additionalMailHeader additions to the smtp mail header
403 static public function send($params) {
405 $fromName = email_header_encode(html_entity_decode($params['fromName'],ENT_QUOTES,'UTF-8'),'UTF-8');
406 $messageSubject = email_header_encode(html_entity_decode($params['messageSubject'],ENT_QUOTES,'UTF-8'),'UTF-8');
408 // generate a mime boundary
409 $mimeBoundary =rand(0,9)."-"
410 .rand(10000000000,9999999999)."-"
411 .rand(10000000000,9999999999)."=:"
414 // generate a multipart/alternative message header
416 $params['additionalMailHeader'] .
417 "From: {$params['fromName']} <{$params['fromEmail']}>\n" .
418 "Reply-To: {$params['fromName']} <{$params['replyTo']}>\n" .
419 "MIME-Version: 1.0\n" .
420 "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
422 // assemble the final multipart message body with the text and html types included
423 $textBody = chunk_split(base64_encode($params['textVersion']));
424 $htmlBody = chunk_split(base64_encode($params['htmlVersion']));
425 $multipartMessageBody =
426 "--" . $mimeBoundary . "\n" . // plain text section
427 "Content-Type: text/plain; charset=UTF-8\n" .
428 "Content-Transfer-Encoding: base64\n\n" .
430 "--" . $mimeBoundary . "\n" . // text/html section
431 "Content-Type: text/html; charset=UTF-8\n" .
432 "Content-Transfer-Encoding: base64\n\n" .
434 "--" . $mimeBoundary . "--\n"; // message ending
438 $params['toEmail'], // send to address
439 $params['messageSubject'], // subject
440 $multipartMessageBody, // message body
441 $messageHeader // message headers
443 logger("notification: enotify::send returns " . $res, LOGGER_DEBUG);