4 use Friendica\Core\System;
6 require_once('include/acl_selectors.php');
7 require_once('include/message.php');
8 require_once('include/Smilies.php');
10 function message_init(App $a) {
14 if ($a->argc >1 && is_numeric($a->argv[1])) {
15 $tabs = render_messages(get_messages(local_user(),0,5), 'mail_list.tpl');
19 'label' => t('New Message'),
20 'url' => 'message/new',
21 'sel'=> ($a->argv[1] == 'new'),
25 $tpl = get_markup_template('message_side.tpl');
26 $a->page['aside'] = replace_macros($tpl, array(
30 $base = System::baseUrl();
32 $head_tpl = get_markup_template('message-head.tpl');
33 $a->page['htmlhead'] .= replace_macros($head_tpl,array(
34 '$baseurl' => System::baseUrl(true),
38 $end_tpl = get_markup_template('message-end.tpl');
39 $a->page['end'] .= replace_macros($end_tpl,array(
40 '$baseurl' => System::baseUrl(true),
46 function message_post(App $a) {
49 notice( t('Permission denied.') . EOL);
53 $replyto = ((x($_REQUEST,'replyto')) ? notags(trim($_REQUEST['replyto'])) : '');
54 $subject = ((x($_REQUEST,'subject')) ? notags(trim($_REQUEST['subject'])) : '');
55 $body = ((x($_REQUEST,'body')) ? escape_tags(trim($_REQUEST['body'])) : '');
56 $recipient = ((x($_REQUEST,'messageto')) ? intval($_REQUEST['messageto']) : 0 );
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
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 . '#!]';
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=' . System::baseUrl() . '/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(App $a) {
169 nav_set_selected('messages');
171 if (! local_user()) {
172 notice( t('Permission denied.') . EOL);
176 $myprofile = System::baseUrl().'/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 // Check if we should do HTML-based delete confirmation
190 if ($_REQUEST['confirm']) {
191 // <form> can't take arguments in its "action" parameter
192 // so add any arguments as hidden inputs
193 $query = explode_querystring($a->query_string);
195 foreach($query['args'] as $arg) {
196 if (strpos($arg, 'confirm=') === false) {
197 $arg_parts = explode('=', $arg);
198 $inputs[] = array('name' => $arg_parts[0], 'value' => $arg_parts[1]);
202 //$a->page['aside'] = '';
203 return replace_macros(get_markup_template('confirm.tpl'), array(
205 '$message' => t('Do you really want to delete this message?'),
206 '$extra_inputs' => $inputs,
207 '$confirm' => t('Yes'),
208 '$confirm_url' => $query['base'],
209 '$confirm_name' => 'confirmed',
210 '$cancel' => t('Cancel'),
213 // Now check how the user responded to the confirmation query
214 if ($_REQUEST['canceled']) {
215 goaway($_SESSION['return_url']);
219 if ($cmd === 'drop') {
220 $r = q("DELETE FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
225 info( t('Message deleted.') . EOL );
227 //goaway(System::baseUrl(true) . '/message' );
228 goaway($_SESSION['return_url']);
230 $r = q("SELECT `parent-uri`,`convid` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
234 if (dbm::is_result($r)) {
235 $parent = $r[0]['parent-uri'];
236 $convid = $r[0]['convid'];
238 $r = q("DELETE FROM `mail` WHERE `parent-uri` = '%s' AND `uid` = %d ",
243 // remove diaspora conversation pointer
244 // Actually if we do this, we can never receive another reply to that conversation,
245 // as we will never again have the info we need to re-create it.
246 // We'll just have to orphan it.
249 // q("delete from conv where id = %d limit 1",
255 info( t('Conversation removed.') . EOL );
257 //goaway(System::baseUrl(true) . '/message' );
258 goaway($_SESSION['return_url']);
263 if (($a->argc > 1) && ($a->argv[1] === 'new')) {
267 $tpl = get_markup_template('msg-header.tpl');
268 $a->page['htmlhead'] .= replace_macros($tpl, array(
269 '$baseurl' => System::baseUrl(true),
270 '$nickname' => $a->user['nickname'],
271 '$linkurl' => t('Please enter a link URL:')
274 $tpl = get_markup_template('msg-end.tpl');
275 $a->page['end'] .= replace_macros($tpl, array(
276 '$baseurl' => System::baseUrl(true),
277 '$nickname' => $a->user['nickname'],
278 '$linkurl' => t('Please enter a link URL:')
281 $preselect = (isset($a->argv[2])?array($a->argv[2]):false);
284 $prename = $preurl = $preid = '';
287 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `id` = %d LIMIT 1",
288 intval(local_user()),
291 if (!dbm::is_result($r)) {
292 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' LIMIT 1",
293 intval(local_user()),
294 dbesc(normalise_link(base64_decode($a->argv[2])))
298 if (!dbm::is_result($r)) {
299 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `addr` = '%s' LIMIT 1",
300 intval(local_user()),
301 dbesc(base64_decode($a->argv[2]))
305 if (dbm::is_result($r)) {
306 $prename = $r[0]['name'];
307 $preurl = $r[0]['url'];
308 $preid = $r[0]['id'];
309 $preselect = array($preid);
314 $prefill = (($preselect) ? $prename : '');
316 // the ugly select box
318 $select = contact_select('messageto','message-to-select', $preselect, 4, true, false, false, 10);
320 $tpl = get_markup_template('prv_message.tpl');
321 $o .= replace_macros($tpl,array(
322 '$header' => t('Send Private Message'),
324 '$showinputs' => 'true',
325 '$prefill' => $prefill,
326 '$autocomp' => $autocomp,
328 '$subject' => t('Subject:'),
329 '$subjtxt' => ((x($_REQUEST,'subject')) ? strip_tags($_REQUEST['subject']) : ''),
330 '$text' => ((x($_REQUEST,'body')) ? escape_tags(htmlspecialchars($_REQUEST['body'])) : ''),
332 '$yourmessage' => t('Your message:'),
333 '$select' => $select,
335 '$upload' => t('Upload photo'),
336 '$insert' => t('Insert web link'),
337 '$wait' => t('Please wait'),
338 '$submit' => t('Submit')
344 $_SESSION['return_url'] = $a->query_string;
352 $r = q("SELECT count(*) AS `total`, ANY_VALUE(`created`) AS `created` FROM `mail`
353 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
357 if (dbm::is_result($r)) {
358 $a->set_pager_total($r[0]['total']);
361 $r = get_messages(local_user(), $a->pager['start'], $a->pager['itemspage']);
363 if (! dbm::is_result($r)) {
364 info( t('No messages.') . EOL);
368 $o .= render_messages($r, 'mail_list.tpl');
375 if (($a->argc > 1) && (intval($a->argv[1]))) {
379 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
380 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
381 WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
382 intval(local_user()),
385 if (dbm::is_result($r)) {
386 $contact_id = $r[0]['contact-id'];
387 $convid = $r[0]['convid'];
389 $sql_extra = sprintf(" and `mail`.`parent-uri` = '%s' ", dbesc($r[0]['parent-uri']));
391 $sql_extra = sprintf(" and ( `mail`.`parent-uri` = '%s' OR `mail`.`convid` = '%d' ) ",
392 dbesc($r[0]['parent-uri']),
396 $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
397 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
398 WHERE `mail`.`uid` = %d $sql_extra ORDER BY `mail`.`created` ASC",
402 if (! count($messages)) {
403 notice( t('Message not available.') . EOL );
407 $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
408 dbesc($r[0]['parent-uri']),
412 require_once("include/bbcode.php");
414 $tpl = get_markup_template('msg-header.tpl');
415 $a->page['htmlhead'] .= replace_macros($tpl, array(
416 '$baseurl' => System::baseUrl(true),
417 '$nickname' => $a->user['nickname'],
418 '$linkurl' => t('Please enter a link URL:')
421 $tpl = get_markup_template('msg-end.tpl');
422 $a->page['end'] .= replace_macros($tpl, array(
423 '$baseurl' => System::baseUrl(true),
424 '$nickname' => $a->user['nickname'],
425 '$linkurl' => t('Please enter a link URL:')
432 foreach($messages as $message) {
433 if ($message['unknown'])
435 if ($message['from-url'] == $myprofile) {
436 $from_url = $myprofile;
438 } elseif ($message['contact-id'] != 0) {
439 $from_url = 'redir/'.$message['contact-id'];
440 $sparkle = ' sparkle';
442 $from_url = $message['from-url']."?zrl=".urlencode($myprofile);
443 $sparkle = ' sparkle';
447 $extracted = item_extract_images($message['body']);
448 if ($extracted['images'])
449 $message['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $message['contact-id']);
451 if ($a->theme['template_engine'] === 'internal') {
452 $from_name_e = template_escape($message['from-name']);
453 $subject_e = template_escape($message['title']);
454 $body_e = template_escape(Smilies::replace(bbcode($message['body'])));
455 $to_name_e = template_escape($message['name']);
457 $from_name_e = $message['from-name'];
458 $subject_e = $message['title'];
459 $body_e = Smilies::replace(bbcode($message['body']));
460 $to_name_e = $message['name'];
463 $contact = get_contact_details_by_url($message['from-url']);
464 if (isset($contact["thumb"]))
465 $from_photo = $contact["thumb"];
467 $from_photo = $message['from-photo'];
470 'id' => $message['id'],
471 'from_name' => $from_name_e,
472 'from_url' => $from_url,
473 'from_addr' => $contact['addr'],
474 'sparkle' => $sparkle,
475 'from_photo' => proxy_url($from_photo, false, PROXY_SIZE_THUMB),
476 'subject' => $subject_e,
478 'delete' => t('Delete message'),
479 'to_name' => $to_name_e,
480 'date' => datetime_convert('UTC',date_default_timezone_get(),$message['created'],'D, d M Y - g:i A'),
481 'ago' => relative_date($message['created']),
484 $seen = $message['seen'];
488 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
489 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
491 $tpl = get_markup_template('mail_display.tpl');
493 if ($a->theme['template_engine'] === 'internal') {
494 $subjtxt_e = template_escape($message['title']);
496 $subjtxt_e = $message['title'];
499 $o = replace_macros($tpl, array(
500 '$thread_id' => $a->argv[1],
501 '$thread_subject' => $message['title'],
502 '$thread_seen' => $seen,
503 '$delete' => t('Delete conversation'),
504 '$canreply' => (($unknown) ? false : '1'),
505 '$unknown_text' => t("No secure communications available. You <strong>may</strong> be able to respond from the sender's profile page."),
509 '$header' => t('Send Reply'),
512 '$subject' => t('Subject:'),
513 '$subjtxt' => $subjtxt_e,
514 '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
515 '$yourmessage' => t('Your message:'),
517 '$select' => $select,
518 '$parent' => $parent,
519 '$upload' => t('Upload photo'),
520 '$insert' => t('Insert web link'),
521 '$submit' => t('Submit'),
522 '$wait' => t('Please wait')
530 function get_messages($user, $lstart, $lend) {
531 //TODO: rewritte with a sub-query to get the first message of each private thread with certainty
532 return q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`,
533 ANY_VALUE(`mail`.`id`) AS `id`, ANY_VALUE(`mail`.`uid`) AS `uid`, ANY_VALUE(`mail`.`guid`) AS `guid`,
534 ANY_VALUE(`mail`.`from-name`) AS `from-name`, ANY_VALUE(`mail`.`from-photo`) AS `from-photo`,
535 ANY_VALUE(`mail`.`from-url`) AS `from-url`, ANY_VALUE(`mail`.`contact-id`) AS `contact-id`,
536 ANY_VALUE(`mail`.`convid`) AS `convid`, ANY_VALUE(`mail`.`title`) AS `title`, ANY_VALUE(`mail`.`body`) AS `body`,
537 ANY_VALUE(`mail`.`seen`) AS `seen`, ANY_VALUE(`mail`.`reply`) AS `reply`, ANY_VALUE(`mail`.`replied`) AS `replied`,
538 ANY_VALUE(`mail`.`unknown`) AS `unknown`, ANY_VALUE(`mail`.`uri`) AS `uri`,
540 ANY_VALUE(`mail`.`created`) AS `created`, ANY_VALUE(`contact`.`name`) AS `name`, ANY_VALUE(`contact`.`url`) AS `url`,
541 ANY_VALUE(`contact`.`thumb`) AS `thumb`, ANY_VALUE(`contact`.`network`) AS `network`,
542 count( * ) as `count`
543 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
544 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `mailcreated` DESC LIMIT %d , %d ",
545 intval($user), intval($lstart), intval($lend)
549 function render_messages(array $msg, $t) {
553 $tpl = get_markup_template($t);
556 $myprofile = System::baseUrl().'/profile/' . $a->user['nickname'];
558 foreach($msg as $rr) {
561 $participants = sprintf( t("Unknown sender - %s"),$rr['from-name']);
562 elseif (link_compare($rr['from-url'], $myprofile))
563 $participants = sprintf( t("You and %s"), $rr['name']);
565 $participants = sprintf(t("%s and You"), $rr['from-name']);
567 if ($a->theme['template_engine'] === 'internal') {
568 $subject_e = template_escape((($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>'));
569 $body_e = template_escape($rr['body']);
570 $to_name_e = template_escape($rr['name']);
572 $subject_e = (($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>');
573 $body_e = $rr['body'];
574 $to_name_e = $rr['name'];
577 $contact = get_contact_details_by_url($rr['url']);
578 if (isset($contact["thumb"]))
579 $from_photo = $contact["thumb"];
581 $from_photo = (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']);
583 $rslt .= replace_macros($tpl, array(
585 '$from_name' => $participants,
586 '$from_url' => (($rr['network'] === NETWORK_DFRN) ? 'redir/' . $rr['contact-id'] : $rr['url']),
587 '$from_addr' => $contact['addr'],
588 '$sparkle' => ' sparkle',
589 '$from_photo' => proxy_url($from_photo, false, PROXY_SIZE_THUMB),
590 '$subject' => $subject_e,
591 '$delete' => t('Delete conversation'),
593 '$to_name' => $to_name_e,
594 '$date' => datetime_convert('UTC',date_default_timezone_get(),$rr['mailcreated'], t('D, d M Y - g:i A')),
595 '$ago' => relative_date($rr['mailcreated']),
596 '$seen' => $rr['mailseen'],
597 '$count' => sprintf( tt('%d message', '%d messages', $rr['count']), $rr['count']),