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