3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 use Friendica\Content\Nav;
24 use Friendica\Content\Pager;
25 use Friendica\Content\Text\BBCode;
26 use Friendica\Core\ACL;
27 use Friendica\Core\Renderer;
28 use Friendica\Database\DBA;
30 use Friendica\Model\Contact;
31 use Friendica\Model\Mail;
32 use Friendica\Model\Notify\Type;
33 use Friendica\Module\Security\Login;
34 use Friendica\Util\DateTimeFormat;
35 use Friendica\Util\Strings;
36 use Friendica\Util\Temporal;
38 function message_init(App $a)
42 if ($a->argc > 1 && is_numeric($a->argv[1])) {
43 $tabs = render_messages(get_messages(local_user(), 0, 5), 'mail_list.tpl');
47 'label' => DI::l10n()->t('New Message'),
48 'url' => 'message/new',
49 'sel' => $a->argc > 1 && $a->argv[1] == 'new',
53 $tpl = Renderer::getMarkupTemplate('message_side.tpl');
54 DI::page()['aside'] = Renderer::replaceMacros($tpl, [
58 $base = DI::baseUrl();
60 $head_tpl = Renderer::getMarkupTemplate('message-head.tpl');
61 DI::page()['htmlhead'] .= Renderer::replaceMacros($head_tpl, [
62 '$baseurl' => DI::baseUrl()->get(true),
67 function message_post(App $a)
70 notice(DI::l10n()->t('Permission denied.'));
74 $replyto = !empty($_REQUEST['replyto']) ? Strings::escapeTags(trim($_REQUEST['replyto'])) : '';
75 $subject = !empty($_REQUEST['subject']) ? Strings::escapeTags(trim($_REQUEST['subject'])) : '';
76 $body = !empty($_REQUEST['body']) ? Strings::escapeHtml(trim($_REQUEST['body'])) : '';
77 $recipient = !empty($_REQUEST['recipient']) ? intval($_REQUEST['recipient']) : 0;
79 $ret = Mail::send($recipient, $body, $subject, $replyto);
84 notice(DI::l10n()->t('No recipient selected.'));
88 notice(DI::l10n()->t('Unable to locate contact information.'));
91 notice(DI::l10n()->t('Message could not be sent.'));
94 notice(DI::l10n()->t('Message collection failure.'));
98 // fake it to go back to the input form if no recipient listed
103 DI::baseUrl()->redirect(DI::args()->getCommand() . '/' . $ret);
107 function message_content(App $a)
110 Nav::setSelected('messages');
113 notice(DI::l10n()->t('Permission denied.'));
114 return Login::form();
117 $myprofile = DI::baseUrl() . '/profile/' . $a->user['nickname'];
119 $tpl = Renderer::getMarkupTemplate('mail_head.tpl');
120 if ($a->argc > 1 && $a->argv[1] == 'new') {
122 'label' => DI::l10n()->t('Discard'),
128 'label' => DI::l10n()->t('New Message'),
129 'url' => '/message/new',
134 $header = Renderer::replaceMacros($tpl, [
135 '$messages' => DI::l10n()->t('Messages'),
136 '$button' => $button,
139 if (($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) {
140 if (!intval($a->argv[2])) {
144 // Check if we should do HTML-based delete confirmation
145 if (!empty($_REQUEST['confirm'])) {
146 // <form> can't take arguments in its "action" parameter
147 // so add any arguments as hidden inputs
148 $query = explode_querystring(DI::args()->getQueryString());
150 foreach ($query['args'] as $arg) {
151 if (strpos($arg, 'confirm=') === false) {
152 $arg_parts = explode('=', $arg);
153 $inputs[] = ['name' => $arg_parts[0], 'value' => $arg_parts[1]];
157 //DI::page()['aside'] = '';
158 return Renderer::replaceMacros(Renderer::getMarkupTemplate('confirm.tpl'), [
160 '$message' => DI::l10n()->t('Do you really want to delete this message?'),
161 '$extra_inputs' => $inputs,
162 '$confirm' => DI::l10n()->t('Yes'),
163 '$confirm_url' => $query['base'],
164 '$confirm_name' => 'confirmed',
165 '$cancel' => DI::l10n()->t('Cancel'),
169 // Now check how the user responded to the confirmation query
170 if (!empty($_REQUEST['canceled'])) {
171 DI::baseUrl()->redirect('message');
175 if ($cmd === 'drop') {
176 $message = DBA::selectFirst('mail', ['convid'], ['id' => $a->argv[2], 'uid' => local_user()]);
177 if(!DBA::isResult($message)){
178 notice(DI::l10n()->t('Conversation not found.'));
179 DI::baseUrl()->redirect('message');
182 if (!DBA::delete('mail', ['id' => $a->argv[2], 'uid' => local_user()])) {
183 notice(DI::l10n()->t('Message was not deleted.'));
186 $conversation = DBA::selectFirst('mail', ['id'], ['convid' => $message['convid'], 'uid' => local_user()]);
187 if(!DBA::isResult($conversation)){
188 DI::baseUrl()->redirect('message');
191 DI::baseUrl()->redirect('message/' . $conversation['id'] );
193 $r = q("SELECT `parent-uri`,`convid` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
197 if (DBA::isResult($r)) {
198 $parent = $r[0]['parent-uri'];
200 if (!DBA::delete('mail', ['parent-uri' => $parent, 'uid' => local_user()])) {
201 notice(DI::l10n()->t('Conversation was not removed.'));
204 DI::baseUrl()->redirect('message');
208 if (($a->argc > 1) && ($a->argv[1] === 'new')) {
211 $tpl = Renderer::getMarkupTemplate('msg-header.tpl');
212 DI::page()['htmlhead'] .= Renderer::replaceMacros($tpl, [
213 '$baseurl' => DI::baseUrl()->get(true),
214 '$nickname' => $a->user['nickname'],
215 '$linkurl' => DI::l10n()->t('Please enter a link URL:')
218 $recipientId = $a->argv[2] ?? null;
220 $select = ACL::getMessageContactSelectHTML($recipientId);
222 $tpl = Renderer::getMarkupTemplate('prv_message.tpl');
223 $o .= Renderer::replaceMacros($tpl, [
224 '$header' => DI::l10n()->t('Send Private Message'),
225 '$to' => DI::l10n()->t('To:'),
226 '$subject' => DI::l10n()->t('Subject:'),
227 '$subjtxt' => $_REQUEST['subject'] ?? '',
228 '$text' => $_REQUEST['body'] ?? '',
230 '$yourmessage'=> DI::l10n()->t('Your message:'),
231 '$select' => $select,
233 '$upload' => DI::l10n()->t('Upload photo'),
234 '$insert' => DI::l10n()->t('Insert web link'),
235 '$wait' => DI::l10n()->t('Please wait'),
236 '$submit' => DI::l10n()->t('Submit')
242 $_SESSION['return_path'] = DI::args()->getQueryString();
251 $r = q("SELECT count(*) AS `total`, ANY_VALUE(`created`) AS `created` FROM `mail`
252 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
255 if (DBA::isResult($r)) {
256 $total = $r[0]['total'];
259 $pager = new Pager(DI::l10n(), DI::args()->getQueryString());
261 $r = get_messages(local_user(), $pager->getStart(), $pager->getItemsPerPage());
263 if (!DBA::isResult($r)) {
264 notice(DI::l10n()->t('No messages.'));
268 $o .= render_messages($r, 'mail_list.tpl');
270 $o .= $pager->renderFull($total);
275 if (($a->argc > 1) && (intval($a->argv[1]))) {
279 $message = DBA::fetchFirst("
280 SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
282 LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
283 WHERE `mail`.`uid` = ? AND `mail`.`id` = ?
288 if (DBA::isResult($message)) {
289 $contact_id = $message['contact-id'];
293 $message['parent-uri']
296 if ($message['convid']) {
297 $sql_extra = "AND (`mail`.`parent-uri` = ? OR `mail`.`convid` = ?)";
298 $params[] = $message['convid'];
300 $sql_extra = "AND `mail`.`parent-uri` = ?";
302 $messages_stmt = DBA::p("
303 SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
305 LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
306 WHERE `mail`.`uid` = ?
308 ORDER BY `mail`.`created` ASC",
312 $messages = DBA::toArray($messages_stmt);
314 DBA::update('mail', ['seen' => 1], ['parent-uri' => $message['parent-uri'], 'uid' => local_user()]);
315 DBA::update('notify', ['seen' => 1], ['type' => Type::MAIL, 'parent' => $message['id'], 'uid' => local_user()]);
320 if (!DBA::isResult($messages)) {
321 notice(DI::l10n()->t('Message not available.'));
325 $tpl = Renderer::getMarkupTemplate('msg-header.tpl');
326 DI::page()['htmlhead'] .= Renderer::replaceMacros($tpl, [
327 '$baseurl' => DI::baseUrl()->get(true),
328 '$nickname' => $a->user['nickname'],
329 '$linkurl' => DI::l10n()->t('Please enter a link URL:')
336 foreach ($messages as $message) {
337 if ($message['unknown']) {
341 if ($message['from-url'] == $myprofile) {
342 $from_url = $myprofile;
345 $from_url = Contact::magicLink($message['from-url']);
346 $sparkle = ' sparkle';
349 $extracted = item_extract_images($message['body']);
350 if ($extracted['images']) {
351 $message['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $message['contact-id']);
354 $from_name_e = $message['from-name'];
355 $subject_e = $message['title'];
356 $body_e = BBCode::convert($message['body']);
357 $to_name_e = $message['name'];
359 $contact = Contact::getByURL($message['from-url'], false, ['thumb', 'addr', 'id', 'avatar']);
360 $from_photo = Contact::getThumb($contact, $message['from-photo']);
363 'id' => $message['id'],
364 'from_name' => $from_name_e,
365 'from_url' => $from_url,
366 'from_addr' => $contact['addr'],
367 'sparkle' => $sparkle,
368 'from_photo' => $from_photo,
369 'subject' => $subject_e,
371 'delete' => DI::l10n()->t('Delete message'),
372 'to_name' => $to_name_e,
373 'date' => DateTimeFormat::local($message['created'], DI::l10n()->t('D, d M Y - g:i A')),
374 'ago' => Temporal::getRelativeDate($message['created']),
377 $seen = $message['seen'];
380 $select = $message['name'] . '<input type="hidden" name="recipient" value="' . $contact_id . '" />';
381 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
383 $tpl = Renderer::getMarkupTemplate('mail_display.tpl');
384 $o = Renderer::replaceMacros($tpl, [
385 '$thread_id' => $a->argv[1],
386 '$thread_subject' => $message['title'],
387 '$thread_seen' => $seen,
388 '$delete' => DI::l10n()->t('Delete conversation'),
389 '$canreply' => (($unknown) ? false : '1'),
390 '$unknown_text' => DI::l10n()->t("No secure communications available. You <strong>may</strong> be able to respond from the sender's profile page."),
394 '$header' => DI::l10n()->t('Send Reply'),
395 '$to' => DI::l10n()->t('To:'),
396 '$subject' => DI::l10n()->t('Subject:'),
397 '$subjtxt' => $message['title'],
398 '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
399 '$yourmessage' => DI::l10n()->t('Your message:'),
401 '$select' => $select,
402 '$parent' => $parent,
403 '$upload' => DI::l10n()->t('Upload photo'),
404 '$insert' => DI::l10n()->t('Insert web link'),
405 '$submit' => DI::l10n()->t('Submit'),
406 '$wait' => DI::l10n()->t('Please wait')
419 function get_messages($uid, $start, $limit)
421 return DBA::toArray(DBA::p('SELECT
452 MAX(`created`) AS `mailcreated`,
453 MIN(`seen`) AS `mailseen`
456 GROUP BY `parent-uri`
457 ) m2 ON m.`parent-uri` = m2.`parent-uri` AND m.`id` = m2.`id`
458 LEFT JOIN `contact` c ON m.`contact-id` = c.`id`
460 ORDER BY m2.`mailcreated` DESC
462 , $uid, $uid, $start, $limit));
465 function render_messages(array $msg, $t)
469 $tpl = Renderer::getMarkupTemplate($t);
472 $myprofile = DI::baseUrl() . '/profile/' . $a->user['nickname'];
474 foreach ($msg as $rr) {
475 if ($rr['unknown']) {
476 $participants = DI::l10n()->t("Unknown sender - %s", $rr['from-name']);
477 } elseif (Strings::compareLink($rr['from-url'], $myprofile)) {
478 $participants = DI::l10n()->t("You and %s", $rr['name']);
480 $participants = DI::l10n()->t("%s and You", $rr['from-name']);
483 $body_e = $rr['body'];
484 $to_name_e = $rr['name'];
486 $contact = Contact::getByURL($rr['url'], false, ['thumb', 'addr', 'id', 'avatar']);
487 $from_photo = Contact::getThumb($contact, $rr['thumb'] ?: $rr['from-photo']);
489 $rslt .= Renderer::replaceMacros($tpl, [
491 '$from_name' => $participants,
492 '$from_url' => Contact::magicLink($rr['url']),
493 '$from_addr' => $contact['addr'] ?? '',
494 '$sparkle' => ' sparkle',
495 '$from_photo' => $from_photo,
496 '$subject' => $rr['title'],
497 '$delete' => DI::l10n()->t('Delete conversation'),
499 '$to_name' => $to_name_e,
500 '$date' => DateTimeFormat::local($rr['mailcreated'], DI::l10n()->t('D, d M Y - g:i A')),
501 '$ago' => Temporal::getRelativeDate($rr['mailcreated']),
502 '$seen' => $rr['mailseen'],
503 '$count' => DI::l10n()->tt('%d message', '%d messages', $rr['count']),