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']);
18 $banner = t('Friendica Notification');
19 $product = FRIENDICA_PLATFORM;
20 $siteurl = $a->get_baseurl(true);
21 $thanks = t('Thank You,');
22 $sitename = $a->config['sitename'];
23 if (!x($a->config['admin_name'])) {
24 $site_admin = sprintf( t('%s Administrator'), $sitename);
26 $site_admin = sprintf( t('%1$s, %2$s Administrator'), $a->config['admin_name'], $sitename);
30 $sender_name = $sitename;
31 $hostname = $a->get_hostname();
32 if(strpos($hostname,':'))
33 $hostname = substr($hostname,0,strpos($hostname,':'));
35 $sender_email = $a->config['sender_email'];
36 if (empty($sender_email)) {
37 $sender_email = t('noreply') . '@' . $hostname;
40 $user = q("SELECT `nickname` FROM `user` WHERE `uid` = %d", intval($params['uid']));
42 $nickname = $user[0]["nickname"];
44 // with $params['show_in_notification_page'] == false, the notification isn't inserted into
45 // the database, and an email is sent if applicable.
46 // default, if not specified: true
47 $show_in_notification_page = ((x($params,'show_in_notification_page')) ? $params['show_in_notification_page']:True);
49 $additional_mail_header = "";
50 $additional_mail_header .= "Precedence: list\n";
51 $additional_mail_header .= "X-Friendica-Host: ".$hostname."\n";
52 $additional_mail_header .= "X-Friendica-Account: <".$nickname."@".$hostname.">\n";
53 $additional_mail_header .= "X-Friendica-Platform: ".FRIENDICA_PLATFORM."\n";
54 $additional_mail_header .= "X-Friendica-Version: ".FRIENDICA_VERSION."\n";
55 $additional_mail_header .= "List-ID: <notification.".$hostname.">\n";
56 $additional_mail_header .= "List-Archive: <".$a->get_baseurl()."/notifications/system>\n";
59 if(array_key_exists('item',$params)) {
60 $title = $params['item']['title'];
61 $body = $params['item']['body'];
67 // e.g. "your post", "David's photo", etc.
68 $possess_desc = t('%s <!item_type!>');
70 if (isset($params['item']['id']))
71 $item_id = $params['item']['id'];
75 if (isset($params['parent']))
76 $parent_id = $params['parent'];
80 if($params['type'] == NOTIFY_MAIL) {
82 $subject = sprintf( t('[Friendica:Notify] New mail received at %s'),$sitename);
84 $preamble = sprintf( t('%1$s sent you a new private message at %2$s.'),$params['source_name'],$sitename);
85 $epreamble = sprintf( t('%1$s sent you %2$s.'),'[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=$itemlink]' . t('a private message') . '[/url]');
86 $sitelink = t('Please visit %s to view and/or reply to your private messages.');
87 $tsitelink = sprintf( $sitelink, $siteurl . '/message/' . $params['item']['id'] );
88 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '/message/' . $params['item']['id'] . '">' . $sitename . '</a>');
89 $itemlink = $siteurl . '/message/' . $params['item']['id'];
92 if($params['type'] == NOTIFY_COMMENT) {
93 // logger("notification: params = " . print_r($params, true), LOGGER_DEBUG);
95 //$parent_id = $params['parent'];
97 $p = q("SELECT `ignored` FROM `thread` WHERE `iid` = %d AND `uid` = %d LIMIT 1",
99 intval($params['uid'])
101 if ($p AND count($p) AND ($p[0]["ignored"])) {
102 logger("Thread ".$parent_id." will be ignored", LOGGER_DEBUG);
106 // Check to see if there was already a tag notify or comment notify for this post.
107 // If so don't create a second notification
110 $p = q("select id from notify where (type = %d or type = %d or type = %d) and link = '%s' and uid = %d limit 1",
111 intval(NOTIFY_TAGSELF),
112 intval(NOTIFY_COMMENT),
113 intval(NOTIFY_SHARE),
114 dbesc($params['link']),
115 intval($params['uid'])
117 if($p and count($p)) {
123 // if it's a post figure out who's post it is.
127 if($params['otype'] === 'item' && $parent_id) {
128 $p = q("select * from item where id = %d and uid = %d limit 1",
130 intval($params['uid'])
134 $item_post_type = item_post_type($p[0]);
135 //$possess_desc = str_replace('<!item_type!>',$possess_desc);
138 $dest_str = sprintf(t('%1$s commented on [url=%2$s]a %3$s[/url]'),
139 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
143 // "George Bull's post"
145 $dest_str = sprintf(t('%1$s commented on [url=%2$s]%3$s\'s %4$s[/url]'),
146 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
148 $p[0]['author-name'],
152 if($p[0]['owner-name'] == $p[0]['author-name'] && $p[0]['wall'])
153 $dest_str = sprintf(t('%1$s commented on [url=%2$s]your %3$s[/url]'),
154 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
158 // Some mail softwares relies on subject field for threading.
159 // So, we cannot have different subjects for notifications of the same thread.
160 // Before this we have the name of the replier on the subject rendering
161 // differents subjects for messages on the same thread.
163 $subject = sprintf( t('[Friendica:Notify] Comment to conversation #%1$d by %2$s'), $parent_id, $params['source_name']);
164 $preamble = sprintf( t('%s commented on an item/conversation you have been following.'), $params['source_name']);
165 $epreamble = $dest_str;
167 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
168 $tsitelink = sprintf( $sitelink, $siteurl );
169 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
170 $itemlink = $params['link'];
173 if($params['type'] == NOTIFY_WALL) {
174 $subject = sprintf( t('[Friendica:Notify] %s posted to your profile wall') , $params['source_name']);
176 $preamble = sprintf( t('%1$s posted to your profile wall at %2$s') , $params['source_name'], $sitename);
178 $epreamble = sprintf( t('%1$s posted to [url=%2$s]your wall[/url]') ,
179 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
182 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
183 $tsitelink = sprintf( $sitelink, $siteurl );
184 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
185 $itemlink = $params['link'];
188 if($params['type'] == NOTIFY_TAGSELF) {
189 $subject = sprintf( t('[Friendica:Notify] %s tagged you') , $params['source_name']);
190 $preamble = sprintf( t('%1$s tagged you at %2$s') , $params['source_name'], $sitename);
191 $epreamble = sprintf( t('%1$s [url=%2$s]tagged you[/url].') ,
192 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
195 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
196 $tsitelink = sprintf( $sitelink, $siteurl );
197 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
198 $itemlink = $params['link'];
201 if($params['type'] == NOTIFY_SHARE) {
202 $subject = sprintf( t('[Friendica:Notify] %s shared a new post') , $params['source_name']);
203 $preamble = sprintf( t('%1$s shared a new post at %2$s') , $params['source_name'], $sitename);
204 $epreamble = sprintf( t('%1$s [url=%2$s]shared a post[/url].') ,
205 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
208 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
209 $tsitelink = sprintf( $sitelink, $siteurl );
210 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
211 $itemlink = $params['link'];
214 if($params['type'] == NOTIFY_POKE) {
216 $subject = sprintf( t('[Friendica:Notify] %1$s poked you') , $params['source_name']);
217 $preamble = sprintf( t('%1$s poked you at %2$s') , $params['source_name'], $sitename);
218 $epreamble = sprintf( t('%1$s [url=%2$s]poked you[/url].') ,
219 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
222 $subject = str_replace('poked', t($params['activity']), $subject);
223 $preamble = str_replace('poked', t($params['activity']), $preamble);
224 $epreamble = str_replace('poked', t($params['activity']), $epreamble);
226 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
227 $tsitelink = sprintf( $sitelink, $siteurl );
228 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
229 $itemlink = $params['link'];
232 if($params['type'] == NOTIFY_TAGSHARE) {
233 $subject = sprintf( t('[Friendica:Notify] %s tagged your post') , $params['source_name']);
234 $preamble = sprintf( t('%1$s tagged your post at %2$s') , $params['source_name'], $sitename);
235 $epreamble = sprintf( t('%1$s tagged [url=%2$s]your post[/url]') ,
236 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
239 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
240 $tsitelink = sprintf( $sitelink, $siteurl );
241 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
242 $itemlink = $params['link'];
245 if($params['type'] == NOTIFY_INTRO) {
246 $subject = sprintf( t('[Friendica:Notify] Introduction received'));
247 $preamble = sprintf( t('You\'ve received an introduction from \'%1$s\' at %2$s'), $params['source_name'], $sitename);
248 $epreamble = sprintf( t('You\'ve received [url=%1$s]an introduction[/url] from %2$s.'),
250 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
251 $body = sprintf( t('You may visit their profile at %s'),$params['source_link']);
253 $sitelink = t('Please visit %s to approve or reject the introduction.');
254 $tsitelink = sprintf( $sitelink, $siteurl );
255 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
256 $itemlink = $params['link'];
258 switch ($params['verb']) {
259 case ACTIVITY_FRIEND:
260 // someone started to share with user (mostly OStatus)
261 $subject = sprintf( t('[Friendica:Notify] A new person is sharing with you'));
262 $preamble = sprintf( t('%1$s is sharing with you at %2$s'), $params['source_name'], $sitename);
263 $epreamble = sprintf( t('%1$s is sharing with you at %2$s'),
264 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
267 case ACTIVITY_FOLLOW:
268 // someone started to follow the user (mostly OStatus)
269 $subject = sprintf( t('[Friendica:Notify] You have a new follower'));
270 $preamble = sprintf( t('You have a new follower at %2$s : %1$s'), $params['source_name'], $sitename);
271 $epreamble = sprintf( t('You have a new follower at %2$s : %1$s'),
272 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
276 // ACTIVITY_REQ_FRIEND is default activity for notifications
283 if($params['type'] == NOTIFY_SUGGEST) {
284 $subject = sprintf( t('[Friendica:Notify] Friend suggestion received'));
285 $preamble = sprintf( t('You\'ve received a friend suggestion from \'%1$s\' at %2$s'), $params['source_name'], $sitename);
286 $epreamble = sprintf( t('You\'ve received [url=%1$s]a friend suggestion[/url] for %2$s from %3$s.'),
288 '[url=' . $params['item']['url'] . ']' . $params['item']['name'] . '[/url]',
289 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
291 $body = t('Name:') . ' ' . $params['item']['name'] . "\n";
292 $body .= t('Photo:') . ' ' . $params['item']['photo'] . "\n";
293 $body .= sprintf( t('You may visit their profile at %s'),$params['item']['url']);
295 $sitelink = t('Please visit %s to approve or reject the suggestion.');
296 $tsitelink = sprintf( $sitelink, $siteurl );
297 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
298 $itemlink = $params['link'];
301 if($params['type'] == NOTIFY_CONFIRM) {
302 if ($params['verb'] == ACTIVITY_FRIEND ){ // mutual connection
303 $subject = sprintf( t('[Friendica:Notify] Connection accepted'));
304 $preamble = sprintf( t('\'%1$s\' has accepted your connection request at %2$s'), $params['source_name'], $sitename);
305 $epreamble = sprintf( t('%2$s has accepted your [url=%1$s]connection request[/url].'),
307 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
308 $body = t('You are now mutual friends and may exchange status updates, photos, and email
309 without restriction.');
311 $sitelink = t('Please visit %s if you wish to make any changes to this relationship.');
312 $tsitelink = sprintf( $sitelink, $siteurl );
313 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
314 $itemlink = $params['link'];
315 } else { // ACTIVITY_FOLLOW
316 $subject = sprintf( t('[Friendica:Notify] Connection accepted'));
317 $preamble = sprintf( t('\'%1$s\' has accepted your connection request at %2$s'), $params['source_name'], $sitename);
318 $epreamble = sprintf( t('%2$s has accepted your [url=%1$s]connection request[/url].'),
320 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
321 $body = sprintf(t('\'%1$s\' has chosen to accept you a "fan", which restricts some forms of communication - such as private messaging and some profile interactions. If this is a celebrity or community page, these settings were applied automatically.'), $params['source_name']);
323 $body .= sprintf(t('\'%1$s\' may choose to extend this into a two-way or more permissive relationship in the future. '), $params['source_name']);
325 $sitelink = t('Please visit %s if you wish to make any changes to this relationship.');
326 $tsitelink = sprintf( $sitelink, $siteurl );
327 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
328 $itemlink = $params['link'];
333 if($params['type'] == NOTIFY_SYSTEM) {
334 switch($params['event']) {
335 case "SYSTEM_REGISTER_REQUEST":
336 $subject = sprintf( t('[Friendica System:Notify] registration request'));
337 $preamble = sprintf( t('You\'ve received a registration request from \'%1$s\' at %2$s'), $params['source_name'], $sitename);
338 $epreamble = sprintf( t('You\'ve received a [url=%1$s]registration request[/url] from %2$s.'),
340 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
341 $body = sprintf( t('Full Name: %1$s\nSite Location: %2$s\nLogin Name: %3$s (%4$s)'),
342 $params['source_name'], $siteurl, $params['source_mail'], $params['source_nick']);
344 $sitelink = t('Please visit %s to approve or reject the request.');
345 $tsitelink = sprintf( $sitelink, $params['link'] );
346 $hsitelink = sprintf( $sitelink, '<a href="' . $params['link'] . '">' . $sitename . '</a><br><br>');
347 $itemlink = $params['link'];
349 case "SYSTEM_DB_UPDATE_FAIL":
354 if ($params['type'] == "SYSTEM_EMAIL"){
355 // not part of the notifications.
356 // it just send a mail to the user.
357 // It will be used by the system to send emails to users (like
358 // password reset, invitations and so) using one look (but without
359 // add a notification to the user, with could be inexistent)
360 $subject = $params['subject'];
361 $preamble = $params['preamble'];
362 $body = $params['body'];
367 $show_in_notification_page = false;
370 $subject .= " (".$nickname."@".$hostname.")";
374 'subject' => $subject,
375 'preamble' => $preamble,
376 'epreamble' => $epreamble,
378 'sitelink' => $sitelink,
379 'tsitelink' => $tsitelink,
380 'hsitelink' => $hsitelink,
381 'itemlink' => $itemlink
384 call_hooks('enotify',$h);
386 $subject = $h['subject'];
387 $preamble = $h['preamble'];
388 $epreamble = $h['epreamble'];
390 $sitelink = $h['sitelink'];
391 $tsitelink = $h['tsitelink'];
392 $hsitelink = $h['hsitelink'];
393 $itemlink = $h['itemlink'];
397 if ($show_in_notification_page) {
398 logger("adding notification entry", LOGGER_DEBUG);
401 $hash = random_string();
402 $r = q("SELECT `id` FROM `notify` WHERE `hash` = '%s' LIMIT 1",
406 } while($dups == true);
410 $datarray['hash'] = $hash;
411 $datarray['name'] = $params['source_name'];
412 $datarray['url'] = $params['source_link'];
413 $datarray['photo'] = $params['source_photo'];
414 $datarray['date'] = datetime_convert();
415 $datarray['uid'] = $params['uid'];
416 $datarray['link'] = $itemlink;
417 $datarray['iid'] = $item_id;
418 $datarray['parent'] = $parent_id;
419 $datarray['type'] = $params['type'];
420 $datarray['verb'] = $params['verb'];
421 $datarray['otype'] = $params['otype'];
422 $datarray['abort'] = false;
424 call_hooks('enotify_store', $datarray);
426 if($datarray['abort']) {
431 // create notification entry in DB
433 $r = q("insert into notify (hash,name,url,photo,date,uid,link,iid,parent,type,verb,otype)
434 values('%s','%s','%s','%s','%s',%d,'%s',%d,%d,%d,'%s','%s')",
435 dbesc($datarray['hash']),
436 dbesc($datarray['name']),
437 dbesc($datarray['url']),
438 dbesc($datarray['photo']),
439 dbesc($datarray['date']),
440 intval($datarray['uid']),
441 dbesc($datarray['link']),
442 intval($datarray['iid']),
443 intval($datarray['parent']),
444 intval($datarray['type']),
445 dbesc($datarray['verb']),
446 dbesc($datarray['otype'])
449 $r = q("select id from notify where hash = '%s' and uid = %d limit 1",
451 intval($params['uid'])
454 $notify_id = $r[0]['id'];
460 // we seem to have a lot of duplicate comment notifications due to race conditions, mostly from forums
461 // After we've stored everything, look again to see if there are any duplicates and if so remove them
464 $p = q("select id from notify where ( type = %d or type = %d ) and link = '%s' and uid = %d order by id",
465 intval(NOTIFY_TAGSELF),
466 intval(NOTIFY_COMMENT),
467 dbesc($params['link']),
468 intval($params['uid'])
470 if($p && (count($p) > 1)) {
471 for ($d = 1; $d < count($p); $d ++) {
472 q("delete from notify where id = %d",
477 // only continue on if we stored the first one
479 if($notify_id != $p[0]['id']) {
486 $itemlink = $a->get_baseurl() . '/notify/view/' . $notify_id;
487 $msg = replace_macros($epreamble,array('$itemlink' => $itemlink));
488 $r = q("update notify set msg = '%s' where id = %d and uid = %d",
491 intval($params['uid'])
496 // send email notification if notification preferences permit
497 if((intval($params['notify_flags']) & intval($params['type']))
498 || $params['type'] == NOTIFY_SYSTEM
499 || $params['type'] == "SYSTEM_EMAIL") {
501 logger('sending notification email');
503 if (isset($params['parent']) AND (intval($params['parent']) != 0)) {
504 $id_for_parent = $params['parent']."@".$hostname;
506 // Is this the first email notification for this parent item and user?
508 $r = q("select `id` from `notify-threads` where `master-parent-item` = %d and `receiver-uid` = %d limit 1",
509 intval($params['parent']),
510 intval($params['uid']) );
512 // If so, create the record of it and use a message-id smtp header.
515 logger("notify_id:".intval($notify_id).", parent: ".intval($params['parent'])."uid: ".intval($params['uid']), LOGGER_DEBUG);
516 $r = q("insert into `notify-threads` (`notify-id`, `master-parent-item`, `receiver-uid`, `parent-item`)
517 values(%d,%d,%d,%d)",
519 intval($params['parent']),
520 intval($params['uid']),
523 $additional_mail_header .= "Message-ID: <${id_for_parent}>\n";
524 $log_msg = "include/enotify: No previous notification found for this parent:\n" .
525 " parent: ${params['parent']}\n" . " uid : ${params['uid']}\n";
526 logger($log_msg, LOGGER_DEBUG);
528 // If not, just "follow" the thread.
529 $additional_mail_header .= "References: <${id_for_parent}>\nIn-Reply-To: <${id_for_parent}>\n";
530 logger("There's already a notification for this parent:\n" . print_r($r, true), LOGGER_DEBUG);
535 // textversion keeps linebreaks
536 $textversion = strip_tags(str_replace("<br>","\n",html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",
537 $body))),ENT_QUOTES,'UTF-8')));
538 $htmlversion = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"),
539 "<br />\n",$body))),ENT_QUOTES,'UTF-8');
543 $datarray['banner'] = $banner;
544 $datarray['product'] = $product;
545 $datarray['preamble'] = $preamble;
546 $datarray['sitename'] = $sitename;
547 $datarray['siteurl'] = $siteurl;
548 $datarray['type'] = $params['type'];
549 $datarray['parent'] = $params['parent'];
550 $datarray['source_name'] = $params['source_name'];
551 $datarray['source_link'] = $params['source_link'];
552 $datarray['source_photo'] = $params['source_photo'];
553 $datarray['uid'] = $params['uid'];
554 $datarray['username'] = $params['to_name'];
555 $datarray['hsitelink'] = $hsitelink;
556 $datarray['tsitelink'] = $tsitelink;
557 $datarray['hitemlink'] = '<a href="' . $itemlink . '">' . $itemlink . '</a>';
558 $datarray['titemlink'] = $itemlink;
559 $datarray['thanks'] = $thanks;
560 $datarray['site_admin'] = $site_admin;
561 $datarray['title'] = stripslashes($title);
562 $datarray['htmlversion'] = $htmlversion;
563 $datarray['textversion'] = $textversion;
564 $datarray['subject'] = $subject;
565 $datarray['headers'] = $additional_mail_header;
567 call_hooks('enotify_mail', $datarray);
569 // check whether sending post content in email notifications is allowed
570 // always true for "SYSTEM_EMAIL"
571 $content_allowed = ((!get_config('system','enotify_no_content')) || ($params['type'] == "SYSTEM_EMAIL"));
573 // load the template for private message notifications
574 $tpl = get_markup_template('email_notify_html.tpl');
575 $email_html_body = replace_macros($tpl,array(
576 '$banner' => $datarray['banner'],
577 '$product' => $datarray['product'],
578 '$preamble' => str_replace("\n","<br>\n",$datarray['preamble']),
579 '$sitename' => $datarray['sitename'],
580 '$siteurl' => $datarray['siteurl'],
581 '$source_name' => $datarray['source_name'],
582 '$source_link' => $datarray['source_link'],
583 '$source_photo' => $datarray['source_photo'],
584 '$username' => $datarray['to_name'],
585 '$hsitelink' => $datarray['hsitelink'],
586 '$hitemlink' => $datarray['hitemlink'],
587 '$thanks' => $datarray['thanks'],
588 '$site_admin' => $datarray['site_admin'],
589 '$title' => $datarray['title'],
590 '$htmlversion' => $datarray['htmlversion'],
591 '$content_allowed' => $content_allowed,
594 // load the template for private message notifications
595 $tpl = get_markup_template('email_notify_text.tpl');
596 $email_text_body = replace_macros($tpl,array(
597 '$banner' => $datarray['banner'],
598 '$product' => $datarray['product'],
599 '$preamble' => $datarray['preamble'],
600 '$sitename' => $datarray['sitename'],
601 '$siteurl' => $datarray['siteurl'],
602 '$source_name' => $datarray['source_name'],
603 '$source_link' => $datarray['source_link'],
604 '$source_photo' => $datarray['source_photo'],
605 '$username' => $datarray['to_name'],
606 '$tsitelink' => $datarray['tsitelink'],
607 '$titemlink' => $datarray['titemlink'],
608 '$thanks' => $datarray['thanks'],
609 '$site_admin' => $datarray['site_admin'],
610 '$title' => $datarray['title'],
611 '$textversion' => $datarray['textversion'],
612 '$content_allowed' => $content_allowed,
615 // logger('text: ' . $email_text_body);
617 // use the Emailer class to send the message
619 return Emailer::send(array(
620 'uid' => $params['uid'],
621 'fromName' => $sender_name,
622 'fromEmail' => $sender_email,
623 'replyTo' => $sender_email,
624 'toEmail' => $params['to_email'],
625 'messageSubject' => $datarray['subject'],
626 'htmlVersion' => $email_html_body,
627 'textVersion' => $email_text_body,
628 'additionalMailHeader' => $datarray['headers'],
637 * @brief Checks for item related notifications and sends them
639 * @param int $itemid ID of the item for which the check should be done
640 * @param int $uid User ID
641 * @param str $defaulttype (Optional) Forces a notification with this type.
643 function check_item_notification($itemid, $uid, $defaulttype = "") {
645 $notification_data = array("uid" => $uid, "profiles" => array());
646 call_hooks('check_item_notification', $notification_data);
648 $profiles = $notification_data["profiles"];
650 $user = q("SELECT `notify-flags`, `language`, `username`, `email` FROM `user` WHERE `uid` = %d", intval($uid));
654 $owner = q("SELECT `id`, `url` FROM `contact` WHERE `self` AND `uid` = %d LIMIT 1", intval($uid));
658 $profiles[] = $owner[0]["url"];
660 $profiles2 = array();
662 foreach ($profiles AS $profile) {
663 $profiles2[] = normalise_link($profile);
664 $profiles2[] = str_replace("http://", "https://", normalise_link($profile));
667 $profiles = $profiles2;
671 foreach ($profiles AS $profile) {
672 if ($profile_list != "")
673 $profile_list .= "', '";
675 $profile_list .= dbesc($profile);
678 $profile_list = "'".$profile_list."'";
680 // Only act if it is a "real" post
681 // We need the additional check for the "local_profile" because of mixed situations on connector networks
682 $item = q("SELECT `id`, `mention`, `tag`,`parent`, `title`, `body`, `author-name`, `author-link`, `author-avatar`, `guid`,
683 `parent-uri`, `uri`, `contact-id`
684 FROM `item` WHERE `id` = %d AND `verb` IN ('%s', '') AND `type` != 'activity' AND
685 NOT (`author-link` IN ($profile_list)) LIMIT 1",
686 intval($itemid), dbesc(ACTIVITY_POST));
690 // Generate the notification array
692 $params["uid"] = $uid;
693 $params["notify_flags"] = $user[0]["notify-flags"];
694 $params["language"] = $user[0]["language"];
695 $params["to_name"] = $user[0]["username"];
696 $params["to_email"] = $user[0]["email"];
697 $params["item"] = $item[0];
698 $params["parent"] = $item[0]["parent"];
699 $params["link"] = App::get_baseurl().'/display/'.urlencode($item[0]["guid"]);
700 $params["otype"] = 'item';
701 $params["source_name"] = $item[0]["author-name"];
702 $params["source_link"] = $item[0]["author-link"];
703 $params["source_photo"] = $item[0]["author-avatar"];
705 if ($item[0]["parent-uri"] === $item[0]["uri"]) {
706 // Send a notification for every new post?
707 $r = q("SELECT `notify_new_posts` FROM `contact` WHERE `id` = %d AND `uid` = %d AND `notify_new_posts` LIMIT 1",
708 intval($item[0]['contact-id']),
711 $send_notification = count($r);
713 if (!$send_notification) {
714 $tags = q("SELECT `url` FROM `term` WHERE `otype` = %d AND `oid` = %d AND `type` = %d AND `uid` = %d",
715 intval(TERM_OBJ_POST), intval($itemid), intval(TERM_MENTION), intval($uid));
718 foreach ($tags AS $tag) {
719 $r = q("SELECT `id` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d AND `notify_new_posts`",
720 normalise_link($tag["url"]), intval($uid));
722 $send_notification = true;
727 if ($send_notification) {
728 $params["type"] = NOTIFY_SHARE;
729 $params["verb"] = ACTIVITY_TAG;
733 // Is the user mentioned in this post?
736 foreach ($profiles AS $profile) {
737 if (strpos($item[0]["tag"], "=".$profile."]") OR strpos($item[0]["body"], "=".$profile."]"))
741 if ($item[0]["mention"] OR $tagged OR ($defaulttype == NOTIFY_TAGSELF)) {
742 $params["type"] = NOTIFY_TAGSELF;
743 $params["verb"] = ACTIVITY_TAG;
746 // Is it a post that the user had started or where he interacted?
747 $parent = q("SELECT `thread`.`iid` FROM `thread` INNER JOIN `item` ON `item`.`parent` = `thread`.`iid`
748 WHERE `thread`.`iid` = %d AND `thread`.`uid` = %d AND NOT `thread`.`ignored` AND
749 (`thread`.`mention` OR `item`.`author-link` IN ($profile_list))
751 intval($item[0]["parent"]), intval($uid));
753 if ($parent AND !isset($params["type"])) {
754 $params["type"] = NOTIFY_COMMENT;
755 $params["verb"] = ACTIVITY_POST;
758 if (isset($params["type"]))
759 notification($params);