3 function notification($params) {
5 logger('notification: entry', LOGGER_DEBUG);
8 $banner = t('Friendica Notification');
9 $product = FRIENDICA_PLATFORM;
11 $thanks = t('Thank You,');
12 $sitename = get_config('config','sitename');
13 $site_admin = sprintf( t('%s Administrator'), $sitename);
15 $sender_name = $product;
16 $hostname = $a->get_hostname();
17 $sender_email = t('noreply') . '@' . $hostname;
18 $additional_mail_header = "";
20 if(array_key_exists('item',$params)) {
21 $title = $params['item']['title'];
22 $body = $params['item']['body'];
28 if($params['otype'] === 'item')
29 $possess_desc = t('%s post');
30 if($params['otype'] == 'photo')
31 $possess_desc = t('%s photo');
34 if($params['type'] == NOTIFY_MAIL) {
36 $subject = sprintf( t('[Friendica:Notify] New mail received at %s'),$sitename);
38 $preamble = sprintf( t('%s sent you a new private message at %s.'),$params['source_name'],$sitename);
39 $epreamble = sprintf( t('%s sent you %s.'),'[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=$itemlink]' . t('a private message') . '[/url]');
40 $sitelink = t('Please visit %s to view and/or reply to your private messages.');
41 $tsitelink = sprintf( $sitelink, $siteurl . '/message/' . $params['item']['id'] );
42 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '/message/' . $params['item']['id'] . '">' . $sitename . '</a>');
43 $itemlink = $siteurl . '/message/' . $params['item']['id'];
46 if($params['type'] == NOTIFY_COMMENT) {
47 // logger("notification: params = " . print_r($params, true), LOGGER_DEBUG);
49 $parent_id = $params['parent'];
52 // if it's a post figure out who's post it is.
56 if($params['otype'] === 'item' && $parent_id) {
57 $p = q("select * from item where id = %d and uid = %d limit 1",
59 intval($params['uid'])
63 $dest_str = sprintf($possess_desc,'a');
65 $dest_str = sprintf($possess_desc,sprintf( t("%s's"),$p[0]['author-name']));
67 if($p[0]['owner-name'] == $p[0]['author-name'] && $p[0]['wall'])
68 $dest_str = sprintf($possess_desc, t('your') );
70 // Some mail softwares relies on subject field for threading.
71 // So, we cannot have different subjects for notifications of the same thread.
72 // Before this we have the name of the replier on the subject rendering
73 // differents subjects for messages on the same thread.
75 $subject = sprintf( t('[Friendica:Notify] Comment to conversation #%d by %s'), $parent_id, $params['source_name']);
76 $preamble = sprintf( t('%s commented on an item/conversation you have been following.'), $params['source_name']);
77 $epreamble = sprintf( t('%s commented on %s.'), '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=$itemlink]' . $dest_str . '[/url]');
79 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
80 $tsitelink = sprintf( $sitelink, $siteurl );
81 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
82 $itemlink = $params['link'];
85 if($params['type'] == NOTIFY_WALL) {
86 $subject = sprintf( t('[Friendica:Notify] %s posted to your profile wall') , $params['source_name']);
88 $preamble = sprintf( t('%s posted to your profile wall at %s') , $params['source_name'], $sitename);
90 $epreamble = sprintf( t('%s posted to %s') , '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=$itemlink]' . t('your profile wall.') . '[/url]');
92 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
93 $tsitelink = sprintf( $sitelink, $siteurl );
94 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
95 $itemlink = $params['link'];
98 if($params['type'] == NOTIFY_TAGSELF) {
99 $subject = sprintf( t('[Friendica:Notify] %s tagged you') , $params['source_name']);
100 $preamble = sprintf( t('%s tagged you at %s') , $params['source_name'], $sitename);
101 $epreamble = sprintf( t('%s %s.') , '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=' . $params['link'] . ']' . t('tagged you') . '[/url]');
103 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
104 $tsitelink = sprintf( $sitelink, $siteurl );
105 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
106 $itemlink = $params['link'];
109 if($params['type'] == NOTIFY_TAGSHARE) {
110 $subject = sprintf( t('[Friendica:Notify] %s tagged your post') , $params['source_name']);
111 $preamble = sprintf( t('%s tagged your post at %s') , $params['source_name'], $sitename);
112 $epreamble = sprintf( t('%s tagged %s') , '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=$itemlink]' . t('your post') . '[/url]' );
114 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
115 $tsitelink = sprintf( $sitelink, $siteurl );
116 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
117 $itemlink = $params['link'];
120 if($params['type'] == NOTIFY_INTRO) {
121 $subject = sprintf( t('[Friendica:Notify] Introduction received'));
122 $preamble = sprintf( t('You\'ve received an introduction from \'%s\' at %s'), $params['source_name'], $sitename);
123 $epreamble = sprintf( t('You\'ve received %s from %s.'), '[url=$itemlink]' . t('an introduction') . '[/url]' , '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
124 $body = sprintf( t('You may visit their profile at %s'),$params['source_link']);
126 $sitelink = t('Please visit %s to approve or reject the introduction.');
127 $tsitelink = sprintf( $sitelink, $siteurl );
128 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
129 $itemlink = $params['link'];
132 if($params['type'] == NOTIFY_SUGGEST) {
133 $subject = sprintf( t('[Friendica:Notify] Friend suggestion received'));
134 $preamble = sprintf( t('You\'ve received a friend suggestion from \'%s\' at %s'), $params['source_name'], $sitename);
135 $epreamble = sprintf( t('You\'ve received %s for %s from %s.'),
136 '[url=$itemlink]' . t('a friend suggestion') . '[/url]',
137 '[url=' . $params['item']['url'] . ']' . $params['item']['name'] . '[/url]',
138 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
139 $body = t('Name:') . ' ' . $params['item']['name'] . "\n";
140 $body .= t('Photo:') . ' ' . $params['item']['photo'] . "\n";
141 $body .= sprintf( t('You may visit their profile at %s'),$params['item']['url']);
143 $sitelink = t('Please visit %s to approve or reject the suggestion.');
144 $tsitelink = sprintf( $sitelink, $siteurl );
145 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
146 $itemlink = $params['link'];
149 if($params['type'] == NOTIFY_CONFIRM) {
153 // from here on everything is in the recipients language
155 push_lang($params['language']);
157 require_once('include/html2bbcode.php');
161 $hash = random_string();
162 $r = q("SELECT `id` FROM `notify` WHERE `hash` = '%s' LIMIT 1",
166 } while($dups == true);
170 $datarray['hash'] = $hash;
171 $datarray['name'] = $params['source_name'];
172 $datarray['url'] = $params['source_link'];
173 $datarray['photo'] = $params['source_photo'];
174 $datarray['date'] = datetime_convert();
175 $datarray['uid'] = $params['uid'];
176 $datarray['link'] = $itemlink;
177 $datarray['parent'] = $parent_id;
178 $datarray['type'] = $params['type'];
179 $datarray['verb'] = $params['verb'];
180 $datarray['otype'] = $params['otype'];
182 call_hooks('enotify_store', $datarray);
184 // create notification entry in DB
186 $r = q("insert into notify (hash,name,url,photo,date,uid,link,parent,type,verb,otype)
187 values('%s','%s','%s','%s','%s',%d,'%s',%d,%d,'%s','%s')",
188 dbesc($datarray['hash']),
189 dbesc($datarray['name']),
190 dbesc($datarray['url']),
191 dbesc($datarray['photo']),
192 dbesc($datarray['date']),
193 intval($datarray['uid']),
194 dbesc($datarray['link']),
195 intval($datarray['parent']),
196 intval($datarray['type']),
197 dbesc($datarray['verb']),
198 dbesc($datarray['otype'])
201 $r = q("select id from notify where hash = '%s' and uid = %d limit 1",
203 intval($params['uid'])
206 $notify_id = $r[0]['id'];
210 $itemlink = $a->get_baseurl() . '/notify/view/' . $notify_id;
211 $msg = replace_macros($epreamble,array('$itemlink' => $itemlink));
212 $r = q("update notify set msg = '%s' where id = %d and uid = %d limit 1",
215 intval($params['uid'])
220 // send email notification if notification preferences permit
222 require_once('bbcode.php');
223 if(intval($params['notify_flags']) & intval($params['type'])) {
225 logger('notification: sending notification email');
227 $id_for_parent = "${params['parent']}@${hostname}";
229 // Is this the first email notification for this parent item and user?
231 $r = q("select `id` from `notify-threads` where `master-parent-item` = %d and `receiver-uid` = %d limit 1",
232 intval($params['parent']),
233 intval($params['uid']) );
235 // If so, create the record of it and use a message-id smtp header.
238 logger("norify_id:" . intval($notify_id). ", parent: " . intval($params['parent']) . "uid: " .
239 intval($params['uid']), LOGGER_DEBUG);
240 $r = q("insert into `notify-threads` (`notify-id`, `master-parent-item`, `receiver-uid`, `parent-item`)
241 values(%d,%d,%d,%d)",
243 intval($params['parent']),
244 intval($params['uid']),
247 $additional_mail_header .= "Message-ID: <${id_for_parent}>\n";
248 $log_msg = "include/enotify: No previous notification found for this parent:\n" .
249 " parent: ${params['parent']}\n" . " uid : ${params['uid']}\n";
250 logger($log_msg, LOGGER_DEBUG);
253 // If not, just "follow" the thread.
256 $additional_mail_header = "References: <${id_for_parent}>\nIn-Reply-To: <${id_for_parent}>\n";
257 logger("include/enotify: There's already a notification for this parent:\n" . print_r($r, true), LOGGER_DEBUG);
262 $textversion = strip_tags(html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",
263 $body))),ENT_QUOTES,'UTF-8'));
264 $htmlversion = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"),
265 "<br />\n",$body))));
268 $datarray['banner'] = $banner;
269 $datarray['product'] = $product;
270 $datarray['preamble'] = $preamble;
271 $datarray['sitename'] = $sitename;
272 $datarray['siteurl'] = $siteurl;
273 $datarray['type'] = $params['type'];
274 $datarray['parent'] = $params['parent'];
275 $datarray['source_name'] = $params['source_name'];
276 $datarray['source_link'] = $params['source_link'];
277 $datarray['source_photo'] = $params['source_photo'];
278 $datarray['uid'] = $params['uid'];
279 $datarray['username'] = $params['to_name'];
280 $datarray['hsitelink'] = $hsitelink;
281 $datarray['tsitelink'] = $tsitelink;
282 $datarray['hitemlink'] = '<a href="' . $itemlink . '">' . $itemlink . '</a>';
283 $datarray['titemlink'] = $itemlink;
284 $datarray['thanks'] = $thanks;
285 $datarray['site_admin'] = $site_admin;
286 $datarray['title'] = stripslashes($title);
287 $datarray['htmlversion'] = $htmlversion;
288 $datarray['textversion'] = $textversion;
289 $datarray['subject'] = $subject;
290 $datarray['headers'] = $additional_mail_header;
292 call_hooks('enotify_mail', $datarray);
294 // load the template for private message notifications
295 $tpl = get_markup_template('email_notify_html.tpl');
296 $email_html_body = replace_macros($tpl,array(
297 '$banner' => $datarray['banner'],
298 '$product' => $datarray['product'],
299 '$preamble' => $datarray['preamble'],
300 '$sitename' => $datarray['sitename'],
301 '$siteurl' => $datarray['siteurl'],
302 '$source_name' => $datarray['source_name'],
303 '$source_link' => $datarray['source_link'],
304 '$source_photo' => $datarray['source_photo'],
305 '$username' => $datarray['to_name'],
306 '$hsitelink' => $datarray['hsitelink'],
307 '$hitemlink' => $datarray['hitemlink'],
308 '$thanks' => $datarray['thanks'],
309 '$site_admin' => $datarray['site_admin'],
310 '$title' => $datarray['title'],
311 '$htmlversion' => $datarray['htmlversion'],
314 // load the template for private message notifications
315 $tpl = get_markup_template('email_notify_text.tpl');
316 $email_text_body = replace_macros($tpl,array(
317 '$banner' => $datarray['banner'],
318 '$product' => $datarray['product'],
319 '$preamble' => $datarray['preamble'],
320 '$sitename' => $datarray['sitename'],
321 '$siteurl' => $datarray['siteurl'],
322 '$source_name' => $datarray['source_name'],
323 '$source_link' => $datarray['source_link'],
324 '$source_photo' => $datarray['source_photo'],
325 '$username' => $datarray['to_name'],
326 '$tsitelink' => $datarray['tsitelink'],
327 '$titemlink' => $datarray['titemlink'],
328 '$thanks' => $datarray['thanks'],
329 '$site_admin' => $datarray['site_admin'],
330 '$title' => $datarray['title'],
331 '$textversion' => $datarray['textversion'],
334 // logger('text: ' . $email_text_body);
336 // use the EmailNotification library to send the message
339 'fromName' => $sender_name,
340 'fromEmail' => $sender_email,
341 'replyTo' => $sender_email,
342 'toEmail' => $params['to_email'],
343 'messageSubject' => $datarray['subject'],
344 'htmlVersion' => $email_html_body,
345 'textVersion' => $email_text_body,
346 'additionalMailHeader' => $datarray['headers'],
354 require_once('include/email.php');
358 * Send a multipart/alternative message with Text and HTML versions
360 * @param fromName name of the sender
361 * @param fromEmail email fo the sender
362 * @param replyTo replyTo address to direct responses
363 * @param toEmail destination email address
364 * @param messageSubject subject of the message
365 * @param htmlVersion html version of the message
366 * @param textVersion text only version of the message
367 * @param additionalMailHeader additions to the smtp mail header
369 static public function send($params) {
371 $fromName = email_header_encode($params['fromName'],'UTF-8');
372 $messageSubject = email_header_encode($params['messageSubject'],'UTF-8');
374 // generate a mime boundary
375 $mimeBoundary =rand(0,9)."-"
376 .rand(10000000000,9999999999)."-"
377 .rand(10000000000,9999999999)."=:"
380 // generate a multipart/alternative message header
382 $params['additionalMailHeader'] .
383 "From: {$params['fromName']} <{$params['fromEmail']}>\n" .
384 "Reply-To: {$params['fromName']} <{$params['replyTo']}>\n" .
385 "MIME-Version: 1.0\n" .
386 "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
388 // assemble the final multipart message body with the text and html types included
389 $textBody = chunk_split(base64_encode($params['textVersion']));
390 $htmlBody = chunk_split(base64_encode($params['htmlVersion']));
391 $multipartMessageBody =
392 "--" . $mimeBoundary . "\n" . // plain text section
393 "Content-Type: text/plain; charset=UTF-8\n" .
394 "Content-Transfer-Encoding: base64\n\n" .
396 "--" . $mimeBoundary . "\n" . // text/html section
397 "Content-Type: text/html; charset=UTF-8\n" .
398 "Content-Transfer-Encoding: base64\n\n" .
400 "--" . $mimeBoundary . "--\n"; // message ending
404 $params['toEmail'], // send to address
405 $params['messageSubject'], // subject
406 $multipartMessageBody, // message body
407 $messageHeader // message headers
409 logger("notification: enotify::send returns " . $res, LOGGER_DEBUG);