3 * @copyright Copyright (C) 2010-2022, the Friendica project
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\Core\Session;
29 use Friendica\Database\DBA;
31 use Friendica\Model\Contact;
32 use Friendica\Model\Mail;
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 (DI::args()->getArgc() > 1 && is_numeric(DI::args()->getArgv()[1])) {
43 $tabs = render_messages(get_messages(DI::userSession()->getLocalUserId(), 0, 5), 'mail_list.tpl');
47 'label' => DI::l10n()->t('New Message'),
48 'url' => 'message/new',
49 'sel' => DI::args()->getArgc() > 1 && DI::args()->getArgv()[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)
69 if (!DI::userSession()->getLocalUserId()) {
70 DI::sysmsg()->addNotice(DI::l10n()->t('Permission denied.'));
74 $replyto = !empty($_REQUEST['replyto']) ? trim($_REQUEST['replyto']) : '';
75 $subject = !empty($_REQUEST['subject']) ? 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 DI::sysmsg()->addNotice(DI::l10n()->t('No recipient selected.'));
89 DI::sysmsg()->addNotice(DI::l10n()->t('Unable to locate contact information.'));
93 DI::sysmsg()->addNotice(DI::l10n()->t('Message could not be sent.'));
97 DI::sysmsg()->addNotice(DI::l10n()->t('Message collection failure.'));
101 // fake it to go back to the input form if no recipient listed
103 DI::args()->setArgv(['message', 'new']);
105 DI::baseUrl()->redirect(DI::args()->getCommand() . '/' . $ret);
109 function message_content(App $a)
112 Nav::setSelected('messages');
114 if (!DI::userSession()->getLocalUserId()) {
115 DI::sysmsg()->addNotice(DI::l10n()->t('Permission denied.'));
116 return Login::form();
119 $myprofile = DI::baseUrl() . '/profile/' . $a->getLoggedInUserNickname();
121 $tpl = Renderer::getMarkupTemplate('mail_head.tpl');
122 if (DI::args()->getArgc() > 1 && DI::args()->getArgv()[1] == 'new') {
124 'label' => DI::l10n()->t('Discard'),
130 'label' => DI::l10n()->t('New Message'),
131 'url' => '/message/new',
136 $header = Renderer::replaceMacros($tpl, [
137 '$messages' => DI::l10n()->t('Messages'),
138 '$button' => $button,
141 if ((DI::args()->getArgc() == 3) && (DI::args()->getArgv()[1] === 'drop' || DI::args()->getArgv()[1] === 'dropconv')) {
142 if (!intval(DI::args()->getArgv()[2])) {
146 $cmd = DI::args()->getArgv()[1];
147 if ($cmd === 'drop') {
148 $message = DBA::selectFirst('mail', ['convid'], ['id' => DI::args()->getArgv()[2], 'uid' => DI::userSession()->getLocalUserId()]);
149 if(!DBA::isResult($message)){
150 DI::sysmsg()->addNotice(DI::l10n()->t('Conversation not found.'));
151 DI::baseUrl()->redirect('message');
154 if (!DBA::delete('mail', ['id' => DI::args()->getArgv()[2], 'uid' => DI::userSession()->getLocalUserId()])) {
155 DI::sysmsg()->addNotice(DI::l10n()->t('Message was not deleted.'));
158 $conversation = DBA::selectFirst('mail', ['id'], ['convid' => $message['convid'], 'uid' => DI::userSession()->getLocalUserId()]);
159 if(!DBA::isResult($conversation)){
160 DI::baseUrl()->redirect('message');
163 DI::baseUrl()->redirect('message/' . $conversation['id'] );
165 $parentmail = DBA::selectFirst('mail', ['parent-uri'], ['id' => DI::args()->getArgv()[2], 'uid' => DI::userSession()->getLocalUserId()]);
166 if (DBA::isResult($parentmail)) {
167 $parent = $parentmail['parent-uri'];
169 if (!DBA::delete('mail', ['parent-uri' => $parent, 'uid' => DI::userSession()->getLocalUserId()])) {
170 DI::sysmsg()->addNotice(DI::l10n()->t('Conversation was not removed.'));
173 DI::baseUrl()->redirect('message');
177 if ((DI::args()->getArgc() > 1) && (DI::args()->getArgv()[1] === 'new')) {
180 $tpl = Renderer::getMarkupTemplate('msg-header.tpl');
181 DI::page()['htmlhead'] .= Renderer::replaceMacros($tpl, [
182 '$baseurl' => DI::baseUrl()->get(true),
183 '$nickname' => $a->getLoggedInUserNickname(),
184 '$linkurl' => DI::l10n()->t('Please enter a link URL:')
187 $recipientId = DI::args()->getArgv()[2] ?? null;
189 $select = ACL::getMessageContactSelectHTML($recipientId);
191 $tpl = Renderer::getMarkupTemplate('prv_message.tpl');
192 $o .= Renderer::replaceMacros($tpl, [
193 '$header' => DI::l10n()->t('Send Private Message'),
194 '$to' => DI::l10n()->t('To:'),
195 '$subject' => DI::l10n()->t('Subject:'),
196 '$subjtxt' => $_REQUEST['subject'] ?? '',
197 '$text' => $_REQUEST['body'] ?? '',
199 '$yourmessage' => DI::l10n()->t('Your message:'),
200 '$select' => $select,
202 '$upload' => DI::l10n()->t('Upload photo'),
203 '$insert' => DI::l10n()->t('Insert web link'),
204 '$wait' => DI::l10n()->t('Please wait'),
205 '$submit' => DI::l10n()->t('Submit')
211 $_SESSION['return_path'] = DI::args()->getQueryString();
213 if (DI::args()->getArgc() == 1) {
219 $total = DBA::count('mail', ['uid' => DI::userSession()->getLocalUserId()], ['distinct' => true, 'expression' => 'parent-uri']);
221 $pager = new Pager(DI::l10n(), DI::args()->getQueryString());
223 $r = get_messages(DI::userSession()->getLocalUserId(), $pager->getStart(), $pager->getItemsPerPage());
225 if (!DBA::isResult($r)) {
226 DI::sysmsg()->addNotice(DI::l10n()->t('No messages.'));
230 $o .= render_messages($r, 'mail_list.tpl');
232 $o .= $pager->renderFull($total);
237 if ((DI::args()->getArgc() > 1) && (intval(DI::args()->getArgv()[1]))) {
241 $message = DBA::fetchFirst("
242 SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
244 LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
245 WHERE `mail`.`uid` = ? AND `mail`.`id` = ?
247 DI::userSession()->getLocalUserId(),
248 DI::args()->getArgv()[1]
250 if (DBA::isResult($message)) {
251 $contact_id = $message['contact-id'];
254 DI::userSession()->getLocalUserId(),
255 $message['parent-uri']
258 if ($message['convid']) {
259 $sql_extra = "AND (`mail`.`parent-uri` = ? OR `mail`.`convid` = ?)";
260 $params[] = $message['convid'];
262 $sql_extra = "AND `mail`.`parent-uri` = ?";
264 $messages_stmt = DBA::p("
265 SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
267 LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
268 WHERE `mail`.`uid` = ?
270 ORDER BY `mail`.`created` ASC",
274 $messages = DBA::toArray($messages_stmt);
276 DBA::update('mail', ['seen' => 1], ['parent-uri' => $message['parent-uri'], 'uid' => DI::userSession()->getLocalUserId()]);
281 if (!DBA::isResult($messages)) {
282 DI::sysmsg()->addNotice(DI::l10n()->t('Message not available.'));
286 $tpl = Renderer::getMarkupTemplate('msg-header.tpl');
287 DI::page()['htmlhead'] .= Renderer::replaceMacros($tpl, [
288 '$baseurl' => DI::baseUrl()->get(true),
289 '$nickname' => $a->getLoggedInUserNickname(),
290 '$linkurl' => DI::l10n()->t('Please enter a link URL:')
297 foreach ($messages as $message) {
298 if ($message['unknown']) {
302 if ($message['from-url'] == $myprofile) {
303 $from_url = $myprofile;
306 $from_url = Contact::magicLink($message['from-url']);
307 $sparkle = ' sparkle';
310 $from_name_e = $message['from-name'];
311 $subject_e = $message['title'];
312 $body_e = BBCode::convertForUriId($message['uri-id'], $message['body']);
313 $to_name_e = $message['name'];
315 $contact = Contact::getByURL($message['from-url'], false, ['thumb', 'addr', 'id', 'avatar', 'url']);
316 $from_photo = Contact::getThumb($contact);
319 'id' => $message['id'],
320 'from_name' => $from_name_e,
321 'from_url' => $from_url,
322 'from_addr' => $contact['addr'] ?? $from_url,
323 'sparkle' => $sparkle,
324 'from_photo' => $from_photo,
325 'subject' => $subject_e,
327 'delete' => DI::l10n()->t('Delete message'),
328 'to_name' => $to_name_e,
329 'date' => DateTimeFormat::local($message['created'], DI::l10n()->t('D, d M Y - g:i A')),
330 'ago' => Temporal::getRelativeDate($message['created']),
333 $seen = $message['seen'];
336 $select = $message['name'] . '<input type="hidden" name="recipient" value="' . $contact_id . '" />';
337 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
339 $tpl = Renderer::getMarkupTemplate('mail_display.tpl');
340 $o = Renderer::replaceMacros($tpl, [
341 '$thread_id' => DI::args()->getArgv()[1],
342 '$thread_subject' => $message['title'],
343 '$thread_seen' => $seen,
344 '$delete' => DI::l10n()->t('Delete conversation'),
345 '$canreply' => (($unknown) ? false : '1'),
346 '$unknown_text' => DI::l10n()->t("No secure communications available. You <strong>may</strong> be able to respond from the sender's profile page."),
349 '$header' => DI::l10n()->t('Send Reply'),
350 '$to' => DI::l10n()->t('To:'),
351 '$subject' => DI::l10n()->t('Subject:'),
352 '$subjtxt' => $message['title'],
353 '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
354 '$yourmessage' => DI::l10n()->t('Your message:'),
356 '$select' => $select,
357 '$parent' => $parent,
358 '$upload' => DI::l10n()->t('Upload photo'),
359 '$insert' => DI::l10n()->t('Insert web link'),
360 '$submit' => DI::l10n()->t('Submit'),
361 '$wait' => DI::l10n()->t('Please wait')
374 function get_messages(int $uid, int $start, int $limit): array
376 return DBA::toArray(DBA::p('SELECT
407 MAX(`created`) AS `mailcreated`,
408 MIN(`seen`) AS `mailseen`
411 GROUP BY `parent-uri`
412 ) m2 ON m.`parent-uri` = m2.`parent-uri` AND m.`id` = m2.`id`
413 LEFT JOIN `contact` c ON m.`contact-id` = c.`id`
415 ORDER BY m2.`mailcreated` DESC
417 , $uid, $uid, $start, $limit));
420 function render_messages(array $msg, string $t): string
424 $tpl = Renderer::getMarkupTemplate($t);
427 $myprofile = DI::baseUrl() . '/profile/' . $a->getLoggedInUserNickname();
429 foreach ($msg as $rr) {
430 if ($rr['unknown']) {
431 $participants = DI::l10n()->t("Unknown sender - %s", $rr['from-name']);
432 } elseif (Strings::compareLink($rr['from-url'], $myprofile)) {
433 $participants = DI::l10n()->t("You and %s", $rr['name']);
435 $participants = DI::l10n()->t("%s and You", $rr['from-name']);
438 $body_e = $rr['body'];
439 $to_name_e = $rr['name'];
441 if (is_null($rr['url'])) {
442 // contact-id is pointing to a non existing contact
446 $contact = Contact::getByURL($rr['url'], false, ['thumb', 'addr', 'id', 'avatar', 'url']);
447 $from_photo = Contact::getThumb($contact);
449 $rslt .= Renderer::replaceMacros($tpl, [
451 '$from_name' => $participants,
452 '$from_url' => Contact::magicLink($rr['url']),
453 '$from_addr' => $contact['addr'] ?? '',
454 '$sparkle' => ' sparkle',
455 '$from_photo' => $from_photo,
456 '$subject' => $rr['title'],
457 '$delete' => DI::l10n()->t('Delete conversation'),
459 '$to_name' => $to_name_e,
460 '$date' => DateTimeFormat::local($rr['mailcreated'], DI::l10n()->t('D, d M Y - g:i A')),
461 '$ago' => Temporal::getRelativeDate($rr['mailcreated']),
462 '$seen' => $rr['mailseen'],
463 '$count' => DI::l10n()->tt('%d message', '%d messages', $rr['count']),