3 * @file mod/message.php
6 use Friendica\Content\Nav;
7 use Friendica\Content\Smilies;
8 use Friendica\Core\System;
9 use Friendica\Database\DBM;
10 use Friendica\Model\Contact;
11 use Friendica\Model\Mail;
13 require_once 'include/acl_selectors.php';
14 require_once 'include/conversation.php';
16 function message_init(App $a)
20 if ($a->argc > 1 && is_numeric($a->argv[1])) {
21 $tabs = render_messages(get_messages(local_user(), 0, 5), 'mail_list.tpl');
25 'label' => t('New Message'),
26 'url' => 'message/new',
27 'sel' => $a->argc > 1 && $a->argv[1] == 'new',
31 $tpl = get_markup_template('message_side.tpl');
32 $a->page['aside'] = replace_macros($tpl, [
36 $base = System::baseUrl();
38 $head_tpl = get_markup_template('message-head.tpl');
39 $a->page['htmlhead'] .= replace_macros($head_tpl, [
40 '$baseurl' => System::baseUrl(true),
44 $end_tpl = get_markup_template('message-end.tpl');
45 $a->page['end'] .= replace_macros($end_tpl, [
46 '$baseurl' => System::baseUrl(true),
51 function message_post(App $a)
54 notice(t('Permission denied.') . EOL);
58 $replyto = x($_REQUEST, 'replyto') ? notags(trim($_REQUEST['replyto'])) : '';
59 $subject = x($_REQUEST, 'subject') ? notags(trim($_REQUEST['subject'])) : '';
60 $body = x($_REQUEST, 'body') ? escape_tags(trim($_REQUEST['body'])) : '';
61 $recipient = x($_REQUEST, 'messageto') ? intval($_REQUEST['messageto']) : 0;
63 $ret = Mail::send($recipient, $body, $subject, $replyto);
68 notice(t('No recipient selected.') . EOL);
72 notice(t('Unable to locate contact information.') . EOL);
75 notice(t('Message could not be sent.') . EOL);
78 notice(t('Message collection failure.') . EOL);
81 info(t('Message sent.') . EOL);
84 // fake it to go back to the input form if no recipient listed
89 goaway($_SESSION['return_url']);
93 function message_content(App $a)
96 Nav::setSelected('messages');
99 notice(t('Permission denied.') . EOL);
103 $myprofile = System::baseUrl() . '/profile/' . $a->user['nickname'];
105 $tpl = get_markup_template('mail_head.tpl');
106 $header = replace_macros($tpl, [
107 '$messages' => t('Messages'),
110 if (($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) {
111 if (!intval($a->argv[2])) {
115 // Check if we should do HTML-based delete confirmation
116 if ($_REQUEST['confirm']) {
117 // <form> can't take arguments in its "action" parameter
118 // so add any arguments as hidden inputs
119 $query = explode_querystring($a->query_string);
121 foreach ($query['args'] as $arg) {
122 if (strpos($arg, 'confirm=') === false) {
123 $arg_parts = explode('=', $arg);
124 $inputs[] = ['name' => $arg_parts[0], 'value' => $arg_parts[1]];
128 //$a->page['aside'] = '';
129 return replace_macros(get_markup_template('confirm.tpl'), [
131 '$message' => t('Do you really want to delete this message?'),
132 '$extra_inputs' => $inputs,
133 '$confirm' => t('Yes'),
134 '$confirm_url' => $query['base'],
135 '$confirm_name' => 'confirmed',
136 '$cancel' => t('Cancel'),
139 // Now check how the user responded to the confirmation query
140 if ($_REQUEST['canceled']) {
141 goaway($_SESSION['return_url']);
145 if ($cmd === 'drop') {
146 $r = q("DELETE FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
151 info(t('Message deleted.') . EOL);
153 //goaway(System::baseUrl(true) . '/message' );
154 goaway($_SESSION['return_url']);
156 $r = q("SELECT `parent-uri`,`convid` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
160 if (DBM::is_result($r)) {
161 $parent = $r[0]['parent-uri'];
162 $convid = $r[0]['convid'];
164 $r = q("DELETE FROM `mail` WHERE `parent-uri` = '%s' AND `uid` = %d ",
169 // remove diaspora conversation pointer
170 // Actually if we do this, we can never receive another reply to that conversation,
171 // as we will never again have the info we need to re-create it.
172 // We'll just have to orphan it.
174 // q("delete from conv where id = %d limit 1",
180 info(t('Conversation removed.') . EOL);
183 //goaway(System::baseUrl(true) . '/message' );
184 goaway($_SESSION['return_url']);
188 if (($a->argc > 1) && ($a->argv[1] === 'new')) {
191 $tpl = get_markup_template('msg-header.tpl');
192 $a->page['htmlhead'] .= replace_macros($tpl, [
193 '$baseurl' => System::baseUrl(true),
194 '$nickname' => $a->user['nickname'],
195 '$linkurl' => t('Please enter a link URL:')
198 $tpl = get_markup_template('msg-end.tpl');
199 $a->page['end'] .= replace_macros($tpl, [
200 '$baseurl' => System::baseUrl(true),
201 '$nickname' => $a->user['nickname'],
202 '$linkurl' => t('Please enter a link URL:')
205 $preselect = isset($a->argv[2]) ? [$a->argv[2]] : false;
207 $prename = $preurl = $preid = '';
210 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `id` = %d LIMIT 1",
211 intval(local_user()),
214 if (!DBM::is_result($r)) {
215 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' LIMIT 1",
216 intval(local_user()),
217 dbesc(normalise_link(base64_decode($a->argv[2])))
221 if (!DBM::is_result($r)) {
222 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `addr` = '%s' LIMIT 1",
223 intval(local_user()),
224 dbesc(base64_decode($a->argv[2]))
228 if (DBM::is_result($r)) {
229 $prename = $r[0]['name'];
230 $preurl = $r[0]['url'];
231 $preid = $r[0]['id'];
232 $preselect = [$preid];
238 $prefill = $preselect ? $prename : '';
240 // the ugly select box
241 $select = contact_select('messageto', 'message-to-select', $preselect, 4, true, false, false, 10);
243 $tpl = get_markup_template('prv_message.tpl');
244 $o .= replace_macros($tpl, [
245 '$header' => t('Send Private Message'),
247 '$showinputs' => 'true',
248 '$prefill' => $prefill,
249 '$autocomp' => $autocomp,
251 '$subject' => t('Subject:'),
252 '$subjtxt' => x($_REQUEST, 'subject') ? strip_tags($_REQUEST['subject']) : '',
253 '$text' => x($_REQUEST, 'body') ? escape_tags(htmlspecialchars($_REQUEST['body'])) : '',
255 '$yourmessage' => t('Your message:'),
256 '$select' => $select,
258 '$upload' => t('Upload photo'),
259 '$insert' => t('Insert web link'),
260 '$wait' => t('Please wait'),
261 '$submit' => t('Submit')
267 $_SESSION['return_url'] = $a->query_string;
275 $r = q("SELECT count(*) AS `total`, ANY_VALUE(`created`) AS `created` FROM `mail`
276 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
280 if (DBM::is_result($r)) {
281 $a->set_pager_total($r[0]['total']);
284 $r = get_messages(local_user(), $a->pager['start'], $a->pager['itemspage']);
286 if (!DBM::is_result($r)) {
287 info(t('No messages.') . EOL);
291 $o .= render_messages($r, 'mail_list.tpl');
298 if (($a->argc > 1) && (intval($a->argv[1]))) {
302 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
303 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
304 WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
305 intval(local_user()),
308 if (DBM::is_result($r)) {
309 $contact_id = $r[0]['contact-id'];
310 $convid = $r[0]['convid'];
312 $sql_extra = sprintf(" and `mail`.`parent-uri` = '%s' ", dbesc($r[0]['parent-uri']));
314 $sql_extra = sprintf(" and ( `mail`.`parent-uri` = '%s' OR `mail`.`convid` = '%d' ) ",
315 dbesc($r[0]['parent-uri']),
319 $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
320 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
321 WHERE `mail`.`uid` = %d $sql_extra ORDER BY `mail`.`created` ASC",
325 if (!count($messages)) {
326 notice(t('Message not available.') . EOL);
330 $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
331 dbesc($r[0]['parent-uri']),
335 require_once("include/bbcode.php");
337 $tpl = get_markup_template('msg-header.tpl');
338 $a->page['htmlhead'] .= replace_macros($tpl, [
339 '$baseurl' => System::baseUrl(true),
340 '$nickname' => $a->user['nickname'],
341 '$linkurl' => t('Please enter a link URL:')
344 $tpl = get_markup_template('msg-end.tpl');
345 $a->page['end'] .= replace_macros($tpl, [
346 '$baseurl' => System::baseUrl(true),
347 '$nickname' => $a->user['nickname'],
348 '$linkurl' => t('Please enter a link URL:')
355 foreach ($messages as $message) {
356 if ($message['unknown'])
358 if ($message['from-url'] == $myprofile) {
359 $from_url = $myprofile;
361 } elseif ($message['contact-id'] != 0) {
362 $from_url = 'redir/' . $message['contact-id'];
363 $sparkle = ' sparkle';
365 $from_url = $message['from-url'] . "?zrl=" . urlencode($myprofile);
366 $sparkle = ' sparkle';
369 $extracted = item_extract_images($message['body']);
370 if ($extracted['images']) {
371 $message['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $message['contact-id']);
374 $from_name_e = $message['from-name'];
375 $subject_e = $message['title'];
376 $body_e = Smilies::replace(bbcode($message['body']));
377 $to_name_e = $message['name'];
379 $contact = Contact::getDetailsByURL($message['from-url']);
380 if (isset($contact["thumb"])) {
381 $from_photo = $contact["thumb"];
383 $from_photo = $message['from-photo'];
387 'id' => $message['id'],
388 'from_name' => $from_name_e,
389 'from_url' => $from_url,
390 'from_addr' => $contact['addr'],
391 'sparkle' => $sparkle,
392 'from_photo' => proxy_url($from_photo, false, PROXY_SIZE_THUMB),
393 'subject' => $subject_e,
395 'delete' => t('Delete message'),
396 'to_name' => $to_name_e,
397 'date' => datetime_convert('UTC', date_default_timezone_get(), $message['created'], 'D, d M Y - g:i A'),
398 'ago' => relative_date($message['created']),
401 $seen = $message['seen'];
404 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
405 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
407 $tpl = get_markup_template('mail_display.tpl');
408 $o = replace_macros($tpl, [
409 '$thread_id' => $a->argv[1],
410 '$thread_subject' => $message['title'],
411 '$thread_seen' => $seen,
412 '$delete' => t('Delete conversation'),
413 '$canreply' => (($unknown) ? false : '1'),
414 '$unknown_text' => t("No secure communications available. You <strong>may</strong> be able to respond from the sender's profile page."),
418 '$header' => t('Send Reply'),
421 '$subject' => t('Subject:'),
422 '$subjtxt' => $message['title'],
423 '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
424 '$yourmessage' => t('Your message:'),
426 '$select' => $select,
427 '$parent' => $parent,
428 '$upload' => t('Upload photo'),
429 '$insert' => t('Insert web link'),
430 '$submit' => t('Submit'),
431 '$wait' => t('Please wait')
438 function get_messages($user, $lstart, $lend)
440 //TODO: rewritte with a sub-query to get the first message of each private thread with certainty
441 return q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`,
442 ANY_VALUE(`mail`.`id`) AS `id`, ANY_VALUE(`mail`.`uid`) AS `uid`, ANY_VALUE(`mail`.`guid`) AS `guid`,
443 ANY_VALUE(`mail`.`from-name`) AS `from-name`, ANY_VALUE(`mail`.`from-photo`) AS `from-photo`,
444 ANY_VALUE(`mail`.`from-url`) AS `from-url`, ANY_VALUE(`mail`.`contact-id`) AS `contact-id`,
445 ANY_VALUE(`mail`.`convid`) AS `convid`, ANY_VALUE(`mail`.`title`) AS `title`, ANY_VALUE(`mail`.`body`) AS `body`,
446 ANY_VALUE(`mail`.`seen`) AS `seen`, ANY_VALUE(`mail`.`reply`) AS `reply`, ANY_VALUE(`mail`.`replied`) AS `replied`,
447 ANY_VALUE(`mail`.`unknown`) AS `unknown`, ANY_VALUE(`mail`.`uri`) AS `uri`,
449 ANY_VALUE(`mail`.`created`) AS `created`, ANY_VALUE(`contact`.`name`) AS `name`, ANY_VALUE(`contact`.`url`) AS `url`,
450 ANY_VALUE(`contact`.`thumb`) AS `thumb`, ANY_VALUE(`contact`.`network`) AS `network`,
451 count( * ) as `count`
452 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
453 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `mailcreated` DESC LIMIT %d , %d ",
454 intval($user), intval($lstart), intval($lend)
458 function render_messages(array $msg, $t)
462 $tpl = get_markup_template($t);
465 $myprofile = System::baseUrl() . '/profile/' . $a->user['nickname'];
467 foreach ($msg as $rr) {
468 if ($rr['unknown']) {
469 $participants = t("Unknown sender - %s", $rr['from-name']);
470 } elseif (link_compare($rr['from-url'], $myprofile)) {
471 $participants = t("You and %s", $rr['name']);
473 $participants = t("%s and You", $rr['from-name']);
476 $subject_e = (($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>');
477 $body_e = $rr['body'];
478 $to_name_e = $rr['name'];
480 $contact = Contact::getDetailsByURL($rr['url']);
481 if (isset($contact["thumb"])) {
482 $from_photo = $contact["thumb"];
484 $from_photo = (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']);
487 $rslt .= replace_macros($tpl, [
489 '$from_name' => $participants,
490 '$from_url' => (($rr['network'] === NETWORK_DFRN) ? 'redir/' . $rr['contact-id'] : $rr['url']),
491 '$from_addr' => $contact['addr'],
492 '$sparkle' => ' sparkle',
493 '$from_photo' => proxy_url($from_photo, false, PROXY_SIZE_THUMB),
494 '$subject' => $subject_e,
495 '$delete' => t('Delete conversation'),
497 '$to_name' => $to_name_e,
498 '$date' => datetime_convert('UTC', date_default_timezone_get(), $rr['mailcreated'], t('D, d M Y - g:i A')),
499 '$ago' => relative_date($rr['mailcreated']),
500 '$seen' => $rr['mailseen'],
501 '$count' => tt('%d message', '%d messages', $rr['count']),