3 * @file mod/message.php
7 use Friendica\Content\Nav;
8 use Friendica\Content\Pager;
9 use Friendica\Content\Text\BBCode;
10 use Friendica\Core\ACL;
11 use Friendica\Core\L10n;
12 use Friendica\Core\Renderer;
13 use Friendica\Database\DBA;
15 use Friendica\Model\Contact;
16 use Friendica\Model\Mail;
17 use Friendica\Module\Security\Login;
18 use Friendica\Util\DateTimeFormat;
19 use Friendica\Util\Proxy as ProxyUtils;
20 use Friendica\Util\Strings;
21 use Friendica\Util\Temporal;
23 function message_init(App $a)
27 if ($a->argc > 1 && is_numeric($a->argv[1])) {
28 $tabs = render_messages(get_messages(local_user(), 0, 5), 'mail_list.tpl');
32 'label' => L10n::t('New Message'),
33 'url' => 'message/new',
34 'sel' => $a->argc > 1 && $a->argv[1] == 'new',
38 $tpl = Renderer::getMarkupTemplate('message_side.tpl');
39 DI::page()['aside'] = Renderer::replaceMacros($tpl, [
43 $base = DI::baseUrl();
45 $head_tpl = Renderer::getMarkupTemplate('message-head.tpl');
46 DI::page()['htmlhead'] .= Renderer::replaceMacros($head_tpl, [
47 '$baseurl' => DI::baseUrl()->get(true),
52 function message_post(App $a)
55 notice(L10n::t('Permission denied.') . EOL);
59 $replyto = !empty($_REQUEST['replyto']) ? Strings::escapeTags(trim($_REQUEST['replyto'])) : '';
60 $subject = !empty($_REQUEST['subject']) ? Strings::escapeTags(trim($_REQUEST['subject'])) : '';
61 $body = !empty($_REQUEST['body']) ? Strings::escapeHtml(trim($_REQUEST['body'])) : '';
62 $recipient = !empty($_REQUEST['messageto']) ? intval($_REQUEST['messageto']) : 0;
64 $ret = Mail::send($recipient, $body, $subject, $replyto);
69 notice(L10n::t('No recipient selected.') . EOL);
73 notice(L10n::t('Unable to locate contact information.') . EOL);
76 notice(L10n::t('Message could not be sent.') . EOL);
79 notice(L10n::t('Message collection failure.') . EOL);
82 info(L10n::t('Message sent.') . EOL);
85 // fake it to go back to the input form if no recipient listed
90 DI::baseUrl()->redirect(DI::args()->getCommand() . '/' . $ret);
94 function message_content(App $a)
97 Nav::setSelected('messages');
100 notice(L10n::t('Permission denied.') . EOL);
101 return Login::form();
104 $myprofile = DI::baseUrl() . '/profile/' . $a->user['nickname'];
106 $tpl = Renderer::getMarkupTemplate('mail_head.tpl');
107 if ($a->argc > 1 && $a->argv[1] == 'new') {
109 'label' => L10n::t('Discard'),
115 'label' => L10n::t('New Message'),
116 'url' => '/message/new',
121 $header = Renderer::replaceMacros($tpl, [
122 '$messages' => L10n::t('Messages'),
123 '$button' => $button,
126 if (($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) {
127 if (!intval($a->argv[2])) {
131 // Check if we should do HTML-based delete confirmation
132 if (!empty($_REQUEST['confirm'])) {
133 // <form> can't take arguments in its "action" parameter
134 // so add any arguments as hidden inputs
135 $query = explode_querystring(DI::args()->getQueryString());
137 foreach ($query['args'] as $arg) {
138 if (strpos($arg, 'confirm=') === false) {
139 $arg_parts = explode('=', $arg);
140 $inputs[] = ['name' => $arg_parts[0], 'value' => $arg_parts[1]];
144 //DI::page()['aside'] = '';
145 return Renderer::replaceMacros(Renderer::getMarkupTemplate('confirm.tpl'), [
147 '$message' => L10n::t('Do you really want to delete this message?'),
148 '$extra_inputs' => $inputs,
149 '$confirm' => L10n::t('Yes'),
150 '$confirm_url' => $query['base'],
151 '$confirm_name' => 'confirmed',
152 '$cancel' => L10n::t('Cancel'),
156 // Now check how the user responded to the confirmation query
157 if (!empty($_REQUEST['canceled'])) {
158 DI::baseUrl()->redirect('message');
162 if ($cmd === 'drop') {
163 $message = DBA::selectFirst('mail', ['convid'], ['id' => $a->argv[2], 'uid' => local_user()]);
164 if(!DBA::isResult($message)){
165 info(L10n::t('Conversation not found.') . EOL);
166 DI::baseUrl()->redirect('message');
169 if (DBA::delete('mail', ['id' => $a->argv[2], 'uid' => local_user()])) {
170 info(L10n::t('Message deleted.') . EOL);
173 $conversation = DBA::selectFirst('mail', ['id'], ['convid' => $message['convid'], 'uid' => local_user()]);
174 if(!DBA::isResult($conversation)){
175 info(L10n::t('Conversation removed.') . EOL);
176 DI::baseUrl()->redirect('message');
179 DI::baseUrl()->redirect('message/' . $conversation['id'] );
181 $r = q("SELECT `parent-uri`,`convid` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
185 if (DBA::isResult($r)) {
186 $parent = $r[0]['parent-uri'];
188 if (DBA::delete('mail', ['parent-uri' => $parent, 'uid' => local_user()])) {
189 info(L10n::t('Conversation removed.') . EOL);
192 DI::baseUrl()->redirect('message');
196 if (($a->argc > 1) && ($a->argv[1] === 'new')) {
199 $tpl = Renderer::getMarkupTemplate('msg-header.tpl');
200 DI::page()['htmlhead'] .= Renderer::replaceMacros($tpl, [
201 '$baseurl' => DI::baseUrl()->get(true),
202 '$nickname' => $a->user['nickname'],
203 '$linkurl' => L10n::t('Please enter a link URL:')
206 $preselect = isset($a->argv[2]) ? [$a->argv[2]] : [];
208 $prename = $preurl = $preid = '';
211 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `id` = %d LIMIT 1",
212 intval(local_user()),
215 if (!DBA::isResult($r)) {
216 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' LIMIT 1",
217 intval(local_user()),
218 DBA::escape(Strings::normaliseLink(base64_decode($a->argv[2])))
222 if (!DBA::isResult($r)) {
223 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `addr` = '%s' LIMIT 1",
224 intval(local_user()),
225 DBA::escape(base64_decode($a->argv[2]))
229 if (DBA::isResult($r)) {
230 $prename = $r[0]['name'];
231 $preid = $r[0]['id'];
232 $preselect = [$preid];
238 $prefill = $preselect ? $prename : '';
240 // the ugly select box
241 $select = ACL::getMessageContactSelectHTML('messageto', 'message-to-select', $preselect, 4, 10);
243 $tpl = Renderer::getMarkupTemplate('prv_message.tpl');
244 $o .= Renderer::replaceMacros($tpl, [
245 '$header' => L10n::t('Send Private Message'),
246 '$to' => L10n::t('To:'),
247 '$showinputs' => 'true',
248 '$prefill' => $prefill,
250 '$subject' => L10n::t('Subject:'),
251 '$subjtxt' => $_REQUEST['subject'] ?? '',
252 '$text' => $_REQUEST['body'] ?? '',
254 '$yourmessage'=> L10n::t('Your message:'),
255 '$select' => $select,
257 '$upload' => L10n::t('Upload photo'),
258 '$insert' => L10n::t('Insert web link'),
259 '$wait' => L10n::t('Please wait'),
260 '$submit' => L10n::t('Submit')
266 $_SESSION['return_path'] = DI::args()->getQueryString();
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",
279 if (DBA::isResult($r)) {
280 $total = $r[0]['total'];
283 $pager = new Pager(DI::args()->getQueryString());
285 $r = get_messages(local_user(), $pager->getStart(), $pager->getItemsPerPage());
287 if (!DBA::isResult($r)) {
288 info(L10n::t('No messages.') . EOL);
292 $o .= render_messages($r, 'mail_list.tpl');
294 $o .= $pager->renderFull($total);
299 if (($a->argc > 1) && (intval($a->argv[1]))) {
303 $message = DBA::fetchFirst("
304 SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
306 LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
307 WHERE `mail`.`uid` = ? AND `mail`.`id` = ?
312 if (DBA::isResult($message)) {
313 $contact_id = $message['contact-id'];
317 $message['parent-uri']
320 if ($message['convid']) {
321 $sql_extra = "AND (`mail`.`parent-uri` = ? OR `mail`.`convid` = ?)";
322 $params[] = $message['convid'];
324 $sql_extra = "AND `mail`.`parent-uri` = ?";
326 $messages_stmt = DBA::p("
327 SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
329 LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
330 WHERE `mail`.`uid` = ?
332 ORDER BY `mail`.`created` ASC",
336 $messages = DBA::toArray($messages_stmt);
338 DBA::update('mail', ['seen' => 1], ['parent-uri' => $message['parent-uri'], 'uid' => local_user()]);
340 if ($message['convid']) {
341 // Clear Diaspora private message notifications
342 DBA::update('notify', ['seen' => 1], ['type' => NOTIFY_MAIL, 'parent' => $message['convid'], 'uid' => local_user()]);
344 // Clear DFRN private message notifications
345 DBA::update('notify', ['seen' => 1], ['type' => NOTIFY_MAIL, 'parent' => $message['parent-uri'], 'uid' => local_user()]);
350 if (!DBA::isResult($messages)) {
351 notice(L10n::t('Message not available.') . EOL);
355 $tpl = Renderer::getMarkupTemplate('msg-header.tpl');
356 DI::page()['htmlhead'] .= Renderer::replaceMacros($tpl, [
357 '$baseurl' => DI::baseUrl()->get(true),
358 '$nickname' => $a->user['nickname'],
359 '$linkurl' => L10n::t('Please enter a link URL:')
366 foreach ($messages as $message) {
367 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 = 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 = Renderer::getMarkupTemplate('mail_display.tpl');
418 $o = Renderer::replaceMacros($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')
454 function get_messages($uid, $start, $limit)
456 return DBA::toArray(DBA::p('SELECT
487 MAX(`created`) AS `mailcreated`,
488 MIN(`seen`) AS `mailseen`
491 GROUP BY `parent-uri`
492 ) m2 ON m.`parent-uri` = m2.`parent-uri` AND m.`id` = m2.`id`
493 LEFT JOIN `contact` c ON m.`contact-id` = c.`id`
495 ORDER BY m2.`mailcreated` DESC
497 , $uid, $uid, $start, $limit));
500 function render_messages(array $msg, $t)
504 $tpl = Renderer::getMarkupTemplate($t);
507 $myprofile = DI::baseUrl() . '/profile/' . $a->user['nickname'];
509 foreach ($msg as $rr) {
510 if ($rr['unknown']) {
511 $participants = L10n::t("Unknown sender - %s", $rr['from-name']);
512 } elseif (Strings::compareLink($rr['from-url'], $myprofile)) {
513 $participants = L10n::t("You and %s", $rr['name']);
515 $participants = L10n::t("%s and You", $rr['from-name']);
518 $body_e = $rr['body'];
519 $to_name_e = $rr['name'];
521 $contact = Contact::getDetailsByURL($rr['url']);
522 if (isset($contact["thumb"])) {
523 $from_photo = $contact["thumb"];
525 $from_photo = (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']);
528 $rslt .= Renderer::replaceMacros($tpl, [
530 '$from_name' => $participants,
531 '$from_url' => Contact::magicLink($rr['url']),
532 '$from_addr' => $contact['addr'] ?? '',
533 '$sparkle' => ' sparkle',
534 '$from_photo' => ProxyUtils::proxifyUrl($from_photo, false, ProxyUtils::SIZE_THUMB),
535 '$subject' => $rr['title'],
536 '$delete' => L10n::t('Delete conversation'),
538 '$to_name' => $to_name_e,
539 '$date' => DateTimeFormat::local($rr['mailcreated'], L10n::t('D, d M Y - g:i A')),
540 '$ago' => Temporal::getRelativeDate($rr['mailcreated']),
541 '$seen' => $rr['mailseen'],
542 '$count' => L10n::tt('%d message', '%d messages', $rr['count']),