3 require_once('include/acl_selectors.php');
4 require_once('include/message.php');
6 function message_init(&$a) {
9 'label' => t('New Message'),
10 'url' => $a->get_baseurl(true) . '/message/new',
11 'sel'=> ($a->argv[1] == 'new'),
14 $tpl = get_markup_template('message_side.tpl');
15 $a->page['aside'] = replace_macros($tpl, array(
19 $base = $a->get_baseurl();
21 $head_tpl = get_markup_template('message-head.tpl');
22 $a->page['htmlhead'] .= replace_macros($head_tpl,array(
23 '$baseurl' => $a->get_baseurl(true),
27 $end_tpl = get_markup_template('message-end.tpl');
28 $a->page['end'] .= replace_macros($end_tpl,array(
29 '$baseurl' => $a->get_baseurl(true),
35 function message_post(&$a) {
38 notice( t('Permission denied.') . EOL);
42 $replyto = ((x($_REQUEST,'replyto')) ? notags(trim($_REQUEST['replyto'])) : '');
43 $subject = ((x($_REQUEST,'subject')) ? notags(trim($_REQUEST['subject'])) : '');
44 $body = ((x($_REQUEST,'body')) ? escape_tags(trim($_REQUEST['body'])) : '');
45 $recipient = ((x($_REQUEST,'messageto')) ? intval($_REQUEST['messageto']) : 0 );
47 // Work around doubled linefeeds in Tinymce 3.5b2
49 /* $plaintext = intval(get_pconfig(local_user(),'system','plaintext') && !feature_enabled(local_user(),'richtext'));
51 $body = fix_mce_lf($body);
53 $plaintext = intval(!feature_enabled(local_user(),'richtext'));
55 $body = fix_mce_lf($body);
58 $ret = send_message($recipient, $body, $subject, $replyto);
63 notice( t('No recipient selected.') . EOL );
67 notice( t('Unable to locate contact information.') . EOL );
70 notice( t('Message could not be sent.') . EOL );
73 notice( t('Message collection failure.') . EOL );
76 info( t('Message sent.') . EOL );
79 // fake it to go back to the input form if no recipient listed
88 // Note: the code in 'item_extract_images' and 'item_redir_and_replace_images'
89 // is identical to the code in include/conversation.php
90 if(! function_exists('item_extract_images')) {
91 function item_extract_images($body) {
93 $saved_image = array();
98 $img_start = strpos($orig_body, '[img');
99 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
100 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
101 while(($img_st_close !== false) && ($img_end !== false)) {
103 $img_st_close++; // make it point to AFTER the closing bracket
104 $img_end += $img_start;
106 if(! strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
107 // This is an embedded image
109 $saved_image[$cnt] = substr($orig_body, $img_start + $img_st_close, $img_end - ($img_start + $img_st_close));
110 $new_body = $new_body . substr($orig_body, 0, $img_start) . '[!#saved_image' . $cnt . '#!]';
115 $new_body = $new_body . substr($orig_body, 0, $img_end + strlen('[/img]'));
117 $orig_body = substr($orig_body, $img_end + strlen('[/img]'));
119 if($orig_body === false) // in case the body ends on a closing image tag
122 $img_start = strpos($orig_body, '[img');
123 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
124 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
127 $new_body = $new_body . $orig_body;
129 return array('body' => $new_body, 'images' => $saved_image);
132 if(! function_exists('item_redir_and_replace_images')) {
133 function item_redir_and_replace_images($body, $images, $cid) {
138 for($i = 0; $i < count($images); $i++) {
139 $search = '/\[url\=(.*?)\]\[!#saved_image' . $i . '#!\]\[\/url\]' . '/is';
140 $replace = '[url=' . z_path() . '/redir/' . $cid
141 . '?f=1&url=' . '$1' . '][!#saved_image' . $i . '#!][/url]' ;
143 $img_end = strpos($origbody, '[!#saved_image' . $i . '#!][/url]') + strlen('[!#saved_image' . $i . '#!][/url]');
144 $process_part = substr($origbody, 0, $img_end);
145 $origbody = substr($origbody, $img_end);
147 $process_part = preg_replace($search, $replace, $process_part);
148 $newbody = $newbody . $process_part;
150 $newbody = $newbody . $origbody;
153 foreach($images as $image) {
154 // We're depending on the property of 'foreach' (specified on the PHP website) that
155 // it loops over the array starting from the first element and going sequentially
156 // to the last element
157 $newbody = str_replace('[!#saved_image' . $cnt . '#!]', '[img]' . $image . '[/img]', $newbody);
166 function message_content(&$a) {
169 nav_set_selected('messages');
172 notice( t('Permission denied.') . EOL);
176 $myprofile = $a->get_baseurl(true) . '/profile/' . $a->user['nickname'];
178 $tpl = get_markup_template('mail_head.tpl');
179 $header = replace_macros($tpl, array(
180 '$messages' => t('Messages'),
181 '$tab_content' => $tab_content
185 if(($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) {
186 if(! intval($a->argv[2]))
189 if($cmd === 'drop') {
190 $r = q("DELETE FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
195 info( t('Message deleted.') . EOL );
197 goaway($a->get_baseurl(true) . '/message' );
200 $r = q("SELECT `parent-uri`,`convid` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
205 $parent = $r[0]['parent-uri'];
206 $convid = $r[0]['convid'];
208 $r = q("DELETE FROM `mail` WHERE `parent-uri` = '%s' AND `uid` = %d ",
213 // remove diaspora conversation pointer
214 // Actually if we do this, we can never receive another reply to that conversation,
215 // as we will never again have the info we need to re-create it.
216 // We'll just have to orphan it.
219 // q("delete from conv where id = %d limit 1",
225 info( t('Conversation removed.') . EOL );
227 goaway($a->get_baseurl(true) . '/message' );
232 if(($a->argc > 1) && ($a->argv[1] === 'new')) {
236 /* $plaintext = false;
237 if(intval(get_pconfig(local_user(),'system','plaintext')))
240 if( local_user() && feature_enabled(local_user(),'richtext') )
244 $tpl = get_markup_template('msg-header.tpl');
245 $a->page['htmlhead'] .= replace_macros($tpl, array(
246 '$baseurl' => $a->get_baseurl(true),
247 '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
248 '$nickname' => $a->user['nickname'],
249 '$linkurl' => t('Please enter a link URL:')
252 $tpl = get_markup_template('msg-end.tpl');
253 $a->page['end'] .= replace_macros($tpl, array(
254 '$baseurl' => $a->get_baseurl(true),
255 '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
256 '$nickname' => $a->user['nickname'],
257 '$linkurl' => t('Please enter a link URL:')
260 $preselect = (isset($a->argv[2])?array($a->argv[2]):false);
263 $prename = $preurl = $preid = '';
266 $r = q("select name, url, id from contact where uid = %d and id = %d limit 1",
267 intval(local_user()),
271 $prename = $r[0]['name'];
272 $preurl = $r[0]['url'];
273 $preid = $r[0]['id'];
277 $prefill = (($preselect) ? $prename : '');
279 // the ugly select box
281 $select = contact_select('messageto','message-to-select', $preselect, 4, true, false, false, 10);
283 $tpl = get_markup_template('prv_message.tpl');
284 $o .= replace_macros($tpl,array(
286 'header' => t('Send Private Message'),
288 'showinputs' => 'true',
289 'prefill' => $prefill,
290 'autocomp' => $autocomp,
292 'subject' => t('Subject:'),
293 'subjtxt' => ((x($_REQUEST,'subject')) ? strip_tags($_REQUEST['subject']) : ''),
294 'text' => ((x($_REQUEST,'body')) ? escape_tags(htmlspecialchars($_REQUEST['body'])) : ''),
296 'yourmessage' => t('Your message:'),
299 'upload' => t('Upload photo'),
300 'insert' => t('Insert web link'),
301 'wait' => t('Please wait'),
302 'submit' => t('Submit')
316 $r = q("SELECT count(*) AS `total` FROM `mail`
317 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
318 intval(local_user()),
322 $a->set_pager_total($r[0]['total']);
324 $r = q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`,
325 `mail`.* , `contact`.`name`, `contact`.`url`, `contact`.`thumb` , `contact`.`network`,
327 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
328 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `mailcreated` DESC LIMIT %d , %d ",
329 intval(local_user()),
331 intval($a->pager['start']),
332 intval($a->pager['itemspage'])
336 info( t('No messages.') . EOL);
340 $tpl = get_markup_template('mail_list.tpl');
343 $partecipants = sprintf( t("Unknown sender - %s"),$rr['from-name']);
345 elseif (link_compare($rr['from-url'],$myprofile)){
346 $partecipants = sprintf( t("You and %s"), $rr['name']);
349 $partecipants = sprintf( t("%s and You"), $rr['from-name']);
352 if($a->theme['template_engine'] === 'internal') {
353 $subject_e = template_escape((($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>'));
354 $body_e = template_escape($rr['body']);
355 $to_name_e = template_escape($rr['name']);
358 $subject_e = (($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>');
359 $body_e = $rr['body'];
360 $to_name_e = $rr['name'];
363 $o .= replace_macros($tpl, array(
365 '$from_name' => $partecipants,
366 '$from_url' => (($rr['network'] === NETWORK_DFRN) ? $a->get_baseurl(true) . '/redir/' . $rr['contact-id'] : $rr['url']),
367 '$sparkle' => ' sparkle',
368 '$from_photo' => (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']),
369 '$subject' => $subject_e,
370 '$delete' => t('Delete conversation'),
372 '$to_name' => $to_name_e,
373 '$date' => datetime_convert('UTC',date_default_timezone_get(),$rr['mailcreated'], t('D, d M Y - g:i A')),
374 '$ago' => relative_date($rr['mailcreated']),
375 '$seen' => $rr['mailseen'],
376 '$count' => sprintf( tt('%d message', '%d messages', $rr['count']), $rr['count']),
383 if(($a->argc > 1) && (intval($a->argv[1]))) {
388 if( local_user() && feature_enabled(local_user(),'richtext') )
391 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
392 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
393 WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
394 intval(local_user()),
398 $contact_id = $r[0]['contact-id'];
399 $convid = $r[0]['convid'];
401 $sql_extra = sprintf(" and `mail`.`parent-uri` = '%s' ", dbesc($r[0]['parent-uri']));
403 $sql_extra = sprintf(" and ( `mail`.`parent-uri` = '%s' OR `mail`.`convid` = '%d' ) ",
404 dbesc($r[0]['parent-uri']),
408 $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
409 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
410 WHERE `mail`.`uid` = %d $sql_extra ORDER BY `mail`.`created` ASC",
414 if(! count($messages)) {
415 notice( t('Message not available.') . EOL );
419 $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
420 dbesc($r[0]['parent-uri']),
424 require_once("include/bbcode.php");
426 $tpl = get_markup_template('msg-header.tpl');
427 $a->page['htmlhead'] .= replace_macros($tpl, array(
428 '$baseurl' => $a->get_baseurl(true),
429 '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
430 '$nickname' => $a->user['nickname'],
431 '$linkurl' => t('Please enter a link URL:')
434 $tpl = get_markup_template('msg-end.tpl');
435 $a->page['end'] .= replace_macros($tpl, array(
436 '$baseurl' => $a->get_baseurl(true),
437 '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
438 '$nickname' => $a->user['nickname'],
439 '$linkurl' => t('Please enter a link URL:')
447 foreach($messages as $message) {
448 if($message['unknown'])
450 if($message['from-url'] == $myprofile) {
451 $from_url = $myprofile;
455 $from_url = $a->get_baseurl(true) . '/redir/' . $message['contact-id'];
456 $sparkle = ' sparkle';
460 $extracted = item_extract_images($message['body']);
461 if($extracted['images'])
462 $message['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $message['contact-id']);
464 if($a->theme['template_engine'] === 'internal') {
465 $from_name_e = template_escape($message['from-name']);
466 $subject_e = template_escape($message['title']);
467 $body_e = template_escape(smilies(bbcode($message['body'])));
468 $to_name_e = template_escape($message['name']);
471 $from_name_e = $message['from-name'];
472 $subject_e = $message['title'];
473 $body_e = smilies(bbcode($message['body']));
474 $to_name_e = $message['name'];
478 'id' => $message['id'],
479 'from_name' => $from_name_e,
480 'from_url' => $from_url,
481 'sparkle' => $sparkle,
482 'from_photo' => $message['from-photo'],
483 'subject' => $subject_e,
485 'delete' => t('Delete message'),
486 'to_name' => $to_name_e,
487 'date' => datetime_convert('UTC',date_default_timezone_get(),$message['created'],'D, d M Y - g:i A'),
488 'ago' => relative_date($message['created']),
491 $seen = $message['seen'];
495 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
496 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
498 $tpl = get_markup_template('mail_display.tpl');
500 if($a->theme['template_engine'] === 'internal') {
501 $subjtxt_e = template_escape($message['title']);
504 $subjtxt_e = $message['title'];
507 $o = replace_macros($tpl, array(
508 '$thread_id' => $a->argv[1],
509 '$thread_subject' => $message['title'],
510 '$thread_seen' => $seen,
511 '$delete' => t('Delete conversation'),
512 '$canreply' => (($unknown) ? false : '1'),
513 '$unknown_text' => t("No secure communications available. You <strong>may</strong> be able to respond from the sender's profile page."),
517 '$reply_info' => array(
518 'header' => t('Send Reply'),
521 'subject' => t('Subject:'),
522 'subjtxt' => $subjtxt_e,
523 'readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
524 'yourmessage' => t('Your message:'),
528 'upload' => t('Upload photo'),
529 'insert' => t('Insert web link'),
530 'submit' => t('Submit'),
531 'wait' => t('Please wait'),