]> git.mxchange.org Git - friendica.git/blob - include/enotify.php
add function to exclude embedded images when checking message length
[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=%2s]your wall[/url]') , 
128                                                                 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
129                                                                 $itemlink); 
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_TAGSHARE) {
151                 $subject =      sprintf( t('[Friendica:Notify] %s tagged your post') , $params['source_name']);
152                 $preamble = sprintf( t('%1$s tagged your post at %2$s') , $params['source_name'], $sitename);
153                 $epreamble = sprintf( t('%1$s tagged [url=%2$s]your post[/url]') ,
154                                                                 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]',
155                                                                 $itemlink); 
156
157                 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
158                 $tsitelink = sprintf( $sitelink, $siteurl );
159                 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
160                 $itemlink =  $params['link'];
161         }
162
163         if($params['type'] == NOTIFY_INTRO) {
164                 $subject = sprintf( t('[Friendica:Notify] Introduction received'));
165                 $preamble = sprintf( t('You\'ve received an introduction from \'%1$s\' at %2$s'), $params['source_name'], $sitename); 
166                 $epreamble = sprintf( t('You\'ve received [url=%1$s]an introduction[/url] from %2$s.'),
167                                                                 $itemlink,
168                                                                 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]'); 
169                 $body = sprintf( t('You may visit their profile at %s'),$params['source_link']);
170
171                 $sitelink = t('Please visit %s to approve or reject the introduction.');
172                 $tsitelink = sprintf( $sitelink, $siteurl );
173                 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
174                 $itemlink =  $params['link'];
175         }
176
177         if($params['type'] == NOTIFY_SUGGEST) {
178                 $subject = sprintf( t('[Friendica:Notify] Friend suggestion received'));
179                 $preamble = sprintf( t('You\'ve received a friend suggestion from \'%1$s\' at %2$s'), $params['source_name'], $sitename); 
180                 $epreamble = sprintf( t('You\'ve received [url=%1$s]a friend suggestion[/url] for %2$s from %3$s.'),
181                                                                         $itemlink,
182                                                                         '[url=' . $params['item']['url'] . ']' . $params['item']['name'] . '[/url]',
183                                                                         '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]'); 
184                                                                         
185                 $body = t('Name:') . ' ' . $params['item']['name'] . "\n";
186                 $body .= t('Photo:') . ' ' . $params['item']['photo'] . "\n";
187                 $body .= sprintf( t('You may visit their profile at %s'),$params['item']['url']);
188
189                 $sitelink = t('Please visit %s to approve or reject the suggestion.');
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_CONFIRM) {
196
197         }
198
199         if($params['type'] == NOTIFY_SYSTEM) {
200                 
201         }
202
203         $h = array(
204                 'params'    => $params, 
205                 'subject'   => $subject,
206                 'preamble'  => $preamble, 
207                 'epreamble' => $epreamble, 
208                 'body'      => $body, 
209                 'sitelink'  => $sitelink,
210                 'tsitelink' => $tsitelink,
211                 'hsitelink' => $hsitelink,
212                 'itemlink'  => $itemlink
213         );
214                 
215         call_hooks('enotify',$h);
216
217         $subject   = $h['subject'];
218         $preamble  = $h['preamble'];
219         $epreamble = $h['epreamble'];
220         $body      = $h['body'];
221         $sitelink  = $h['sitelink'];
222         $tsitelink = $h['tsitelink'];
223         $hsitelink = $h['hsitelink'];
224         $itemlink  = $h['itemlink']; 
225
226
227         require_once('include/html2bbcode.php');        
228
229         do {
230                 $dups = false;
231                 $hash = random_string();
232         $r = q("SELECT `id` FROM `notify` WHERE `hash` = '%s' LIMIT 1",
233                         dbesc($hash));
234                 if(count($r))
235                         $dups = true;
236         } while($dups == true);
237
238
239         $datarray = array();
240         $datarray['hash']  = $hash;
241         $datarray['name']  = $params['source_name'];
242         $datarray['url']   = $params['source_link'];
243         $datarray['photo'] = $params['source_photo'];
244         $datarray['date']  = datetime_convert();
245         $datarray['uid']   = $params['uid'];
246         $datarray['link']  = $itemlink;
247         $datarray['parent'] = $parent_id;
248         $datarray['type']  = $params['type'];
249         $datarray['verb']  = $params['verb'];
250         $datarray['otype'] = $params['otype'];
251  
252         call_hooks('enotify_store', $datarray);
253
254         // create notification entry in DB
255
256         $r = q("insert into notify (hash,name,url,photo,date,uid,link,parent,type,verb,otype)
257                 values('%s','%s','%s','%s','%s',%d,'%s',%d,%d,'%s','%s')",
258                 dbesc($datarray['hash']),
259                 dbesc($datarray['name']),
260                 dbesc($datarray['url']),
261                 dbesc($datarray['photo']),
262                 dbesc($datarray['date']),
263                 intval($datarray['uid']),
264                 dbesc($datarray['link']),
265                 intval($datarray['parent']),
266                 intval($datarray['type']),
267                 dbesc($datarray['verb']),
268                 dbesc($datarray['otype'])
269         );
270
271         $r = q("select id from notify where hash = '%s' and uid = %d limit 1",
272                 dbesc($hash),
273                 intval($params['uid'])
274         );
275         if($r)
276                 $notify_id = $r[0]['id'];
277         else {
278                 pop_lang();
279                 return;
280         }
281
282         $itemlink = $a->get_baseurl() . '/notify/view/' . $notify_id;
283         $msg = replace_macros($epreamble,array('$itemlink' => $itemlink));
284         $r = q("update notify set msg = '%s' where id = %d and uid = %d limit 1",
285                 dbesc($msg),
286                 intval($notify_id),
287                 intval($params['uid'])
288         );
289                 
290
291         // send email notification if notification preferences permit
292
293         require_once('bbcode.php');
294         if((intval($params['notify_flags']) & intval($params['type'])) || $params['type'] == NOTIFY_SYSTEM) {
295
296                 logger('notification: sending notification email');
297
298                 $id_for_parent = "${params['parent']}@${hostname}";
299
300                 // Is this the first email notification for this parent item and user?
301                 
302                 $r = q("select `id` from `notify-threads` where `master-parent-item` = %d and `receiver-uid` = %d limit 1", 
303                         intval($params['parent']),
304                         intval($params['uid']) );
305
306                 // If so, create the record of it and use a message-id smtp header.
307
308                 if(!$r) {
309                         logger("norify_id:" . intval($notify_id). ", parent: " . intval($params['parent']) . "uid: " . 
310 intval($params['uid']), LOGGER_DEBUG);
311                         $r = q("insert into `notify-threads` (`notify-id`, `master-parent-item`, `receiver-uid`, `parent-item`)
312                                 values(%d,%d,%d,%d)",
313                                 intval($notify_id),
314                                 intval($params['parent']),
315                                 intval($params['uid']), 
316                                 0 );
317
318                         $additional_mail_header .= "Message-ID: <${id_for_parent}>\n";
319                         $log_msg = "include/enotify: No previous notification found for this parent:\n" . 
320                                         "  parent: ${params['parent']}\n" . "  uid   : ${params['uid']}\n";
321                         logger($log_msg, LOGGER_DEBUG);
322                 }
323
324                 // If not, just "follow" the thread.
325
326                 else {
327                         $additional_mail_header = "References: <${id_for_parent}>\nIn-Reply-To: <${id_for_parent}>\n";
328                         logger("include/enotify: There's already a notification for this parent:\n" . print_r($r, true), LOGGER_DEBUG);
329                 }
330
331
332
333                 $textversion = strip_tags(html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",
334                         $body))),ENT_QUOTES,'UTF-8'));
335                 $htmlversion = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"), 
336                         "<br />\n",$body))));
337
338                 $datarray = array();
339                 $datarray['banner'] = $banner;
340                 $datarray['product'] = $product;
341                 $datarray['preamble'] = $preamble;
342                 $datarray['sitename'] = $sitename;
343                 $datarray['siteurl'] = $siteurl;
344                 $datarray['type'] = $params['type'];
345                 $datarray['parent'] = $params['parent'];
346                 $datarray['source_name'] = $params['source_name'];
347                 $datarray['source_link'] = $params['source_link'];
348                 $datarray['source_photo'] = $params['source_photo'];
349                 $datarray['uid'] = $params['uid'];
350                 $datarray['username'] = $params['to_name'];
351                 $datarray['hsitelink'] = $hsitelink;
352                 $datarray['tsitelink'] = $tsitelink;
353                 $datarray['hitemlink'] = '<a href="' . $itemlink . '">' . $itemlink . '</a>';
354                 $datarray['titemlink'] = $itemlink;
355                 $datarray['thanks'] = $thanks;
356                 $datarray['site_admin'] = $site_admin;
357                 $datarray['title'] = stripslashes($title);
358                 $datarray['htmlversion'] = $htmlversion;
359                 $datarray['textversion'] = $textversion;
360                 $datarray['subject'] = $subject;
361                 $datarray['headers'] = $additional_mail_header;
362
363                 call_hooks('enotify_mail', $datarray);
364
365                 // load the template for private message notifications
366                 $tpl = get_markup_template('email_notify_html.tpl');
367                 $email_html_body = replace_macros($tpl,array(
368                         '$banner'       => $datarray['banner'],
369                         '$product'      => $datarray['product'],
370                         '$preamble'     => $datarray['preamble'],
371                         '$sitename'     => $datarray['sitename'],
372                         '$siteurl'      => $datarray['siteurl'],
373                         '$source_name'  => $datarray['source_name'],
374                         '$source_link'  => $datarray['source_link'],
375                         '$source_photo' => $datarray['source_photo'],
376                         '$username'     => $datarray['to_name'],
377                         '$hsitelink'    => $datarray['hsitelink'],
378                         '$hitemlink'    => $datarray['hitemlink'],
379                         '$thanks'       => $datarray['thanks'],
380                         '$site_admin'   => $datarray['site_admin'],
381                         '$title'                => $datarray['title'],
382                         '$htmlversion'  => $datarray['htmlversion'],    
383                 ));
384                 
385                 // load the template for private message notifications
386                 $tpl = get_markup_template('email_notify_text.tpl');
387                 $email_text_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                         '$tsitelink'    => $datarray['tsitelink'],
398                         '$titemlink'    => $datarray['titemlink'],
399                         '$thanks'       => $datarray['thanks'],
400                         '$site_admin'   => $datarray['site_admin'],
401                         '$title'                => $datarray['title'],
402                         '$textversion'  => $datarray['textversion'],    
403                 ));
404
405 //              logger('text: ' . $email_text_body);
406
407                 // use the EmailNotification library to send the message
408
409                 enotify::send(array(
410                         'fromName' => $sender_name,
411                         'fromEmail' => $sender_email,
412                         'replyTo' => $sender_email,
413                         'toEmail' => $params['to_email'],
414                         'messageSubject' => $datarray['subject'],
415                         'htmlVersion' => $email_html_body,
416                         'textVersion' => $email_text_body,
417                         'additionalMailHeader' => $datarray['headers'],
418                 ));
419         }
420
421         pop_lang();
422
423 }
424
425 require_once('include/email.php');
426
427 class enotify {
428         /**
429          * Send a multipart/alternative message with Text and HTML versions
430          *
431          * @param fromName                      name of the sender
432          * @param fromEmail                     email fo the sender
433          * @param replyTo                       replyTo address to direct responses
434          * @param toEmail                       destination email address
435          * @param messageSubject        subject of the message
436          * @param htmlVersion           html version of the message
437          * @param textVersion           text only version of the message
438          * @param additionalMailHeader  additions to the smtp mail header
439          */
440         static public function send($params) {
441
442                 $fromName = email_header_encode(html_entity_decode($params['fromName'],ENT_QUOTES,'UTF-8'),'UTF-8'); 
443                 $messageSubject = email_header_encode(html_entity_decode($params['messageSubject'],ENT_QUOTES,'UTF-8'),'UTF-8');
444                 
445                 // generate a mime boundary
446                 $mimeBoundary   =rand(0,9)."-"
447                                 .rand(10000000000,9999999999)."-"
448                                 .rand(10000000000,9999999999)."=:"
449                                 .rand(10000,99999);
450
451                 // generate a multipart/alternative message header
452                 $messageHeader =
453                         $params['additionalMailHeader'] .
454                         "From: {$params['fromName']} <{$params['fromEmail']}>\n" . 
455                         "Reply-To: {$params['fromName']} <{$params['replyTo']}>\n" .
456                         "MIME-Version: 1.0\n" .
457                         "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
458
459                 // assemble the final multipart message body with the text and html types included
460                 $textBody       =       chunk_split(base64_encode($params['textVersion']));
461                 $htmlBody       =       chunk_split(base64_encode($params['htmlVersion']));
462                 $multipartMessageBody =
463                         "--" . $mimeBoundary . "\n" .                                   // plain text section
464                         "Content-Type: text/plain; charset=UTF-8\n" .
465                         "Content-Transfer-Encoding: base64\n\n" .
466                         $textBody . "\n" .
467                         "--" . $mimeBoundary . "\n" .                                   // text/html section
468                         "Content-Type: text/html; charset=UTF-8\n" .
469                         "Content-Transfer-Encoding: base64\n\n" .
470                         $htmlBody . "\n" .
471                         "--" . $mimeBoundary . "--\n";                                  // message ending
472
473                 // send the message
474                 $res = mail(
475                         $params['toEmail'],                                                                             // send to address
476                         $params['messageSubject'],                                                              // subject
477                         $multipartMessageBody,                                                  // message body
478                         $messageHeader                                                                  // message headers
479                 );
480                 logger("notification: enotify::send returns " . $res, LOGGER_DEBUG);
481         }
482 }
483 ?>