]> git.mxchange.org Git - friendica.git/blob - include/enotify.php
cleanup multipart email sending code
[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
19         if(array_key_exists('item',$params)) {
20                 $title = $params['item']['title'];
21                 $body = $params['item']['body'];
22         }
23         else {
24                 $title = $body = '';
25         }
26
27         // e.g. "your post", "David's photo", etc.
28         $possess_desc = t('%s <!item_type!>');
29
30         if($params['type'] == NOTIFY_MAIL) {
31
32                 $subject =      sprintf( t('[Friendica:Notify] New mail received at %s'),$sitename);
33
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'];
40         }
41
42         if($params['type'] == NOTIFY_COMMENT) {
43 //              logger("notification: params = " . print_r($params, true), LOGGER_DEBUG);
44
45                 $parent_id = $params['parent'];
46
47                 $p = q("SELECT `ignored` FROM `thread` WHERE `iid` = %d AND `uid` = %d LIMIT 1",
48                         intval($parent_id),
49                         intval($params['uid'])
50                 );
51                 if ($p AND count($p) AND ($p[0]["ignored"])) {
52                         logger("Thread ".$parent_id." will be ignored", LOGGER_DEBUG);
53                         return;
54                 }
55
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
58
59                 $p = null;
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),
63                         intval(NOTIFY_SHARE),
64                         dbesc($params['link']),
65                         intval($params['uid'])
66                 );
67                 if($p and count($p)) {
68                         pop_lang();
69                         return;
70                 }
71
72
73                 // if it's a post figure out who's post it is.
74
75                 $p = null;
76
77                 if($params['otype'] === 'item' && $parent_id) {
78                         $p = q("select * from item where id = %d and uid = %d limit 1",
79                                 intval($parent_id),
80                                 intval($params['uid'])
81                         );
82                 }
83
84                 $item_post_type = item_post_type($p[0]);
85                 //$possess_desc = str_replace('<!item_type!>',$possess_desc);
86
87                 // "a post"
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]',
90                                                                 $itemlink,
91                                                                 $item_post_type);
92
93                 // "George Bull's post"
94                 if($p)
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]',
97                                                 $itemlink,
98                                                 $p[0]['author-name'],
99                                                 $item_post_type);
100
101                 // "your post"
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]',
105                                                                 $itemlink,
106                                                                 $item_post_type);
107
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.
112
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;
116
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'];
121         }
122
123         if($params['type'] == NOTIFY_WALL) {
124                 $subject = sprintf( t('[Friendica:Notify] %s posted to your profile wall') , $params['source_name']);
125
126                 $preamble = sprintf( t('%1$s posted to your profile wall at %2$s') , $params['source_name'], $sitename);
127
128                 $epreamble = sprintf( t('%1$s posted to [url=%2$s]your wall[/url]') ,
129                                                                 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
130                                                                 $params['link']);
131
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'];
136         }
137
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]',
143                                                                 $params['link']);
144
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'];
149         }
150
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]',
156                                                                 $params['link']);
157
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'];
162         }
163
164         if($params['type'] == NOTIFY_POKE) {
165
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]',
170                                                                 $params['link']);
171
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);
175
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'];
180         }
181
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]',
187                                                                 $itemlink);
188
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'];
193         }
194
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.'),
199                                                                 $itemlink,
200                                                                 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
201                 $body = sprintf( t('You may visit their profile at %s'),$params['source_link']);
202
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'];
207         }
208
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.'),
213                                                                         $itemlink,
214                                                                         '[url=' . $params['item']['url'] . ']' . $params['item']['name'] . '[/url]',
215                                                                         '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
216
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']);
220
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'];
225         }
226
227         if($params['type'] == NOTIFY_CONFIRM) {
228         }
229
230         if($params['type'] == NOTIFY_SYSTEM) {
231
232         }
233
234
235         /*$email = prepare_notificaion_mail($params, $subject, $preamble, $body, $sitelink, $tsitelink, $hsitelink, $itemlink);
236         if ($email) Emailer::send($email);
237         pop_lang();*/
238
239
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);
246
247         $sender_name = $product;
248         $hostname = $a->get_hostname();
249         if(strpos($hostname,':'))
250                 $hostname = substr($hostname,0,strpos($hostname,':'));
251
252         $sender_email = t('noreply') . '@' . $hostname;
253
254
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";
262
263         $h = array(
264                 'params'    => $params,
265                 'subject'   => $subject,
266                 'preamble'  => $preamble,
267                 'epreamble' => $epreamble,
268                 'body'      => $body,
269                 'sitelink'  => $sitelink,
270                 'tsitelink' => $tsitelink,
271                 'hsitelink' => $hsitelink,
272                 'itemlink'  => $itemlink
273         );
274
275         call_hooks('enotify',$h);
276
277         $subject   = $h['subject'];
278         $preamble  = $h['preamble'];
279         $epreamble = $h['epreamble'];
280         $body      = $h['body'];
281         $sitelink  = $h['sitelink'];
282         $tsitelink = $h['tsitelink'];
283         $hsitelink = $h['hsitelink'];
284         $itemlink  = $h['itemlink'];
285
286
287
288         do {
289                 $dups = false;
290                 $hash = random_string();
291         $r = q("SELECT `id` FROM `notify` WHERE `hash` = '%s' LIMIT 1",
292                         dbesc($hash));
293                 if(count($r))
294                         $dups = true;
295         } while($dups == true);
296
297
298         $datarray = array();
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;
311
312         call_hooks('enotify_store', $datarray);
313
314         if($datarray['abort']) {
315                 pop_lang();
316                 return False;
317         }
318
319         // create notification entry in DB
320
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'])
334         );
335
336         $r = q("select id from notify where hash = '%s' and uid = %d limit 1",
337                 dbesc($hash),
338                 intval($params['uid'])
339         );
340         if($r)
341                 $notify_id = $r[0]['id'];
342         else {
343                 pop_lang();
344                 return False;
345         }
346
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
349
350         $p = null;
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'])
356         );
357         if($p && (count($p) > 1)) {
358                 for ($d = 1; $d < count($p); $d ++) {
359                         q("delete from notify where id = %d",
360                                 intval($p[$d]['id'])
361                         );
362                 }
363
364                 // only continue on if we stored the first one
365
366                 if($notify_id != $p[0]['id']) {
367                         pop_lang();
368                         return False;
369                 }
370         }
371
372
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",
376                 dbesc($msg),
377                 intval($notify_id),
378                 intval($params['uid'])
379         );
380
381
382         // send email notification if notification preferences permit
383
384         if((intval($params['notify_flags']) & intval($params['type'])) || $params['type'] == NOTIFY_SYSTEM) {
385
386                 logger('notification: sending notification email');
387
388                 if (isset($params['parent']) AND (intval($params['parent']) != 0)) {
389                         $id_for_parent = $params['parent']."@".$hostname;
390
391                         // Is this the first email notification for this parent item and user?
392
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']) );
396
397                         // If so, create the record of it and use a message-id smtp header.
398
399                         if(!$r) {
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)",
403                                         intval($notify_id),
404                                         intval($params['parent']),
405                                         intval($params['uid']),
406                                         0 );
407
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);
412                         } else {
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);
416                         }
417                 }
418
419
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');
424
425                 $datarray = array();
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;
449
450                 call_hooks('enotify_mail', $datarray);
451
452                 // check whether sending post content in email notifications is allowed
453                 $content_allowed = !get_config('system','enotify_no_content');
454
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,
474                 ));
475
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,
495                 ));
496
497 //              logger('text: ' . $email_text_body);
498
499                 // use the Emailer class to send the message
500
501                 Emailer::send(array(
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'],
510                 ));
511         return True;
512         }
513
514     return False;
515
516 }
517
518 ?>