]> git.mxchange.org Git - friendica.git/blob - include/enotify.php
Wrong variable name
[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         // create notification entry in DB
139
140         $r = q("insert into notify (hash,name,url,photo,date,uid,link,type,verb,otype)
141                 values('%s','%s','%s','%s','%s',%d,'%s',%d,'%s','%s')",
142                 dbesc($hash),
143                 dbesc($params['source_name']),
144                 dbesc($params['source_link']),
145                 dbesc($params['source_photo']),
146                 dbesc(datetime_convert()),
147                 intval($params['uid']),
148                 dbesc($itemlink),
149                 intval($params['type']),
150                 dbesc($params['verb']),
151                 dbesc($params['otype'])
152         );
153
154         $r = q("select id from notify where hash = '%s' and uid = %d limit 1",
155                 dbesc($hash),
156                 intval($params['uid'])
157         );
158         if($r)
159                 $notify_id = $r[0]['id'];
160         else
161                 return;
162
163         $itemlink = $a->get_baseurl() . '/notify/view/' . $notify_id;
164         $msg = replace_macros($epreamble,array('$itemlink' => $itemlink));
165         $r = q("update notify set msg = '%s' where id = %d and uid = %d limit 1",
166                 dbesc($msg),
167                 intval($notify_id),
168                 intval($params['uid'])
169         );
170                 
171
172
173         // send email notification if notification preferences permit
174
175         require_once('bbcode.php');
176         if(intval($params['notify_flags']) & intval($params['type'])) {
177
178                 logger('notification: sending notification email');
179
180                 $id_for_parent = "${params['parent']}@${hostname}";
181
182                 // Is this the first email notification for this parent item and user?
183                 
184                 $r = q("select `id` from `notify-threads` where `master-parent-item` = %d and `receiver-uid` = %d limit 1", 
185                         intval($params['parent']),
186                         intval($params['uid']) );
187
188                 // If so, create the record of it and use a message-id smtp header.
189
190                 if(!$r) {
191                         logger("norify_id:" . intval($notify_id). ", parent: " . intval($params['parent']) . "uid: " . 
192 intval($params['uid']), LOGGER_DEBUG);
193                         $r = q("insert into `notify-threads` (`notify-id`, `master-parent-item`, `receiver-uid`, `parent-item`)
194                                 values(%d,%d,%d,%d)",
195                                 intval($notify_id),
196                                 intval($params['parent']),
197                                 intval($params['uid']), 
198                                 0 );
199
200                         $additional_mail_header .= "Message-ID: <${id_for_parent}>\n";
201                         $log_msg = "include/enotify: No previous notification found for this parent:\n" . 
202                                         "  parent: ${params['parent']}\n" . "  uid   : ${params['uid']}\n";
203                         logger($log_msg, LOGGER_DEBUG);
204                 }
205
206                 // If not, just "follow" the thread.
207
208                 else {
209                         $additional_mail_header = "References: <${id_for_parent}>\nIn-Reply-To: <${id_for_parent}>\n";
210                         logger("include/enotify: There's already a notification for this parent:\n" . print_r($r, true), LOGGER_DEBUG);
211                 }
212
213
214
215                 $textversion = strip_tags(html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",
216                         $body))),ENT_QUOTES,'UTF-8'));
217                 $htmlversion = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"), 
218                         "<br />\n",$body))));
219
220                 // load the template for private message notifications
221                 $tpl = get_markup_template('email_notify_html.tpl');
222                 $email_html_body = replace_macros($tpl,array(
223                         '$banner'       => $banner,
224                         '$product'      => $product,
225                         '$preamble'     => $preamble,
226                         '$sitename'     => $sitename,
227                         '$siteurl'      => $siteurl,
228                         '$source_name'  => $params['source_name'],
229                         '$source_link'  => $params['source_link'],
230                         '$source_photo' => $params['source_photo'],
231                         '$username'     => $params['to_name'],
232                         '$hsitelink'    => $hsitelink,
233                         '$itemlink'     => '<a href="' . $itemlink . '">' . $itemlink . '</a>',
234                         '$thanks'       => $thanks,
235                         '$site_admin'   => $site_admin,
236                         '$title'                => stripslashes($title),
237                         '$htmlversion'  => $htmlversion,        
238                 ));
239                 
240                 // load the template for private message notifications
241                 $tpl = get_markup_template('email_notify_text.tpl');
242                 $email_text_body = replace_macros($tpl,array(
243                         '$banner'       => $banner,
244                         '$product'      => $product,
245                         '$preamble'     => $preamble,
246                         '$sitename'     => $sitename,
247                         '$siteurl'      => $siteurl,
248                         '$source_name'  => $params['source_name'],
249                         '$source_link'  => $params['source_link'],
250                         '$source_photo' => $params['source_photo'],
251                         '$username'     => $params['to_name'],
252                         '$tsitelink'    => $tsitelink,
253                         '$itemlink'     => $itemlink,
254                         '$thanks'       => $thanks,
255                         '$site_admin'   => $site_admin,
256                         '$title'                => stripslashes($title),
257                         '$textversion'  => $textversion,        
258                 ));
259
260 //              logger('text: ' . $email_text_body);
261
262                 // use the EmailNotification library to send the message
263
264                 enotify::send(array(
265                         'fromName' => $sender_name,
266                         'fromEmail' => $sender_email,
267                         'replyTo' => $sender_email,
268                         'toEmail' => $params['to_email'],
269                         'messageSubject' => $subject,
270                         'htmlVersion' => $email_html_body,
271                         'textVersion' => $email_text_body,
272                         'additionalMailHeader' => $additional_mail_header,
273                 ));
274         }
275
276         pop_lang();
277
278 }
279
280 require_once('include/email.php');
281
282 class enotify {
283         /**
284          * Send a multipart/alternative message with Text and HTML versions
285          *
286          * @param fromName                      name of the sender
287          * @param fromEmail                     email fo the sender
288          * @param replyTo                       replyTo address to direct responses
289          * @param toEmail                       destination email address
290          * @param messageSubject        subject of the message
291          * @param htmlVersion           html version of the message
292          * @param textVersion           text only version of the message
293          * @param additionalMailHeader  additions to the smtp mail header
294          */
295         static public function send($params) {
296
297                 $fromName = email_header_encode($params['fromName'],'UTF-8'); 
298                 $messageSubject = email_header_encode($params['messageSubject'],'UTF-8');
299                 
300                 // generate a mime boundary
301                 $mimeBoundary   =rand(0,9)."-"
302                                 .rand(10000000000,9999999999)."-"
303                                 .rand(10000000000,9999999999)."=:"
304                                 .rand(10000,99999);
305
306                 // generate a multipart/alternative message header
307                 $messageHeader =
308                         $params['additionalMailHeader'] .
309                         "From: {$params['fromName']} <{$params['fromEmail']}>\n" . 
310                         "Reply-To: {$params['fromName']} <{$params['replyTo']}>\n" .
311                         "MIME-Version: 1.0\n" .
312                         "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
313
314                 // assemble the final multipart message body with the text and html types included
315                 $textBody       =       chunk_split(base64_encode($params['textVersion']));
316                 $htmlBody       =       chunk_split(base64_encode($params['htmlVersion']));
317                 $multipartMessageBody =
318                         "--" . $mimeBoundary . "\n" .                                   // plain text section
319                         "Content-Type: text/plain; charset=UTF-8\n" .
320                         "Content-Transfer-Encoding: base64\n\n" .
321                         $textBody . "\n" .
322                         "--" . $mimeBoundary . "\n" .                                   // text/html section
323                         "Content-Type: text/html; charset=UTF-8\n" .
324                         "Content-Transfer-Encoding: base64\n\n" .
325                         $htmlBody . "\n" .
326                         "--" . $mimeBoundary . "--\n";                                  // message ending
327
328                 // send the message
329                 $res = mail(
330                         $params['toEmail'],                                                                             // send to address
331                         $params['messageSubject'],                                                              // subject
332                         $multipartMessageBody,                                                  // message body
333                         $messageHeader                                                                  // message headers
334                 );
335                 logger("notification: enotify::send returns " . $res, LOGGER_DEBUG);
336         }
337 }
338 ?>