]> 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         $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 a private message.'),'[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
32                 $sitelink = t('Please visit %s to view and/or reply to your private messages.');
33                 $tsitelink = sprintf( $sitelink, $siteurl . '/message' );
34                 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '/message">' . $sitename . '</a>');
35                 $itemlink = $siteurl . '/message';
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 an item/conversation you have been following.'), '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/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 your profile wall') , '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/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 tagged you') , '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/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 your post') , '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/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 an introduction from %s'), '[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 a friend suggestion for %s from %s'),
96                         '[url=' . $params['item']['url'] . ']' . $params['item']['name'] . '[/url]', 
97                         '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]'); 
98                 $body = t('Name:') . ' ' . $params['item']['name'] . "\n";
99                 $body .= t('Photo:') . ' ' . $params['item']['photo'] . "\n";
100                 $body .= sprintf( t('You may visit their profile at %s'),$params['item']['url']);
101
102                 $sitelink = t('Please visit %s to approve or reject the suggestion.');
103                 $tsitelink = sprintf( $sitelink, $siteurl );
104                 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
105                 $itemlink =  $params['link'];
106         }
107
108         if($params['type'] == NOTIFY_CONFIRM) {
109
110         }
111
112         // from here on everything is in the recipients language
113
114         push_lang($params['language']);
115
116         require_once('include/html2bbcode.php');        
117
118         // create notification entry in DB
119
120         $r = q("insert into notify (name,url,photo,date,msg,uid,link,type,verb,otype)
121                 values('%s','%s','%s','%s','%s',%d,'%s',%d,'%s','%s')",
122                 dbesc($params['source_name']),
123                 dbesc($params['source_link']),
124                 dbesc($params['source_photo']),
125                 dbesc(datetime_convert()),
126                 dbesc($epreamble),
127                 intval($params['uid']),
128                 dbesc($itemlink),
129                 intval($params['type']),
130                 dbesc($params['verb']),
131                 dbesc($params['otype'])
132         );
133
134         // send email notification if notification preferences permit
135
136         require_once('bbcode.php');
137         if(intval($params['notify_flags']) & intval($params['type'])) {
138
139                 logger('notification: sending notification email');
140
141
142                 $textversion = strip_tags(html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",
143                         $body))),ENT_QUOTES,'UTF-8'));
144                 $htmlversion = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"), 
145                         "<br />\n",$body))));
146
147                 // load the template for private message notifications
148                 $tpl = get_markup_template('email_notify_html.tpl');
149                 $email_html_body = replace_macros($tpl,array(
150                         '$banner'       => $banner,
151                         '$product'      => $product,
152                         '$preamble'     => $preamble,
153                         '$sitename'     => $sitename,
154                         '$siteurl'      => $siteurl,
155                         '$source_name'  => $params['source_name'],
156                         '$source_link'  => $params['source_link'],
157                         '$source_photo' => $params['source_photo'],
158                         '$username'     => $params['to_name'],
159                         '$hsitelink'    => $hsitelink,
160                         '$itemlink'     => '<a href="' . $itemlink . '">' . $itemlink . '</a>',
161                         '$thanks'       => $thanks,
162                         '$site_admin'   => $site_admin,
163                         '$title'                => stripslashes($title),
164                         '$htmlversion'  => $htmlversion,        
165                 ));
166                 
167                 // load the template for private message notifications
168                 $tpl = get_markup_template('email_notify_text.tpl');
169                 $email_text_body = replace_macros($tpl,array(
170                         '$banner'       => $banner,
171                         '$product'      => $product,
172                         '$preamble'     => $preamble,
173                         '$sitename'     => $sitename,
174                         '$siteurl'      => $siteurl,
175                         '$source_name'  => $params['source_name'],
176                         '$source_link'  => $params['source_link'],
177                         '$source_photo' => $params['source_photo'],
178                         '$username'     => $params['to_name'],
179                         '$tsitelink'    => $tsitelink,
180                         '$itemlink'     => $itemlink,
181                         '$thanks'       => $thanks,
182                         '$site_admin'   => $site_admin,
183                         '$title'                => stripslashes($title),
184                         '$textversion'  => $textversion,        
185                 ));
186
187 //              logger('text: ' . $email_text_body);
188
189                 // use the EmailNotification library to send the message
190
191                 enotify::send(array(
192                         'fromName' => $sender_name,
193                         'fromEmail' => $sender_email,
194                         'replyTo' => $sender_email,
195                         'toEmail' => $params['to_email'],
196                         'messageSubject' => $subject,
197                         'htmlVersion' => $email_html_body,
198                         'textVersion' => $email_text_body
199                 ));
200         }
201
202         pop_lang();
203
204 }
205
206 require_once('include/email.php');
207
208 class enotify {
209         /**
210          * Send a multipart/alternative message with Text and HTML versions
211          *
212          * @param fromName                      name of the sender
213          * @param fromEmail                     email fo the sender
214          * @param replyTo                       replyTo address to direct responses
215          * @param toEmail                       destination email address
216          * @param messageSubject        subject of the message
217          * @param htmlVersion           html version of the message
218          * @param textVersion           text only version of the message
219          */
220         static public function send($params) {
221
222                 $fromName = email_header_encode($params['fromName'],'UTF-8'); 
223                 $messageSubject = email_header_encode($params['messageSubject'],'UTF-8');
224                 
225                 // generate a mime boundary
226                 $mimeBoundary   =rand(0,9)."-"
227                                 .rand(10000000000,9999999999)."-"
228                                 .rand(10000000000,9999999999)."=:"
229                                 .rand(10000,99999);
230
231                 // generate a multipart/alternative message header
232                 $messageHeader =
233                         "From: {$params['fromName']} <{$params['fromEmail']}>\n" . 
234                         "Reply-To: {$params['fromName']} <{$params['replyTo']}>\n" .
235                         "MIME-Version: 1.0\n" .
236                         "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
237
238                 // assemble the final multipart message body with the text and html types included
239                 $textBody       =       chunk_split(base64_encode($params['textVersion']));
240                 $htmlBody       =       chunk_split(base64_encode($params['htmlVersion']));
241                 $multipartMessageBody =
242                         "--" . $mimeBoundary . "\n" .                                   // plain text section
243                         "Content-Type: text/plain; charset=UTF-8\n" .
244                         "Content-Transfer-Encoding: base64\n\n" .
245                         $textBody . "\n" .
246                         "--" . $mimeBoundary . "\n" .                                   // text/html section
247                         "Content-Type: text/html; charset=UTF-8\n" .
248                         "Content-Transfer-Encoding: base64\n\n" .
249                         $htmlBody . "\n" .
250                         "--" . $mimeBoundary . "--\n";                                  // message ending
251
252                 // send the message
253                 $res = mail(
254                         $params['toEmail'],                                                                             // send to address
255                         $params['messageSubject'],                                                              // subject
256                         $multipartMessageBody,                                                  // message body
257                         $messageHeader                                                                  // message headers
258                 );
259                 logger("notification: enotify::send returns " . $res, LOGGER_DEBUG);
260         }
261 }
262 ?>