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) {
161 // from here on everything is in the recipients language
163 push_lang($params['language']);
165 require_once('include/html2bbcode.php');
169 $hash = random_string();
170 $r = q("SELECT `id` FROM `notify` WHERE `hash` = '%s' LIMIT 1",
174 } while($dups == true);
178 $datarray['hash'] = $hash;
179 $datarray['name'] = $params['source_name'];
180 $datarray['url'] = $params['source_link'];
181 $datarray['photo'] = $params['source_photo'];
182 $datarray['date'] = datetime_convert();
183 $datarray['uid'] = $params['uid'];
184 $datarray['link'] = $itemlink;
185 $datarray['parent'] = $parent_id;
186 $datarray['type'] = $params['type'];
187 $datarray['verb'] = $params['verb'];
188 $datarray['otype'] = $params['otype'];
190 call_hooks('enotify_store', $datarray);
192 // create notification entry in DB
194 $r = q("insert into notify (hash,name,url,photo,date,uid,link,parent,type,verb,otype)
195 values('%s','%s','%s','%s','%s',%d,'%s',%d,%d,'%s','%s')",
196 dbesc($datarray['hash']),
197 dbesc($datarray['name']),
198 dbesc($datarray['url']),
199 dbesc($datarray['photo']),
200 dbesc($datarray['date']),
201 intval($datarray['uid']),
202 dbesc($datarray['link']),
203 intval($datarray['parent']),
204 intval($datarray['type']),
205 dbesc($datarray['verb']),
206 dbesc($datarray['otype'])
209 $r = q("select id from notify where hash = '%s' and uid = %d limit 1",
211 intval($params['uid'])
214 $notify_id = $r[0]['id'];
218 $itemlink = $a->get_baseurl() . '/notify/view/' . $notify_id;
219 $msg = replace_macros($epreamble,array('$itemlink' => $itemlink));
220 $r = q("update notify set msg = '%s' where id = %d and uid = %d limit 1",
223 intval($params['uid'])
228 // send email notification if notification preferences permit
230 require_once('bbcode.php');
231 if((intval($params['notify_flags']) & intval($params['type'])) || $params['type'] == NOTIFY_SYSTEM) {
233 logger('notification: sending notification email');
235 $id_for_parent = "${params['parent']}@${hostname}";
237 // Is this the first email notification for this parent item and user?
239 $r = q("select `id` from `notify-threads` where `master-parent-item` = %d and `receiver-uid` = %d limit 1",
240 intval($params['parent']),
241 intval($params['uid']) );
243 // If so, create the record of it and use a message-id smtp header.
246 logger("norify_id:" . intval($notify_id). ", parent: " . intval($params['parent']) . "uid: " .
247 intval($params['uid']), LOGGER_DEBUG);
248 $r = q("insert into `notify-threads` (`notify-id`, `master-parent-item`, `receiver-uid`, `parent-item`)
249 values(%d,%d,%d,%d)",
251 intval($params['parent']),
252 intval($params['uid']),
255 $additional_mail_header .= "Message-ID: <${id_for_parent}>\n";
256 $log_msg = "include/enotify: No previous notification found for this parent:\n" .
257 " parent: ${params['parent']}\n" . " uid : ${params['uid']}\n";
258 logger($log_msg, LOGGER_DEBUG);
261 // If not, just "follow" the thread.
264 $additional_mail_header = "References: <${id_for_parent}>\nIn-Reply-To: <${id_for_parent}>\n";
265 logger("include/enotify: There's already a notification for this parent:\n" . print_r($r, true), LOGGER_DEBUG);
270 $textversion = strip_tags(html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",
271 $body))),ENT_QUOTES,'UTF-8'));
272 $htmlversion = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"),
273 "<br />\n",$body))));
276 $datarray['banner'] = $banner;
277 $datarray['product'] = $product;
278 $datarray['preamble'] = $preamble;
279 $datarray['sitename'] = $sitename;
280 $datarray['siteurl'] = $siteurl;
281 $datarray['type'] = $params['type'];
282 $datarray['parent'] = $params['parent'];
283 $datarray['source_name'] = $params['source_name'];
284 $datarray['source_link'] = $params['source_link'];
285 $datarray['source_photo'] = $params['source_photo'];
286 $datarray['uid'] = $params['uid'];
287 $datarray['username'] = $params['to_name'];
288 $datarray['hsitelink'] = $hsitelink;
289 $datarray['tsitelink'] = $tsitelink;
290 $datarray['hitemlink'] = '<a href="' . $itemlink . '">' . $itemlink . '</a>';
291 $datarray['titemlink'] = $itemlink;
292 $datarray['thanks'] = $thanks;
293 $datarray['site_admin'] = $site_admin;
294 $datarray['title'] = stripslashes($title);
295 $datarray['htmlversion'] = $htmlversion;
296 $datarray['textversion'] = $textversion;
297 $datarray['subject'] = $subject;
298 $datarray['headers'] = $additional_mail_header;
300 call_hooks('enotify_mail', $datarray);
302 // load the template for private message notifications
303 $tpl = get_markup_template('email_notify_html.tpl');
304 $email_html_body = replace_macros($tpl,array(
305 '$banner' => $datarray['banner'],
306 '$product' => $datarray['product'],
307 '$preamble' => $datarray['preamble'],
308 '$sitename' => $datarray['sitename'],
309 '$siteurl' => $datarray['siteurl'],
310 '$source_name' => $datarray['source_name'],
311 '$source_link' => $datarray['source_link'],
312 '$source_photo' => $datarray['source_photo'],
313 '$username' => $datarray['to_name'],
314 '$hsitelink' => $datarray['hsitelink'],
315 '$hitemlink' => $datarray['hitemlink'],
316 '$thanks' => $datarray['thanks'],
317 '$site_admin' => $datarray['site_admin'],
318 '$title' => $datarray['title'],
319 '$htmlversion' => $datarray['htmlversion'],
322 // load the template for private message notifications
323 $tpl = get_markup_template('email_notify_text.tpl');
324 $email_text_body = replace_macros($tpl,array(
325 '$banner' => $datarray['banner'],
326 '$product' => $datarray['product'],
327 '$preamble' => $datarray['preamble'],
328 '$sitename' => $datarray['sitename'],
329 '$siteurl' => $datarray['siteurl'],
330 '$source_name' => $datarray['source_name'],
331 '$source_link' => $datarray['source_link'],
332 '$source_photo' => $datarray['source_photo'],
333 '$username' => $datarray['to_name'],
334 '$tsitelink' => $datarray['tsitelink'],
335 '$titemlink' => $datarray['titemlink'],
336 '$thanks' => $datarray['thanks'],
337 '$site_admin' => $datarray['site_admin'],
338 '$title' => $datarray['title'],
339 '$textversion' => $datarray['textversion'],
342 // logger('text: ' . $email_text_body);
344 // use the EmailNotification library to send the message
347 'fromName' => $sender_name,
348 'fromEmail' => $sender_email,
349 'replyTo' => $sender_email,
350 'toEmail' => $params['to_email'],
351 'messageSubject' => $datarray['subject'],
352 'htmlVersion' => $email_html_body,
353 'textVersion' => $email_text_body,
354 'additionalMailHeader' => $datarray['headers'],
362 require_once('include/email.php');
366 * Send a multipart/alternative message with Text and HTML versions
368 * @param fromName name of the sender
369 * @param fromEmail email fo the sender
370 * @param replyTo replyTo address to direct responses
371 * @param toEmail destination email address
372 * @param messageSubject subject of the message
373 * @param htmlVersion html version of the message
374 * @param textVersion text only version of the message
375 * @param additionalMailHeader additions to the smtp mail header
377 static public function send($params) {
379 $fromName = email_header_encode($params['fromName'],'UTF-8');
380 $messageSubject = email_header_encode($params['messageSubject'],'UTF-8');
382 // generate a mime boundary
383 $mimeBoundary =rand(0,9)."-"
384 .rand(10000000000,9999999999)."-"
385 .rand(10000000000,9999999999)."=:"
388 // generate a multipart/alternative message header
390 $params['additionalMailHeader'] .
391 "From: {$params['fromName']} <{$params['fromEmail']}>\n" .
392 "Reply-To: {$params['fromName']} <{$params['replyTo']}>\n" .
393 "MIME-Version: 1.0\n" .
394 "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
396 // assemble the final multipart message body with the text and html types included
397 $textBody = chunk_split(base64_encode($params['textVersion']));
398 $htmlBody = chunk_split(base64_encode($params['htmlVersion']));
399 $multipartMessageBody =
400 "--" . $mimeBoundary . "\n" . // plain text section
401 "Content-Type: text/plain; charset=UTF-8\n" .
402 "Content-Transfer-Encoding: base64\n\n" .
404 "--" . $mimeBoundary . "\n" . // text/html section
405 "Content-Type: text/html; charset=UTF-8\n" .
406 "Content-Transfer-Encoding: base64\n\n" .
408 "--" . $mimeBoundary . "--\n"; // message ending
412 $params['toEmail'], // send to address
413 $params['messageSubject'], // subject
414 $multipartMessageBody, // message body
415 $messageHeader // message headers
417 logger("notification: enotify::send returns " . $res, LOGGER_DEBUG);