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);
138 // create notification entry in DB
140 $r = q("insert into notify (hash,name,url,photo,date,uid,link,type,verb,otype)
141 values('%s','%s','%s','%s','%s',%d,'%s',%d,'%s','%s')",
143 dbesc($params['source_name']),
144 dbesc($params['source_link']),
145 dbesc($params['source_photo']),
146 dbesc(datetime_convert()),
147 intval($params['uid']),
149 intval($params['type']),
150 dbesc($params['verb']),
151 dbesc($params['otype'])
154 $r = q("select id from notify where hash = '%s' and uid = %d limit 1",
156 intval($params['uid'])
159 $notify_id = $r[0]['id'];
163 $itemlink = $a->get_baseurl() . '/notify/view/' . $notify_id;
164 $msg = replace_macros($epreamble,array('$itemlink' => $itemlink));
165 $r = q("update notify set msg = '%s' where id = %d and uid = %d limit 1",
168 intval($params['uid'])
173 // send email notification if notification preferences permit
175 require_once('bbcode.php');
176 if(intval($params['notify_flags']) & intval($params['type'])) {
178 logger('notification: sending notification email');
180 $id_for_parent = "${params['parent']}@${hostname}";
182 // Is this the first email notification for this parent item and user?
184 $r = q("select `id` from `notify-threads` where `master-parent-item` = %d and `receiver-uid` = %d limit 1",
185 intval($params['parent']),
186 intval($params['uid']) );
188 // If so, create the record of it and use a message-id smtp header.
191 logger("norify_id:" . intval($notify_id). ", parent: " . intval($params['parent']) . "uid: " .
192 intval($params['uid']), LOGGER_DEBUG);
193 $r = q("insert into `notify-threads` (`notify-id`, `master-parent-item`, `receiver-uid`, `parent-item`)
194 values(%d,%d,%d,%d)",
196 intval($params['parent']),
197 intval($params['uid']),
200 $additional_mail_header .= "Message-ID: <${id_for_parent}>\n";
201 $log_msg = "include/enotify: No previous notification found for this parent:\n" .
202 " parent: ${params['parent']}\n" . " uid : ${params['uid']}\n";
203 logger($log_msg, LOGGER_DEBUG);
206 // If not, just "follow" the thread.
209 $additional_mail_header = "References: <${id_for_parent}>\nIn-Reply-To: <${id_for_parent}>\n";
210 logger("include/enotify: There's already a notification for this parent:\n" . print_r($r, true), LOGGER_DEBUG);
215 $textversion = strip_tags(html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",
216 $body))),ENT_QUOTES,'UTF-8'));
217 $htmlversion = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"),
218 "<br />\n",$body))));
220 // load the template for private message notifications
221 $tpl = get_markup_template('email_notify_html.tpl');
222 $email_html_body = replace_macros($tpl,array(
223 '$banner' => $banner,
224 '$product' => $product,
225 '$preamble' => $preamble,
226 '$sitename' => $sitename,
227 '$siteurl' => $siteurl,
228 '$source_name' => $params['source_name'],
229 '$source_link' => $params['source_link'],
230 '$source_photo' => $params['source_photo'],
231 '$username' => $params['to_name'],
232 '$hsitelink' => $hsitelink,
233 '$itemlink' => '<a href="' . $itemlink . '">' . $itemlink . '</a>',
234 '$thanks' => $thanks,
235 '$site_admin' => $site_admin,
236 '$title' => stripslashes($title),
237 '$htmlversion' => $htmlversion,
240 // load the template for private message notifications
241 $tpl = get_markup_template('email_notify_text.tpl');
242 $email_text_body = replace_macros($tpl,array(
243 '$banner' => $banner,
244 '$product' => $product,
245 '$preamble' => $preamble,
246 '$sitename' => $sitename,
247 '$siteurl' => $siteurl,
248 '$source_name' => $params['source_name'],
249 '$source_link' => $params['source_link'],
250 '$source_photo' => $params['source_photo'],
251 '$username' => $params['to_name'],
252 '$tsitelink' => $tsitelink,
253 '$itemlink' => $itemlink,
254 '$thanks' => $thanks,
255 '$site_admin' => $site_admin,
256 '$title' => stripslashes($title),
257 '$textversion' => $textversion,
260 // logger('text: ' . $email_text_body);
262 // use the EmailNotification library to send the message
265 'fromName' => $sender_name,
266 'fromEmail' => $sender_email,
267 'replyTo' => $sender_email,
268 'toEmail' => $params['to_email'],
269 'messageSubject' => $subject,
270 'htmlVersion' => $email_html_body,
271 'textVersion' => $email_text_body,
272 'additionalMailHeader' => $additional_mail_header,
280 require_once('include/email.php');
284 * Send a multipart/alternative message with Text and HTML versions
286 * @param fromName name of the sender
287 * @param fromEmail email fo the sender
288 * @param replyTo replyTo address to direct responses
289 * @param toEmail destination email address
290 * @param messageSubject subject of the message
291 * @param htmlVersion html version of the message
292 * @param textVersion text only version of the message
293 * @param additionalMailHeader additions to the smtp mail header
295 static public function send($params) {
297 $fromName = email_header_encode($params['fromName'],'UTF-8');
298 $messageSubject = email_header_encode($params['messageSubject'],'UTF-8');
300 // generate a mime boundary
301 $mimeBoundary =rand(0,9)."-"
302 .rand(10000000000,9999999999)."-"
303 .rand(10000000000,9999999999)."=:"
306 // generate a multipart/alternative message header
308 $params['additionalMailHeader'] .
309 "From: {$params['fromName']} <{$params['fromEmail']}>\n" .
310 "Reply-To: {$params['fromName']} <{$params['replyTo']}>\n" .
311 "MIME-Version: 1.0\n" .
312 "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
314 // assemble the final multipart message body with the text and html types included
315 $textBody = chunk_split(base64_encode($params['textVersion']));
316 $htmlBody = chunk_split(base64_encode($params['htmlVersion']));
317 $multipartMessageBody =
318 "--" . $mimeBoundary . "\n" . // plain text section
319 "Content-Type: text/plain; charset=UTF-8\n" .
320 "Content-Transfer-Encoding: base64\n\n" .
322 "--" . $mimeBoundary . "\n" . // text/html section
323 "Content-Type: text/html; charset=UTF-8\n" .
324 "Content-Transfer-Encoding: base64\n\n" .
326 "--" . $mimeBoundary . "--\n"; // message ending
330 $params['toEmail'], // send to address
331 $params['messageSubject'], // subject
332 $multipartMessageBody, // message body
333 $messageHeader // message headers
335 logger("notification: enotify::send returns " . $res, LOGGER_DEBUG);