]> git.mxchange.org Git - friendica.git/blob - include/enotify.php
Reworked contact relations between Friendica and Diaspora
[friendica.git] / include / enotify.php
1 <?php
2 require_once('include/Emailer.php');
3 require_once('include/email.php');
4 require_once('include/bbcode.php');
5 require_once('include/html2bbcode.php');
6
7 function notification($params) {
8
9         #logger('notification()', LOGGER_DEBUG);
10
11         $a = get_app();
12
13         // from here on everything is in the recipients language
14
15         push_lang($params['language']);
16
17
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         $site_admin = sprintf( t('%s Administrator'), $sitename);
24         $nickname = "";
25
26         $sender_name = $sitename;
27         $hostname = $a->get_hostname();
28         if(strpos($hostname,':'))
29                 $hostname = substr($hostname,0,strpos($hostname,':'));
30
31         $sender_email = $a->config['sender_email'];
32         if (empty($sender_email)) {
33                 $sender_email = t('noreply') . '@' . $hostname;
34         }
35
36         $user = q("SELECT `nickname` FROM `user` WHERE `uid` = %d", intval($params['uid']));
37         if ($user)
38                 $nickname = $user[0]["nickname"];
39
40         // with $params['show_in_notification_page'] == false, the notification isn't inserted into
41         // the database, and an email is sent if applicable.
42         // default, if not specified: true
43         $show_in_notification_page = ((x($params,'show_in_notification_page'))  ?       $params['show_in_notification_page']:True);
44
45         $additional_mail_header = "";
46         $additional_mail_header .= "Precedence: list\n";
47         $additional_mail_header .= "X-Friendica-Host: ".$hostname."\n";
48         $additional_mail_header .= "X-Friendica-Account: <".$nickname."@".$hostname.">\n";
49         $additional_mail_header .= "X-Friendica-Platform: ".FRIENDICA_PLATFORM."\n";
50         $additional_mail_header .= "X-Friendica-Version: ".FRIENDICA_VERSION."\n";
51         $additional_mail_header .= "List-ID: <notification.".$hostname.">\n";
52         $additional_mail_header .= "List-Archive: <".$a->get_baseurl()."/notifications/system>\n";
53
54
55         if(array_key_exists('item',$params)) {
56                 $title = $params['item']['title'];
57                 $body = $params['item']['body'];
58         }
59         else {
60                 $title = $body = '';
61         }
62
63         // e.g. "your post", "David's photo", etc.
64         $possess_desc = t('%s <!item_type!>');
65
66         if (isset($params['item']['id']))
67                 $item_id = $params['item']['id'];
68         else
69                 $item_id = 0;
70
71         if (isset($params['parent']))
72                 $parent_id = $params['parent'];
73         else
74                 $parent_id = 0;
75
76         if($params['type'] == NOTIFY_MAIL) {
77
78                 $subject =      sprintf( t('[Friendica:Notify] New mail received at %s'),$sitename);
79
80                 $preamble = sprintf( t('%1$s sent you a new private message at %2$s.'),$params['source_name'],$sitename);
81                 $epreamble = sprintf( t('%1$s sent you %2$s.'),'[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=$itemlink]' . t('a private message') . '[/url]');
82                 $sitelink = t('Please visit %s to view and/or reply to your private messages.');
83                 $tsitelink = sprintf( $sitelink, $siteurl . '/message/' . $params['item']['id'] );
84                 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '/message/' . $params['item']['id'] . '">' . $sitename . '</a>');
85                 $itemlink = $siteurl . '/message/' . $params['item']['id'];
86         }
87
88         if($params['type'] == NOTIFY_COMMENT) {
89 //              logger("notification: params = " . print_r($params, true), LOGGER_DEBUG);
90
91                 //$parent_id = $params['parent'];
92
93                 $p = q("SELECT `ignored` FROM `thread` WHERE `iid` = %d AND `uid` = %d LIMIT 1",
94                         intval($parent_id),
95                         intval($params['uid'])
96                 );
97                 if ($p AND count($p) AND ($p[0]["ignored"])) {
98                         logger("Thread ".$parent_id." will be ignored", LOGGER_DEBUG);
99                         return;
100                 }
101
102                 // Check to see if there was already a tag notify or comment notify for this post.
103                 // If so don't create a second notification
104
105                 $p = null;
106                 $p = q("select id from notify where (type = %d or type = %d or type = %d) and link = '%s' and uid = %d limit 1",
107                         intval(NOTIFY_TAGSELF),
108                         intval(NOTIFY_COMMENT),
109                         intval(NOTIFY_SHARE),
110                         dbesc($params['link']),
111                         intval($params['uid'])
112                 );
113                 if($p and count($p)) {
114                         pop_lang();
115                         return;
116                 }
117
118
119                 // if it's a post figure out who's post it is.
120
121                 $p = null;
122
123                 if($params['otype'] === 'item' && $parent_id) {
124                         $p = q("select * from item where id = %d and uid = %d limit 1",
125                                 intval($parent_id),
126                                 intval($params['uid'])
127                         );
128                 }
129
130                 $item_post_type = item_post_type($p[0]);
131                 //$possess_desc = str_replace('<!item_type!>',$possess_desc);
132
133                 // "a post"
134                 $dest_str = sprintf(t('%1$s commented on [url=%2$s]a %3$s[/url]'),
135                                                                 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
136                                                                 $itemlink,
137                                                                 $item_post_type);
138
139                 // "George Bull's post"
140                 if($p)
141                         $dest_str = sprintf(t('%1$s commented on [url=%2$s]%3$s\'s %4$s[/url]'),
142                                                 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
143                                                 $itemlink,
144                                                 $p[0]['author-name'],
145                                                 $item_post_type);
146
147                 // "your post"
148                 if($p[0]['owner-name'] == $p[0]['author-name'] && $p[0]['wall'])
149                         $dest_str = sprintf(t('%1$s commented on [url=%2$s]your %3$s[/url]'),
150                                                                 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
151                                                                 $itemlink,
152                                                                 $item_post_type);
153
154                 // Some mail softwares relies on subject field for threading.
155                 // So, we cannot have different subjects for notifications of the same thread.
156                 // Before this we have the name of the replier on the subject rendering
157                 // differents subjects for messages on the same thread.
158
159                 $subject = sprintf( t('[Friendica:Notify] Comment to conversation #%1$d by %2$s'), $parent_id, $params['source_name']);
160                 $preamble = sprintf( t('%s commented on an item/conversation you have been following.'), $params['source_name']);
161                 $epreamble = $dest_str;
162
163                 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
164                 $tsitelink = sprintf( $sitelink, $siteurl );
165                 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
166                 $itemlink =  $params['link'];
167         }
168
169         if($params['type'] == NOTIFY_WALL) {
170                 $subject = sprintf( t('[Friendica:Notify] %s posted to your profile wall') , $params['source_name']);
171
172                 $preamble = sprintf( t('%1$s posted to your profile wall at %2$s') , $params['source_name'], $sitename);
173
174                 $epreamble = sprintf( t('%1$s posted to [url=%2$s]your wall[/url]') ,
175                                                                 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
176                                                                 $params['link']);
177
178                 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
179                 $tsitelink = sprintf( $sitelink, $siteurl );
180                 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
181                 $itemlink =  $params['link'];
182         }
183
184         if($params['type'] == NOTIFY_TAGSELF) {
185                 $subject =      sprintf( t('[Friendica:Notify] %s tagged you') , $params['source_name']);
186                 $preamble = sprintf( t('%1$s tagged you at %2$s') , $params['source_name'], $sitename);
187                 $epreamble = sprintf( t('%1$s [url=%2$s]tagged you[/url].') ,
188                                                                 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
189                                                                 $params['link']);
190
191                 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
192                 $tsitelink = sprintf( $sitelink, $siteurl );
193                 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
194                 $itemlink =  $params['link'];
195         }
196
197         if($params['type'] == NOTIFY_SHARE) {
198                 $subject =      sprintf( t('[Friendica:Notify] %s shared a new post') , $params['source_name']);
199                 $preamble = sprintf( t('%1$s shared a new post at %2$s') , $params['source_name'], $sitename);
200                 $epreamble = sprintf( t('%1$s [url=%2$s]shared a post[/url].') ,
201                                                                 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
202                                                                 $params['link']);
203
204                 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
205                 $tsitelink = sprintf( $sitelink, $siteurl );
206                 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
207                 $itemlink =  $params['link'];
208         }
209
210         if($params['type'] == NOTIFY_POKE) {
211
212                 $subject =      sprintf( t('[Friendica:Notify] %1$s poked you') , $params['source_name']);
213                 $preamble = sprintf( t('%1$s poked you at %2$s') , $params['source_name'], $sitename);
214                 $epreamble = sprintf( t('%1$s [url=%2$s]poked you[/url].') ,
215                                                                 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
216                                                                 $params['link']);
217
218                 $subject = str_replace('poked', t($params['activity']), $subject);
219                 $preamble = str_replace('poked', t($params['activity']), $preamble);
220                 $epreamble = str_replace('poked', t($params['activity']), $epreamble);
221
222                 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
223                 $tsitelink = sprintf( $sitelink, $siteurl );
224                 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
225                 $itemlink =  $params['link'];
226         }
227
228         if($params['type'] == NOTIFY_TAGSHARE) {
229                 $subject =      sprintf( t('[Friendica:Notify] %s tagged your post') , $params['source_name']);
230                 $preamble = sprintf( t('%1$s tagged your post at %2$s') , $params['source_name'], $sitename);
231                 $epreamble = sprintf( t('%1$s tagged [url=%2$s]your post[/url]') ,
232                                                                 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
233                                                                 $itemlink);
234
235                 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
236                 $tsitelink = sprintf( $sitelink, $siteurl );
237                 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
238                 $itemlink =  $params['link'];
239         }
240
241         if($params['type'] == NOTIFY_INTRO) {
242                 $subject = sprintf( t('[Friendica:Notify] Introduction received'));
243                 $preamble = sprintf( t('You\'ve received an introduction from \'%1$s\' at %2$s'), $params['source_name'], $sitename);
244                 $epreamble = sprintf( t('You\'ve received [url=%1$s]an introduction[/url] from %2$s.'),
245                                                                 $itemlink,
246                                                                 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
247                 $body = sprintf( t('You may visit their profile at %s'),$params['source_link']);
248
249                 $sitelink = t('Please visit %s to approve or reject the introduction.');
250                 $tsitelink = sprintf( $sitelink, $siteurl );
251                 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
252                 $itemlink =  $params['link'];
253
254                 switch ($params['verb']) {
255                         case ACTIVITY_FRIEND:
256                                 // someone started to share with user (mostly OStatus)
257                                 $subject = sprintf( t('[Friendica:Notify] A new person is sharing with you'));
258                                 $preamble = sprintf( t('%1$s is sharing with you at %2$s'), $params['source_name'], $sitename);
259                                 $epreamble = sprintf( t('%1$s is sharing with you at %2$s'),
260                                                                                 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
261                                                                                 $sitename);
262                                 break;
263                         case ACTIVITY_FOLLOW:
264                                 // someone started to follow the user (mostly OStatus)
265                                 $subject = sprintf( t('[Friendica:Notify] You have a new follower'));
266                                 $preamble = sprintf( t('You have a new follower at %2$s : %1$s'), $params['source_name'], $sitename);
267                                 $epreamble = sprintf( t('You have a new follower at %2$s : %1$s'),
268                                                                                 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
269                                                                                 $sitename);
270                                 break;
271                         default:
272                                 // ACTIVITY_REQ_FRIEND is default activity for notifications
273                                 break;
274                 }
275
276
277         }
278
279         if($params['type'] == NOTIFY_SUGGEST) {
280                 $subject = sprintf( t('[Friendica:Notify] Friend suggestion received'));
281                 $preamble = sprintf( t('You\'ve received a friend suggestion from \'%1$s\' at %2$s'), $params['source_name'], $sitename);
282                 $epreamble = sprintf( t('You\'ve received [url=%1$s]a friend suggestion[/url] for %2$s from %3$s.'),
283                                                                         $itemlink,
284                                                                         '[url=' . $params['item']['url'] . ']' . $params['item']['name'] . '[/url]',
285                                                                         '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
286
287                 $body = t('Name:') . ' ' . $params['item']['name'] . "\n";
288                 $body .= t('Photo:') . ' ' . $params['item']['photo'] . "\n";
289                 $body .= sprintf( t('You may visit their profile at %s'),$params['item']['url']);
290
291                 $sitelink = t('Please visit %s to approve or reject the suggestion.');
292                 $tsitelink = sprintf( $sitelink, $siteurl );
293                 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
294                 $itemlink =  $params['link'];
295         }
296
297         if($params['type'] == NOTIFY_CONFIRM) {
298                 if ($params['verb'] == ACTIVITY_FRIEND ){ // mutual connection
299                         $subject = sprintf( t('[Friendica:Notify] Connection accepted'));
300                         $preamble = sprintf( t('\'%1$s\' has accepted your connection request at %2$s'), $params['source_name'], $sitename);
301                         $epreamble = sprintf( t('%2$s has accepted your [url=%1$s]connection request[/url].'),
302                                                                         $itemlink,
303                                                                         '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
304                         $body =  t('You are now mutual friends and may exchange status updates, photos, and email
305         without restriction.');
306
307                         $sitelink = t('Please visit %s  if you wish to make any changes to this relationship.');
308                         $tsitelink = sprintf( $sitelink, $siteurl );
309                         $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
310                         $itemlink =  $params['link'];
311                 } else { // ACTIVITY_FOLLOW
312                         $subject = sprintf( t('[Friendica:Notify] Connection accepted'));
313                         $preamble = sprintf( t('\'%1$s\' has accepted your connection request at %2$s'), $params['source_name'], $sitename);
314                         $epreamble = sprintf( t('%2$s has accepted your [url=%1$s]connection request[/url].'),
315                                                                         $itemlink,
316                                                                         '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
317                         $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']);
318                         $body .= "\n\n";
319                         $body .= sprintf(t('\'%1$s\' may choose to extend this into a two-way or more permissive relationship in the future. '), $params['source_name']);
320
321                         $sitelink = t('Please visit %s  if you wish to make any changes to this relationship.');
322                         $tsitelink = sprintf( $sitelink, $siteurl );
323                         $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
324                         $itemlink =  $params['link'];
325                 }
326
327         }
328
329         if($params['type'] == NOTIFY_SYSTEM) {
330                 switch($params['event']) {
331                         case "SYSTEM_REGISTER_REQUEST":
332                                 $subject = sprintf( t('[Friendica System:Notify] registration request'));
333                                 $preamble = sprintf( t('You\'ve received a registration request from \'%1$s\' at %2$s'), $params['source_name'], $sitename);
334                                 $epreamble = sprintf( t('You\'ve received a [url=%1$s]registration request[/url] from %2$s.'),
335                                                                                 $itemlink,
336                                                                                 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
337                                 $body = sprintf( t('Full Name:  %1$s\nSite Location:    %2$s\nLogin Name:       %3$s (%4$s)'),
338                                                                         $params['source_name'], $siteurl, $params['source_mail'], $params['source_nick']);
339
340                                 $sitelink = t('Please visit %s to approve or reject the request.');
341                                 $tsitelink = sprintf( $sitelink, $params['link'] );
342                                 $hsitelink = sprintf( $sitelink, '<a href="' . $params['link'] . '">' . $sitename . '</a><br><br>');
343                                 $itemlink =  $params['link'];
344                                 break;
345                         case "SYSTEM_DB_UPDATE_FAIL":
346                                 break;
347                 }
348         }
349
350         if ($params['type'] == "SYSTEM_EMAIL"){
351                 // not part of the notifications.
352                 // it just send a mail to the user.
353                 // It will be used by the system to send emails to users (like
354                 // password reset, invitations and so) using one look (but without
355                 // add a notification to the user, with could be inexistent)
356                         $subject = $params['subject'];
357                         $preamble = $params['preamble'];
358                         $body =  $params['body'];
359                         $sitelink = "";
360                         $tsitelink = "";
361                         $hsitelink = "";
362                         $itemlink =  "";
363                         $show_in_notification_page = false;
364         }
365
366         $subject .= " (".$nickname."@".$hostname.")";
367
368         $h = array(
369                 'params'    => $params,
370                 'subject'   => $subject,
371                 'preamble'  => $preamble,
372                 'epreamble' => $epreamble,
373                 'body'      => $body,
374                 'sitelink'  => $sitelink,
375                 'tsitelink' => $tsitelink,
376                 'hsitelink' => $hsitelink,
377                 'itemlink'  => $itemlink
378         );
379
380         call_hooks('enotify',$h);
381
382         $subject   = $h['subject'];
383         $preamble  = $h['preamble'];
384         $epreamble = $h['epreamble'];
385         $body      = $h['body'];
386         $sitelink  = $h['sitelink'];
387         $tsitelink = $h['tsitelink'];
388         $hsitelink = $h['hsitelink'];
389         $itemlink  = $h['itemlink'];
390
391
392
393         if ($show_in_notification_page) {
394                 logger("adding notification entry", LOGGER_DEBUG);
395                 do {
396                         $dups = false;
397                         $hash = random_string();
398                         $r = q("SELECT `id` FROM `notify` WHERE `hash` = '%s' LIMIT 1",
399                                 dbesc($hash));
400                         if(count($r))
401                                 $dups = true;
402                 } while($dups == true);
403
404
405                 $datarray = array();
406                 $datarray['hash']  = $hash;
407                 $datarray['name']  = $params['source_name'];
408                 $datarray['url']   = $params['source_link'];
409                 $datarray['photo'] = $params['source_photo'];
410                 $datarray['date']  = datetime_convert();
411                 $datarray['uid']   = $params['uid'];
412                 $datarray['link']  = $itemlink;
413                 $datarray['iid']   = $item_id;
414                 $datarray['parent'] = $parent_id;
415                 $datarray['type']  = $params['type'];
416                 $datarray['verb']  = $params['verb'];
417                 $datarray['otype'] = $params['otype'];
418                 $datarray['abort'] = false;
419
420                 call_hooks('enotify_store', $datarray);
421
422                 if($datarray['abort']) {
423                         pop_lang();
424                         return False;
425                 }
426
427                 // create notification entry in DB
428
429                 $r = q("insert into notify (hash,name,url,photo,date,uid,link,iid,parent,type,verb,otype)
430                         values('%s','%s','%s','%s','%s',%d,'%s',%d,%d,%d,'%s','%s')",
431                         dbesc($datarray['hash']),
432                         dbesc($datarray['name']),
433                         dbesc($datarray['url']),
434                         dbesc($datarray['photo']),
435                         dbesc($datarray['date']),
436                         intval($datarray['uid']),
437                         dbesc($datarray['link']),
438                         intval($datarray['iid']),
439                         intval($datarray['parent']),
440                         intval($datarray['type']),
441                         dbesc($datarray['verb']),
442                         dbesc($datarray['otype'])
443                 );
444
445                 $r = q("select id from notify where hash = '%s' and uid = %d limit 1",
446                         dbesc($hash),
447                         intval($params['uid'])
448                 );
449                 if($r)
450                         $notify_id = $r[0]['id'];
451                 else {
452                         pop_lang();
453                         return False;
454                 }
455
456                 // we seem to have a lot of duplicate comment notifications due to race conditions, mostly from forums
457                 // After we've stored everything, look again to see if there are any duplicates and if so remove them
458
459                 $p = null;
460                 $p = q("select id from notify where ( type = %d or type = %d ) and link = '%s' and uid = %d order by id",
461                         intval(NOTIFY_TAGSELF),
462                         intval(NOTIFY_COMMENT),
463                         dbesc($params['link']),
464                         intval($params['uid'])
465                 );
466                 if($p && (count($p) > 1)) {
467                         for ($d = 1; $d < count($p); $d ++) {
468                                 q("delete from notify where id = %d",
469                                         intval($p[$d]['id'])
470                                 );
471                         }
472
473                         // only continue on if we stored the first one
474
475                         if($notify_id != $p[0]['id']) {
476                                 pop_lang();
477                                 return False;
478                         }
479                 }
480
481
482                 $itemlink = $a->get_baseurl() . '/notify/view/' . $notify_id;
483                 $msg = replace_macros($epreamble,array('$itemlink' => $itemlink));
484                 $r = q("update notify set msg = '%s' where id = %d and uid = %d",
485                         dbesc($msg),
486                         intval($notify_id),
487                         intval($params['uid'])
488                 );
489
490         }
491
492         // send email notification if notification preferences permit
493         if((intval($params['notify_flags']) & intval($params['type']))
494                 || $params['type'] == NOTIFY_SYSTEM
495                 || $params['type'] == "SYSTEM_EMAIL") {
496
497                 logger('sending notification email');
498
499                 if (isset($params['parent']) AND (intval($params['parent']) != 0)) {
500                         $id_for_parent = $params['parent']."@".$hostname;
501
502                         // Is this the first email notification for this parent item and user?
503
504                         $r = q("select `id` from `notify-threads` where `master-parent-item` = %d and `receiver-uid` = %d limit 1",
505                                 intval($params['parent']),
506                                 intval($params['uid']) );
507
508                         // If so, create the record of it and use a message-id smtp header.
509
510                         if(!$r) {
511                                 logger("notify_id:".intval($notify_id).", parent: ".intval($params['parent'])."uid: ".intval($params['uid']), LOGGER_DEBUG);
512                                 $r = q("insert into `notify-threads` (`notify-id`, `master-parent-item`, `receiver-uid`, `parent-item`)
513                                         values(%d,%d,%d,%d)",
514                                         intval($notify_id),
515                                         intval($params['parent']),
516                                         intval($params['uid']),
517                                         0 );
518
519                                 $additional_mail_header .= "Message-ID: <${id_for_parent}>\n";
520                                 $log_msg = "include/enotify: No previous notification found for this parent:\n" .
521                                                 "  parent: ${params['parent']}\n" . "  uid   : ${params['uid']}\n";
522                                 logger($log_msg, LOGGER_DEBUG);
523                         } else {
524                                 // If not, just "follow" the thread.
525                                 $additional_mail_header .= "References: <${id_for_parent}>\nIn-Reply-To: <${id_for_parent}>\n";
526                                 logger("There's already a notification for this parent:\n" . print_r($r, true), LOGGER_DEBUG);
527                         }
528                 }
529
530
531                 // textversion keeps linebreaks
532                 $textversion = strip_tags(str_replace("<br>","\n",html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",
533                         $body))),ENT_QUOTES,'UTF-8')));
534                 $htmlversion = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"),
535                         "<br />\n",$body))),ENT_QUOTES,'UTF-8');
536
537
538                 $datarray = array();
539                 $datarray['banner'] = $banner;
540                 $datarray['product'] = $product;
541                 $datarray['preamble'] = $preamble;
542                 $datarray['sitename'] = $sitename;
543                 $datarray['siteurl'] = $siteurl;
544                 $datarray['type'] = $params['type'];
545                 $datarray['parent'] = $params['parent'];
546                 $datarray['source_name'] = $params['source_name'];
547                 $datarray['source_link'] = $params['source_link'];
548                 $datarray['source_photo'] = $params['source_photo'];
549                 $datarray['uid'] = $params['uid'];
550                 $datarray['username'] = $params['to_name'];
551                 $datarray['hsitelink'] = $hsitelink;
552                 $datarray['tsitelink'] = $tsitelink;
553                 $datarray['hitemlink'] = '<a href="' . $itemlink . '">' . $itemlink . '</a>';
554                 $datarray['titemlink'] = $itemlink;
555                 $datarray['thanks'] = $thanks;
556                 $datarray['site_admin'] = $site_admin;
557                 $datarray['title'] = stripslashes($title);
558                 $datarray['htmlversion'] = $htmlversion;
559                 $datarray['textversion'] = $textversion;
560                 $datarray['subject'] = $subject;
561                 $datarray['headers'] = $additional_mail_header;
562
563                 call_hooks('enotify_mail', $datarray);
564
565                 // check whether sending post content in email notifications is allowed
566                 // always true for "SYSTEM_EMAIL"
567                 $content_allowed = ((!get_config('system','enotify_no_content')) || ($params['type'] == "SYSTEM_EMAIL"));
568
569                 // load the template for private message notifications
570                 $tpl = get_markup_template('email_notify_html.tpl');
571                 $email_html_body = replace_macros($tpl,array(
572                         '$banner'       => $datarray['banner'],
573                         '$product'      => $datarray['product'],
574                         '$preamble'     => str_replace("\n","<br>\n",$datarray['preamble']),
575                         '$sitename'     => $datarray['sitename'],
576                         '$siteurl'      => $datarray['siteurl'],
577                         '$source_name'  => $datarray['source_name'],
578                         '$source_link'  => $datarray['source_link'],
579                         '$source_photo' => $datarray['source_photo'],
580                         '$username'     => $datarray['to_name'],
581                         '$hsitelink'    => $datarray['hsitelink'],
582                         '$hitemlink'    => $datarray['hitemlink'],
583                         '$thanks'       => $datarray['thanks'],
584                         '$site_admin'   => $datarray['site_admin'],
585                         '$title'                => $datarray['title'],
586                         '$htmlversion'  => $datarray['htmlversion'],
587                         '$content_allowed'      => $content_allowed,
588                 ));
589
590                 // load the template for private message notifications
591                 $tpl = get_markup_template('email_notify_text.tpl');
592                 $email_text_body = replace_macros($tpl,array(
593                         '$banner'       => $datarray['banner'],
594                         '$product'      => $datarray['product'],
595                         '$preamble'     => $datarray['preamble'],
596                         '$sitename'     => $datarray['sitename'],
597                         '$siteurl'      => $datarray['siteurl'],
598                         '$source_name'  => $datarray['source_name'],
599                         '$source_link'  => $datarray['source_link'],
600                         '$source_photo' => $datarray['source_photo'],
601                         '$username'     => $datarray['to_name'],
602                         '$tsitelink'    => $datarray['tsitelink'],
603                         '$titemlink'    => $datarray['titemlink'],
604                         '$thanks'       => $datarray['thanks'],
605                         '$site_admin'   => $datarray['site_admin'],
606                         '$title'                => $datarray['title'],
607                         '$textversion'  => $datarray['textversion'],
608                         '$content_allowed'      => $content_allowed,
609                 ));
610
611 //              logger('text: ' . $email_text_body);
612
613                 // use the Emailer class to send the message
614
615                 return Emailer::send(array(
616                         'uid' => $params['uid'],
617                         'fromName' => $sender_name,
618                         'fromEmail' => $sender_email,
619                         'replyTo' => $sender_email,
620                         'toEmail' => $params['to_email'],
621                         'messageSubject' => $datarray['subject'],
622                         'htmlVersion' => $email_html_body,
623                         'textVersion' => $email_text_body,
624                         'additionalMailHeader' => $datarray['headers'],
625                 ));
626         }
627
628     return False;
629
630 }
631
632 ?>