2 require_once('include/Emailer.php');
3 require_once('include/email.php');
4 require_once('include/bbcode.php');
5 require_once('include/html2bbcode.php');
7 function notification($params) {
9 #logger('notification()', LOGGER_DEBUG);
13 // from here on everything is in the recipients language
15 push_lang($params['language']);
19 if(array_key_exists('item',$params)) {
20 $title = $params['item']['title'];
21 $body = $params['item']['body'];
27 // e.g. "your post", "David's photo", etc.
28 $possess_desc = t('%s <!item_type!>');
30 if($params['type'] == NOTIFY_MAIL) {
32 $subject = sprintf( t('[Friendica:Notify] New mail received at %s'),$sitename);
34 $preamble = sprintf( t('%1$s sent you a new private message at %2$s.'),$params['source_name'],$sitename);
35 $epreamble = sprintf( t('%1$s sent you %2$s.'),'[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=$itemlink]' . t('a private message') . '[/url]');
36 $sitelink = t('Please visit %s to view and/or reply to your private messages.');
37 $tsitelink = sprintf( $sitelink, $siteurl . '/message/' . $params['item']['id'] );
38 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '/message/' . $params['item']['id'] . '">' . $sitename . '</a>');
39 $itemlink = $siteurl . '/message/' . $params['item']['id'];
42 if($params['type'] == NOTIFY_COMMENT) {
43 // logger("notification: params = " . print_r($params, true), LOGGER_DEBUG);
45 $parent_id = $params['parent'];
47 $p = q("SELECT `ignored` FROM `thread` WHERE `iid` = %d AND `uid` = %d LIMIT 1",
49 intval($params['uid'])
51 if ($p AND count($p) AND ($p[0]["ignored"])) {
52 logger("Thread ".$parent_id." will be ignored", LOGGER_DEBUG);
56 // Check to see if there was already a tag notify or comment notify for this post.
57 // If so don't create a second notification
60 $p = q("select id from notify where (type = %d or type = %d or type = %d) and link = '%s' and uid = %d limit 1",
61 intval(NOTIFY_TAGSELF),
62 intval(NOTIFY_COMMENT),
64 dbesc($params['link']),
65 intval($params['uid'])
67 if($p and count($p)) {
73 // if it's a post figure out who's post it is.
77 if($params['otype'] === 'item' && $parent_id) {
78 $p = q("select * from item where id = %d and uid = %d limit 1",
80 intval($params['uid'])
84 $item_post_type = item_post_type($p[0]);
85 //$possess_desc = str_replace('<!item_type!>',$possess_desc);
88 $dest_str = sprintf(t('%1$s commented on [url=%2$s]a %3$s[/url]'),
89 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
93 // "George Bull's post"
95 $dest_str = sprintf(t('%1$s commented on [url=%2$s]%3$s\'s %4$s[/url]'),
96 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
102 if($p[0]['owner-name'] == $p[0]['author-name'] && $p[0]['wall'])
103 $dest_str = sprintf(t('%1$s commented on [url=%2$s]your %3$s[/url]'),
104 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
108 // Some mail softwares relies on subject field for threading.
109 // So, we cannot have different subjects for notifications of the same thread.
110 // Before this we have the name of the replier on the subject rendering
111 // differents subjects for messages on the same thread.
113 $subject = sprintf( t('[Friendica:Notify] Comment to conversation #%1$d by %2$s'), $parent_id, $params['source_name']);
114 $preamble = sprintf( t('%s commented on an item/conversation you have been following.'), $params['source_name']);
115 $epreamble = $dest_str;
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_WALL) {
124 $subject = sprintf( t('[Friendica:Notify] %s posted to your profile wall') , $params['source_name']);
126 $preamble = sprintf( t('%1$s posted to your profile wall at %2$s') , $params['source_name'], $sitename);
128 $epreamble = sprintf( t('%1$s posted to [url=%2$s]your wall[/url]') ,
129 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
132 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
133 $tsitelink = sprintf( $sitelink, $siteurl );
134 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
135 $itemlink = $params['link'];
138 if($params['type'] == NOTIFY_TAGSELF) {
139 $subject = sprintf( t('[Friendica:Notify] %s tagged you') , $params['source_name']);
140 $preamble = sprintf( t('%1$s tagged you at %2$s') , $params['source_name'], $sitename);
141 $epreamble = sprintf( t('%1$s [url=%2$s]tagged you[/url].') ,
142 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
145 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
146 $tsitelink = sprintf( $sitelink, $siteurl );
147 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
148 $itemlink = $params['link'];
151 if($params['type'] == NOTIFY_SHARE) {
152 $subject = sprintf( t('[Friendica:Notify] %s shared a new post') , $params['source_name']);
153 $preamble = sprintf( t('%1$s shared a new post at %2$s') , $params['source_name'], $sitename);
154 $epreamble = sprintf( t('%1$s [url=%2$s]shared a post[/url].') ,
155 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
158 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
159 $tsitelink = sprintf( $sitelink, $siteurl );
160 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
161 $itemlink = $params['link'];
164 if($params['type'] == NOTIFY_POKE) {
166 $subject = sprintf( t('[Friendica:Notify] %1$s poked you') , $params['source_name']);
167 $preamble = sprintf( t('%1$s poked you at %2$s') , $params['source_name'], $sitename);
168 $epreamble = sprintf( t('%1$s [url=%2$s]poked you[/url].') ,
169 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
172 $subject = str_replace('poked', t($params['activity']), $subject);
173 $preamble = str_replace('poked', t($params['activity']), $preamble);
174 $epreamble = str_replace('poked', t($params['activity']), $epreamble);
176 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
177 $tsitelink = sprintf( $sitelink, $siteurl );
178 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
179 $itemlink = $params['link'];
182 if($params['type'] == NOTIFY_TAGSHARE) {
183 $subject = sprintf( t('[Friendica:Notify] %s tagged your post') , $params['source_name']);
184 $preamble = sprintf( t('%1$s tagged your post at %2$s') , $params['source_name'], $sitename);
185 $epreamble = sprintf( t('%1$s tagged [url=%2$s]your post[/url]') ,
186 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
189 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
190 $tsitelink = sprintf( $sitelink, $siteurl );
191 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
192 $itemlink = $params['link'];
195 if($params['type'] == NOTIFY_INTRO) {
196 $subject = sprintf( t('[Friendica:Notify] Introduction received'));
197 $preamble = sprintf( t('You\'ve received an introduction from \'%1$s\' at %2$s'), $params['source_name'], $sitename);
198 $epreamble = sprintf( t('You\'ve received [url=%1$s]an introduction[/url] from %2$s.'),
200 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
201 $body = sprintf( t('You may visit their profile at %s'),$params['source_link']);
203 $sitelink = t('Please visit %s to approve or reject the introduction.');
204 $tsitelink = sprintf( $sitelink, $siteurl );
205 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
206 $itemlink = $params['link'];
209 if($params['type'] == NOTIFY_SUGGEST) {
210 $subject = sprintf( t('[Friendica:Notify] Friend suggestion received'));
211 $preamble = sprintf( t('You\'ve received a friend suggestion from \'%1$s\' at %2$s'), $params['source_name'], $sitename);
212 $epreamble = sprintf( t('You\'ve received [url=%1$s]a friend suggestion[/url] for %2$s from %3$s.'),
214 '[url=' . $params['item']['url'] . ']' . $params['item']['name'] . '[/url]',
215 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
217 $body = t('Name:') . ' ' . $params['item']['name'] . "\n";
218 $body .= t('Photo:') . ' ' . $params['item']['photo'] . "\n";
219 $body .= sprintf( t('You may visit their profile at %s'),$params['item']['url']);
221 $sitelink = t('Please visit %s to approve or reject the suggestion.');
222 $tsitelink = sprintf( $sitelink, $siteurl );
223 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
224 $itemlink = $params['link'];
227 if($params['type'] == NOTIFY_CONFIRM) {
230 if($params['type'] == NOTIFY_SYSTEM) {
235 /*$email = prepare_notificaion_mail($params, $subject, $preamble, $body, $sitelink, $tsitelink, $hsitelink, $itemlink);
236 if ($email) Emailer::send($email);
240 $banner = t('Friendica Notification');
241 $product = FRIENDICA_PLATFORM;
242 $siteurl = $a->get_baseurl(true);
243 $thanks = t('Thank You,');
244 $sitename = $a->config['sitename'];
245 $site_admin = sprintf( t('%s Administrator'), $sitename);
247 $sender_name = $product;
248 $hostname = $a->get_hostname();
249 if(strpos($hostname,':'))
250 $hostname = substr($hostname,0,strpos($hostname,':'));
252 $sender_email = t('noreply') . '@' . $hostname;
255 $additional_mail_header = "";
256 $additional_mail_header .= "Precedence: list\n";
257 $additional_mail_header .= "X-Friendica-Host: ".$hostname."\n";
258 $additional_mail_header .= "X-Friendica-Platform: ".FRIENDICA_PLATFORM."\n";
259 $additional_mail_header .= "X-Friendica-Version: ".FRIENDICA_VERSION."\n";
260 $additional_mail_header .= "List-ID: <notification.".$hostname.">\n";
261 $additional_mail_header .= "List-Archive: <".$a->get_baseurl()."/notifications/system>\n";
265 'subject' => $subject,
266 'preamble' => $preamble,
267 'epreamble' => $epreamble,
269 'sitelink' => $sitelink,
270 'tsitelink' => $tsitelink,
271 'hsitelink' => $hsitelink,
272 'itemlink' => $itemlink
275 call_hooks('enotify',$h);
277 $subject = $h['subject'];
278 $preamble = $h['preamble'];
279 $epreamble = $h['epreamble'];
281 $sitelink = $h['sitelink'];
282 $tsitelink = $h['tsitelink'];
283 $hsitelink = $h['hsitelink'];
284 $itemlink = $h['itemlink'];
290 $hash = random_string();
291 $r = q("SELECT `id` FROM `notify` WHERE `hash` = '%s' LIMIT 1",
295 } while($dups == true);
299 $datarray['hash'] = $hash;
300 $datarray['name'] = $params['source_name'];
301 $datarray['url'] = $params['source_link'];
302 $datarray['photo'] = $params['source_photo'];
303 $datarray['date'] = datetime_convert();
304 $datarray['uid'] = $params['uid'];
305 $datarray['link'] = $itemlink;
306 $datarray['parent'] = $parent_id;
307 $datarray['type'] = $params['type'];
308 $datarray['verb'] = $params['verb'];
309 $datarray['otype'] = $params['otype'];
310 $datarray['abort'] = false;
312 call_hooks('enotify_store', $datarray);
314 if($datarray['abort']) {
319 // create notification entry in DB
321 $r = q("insert into notify (hash,name,url,photo,date,uid,link,parent,type,verb,otype)
322 values('%s','%s','%s','%s','%s',%d,'%s',%d,%d,'%s','%s')",
323 dbesc($datarray['hash']),
324 dbesc($datarray['name']),
325 dbesc($datarray['url']),
326 dbesc($datarray['photo']),
327 dbesc($datarray['date']),
328 intval($datarray['uid']),
329 dbesc($datarray['link']),
330 intval($datarray['parent']),
331 intval($datarray['type']),
332 dbesc($datarray['verb']),
333 dbesc($datarray['otype'])
336 $r = q("select id from notify where hash = '%s' and uid = %d limit 1",
338 intval($params['uid'])
341 $notify_id = $r[0]['id'];
347 // we seem to have a lot of duplicate comment notifications due to race conditions, mostly from forums
348 // After we've stored everything, look again to see if there are any duplicates and if so remove them
351 $p = q("select id from notify where ( type = %d or type = %d ) and link = '%s' and uid = %d order by id",
352 intval(NOTIFY_TAGSELF),
353 intval(NOTIFY_COMMENT),
354 dbesc($params['link']),
355 intval($params['uid'])
357 if($p && (count($p) > 1)) {
358 for ($d = 1; $d < count($p); $d ++) {
359 q("delete from notify where id = %d",
364 // only continue on if we stored the first one
366 if($notify_id != $p[0]['id']) {
373 $itemlink = $a->get_baseurl() . '/notify/view/' . $notify_id;
374 $msg = replace_macros($epreamble,array('$itemlink' => $itemlink));
375 $r = q("update notify set msg = '%s' where id = %d and uid = %d",
378 intval($params['uid'])
382 // send email notification if notification preferences permit
384 if((intval($params['notify_flags']) & intval($params['type'])) || $params['type'] == NOTIFY_SYSTEM) {
386 logger('notification: sending notification email');
388 if (isset($params['parent']) AND (intval($params['parent']) != 0)) {
389 $id_for_parent = $params['parent']."@".$hostname;
391 // Is this the first email notification for this parent item and user?
393 $r = q("select `id` from `notify-threads` where `master-parent-item` = %d and `receiver-uid` = %d limit 1",
394 intval($params['parent']),
395 intval($params['uid']) );
397 // If so, create the record of it and use a message-id smtp header.
400 logger("notify_id:".intval($notify_id).", parent: ".intval($params['parent'])."uid: ".intval($params['uid']), LOGGER_DEBUG);
401 $r = q("insert into `notify-threads` (`notify-id`, `master-parent-item`, `receiver-uid`, `parent-item`)
402 values(%d,%d,%d,%d)",
404 intval($params['parent']),
405 intval($params['uid']),
408 $additional_mail_header .= "Message-ID: <${id_for_parent}>\n";
409 $log_msg = "include/enotify: No previous notification found for this parent:\n" .
410 " parent: ${params['parent']}\n" . " uid : ${params['uid']}\n";
411 logger($log_msg, LOGGER_DEBUG);
413 // If not, just "follow" the thread.
414 $additional_mail_header .= "References: <${id_for_parent}>\nIn-Reply-To: <${id_for_parent}>\n";
415 logger("There's already a notification for this parent:\n" . print_r($r, true), LOGGER_DEBUG);
420 $textversion = strip_tags(html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",
421 $body))),ENT_QUOTES,'UTF-8'));
422 $htmlversion = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"),
423 "<br />\n",$body))),ENT_QUOTES,'UTF-8');
426 $datarray['banner'] = $banner;
427 $datarray['product'] = $product;
428 $datarray['preamble'] = $preamble;
429 $datarray['sitename'] = $sitename;
430 $datarray['siteurl'] = $siteurl;
431 $datarray['type'] = $params['type'];
432 $datarray['parent'] = $params['parent'];
433 $datarray['source_name'] = $params['source_name'];
434 $datarray['source_link'] = $params['source_link'];
435 $datarray['source_photo'] = $params['source_photo'];
436 $datarray['uid'] = $params['uid'];
437 $datarray['username'] = $params['to_name'];
438 $datarray['hsitelink'] = $hsitelink;
439 $datarray['tsitelink'] = $tsitelink;
440 $datarray['hitemlink'] = '<a href="' . $itemlink . '">' . $itemlink . '</a>';
441 $datarray['titemlink'] = $itemlink;
442 $datarray['thanks'] = $thanks;
443 $datarray['site_admin'] = $site_admin;
444 $datarray['title'] = stripslashes($title);
445 $datarray['htmlversion'] = $htmlversion;
446 $datarray['textversion'] = $textversion;
447 $datarray['subject'] = $subject;
448 $datarray['headers'] = $additional_mail_header;
450 call_hooks('enotify_mail', $datarray);
452 // check whether sending post content in email notifications is allowed
453 $content_allowed = !get_config('system','enotify_no_content');
455 // load the template for private message notifications
456 $tpl = get_markup_template('email_notify_html.tpl');
457 $email_html_body = replace_macros($tpl,array(
458 '$banner' => $datarray['banner'],
459 '$product' => $datarray['product'],
460 '$preamble' => $datarray['preamble'],
461 '$sitename' => $datarray['sitename'],
462 '$siteurl' => $datarray['siteurl'],
463 '$source_name' => $datarray['source_name'],
464 '$source_link' => $datarray['source_link'],
465 '$source_photo' => $datarray['source_photo'],
466 '$username' => $datarray['to_name'],
467 '$hsitelink' => $datarray['hsitelink'],
468 '$hitemlink' => $datarray['hitemlink'],
469 '$thanks' => $datarray['thanks'],
470 '$site_admin' => $datarray['site_admin'],
471 '$title' => $datarray['title'],
472 '$htmlversion' => $datarray['htmlversion'],
473 '$content_allowed' => $content_allowed,
476 // load the template for private message notifications
477 $tpl = get_markup_template('email_notify_text.tpl');
478 $email_text_body = replace_macros($tpl,array(
479 '$banner' => $datarray['banner'],
480 '$product' => $datarray['product'],
481 '$preamble' => $datarray['preamble'],
482 '$sitename' => $datarray['sitename'],
483 '$siteurl' => $datarray['siteurl'],
484 '$source_name' => $datarray['source_name'],
485 '$source_link' => $datarray['source_link'],
486 '$source_photo' => $datarray['source_photo'],
487 '$username' => $datarray['to_name'],
488 '$tsitelink' => $datarray['tsitelink'],
489 '$titemlink' => $datarray['titemlink'],
490 '$thanks' => $datarray['thanks'],
491 '$site_admin' => $datarray['site_admin'],
492 '$title' => $datarray['title'],
493 '$textversion' => $datarray['textversion'],
494 '$content_allowed' => $content_allowed,
497 // logger('text: ' . $email_text_body);
499 // use the Emailer class to send the message
502 'fromName' => $sender_name,
503 'fromEmail' => $sender_email,
504 'replyTo' => $sender_email,
505 'toEmail' => $params['to_email'],
506 'messageSubject' => $datarray['subject'],
507 'htmlVersion' => $email_html_body,
508 'textVersion' => $email_text_body,
509 'additionalMailHeader' => $datarray['headers'],