]> git.mxchange.org Git - friendica.git/blob - include/enotify.php
Merge branch 'master' into notify
[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         $sender_email = t('noreply') . '@' . $a->get_hostname();
17
18         if(array_key_exists('item',$params)) {
19                 $title = $params['item']['title'];
20                 $body = $params['item']['body'];
21         }
22         else {
23                 $title = $body = '';
24         }
25
26         if($params['type'] == NOTIFY_MAIL) {
27
28                 $subject =      sprintf( t('New mail received at %s'),$sitename);
29
30                 $preamble = sprintf( t('%s sent you a new private message at %s.'),$params['source_name'],$sitename);
31                 $epreamble = sprintf( t('%s sent you %s.'),'[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=$itemlink]' . t('a private message') . '[/url]');
32                 $sitelink = t('Please visit %s to view and/or reply to your private messages.');
33                 $tsitelink = sprintf( $sitelink, $siteurl . '/message/' . $params['item']['id'] );
34                 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '/message/' . $params['item']['id'] . '">' . $sitename . '</a>');
35                 $itemlink = $siteurl . '/message/' . $params['item']['id'];
36         }
37
38         if($params['type'] == NOTIFY_COMMENT) {
39
40                 $subject = sprintf( t('%s commented on an item at %s'), $params['source_name'], $sitename);
41                 $preamble = sprintf( t('%s commented on an item/conversation you have been following.'), $params['source_name']); 
42                 $epreamble = sprintf( t('%s commented on %s you have been following.'), '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=$itemlink]' . t('an item/conversation') . '[/url]'); 
43
44                 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
45                 $tsitelink = sprintf( $sitelink, $siteurl );
46                 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
47                 $itemlink =  $params['link'];
48         }
49
50         if($params['type'] == NOTIFY_WALL) {
51                 $preamble = $subject =  sprintf( t('%s posted to your profile wall at %s') , $params['source_name'], $sitename);
52                 $epreamble = sprintf( t('%s posted to %s') , '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=$itemlink]' . t('your profile wall.') . '[/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_TAGSELF) {
61                 $preamble = $subject =  sprintf( t('%s tagged you at %s') , $params['source_name'], $sitename);
62                 $epreamble = sprintf( t('%s %s.') , '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=' . $params['link'] . ']' . t('tagged you') . '[/url]'); 
63
64                 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
65                 $tsitelink = sprintf( $sitelink, $siteurl );
66                 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
67                 $itemlink =  $params['link'];
68         }
69
70         if($params['type'] == NOTIFY_TAGSHARE) {
71                 $preamble = $subject =  sprintf( t('%s tagged your post at %s') , $params['source_name'], $sitename);
72                 $epreamble = sprintf( t('%s tagged %s') , '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=$itemlink]' . t('your post') . '[/url]' ); 
73
74                 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
75                 $tsitelink = sprintf( $sitelink, $siteurl );
76                 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
77                 $itemlink =  $params['link'];
78         }
79
80         if($params['type'] == NOTIFY_INTRO) {
81                 $subject = sprintf( t('Introduction received at %s'), $sitename);
82                 $preamble = sprintf( t('You\'ve received an introduction from \'%s\' at %s'), $params['source_name'], $sitename); 
83                 $epreamble = sprintf( t('You\'ve received %s from %s.'), '[url=$itemlink]' . t('an introduction') . '[/url]' , '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]'); 
84                 $body = sprintf( t('You may visit their profile at %s'),$params['source_link']);
85
86                 $sitelink = t('Please visit %s to approve or reject the introduction.');
87                 $tsitelink = sprintf( $sitelink, $siteurl );
88                 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
89                 $itemlink =  $params['link'];
90         }
91
92         if($params['type'] == NOTIFY_SUGGEST) {
93                 $subject = sprintf( t('Friend suggestion received at %s'), $sitename);
94                 $preamble = sprintf( t('You\'ve received a friend suggestion from \'%s\' at %s'), $params['source_name'], $sitename); 
95                 $epreamble = sprintf( t('You\'ve received %s for %s from %s.'),
96                         '[url=$itemlink]' . t('a friend suggestion') . '[/url]',
97                         '[url=' . $params['item']['url'] . ']' . $params['item']['name'] . '[/url]', 
98                         '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]'); 
99                 $body = t('Name:') . ' ' . $params['item']['name'] . "\n";
100                 $body .= t('Photo:') . ' ' . $params['item']['photo'] . "\n";
101                 $body .= sprintf( t('You may visit their profile at %s'),$params['item']['url']);
102
103                 $sitelink = t('Please visit %s to approve or reject the suggestion.');
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_CONFIRM) {
110
111         }
112
113         // from here on everything is in the recipients language
114
115         push_lang($params['language']);
116
117         require_once('include/html2bbcode.php');        
118
119         do {
120                 $dups = false;
121                 $hash = random_string();
122         $r = q("SELECT `id` FROM `notify` WHERE `hash` = '%s' LIMIT 1",
123                         dbesc($hash));
124                 if(count($r))
125                         $dups = true;
126         } while($dups == true);
127
128
129
130
131         // create notification entry in DB
132
133         $r = q("insert into notify (hash,name,url,photo,date,uid,link,type,verb,otype)
134                 values('%s','%s','%s','%s','%s',%d,'%s',%d,'%s','%s')",
135                 dbesc($hash),
136                 dbesc($params['source_name']),
137                 dbesc($params['source_link']),
138                 dbesc($params['source_photo']),
139                 dbesc(datetime_convert()),
140                 intval($params['uid']),
141                 dbesc($itemlink),
142                 intval($params['type']),
143                 dbesc($params['verb']),
144                 dbesc($params['otype'])
145         );
146
147         $r = q("select id from notify where hash = '%s' and uid = %d limit 1",
148                 dbesc($hash),
149                 intval($params['uid'])
150         );
151         if($r)
152                 $notify_id = $r[0]['id'];
153         else
154                 return;
155
156         $itemlink = $a->get_baseurl() . '/notify/view/' . $notify_id;
157         $msg = replace_macros($epreamble,array('$itemlink' => $itemlink));
158         $r = q("update notify set msg = '%s' where id = %d and uid = %d limit 1",
159                 dbesc($msg),
160                 intval($notify_id),
161                 intval($params['uid'])
162         );
163                 
164
165
166         // send email notification if notification preferences permit
167
168         require_once('bbcode.php');
169         if(intval($params['notify_flags']) & intval($params['type'])) {
170
171                 logger('notification: sending notification email');
172
173
174                 $textversion = strip_tags(html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",
175                         $body))),ENT_QUOTES,'UTF-8'));
176                 $htmlversion = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"), 
177                         "<br />\n",$body))));
178
179                 // load the template for private message notifications
180                 $tpl = get_markup_template('email_notify_html.tpl');
181                 $email_html_body = replace_macros($tpl,array(
182                         '$banner'       => $banner,
183                         '$product'      => $product,
184                         '$preamble'     => $preamble,
185                         '$sitename'     => $sitename,
186                         '$siteurl'      => $siteurl,
187                         '$source_name'  => $params['source_name'],
188                         '$source_link'  => $params['source_link'],
189                         '$source_photo' => $params['source_photo'],
190                         '$username'     => $params['to_name'],
191                         '$hsitelink'    => $hsitelink,
192                         '$itemlink'     => '<a href="' . $itemlink . '">' . $itemlink . '</a>',
193                         '$thanks'       => $thanks,
194                         '$site_admin'   => $site_admin,
195                         '$title'                => stripslashes($title),
196                         '$htmlversion'  => $htmlversion,        
197                 ));
198                 
199                 // load the template for private message notifications
200                 $tpl = get_markup_template('email_notify_text.tpl');
201                 $email_text_body = replace_macros($tpl,array(
202                         '$banner'       => $banner,
203                         '$product'      => $product,
204                         '$preamble'     => $preamble,
205                         '$sitename'     => $sitename,
206                         '$siteurl'      => $siteurl,
207                         '$source_name'  => $params['source_name'],
208                         '$source_link'  => $params['source_link'],
209                         '$source_photo' => $params['source_photo'],
210                         '$username'     => $params['to_name'],
211                         '$tsitelink'    => $tsitelink,
212                         '$itemlink'     => $itemlink,
213                         '$thanks'       => $thanks,
214                         '$site_admin'   => $site_admin,
215                         '$title'                => stripslashes($title),
216                         '$textversion'  => $textversion,        
217                 ));
218
219 //              logger('text: ' . $email_text_body);
220
221                 // use the EmailNotification library to send the message
222
223                 enotify::send(array(
224                         'fromName' => $sender_name,
225                         'fromEmail' => $sender_email,
226                         'replyTo' => $sender_email,
227                         'toEmail' => $params['to_email'],
228                         'messageSubject' => $subject,
229                         'htmlVersion' => $email_html_body,
230                         'textVersion' => $email_text_body
231                 ));
232         }
233
234         pop_lang();
235
236 }
237
238 require_once('include/email.php');
239
240 class enotify {
241         /**
242          * Send a multipart/alternative message with Text and HTML versions
243          *
244          * @param fromName                      name of the sender
245          * @param fromEmail                     email fo the sender
246          * @param replyTo                       replyTo address to direct responses
247          * @param toEmail                       destination email address
248          * @param messageSubject        subject of the message
249          * @param htmlVersion           html version of the message
250          * @param textVersion           text only version of the message
251          */
252         static public function send($params) {
253
254                 $fromName = email_header_encode($params['fromName'],'UTF-8'); 
255                 $messageSubject = email_header_encode($params['messageSubject'],'UTF-8');
256                 
257                 // generate a mime boundary
258                 $mimeBoundary   =rand(0,9)."-"
259                                 .rand(10000000000,9999999999)."-"
260                                 .rand(10000000000,9999999999)."=:"
261                                 .rand(10000,99999);
262
263                 // generate a multipart/alternative message header
264                 $messageHeader =
265                         "From: {$params['fromName']} <{$params['fromEmail']}>\n" . 
266                         "Reply-To: {$params['fromName']} <{$params['replyTo']}>\n" .
267                         "MIME-Version: 1.0\n" .
268                         "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
269
270                 // assemble the final multipart message body with the text and html types included
271                 $textBody       =       chunk_split(base64_encode($params['textVersion']));
272                 $htmlBody       =       chunk_split(base64_encode($params['htmlVersion']));
273                 $multipartMessageBody =
274                         "--" . $mimeBoundary . "\n" .                                   // plain text section
275                         "Content-Type: text/plain; charset=UTF-8\n" .
276                         "Content-Transfer-Encoding: base64\n\n" .
277                         $textBody . "\n" .
278                         "--" . $mimeBoundary . "\n" .                                   // text/html section
279                         "Content-Type: text/html; charset=UTF-8\n" .
280                         "Content-Transfer-Encoding: base64\n\n" .
281                         $htmlBody . "\n" .
282                         "--" . $mimeBoundary . "--\n";                                  // message ending
283
284                 // send the message
285                 $res = mail(
286                         $params['toEmail'],                                                                             // send to address
287                         $params['messageSubject'],                                                              // subject
288                         $multipartMessageBody,                                                  // message body
289                         $messageHeader                                                                  // message headers
290                 );
291                 logger("notification: enotify::send returns " . $res, LOGGER_DEBUG);
292         }
293 }
294 ?>