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