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