5 require_once('include/acl_selectors.php');
6 require_once('include/message.php');
7 require_once('include/Smilies.php');
9 function message_init(App $a) {
13 if ($a->argc >1 && is_numeric($a->argv[1])) {
14 $tabs = render_messages(get_messages(local_user(),0,5), 'mail_list.tpl');
18 'label' => t('New Message'),
19 'url' => 'message/new',
20 'sel'=> ($a->argv[1] == 'new'),
24 $tpl = get_markup_template('message_side.tpl');
25 $a->page['aside'] = replace_macros($tpl, array(
29 $base = App::get_baseurl();
31 $head_tpl = get_markup_template('message-head.tpl');
32 $a->page['htmlhead'] .= replace_macros($head_tpl,array(
33 '$baseurl' => App::get_baseurl(true),
37 $end_tpl = get_markup_template('message-end.tpl');
38 $a->page['end'] .= replace_macros($end_tpl,array(
39 '$baseurl' => App::get_baseurl(true),
45 function message_post(App $a) {
48 notice( t('Permission denied.') . EOL);
52 $replyto = ((x($_REQUEST,'replyto')) ? notags(trim($_REQUEST['replyto'])) : '');
53 $subject = ((x($_REQUEST,'subject')) ? notags(trim($_REQUEST['subject'])) : '');
54 $body = ((x($_REQUEST,'body')) ? escape_tags(trim($_REQUEST['body'])) : '');
55 $recipient = ((x($_REQUEST,'messageto')) ? intval($_REQUEST['messageto']) : 0 );
57 $ret = send_message($recipient, $body, $subject, $replyto);
62 notice( t('No recipient selected.') . EOL );
66 notice( t('Unable to locate contact information.') . EOL );
69 notice( t('Message could not be sent.') . EOL );
72 notice( t('Message collection failure.') . EOL );
75 info( t('Message sent.') . EOL );
78 // fake it to go back to the input form if no recipient listed
85 goaway($_SESSION['return_url']);
89 // Note: the code in 'item_extract_images' and 'item_redir_and_replace_images'
90 // is identical to the code in include/conversation.php
91 if(! function_exists('item_extract_images')) {
92 function item_extract_images($body) {
94 $saved_image = array();
99 $img_start = strpos($orig_body, '[img');
100 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
101 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
102 while(($img_st_close !== false) && ($img_end !== false)) {
104 $img_st_close++; // make it point to AFTER the closing bracket
105 $img_end += $img_start;
107 if(! strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
108 // This is an embedded image
110 $saved_image[$cnt] = substr($orig_body, $img_start + $img_st_close, $img_end - ($img_start + $img_st_close));
111 $new_body = $new_body . substr($orig_body, 0, $img_start) . '[!#saved_image' . $cnt . '#!]';
116 $new_body = $new_body . substr($orig_body, 0, $img_end + strlen('[/img]'));
118 $orig_body = substr($orig_body, $img_end + strlen('[/img]'));
120 if($orig_body === false) // in case the body ends on a closing image tag
123 $img_start = strpos($orig_body, '[img');
124 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
125 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
128 $new_body = $new_body . $orig_body;
130 return array('body' => $new_body, 'images' => $saved_image);
133 if(! function_exists('item_redir_and_replace_images')) {
134 function item_redir_and_replace_images($body, $images, $cid) {
139 for($i = 0; $i < count($images); $i++) {
140 $search = '/\[url\=(.*?)\]\[!#saved_image' . $i . '#!\]\[\/url\]' . '/is';
141 $replace = '[url=' . z_path() . '/redir/' . $cid
142 . '?f=1&url=' . '$1' . '][!#saved_image' . $i . '#!][/url]' ;
144 $img_end = strpos($origbody, '[!#saved_image' . $i . '#!][/url]') + strlen('[!#saved_image' . $i . '#!][/url]');
145 $process_part = substr($origbody, 0, $img_end);
146 $origbody = substr($origbody, $img_end);
148 $process_part = preg_replace($search, $replace, $process_part);
149 $newbody = $newbody . $process_part;
151 $newbody = $newbody . $origbody;
154 foreach ($images as $image) {
155 // We're depending on the property of 'foreach' (specified on the PHP website) that
156 // it loops over the array starting from the first element and going sequentially
157 // to the last element
158 $newbody = str_replace('[!#saved_image' . $cnt . '#!]', '[img]' . $image . '[/img]', $newbody);
167 function message_content(App $a) {
170 nav_set_selected('messages');
172 if (! local_user()) {
173 notice( t('Permission denied.') . EOL);
177 $myprofile = App::get_baseurl().'/profile/' . $a->user['nickname'];
179 $tpl = get_markup_template('mail_head.tpl');
180 $header = replace_macros($tpl, array(
181 '$messages' => t('Messages'),
182 '$tab_content' => $tab_content
186 if(($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) {
187 if(! intval($a->argv[2]))
190 // Check if we should do HTML-based delete confirmation
191 if($_REQUEST['confirm']) {
192 // <form> can't take arguments in its "action" parameter
193 // so add any arguments as hidden inputs
194 $query = explode_querystring($a->query_string);
196 foreach($query['args'] as $arg) {
197 if(strpos($arg, 'confirm=') === false) {
198 $arg_parts = explode('=', $arg);
199 $inputs[] = array('name' => $arg_parts[0], 'value' => $arg_parts[1]);
203 //$a->page['aside'] = '';
204 return replace_macros(get_markup_template('confirm.tpl'), array(
206 '$message' => t('Do you really want to delete this message?'),
207 '$extra_inputs' => $inputs,
208 '$confirm' => t('Yes'),
209 '$confirm_url' => $query['base'],
210 '$confirm_name' => 'confirmed',
211 '$cancel' => t('Cancel'),
214 // Now check how the user responded to the confirmation query
215 if($_REQUEST['canceled']) {
216 goaway($_SESSION['return_url']);
220 if($cmd === 'drop') {
221 $r = q("DELETE FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
226 info( t('Message deleted.') . EOL );
228 //goaway(App::get_baseurl(true) . '/message' );
229 goaway($_SESSION['return_url']);
232 $r = q("SELECT `parent-uri`,`convid` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
236 if (dbm::is_result($r)) {
237 $parent = $r[0]['parent-uri'];
238 $convid = $r[0]['convid'];
240 $r = q("DELETE FROM `mail` WHERE `parent-uri` = '%s' AND `uid` = %d ",
245 // remove diaspora conversation pointer
246 // Actually if we do this, we can never receive another reply to that conversation,
247 // as we will never again have the info we need to re-create it.
248 // We'll just have to orphan it.
251 // q("delete from conv where id = %d limit 1",
257 info( t('Conversation removed.') . EOL );
259 //goaway(App::get_baseurl(true) . '/message' );
260 goaway($_SESSION['return_url']);
265 if(($a->argc > 1) && ($a->argv[1] === 'new')) {
269 $tpl = get_markup_template('msg-header.tpl');
270 $a->page['htmlhead'] .= replace_macros($tpl, array(
271 '$baseurl' => App::get_baseurl(true),
272 '$nickname' => $a->user['nickname'],
273 '$linkurl' => t('Please enter a link URL:')
276 $tpl = get_markup_template('msg-end.tpl');
277 $a->page['end'] .= replace_macros($tpl, array(
278 '$baseurl' => App::get_baseurl(true),
279 '$nickname' => $a->user['nickname'],
280 '$linkurl' => t('Please enter a link URL:')
283 $preselect = (isset($a->argv[2])?array($a->argv[2]):false);
286 $prename = $preurl = $preid = '';
289 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `id` = %d LIMIT 1",
290 intval(local_user()),
293 if (!dbm::is_result($r)) {
294 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' LIMIT 1",
295 intval(local_user()),
296 dbesc(normalise_link(base64_decode($a->argv[2])))
300 if (!dbm::is_result($r)) {
301 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `addr` = '%s' LIMIT 1",
302 intval(local_user()),
303 dbesc(base64_decode($a->argv[2]))
307 if (dbm::is_result($r)) {
308 $prename = $r[0]['name'];
309 $preurl = $r[0]['url'];
310 $preid = $r[0]['id'];
311 $preselect = array($preid);
316 $prefill = (($preselect) ? $prename : '');
318 // the ugly select box
320 $select = contact_select('messageto','message-to-select', $preselect, 4, true, false, false, 10);
322 $tpl = get_markup_template('prv_message.tpl');
323 $o .= replace_macros($tpl,array(
324 '$header' => t('Send Private Message'),
326 '$showinputs' => 'true',
327 '$prefill' => $prefill,
328 '$autocomp' => $autocomp,
330 '$subject' => t('Subject:'),
331 '$subjtxt' => ((x($_REQUEST,'subject')) ? strip_tags($_REQUEST['subject']) : ''),
332 '$text' => ((x($_REQUEST,'body')) ? escape_tags(htmlspecialchars($_REQUEST['body'])) : ''),
334 '$yourmessage' => t('Your message:'),
335 '$select' => $select,
337 '$upload' => t('Upload photo'),
338 '$insert' => t('Insert web link'),
339 '$wait' => t('Please wait'),
340 '$submit' => t('Submit')
346 $_SESSION['return_url'] = $a->query_string;
354 $r = q("SELECT count(*) AS `total`, ANY_VALUE(`created`) AS `created` FROM `mail`
355 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
359 if (dbm::is_result($r)) {
360 $a->set_pager_total($r[0]['total']);
363 $r = get_messages(local_user(), $a->pager['start'], $a->pager['itemspage']);
365 if (! dbm::is_result($r)) {
366 info( t('No messages.') . EOL);
370 $o .= render_messages($r, 'mail_list.tpl');
377 if(($a->argc > 1) && (intval($a->argv[1]))) {
381 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
382 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
383 WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
384 intval(local_user()),
387 if (dbm::is_result($r)) {
388 $contact_id = $r[0]['contact-id'];
389 $convid = $r[0]['convid'];
391 $sql_extra = sprintf(" and `mail`.`parent-uri` = '%s' ", dbesc($r[0]['parent-uri']));
393 $sql_extra = sprintf(" and ( `mail`.`parent-uri` = '%s' OR `mail`.`convid` = '%d' ) ",
394 dbesc($r[0]['parent-uri']),
398 $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
399 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
400 WHERE `mail`.`uid` = %d $sql_extra ORDER BY `mail`.`created` ASC",
404 if(! count($messages)) {
405 notice( t('Message not available.') . EOL );
409 $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
410 dbesc($r[0]['parent-uri']),
414 require_once("include/bbcode.php");
416 $tpl = get_markup_template('msg-header.tpl');
417 $a->page['htmlhead'] .= replace_macros($tpl, array(
418 '$baseurl' => App::get_baseurl(true),
419 '$nickname' => $a->user['nickname'],
420 '$linkurl' => t('Please enter a link URL:')
423 $tpl = get_markup_template('msg-end.tpl');
424 $a->page['end'] .= replace_macros($tpl, array(
425 '$baseurl' => App::get_baseurl(true),
426 '$nickname' => $a->user['nickname'],
427 '$linkurl' => t('Please enter a link URL:')
434 foreach($messages as $message) {
435 if($message['unknown'])
437 if($message['from-url'] == $myprofile) {
438 $from_url = $myprofile;
440 } elseif ($message['contact-id'] != 0) {
441 $from_url = 'redir/'.$message['contact-id'];
442 $sparkle = ' sparkle';
444 $from_url = $message['from-url']."?zrl=".urlencode($myprofile);
445 $sparkle = ' sparkle';
449 $extracted = item_extract_images($message['body']);
450 if($extracted['images'])
451 $message['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $message['contact-id']);
453 if($a->theme['template_engine'] === 'internal') {
454 $from_name_e = template_escape($message['from-name']);
455 $subject_e = template_escape($message['title']);
456 $body_e = template_escape(Smilies::replace(bbcode($message['body'])));
457 $to_name_e = template_escape($message['name']);
459 $from_name_e = $message['from-name'];
460 $subject_e = $message['title'];
461 $body_e = Smilies::replace(bbcode($message['body']));
462 $to_name_e = $message['name'];
465 $contact = get_contact_details_by_url($message['from-url']);
466 if (isset($contact["thumb"]))
467 $from_photo = $contact["thumb"];
469 $from_photo = $message['from-photo'];
472 'id' => $message['id'],
473 'from_name' => $from_name_e,
474 'from_url' => $from_url,
475 'sparkle' => $sparkle,
476 'from_photo' => proxy_url($from_photo, false, PROXY_SIZE_THUMB),
477 'subject' => $subject_e,
479 'delete' => t('Delete message'),
480 'to_name' => $to_name_e,
481 'date' => datetime_convert('UTC',date_default_timezone_get(),$message['created'],'D, d M Y - g:i A'),
482 'ago' => relative_date($message['created']),
485 $seen = $message['seen'];
489 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
490 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
492 $tpl = get_markup_template('mail_display.tpl');
494 if($a->theme['template_engine'] === 'internal') {
495 $subjtxt_e = template_escape($message['title']);
498 $subjtxt_e = $message['title'];
501 $o = replace_macros($tpl, array(
502 '$thread_id' => $a->argv[1],
503 '$thread_subject' => $message['title'],
504 '$thread_seen' => $seen,
505 '$delete' => t('Delete conversation'),
506 '$canreply' => (($unknown) ? false : '1'),
507 '$unknown_text' => t("No secure communications available. You <strong>may</strong> be able to respond from the sender's profile page."),
511 '$header' => t('Send Reply'),
514 '$subject' => t('Subject:'),
515 '$subjtxt' => $subjtxt_e,
516 '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
517 '$yourmessage' => t('Your message:'),
519 '$select' => $select,
520 '$parent' => $parent,
521 '$upload' => t('Upload photo'),
522 '$insert' => t('Insert web link'),
523 '$submit' => t('Submit'),
524 '$wait' => t('Please wait')
532 function get_messages($user, $lstart, $lend) {
533 //TODO: rewritte with a sub-query to get the first message of each private thread with certainty
534 return q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`,
535 ANY_VALUE(`mail`.`id`) AS `id`, ANY_VALUE(`mail`.`uid`) AS `uid`, ANY_VALUE(`mail`.`guid`) AS `guid`,
536 ANY_VALUE(`mail`.`from-name`) AS `from-name`, ANY_VALUE(`mail`.`from-photo`) AS `from-photo`,
537 ANY_VALUE(`mail`.`from-url`) AS `from-url`, ANY_VALUE(`mail`.`contact-id`) AS `contact-id`,
538 ANY_VALUE(`mail`.`convid`) AS `convid`, ANY_VALUE(`mail`.`title`) AS `title`, ANY_VALUE(`mail`.`body`) AS `body`,
539 ANY_VALUE(`mail`.`seen`) AS `seen`, ANY_VALUE(`mail`.`reply`) AS `reply`, ANY_VALUE(`mail`.`replied`) AS `replied`,
540 ANY_VALUE(`mail`.`unknown`) AS `unknown`, ANY_VALUE(`mail`.`uri`) AS `uri`,
542 ANY_VALUE(`mail`.`created`) AS `created`, ANY_VALUE(`contact`.`name`) AS `name`, ANY_VALUE(`contact`.`url`) AS `url`,
543 ANY_VALUE(`contact`.`thumb`) AS `thumb`, ANY_VALUE(`contact`.`network`) AS `network`,
544 count( * ) as `count`
545 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
546 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `mailcreated` DESC LIMIT %d , %d ",
547 intval($user), intval($lstart), intval($lend)
551 function render_messages(array $msg, $t) {
555 $tpl = get_markup_template($t);
558 $myprofile = App::get_baseurl().'/profile/' . $a->user['nickname'];
560 foreach($msg as $rr) {
563 $participants = sprintf( t("Unknown sender - %s"),$rr['from-name']);
564 elseif (link_compare($rr['from-url'], $myprofile))
565 $participants = sprintf( t("You and %s"), $rr['name']);
567 $participants = sprintf(t("%s and You"), $rr['from-name']);
569 if($a->theme['template_engine'] === 'internal') {
570 $subject_e = template_escape((($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>'));
571 $body_e = template_escape($rr['body']);
572 $to_name_e = template_escape($rr['name']);
575 $subject_e = (($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>');
576 $body_e = $rr['body'];
577 $to_name_e = $rr['name'];
580 $contact = get_contact_details_by_url($rr['url']);
581 if (isset($contact["thumb"]))
582 $from_photo = $contact["thumb"];
584 $from_photo = (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']);
586 $rslt .= replace_macros($tpl, array(
588 '$from_name' => $participants,
589 '$from_url' => (($rr['network'] === NETWORK_DFRN) ? 'redir/' . $rr['contact-id'] : $rr['url']),
590 '$sparkle' => ' sparkle',
591 '$from_photo' => proxy_url($from_photo, false, PROXY_SIZE_THUMB),
592 '$subject' => $subject_e,
593 '$delete' => t('Delete conversation'),
595 '$to_name' => $to_name_e,
596 '$date' => datetime_convert('UTC',date_default_timezone_get(),$rr['mailcreated'], t('D, d M Y - g:i A')),
597 '$ago' => relative_date($rr['mailcreated']),
598 '$seen' => $rr['mailseen'],
599 '$count' => sprintf( tt('%d message', '%d messages', $rr['count']), $rr['count']),