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\DBM;
14 use Friendica\Model\Contact;
15 use Friendica\Model\Mail;
16 use Friendica\Util\DateTimeFormat;
17 use Friendica\Util\Temporal;
19 require_once 'include/acl_selectors.php';
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($_SESSION['return_url']);
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 $header = replace_macros($tpl, [
113 '$messages' => L10n::t('Messages'),
116 if (($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) {
117 if (!intval($a->argv[2])) {
121 // Check if we should do HTML-based delete confirmation
122 if ($_REQUEST['confirm']) {
123 // <form> can't take arguments in its "action" parameter
124 // so add any arguments as hidden inputs
125 $query = explode_querystring($a->query_string);
127 foreach ($query['args'] as $arg) {
128 if (strpos($arg, 'confirm=') === false) {
129 $arg_parts = explode('=', $arg);
130 $inputs[] = ['name' => $arg_parts[0], 'value' => $arg_parts[1]];
134 //$a->page['aside'] = '';
135 return replace_macros(get_markup_template('confirm.tpl'), [
137 '$message' => L10n::t('Do you really want to delete this message?'),
138 '$extra_inputs' => $inputs,
139 '$confirm' => L10n::t('Yes'),
140 '$confirm_url' => $query['base'],
141 '$confirm_name' => 'confirmed',
142 '$cancel' => L10n::t('Cancel'),
145 // Now check how the user responded to the confirmation query
146 if ($_REQUEST['canceled']) {
147 goaway($_SESSION['return_url']);
151 if ($cmd === 'drop') {
152 $r = q("DELETE FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
157 info(L10n::t('Message deleted.') . EOL);
159 //goaway(System::baseUrl(true) . '/message' );
160 goaway($_SESSION['return_url']);
162 $r = q("SELECT `parent-uri`,`convid` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
166 if (DBM::is_result($r)) {
167 $parent = $r[0]['parent-uri'];
168 $convid = $r[0]['convid'];
170 $r = q("DELETE FROM `mail` WHERE `parent-uri` = '%s' AND `uid` = %d ",
175 // remove diaspora conversation pointer
176 // Actually if we do this, we can never receive another reply to that conversation,
177 // as we will never again have the info we need to re-create it.
178 // We'll just have to orphan it.
180 // q("delete from conv where id = %d limit 1",
186 info(L10n::t('Conversation removed.') . EOL);
189 //goaway(System::baseUrl(true) . '/message' );
190 goaway($_SESSION['return_url']);
194 if (($a->argc > 1) && ($a->argv[1] === 'new')) {
197 $tpl = get_markup_template('msg-header.tpl');
198 $a->page['htmlhead'] .= replace_macros($tpl, [
199 '$baseurl' => System::baseUrl(true),
200 '$nickname' => $a->user['nickname'],
201 '$linkurl' => L10n::t('Please enter a link URL:')
204 $tpl = get_markup_template('msg-end.tpl');
205 $a->page['end'] .= replace_macros($tpl, [
206 '$baseurl' => System::baseUrl(true),
207 '$nickname' => $a->user['nickname'],
208 '$linkurl' => L10n::t('Please enter a link URL:')
211 $preselect = isset($a->argv[2]) ? [$a->argv[2]] : false;
213 $prename = $preurl = $preid = '';
216 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `id` = %d LIMIT 1",
217 intval(local_user()),
220 if (!DBM::is_result($r)) {
221 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' LIMIT 1",
222 intval(local_user()),
223 dbesc(normalise_link(base64_decode($a->argv[2])))
227 if (!DBM::is_result($r)) {
228 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `addr` = '%s' LIMIT 1",
229 intval(local_user()),
230 dbesc(base64_decode($a->argv[2]))
234 if (DBM::is_result($r)) {
235 $prename = $r[0]['name'];
236 $preurl = $r[0]['url'];
237 $preid = $r[0]['id'];
238 $preselect = [$preid];
244 $prefill = $preselect ? $prename : '';
246 // the ugly select box
247 $select = Acl::getMessageContactSelectHTML('messageto', 'message-to-select', $preselect, 4, 10);
249 $tpl = get_markup_template('prv_message.tpl');
250 $o .= replace_macros($tpl, [
251 '$header' => L10n::t('Send Private Message'),
252 '$to' => L10n::t('To:'),
253 '$showinputs' => 'true',
254 '$prefill' => $prefill,
256 '$subject' => L10n::t('Subject:'),
257 '$subjtxt' => x($_REQUEST, 'subject') ? strip_tags($_REQUEST['subject']) : '',
258 '$text' => x($_REQUEST, 'body') ? escape_tags(htmlspecialchars($_REQUEST['body'])) : '',
260 '$yourmessage' => L10n::t('Your message:'),
261 '$select' => $select,
263 '$upload' => L10n::t('Upload photo'),
264 '$insert' => L10n::t('Insert web link'),
265 '$wait' => L10n::t('Please wait'),
266 '$submit' => L10n::t('Submit')
272 $_SESSION['return_url'] = $a->query_string;
280 $r = q("SELECT count(*) AS `total`, ANY_VALUE(`created`) AS `created` FROM `mail`
281 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
285 if (DBM::is_result($r)) {
286 $a->set_pager_total($r[0]['total']);
289 $r = get_messages(local_user(), $a->pager['start'], $a->pager['itemspage']);
291 if (!DBM::is_result($r)) {
292 info(L10n::t('No messages.') . EOL);
296 $o .= render_messages($r, 'mail_list.tpl');
303 if (($a->argc > 1) && (intval($a->argv[1]))) {
307 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
308 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
309 WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
310 intval(local_user()),
313 if (DBM::is_result($r)) {
314 $contact_id = $r[0]['contact-id'];
315 $convid = $r[0]['convid'];
317 $sql_extra = sprintf(" and `mail`.`parent-uri` = '%s' ", dbesc($r[0]['parent-uri']));
319 $sql_extra = sprintf(" and ( `mail`.`parent-uri` = '%s' OR `mail`.`convid` = '%d' ) ",
320 dbesc($r[0]['parent-uri']),
324 $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
325 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
326 WHERE `mail`.`uid` = %d $sql_extra ORDER BY `mail`.`created` ASC",
330 if (!count($messages)) {
331 notice(L10n::t('Message not available.') . EOL);
335 $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
336 dbesc($r[0]['parent-uri']),
340 $tpl = get_markup_template('msg-header.tpl');
341 $a->page['htmlhead'] .= replace_macros($tpl, [
342 '$baseurl' => System::baseUrl(true),
343 '$nickname' => $a->user['nickname'],
344 '$linkurl' => L10n::t('Please enter a link URL:')
347 $tpl = get_markup_template('msg-end.tpl');
348 $a->page['end'] .= replace_macros($tpl, [
349 '$baseurl' => System::baseUrl(true),
350 '$nickname' => $a->user['nickname'],
351 '$linkurl' => L10n::t('Please enter a link URL:')
358 foreach ($messages as $message) {
359 if ($message['unknown'])
361 if ($message['from-url'] == $myprofile) {
362 $from_url = $myprofile;
364 } elseif ($message['contact-id'] != 0) {
365 $from_url = 'redir/' . $message['contact-id'];
366 $sparkle = ' sparkle';
368 $from_url = $message['from-url'] . "?zrl=" . urlencode($myprofile);
369 $sparkle = ' sparkle';
372 $extracted = item_extract_images($message['body']);
373 if ($extracted['images']) {
374 $message['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $message['contact-id']);
377 $from_name_e = $message['from-name'];
378 $subject_e = $message['title'];
379 $body_e = Smilies::replace(BBCode::convert($message['body']));
380 $to_name_e = $message['name'];
382 $contact = Contact::getDetailsByURL($message['from-url']);
383 if (isset($contact["thumb"])) {
384 $from_photo = $contact["thumb"];
386 $from_photo = $message['from-photo'];
390 'id' => $message['id'],
391 'from_name' => $from_name_e,
392 'from_url' => $from_url,
393 'from_addr' => $contact['addr'],
394 'sparkle' => $sparkle,
395 'from_photo' => proxy_url($from_photo, false, PROXY_SIZE_THUMB),
396 'subject' => $subject_e,
398 'delete' => L10n::t('Delete message'),
399 'to_name' => $to_name_e,
400 'date' => DateTimeFormat::local($message['created'], L10n::t('D, d M Y - g:i A')),
401 'ago' => Temporal::getRelativeDate($message['created']),
404 $seen = $message['seen'];
407 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
408 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
410 $tpl = get_markup_template('mail_display.tpl');
411 $o = replace_macros($tpl, [
412 '$thread_id' => $a->argv[1],
413 '$thread_subject' => $message['title'],
414 '$thread_seen' => $seen,
415 '$delete' => L10n::t('Delete conversation'),
416 '$canreply' => (($unknown) ? false : '1'),
417 '$unknown_text' => L10n::t("No secure communications available. You <strong>may</strong> be able to respond from the sender's profile page."),
421 '$header' => L10n::t('Send Reply'),
422 '$to' => L10n::t('To:'),
424 '$subject' => L10n::t('Subject:'),
425 '$subjtxt' => $message['title'],
426 '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
427 '$yourmessage' => L10n::t('Your message:'),
429 '$select' => $select,
430 '$parent' => $parent,
431 '$upload' => L10n::t('Upload photo'),
432 '$insert' => L10n::t('Insert web link'),
433 '$submit' => L10n::t('Submit'),
434 '$wait' => L10n::t('Please wait')
441 function get_messages($user, $lstart, $lend)
443 //TODO: rewritte with a sub-query to get the first message of each private thread with certainty
444 return q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`,
445 ANY_VALUE(`mail`.`id`) AS `id`, ANY_VALUE(`mail`.`uid`) AS `uid`, ANY_VALUE(`mail`.`guid`) AS `guid`,
446 ANY_VALUE(`mail`.`from-name`) AS `from-name`, ANY_VALUE(`mail`.`from-photo`) AS `from-photo`,
447 ANY_VALUE(`mail`.`from-url`) AS `from-url`, ANY_VALUE(`mail`.`contact-id`) AS `contact-id`,
448 ANY_VALUE(`mail`.`convid`) AS `convid`, ANY_VALUE(`mail`.`title`) AS `title`, ANY_VALUE(`mail`.`body`) AS `body`,
449 ANY_VALUE(`mail`.`seen`) AS `seen`, ANY_VALUE(`mail`.`reply`) AS `reply`, ANY_VALUE(`mail`.`replied`) AS `replied`,
450 ANY_VALUE(`mail`.`unknown`) AS `unknown`, ANY_VALUE(`mail`.`uri`) AS `uri`,
452 ANY_VALUE(`mail`.`created`) AS `created`, ANY_VALUE(`contact`.`name`) AS `name`, ANY_VALUE(`contact`.`url`) AS `url`,
453 ANY_VALUE(`contact`.`thumb`) AS `thumb`, ANY_VALUE(`contact`.`network`) AS `network`,
454 count( * ) as `count`
455 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
456 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `mailcreated` DESC LIMIT %d , %d ",
457 intval($user), intval($lstart), intval($lend)
461 function render_messages(array $msg, $t)
465 $tpl = get_markup_template($t);
468 $myprofile = System::baseUrl() . '/profile/' . $a->user['nickname'];
470 foreach ($msg as $rr) {
471 if ($rr['unknown']) {
472 $participants = L10n::t("Unknown sender - %s", $rr['from-name']);
473 } elseif (link_compare($rr['from-url'], $myprofile)) {
474 $participants = L10n::t("You and %s", $rr['name']);
476 $participants = L10n::t("%s and You", $rr['from-name']);
479 $subject_e = (($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>');
480 $body_e = $rr['body'];
481 $to_name_e = $rr['name'];
483 $contact = Contact::getDetailsByURL($rr['url']);
484 if (isset($contact["thumb"])) {
485 $from_photo = $contact["thumb"];
487 $from_photo = (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']);
490 $rslt .= replace_macros($tpl, [
492 '$from_name' => $participants,
493 '$from_url' => (($rr['network'] === NETWORK_DFRN) ? 'redir/' . $rr['contact-id'] : $rr['url']),
494 '$from_addr' => $contact['addr'],
495 '$sparkle' => ' sparkle',
496 '$from_photo' => proxy_url($from_photo, false, PROXY_SIZE_THUMB),
497 '$subject' => $subject_e,
498 '$delete' => L10n::t('Delete conversation'),
500 '$to_name' => $to_name_e,
501 '$date' => DateTimeFormat::local($rr['mailcreated'], L10n::t('D, d M Y - g:i A')),
502 '$ago' => Temporal::getRelativeDate($rr['mailcreated']),
503 '$seen' => $rr['mailseen'],
504 '$count' => L10n::tt('%d message', '%d messages', $rr['count']),