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 $message = DBA::selectFirst('mail', ['convid'], ['id' => $a->argv[2], 'uid' => local_user()]);
169 if(!DBA::isResult($r)){
170 info(L10n::t('Conversation not found.') . EOL);
174 if (DBA::delete('mail', ['id' => $a->argv[2], 'uid' => local_user()])) {
175 info(L10n::t('Message deleted.') . EOL);
178 $conversation = DBA::selectFirst('mail', ['id'], ['convid' => $message['convid'], 'uid' => local_user()]);
179 if(!DBA::isResult($conversation)){
180 info(L10n::t('Conversation removed.') . EOL);
184 goaway('/message/' . $conversation['id'] );
186 $r = q("SELECT `parent-uri`,`convid` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
190 if (DBA::isResult($r)) {
191 $parent = $r[0]['parent-uri'];
192 $convid = $r[0]['convid'];
194 if (DBA::delete('mail', ['parent-uri' => $parent, 'uid' => local_user()])) {
195 info(L10n::t('Conversation removed.') . EOL);
202 if (($a->argc > 1) && ($a->argv[1] === 'new')) {
205 $tpl = get_markup_template('msg-header.tpl');
206 $a->page['htmlhead'] .= replace_macros($tpl, [
207 '$baseurl' => System::baseUrl(true),
208 '$nickname' => $a->user['nickname'],
209 '$linkurl' => L10n::t('Please enter a link URL:')
212 $tpl = get_markup_template('msg-end.tpl');
213 $a->page['end'] .= replace_macros($tpl, [
214 '$baseurl' => System::baseUrl(true),
215 '$nickname' => $a->user['nickname'],
216 '$linkurl' => L10n::t('Please enter a link URL:')
219 $preselect = isset($a->argv[2]) ? [$a->argv[2]] : [];
221 $prename = $preurl = $preid = '';
224 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `id` = %d LIMIT 1",
225 intval(local_user()),
228 if (!DBA::isResult($r)) {
229 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' LIMIT 1",
230 intval(local_user()),
231 DBA::escape(normalise_link(base64_decode($a->argv[2])))
235 if (!DBA::isResult($r)) {
236 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `addr` = '%s' LIMIT 1",
237 intval(local_user()),
238 DBA::escape(base64_decode($a->argv[2]))
242 if (DBA::isResult($r)) {
243 $prename = $r[0]['name'];
244 $preurl = $r[0]['url'];
245 $preid = $r[0]['id'];
246 $preselect = [$preid];
252 $prefill = $preselect ? $prename : '';
254 // the ugly select box
255 $select = ACL::getMessageContactSelectHTML('messageto', 'message-to-select', $preselect, 4, 10);
257 $tpl = get_markup_template('prv_message.tpl');
258 $o .= replace_macros($tpl, [
259 '$header' => L10n::t('Send Private Message'),
260 '$to' => L10n::t('To:'),
261 '$showinputs' => 'true',
262 '$prefill' => $prefill,
264 '$subject' => L10n::t('Subject:'),
265 '$subjtxt' => x($_REQUEST, 'subject') ? strip_tags($_REQUEST['subject']) : '',
266 '$text' => x($_REQUEST, 'body') ? escape_tags(htmlspecialchars($_REQUEST['body'])) : '',
268 '$yourmessage' => L10n::t('Your message:'),
269 '$select' => $select,
271 '$upload' => L10n::t('Upload photo'),
272 '$insert' => L10n::t('Insert web link'),
273 '$wait' => L10n::t('Please wait'),
274 '$submit' => L10n::t('Submit')
280 $_SESSION['return_url'] = $a->query_string;
288 $r = q("SELECT count(*) AS `total`, ANY_VALUE(`created`) AS `created` FROM `mail`
289 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
293 if (DBA::isResult($r)) {
294 $a->set_pager_total($r[0]['total']);
297 $r = get_messages(local_user(), $a->pager['start'], $a->pager['itemspage']);
299 if (!DBA::isResult($r)) {
300 info(L10n::t('No messages.') . EOL);
304 $o .= render_messages($r, 'mail_list.tpl');
311 if (($a->argc > 1) && (intval($a->argv[1]))) {
315 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
316 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
317 WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
318 intval(local_user()),
321 if (DBA::isResult($r)) {
322 $contact_id = $r[0]['contact-id'];
323 $convid = $r[0]['convid'];
325 $sql_extra = sprintf(" and `mail`.`parent-uri` = '%s' ", DBA::escape($r[0]['parent-uri']));
327 $sql_extra = sprintf(" and ( `mail`.`parent-uri` = '%s' OR `mail`.`convid` = '%d' ) ",
328 DBA::escape($r[0]['parent-uri']),
332 $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
333 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
334 WHERE `mail`.`uid` = %d $sql_extra ORDER BY `mail`.`created` ASC",
340 if (!DBA::isResult($messages)) {
341 notice(L10n::t('Message not available.') . EOL);
345 $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
346 DBA::escape($r[0]['parent-uri']),
350 $tpl = get_markup_template('msg-header.tpl');
351 $a->page['htmlhead'] .= replace_macros($tpl, [
352 '$baseurl' => System::baseUrl(true),
353 '$nickname' => $a->user['nickname'],
354 '$linkurl' => L10n::t('Please enter a link URL:')
357 $tpl = get_markup_template('msg-end.tpl');
358 $a->page['end'] .= replace_macros($tpl, [
359 '$baseurl' => System::baseUrl(true),
360 '$nickname' => $a->user['nickname'],
361 '$linkurl' => L10n::t('Please enter a link URL:')
368 foreach ($messages as $message) {
369 if ($message['unknown'])
371 if ($message['from-url'] == $myprofile) {
372 $from_url = $myprofile;
375 $from_url = Contact::magicLink($message['from-url']);
376 $sparkle = ' sparkle';
379 $extracted = item_extract_images($message['body']);
380 if ($extracted['images']) {
381 $message['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $message['contact-id']);
384 $from_name_e = $message['from-name'];
385 $subject_e = $message['title'];
386 $body_e = Smilies::replace(BBCode::convert($message['body']));
387 $to_name_e = $message['name'];
389 $contact = Contact::getDetailsByURL($message['from-url']);
390 if (isset($contact["thumb"])) {
391 $from_photo = $contact["thumb"];
393 $from_photo = $message['from-photo'];
397 'id' => $message['id'],
398 'from_name' => $from_name_e,
399 'from_url' => $from_url,
400 'from_addr' => $contact['addr'],
401 'sparkle' => $sparkle,
402 'from_photo' => ProxyUtils::proxifyUrl($from_photo, false, ProxyUtils::SIZE_THUMB),
403 'subject' => $subject_e,
405 'delete' => L10n::t('Delete message'),
406 'to_name' => $to_name_e,
407 'date' => DateTimeFormat::local($message['created'], L10n::t('D, d M Y - g:i A')),
408 'ago' => Temporal::getRelativeDate($message['created']),
411 $seen = $message['seen'];
414 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
415 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
417 $tpl = get_markup_template('mail_display.tpl');
418 $o = replace_macros($tpl, [
419 '$thread_id' => $a->argv[1],
420 '$thread_subject' => $message['title'],
421 '$thread_seen' => $seen,
422 '$delete' => L10n::t('Delete conversation'),
423 '$canreply' => (($unknown) ? false : '1'),
424 '$unknown_text' => L10n::t("No secure communications available. You <strong>may</strong> be able to respond from the sender's profile page."),
428 '$header' => L10n::t('Send Reply'),
429 '$to' => L10n::t('To:'),
431 '$subject' => L10n::t('Subject:'),
432 '$subjtxt' => $message['title'],
433 '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
434 '$yourmessage' => L10n::t('Your message:'),
436 '$select' => $select,
437 '$parent' => $parent,
438 '$upload' => L10n::t('Upload photo'),
439 '$insert' => L10n::t('Insert web link'),
440 '$submit' => L10n::t('Submit'),
441 '$wait' => L10n::t('Please wait')
448 function get_messages($user, $lstart, $lend)
450 //TODO: rewritte with a sub-query to get the first message of each private thread with certainty
451 return q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`,
452 ANY_VALUE(`mail`.`id`) AS `id`, ANY_VALUE(`mail`.`uid`) AS `uid`, ANY_VALUE(`mail`.`guid`) AS `guid`,
453 ANY_VALUE(`mail`.`from-name`) AS `from-name`, ANY_VALUE(`mail`.`from-photo`) AS `from-photo`,
454 ANY_VALUE(`mail`.`from-url`) AS `from-url`, ANY_VALUE(`mail`.`contact-id`) AS `contact-id`,
455 ANY_VALUE(`mail`.`convid`) AS `convid`, ANY_VALUE(`mail`.`title`) AS `title`, ANY_VALUE(`mail`.`body`) AS `body`,
456 ANY_VALUE(`mail`.`seen`) AS `seen`, ANY_VALUE(`mail`.`reply`) AS `reply`, ANY_VALUE(`mail`.`replied`) AS `replied`,
457 ANY_VALUE(`mail`.`unknown`) AS `unknown`, ANY_VALUE(`mail`.`uri`) AS `uri`,
459 ANY_VALUE(`mail`.`created`) AS `created`, ANY_VALUE(`contact`.`name`) AS `name`, ANY_VALUE(`contact`.`url`) AS `url`,
460 ANY_VALUE(`contact`.`thumb`) AS `thumb`, ANY_VALUE(`contact`.`network`) AS `network`,
461 count( * ) as `count`
462 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
463 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `mailcreated` DESC LIMIT %d , %d ",
464 intval($user), intval($lstart), intval($lend)
468 function render_messages(array $msg, $t)
472 $tpl = get_markup_template($t);
475 $myprofile = System::baseUrl() . '/profile/' . $a->user['nickname'];
477 foreach ($msg as $rr) {
478 if ($rr['unknown']) {
479 $participants = L10n::t("Unknown sender - %s", $rr['from-name']);
480 } elseif (link_compare($rr['from-url'], $myprofile)) {
481 $participants = L10n::t("You and %s", $rr['name']);
483 $participants = L10n::t("%s and You", $rr['from-name']);
486 $subject_e = (($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>');
487 $body_e = $rr['body'];
488 $to_name_e = $rr['name'];
490 $contact = Contact::getDetailsByURL($rr['url']);
491 if (isset($contact["thumb"])) {
492 $from_photo = $contact["thumb"];
494 $from_photo = (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']);
497 $rslt .= replace_macros($tpl, [
499 '$from_name' => $participants,
500 '$from_url' => Contact::magicLink($rr['url']),
501 '$from_addr' => defaults($contact, 'addr', ''),
502 '$sparkle' => ' sparkle',
503 '$from_photo' => ProxyUtils::proxifyUrl($from_photo, false, ProxyUtils::SIZE_THUMB),
504 '$subject' => $subject_e,
505 '$delete' => L10n::t('Delete conversation'),
507 '$to_name' => $to_name_e,
508 '$date' => DateTimeFormat::local($rr['mailcreated'], L10n::t('D, d M Y - g:i A')),
509 '$ago' => Temporal::getRelativeDate($rr['mailcreated']),
510 '$seen' => $rr['mailseen'],
511 '$count' => L10n::tt('%d message', '%d messages', $rr['count']),