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 $sender_email = t('noreply') . '@' . $a->get_hostname();
18 if(array_key_exists('item',$params)) {
19 $title = $params['item']['title'];
20 $body = $params['item']['body'];
26 if($params['type'] == NOTIFY_MAIL) {
28 $subject = sprintf( t('New mail received at %s'),$sitename);
30 $preamble = sprintf( t('%s sent you a new private message at %s.'),$params['source_name'],$sitename);
31 $epreamble = sprintf( t('%s sent you %s.'),'[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=' . $siteurl . '/message]' . t('a private message') . '[/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';
38 if($params['type'] == NOTIFY_COMMENT) {
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 %s you have been following.'), '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=' . $params['link'] . ']' . t('an item/conversation') . '[/url]');
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'];
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 %s') , '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=' . $params['link'] . ']' . t('your profile wall.') . '[/url]');
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'];
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 %s.') , '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=' . $params['link'] . ']' . t('tagged you') . '[/url]');
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'];
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 %s') , '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]', '[url=' . $params['link'] . ']' . t('your post') . '[/url]' );
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'];
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 %s from %s.'), '[url=' . $params['link'] . ']' . t('an introduction') . '[/url]' , '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
84 $body = sprintf( t('You may visit their profile at %s'),$params['source_link']);
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'];
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 %s for %s from %s.'),
96 '[url=' . $params['link'] . ']' . t('a friend suggestion') . '[/url]',
97 '[url=' . $params['item']['url'] . ']' . $params['item']['name'] . '[/url]',
98 '[url=' . $params['source_link'] . ']' . $params['source_name'] . '[/url]');
99 $body = t('Name:') . ' ' . $params['item']['name'] . "\n";
100 $body .= t('Photo:') . ' ' . $params['item']['photo'] . "\n";
101 $body .= sprintf( t('You may visit their profile at %s'),$params['item']['url']);
103 $sitelink = t('Please visit %s to approve or reject the suggestion.');
104 $tsitelink = sprintf( $sitelink, $siteurl );
105 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '">' . $sitename . '</a>');
106 $itemlink = $params['link'];
109 if($params['type'] == NOTIFY_CONFIRM) {
113 // from here on everything is in the recipients language
115 push_lang($params['language']);
117 require_once('include/html2bbcode.php');
121 $hash = random_string();
122 $r = q("SELECT `id` FROM `notify` WHERE `hash` = '%s' LIMIT 1",
126 } while($dups == true);
131 // create notification entry in DB
133 $r = q("insert into notify (hash,name,url,photo,date,msg,uid,link,type,verb,otype)
134 values('%s','%s','%s','%s','%s','%s',%d,'%s',%d,'%s','%s')",
136 dbesc($params['source_name']),
137 dbesc($params['source_link']),
138 dbesc($params['source_photo']),
139 dbesc(datetime_convert()),
141 intval($params['uid']),
143 intval($params['type']),
144 dbesc($params['verb']),
145 dbesc($params['otype'])
148 $r = q("select id from notify where hash = '%s' and uid = %d limit 1",
150 intval($params['uid'])
153 $notify_id = $r[0]['id'];
157 $itemlink = $a->get_baseurl() . '/notify/view/' . $notify_id;
159 // send email notification if notification preferences permit
161 require_once('bbcode.php');
162 if(intval($params['notify_flags']) & intval($params['type'])) {
164 logger('notification: sending notification email');
167 $textversion = strip_tags(html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",
168 $body))),ENT_QUOTES,'UTF-8'));
169 $htmlversion = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"),
170 "<br />\n",$body))));
172 // load the template for private message notifications
173 $tpl = get_markup_template('email_notify_html.tpl');
174 $email_html_body = replace_macros($tpl,array(
175 '$banner' => $banner,
176 '$product' => $product,
177 '$preamble' => $preamble,
178 '$sitename' => $sitename,
179 '$siteurl' => $siteurl,
180 '$source_name' => $params['source_name'],
181 '$source_link' => $params['source_link'],
182 '$source_photo' => $params['source_photo'],
183 '$username' => $params['to_name'],
184 '$hsitelink' => $hsitelink,
185 '$itemlink' => '<a href="' . $itemlink . '">' . $itemlink . '</a>',
186 '$thanks' => $thanks,
187 '$site_admin' => $site_admin,
188 '$title' => stripslashes($title),
189 '$htmlversion' => $htmlversion,
192 // load the template for private message notifications
193 $tpl = get_markup_template('email_notify_text.tpl');
194 $email_text_body = replace_macros($tpl,array(
195 '$banner' => $banner,
196 '$product' => $product,
197 '$preamble' => $preamble,
198 '$sitename' => $sitename,
199 '$siteurl' => $siteurl,
200 '$source_name' => $params['source_name'],
201 '$source_link' => $params['source_link'],
202 '$source_photo' => $params['source_photo'],
203 '$username' => $params['to_name'],
204 '$tsitelink' => $tsitelink,
205 '$itemlink' => $itemlink,
206 '$thanks' => $thanks,
207 '$site_admin' => $site_admin,
208 '$title' => stripslashes($title),
209 '$textversion' => $textversion,
212 // logger('text: ' . $email_text_body);
214 // use the EmailNotification library to send the message
217 'fromName' => $sender_name,
218 'fromEmail' => $sender_email,
219 'replyTo' => $sender_email,
220 'toEmail' => $params['to_email'],
221 'messageSubject' => $subject,
222 'htmlVersion' => $email_html_body,
223 'textVersion' => $email_text_body
231 require_once('include/email.php');
235 * Send a multipart/alternative message with Text and HTML versions
237 * @param fromName name of the sender
238 * @param fromEmail email fo the sender
239 * @param replyTo replyTo address to direct responses
240 * @param toEmail destination email address
241 * @param messageSubject subject of the message
242 * @param htmlVersion html version of the message
243 * @param textVersion text only version of the message
245 static public function send($params) {
247 $fromName = email_header_encode($params['fromName'],'UTF-8');
248 $messageSubject = email_header_encode($params['messageSubject'],'UTF-8');
250 // generate a mime boundary
251 $mimeBoundary =rand(0,9)."-"
252 .rand(10000000000,9999999999)."-"
253 .rand(10000000000,9999999999)."=:"
256 // generate a multipart/alternative message header
258 "From: {$params['fromName']} <{$params['fromEmail']}>\n" .
259 "Reply-To: {$params['fromName']} <{$params['replyTo']}>\n" .
260 "MIME-Version: 1.0\n" .
261 "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
263 // assemble the final multipart message body with the text and html types included
264 $textBody = chunk_split(base64_encode($params['textVersion']));
265 $htmlBody = chunk_split(base64_encode($params['htmlVersion']));
266 $multipartMessageBody =
267 "--" . $mimeBoundary . "\n" . // plain text section
268 "Content-Type: text/plain; charset=UTF-8\n" .
269 "Content-Transfer-Encoding: base64\n\n" .
271 "--" . $mimeBoundary . "\n" . // text/html section
272 "Content-Type: text/html; charset=UTF-8\n" .
273 "Content-Transfer-Encoding: base64\n\n" .
275 "--" . $mimeBoundary . "--\n"; // message ending
279 $params['toEmail'], // send to address
280 $params['messageSubject'], // subject
281 $multipartMessageBody, // message body
282 $messageHeader // message headers
284 logger("notification: enotify::send returns " . $res, LOGGER_DEBUG);