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 // e.g. "your post", "David's photo", etc.
29 $possess_desc = t('%s <!item_type!>');
31 if($params['type'] == NOTIFY_MAIL) {
33 $subject = sprintf( t('[Friendica:Notify] New mail received at %s'),$sitename);
35 $preamble = sprintf( t('%s sent you a new private message at %s.'),$params['source_name'],$sitename);
36 $epreamble = sprintf( t('%s sent you %s.'),'[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=$itemlink]' . t('a private message') . '[/url]');
37 $sitelink = t('Please visit %s to view and/or reply to your private messages.');
38 $tsitelink = sprintf( $sitelink, $siteurl . '/message/' . $params['item']['id'] );
39 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '/message/' . $params['item']['id'] . '">' . $sitename . '</a>');
40 $itemlink = $siteurl . '/message/' . $params['item']['id'];
43 if($params['type'] == NOTIFY_COMMENT) {
44 // logger("notification: params = " . print_r($params, true), LOGGER_DEBUG);
46 $parent_id = $params['parent'];
49 // if it's a post figure out who's post it is.
53 if($params['otype'] === 'item' && $parent_id) {
54 $p = q("select * from item where id = %d and uid = %d limit 1",
56 intval($params['uid'])
60 $possess_desc = str_replace('<!item_type!>',item_post_type($p[0]),$possess_desc);
63 $dest_str = sprintf($possess_desc,'a');
65 // "George Bull's post"
67 $dest_str = sprintf($possess_desc,sprintf( t("%s's"),$p[0]['author-name']));
70 if($p[0]['owner-name'] == $p[0]['author-name'] && $p[0]['wall'])
71 $dest_str = sprintf($possess_desc, t('your') );
73 // Some mail softwares relies on subject field for threading.
74 // So, we cannot have different subjects for notifications of the same thread.
75 // Before this we have the name of the replier on the subject rendering
76 // differents subjects for messages on the same thread.
78 $subject = sprintf( t('[Friendica:Notify] Comment to conversation #%d by %s'), $parent_id, $params['source_name']);
79 $preamble = sprintf( t('%s commented on an item/conversation you have been following.'), $params['source_name']);
80 $epreamble = sprintf( t('%s commented on %s.'), '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=$itemlink]' . $dest_str . '[/url]');
82 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
83 $tsitelink = sprintf( $sitelink, $siteurl );
84 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
85 $itemlink = $params['link'];
88 if($params['type'] == NOTIFY_WALL) {
89 $subject = sprintf( t('[Friendica:Notify] %s posted to your profile wall') , $params['source_name']);
91 $preamble = sprintf( t('%s posted to your profile wall at %s') , $params['source_name'], $sitename);
93 $epreamble = sprintf( t('%s posted to %s') , '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=$itemlink]' . t('your profile wall.') . '[/url]');
95 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
96 $tsitelink = sprintf( $sitelink, $siteurl );
97 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
98 $itemlink = $params['link'];
101 if($params['type'] == NOTIFY_TAGSELF) {
102 $subject = sprintf( t('[Friendica:Notify] %s tagged you') , $params['source_name']);
103 $preamble = sprintf( t('%s tagged you at %s') , $params['source_name'], $sitename);
104 $epreamble = sprintf( t('%s %s.') , '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=' . $params['link'] . ']' . t('tagged you') . '[/url]');
106 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
107 $tsitelink = sprintf( $sitelink, $siteurl );
108 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
109 $itemlink = $params['link'];
112 if($params['type'] == NOTIFY_TAGSHARE) {
113 $subject = sprintf( t('[Friendica:Notify] %s tagged your post') , $params['source_name']);
114 $preamble = sprintf( t('%s tagged your post at %s') , $params['source_name'], $sitename);
115 $epreamble = sprintf( t('%s tagged %s') , '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=$itemlink]' . t('your post') . '[/url]' );
117 $sitelink = t('Please visit %s to view and/or reply to the conversation.');
118 $tsitelink = sprintf( $sitelink, $siteurl );
119 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
120 $itemlink = $params['link'];
123 if($params['type'] == NOTIFY_INTRO) {
124 $subject = sprintf( t('[Friendica:Notify] Introduction received'));
125 $preamble = sprintf( t('You\'ve received an introduction from \'%s\' at %s'), $params['source_name'], $sitename);
126 $epreamble = sprintf( t('You\'ve received %s from %s.'), '[url=$itemlink]' . t('an introduction') . '[/url]' , '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
127 $body = sprintf( t('You may visit their profile at %s'),$params['source_link']);
129 $sitelink = t('Please visit %s to approve or reject the introduction.');
130 $tsitelink = sprintf( $sitelink, $siteurl );
131 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
132 $itemlink = $params['link'];
135 if($params['type'] == NOTIFY_SUGGEST) {
136 $subject = sprintf( t('[Friendica:Notify] Friend suggestion received'));
137 $preamble = sprintf( t('You\'ve received a friend suggestion from \'%s\' at %s'), $params['source_name'], $sitename);
138 $epreamble = sprintf( t('You\'ve received %s for %s from %s.'),
139 '[url=$itemlink]' . t('a friend suggestion') . '[/url]',
140 '[url=' . $params['item']['url'] . ']' . $params['item']['name'] . '[/url]',
141 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
142 $body = t('Name:') . ' ' . $params['item']['name'] . "\n";
143 $body .= t('Photo:') . ' ' . $params['item']['photo'] . "\n";
144 $body .= sprintf( t('You may visit their profile at %s'),$params['item']['url']);
146 $sitelink = t('Please visit %s to approve or reject the suggestion.');
147 $tsitelink = sprintf( $sitelink, $siteurl );
148 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
149 $itemlink = $params['link'];
152 if($params['type'] == NOTIFY_CONFIRM) {
156 // from here on everything is in the recipients language
158 push_lang($params['language']);
160 require_once('include/html2bbcode.php');
164 $hash = random_string();
165 $r = q("SELECT `id` FROM `notify` WHERE `hash` = '%s' LIMIT 1",
169 } while($dups == true);
173 $datarray['hash'] = $hash;
174 $datarray['name'] = $params['source_name'];
175 $datarray['url'] = $params['source_link'];
176 $datarray['photo'] = $params['source_photo'];
177 $datarray['date'] = datetime_convert();
178 $datarray['uid'] = $params['uid'];
179 $datarray['link'] = $itemlink;
180 $datarray['parent'] = $parent_id;
181 $datarray['type'] = $params['type'];
182 $datarray['verb'] = $params['verb'];
183 $datarray['otype'] = $params['otype'];
185 call_hooks('enotify_store', $datarray);
187 // create notification entry in DB
189 $r = q("insert into notify (hash,name,url,photo,date,uid,link,parent,type,verb,otype)
190 values('%s','%s','%s','%s','%s',%d,'%s',%d,%d,'%s','%s')",
191 dbesc($datarray['hash']),
192 dbesc($datarray['name']),
193 dbesc($datarray['url']),
194 dbesc($datarray['photo']),
195 dbesc($datarray['date']),
196 intval($datarray['uid']),
197 dbesc($datarray['link']),
198 intval($datarray['parent']),
199 intval($datarray['type']),
200 dbesc($datarray['verb']),
201 dbesc($datarray['otype'])
204 $r = q("select id from notify where hash = '%s' and uid = %d limit 1",
206 intval($params['uid'])
209 $notify_id = $r[0]['id'];
213 $itemlink = $a->get_baseurl() . '/notify/view/' . $notify_id;
214 $msg = replace_macros($epreamble,array('$itemlink' => $itemlink));
215 $r = q("update notify set msg = '%s' where id = %d and uid = %d limit 1",
218 intval($params['uid'])
223 // send email notification if notification preferences permit
225 require_once('bbcode.php');
226 if(intval($params['notify_flags']) & intval($params['type'])) {
228 logger('notification: sending notification email');
230 $id_for_parent = "${params['parent']}@${hostname}";
232 // Is this the first email notification for this parent item and user?
234 $r = q("select `id` from `notify-threads` where `master-parent-item` = %d and `receiver-uid` = %d limit 1",
235 intval($params['parent']),
236 intval($params['uid']) );
238 // If so, create the record of it and use a message-id smtp header.
241 logger("norify_id:" . intval($notify_id). ", parent: " . intval($params['parent']) . "uid: " .
242 intval($params['uid']), LOGGER_DEBUG);
243 $r = q("insert into `notify-threads` (`notify-id`, `master-parent-item`, `receiver-uid`, `parent-item`)
244 values(%d,%d,%d,%d)",
246 intval($params['parent']),
247 intval($params['uid']),
250 $additional_mail_header .= "Message-ID: <${id_for_parent}>\n";
251 $log_msg = "include/enotify: No previous notification found for this parent:\n" .
252 " parent: ${params['parent']}\n" . " uid : ${params['uid']}\n";
253 logger($log_msg, LOGGER_DEBUG);
256 // If not, just "follow" the thread.
259 $additional_mail_header = "References: <${id_for_parent}>\nIn-Reply-To: <${id_for_parent}>\n";
260 logger("include/enotify: There's already a notification for this parent:\n" . print_r($r, true), LOGGER_DEBUG);
265 $textversion = strip_tags(html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",
266 $body))),ENT_QUOTES,'UTF-8'));
267 $htmlversion = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"),
268 "<br />\n",$body))));
271 $datarray['banner'] = $banner;
272 $datarray['product'] = $product;
273 $datarray['preamble'] = $preamble;
274 $datarray['sitename'] = $sitename;
275 $datarray['siteurl'] = $siteurl;
276 $datarray['type'] = $params['type'];
277 $datarray['parent'] = $params['parent'];
278 $datarray['source_name'] = $params['source_name'];
279 $datarray['source_link'] = $params['source_link'];
280 $datarray['source_photo'] = $params['source_photo'];
281 $datarray['uid'] = $params['uid'];
282 $datarray['username'] = $params['to_name'];
283 $datarray['hsitelink'] = $hsitelink;
284 $datarray['tsitelink'] = $tsitelink;
285 $datarray['hitemlink'] = '<a href="' . $itemlink . '">' . $itemlink . '</a>';
286 $datarray['titemlink'] = $itemlink;
287 $datarray['thanks'] = $thanks;
288 $datarray['site_admin'] = $site_admin;
289 $datarray['title'] = stripslashes($title);
290 $datarray['htmlversion'] = $htmlversion;
291 $datarray['textversion'] = $textversion;
292 $datarray['subject'] = $subject;
293 $datarray['headers'] = $additional_mail_header;
295 call_hooks('enotify_mail', $datarray);
297 // load the template for private message notifications
298 $tpl = get_markup_template('email_notify_html.tpl');
299 $email_html_body = replace_macros($tpl,array(
300 '$banner' => $datarray['banner'],
301 '$product' => $datarray['product'],
302 '$preamble' => $datarray['preamble'],
303 '$sitename' => $datarray['sitename'],
304 '$siteurl' => $datarray['siteurl'],
305 '$source_name' => $datarray['source_name'],
306 '$source_link' => $datarray['source_link'],
307 '$source_photo' => $datarray['source_photo'],
308 '$username' => $datarray['to_name'],
309 '$hsitelink' => $datarray['hsitelink'],
310 '$hitemlink' => $datarray['hitemlink'],
311 '$thanks' => $datarray['thanks'],
312 '$site_admin' => $datarray['site_admin'],
313 '$title' => $datarray['title'],
314 '$htmlversion' => $datarray['htmlversion'],
317 // load the template for private message notifications
318 $tpl = get_markup_template('email_notify_text.tpl');
319 $email_text_body = replace_macros($tpl,array(
320 '$banner' => $datarray['banner'],
321 '$product' => $datarray['product'],
322 '$preamble' => $datarray['preamble'],
323 '$sitename' => $datarray['sitename'],
324 '$siteurl' => $datarray['siteurl'],
325 '$source_name' => $datarray['source_name'],
326 '$source_link' => $datarray['source_link'],
327 '$source_photo' => $datarray['source_photo'],
328 '$username' => $datarray['to_name'],
329 '$tsitelink' => $datarray['tsitelink'],
330 '$titemlink' => $datarray['titemlink'],
331 '$thanks' => $datarray['thanks'],
332 '$site_admin' => $datarray['site_admin'],
333 '$title' => $datarray['title'],
334 '$textversion' => $datarray['textversion'],
337 // logger('text: ' . $email_text_body);
339 // use the EmailNotification library to send the message
342 'fromName' => $sender_name,
343 'fromEmail' => $sender_email,
344 'replyTo' => $sender_email,
345 'toEmail' => $params['to_email'],
346 'messageSubject' => $datarray['subject'],
347 'htmlVersion' => $email_html_body,
348 'textVersion' => $email_text_body,
349 'additionalMailHeader' => $datarray['headers'],
357 require_once('include/email.php');
361 * Send a multipart/alternative message with Text and HTML versions
363 * @param fromName name of the sender
364 * @param fromEmail email fo the sender
365 * @param replyTo replyTo address to direct responses
366 * @param toEmail destination email address
367 * @param messageSubject subject of the message
368 * @param htmlVersion html version of the message
369 * @param textVersion text only version of the message
370 * @param additionalMailHeader additions to the smtp mail header
372 static public function send($params) {
374 $fromName = email_header_encode($params['fromName'],'UTF-8');
375 $messageSubject = email_header_encode($params['messageSubject'],'UTF-8');
377 // generate a mime boundary
378 $mimeBoundary =rand(0,9)."-"
379 .rand(10000000000,9999999999)."-"
380 .rand(10000000000,9999999999)."=:"
383 // generate a multipart/alternative message header
385 $params['additionalMailHeader'] .
386 "From: {$params['fromName']} <{$params['fromEmail']}>\n" .
387 "Reply-To: {$params['fromName']} <{$params['replyTo']}>\n" .
388 "MIME-Version: 1.0\n" .
389 "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
391 // assemble the final multipart message body with the text and html types included
392 $textBody = chunk_split(base64_encode($params['textVersion']));
393 $htmlBody = chunk_split(base64_encode($params['htmlVersion']));
394 $multipartMessageBody =
395 "--" . $mimeBoundary . "\n" . // plain text section
396 "Content-Type: text/plain; charset=UTF-8\n" .
397 "Content-Transfer-Encoding: base64\n\n" .
399 "--" . $mimeBoundary . "\n" . // text/html section
400 "Content-Type: text/html; charset=UTF-8\n" .
401 "Content-Transfer-Encoding: base64\n\n" .
403 "--" . $mimeBoundary . "--\n"; // message ending
407 $params['toEmail'], // send to address
408 $params['messageSubject'], // subject
409 $multipartMessageBody, // message body
410 $messageHeader // message headers
412 logger("notification: enotify::send returns " . $res, LOGGER_DEBUG);