3 function notification($params) {
5 logger('notification: entry', LOGGER_DEBUG);
8 $banner = t('Friendica Notification');
9 $product = FRIENDICA_PLATFORM;
11 $thanks = t('Thank You,');
12 $sitename = get_config('config','sitename');
13 $site_admin = sprintf( t('%s Administrator'), $sitename);
15 $sender_name = $product;
16 $hostname = $a->get_hostname();
17 $sender_email = t('noreply') . '@' . $hostname;
18 $additional_mail_header = "";
20 if(array_key_exists('item',$params)) {
21 $title = $params['item']['title'];
22 $body = $params['item']['body'];
28 // e.g. "your post", "David's photo", etc.
29 $possess_desc = t('%s <!item_type!>');
31 if($params['type'] == NOTIFY_MAIL) {
33 $subject = sprintf( t('[Friendica:Notify] New mail received at %s'),$sitename);
35 $preamble = sprintf( t('%s sent you a new private message at %s.'),$params['source_name'],$sitename);
36 $epreamble = sprintf( t('%s sent you %s.'),'[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=$itemlink]' . t('a private message') . '[/url]');
37 $sitelink = t('Please visit %s to view and/or reply to your private messages.');
38 $tsitelink = sprintf( $sitelink, $siteurl . '/message/' . $params['item']['id'] );
39 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '/message/' . $params['item']['id'] . '">' . $sitename . '</a>');
40 $itemlink = $siteurl . '/message/' . $params['item']['id'];
43 if($params['type'] == NOTIFY_COMMENT) {
44 // logger("notification: params = " . print_r($params, true), LOGGER_DEBUG);
46 $parent_id = $params['parent'];
49 // if it's a post figure out who's post it is.
53 if($params['otype'] === 'item' && $parent_id) {
54 $p = q("select * from item where id = %d and uid = %d limit 1",
56 intval($params['uid'])
60 $possess_desc = str_replace('<!item_type!>',item_post_type($p[0]),$possess_desc);
63 $dest_str = sprintf($possess_desc,'a');
65 // "George Bull's post"
67 $dest_str = sprintf($possess_desc,sprintf( t("%s's"),$p[0]['author-name']));
70 if($p[0]['owner-name'] == $p[0]['author-name'] && $p[0]['wall'])
71 $dest_str = sprintf($possess_desc, t('your') );
73 // Some mail softwares relies on subject field for threading.
74 // So, we cannot have different subjects for notifications of the same thread.
75 // Before this we have the name of the replier on the subject rendering
76 // differents subjects for messages on the same thread.
78 $subject = sprintf( t('[Friendica:Notify] Comment to conversation #%d by %s'), $parent_id, $params['source_name']);
79 $preamble = sprintf( t('%s commented on an item/conversation you have been following.'), $params['source_name']);
80 $epreamble = sprintf( t('%s commented on %s.'), '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=$itemlink]' . $dest_str . '[/url]');
82 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
83 $tsitelink = sprintf( $sitelink, $siteurl );
84 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
85 $itemlink = $params['link'];
88 if($params['type'] == NOTIFY_WALL) {
89 $subject = sprintf( t('[Friendica:Notify] %s posted to your profile wall') , $params['source_name']);
91 $preamble = sprintf( t('%s posted to your profile wall at %s') , $params['source_name'], $sitename);
93 $epreamble = sprintf( t('%s posted to %s') , '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=$itemlink]' . t('your profile wall.') . '[/url]');
95 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
96 $tsitelink = sprintf( $sitelink, $siteurl );
97 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
98 $itemlink = $params['link'];
101 if($params['type'] == NOTIFY_TAGSELF) {
102 $subject = sprintf( t('[Friendica:Notify] %s tagged you') , $params['source_name']);
103 $preamble = sprintf( t('%s tagged you at %s') , $params['source_name'], $sitename);
104 $epreamble = sprintf( t('%s %s.') , '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=' . $params['link'] . ']' . t('tagged you') . '[/url]');
106 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
107 $tsitelink = sprintf( $sitelink, $siteurl );
108 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
109 $itemlink = $params['link'];
112 if($params['type'] == NOTIFY_TAGSHARE) {
113 $subject = sprintf( t('[Friendica:Notify] %s tagged your post') , $params['source_name']);
114 $preamble = sprintf( t('%s tagged your post at %s') , $params['source_name'], $sitename);
115 $epreamble = sprintf( t('%s tagged %s') , '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=$itemlink]' . t('your post') . '[/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_INTRO) {
124 $subject = sprintf( t('[Friendica:Notify] Introduction received'));
125 $preamble = sprintf( t('You\'ve received an introduction from \'%s\' at %s'), $params['source_name'], $sitename);
126 $epreamble = sprintf( t('You\'ve received %s from %s.'), '[url=$itemlink]' . t('an introduction') . '[/url]' , '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
127 $body = sprintf( t('You may visit their profile at %s'),$params['source_link']);
129 $sitelink = t('Please visit %s to approve or reject the introduction.');
130 $tsitelink = sprintf( $sitelink, $siteurl );
131 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
132 $itemlink = $params['link'];
135 if($params['type'] == NOTIFY_SUGGEST) {
136 $subject = sprintf( t('[Friendica:Notify] Friend suggestion received'));
137 $preamble = sprintf( t('You\'ve received a friend suggestion from \'%s\' at %s'), $params['source_name'], $sitename);
138 $epreamble = sprintf( t('You\'ve received %s for %s from %s.'),
139 '[url=$itemlink]' . t('a friend suggestion') . '[/url]',
140 '[url=' . $params['item']['url'] . ']' . $params['item']['name'] . '[/url]',
141 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
142 $body = t('Name:') . ' ' . $params['item']['name'] . "\n";
143 $body .= t('Photo:') . ' ' . $params['item']['photo'] . "\n";
144 $body .= sprintf( t('You may visit their profile at %s'),$params['item']['url']);
146 $sitelink = t('Please visit %s to approve or reject the suggestion.');
147 $tsitelink = sprintf( $sitelink, $siteurl );
148 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
149 $itemlink = $params['link'];
152 if($params['type'] == NOTIFY_CONFIRM) {
156 if($params['type'] == NOTIFY_SYSTEM) {
162 'subject' => $subject,
163 'preamble' => $preamble,
164 'epreamble' => $epreamble,
166 'sitelink' => $sitelink,
167 'tsitelink' => $tsitelink,
168 'hsitelink' => $hsitelink,
169 'itemlink' => $itemlink
172 call_hooks('enotify',$h);
174 $subject = $h['subject'];
175 $preamble = $h['preamble'];
176 $epreamble = $h['epreamble'];
178 $sitelink = $h['sitelink'];
179 $tsitelink = $h['tsitelink'];
180 $hsitelink = $h['hsitelink'];
181 $itemlink = $h['itemlink'];
183 // from here on everything is in the recipients language
185 push_lang($params['language']);
187 require_once('include/html2bbcode.php');
191 $hash = random_string();
192 $r = q("SELECT `id` FROM `notify` WHERE `hash` = '%s' LIMIT 1",
196 } while($dups == true);
200 $datarray['hash'] = $hash;
201 $datarray['name'] = $params['source_name'];
202 $datarray['url'] = $params['source_link'];
203 $datarray['photo'] = $params['source_photo'];
204 $datarray['date'] = datetime_convert();
205 $datarray['uid'] = $params['uid'];
206 $datarray['link'] = $itemlink;
207 $datarray['parent'] = $parent_id;
208 $datarray['type'] = $params['type'];
209 $datarray['verb'] = $params['verb'];
210 $datarray['otype'] = $params['otype'];
212 call_hooks('enotify_store', $datarray);
214 // create notification entry in DB
216 $r = q("insert into notify (hash,name,url,photo,date,uid,link,parent,type,verb,otype)
217 values('%s','%s','%s','%s','%s',%d,'%s',%d,%d,'%s','%s')",
218 dbesc($datarray['hash']),
219 dbesc($datarray['name']),
220 dbesc($datarray['url']),
221 dbesc($datarray['photo']),
222 dbesc($datarray['date']),
223 intval($datarray['uid']),
224 dbesc($datarray['link']),
225 intval($datarray['parent']),
226 intval($datarray['type']),
227 dbesc($datarray['verb']),
228 dbesc($datarray['otype'])
231 $r = q("select id from notify where hash = '%s' and uid = %d limit 1",
233 intval($params['uid'])
236 $notify_id = $r[0]['id'];
240 $itemlink = $a->get_baseurl() . '/notify/view/' . $notify_id;
241 $msg = replace_macros($epreamble,array('$itemlink' => $itemlink));
242 $r = q("update notify set msg = '%s' where id = %d and uid = %d limit 1",
245 intval($params['uid'])
250 // send email notification if notification preferences permit
252 require_once('bbcode.php');
253 if((intval($params['notify_flags']) & intval($params['type'])) || $params['type'] == NOTIFY_SYSTEM) {
255 logger('notification: sending notification email');
257 $id_for_parent = "${params['parent']}@${hostname}";
259 // Is this the first email notification for this parent item and user?
261 $r = q("select `id` from `notify-threads` where `master-parent-item` = %d and `receiver-uid` = %d limit 1",
262 intval($params['parent']),
263 intval($params['uid']) );
265 // If so, create the record of it and use a message-id smtp header.
268 logger("norify_id:" . intval($notify_id). ", parent: " . intval($params['parent']) . "uid: " .
269 intval($params['uid']), LOGGER_DEBUG);
270 $r = q("insert into `notify-threads` (`notify-id`, `master-parent-item`, `receiver-uid`, `parent-item`)
271 values(%d,%d,%d,%d)",
273 intval($params['parent']),
274 intval($params['uid']),
277 $additional_mail_header .= "Message-ID: <${id_for_parent}>\n";
278 $log_msg = "include/enotify: No previous notification found for this parent:\n" .
279 " parent: ${params['parent']}\n" . " uid : ${params['uid']}\n";
280 logger($log_msg, LOGGER_DEBUG);
283 // If not, just "follow" the thread.
286 $additional_mail_header = "References: <${id_for_parent}>\nIn-Reply-To: <${id_for_parent}>\n";
287 logger("include/enotify: There's already a notification for this parent:\n" . print_r($r, true), LOGGER_DEBUG);
292 $textversion = strip_tags(html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",
293 $body))),ENT_QUOTES,'UTF-8'));
294 $htmlversion = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"),
295 "<br />\n",$body))));
298 $datarray['banner'] = $banner;
299 $datarray['product'] = $product;
300 $datarray['preamble'] = $preamble;
301 $datarray['sitename'] = $sitename;
302 $datarray['siteurl'] = $siteurl;
303 $datarray['type'] = $params['type'];
304 $datarray['parent'] = $params['parent'];
305 $datarray['source_name'] = $params['source_name'];
306 $datarray['source_link'] = $params['source_link'];
307 $datarray['source_photo'] = $params['source_photo'];
308 $datarray['uid'] = $params['uid'];
309 $datarray['username'] = $params['to_name'];
310 $datarray['hsitelink'] = $hsitelink;
311 $datarray['tsitelink'] = $tsitelink;
312 $datarray['hitemlink'] = '<a href="' . $itemlink . '">' . $itemlink . '</a>';
313 $datarray['titemlink'] = $itemlink;
314 $datarray['thanks'] = $thanks;
315 $datarray['site_admin'] = $site_admin;
316 $datarray['title'] = stripslashes($title);
317 $datarray['htmlversion'] = $htmlversion;
318 $datarray['textversion'] = $textversion;
319 $datarray['subject'] = $subject;
320 $datarray['headers'] = $additional_mail_header;
322 call_hooks('enotify_mail', $datarray);
324 // load the template for private message notifications
325 $tpl = get_markup_template('email_notify_html.tpl');
326 $email_html_body = replace_macros($tpl,array(
327 '$banner' => $datarray['banner'],
328 '$product' => $datarray['product'],
329 '$preamble' => $datarray['preamble'],
330 '$sitename' => $datarray['sitename'],
331 '$siteurl' => $datarray['siteurl'],
332 '$source_name' => $datarray['source_name'],
333 '$source_link' => $datarray['source_link'],
334 '$source_photo' => $datarray['source_photo'],
335 '$username' => $datarray['to_name'],
336 '$hsitelink' => $datarray['hsitelink'],
337 '$hitemlink' => $datarray['hitemlink'],
338 '$thanks' => $datarray['thanks'],
339 '$site_admin' => $datarray['site_admin'],
340 '$title' => $datarray['title'],
341 '$htmlversion' => $datarray['htmlversion'],
344 // load the template for private message notifications
345 $tpl = get_markup_template('email_notify_text.tpl');
346 $email_text_body = replace_macros($tpl,array(
347 '$banner' => $datarray['banner'],
348 '$product' => $datarray['product'],
349 '$preamble' => $datarray['preamble'],
350 '$sitename' => $datarray['sitename'],
351 '$siteurl' => $datarray['siteurl'],
352 '$source_name' => $datarray['source_name'],
353 '$source_link' => $datarray['source_link'],
354 '$source_photo' => $datarray['source_photo'],
355 '$username' => $datarray['to_name'],
356 '$tsitelink' => $datarray['tsitelink'],
357 '$titemlink' => $datarray['titemlink'],
358 '$thanks' => $datarray['thanks'],
359 '$site_admin' => $datarray['site_admin'],
360 '$title' => $datarray['title'],
361 '$textversion' => $datarray['textversion'],
364 // logger('text: ' . $email_text_body);
366 // use the EmailNotification library to send the message
369 'fromName' => $sender_name,
370 'fromEmail' => $sender_email,
371 'replyTo' => $sender_email,
372 'toEmail' => $params['to_email'],
373 'messageSubject' => $datarray['subject'],
374 'htmlVersion' => $email_html_body,
375 'textVersion' => $email_text_body,
376 'additionalMailHeader' => $datarray['headers'],
384 require_once('include/email.php');
388 * Send a multipart/alternative message with Text and HTML versions
390 * @param fromName name of the sender
391 * @param fromEmail email fo the sender
392 * @param replyTo replyTo address to direct responses
393 * @param toEmail destination email address
394 * @param messageSubject subject of the message
395 * @param htmlVersion html version of the message
396 * @param textVersion text only version of the message
397 * @param additionalMailHeader additions to the smtp mail header
399 static public function send($params) {
401 $fromName = email_header_encode($params['fromName'],'UTF-8');
402 $messageSubject = email_header_encode($params['messageSubject'],'UTF-8');
404 // generate a mime boundary
405 $mimeBoundary =rand(0,9)."-"
406 .rand(10000000000,9999999999)."-"
407 .rand(10000000000,9999999999)."=:"
410 // generate a multipart/alternative message header
412 $params['additionalMailHeader'] .
413 "From: {$params['fromName']} <{$params['fromEmail']}>\n" .
414 "Reply-To: {$params['fromName']} <{$params['replyTo']}>\n" .
415 "MIME-Version: 1.0\n" .
416 "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
418 // assemble the final multipart message body with the text and html types included
419 $textBody = chunk_split(base64_encode($params['textVersion']));
420 $htmlBody = chunk_split(base64_encode($params['htmlVersion']));
421 $multipartMessageBody =
422 "--" . $mimeBoundary . "\n" . // plain text section
423 "Content-Type: text/plain; charset=UTF-8\n" .
424 "Content-Transfer-Encoding: base64\n\n" .
426 "--" . $mimeBoundary . "\n" . // text/html section
427 "Content-Type: text/html; charset=UTF-8\n" .
428 "Content-Transfer-Encoding: base64\n\n" .
430 "--" . $mimeBoundary . "--\n"; // message ending
434 $params['toEmail'], // send to address
435 $params['messageSubject'], // subject
436 $multipartMessageBody, // message body
437 $messageHeader // message headers
439 logger("notification: enotify::send returns " . $res, LOGGER_DEBUG);