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('%s sent you a new private message at %s.'),$params['source_name'],$sitename);
45 $epreamble = sprintf( t('%s sent you %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 $possess_desc = str_replace('<!item_type!>',item_post_type($p[0]),$possess_desc);
72 $dest_str = sprintf($possess_desc,'a');
74 // "George Bull's post"
76 $dest_str = sprintf($possess_desc,sprintf( t("%s's"),$p[0]['author-name']));
79 if($p[0]['owner-name'] == $p[0]['author-name'] && $p[0]['wall'])
80 $dest_str = sprintf($possess_desc, t('your') );
82 // Some mail softwares relies on subject field for threading.
83 // So, we cannot have different subjects for notifications of the same thread.
84 // Before this we have the name of the replier on the subject rendering
85 // differents subjects for messages on the same thread.
87 $subject = sprintf( t('[Friendica:Notify] Comment to conversation #%d by %s'), $parent_id, $params['source_name']);
88 $preamble = sprintf( t('%s commented on an item/conversation you have been following.'), $params['source_name']);
89 $epreamble = sprintf( t('%s commented on %s.'), '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=$itemlink]' . $dest_str . '[/url]');
91 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
92 $tsitelink = sprintf( $sitelink, $siteurl );
93 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
94 $itemlink = $params['link'];
97 if($params['type'] == NOTIFY_WALL) {
98 $subject = sprintf( t('[Friendica:Notify] %s posted to your profile wall') , $params['source_name']);
100 $preamble = sprintf( t('%s posted to your profile wall at %s') , $params['source_name'], $sitename);
102 $epreamble = sprintf( t('%s posted to %s') , '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=$itemlink]' . t('your profile wall.') . '[/url]');
104 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
105 $tsitelink = sprintf( $sitelink, $siteurl );
106 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
107 $itemlink = $params['link'];
110 if($params['type'] == NOTIFY_TAGSELF) {
111 $subject = sprintf( t('[Friendica:Notify] %s tagged you') , $params['source_name']);
112 $preamble = sprintf( t('%s tagged you at %s') , $params['source_name'], $sitename);
113 $epreamble = sprintf( t('%s %s.') , '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=' . $params['link'] . ']' . t('tagged you') . '[/url]');
115 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
116 $tsitelink = sprintf( $sitelink, $siteurl );
117 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
118 $itemlink = $params['link'];
121 if($params['type'] == NOTIFY_TAGSHARE) {
122 $subject = sprintf( t('[Friendica:Notify] %s tagged your post') , $params['source_name']);
123 $preamble = sprintf( t('%s tagged your post at %s') , $params['source_name'], $sitename);
124 $epreamble = sprintf( t('%s tagged %s') , '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=$itemlink]' . t('your post') . '[/url]' );
126 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
127 $tsitelink = sprintf( $sitelink, $siteurl );
128 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
129 $itemlink = $params['link'];
132 if($params['type'] == NOTIFY_INTRO) {
133 $subject = sprintf( t('[Friendica:Notify] Introduction received'));
134 $preamble = sprintf( t('You\'ve received an introduction from \'%s\' at %s'), $params['source_name'], $sitename);
135 $epreamble = sprintf( t('You\'ve received %s from %s.'), '[url=$itemlink]' . t('an introduction') . '[/url]' , '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
136 $body = sprintf( t('You may visit their profile at %s'),$params['source_link']);
138 $sitelink = t('Please visit %s to approve or reject the introduction.');
139 $tsitelink = sprintf( $sitelink, $siteurl );
140 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
141 $itemlink = $params['link'];
144 if($params['type'] == NOTIFY_SUGGEST) {
145 $subject = sprintf( t('[Friendica:Notify] Friend suggestion received'));
146 $preamble = sprintf( t('You\'ve received a friend suggestion from \'%s\' at %s'), $params['source_name'], $sitename);
147 $epreamble = sprintf( t('You\'ve received %s for %s from %s.'),
148 '[url=$itemlink]' . t('a friend suggestion') . '[/url]',
149 '[url=' . $params['item']['url'] . ']' . $params['item']['name'] . '[/url]',
150 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
151 $body = t('Name:') . ' ' . $params['item']['name'] . "\n";
152 $body .= t('Photo:') . ' ' . $params['item']['photo'] . "\n";
153 $body .= sprintf( t('You may visit their profile at %s'),$params['item']['url']);
155 $sitelink = t('Please visit %s to approve or reject the suggestion.');
156 $tsitelink = sprintf( $sitelink, $siteurl );
157 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
158 $itemlink = $params['link'];
161 if($params['type'] == NOTIFY_CONFIRM) {
165 if($params['type'] == NOTIFY_SYSTEM) {
171 'subject' => $subject,
172 'preamble' => $preamble,
173 'epreamble' => $epreamble,
175 'sitelink' => $sitelink,
176 'tsitelink' => $tsitelink,
177 'hsitelink' => $hsitelink,
178 'itemlink' => $itemlink
181 call_hooks('enotify',$h);
183 $subject = $h['subject'];
184 $preamble = $h['preamble'];
185 $epreamble = $h['epreamble'];
187 $sitelink = $h['sitelink'];
188 $tsitelink = $h['tsitelink'];
189 $hsitelink = $h['hsitelink'];
190 $itemlink = $h['itemlink'];
193 require_once('include/html2bbcode.php');
197 $hash = random_string();
198 $r = q("SELECT `id` FROM `notify` WHERE `hash` = '%s' LIMIT 1",
202 } while($dups == true);
206 $datarray['hash'] = $hash;
207 $datarray['name'] = $params['source_name'];
208 $datarray['url'] = $params['source_link'];
209 $datarray['photo'] = $params['source_photo'];
210 $datarray['date'] = datetime_convert();
211 $datarray['uid'] = $params['uid'];
212 $datarray['link'] = $itemlink;
213 $datarray['parent'] = $parent_id;
214 $datarray['type'] = $params['type'];
215 $datarray['verb'] = $params['verb'];
216 $datarray['otype'] = $params['otype'];
218 call_hooks('enotify_store', $datarray);
220 // create notification entry in DB
222 $r = q("insert into notify (hash,name,url,photo,date,uid,link,parent,type,verb,otype)
223 values('%s','%s','%s','%s','%s',%d,'%s',%d,%d,'%s','%s')",
224 dbesc($datarray['hash']),
225 dbesc($datarray['name']),
226 dbesc($datarray['url']),
227 dbesc($datarray['photo']),
228 dbesc($datarray['date']),
229 intval($datarray['uid']),
230 dbesc($datarray['link']),
231 intval($datarray['parent']),
232 intval($datarray['type']),
233 dbesc($datarray['verb']),
234 dbesc($datarray['otype'])
237 $r = q("select id from notify where hash = '%s' and uid = %d limit 1",
239 intval($params['uid'])
242 $notify_id = $r[0]['id'];
248 $itemlink = $a->get_baseurl() . '/notify/view/' . $notify_id;
249 $msg = replace_macros($epreamble,array('$itemlink' => $itemlink));
250 $r = q("update notify set msg = '%s' where id = %d and uid = %d limit 1",
253 intval($params['uid'])
257 // send email notification if notification preferences permit
259 require_once('bbcode.php');
260 if((intval($params['notify_flags']) & intval($params['type'])) || $params['type'] == NOTIFY_SYSTEM) {
262 logger('notification: sending notification email');
264 $id_for_parent = "${params['parent']}@${hostname}";
266 // Is this the first email notification for this parent item and user?
268 $r = q("select `id` from `notify-threads` where `master-parent-item` = %d and `receiver-uid` = %d limit 1",
269 intval($params['parent']),
270 intval($params['uid']) );
272 // If so, create the record of it and use a message-id smtp header.
275 logger("norify_id:" . intval($notify_id). ", parent: " . intval($params['parent']) . "uid: " .
276 intval($params['uid']), LOGGER_DEBUG);
277 $r = q("insert into `notify-threads` (`notify-id`, `master-parent-item`, `receiver-uid`, `parent-item`)
278 values(%d,%d,%d,%d)",
280 intval($params['parent']),
281 intval($params['uid']),
284 $additional_mail_header .= "Message-ID: <${id_for_parent}>\n";
285 $log_msg = "include/enotify: No previous notification found for this parent:\n" .
286 " parent: ${params['parent']}\n" . " uid : ${params['uid']}\n";
287 logger($log_msg, LOGGER_DEBUG);
290 // If not, just "follow" the thread.
293 $additional_mail_header = "References: <${id_for_parent}>\nIn-Reply-To: <${id_for_parent}>\n";
294 logger("include/enotify: There's already a notification for this parent:\n" . print_r($r, true), LOGGER_DEBUG);
299 $textversion = strip_tags(html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",
300 $body))),ENT_QUOTES,'UTF-8'));
301 $htmlversion = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"),
302 "<br />\n",$body))));
305 $datarray['banner'] = $banner;
306 $datarray['product'] = $product;
307 $datarray['preamble'] = $preamble;
308 $datarray['sitename'] = $sitename;
309 $datarray['siteurl'] = $siteurl;
310 $datarray['type'] = $params['type'];
311 $datarray['parent'] = $params['parent'];
312 $datarray['source_name'] = $params['source_name'];
313 $datarray['source_link'] = $params['source_link'];
314 $datarray['source_photo'] = $params['source_photo'];
315 $datarray['uid'] = $params['uid'];
316 $datarray['username'] = $params['to_name'];
317 $datarray['hsitelink'] = $hsitelink;
318 $datarray['tsitelink'] = $tsitelink;
319 $datarray['hitemlink'] = '<a href="' . $itemlink . '">' . $itemlink . '</a>';
320 $datarray['titemlink'] = $itemlink;
321 $datarray['thanks'] = $thanks;
322 $datarray['site_admin'] = $site_admin;
323 $datarray['title'] = stripslashes($title);
324 $datarray['htmlversion'] = $htmlversion;
325 $datarray['textversion'] = $textversion;
326 $datarray['subject'] = $subject;
327 $datarray['headers'] = $additional_mail_header;
329 call_hooks('enotify_mail', $datarray);
331 // load the template for private message notifications
332 $tpl = get_markup_template('email_notify_html.tpl');
333 $email_html_body = replace_macros($tpl,array(
334 '$banner' => $datarray['banner'],
335 '$product' => $datarray['product'],
336 '$preamble' => $datarray['preamble'],
337 '$sitename' => $datarray['sitename'],
338 '$siteurl' => $datarray['siteurl'],
339 '$source_name' => $datarray['source_name'],
340 '$source_link' => $datarray['source_link'],
341 '$source_photo' => $datarray['source_photo'],
342 '$username' => $datarray['to_name'],
343 '$hsitelink' => $datarray['hsitelink'],
344 '$hitemlink' => $datarray['hitemlink'],
345 '$thanks' => $datarray['thanks'],
346 '$site_admin' => $datarray['site_admin'],
347 '$title' => $datarray['title'],
348 '$htmlversion' => $datarray['htmlversion'],
351 // load the template for private message notifications
352 $tpl = get_markup_template('email_notify_text.tpl');
353 $email_text_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 '$tsitelink' => $datarray['tsitelink'],
364 '$titemlink' => $datarray['titemlink'],
365 '$thanks' => $datarray['thanks'],
366 '$site_admin' => $datarray['site_admin'],
367 '$title' => $datarray['title'],
368 '$textversion' => $datarray['textversion'],
371 // logger('text: ' . $email_text_body);
373 // use the EmailNotification library to send the message
376 'fromName' => $sender_name,
377 'fromEmail' => $sender_email,
378 'replyTo' => $sender_email,
379 'toEmail' => $params['to_email'],
380 'messageSubject' => $datarray['subject'],
381 'htmlVersion' => $email_html_body,
382 'textVersion' => $email_text_body,
383 'additionalMailHeader' => $datarray['headers'],
391 require_once('include/email.php');
395 * Send a multipart/alternative message with Text and HTML versions
397 * @param fromName name of the sender
398 * @param fromEmail email fo the sender
399 * @param replyTo replyTo address to direct responses
400 * @param toEmail destination email address
401 * @param messageSubject subject of the message
402 * @param htmlVersion html version of the message
403 * @param textVersion text only version of the message
404 * @param additionalMailHeader additions to the smtp mail header
406 static public function send($params) {
408 $fromName = email_header_encode(html_entity_decode($params['fromName'],ENT_QUOTES,'UTF-8'),'UTF-8');
409 $messageSubject = email_header_encode(html_entity_decode($params['messageSubject'],ENT_QUOTES,'UTF-8'),'UTF-8');
411 // generate a mime boundary
412 $mimeBoundary =rand(0,9)."-"
413 .rand(10000000000,9999999999)."-"
414 .rand(10000000000,9999999999)."=:"
417 // generate a multipart/alternative message header
419 $params['additionalMailHeader'] .
420 "From: {$params['fromName']} <{$params['fromEmail']}>\n" .
421 "Reply-To: {$params['fromName']} <{$params['replyTo']}>\n" .
422 "MIME-Version: 1.0\n" .
423 "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
425 // assemble the final multipart message body with the text and html types included
426 $textBody = chunk_split(base64_encode($params['textVersion']));
427 $htmlBody = chunk_split(base64_encode($params['htmlVersion']));
428 $multipartMessageBody =
429 "--" . $mimeBoundary . "\n" . // plain text section
430 "Content-Type: text/plain; charset=UTF-8\n" .
431 "Content-Transfer-Encoding: base64\n\n" .
433 "--" . $mimeBoundary . "\n" . // text/html section
434 "Content-Type: text/html; charset=UTF-8\n" .
435 "Content-Transfer-Encoding: base64\n\n" .
437 "--" . $mimeBoundary . "--\n"; // message ending
441 $params['toEmail'], // send to address
442 $params['messageSubject'], // subject
443 $multipartMessageBody, // message body
444 $messageHeader // message headers
446 logger("notification: enotify::send returns " . $res, LOGGER_DEBUG);