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