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