]> git.mxchange.org Git - friendica.git/blob - include/enotify.php
bug #439 - remove web port number from notification email "from" address
[friendica.git] / include / enotify.php
1 <?php
2
3 function notification($params) {
4
5         logger('notification: entry', LOGGER_DEBUG);
6
7         $a = get_app();
8
9         // from here on everything is in the recipients language
10
11         push_lang($params['language']);
12
13
14         $banner = t('Friendica Notification');
15         $product = FRIENDICA_PLATFORM;
16         $siteurl = $a->get_baseurl(true);
17         $thanks = t('Thank You,');
18         $sitename = get_config('config','sitename');
19         $site_admin = sprintf( t('%s Administrator'), $sitename);
20
21         $sender_name = $product;
22         $hostname = $a->get_hostname();
23         if(strpos($hostname,':'))
24                 $hostname = substr($hostname,0,strpos($hostname,':'));
25
26         $sender_email = t('noreply') . '@' . $hostname;
27         $additional_mail_header = "";
28
29         if(array_key_exists('item',$params)) {
30                 $title = $params['item']['title'];
31                 $body = $params['item']['body'];
32         }
33         else {
34                 $title = $body = '';
35         }
36
37         // e.g. "your post", "David's photo", etc.
38         $possess_desc = t('%s <!item_type!>');
39
40         if($params['type'] == NOTIFY_MAIL) {
41
42                 $subject =      sprintf( t('[Friendica:Notify] New mail received at %s'),$sitename);
43
44                 $preamble = sprintf( t('%s sent you a new private message at %s.'),$params['source_name'],$sitename);
45                 $epreamble = sprintf( t('%s sent you %s.'),'[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=$itemlink]' . t('a private message') . '[/url]');
46                 $sitelink = t('Please visit %s to view and/or reply to your private messages.');
47                 $tsitelink = sprintf( $sitelink, $siteurl . '/message/' . $params['item']['id'] );
48                 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '/message/' . $params['item']['id'] . '">' . $sitename . '</a>');
49                 $itemlink = $siteurl . '/message/' . $params['item']['id'];
50         }
51
52         if($params['type'] == NOTIFY_COMMENT) {
53 //              logger("notification: params = " . print_r($params, true), LOGGER_DEBUG);
54
55                 $parent_id = $params['parent'];
56
57
58                 // if it's a post figure out who's post it is.
59
60                 $p = null;
61
62                 if($params['otype'] === 'item' && $parent_id) {
63                         $p = q("select * from item where id = %d and uid = %d limit 1",
64                                 intval($parent_id),
65                                 intval($params['uid'])
66                         );
67                 }
68
69                 $possess_desc = str_replace('<!item_type!>',item_post_type($p[0]),$possess_desc);
70
71                 // "a post"
72                 $dest_str = sprintf($possess_desc,'a');
73
74                 // "George Bull's post"
75                 if($p)
76                         $dest_str = sprintf($possess_desc,sprintf( t("%s's"),$p[0]['author-name']));
77                 
78                 // "your post"
79                 if($p[0]['owner-name'] == $p[0]['author-name'] && $p[0]['wall'])
80                         $dest_str = sprintf($possess_desc, t('your') );
81
82                 // Some mail softwares relies on subject field for threading.
83                 // So, we cannot have different subjects for notifications of the same thread.
84                 // Before this we have the name of the replier on the subject rendering 
85                 // differents subjects for messages on the same thread.
86
87                 $subject = sprintf( t('[Friendica:Notify] Comment to conversation #%d by %s'), $parent_id, $params['source_name']);
88                 $preamble = sprintf( t('%s commented on an item/conversation you have been following.'), $params['source_name']); 
89                 $epreamble = sprintf( t('%s commented on %s.'), '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=$itemlink]' . $dest_str . '[/url]'); 
90
91                 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
92                 $tsitelink = sprintf( $sitelink, $siteurl );
93                 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
94                 $itemlink =  $params['link'];
95         }
96
97         if($params['type'] == NOTIFY_WALL) {
98                 $subject = sprintf( t('[Friendica:Notify] %s posted to your profile wall') , $params['source_name']);
99
100                 $preamble = sprintf( t('%s posted to your profile wall at %s') , $params['source_name'], $sitename);
101                 
102                 $epreamble = sprintf( t('%s posted to %s') , '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=$itemlink]' . t('your profile wall.') . '[/url]'); 
103                 
104                 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
105                 $tsitelink = sprintf( $sitelink, $siteurl );
106                 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
107                 $itemlink =  $params['link'];
108         }
109
110         if($params['type'] == NOTIFY_TAGSELF) {
111                 $subject =      sprintf( t('[Friendica:Notify] %s tagged you') , $params['source_name']);
112                 $preamble = sprintf( t('%s tagged you at %s') , $params['source_name'], $sitename);
113                 $epreamble = sprintf( t('%s %s.') , '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=' . $params['link'] . ']' . t('tagged you') . '[/url]'); 
114
115                 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
116                 $tsitelink = sprintf( $sitelink, $siteurl );
117                 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
118                 $itemlink =  $params['link'];
119         }
120
121         if($params['type'] == NOTIFY_TAGSHARE) {
122                 $subject =      sprintf( t('[Friendica:Notify] %s tagged your post') , $params['source_name']);
123                 $preamble = sprintf( t('%s tagged your post at %s') , $params['source_name'], $sitename);
124                 $epreamble = sprintf( t('%s tagged %s') , '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=$itemlink]' . t('your post') . '[/url]' ); 
125
126                 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
127                 $tsitelink = sprintf( $sitelink, $siteurl );
128                 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
129                 $itemlink =  $params['link'];
130         }
131
132         if($params['type'] == NOTIFY_INTRO) {
133                 $subject = sprintf( t('[Friendica:Notify] Introduction received'));
134                 $preamble = sprintf( t('You\'ve received an introduction from \'%s\' at %s'), $params['source_name'], $sitename); 
135                 $epreamble = sprintf( t('You\'ve received %s from %s.'), '[url=$itemlink]' . t('an introduction') . '[/url]' , '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]'); 
136                 $body = sprintf( t('You may visit their profile at %s'),$params['source_link']);
137
138                 $sitelink = t('Please visit %s to approve or reject the introduction.');
139                 $tsitelink = sprintf( $sitelink, $siteurl );
140                 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
141                 $itemlink =  $params['link'];
142         }
143
144         if($params['type'] == NOTIFY_SUGGEST) {
145                 $subject = sprintf( t('[Friendica:Notify] Friend suggestion received'));
146                 $preamble = sprintf( t('You\'ve received a friend suggestion from \'%s\' at %s'), $params['source_name'], $sitename); 
147                 $epreamble = sprintf( t('You\'ve received %s for %s from %s.'),
148                         '[url=$itemlink]' . t('a friend suggestion') . '[/url]',
149                         '[url=' . $params['item']['url'] . ']' . $params['item']['name'] . '[/url]', 
150                         '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]'); 
151                 $body = t('Name:') . ' ' . $params['item']['name'] . "\n";
152                 $body .= t('Photo:') . ' ' . $params['item']['photo'] . "\n";
153                 $body .= sprintf( t('You may visit their profile at %s'),$params['item']['url']);
154
155                 $sitelink = t('Please visit %s to approve or reject the suggestion.');
156                 $tsitelink = sprintf( $sitelink, $siteurl );
157                 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
158                 $itemlink =  $params['link'];
159         }
160
161         if($params['type'] == NOTIFY_CONFIRM) {
162
163         }
164
165         if($params['type'] == NOTIFY_SYSTEM) {
166                 
167         }
168
169         $h = array(
170                 'params'    => $params, 
171                 'subject'   => $subject,
172                 'preamble'  => $preamble, 
173                 'epreamble' => $epreamble, 
174                 'body'      => $body, 
175                 'sitelink'  => $sitelink,
176                 'tsitelink' => $tsitelink,
177                 'hsitelink' => $hsitelink,
178                 'itemlink'  => $itemlink
179         );
180                 
181         call_hooks('enotify',$h);
182
183         $subject   = $h['subject'];
184         $preamble  = $h['preamble'];
185         $epreamble = $h['epreamble'];
186         $body      = $h['body'];
187         $sitelink  = $h['sitelink'];
188         $tsitelink = $h['tsitelink'];
189         $hsitelink = $h['hsitelink'];
190         $itemlink  = $h['itemlink']; 
191
192
193         require_once('include/html2bbcode.php');        
194
195         do {
196                 $dups = false;
197                 $hash = random_string();
198         $r = q("SELECT `id` FROM `notify` WHERE `hash` = '%s' LIMIT 1",
199                         dbesc($hash));
200                 if(count($r))
201                         $dups = true;
202         } while($dups == true);
203
204
205         $datarray = array();
206         $datarray['hash']  = $hash;
207         $datarray['name']  = $params['source_name'];
208         $datarray['url']   = $params['source_link'];
209         $datarray['photo'] = $params['source_photo'];
210         $datarray['date']  = datetime_convert();
211         $datarray['uid']   = $params['uid'];
212         $datarray['link']  = $itemlink;
213         $datarray['parent'] = $parent_id;
214         $datarray['type']  = $params['type'];
215         $datarray['verb']  = $params['verb'];
216         $datarray['otype'] = $params['otype'];
217  
218         call_hooks('enotify_store', $datarray);
219
220         // create notification entry in DB
221
222         $r = q("insert into notify (hash,name,url,photo,date,uid,link,parent,type,verb,otype)
223                 values('%s','%s','%s','%s','%s',%d,'%s',%d,%d,'%s','%s')",
224                 dbesc($datarray['hash']),
225                 dbesc($datarray['name']),
226                 dbesc($datarray['url']),
227                 dbesc($datarray['photo']),
228                 dbesc($datarray['date']),
229                 intval($datarray['uid']),
230                 dbesc($datarray['link']),
231                 intval($datarray['parent']),
232                 intval($datarray['type']),
233                 dbesc($datarray['verb']),
234                 dbesc($datarray['otype'])
235         );
236
237         $r = q("select id from notify where hash = '%s' and uid = %d limit 1",
238                 dbesc($hash),
239                 intval($params['uid'])
240         );
241         if($r)
242                 $notify_id = $r[0]['id'];
243         else {
244                 pop_lang();
245                 return;
246         }
247
248         $itemlink = $a->get_baseurl() . '/notify/view/' . $notify_id;
249         $msg = replace_macros($epreamble,array('$itemlink' => $itemlink));
250         $r = q("update notify set msg = '%s' where id = %d and uid = %d limit 1",
251                 dbesc($msg),
252                 intval($notify_id),
253                 intval($params['uid'])
254         );
255                 
256
257         // send email notification if notification preferences permit
258
259         require_once('bbcode.php');
260         if((intval($params['notify_flags']) & intval($params['type'])) || $params['type'] == NOTIFY_SYSTEM) {
261
262                 logger('notification: sending notification email');
263
264                 $id_for_parent = "${params['parent']}@${hostname}";
265
266                 // Is this the first email notification for this parent item and user?
267                 
268                 $r = q("select `id` from `notify-threads` where `master-parent-item` = %d and `receiver-uid` = %d limit 1", 
269                         intval($params['parent']),
270                         intval($params['uid']) );
271
272                 // If so, create the record of it and use a message-id smtp header.
273
274                 if(!$r) {
275                         logger("norify_id:" . intval($notify_id). ", parent: " . intval($params['parent']) . "uid: " . 
276 intval($params['uid']), LOGGER_DEBUG);
277                         $r = q("insert into `notify-threads` (`notify-id`, `master-parent-item`, `receiver-uid`, `parent-item`)
278                                 values(%d,%d,%d,%d)",
279                                 intval($notify_id),
280                                 intval($params['parent']),
281                                 intval($params['uid']), 
282                                 0 );
283
284                         $additional_mail_header .= "Message-ID: <${id_for_parent}>\n";
285                         $log_msg = "include/enotify: No previous notification found for this parent:\n" . 
286                                         "  parent: ${params['parent']}\n" . "  uid   : ${params['uid']}\n";
287                         logger($log_msg, LOGGER_DEBUG);
288                 }
289
290                 // If not, just "follow" the thread.
291
292                 else {
293                         $additional_mail_header = "References: <${id_for_parent}>\nIn-Reply-To: <${id_for_parent}>\n";
294                         logger("include/enotify: There's already a notification for this parent:\n" . print_r($r, true), LOGGER_DEBUG);
295                 }
296
297
298
299                 $textversion = strip_tags(html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",
300                         $body))),ENT_QUOTES,'UTF-8'));
301                 $htmlversion = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"), 
302                         "<br />\n",$body))));
303
304                 $datarray = array();
305                 $datarray['banner'] = $banner;
306                 $datarray['product'] = $product;
307                 $datarray['preamble'] = $preamble;
308                 $datarray['sitename'] = $sitename;
309                 $datarray['siteurl'] = $siteurl;
310                 $datarray['type'] = $params['type'];
311                 $datarray['parent'] = $params['parent'];
312                 $datarray['source_name'] = $params['source_name'];
313                 $datarray['source_link'] = $params['source_link'];
314                 $datarray['source_photo'] = $params['source_photo'];
315                 $datarray['uid'] = $params['uid'];
316                 $datarray['username'] = $params['to_name'];
317                 $datarray['hsitelink'] = $hsitelink;
318                 $datarray['tsitelink'] = $tsitelink;
319                 $datarray['hitemlink'] = '<a href="' . $itemlink . '">' . $itemlink . '</a>';
320                 $datarray['titemlink'] = $itemlink;
321                 $datarray['thanks'] = $thanks;
322                 $datarray['site_admin'] = $site_admin;
323                 $datarray['title'] = stripslashes($title);
324                 $datarray['htmlversion'] = $htmlversion;
325                 $datarray['textversion'] = $textversion;
326                 $datarray['subject'] = $subject;
327                 $datarray['headers'] = $additional_mail_header;
328
329                 call_hooks('enotify_mail', $datarray);
330
331                 // load the template for private message notifications
332                 $tpl = get_markup_template('email_notify_html.tpl');
333                 $email_html_body = replace_macros($tpl,array(
334                         '$banner'       => $datarray['banner'],
335                         '$product'      => $datarray['product'],
336                         '$preamble'     => $datarray['preamble'],
337                         '$sitename'     => $datarray['sitename'],
338                         '$siteurl'      => $datarray['siteurl'],
339                         '$source_name'  => $datarray['source_name'],
340                         '$source_link'  => $datarray['source_link'],
341                         '$source_photo' => $datarray['source_photo'],
342                         '$username'     => $datarray['to_name'],
343                         '$hsitelink'    => $datarray['hsitelink'],
344                         '$hitemlink'    => $datarray['hitemlink'],
345                         '$thanks'       => $datarray['thanks'],
346                         '$site_admin'   => $datarray['site_admin'],
347                         '$title'                => $datarray['title'],
348                         '$htmlversion'  => $datarray['htmlversion'],    
349                 ));
350                 
351                 // load the template for private message notifications
352                 $tpl = get_markup_template('email_notify_text.tpl');
353                 $email_text_body = replace_macros($tpl,array(
354                         '$banner'       => $datarray['banner'],
355                         '$product'      => $datarray['product'],
356                         '$preamble'     => $datarray['preamble'],
357                         '$sitename'     => $datarray['sitename'],
358                         '$siteurl'      => $datarray['siteurl'],
359                         '$source_name'  => $datarray['source_name'],
360                         '$source_link'  => $datarray['source_link'],
361                         '$source_photo' => $datarray['source_photo'],
362                         '$username'     => $datarray['to_name'],
363                         '$tsitelink'    => $datarray['tsitelink'],
364                         '$titemlink'    => $datarray['titemlink'],
365                         '$thanks'       => $datarray['thanks'],
366                         '$site_admin'   => $datarray['site_admin'],
367                         '$title'                => $datarray['title'],
368                         '$textversion'  => $datarray['textversion'],    
369                 ));
370
371 //              logger('text: ' . $email_text_body);
372
373                 // use the EmailNotification library to send the message
374
375                 enotify::send(array(
376                         'fromName' => $sender_name,
377                         'fromEmail' => $sender_email,
378                         'replyTo' => $sender_email,
379                         'toEmail' => $params['to_email'],
380                         'messageSubject' => $datarray['subject'],
381                         'htmlVersion' => $email_html_body,
382                         'textVersion' => $email_text_body,
383                         'additionalMailHeader' => $datarray['headers'],
384                 ));
385         }
386
387         pop_lang();
388
389 }
390
391 require_once('include/email.php');
392
393 class enotify {
394         /**
395          * Send a multipart/alternative message with Text and HTML versions
396          *
397          * @param fromName                      name of the sender
398          * @param fromEmail                     email fo the sender
399          * @param replyTo                       replyTo address to direct responses
400          * @param toEmail                       destination email address
401          * @param messageSubject        subject of the message
402          * @param htmlVersion           html version of the message
403          * @param textVersion           text only version of the message
404          * @param additionalMailHeader  additions to the smtp mail header
405          */
406         static public function send($params) {
407
408                 $fromName = email_header_encode(html_entity_decode($params['fromName'],ENT_QUOTES,'UTF-8'),'UTF-8'); 
409                 $messageSubject = email_header_encode(html_entity_decode($params['messageSubject'],ENT_QUOTES,'UTF-8'),'UTF-8');
410                 
411                 // generate a mime boundary
412                 $mimeBoundary   =rand(0,9)."-"
413                                 .rand(10000000000,9999999999)."-"
414                                 .rand(10000000000,9999999999)."=:"
415                                 .rand(10000,99999);
416
417                 // generate a multipart/alternative message header
418                 $messageHeader =
419                         $params['additionalMailHeader'] .
420                         "From: {$params['fromName']} <{$params['fromEmail']}>\n" . 
421                         "Reply-To: {$params['fromName']} <{$params['replyTo']}>\n" .
422                         "MIME-Version: 1.0\n" .
423                         "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
424
425                 // assemble the final multipart message body with the text and html types included
426                 $textBody       =       chunk_split(base64_encode($params['textVersion']));
427                 $htmlBody       =       chunk_split(base64_encode($params['htmlVersion']));
428                 $multipartMessageBody =
429                         "--" . $mimeBoundary . "\n" .                                   // plain text section
430                         "Content-Type: text/plain; charset=UTF-8\n" .
431                         "Content-Transfer-Encoding: base64\n\n" .
432                         $textBody . "\n" .
433                         "--" . $mimeBoundary . "\n" .                                   // text/html section
434                         "Content-Type: text/html; charset=UTF-8\n" .
435                         "Content-Transfer-Encoding: base64\n\n" .
436                         $htmlBody . "\n" .
437                         "--" . $mimeBoundary . "--\n";                                  // message ending
438
439                 // send the message
440                 $res = mail(
441                         $params['toEmail'],                                                                             // send to address
442                         $params['messageSubject'],                                                              // subject
443                         $multipartMessageBody,                                                  // message body
444                         $messageHeader                                                                  // message headers
445                 );
446                 logger("notification: enotify::send returns " . $res, LOGGER_DEBUG);
447         }
448 }
449 ?>