3 require_once('include/acl_selectors.php');
4 require_once('include/message.php');
6 function message_init(&$a) {
10 if (is_numeric($a->argv[1]))
11 $tabs = render_messages(get_messages(local_user(),0,5), 'mail_list.tpl');
14 'label' => t('New Message'),
15 'url' => $a->get_baseurl(true) . '/message/new',
16 'sel'=> ($a->argv[1] == 'new'),
20 $tpl = get_markup_template('message_side.tpl');
21 $a->page['aside'] = replace_macros($tpl, array(
25 $base = $a->get_baseurl();
27 $head_tpl = get_markup_template('message-head.tpl');
28 $a->page['htmlhead'] .= replace_macros($head_tpl,array(
29 '$baseurl' => $a->get_baseurl(true),
33 $end_tpl = get_markup_template('message-end.tpl');
34 $a->page['end'] .= replace_macros($end_tpl,array(
35 '$baseurl' => $a->get_baseurl(true),
41 function message_post(&$a) {
44 notice( t('Permission denied.') . EOL);
48 $replyto = ((x($_REQUEST,'replyto')) ? notags(trim($_REQUEST['replyto'])) : '');
49 $subject = ((x($_REQUEST,'subject')) ? notags(trim($_REQUEST['subject'])) : '');
50 $body = ((x($_REQUEST,'body')) ? escape_tags(trim($_REQUEST['body'])) : '');
51 $recipient = ((x($_REQUEST,'messageto')) ? intval($_REQUEST['messageto']) : 0 );
53 // Work around doubled linefeeds in Tinymce 3.5b2
55 /* $plaintext = intval(get_pconfig(local_user(),'system','plaintext') && !feature_enabled(local_user(),'richtext'));
57 $body = fix_mce_lf($body);
59 $plaintext = intval(!feature_enabled(local_user(),'richtext'));
61 $body = fix_mce_lf($body);
64 $ret = send_message($recipient, $body, $subject, $replyto);
69 notice( t('No recipient selected.') . EOL );
73 notice( t('Unable to locate contact information.') . EOL );
76 notice( t('Message could not be sent.') . EOL );
79 notice( t('Message collection failure.') . EOL );
82 info( t('Message sent.') . EOL );
85 // fake it to go back to the input form if no recipient listed
92 goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
96 // Note: the code in 'item_extract_images' and 'item_redir_and_replace_images'
97 // is identical to the code in include/conversation.php
98 if(! function_exists('item_extract_images')) {
99 function item_extract_images($body) {
101 $saved_image = array();
106 $img_start = strpos($orig_body, '[img');
107 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
108 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
109 while(($img_st_close !== false) && ($img_end !== false)) {
111 $img_st_close++; // make it point to AFTER the closing bracket
112 $img_end += $img_start;
114 if(! strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
115 // This is an embedded image
117 $saved_image[$cnt] = substr($orig_body, $img_start + $img_st_close, $img_end - ($img_start + $img_st_close));
118 $new_body = $new_body . substr($orig_body, 0, $img_start) . '[!#saved_image' . $cnt . '#!]';
123 $new_body = $new_body . substr($orig_body, 0, $img_end + strlen('[/img]'));
125 $orig_body = substr($orig_body, $img_end + strlen('[/img]'));
127 if($orig_body === false) // in case the body ends on a closing image tag
130 $img_start = strpos($orig_body, '[img');
131 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
132 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
135 $new_body = $new_body . $orig_body;
137 return array('body' => $new_body, 'images' => $saved_image);
140 if(! function_exists('item_redir_and_replace_images')) {
141 function item_redir_and_replace_images($body, $images, $cid) {
146 for($i = 0; $i < count($images); $i++) {
147 $search = '/\[url\=(.*?)\]\[!#saved_image' . $i . '#!\]\[\/url\]' . '/is';
148 $replace = '[url=' . z_path() . '/redir/' . $cid
149 . '?f=1&url=' . '$1' . '][!#saved_image' . $i . '#!][/url]' ;
151 $img_end = strpos($origbody, '[!#saved_image' . $i . '#!][/url]') + strlen('[!#saved_image' . $i . '#!][/url]');
152 $process_part = substr($origbody, 0, $img_end);
153 $origbody = substr($origbody, $img_end);
155 $process_part = preg_replace($search, $replace, $process_part);
156 $newbody = $newbody . $process_part;
158 $newbody = $newbody . $origbody;
161 foreach($images as $image) {
162 // We're depending on the property of 'foreach' (specified on the PHP website) that
163 // it loops over the array starting from the first element and going sequentially
164 // to the last element
165 $newbody = str_replace('[!#saved_image' . $cnt . '#!]', '[img]' . $image . '[/img]', $newbody);
174 function message_content(&$a) {
177 nav_set_selected('messages');
180 notice( t('Permission denied.') . EOL);
184 $myprofile = $a->get_baseurl(true) . '/profile/' . $a->user['nickname'];
186 $tpl = get_markup_template('mail_head.tpl');
187 $header = replace_macros($tpl, array(
188 '$messages' => t('Messages'),
189 '$tab_content' => $tab_content
193 if(($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) {
194 if(! intval($a->argv[2]))
197 // Check if we should do HTML-based delete confirmation
198 if($_REQUEST['confirm']) {
199 // <form> can't take arguments in its "action" parameter
200 // so add any arguments as hidden inputs
201 $query = explode_querystring($a->query_string);
203 foreach($query['args'] as $arg) {
204 if(strpos($arg, 'confirm=') === false) {
205 $arg_parts = explode('=', $arg);
206 $inputs[] = array('name' => $arg_parts[0], 'value' => $arg_parts[1]);
210 //$a->page['aside'] = '';
211 return replace_macros(get_markup_template('confirm.tpl'), array(
213 '$message' => t('Do you really want to delete this message?'),
214 '$extra_inputs' => $inputs,
215 '$confirm' => t('Yes'),
216 '$confirm_url' => $query['base'],
217 '$confirm_name' => 'confirmed',
218 '$cancel' => t('Cancel'),
221 // Now check how the user responded to the confirmation query
222 if($_REQUEST['canceled']) {
223 goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
227 if($cmd === 'drop') {
228 $r = q("DELETE FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
233 info( t('Message deleted.') . EOL );
235 //goaway($a->get_baseurl(true) . '/message' );
236 goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
239 $r = q("SELECT `parent-uri`,`convid` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
244 $parent = $r[0]['parent-uri'];
245 $convid = $r[0]['convid'];
247 $r = q("DELETE FROM `mail` WHERE `parent-uri` = '%s' AND `uid` = %d ",
252 // remove diaspora conversation pointer
253 // Actually if we do this, we can never receive another reply to that conversation,
254 // as we will never again have the info we need to re-create it.
255 // We'll just have to orphan it.
258 // q("delete from conv where id = %d limit 1",
264 info( t('Conversation removed.') . EOL );
266 //goaway($a->get_baseurl(true) . '/message' );
267 goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
272 if(($a->argc > 1) && ($a->argv[1] === 'new')) {
276 /* $plaintext = false;
277 if(intval(get_pconfig(local_user(),'system','plaintext')))
280 if( local_user() && feature_enabled(local_user(),'richtext') )
284 $tpl = get_markup_template('msg-header.tpl');
285 $a->page['htmlhead'] .= replace_macros($tpl, array(
286 '$baseurl' => $a->get_baseurl(true),
287 '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
288 '$nickname' => $a->user['nickname'],
289 '$linkurl' => t('Please enter a link URL:')
292 $tpl = get_markup_template('msg-end.tpl');
293 $a->page['end'] .= replace_macros($tpl, array(
294 '$baseurl' => $a->get_baseurl(true),
295 '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
296 '$nickname' => $a->user['nickname'],
297 '$linkurl' => t('Please enter a link URL:')
300 $preselect = (isset($a->argv[2])?array($a->argv[2]):false);
303 $prename = $preurl = $preid = '';
306 $r = q("select name, url, id from contact where uid = %d and id = %d limit 1",
307 intval(local_user()),
311 $prename = $r[0]['name'];
312 $preurl = $r[0]['url'];
313 $preid = $r[0]['id'];
317 $prefill = (($preselect) ? $prename : '');
319 // the ugly select box
321 $select = contact_select('messageto','message-to-select', $preselect, 4, true, false, false, 10);
323 $tpl = get_markup_template('prv_message.tpl');
324 $o .= replace_macros($tpl,array(
325 '$header' => t('Send Private Message'),
327 '$showinputs' => 'true',
328 '$prefill' => $prefill,
329 '$autocomp' => $autocomp,
331 '$subject' => t('Subject:'),
332 '$subjtxt' => ((x($_REQUEST,'subject')) ? strip_tags($_REQUEST['subject']) : ''),
333 '$text' => ((x($_REQUEST,'body')) ? escape_tags(htmlspecialchars($_REQUEST['body'])) : ''),
335 '$yourmessage' => t('Your message:'),
336 '$select' => $select,
338 '$upload' => t('Upload photo'),
339 '$insert' => t('Insert web link'),
340 '$wait' => t('Please wait'),
341 '$submit' => t('Submit')
348 $_SESSION['return_url'] = $a->query_string;
356 $r = q("SELECT count(*) AS `total` FROM `mail`
357 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
358 intval(local_user()),
362 if(count($r)) $a->set_pager_total($r[0]['total']);
364 $r = get_messages(local_user(), $a->pager['start'], $a->pager['itemspage']);
367 info( t('No messages.') . EOL);
371 $o .= render_messages($r, 'mail_list.tpl');
378 if(($a->argc > 1) && (intval($a->argv[1]))) {
383 if( local_user() && feature_enabled(local_user(),'richtext') )
386 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
387 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
388 WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
389 intval(local_user()),
393 $contact_id = $r[0]['contact-id'];
394 $convid = $r[0]['convid'];
396 $sql_extra = sprintf(" and `mail`.`parent-uri` = '%s' ", dbesc($r[0]['parent-uri']));
398 $sql_extra = sprintf(" and ( `mail`.`parent-uri` = '%s' OR `mail`.`convid` = '%d' ) ",
399 dbesc($r[0]['parent-uri']),
403 $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
404 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
405 WHERE `mail`.`uid` = %d $sql_extra ORDER BY `mail`.`created` ASC",
409 if(! count($messages)) {
410 notice( t('Message not available.') . EOL );
414 $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
415 dbesc($r[0]['parent-uri']),
419 require_once("include/bbcode.php");
421 $tpl = get_markup_template('msg-header.tpl');
422 $a->page['htmlhead'] .= replace_macros($tpl, array(
423 '$baseurl' => $a->get_baseurl(true),
424 '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
425 '$nickname' => $a->user['nickname'],
426 '$linkurl' => t('Please enter a link URL:')
429 $tpl = get_markup_template('msg-end.tpl');
430 $a->page['end'] .= replace_macros($tpl, array(
431 '$baseurl' => $a->get_baseurl(true),
432 '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
433 '$nickname' => $a->user['nickname'],
434 '$linkurl' => t('Please enter a link URL:')
442 foreach($messages as $message) {
443 if($message['unknown'])
445 if($message['from-url'] == $myprofile) {
446 $from_url = $myprofile;
450 $from_url = $a->get_baseurl(true) . '/redir/' . $message['contact-id'];
451 $sparkle = ' sparkle';
455 $extracted = item_extract_images($message['body']);
456 if($extracted['images'])
457 $message['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $message['contact-id']);
459 if($a->theme['template_engine'] === 'internal') {
460 $from_name_e = template_escape($message['from-name']);
461 $subject_e = template_escape($message['title']);
462 $body_e = template_escape(smilies(bbcode($message['body'])));
463 $to_name_e = template_escape($message['name']);
466 $from_name_e = $message['from-name'];
467 $subject_e = $message['title'];
468 $body_e = smilies(bbcode($message['body']));
469 $to_name_e = $message['name'];
473 'id' => $message['id'],
474 'from_name' => $from_name_e,
475 'from_url' => $from_url,
476 'sparkle' => $sparkle,
477 'from_photo' => $message['from-photo'],
478 'subject' => $subject_e,
480 'delete' => t('Delete message'),
481 'to_name' => $to_name_e,
482 'date' => datetime_convert('UTC',date_default_timezone_get(),$message['created'],'D, d M Y - g:i A'),
483 'ago' => relative_date($message['created']),
486 $seen = $message['seen'];
490 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
491 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
493 $tpl = get_markup_template('mail_display.tpl');
495 if($a->theme['template_engine'] === 'internal') {
496 $subjtxt_e = template_escape($message['title']);
499 $subjtxt_e = $message['title'];
502 $o = replace_macros($tpl, array(
503 '$thread_id' => $a->argv[1],
504 '$thread_subject' => $message['title'],
505 '$thread_seen' => $seen,
506 '$delete' => t('Delete conversation'),
507 '$canreply' => (($unknown) ? false : '1'),
508 '$unknown_text' => t("No secure communications available. You <strong>may</strong> be able to respond from the sender's profile page."),
512 '$header' => t('Send Reply'),
515 '$subject' => t('Subject:'),
516 '$subjtxt' => $subjtxt_e,
517 '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
518 '$yourmessage' => t('Your message:'),
520 '$select' => $select,
521 '$parent' => $parent,
522 '$upload' => t('Upload photo'),
523 '$insert' => t('Insert web link'),
524 '$submit' => t('Submit'),
525 '$wait' => t('Please wait')
533 function get_messages($user, $lstart, $lend) {
535 return q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`,
536 `mail`.* , `contact`.`name`, `contact`.`url`, `contact`.`thumb` , `contact`.`network`,
538 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
539 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `mailcreated` DESC LIMIT %d , %d ",
540 intval($user), intval($lstart), intval($lend)
544 function render_messages($msg, $t) {
548 $tpl = get_markup_template($t);
551 foreach($msg as $rr) {
554 $participants = sprintf( t("Unknown sender - %s"),$rr['from-name']);
556 elseif (link_compare($rr['from-url'], $myprofile)){
557 $participants = sprintf( t("You and %s"), $rr['name']);
560 $participants = sprintf( t("%s and You"), $rr['from-name']);
563 if($a->theme['template_engine'] === 'internal') {
564 $subject_e = template_escape((($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>'));
565 $body_e = template_escape($rr['body']);
566 $to_name_e = template_escape($rr['name']);
569 $subject_e = (($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>');
570 $body_e = $rr['body'];
571 $to_name_e = $rr['name'];
574 $rslt .= replace_macros($tpl, array(
576 '$from_name' => $participants,
577 '$from_url' => (($rr['network'] === NETWORK_DFRN) ? $a->get_baseurl(true) . '/redir/' . $rr['contact-id'] : $rr['url']),
578 '$sparkle' => ' sparkle',
579 '$from_photo' => (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']),
580 '$subject' => $subject_e,
581 '$delete' => t('Delete conversation'),
583 '$to_name' => $to_name_e,
584 '$date' => datetime_convert('UTC',date_default_timezone_get(),$rr['mailcreated'], t('D, d M Y - g:i A')),
585 '$ago' => relative_date($rr['mailcreated']),
586 '$seen' => $rr['mailseen'],
587 '$count' => sprintf( tt('%d message', '%d messages', $rr['count']), $rr['count']),