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['type'] == NOTIFY_MAIL) {
30 $subject = sprintf( t('New mail received at %s'),$sitename);
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'];
40 if($params['type'] == NOTIFY_COMMENT) {
41 logger("notification: params = " . print_r($params, true), LOGGER_DEBUG);
43 $parent_id = $params['parent'];
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]');
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'];
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]');
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'];
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]');
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'];
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]' );
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'];
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']);
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'];
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']);
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'];
118 if($params['type'] == NOTIFY_CONFIRM) {
122 // from here on everything is in the recipients language
124 push_lang($params['language']);
126 require_once('include/html2bbcode.php');
130 $hash = random_string();
131 $r = q("SELECT `id` FROM `notify` WHERE `hash` = '%s' LIMIT 1",
135 } while($dups == true);
139 $datarray['hash'] = $hash;
140 $datarray['name'] = $params['source_name'];
141 $datarray['url'] = $params['source_link'];
142 $datarray['photo'] = $params['source_photo'];
143 $datarray['date'] = datetime_convert();
144 $datarray['uid'] = $params['uid'];
145 $datarray['link'] = $itemlink;
146 $datarray['type'] = $params['type'];
147 $datarray['verb'] = $params['verb'];
148 $datarray['otype'] = $params['otype'];
150 call_hooks('enotify_store', $datarray);
152 // create notification entry in DB
154 $r = q("insert into notify (hash,name,url,photo,date,uid,link,type,verb,otype)
155 values('%s','%s','%s','%s','%s',%d,'%s',%d,'%s','%s')",
156 dbesc($datarray['hash']),
157 dbesc($datarray['name']),
158 dbesc($datarray['url']),
159 dbesc($datarray['photo']),
160 dbesc($datarray['date']),
161 intval($datarray['uid']),
162 dbesc($datarray['link']),
163 intval($datarray['type']),
164 dbesc($datarray['verb']),
165 dbesc($datarray['otype'])
168 $r = q("select id from notify where hash = '%s' and uid = %d limit 1",
170 intval($params['uid'])
173 $notify_id = $r[0]['id'];
177 $itemlink = $a->get_baseurl() . '/notify/view/' . $notify_id;
178 $msg = replace_macros($epreamble,array('$itemlink' => $itemlink));
179 $r = q("update notify set msg = '%s' where id = %d and uid = %d limit 1",
182 intval($params['uid'])
187 // send email notification if notification preferences permit
189 require_once('bbcode.php');
190 if(intval($params['notify_flags']) & intval($params['type'])) {
192 logger('notification: sending notification email');
194 $id_for_parent = "${params['parent']}@${hostname}";
196 // Is this the first email notification for this parent item and user?
198 $r = q("select `id` from `notify-threads` where `master-parent-item` = %d and `receiver-uid` = %d limit 1",
199 intval($params['parent']),
200 intval($params['uid']) );
202 // If so, create the record of it and use a message-id smtp header.
205 logger("norify_id:" . intval($notify_id). ", parent: " . intval($params['parent']) . "uid: " .
206 intval($params['uid']), LOGGER_DEBUG);
207 $r = q("insert into `notify-threads` (`notify-id`, `master-parent-item`, `receiver-uid`, `parent-item`)
208 values(%d,%d,%d,%d)",
210 intval($params['parent']),
211 intval($params['uid']),
214 $additional_mail_header .= "Message-ID: <${id_for_parent}>\n";
215 $log_msg = "include/enotify: No previous notification found for this parent:\n" .
216 " parent: ${params['parent']}\n" . " uid : ${params['uid']}\n";
217 logger($log_msg, LOGGER_DEBUG);
220 // If not, just "follow" the thread.
223 $additional_mail_header = "References: <${id_for_parent}>\nIn-Reply-To: <${id_for_parent}>\n";
224 logger("include/enotify: There's already a notification for this parent:\n" . print_r($r, true), LOGGER_DEBUG);
229 $textversion = strip_tags(html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",
230 $body))),ENT_QUOTES,'UTF-8'));
231 $htmlversion = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"),
232 "<br />\n",$body))));
235 $datarray['banner'] = $banner;
236 $datarray['product'] = $product;
237 $datarray['preamble'] = $preamble;
238 $datarray['sitename'] = $sitename;
239 $datarray['siteurl'] = $siteurl;
240 $datarray['source_name'] = $params['source_name'];
241 $datarray['source_link'] = $params['source_link'];
242 $datarray['source_photo'] = $params['source_photo'];
243 $datarray['username'] = $params['to_name'];
244 $datarray['hsitelink'] = $hsitelink;
245 $datarray['tsitelink'] = $tsitelink;
246 $datarray['hitemlink'] = '<a href="' . $itemlink . '">' . $itemlink . '</a>';
247 $datarray['titemlink'] = $itemlink;
248 $datarray['thanks'] = $thanks;
249 $datarray['site_admin'] = $site_admin;
250 $datarray['title'] = stripslashes($title);
251 $datarray['htmlversion'] = $htmlversion;
252 $datarray['textversion'] = $textversion;
253 $datarray['subject'] = $subject;
254 $datarray['headers'] = $additional_mail_header;
256 call_hooks('enotify_mail', $datarray);
258 // load the template for private message notifications
259 $tpl = get_markup_template('email_notify_html.tpl');
260 $email_html_body = replace_macros($tpl,array(
261 '$banner' => $datarray['banner'],
262 '$product' => $datarray['product'],
263 '$preamble' => $datarray['preamble'],
264 '$sitename' => $datarray['sitename'],
265 '$siteurl' => $datarray['siteurl'],
266 '$source_name' => $datarray['source_name'],
267 '$source_link' => $datarray['source_link'],
268 '$source_photo' => $datarray['source_photo'],
269 '$username' => $datarray['to_name'],
270 '$hsitelink' => $datarray['hsitelink'],
271 '$hitemlink' => $datarray['hitemlink'],
272 '$thanks' => $datarray['thanks'],
273 '$site_admin' => $datarray['site_admin'],
274 '$title' => $datarray['title'],
275 '$htmlversion' => $datarray['htmlversion'],
278 // load the template for private message notifications
279 $tpl = get_markup_template('email_notify_text.tpl');
280 $email_text_body = replace_macros($tpl,array(
281 '$banner' => $datarray['banner'],
282 '$product' => $datarray['product'],
283 '$preamble' => $datarray['preamble'],
284 '$sitename' => $datarray['sitename'],
285 '$siteurl' => $datarray['siteurl'],
286 '$source_name' => $datarray['source_name'],
287 '$source_link' => $datarray['source_link'],
288 '$source_photo' => $datarray['source_photo'],
289 '$username' => $datarray['to_name'],
290 '$tsitelink' => $datarray['tsitelink'],
291 '$titemlink' => $datarray['titemlink'],
292 '$thanks' => $datarray['thanks'],
293 '$site_admin' => $datarray['site_admin'],
294 '$title' => $datarray['title'],
295 '$textversion' => $datarray['textversion'],
298 // logger('text: ' . $email_text_body);
300 // use the EmailNotification library to send the message
303 'fromName' => $sender_name,
304 'fromEmail' => $sender_email,
305 'replyTo' => $sender_email,
306 'toEmail' => $params['to_email'],
307 'messageSubject' => $datarray['subject'],
308 'htmlVersion' => $email_html_body,
309 'textVersion' => $email_text_body,
310 'additionalMailHeader' => $datarray['headers'],
318 require_once('include/email.php');
322 * Send a multipart/alternative message with Text and HTML versions
324 * @param fromName name of the sender
325 * @param fromEmail email fo the sender
326 * @param replyTo replyTo address to direct responses
327 * @param toEmail destination email address
328 * @param messageSubject subject of the message
329 * @param htmlVersion html version of the message
330 * @param textVersion text only version of the message
331 * @param additionalMailHeader additions to the smtp mail header
333 static public function send($params) {
335 $fromName = email_header_encode($params['fromName'],'UTF-8');
336 $messageSubject = email_header_encode($params['messageSubject'],'UTF-8');
338 // generate a mime boundary
339 $mimeBoundary =rand(0,9)."-"
340 .rand(10000000000,9999999999)."-"
341 .rand(10000000000,9999999999)."=:"
344 // generate a multipart/alternative message header
346 $params['additionalMailHeader'] .
347 "From: {$params['fromName']} <{$params['fromEmail']}>\n" .
348 "Reply-To: {$params['fromName']} <{$params['replyTo']}>\n" .
349 "MIME-Version: 1.0\n" .
350 "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
352 // assemble the final multipart message body with the text and html types included
353 $textBody = chunk_split(base64_encode($params['textVersion']));
354 $htmlBody = chunk_split(base64_encode($params['htmlVersion']));
355 $multipartMessageBody =
356 "--" . $mimeBoundary . "\n" . // plain text section
357 "Content-Type: text/plain; charset=UTF-8\n" .
358 "Content-Transfer-Encoding: base64\n\n" .
360 "--" . $mimeBoundary . "\n" . // text/html section
361 "Content-Type: text/html; charset=UTF-8\n" .
362 "Content-Transfer-Encoding: base64\n\n" .
364 "--" . $mimeBoundary . "--\n"; // message ending
368 $params['toEmail'], // send to address
369 $params['messageSubject'], // subject
370 $multipartMessageBody, // message body
371 $messageHeader // message headers
373 logger("notification: enotify::send returns " . $res, LOGGER_DEBUG);