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