]> git.mxchange.org Git - friendica.git/blob - include/enotify.php
Merge remote-tracking branch 'friendica/master' into randomerror
[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         // from here on everything is in the recipients language
157
158         push_lang($params['language']);
159
160         require_once('include/html2bbcode.php');        
161
162         do {
163                 $dups = false;
164                 $hash = random_string();
165         $r = q("SELECT `id` FROM `notify` WHERE `hash` = '%s' LIMIT 1",
166                         dbesc($hash));
167                 if(count($r))
168                         $dups = true;
169         } while($dups == true);
170
171
172         $datarray = array();
173         $datarray['hash']  = $hash;
174         $datarray['name']  = $params['source_name'];
175         $datarray['url']   = $params['source_link'];
176         $datarray['photo'] = $params['source_photo'];
177         $datarray['date']  = datetime_convert();
178         $datarray['uid']   = $params['uid'];
179         $datarray['link']  = $itemlink;
180         $datarray['parent'] = $parent_id;
181         $datarray['type']  = $params['type'];
182         $datarray['verb']  = $params['verb'];
183         $datarray['otype'] = $params['otype'];
184  
185         call_hooks('enotify_store', $datarray);
186
187         // create notification entry in DB
188
189         $r = q("insert into notify (hash,name,url,photo,date,uid,link,parent,type,verb,otype)
190                 values('%s','%s','%s','%s','%s',%d,'%s',%d,%d,'%s','%s')",
191                 dbesc($datarray['hash']),
192                 dbesc($datarray['name']),
193                 dbesc($datarray['url']),
194                 dbesc($datarray['photo']),
195                 dbesc($datarray['date']),
196                 intval($datarray['uid']),
197                 dbesc($datarray['link']),
198                 intval($datarray['parent']),
199                 intval($datarray['type']),
200                 dbesc($datarray['verb']),
201                 dbesc($datarray['otype'])
202         );
203
204         $r = q("select id from notify where hash = '%s' and uid = %d limit 1",
205                 dbesc($hash),
206                 intval($params['uid'])
207         );
208         if($r)
209                 $notify_id = $r[0]['id'];
210         else
211                 return;
212
213         $itemlink = $a->get_baseurl() . '/notify/view/' . $notify_id;
214         $msg = replace_macros($epreamble,array('$itemlink' => $itemlink));
215         $r = q("update notify set msg = '%s' where id = %d and uid = %d limit 1",
216                 dbesc($msg),
217                 intval($notify_id),
218                 intval($params['uid'])
219         );
220                 
221
222
223         // send email notification if notification preferences permit
224
225         require_once('bbcode.php');
226         if(intval($params['notify_flags']) & intval($params['type'])) {
227
228                 logger('notification: sending notification email');
229
230                 $id_for_parent = "${params['parent']}@${hostname}";
231
232                 // Is this the first email notification for this parent item and user?
233                 
234                 $r = q("select `id` from `notify-threads` where `master-parent-item` = %d and `receiver-uid` = %d limit 1", 
235                         intval($params['parent']),
236                         intval($params['uid']) );
237
238                 // If so, create the record of it and use a message-id smtp header.
239
240                 if(!$r) {
241                         logger("norify_id:" . intval($notify_id). ", parent: " . intval($params['parent']) . "uid: " . 
242 intval($params['uid']), LOGGER_DEBUG);
243                         $r = q("insert into `notify-threads` (`notify-id`, `master-parent-item`, `receiver-uid`, `parent-item`)
244                                 values(%d,%d,%d,%d)",
245                                 intval($notify_id),
246                                 intval($params['parent']),
247                                 intval($params['uid']), 
248                                 0 );
249
250                         $additional_mail_header .= "Message-ID: <${id_for_parent}>\n";
251                         $log_msg = "include/enotify: No previous notification found for this parent:\n" . 
252                                         "  parent: ${params['parent']}\n" . "  uid   : ${params['uid']}\n";
253                         logger($log_msg, LOGGER_DEBUG);
254                 }
255
256                 // If not, just "follow" the thread.
257
258                 else {
259                         $additional_mail_header = "References: <${id_for_parent}>\nIn-Reply-To: <${id_for_parent}>\n";
260                         logger("include/enotify: There's already a notification for this parent:\n" . print_r($r, true), LOGGER_DEBUG);
261                 }
262
263
264
265                 $textversion = strip_tags(html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",
266                         $body))),ENT_QUOTES,'UTF-8'));
267                 $htmlversion = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"), 
268                         "<br />\n",$body))));
269
270                 $datarray = array();
271                 $datarray['banner'] = $banner;
272                 $datarray['product'] = $product;
273                 $datarray['preamble'] = $preamble;
274                 $datarray['sitename'] = $sitename;
275                 $datarray['siteurl'] = $siteurl;
276                 $datarray['type'] = $params['type'];
277                 $datarray['parent'] = $params['parent'];
278                 $datarray['source_name'] = $params['source_name'];
279                 $datarray['source_link'] = $params['source_link'];
280                 $datarray['source_photo'] = $params['source_photo'];
281                 $datarray['uid'] = $params['uid'];
282                 $datarray['username'] = $params['to_name'];
283                 $datarray['hsitelink'] = $hsitelink;
284                 $datarray['tsitelink'] = $tsitelink;
285                 $datarray['hitemlink'] = '<a href="' . $itemlink . '">' . $itemlink . '</a>';
286                 $datarray['titemlink'] = $itemlink;
287                 $datarray['thanks'] = $thanks;
288                 $datarray['site_admin'] = $site_admin;
289                 $datarray['title'] = stripslashes($title);
290                 $datarray['htmlversion'] = $htmlversion;
291                 $datarray['textversion'] = $textversion;
292                 $datarray['subject'] = $subject;
293                 $datarray['headers'] = $additional_mail_header;
294
295                 call_hooks('enotify_mail', $datarray);
296
297                 // load the template for private message notifications
298                 $tpl = get_markup_template('email_notify_html.tpl');
299                 $email_html_body = replace_macros($tpl,array(
300                         '$banner'       => $datarray['banner'],
301                         '$product'      => $datarray['product'],
302                         '$preamble'     => $datarray['preamble'],
303                         '$sitename'     => $datarray['sitename'],
304                         '$siteurl'      => $datarray['siteurl'],
305                         '$source_name'  => $datarray['source_name'],
306                         '$source_link'  => $datarray['source_link'],
307                         '$source_photo' => $datarray['source_photo'],
308                         '$username'     => $datarray['to_name'],
309                         '$hsitelink'    => $datarray['hsitelink'],
310                         '$hitemlink'    => $datarray['hitemlink'],
311                         '$thanks'       => $datarray['thanks'],
312                         '$site_admin'   => $datarray['site_admin'],
313                         '$title'                => $datarray['title'],
314                         '$htmlversion'  => $datarray['htmlversion'],    
315                 ));
316                 
317                 // load the template for private message notifications
318                 $tpl = get_markup_template('email_notify_text.tpl');
319                 $email_text_body = replace_macros($tpl,array(
320                         '$banner'       => $datarray['banner'],
321                         '$product'      => $datarray['product'],
322                         '$preamble'     => $datarray['preamble'],
323                         '$sitename'     => $datarray['sitename'],
324                         '$siteurl'      => $datarray['siteurl'],
325                         '$source_name'  => $datarray['source_name'],
326                         '$source_link'  => $datarray['source_link'],
327                         '$source_photo' => $datarray['source_photo'],
328                         '$username'     => $datarray['to_name'],
329                         '$tsitelink'    => $datarray['tsitelink'],
330                         '$titemlink'    => $datarray['titemlink'],
331                         '$thanks'       => $datarray['thanks'],
332                         '$site_admin'   => $datarray['site_admin'],
333                         '$title'                => $datarray['title'],
334                         '$textversion'  => $datarray['textversion'],    
335                 ));
336
337 //              logger('text: ' . $email_text_body);
338
339                 // use the EmailNotification library to send the message
340
341                 enotify::send(array(
342                         'fromName' => $sender_name,
343                         'fromEmail' => $sender_email,
344                         'replyTo' => $sender_email,
345                         'toEmail' => $params['to_email'],
346                         'messageSubject' => $datarray['subject'],
347                         'htmlVersion' => $email_html_body,
348                         'textVersion' => $email_text_body,
349                         'additionalMailHeader' => $datarray['headers'],
350                 ));
351         }
352
353         pop_lang();
354
355 }
356
357 require_once('include/email.php');
358
359 class enotify {
360         /**
361          * Send a multipart/alternative message with Text and HTML versions
362          *
363          * @param fromName                      name of the sender
364          * @param fromEmail                     email fo the sender
365          * @param replyTo                       replyTo address to direct responses
366          * @param toEmail                       destination email address
367          * @param messageSubject        subject of the message
368          * @param htmlVersion           html version of the message
369          * @param textVersion           text only version of the message
370          * @param additionalMailHeader  additions to the smtp mail header
371          */
372         static public function send($params) {
373
374                 $fromName = email_header_encode($params['fromName'],'UTF-8'); 
375                 $messageSubject = email_header_encode($params['messageSubject'],'UTF-8');
376                 
377                 // generate a mime boundary
378                 $mimeBoundary   =rand(0,9)."-"
379                                 .rand(10000000000,9999999999)."-"
380                                 .rand(10000000000,9999999999)."=:"
381                                 .rand(10000,99999);
382
383                 // generate a multipart/alternative message header
384                 $messageHeader =
385                         $params['additionalMailHeader'] .
386                         "From: {$params['fromName']} <{$params['fromEmail']}>\n" . 
387                         "Reply-To: {$params['fromName']} <{$params['replyTo']}>\n" .
388                         "MIME-Version: 1.0\n" .
389                         "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
390
391                 // assemble the final multipart message body with the text and html types included
392                 $textBody       =       chunk_split(base64_encode($params['textVersion']));
393                 $htmlBody       =       chunk_split(base64_encode($params['htmlVersion']));
394                 $multipartMessageBody =
395                         "--" . $mimeBoundary . "\n" .                                   // plain text section
396                         "Content-Type: text/plain; charset=UTF-8\n" .
397                         "Content-Transfer-Encoding: base64\n\n" .
398                         $textBody . "\n" .
399                         "--" . $mimeBoundary . "\n" .                                   // text/html section
400                         "Content-Type: text/html; charset=UTF-8\n" .
401                         "Content-Transfer-Encoding: base64\n\n" .
402                         $htmlBody . "\n" .
403                         "--" . $mimeBoundary . "--\n";                                  // message ending
404
405                 // send the message
406                 $res = mail(
407                         $params['toEmail'],                                                                             // send to address
408                         $params['messageSubject'],                                                              // subject
409                         $multipartMessageBody,                                                  // message body
410                         $messageHeader                                                                  // message headers
411                 );
412                 logger("notification: enotify::send returns " . $res, LOGGER_DEBUG);
413         }
414 }
415 ?>