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