3 * @file mod/message.php
7 use Friendica\Content\Nav;
8 use Friendica\Content\Smilies;
9 use Friendica\Content\Text\BBCode;
10 use Friendica\Core\ACL;
11 use Friendica\Core\L10n;
12 use Friendica\Core\System;
13 use Friendica\Database\DBA;
14 use Friendica\Model\Contact;
15 use Friendica\Model\Mail;
16 use Friendica\Util\DateTimeFormat;
17 use Friendica\Util\Proxy as ProxyUtils;
18 use Friendica\Util\Temporal;
20 require_once 'include/conversation.php';
22 function message_init(App $a)
26 if ($a->argc > 1 && is_numeric($a->argv[1])) {
27 $tabs = render_messages(get_messages(local_user(), 0, 5), 'mail_list.tpl');
31 'label' => L10n::t('New Message'),
32 'url' => 'message/new',
33 'sel' => $a->argc > 1 && $a->argv[1] == 'new',
37 $tpl = get_markup_template('message_side.tpl');
38 $a->page['aside'] = replace_macros($tpl, [
42 $base = System::baseUrl();
44 $head_tpl = get_markup_template('message-head.tpl');
45 $a->page['htmlhead'] .= replace_macros($head_tpl, [
46 '$baseurl' => System::baseUrl(true),
50 $end_tpl = get_markup_template('message-end.tpl');
51 $a->page['end'] .= replace_macros($end_tpl, [
52 '$baseurl' => System::baseUrl(true),
57 function message_post(App $a)
60 notice(L10n::t('Permission denied.') . EOL);
64 $replyto = x($_REQUEST, 'replyto') ? notags(trim($_REQUEST['replyto'])) : '';
65 $subject = x($_REQUEST, 'subject') ? notags(trim($_REQUEST['subject'])) : '';
66 $body = x($_REQUEST, 'body') ? escape_tags(trim($_REQUEST['body'])) : '';
67 $recipient = x($_REQUEST, 'messageto') ? intval($_REQUEST['messageto']) : 0;
69 $ret = Mail::send($recipient, $body, $subject, $replyto);
74 notice(L10n::t('No recipient selected.') . EOL);
78 notice(L10n::t('Unable to locate contact information.') . EOL);
81 notice(L10n::t('Message could not be sent.') . EOL);
84 notice(L10n::t('Message collection failure.') . EOL);
87 info(L10n::t('Message sent.') . EOL);
90 // fake it to go back to the input form if no recipient listed
95 goaway($a->cmd . '/' . $ret);
99 function message_content(App $a)
102 Nav::setSelected('messages');
105 notice(L10n::t('Permission denied.') . EOL);
109 $myprofile = System::baseUrl() . '/profile/' . $a->user['nickname'];
111 $tpl = get_markup_template('mail_head.tpl');
112 if ($a->argc > 1 && $a->argv[1] == 'new') {
114 'label' => L10n::t('Discard'),
120 'label' => L10n::t('New Message'),
121 'url' => '/message/new',
126 $header = replace_macros($tpl, [
127 '$messages' => L10n::t('Messages'),
128 '$button' => $button,
131 if (($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) {
132 if (!intval($a->argv[2])) {
136 // Check if we should do HTML-based delete confirmation
137 if (!empty($_REQUEST['confirm'])) {
138 // <form> can't take arguments in its "action" parameter
139 // so add any arguments as hidden inputs
140 $query = explode_querystring($a->query_string);
142 foreach ($query['args'] as $arg) {
143 if (strpos($arg, 'confirm=') === false) {
144 $arg_parts = explode('=', $arg);
145 $inputs[] = ['name' => $arg_parts[0], 'value' => $arg_parts[1]];
149 //$a->page['aside'] = '';
150 return replace_macros(get_markup_template('confirm.tpl'), [
152 '$message' => L10n::t('Do you really want to delete this message?'),
153 '$extra_inputs' => $inputs,
154 '$confirm' => L10n::t('Yes'),
155 '$confirm_url' => $query['base'],
156 '$confirm_name' => 'confirmed',
157 '$cancel' => L10n::t('Cancel'),
161 // Now check how the user responded to the confirmation query
162 if (!empty($_REQUEST['canceled'])) {
167 if ($cmd === 'drop') {
168 if (DBA::delete('mail', ['id' => $a->argv[2], 'uid' => local_user()])) {
169 info(L10n::t('Message deleted.') . EOL);
174 $r = q("SELECT `parent-uri`,`convid` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
178 if (DBA::isResult($r)) {
179 $parent = $r[0]['parent-uri'];
180 $convid = $r[0]['convid'];
182 if (DBA::delete('mail', ['parent-uri' => $parent, 'uid' => local_user()])) {
183 info(L10n::t('Conversation removed.') . EOL);
190 if (($a->argc > 1) && ($a->argv[1] === 'new')) {
193 $tpl = get_markup_template('msg-header.tpl');
194 $a->page['htmlhead'] .= replace_macros($tpl, [
195 '$baseurl' => System::baseUrl(true),
196 '$nickname' => $a->user['nickname'],
197 '$linkurl' => L10n::t('Please enter a link URL:')
200 $tpl = get_markup_template('msg-end.tpl');
201 $a->page['end'] .= replace_macros($tpl, [
202 '$baseurl' => System::baseUrl(true),
203 '$nickname' => $a->user['nickname'],
204 '$linkurl' => L10n::t('Please enter a link URL:')
207 $preselect = isset($a->argv[2]) ? [$a->argv[2]] : [];
209 $prename = $preurl = $preid = '';
212 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `id` = %d LIMIT 1",
213 intval(local_user()),
216 if (!DBA::isResult($r)) {
217 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' LIMIT 1",
218 intval(local_user()),
219 DBA::escape(normalise_link(base64_decode($a->argv[2])))
223 if (!DBA::isResult($r)) {
224 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `addr` = '%s' LIMIT 1",
225 intval(local_user()),
226 DBA::escape(base64_decode($a->argv[2]))
230 if (DBA::isResult($r)) {
231 $prename = $r[0]['name'];
232 $preurl = $r[0]['url'];
233 $preid = $r[0]['id'];
234 $preselect = [$preid];
240 $prefill = $preselect ? $prename : '';
242 // the ugly select box
243 $select = ACL::getMessageContactSelectHTML('messageto', 'message-to-select', $preselect, 4, 10);
245 $tpl = get_markup_template('prv_message.tpl');
246 $o .= replace_macros($tpl, [
247 '$header' => L10n::t('Send Private Message'),
248 '$to' => L10n::t('To:'),
249 '$showinputs' => 'true',
250 '$prefill' => $prefill,
252 '$subject' => L10n::t('Subject:'),
253 '$subjtxt' => x($_REQUEST, 'subject') ? strip_tags($_REQUEST['subject']) : '',
254 '$text' => x($_REQUEST, 'body') ? escape_tags(htmlspecialchars($_REQUEST['body'])) : '',
256 '$yourmessage' => L10n::t('Your message:'),
257 '$select' => $select,
259 '$upload' => L10n::t('Upload photo'),
260 '$insert' => L10n::t('Insert web link'),
261 '$wait' => L10n::t('Please wait'),
262 '$submit' => L10n::t('Submit')
268 $_SESSION['return_url'] = $a->query_string;
276 $r = q("SELECT count(*) AS `total`, ANY_VALUE(`created`) AS `created` FROM `mail`
277 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
281 if (DBA::isResult($r)) {
282 $a->set_pager_total($r[0]['total']);
285 $r = get_messages(local_user(), $a->pager['start'], $a->pager['itemspage']);
287 if (!DBA::isResult($r)) {
288 info(L10n::t('No messages.') . EOL);
292 $o .= render_messages($r, 'mail_list.tpl');
299 if (($a->argc > 1) && (intval($a->argv[1]))) {
303 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
304 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
305 WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
306 intval(local_user()),
309 if (DBA::isResult($r)) {
310 $contact_id = $r[0]['contact-id'];
311 $convid = $r[0]['convid'];
313 $sql_extra = sprintf(" and `mail`.`parent-uri` = '%s' ", DBA::escape($r[0]['parent-uri']));
315 $sql_extra = sprintf(" and ( `mail`.`parent-uri` = '%s' OR `mail`.`convid` = '%d' ) ",
316 DBA::escape($r[0]['parent-uri']),
320 $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
321 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
322 WHERE `mail`.`uid` = %d $sql_extra ORDER BY `mail`.`created` ASC",
328 if (!DBA::isResult($messages)) {
329 notice(L10n::t('Message not available.') . EOL);
333 $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
334 DBA::escape($r[0]['parent-uri']),
338 $tpl = get_markup_template('msg-header.tpl');
339 $a->page['htmlhead'] .= replace_macros($tpl, [
340 '$baseurl' => System::baseUrl(true),
341 '$nickname' => $a->user['nickname'],
342 '$linkurl' => L10n::t('Please enter a link URL:')
345 $tpl = get_markup_template('msg-end.tpl');
346 $a->page['end'] .= replace_macros($tpl, [
347 '$baseurl' => System::baseUrl(true),
348 '$nickname' => $a->user['nickname'],
349 '$linkurl' => L10n::t('Please enter a link URL:')
356 foreach ($messages as $message) {
357 if ($message['unknown'])
359 if ($message['from-url'] == $myprofile) {
360 $from_url = $myprofile;
363 $from_url = Contact::magicLink($message['from-url']);
364 $sparkle = ' sparkle';
367 $extracted = item_extract_images($message['body']);
368 if ($extracted['images']) {
369 $message['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $message['contact-id']);
372 $from_name_e = $message['from-name'];
373 $subject_e = $message['title'];
374 $body_e = Smilies::replace(BBCode::convert($message['body']));
375 $to_name_e = $message['name'];
377 $contact = Contact::getDetailsByURL($message['from-url']);
378 if (isset($contact["thumb"])) {
379 $from_photo = $contact["thumb"];
381 $from_photo = $message['from-photo'];
385 'id' => $message['id'],
386 'from_name' => $from_name_e,
387 'from_url' => $from_url,
388 'from_addr' => $contact['addr'],
389 'sparkle' => $sparkle,
390 'from_photo' => ProxyUtils::proxifyUrl($from_photo, false, ProxyUtils::SIZE_THUMB),
391 'subject' => $subject_e,
393 'delete' => L10n::t('Delete message'),
394 'to_name' => $to_name_e,
395 'date' => DateTimeFormat::local($message['created'], L10n::t('D, d M Y - g:i A')),
396 'ago' => Temporal::getRelativeDate($message['created']),
399 $seen = $message['seen'];
402 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
403 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
405 $tpl = get_markup_template('mail_display.tpl');
406 $o = replace_macros($tpl, [
407 '$thread_id' => $a->argv[1],
408 '$thread_subject' => $message['title'],
409 '$thread_seen' => $seen,
410 '$delete' => L10n::t('Delete conversation'),
411 '$canreply' => (($unknown) ? false : '1'),
412 '$unknown_text' => L10n::t("No secure communications available. You <strong>may</strong> be able to respond from the sender's profile page."),
416 '$header' => L10n::t('Send Reply'),
417 '$to' => L10n::t('To:'),
419 '$subject' => L10n::t('Subject:'),
420 '$subjtxt' => $message['title'],
421 '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
422 '$yourmessage' => L10n::t('Your message:'),
424 '$select' => $select,
425 '$parent' => $parent,
426 '$upload' => L10n::t('Upload photo'),
427 '$insert' => L10n::t('Insert web link'),
428 '$submit' => L10n::t('Submit'),
429 '$wait' => L10n::t('Please wait')
436 function get_messages($user, $lstart, $lend)
438 //TODO: rewritte with a sub-query to get the first message of each private thread with certainty
439 return q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`,
440 ANY_VALUE(`mail`.`id`) AS `id`, ANY_VALUE(`mail`.`uid`) AS `uid`, ANY_VALUE(`mail`.`guid`) AS `guid`,
441 ANY_VALUE(`mail`.`from-name`) AS `from-name`, ANY_VALUE(`mail`.`from-photo`) AS `from-photo`,
442 ANY_VALUE(`mail`.`from-url`) AS `from-url`, ANY_VALUE(`mail`.`contact-id`) AS `contact-id`,
443 ANY_VALUE(`mail`.`convid`) AS `convid`, ANY_VALUE(`mail`.`title`) AS `title`, ANY_VALUE(`mail`.`body`) AS `body`,
444 ANY_VALUE(`mail`.`seen`) AS `seen`, ANY_VALUE(`mail`.`reply`) AS `reply`, ANY_VALUE(`mail`.`replied`) AS `replied`,
445 ANY_VALUE(`mail`.`unknown`) AS `unknown`, ANY_VALUE(`mail`.`uri`) AS `uri`,
447 ANY_VALUE(`mail`.`created`) AS `created`, ANY_VALUE(`contact`.`name`) AS `name`, ANY_VALUE(`contact`.`url`) AS `url`,
448 ANY_VALUE(`contact`.`thumb`) AS `thumb`, ANY_VALUE(`contact`.`network`) AS `network`,
449 count( * ) as `count`
450 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
451 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `mailcreated` DESC LIMIT %d , %d ",
452 intval($user), intval($lstart), intval($lend)
456 function render_messages(array $msg, $t)
460 $tpl = get_markup_template($t);
463 $myprofile = System::baseUrl() . '/profile/' . $a->user['nickname'];
465 foreach ($msg as $rr) {
466 if ($rr['unknown']) {
467 $participants = L10n::t("Unknown sender - %s", $rr['from-name']);
468 } elseif (link_compare($rr['from-url'], $myprofile)) {
469 $participants = L10n::t("You and %s", $rr['name']);
471 $participants = L10n::t("%s and You", $rr['from-name']);
474 $subject_e = (($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>');
475 $body_e = $rr['body'];
476 $to_name_e = $rr['name'];
478 $contact = Contact::getDetailsByURL($rr['url']);
479 if (isset($contact["thumb"])) {
480 $from_photo = $contact["thumb"];
482 $from_photo = (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']);
485 $rslt .= replace_macros($tpl, [
487 '$from_name' => $participants,
488 '$from_url' => Contact::magicLink($rr['url']),
489 '$from_addr' => defaults($contact, 'addr', ''),
490 '$sparkle' => ' sparkle',
491 '$from_photo' => ProxyUtils::proxifyUrl($from_photo, false, ProxyUtils::SIZE_THUMB),
492 '$subject' => $subject_e,
493 '$delete' => L10n::t('Delete conversation'),
495 '$to_name' => $to_name_e,
496 '$date' => DateTimeFormat::local($rr['mailcreated'], L10n::t('D, d M Y - g:i A')),
497 '$ago' => Temporal::getRelativeDate($rr['mailcreated']),
498 '$seen' => $rr['mailseen'],
499 '$count' => L10n::tt('%d message', '%d messages', $rr['count']),