3 * @file mod/message.php
7 use Friendica\Content\Nav;
8 use Friendica\Content\Pager;
9 use Friendica\Content\Smilies;
10 use Friendica\Content\Text\BBCode;
11 use Friendica\Core\ACL;
12 use Friendica\Core\L10n;
13 use Friendica\Core\Renderer;
14 use Friendica\Core\System;
15 use Friendica\Database\DBA;
17 use Friendica\Model\Contact;
18 use Friendica\Model\Mail;
19 use Friendica\Module\Security\Login;
20 use Friendica\Util\DateTimeFormat;
21 use Friendica\Util\Proxy as ProxyUtils;
22 use Friendica\Util\Strings;
23 use Friendica\Util\Temporal;
25 function message_init(App $a)
29 if ($a->argc > 1 && is_numeric($a->argv[1])) {
30 $tabs = render_messages(get_messages(local_user(), 0, 5), 'mail_list.tpl');
34 'label' => L10n::t('New Message'),
35 'url' => 'message/new',
36 'sel' => $a->argc > 1 && $a->argv[1] == 'new',
40 $tpl = Renderer::getMarkupTemplate('message_side.tpl');
41 DI::page()['aside'] = Renderer::replaceMacros($tpl, [
45 $base = System::baseUrl();
47 $head_tpl = Renderer::getMarkupTemplate('message-head.tpl');
48 DI::page()['htmlhead'] .= Renderer::replaceMacros($head_tpl, [
49 '$baseurl' => System::baseUrl(true),
54 function message_post(App $a)
57 notice(L10n::t('Permission denied.') . EOL);
61 $replyto = !empty($_REQUEST['replyto']) ? Strings::escapeTags(trim($_REQUEST['replyto'])) : '';
62 $subject = !empty($_REQUEST['subject']) ? Strings::escapeTags(trim($_REQUEST['subject'])) : '';
63 $body = !empty($_REQUEST['body']) ? Strings::escapeHtml(trim($_REQUEST['body'])) : '';
64 $recipient = !empty($_REQUEST['messageto']) ? intval($_REQUEST['messageto']) : 0;
66 $ret = Mail::send($recipient, $body, $subject, $replyto);
71 notice(L10n::t('No recipient selected.') . EOL);
75 notice(L10n::t('Unable to locate contact information.') . EOL);
78 notice(L10n::t('Message could not be sent.') . EOL);
81 notice(L10n::t('Message collection failure.') . EOL);
84 info(L10n::t('Message sent.') . EOL);
87 // fake it to go back to the input form if no recipient listed
92 DI::baseUrl()->redirect(DI::args()->getCommand() . '/' . $ret);
96 function message_content(App $a)
99 Nav::setSelected('messages');
102 notice(L10n::t('Permission denied.') . EOL);
103 return Login::form();
106 $myprofile = System::baseUrl() . '/profile/' . $a->user['nickname'];
108 $tpl = Renderer::getMarkupTemplate('mail_head.tpl');
109 if ($a->argc > 1 && $a->argv[1] == 'new') {
111 'label' => L10n::t('Discard'),
117 'label' => L10n::t('New Message'),
118 'url' => '/message/new',
123 $header = Renderer::replaceMacros($tpl, [
124 '$messages' => L10n::t('Messages'),
125 '$button' => $button,
128 if (($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) {
129 if (!intval($a->argv[2])) {
133 // Check if we should do HTML-based delete confirmation
134 if (!empty($_REQUEST['confirm'])) {
135 // <form> can't take arguments in its "action" parameter
136 // so add any arguments as hidden inputs
137 $query = explode_querystring(DI::args()->getQueryString());
139 foreach ($query['args'] as $arg) {
140 if (strpos($arg, 'confirm=') === false) {
141 $arg_parts = explode('=', $arg);
142 $inputs[] = ['name' => $arg_parts[0], 'value' => $arg_parts[1]];
146 //DI::page()['aside'] = '';
147 return Renderer::replaceMacros(Renderer::getMarkupTemplate('confirm.tpl'), [
149 '$message' => L10n::t('Do you really want to delete this message?'),
150 '$extra_inputs' => $inputs,
151 '$confirm' => L10n::t('Yes'),
152 '$confirm_url' => $query['base'],
153 '$confirm_name' => 'confirmed',
154 '$cancel' => L10n::t('Cancel'),
158 // Now check how the user responded to the confirmation query
159 if (!empty($_REQUEST['canceled'])) {
160 DI::baseUrl()->redirect('message');
164 if ($cmd === 'drop') {
165 $message = DBA::selectFirst('mail', ['convid'], ['id' => $a->argv[2], 'uid' => local_user()]);
166 if(!DBA::isResult($message)){
167 info(L10n::t('Conversation not found.') . EOL);
168 DI::baseUrl()->redirect('message');
171 if (DBA::delete('mail', ['id' => $a->argv[2], 'uid' => local_user()])) {
172 info(L10n::t('Message deleted.') . EOL);
175 $conversation = DBA::selectFirst('mail', ['id'], ['convid' => $message['convid'], 'uid' => local_user()]);
176 if(!DBA::isResult($conversation)){
177 info(L10n::t('Conversation removed.') . EOL);
178 DI::baseUrl()->redirect('message');
181 DI::baseUrl()->redirect('message/' . $conversation['id'] );
183 $r = q("SELECT `parent-uri`,`convid` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
187 if (DBA::isResult($r)) {
188 $parent = $r[0]['parent-uri'];
190 if (DBA::delete('mail', ['parent-uri' => $parent, 'uid' => local_user()])) {
191 info(L10n::t('Conversation removed.') . EOL);
194 DI::baseUrl()->redirect('message');
198 if (($a->argc > 1) && ($a->argv[1] === 'new')) {
201 $tpl = Renderer::getMarkupTemplate('msg-header.tpl');
202 DI::page()['htmlhead'] .= Renderer::replaceMacros($tpl, [
203 '$baseurl' => System::baseUrl(true),
204 '$nickname' => $a->user['nickname'],
205 '$linkurl' => L10n::t('Please enter a link URL:')
208 $preselect = isset($a->argv[2]) ? [$a->argv[2]] : [];
210 $prename = $preurl = $preid = '';
213 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `id` = %d LIMIT 1",
214 intval(local_user()),
217 if (!DBA::isResult($r)) {
218 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' LIMIT 1",
219 intval(local_user()),
220 DBA::escape(Strings::normaliseLink(base64_decode($a->argv[2])))
224 if (!DBA::isResult($r)) {
225 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `addr` = '%s' LIMIT 1",
226 intval(local_user()),
227 DBA::escape(base64_decode($a->argv[2]))
231 if (DBA::isResult($r)) {
232 $prename = $r[0]['name'];
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 = Renderer::getMarkupTemplate('prv_message.tpl');
246 $o .= Renderer::replaceMacros($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' => $_REQUEST['subject'] ?? '',
254 '$text' => $_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_path'] = DI::args()->getQueryString();
277 $r = q("SELECT count(*) AS `total`, ANY_VALUE(`created`) AS `created` FROM `mail`
278 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
281 if (DBA::isResult($r)) {
282 $total = $r[0]['total'];
285 $pager = new Pager(DI::args()->getQueryString());
287 $r = get_messages(local_user(), $pager->getStart(), $pager->getItemsPerPage());
289 if (!DBA::isResult($r)) {
290 info(L10n::t('No messages.') . EOL);
294 $o .= render_messages($r, 'mail_list.tpl');
296 $o .= $pager->renderFull($total);
301 if (($a->argc > 1) && (intval($a->argv[1]))) {
305 $message = DBA::fetchFirst("
306 SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
308 LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
309 WHERE `mail`.`uid` = ? AND `mail`.`id` = ?
314 if (DBA::isResult($message)) {
315 $contact_id = $message['contact-id'];
319 $message['parent-uri']
322 if ($message['convid']) {
323 $sql_extra = "AND (`mail`.`parent-uri` = ? OR `mail`.`convid` = ?)";
324 $params[] = $message['convid'];
326 $sql_extra = "AND `mail`.`parent-uri` = ?";
328 $messages_stmt = DBA::p("
329 SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
331 LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
332 WHERE `mail`.`uid` = ?
334 ORDER BY `mail`.`created` ASC",
338 $messages = DBA::toArray($messages_stmt);
340 DBA::update('mail', ['seen' => 1], ['parent-uri' => $message['parent-uri'], 'uid' => local_user()]);
342 if ($message['convid']) {
343 // Clear Diaspora private message notifications
344 DBA::update('notify', ['seen' => 1], ['type' => NOTIFY_MAIL, 'parent' => $message['convid'], 'uid' => local_user()]);
346 // Clear DFRN private message notifications
347 DBA::update('notify', ['seen' => 1], ['type' => NOTIFY_MAIL, 'parent' => $message['parent-uri'], 'uid' => local_user()]);
352 if (!DBA::isResult($messages)) {
353 notice(L10n::t('Message not available.') . EOL);
357 $tpl = Renderer::getMarkupTemplate('msg-header.tpl');
358 DI::page()['htmlhead'] .= Renderer::replaceMacros($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']) {
373 if ($message['from-url'] == $myprofile) {
374 $from_url = $myprofile;
377 $from_url = Contact::magicLink($message['from-url']);
378 $sparkle = ' sparkle';
381 $extracted = item_extract_images($message['body']);
382 if ($extracted['images']) {
383 $message['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $message['contact-id']);
386 $from_name_e = $message['from-name'];
387 $subject_e = $message['title'];
388 $body_e = BBCode::convert($message['body']);
389 $to_name_e = $message['name'];
391 $contact = Contact::getDetailsByURL($message['from-url']);
392 if (isset($contact["thumb"])) {
393 $from_photo = $contact["thumb"];
395 $from_photo = $message['from-photo'];
399 'id' => $message['id'],
400 'from_name' => $from_name_e,
401 'from_url' => $from_url,
402 'from_addr' => $contact['addr'],
403 'sparkle' => $sparkle,
404 'from_photo' => ProxyUtils::proxifyUrl($from_photo, false, ProxyUtils::SIZE_THUMB),
405 'subject' => $subject_e,
407 'delete' => L10n::t('Delete message'),
408 'to_name' => $to_name_e,
409 'date' => DateTimeFormat::local($message['created'], L10n::t('D, d M Y - g:i A')),
410 'ago' => Temporal::getRelativeDate($message['created']),
413 $seen = $message['seen'];
416 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
417 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
419 $tpl = Renderer::getMarkupTemplate('mail_display.tpl');
420 $o = Renderer::replaceMacros($tpl, [
421 '$thread_id' => $a->argv[1],
422 '$thread_subject' => $message['title'],
423 '$thread_seen' => $seen,
424 '$delete' => L10n::t('Delete conversation'),
425 '$canreply' => (($unknown) ? false : '1'),
426 '$unknown_text' => L10n::t("No secure communications available. You <strong>may</strong> be able to respond from the sender's profile page."),
430 '$header' => L10n::t('Send Reply'),
431 '$to' => L10n::t('To:'),
433 '$subject' => L10n::t('Subject:'),
434 '$subjtxt' => $message['title'],
435 '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
436 '$yourmessage' => L10n::t('Your message:'),
438 '$select' => $select,
439 '$parent' => $parent,
440 '$upload' => L10n::t('Upload photo'),
441 '$insert' => L10n::t('Insert web link'),
442 '$submit' => L10n::t('Submit'),
443 '$wait' => L10n::t('Please wait')
456 function get_messages($uid, $start, $limit)
458 return DBA::toArray(DBA::p('SELECT
489 MAX(`created`) AS `mailcreated`,
490 MIN(`seen`) AS `mailseen`
493 GROUP BY `parent-uri`
494 ) m2 ON m.`parent-uri` = m2.`parent-uri` AND m.`id` = m2.`id`
495 LEFT JOIN `contact` c ON m.`contact-id` = c.`id`
497 ORDER BY m2.`mailcreated` DESC
499 , $uid, $uid, $start, $limit));
502 function render_messages(array $msg, $t)
506 $tpl = Renderer::getMarkupTemplate($t);
509 $myprofile = System::baseUrl() . '/profile/' . $a->user['nickname'];
511 foreach ($msg as $rr) {
512 if ($rr['unknown']) {
513 $participants = L10n::t("Unknown sender - %s", $rr['from-name']);
514 } elseif (Strings::compareLink($rr['from-url'], $myprofile)) {
515 $participants = L10n::t("You and %s", $rr['name']);
517 $participants = L10n::t("%s and You", $rr['from-name']);
520 $body_e = $rr['body'];
521 $to_name_e = $rr['name'];
523 $contact = Contact::getDetailsByURL($rr['url']);
524 if (isset($contact["thumb"])) {
525 $from_photo = $contact["thumb"];
527 $from_photo = (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']);
530 $rslt .= Renderer::replaceMacros($tpl, [
532 '$from_name' => $participants,
533 '$from_url' => Contact::magicLink($rr['url']),
534 '$from_addr' => $contact['addr'] ?? '',
535 '$sparkle' => ' sparkle',
536 '$from_photo' => ProxyUtils::proxifyUrl($from_photo, false, ProxyUtils::SIZE_THUMB),
537 '$subject' => $rr['title'],
538 '$delete' => L10n::t('Delete conversation'),
540 '$to_name' => $to_name_e,
541 '$date' => DateTimeFormat::local($rr['mailcreated'], L10n::t('D, d M Y - g:i A')),
542 '$ago' => Temporal::getRelativeDate($rr['mailcreated']),
543 '$seen' => $rr['mailseen'],
544 '$count' => L10n::tt('%d message', '%d messages', $rr['count']),