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