3 require_once('include/acl_selectors.php');
4 require_once('include/message.php');
6 function message_init(&$a) {
10 if ($a->argc >1 && is_numeric($a->argv[1])) {
11 $tabs = render_messages(get_messages(local_user(),0,5), 'mail_list.tpl');
15 'label' => t('New Message'),
16 'url' => 'message/new',
17 'sel'=> ($a->argv[1] == 'new'),
21 $tpl = get_markup_template('message_side.tpl');
22 $a->page['aside'] = replace_macros($tpl, array(
26 $base = $a->get_baseurl();
28 $head_tpl = get_markup_template('message-head.tpl');
29 $a->page['htmlhead'] .= replace_macros($head_tpl,array(
30 '$baseurl' => $a->get_baseurl(true),
34 $end_tpl = get_markup_template('message-end.tpl');
35 $a->page['end'] .= replace_macros($end_tpl,array(
36 '$baseurl' => $a->get_baseurl(true),
42 function message_post(&$a) {
45 notice( t('Permission denied.') . EOL);
49 $replyto = ((x($_REQUEST,'replyto')) ? notags(trim($_REQUEST['replyto'])) : '');
50 $subject = ((x($_REQUEST,'subject')) ? notags(trim($_REQUEST['subject'])) : '');
51 $body = ((x($_REQUEST,'body')) ? escape_tags(trim($_REQUEST['body'])) : '');
52 $recipient = ((x($_REQUEST,'messageto')) ? intval($_REQUEST['messageto']) : 0 );
54 // Work around doubled linefeeds in Tinymce 3.5b2
56 /* $plaintext = intval(get_pconfig(local_user(),'system','plaintext') && !feature_enabled(local_user(),'richtext'));
58 $body = fix_mce_lf($body);
60 $plaintext = intval(!feature_enabled(local_user(),'richtext'));
62 $body = fix_mce_lf($body);
65 $ret = send_message($recipient, $body, $subject, $replyto);
70 notice( t('No recipient selected.') . EOL );
74 notice( t('Unable to locate contact information.') . EOL );
77 notice( t('Message could not be sent.') . EOL );
80 notice( t('Message collection failure.') . EOL );
83 info( t('Message sent.') . EOL );
86 // fake it to go back to the input form if no recipient listed
93 goaway($_SESSION['return_url']);
97 // Note: the code in 'item_extract_images' and 'item_redir_and_replace_images'
98 // is identical to the code in include/conversation.php
99 if(! function_exists('item_extract_images')) {
100 function item_extract_images($body) {
102 $saved_image = array();
107 $img_start = strpos($orig_body, '[img');
108 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
109 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
110 while(($img_st_close !== false) && ($img_end !== false)) {
112 $img_st_close++; // make it point to AFTER the closing bracket
113 $img_end += $img_start;
115 if(! strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
116 // This is an embedded image
118 $saved_image[$cnt] = substr($orig_body, $img_start + $img_st_close, $img_end - ($img_start + $img_st_close));
119 $new_body = $new_body . substr($orig_body, 0, $img_start) . '[!#saved_image' . $cnt . '#!]';
124 $new_body = $new_body . substr($orig_body, 0, $img_end + strlen('[/img]'));
126 $orig_body = substr($orig_body, $img_end + strlen('[/img]'));
128 if($orig_body === false) // in case the body ends on a closing image tag
131 $img_start = strpos($orig_body, '[img');
132 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
133 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
136 $new_body = $new_body . $orig_body;
138 return array('body' => $new_body, 'images' => $saved_image);
141 if(! function_exists('item_redir_and_replace_images')) {
142 function item_redir_and_replace_images($body, $images, $cid) {
147 for($i = 0; $i < count($images); $i++) {
148 $search = '/\[url\=(.*?)\]\[!#saved_image' . $i . '#!\]\[\/url\]' . '/is';
149 $replace = '[url=' . z_path() . '/redir/' . $cid
150 . '?f=1&url=' . '$1' . '][!#saved_image' . $i . '#!][/url]' ;
152 $img_end = strpos($origbody, '[!#saved_image' . $i . '#!][/url]') + strlen('[!#saved_image' . $i . '#!][/url]');
153 $process_part = substr($origbody, 0, $img_end);
154 $origbody = substr($origbody, $img_end);
156 $process_part = preg_replace($search, $replace, $process_part);
157 $newbody = $newbody . $process_part;
159 $newbody = $newbody . $origbody;
162 foreach($images as $image) {
163 // We're depending on the property of 'foreach' (specified on the PHP website) that
164 // it loops over the array starting from the first element and going sequentially
165 // to the last element
166 $newbody = str_replace('[!#saved_image' . $cnt . '#!]', '[img]' . $image . '[/img]', $newbody);
175 function message_content(&$a) {
178 nav_set_selected('messages');
181 notice( t('Permission denied.') . EOL);
185 $myprofile = 'profile/' . $a->user['nickname'];
187 $tpl = get_markup_template('mail_head.tpl');
188 $header = replace_macros($tpl, array(
189 '$messages' => t('Messages'),
190 '$tab_content' => $tab_content
194 if(($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) {
195 if(! intval($a->argv[2]))
198 // Check if we should do HTML-based delete confirmation
199 if($_REQUEST['confirm']) {
200 // <form> can't take arguments in its "action" parameter
201 // so add any arguments as hidden inputs
202 $query = explode_querystring($a->query_string);
204 foreach($query['args'] as $arg) {
205 if(strpos($arg, 'confirm=') === false) {
206 $arg_parts = explode('=', $arg);
207 $inputs[] = array('name' => $arg_parts[0], 'value' => $arg_parts[1]);
211 //$a->page['aside'] = '';
212 return replace_macros(get_markup_template('confirm.tpl'), array(
214 '$message' => t('Do you really want to delete this message?'),
215 '$extra_inputs' => $inputs,
216 '$confirm' => t('Yes'),
217 '$confirm_url' => $query['base'],
218 '$confirm_name' => 'confirmed',
219 '$cancel' => t('Cancel'),
222 // Now check how the user responded to the confirmation query
223 if($_REQUEST['canceled']) {
224 goaway($_SESSION['return_url']);
228 if($cmd === 'drop') {
229 $r = q("DELETE FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
234 info( t('Message deleted.') . EOL );
236 //goaway($a->get_baseurl(true) . '/message' );
237 goaway($_SESSION['return_url']);
240 $r = q("SELECT `parent-uri`,`convid` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
245 $parent = $r[0]['parent-uri'];
246 $convid = $r[0]['convid'];
248 $r = q("DELETE FROM `mail` WHERE `parent-uri` = '%s' AND `uid` = %d ",
253 // remove diaspora conversation pointer
254 // Actually if we do this, we can never receive another reply to that conversation,
255 // as we will never again have the info we need to re-create it.
256 // We'll just have to orphan it.
259 // q("delete from conv where id = %d limit 1",
265 info( t('Conversation removed.') . EOL );
267 //goaway($a->get_baseurl(true) . '/message' );
268 goaway($_SESSION['return_url']);
273 if(($a->argc > 1) && ($a->argv[1] === 'new')) {
277 /* $plaintext = false;
278 if(intval(get_pconfig(local_user(),'system','plaintext')))
281 if( local_user() && feature_enabled(local_user(),'richtext') )
285 $tpl = get_markup_template('msg-header.tpl');
286 $a->page['htmlhead'] .= replace_macros($tpl, array(
287 '$baseurl' => $a->get_baseurl(true),
288 '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
289 '$nickname' => $a->user['nickname'],
290 '$linkurl' => t('Please enter a link URL:')
293 $tpl = get_markup_template('msg-end.tpl');
294 $a->page['end'] .= replace_macros($tpl, array(
295 '$baseurl' => $a->get_baseurl(true),
296 '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
297 '$nickname' => $a->user['nickname'],
298 '$linkurl' => t('Please enter a link URL:')
301 $preselect = (isset($a->argv[2])?array($a->argv[2]):false);
304 $prename = $preurl = $preid = '';
307 $r = q("select name, url, id from contact where uid = %d and id = %d limit 1",
308 intval(local_user()),
312 $prename = $r[0]['name'];
313 $preurl = $r[0]['url'];
314 $preid = $r[0]['id'];
318 $prefill = (($preselect) ? $prename : '');
320 // the ugly select box
322 $select = contact_select('messageto','message-to-select', $preselect, 4, true, false, false, 10);
324 $tpl = get_markup_template('prv_message.tpl');
325 $o .= replace_macros($tpl,array(
326 '$header' => t('Send Private Message'),
328 '$showinputs' => 'true',
329 '$prefill' => $prefill,
330 '$autocomp' => $autocomp,
332 '$subject' => t('Subject:'),
333 '$subjtxt' => ((x($_REQUEST,'subject')) ? strip_tags($_REQUEST['subject']) : ''),
334 '$text' => ((x($_REQUEST,'body')) ? escape_tags(htmlspecialchars($_REQUEST['body'])) : ''),
336 '$yourmessage' => t('Your message:'),
337 '$select' => $select,
339 '$upload' => t('Upload photo'),
340 '$insert' => t('Insert web link'),
341 '$wait' => t('Please wait'),
342 '$submit' => t('Submit')
349 $_SESSION['return_url'] = $a->query_string;
357 $r = q("SELECT count(*) AS `total` FROM `mail`
358 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
359 intval(local_user()),
363 if (is_filled_array($r)) {
364 $a->set_pager_total($r[0]['total']);
367 $r = get_messages(local_user(), $a->pager['start'], $a->pager['itemspage']);
369 if(! is_filled_array($r)) {
370 info( t('No messages.') . EOL);
374 $o .= render_messages($r, 'mail_list.tpl');
381 if(($a->argc > 1) && (intval($a->argv[1]))) {
386 if( local_user() && feature_enabled(local_user(),'richtext') )
389 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
390 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
391 WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
392 intval(local_user()),
396 $contact_id = $r[0]['contact-id'];
397 $convid = $r[0]['convid'];
399 $sql_extra = sprintf(" and `mail`.`parent-uri` = '%s' ", dbesc($r[0]['parent-uri']));
401 $sql_extra = sprintf(" and ( `mail`.`parent-uri` = '%s' OR `mail`.`convid` = '%d' ) ",
402 dbesc($r[0]['parent-uri']),
406 $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
407 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
408 WHERE `mail`.`uid` = %d $sql_extra ORDER BY `mail`.`created` ASC",
412 if(! count($messages)) {
413 notice( t('Message not available.') . EOL );
417 $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
418 dbesc($r[0]['parent-uri']),
422 require_once("include/bbcode.php");
424 $tpl = get_markup_template('msg-header.tpl');
425 $a->page['htmlhead'] .= replace_macros($tpl, array(
426 '$baseurl' => $a->get_baseurl(true),
427 '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
428 '$nickname' => $a->user['nickname'],
429 '$linkurl' => t('Please enter a link URL:')
432 $tpl = get_markup_template('msg-end.tpl');
433 $a->page['end'] .= replace_macros($tpl, array(
434 '$baseurl' => $a->get_baseurl(true),
435 '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
436 '$nickname' => $a->user['nickname'],
437 '$linkurl' => t('Please enter a link URL:')
445 foreach($messages as $message) {
446 if($message['unknown'])
448 if($message['from-url'] == $myprofile) {
449 $from_url = $myprofile;
453 $from_url = 'redir/' . $message['contact-id'];
454 $sparkle = ' sparkle';
458 $extracted = item_extract_images($message['body']);
459 if($extracted['images'])
460 $message['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $message['contact-id']);
462 if($a->theme['template_engine'] === 'internal') {
463 $from_name_e = template_escape($message['from-name']);
464 $subject_e = template_escape($message['title']);
465 $body_e = template_escape(smilies(bbcode($message['body'])));
466 $to_name_e = template_escape($message['name']);
469 $from_name_e = $message['from-name'];
470 $subject_e = $message['title'];
471 $body_e = smilies(bbcode($message['body']));
472 $to_name_e = $message['name'];
476 'id' => $message['id'],
477 'from_name' => $from_name_e,
478 'from_url' => $from_url,
479 'sparkle' => $sparkle,
480 'from_photo' => $message['from-photo'],
481 'subject' => $subject_e,
483 'delete' => t('Delete message'),
484 'to_name' => $to_name_e,
485 'date' => datetime_convert('UTC',date_default_timezone_get(),$message['created'],'D, d M Y - g:i A'),
486 'ago' => relative_date($message['created']),
489 $seen = $message['seen'];
493 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
494 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
496 $tpl = get_markup_template('mail_display.tpl');
498 if($a->theme['template_engine'] === 'internal') {
499 $subjtxt_e = template_escape($message['title']);
502 $subjtxt_e = $message['title'];
505 $o = replace_macros($tpl, array(
506 '$thread_id' => $a->argv[1],
507 '$thread_subject' => $message['title'],
508 '$thread_seen' => $seen,
509 '$delete' => t('Delete conversation'),
510 '$canreply' => (($unknown) ? false : '1'),
511 '$unknown_text' => t("No secure communications available. You <strong>may</strong> be able to respond from the sender's profile page."),
515 '$header' => t('Send Reply'),
518 '$subject' => t('Subject:'),
519 '$subjtxt' => $subjtxt_e,
520 '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
521 '$yourmessage' => t('Your message:'),
523 '$select' => $select,
524 '$parent' => $parent,
525 '$upload' => t('Upload photo'),
526 '$insert' => t('Insert web link'),
527 '$submit' => t('Submit'),
528 '$wait' => t('Please wait')
536 function get_messages($user, $lstart, $lend) {
538 return q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`,
539 `mail`.* , `contact`.`name`, `contact`.`url`, `contact`.`thumb` , `contact`.`network`,
541 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
542 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `mailcreated` DESC LIMIT %d , %d ",
543 intval($user), intval($lstart), intval($lend)
547 function render_messages(array $msg, $t) {
551 $tpl = get_markup_template($t);
554 $myprofile = 'profile/' . $a->user['nickname'];
556 foreach($msg as $rr) {
559 $participants = sprintf( t("Unknown sender - %s"),$rr['from-name']);
561 elseif (link_compare($rr['from-url'], $myprofile)){
562 $participants = sprintf( t("You and %s"), $rr['name']);
565 $participants = sprintf( t("%s and You"), $rr['from-name']);
568 if($a->theme['template_engine'] === 'internal') {
569 $subject_e = template_escape((($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>'));
570 $body_e = template_escape($rr['body']);
571 $to_name_e = template_escape($rr['name']);
574 $subject_e = (($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>');
575 $body_e = $rr['body'];
576 $to_name_e = $rr['name'];
579 $rslt .= replace_macros($tpl, array(
581 '$from_name' => $participants,
582 '$from_url' => (($rr['network'] === NETWORK_DFRN) ? 'redir/' . $rr['contact-id'] : $rr['url']),
583 '$sparkle' => ' sparkle',
584 '$from_photo' => (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']),
585 '$subject' => $subject_e,
586 '$delete' => t('Delete conversation'),
588 '$to_name' => $to_name_e,
589 '$date' => datetime_convert('UTC',date_default_timezone_get(),$rr['mailcreated'], t('D, d M Y - g:i A')),
590 '$ago' => relative_date($rr['mailcreated']),
591 '$seen' => $rr['mailseen'],
592 '$count' => sprintf( tt('%d message', '%d messages', $rr['count']), $rr['count']),