3 * @copyright Copyright (C) 2010-2023, 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\Database\DBA;
30 use Friendica\Model\Contact;
31 use Friendica\Model\Mail;
32 use Friendica\Module\Security\Login;
33 use Friendica\Util\DateTimeFormat;
34 use Friendica\Util\Strings;
35 use Friendica\Util\Temporal;
37 function message_init(App $a)
41 if (DI::args()->getArgc() > 1 && is_numeric(DI::args()->getArgv()[1])) {
42 $tabs = render_messages(get_messages(DI::userSession()->getLocalUserId(), 0, 5), 'mail_list.tpl');
46 'label' => DI::l10n()->t('New Message'),
47 'url' => 'message/new',
48 'sel' => DI::args()->getArgc() > 1 && DI::args()->getArgv()[1] == 'new',
52 $tpl = Renderer::getMarkupTemplate('message_side.tpl');
53 DI::page()['aside'] = Renderer::replaceMacros($tpl, [
57 $base = DI::baseUrl();
59 $head_tpl = Renderer::getMarkupTemplate('message-head.tpl');
60 DI::page()['htmlhead'] .= Renderer::replaceMacros($head_tpl, [
65 function message_post(App $a)
67 if (!DI::userSession()->getLocalUserId()) {
68 DI::sysmsg()->addNotice(DI::l10n()->t('Permission denied.'));
72 $sender_id = DI::userSession()->getLocalUserId();
73 $replyto = !empty($_REQUEST['replyto']) ? trim($_REQUEST['replyto']) : '';
74 $subject = !empty($_REQUEST['subject']) ? trim($_REQUEST['subject']) : '';
75 $body = !empty($_REQUEST['body']) ? Strings::escapeHtml(trim($_REQUEST['body'])) : '';
76 $recipient = !empty($_REQUEST['recipient']) ? intval($_REQUEST['recipient']) : 0;
78 $ret = Mail::send($sender_id, $recipient, $body, $subject, $replyto);
83 DI::sysmsg()->addNotice(DI::l10n()->t('No recipient selected.'));
88 DI::sysmsg()->addNotice(DI::l10n()->t('Unable to locate contact information.'));
92 DI::sysmsg()->addNotice(DI::l10n()->t('Message could not be sent.'));
96 DI::sysmsg()->addNotice(DI::l10n()->t('Message collection failure.'));
100 // fake it to go back to the input form if no recipient listed
102 DI::args()->setArgv(['message', 'new']);
104 DI::baseUrl()->redirect(DI::args()->getCommand() . '/' . $ret);
108 function message_content(App $a)
111 Nav::setSelected('messages');
113 if (!DI::userSession()->getLocalUserId()) {
114 DI::sysmsg()->addNotice(DI::l10n()->t('Permission denied.'));
115 return Login::form();
118 $myprofile = DI::baseUrl() . '/profile/' . $a->getLoggedInUserNickname();
120 $tpl = Renderer::getMarkupTemplate('mail_head.tpl');
121 if (DI::args()->getArgc() > 1 && DI::args()->getArgv()[1] == 'new') {
123 'label' => DI::l10n()->t('Discard'),
129 'label' => DI::l10n()->t('New Message'),
130 'url' => '/message/new',
135 $header = Renderer::replaceMacros($tpl, [
136 '$messages' => DI::l10n()->t('Messages'),
137 '$button' => $button,
140 if ((DI::args()->getArgc() == 3) && (DI::args()->getArgv()[1] === 'drop' || DI::args()->getArgv()[1] === 'dropconv')) {
141 if (!intval(DI::args()->getArgv()[2])) {
145 $cmd = DI::args()->getArgv()[1];
146 if ($cmd === 'drop') {
147 $message = DBA::selectFirst('mail', ['convid'], ['id' => DI::args()->getArgv()[2], 'uid' => DI::userSession()->getLocalUserId()]);
148 if(!DBA::isResult($message)){
149 DI::sysmsg()->addNotice(DI::l10n()->t('Conversation not found.'));
150 DI::baseUrl()->redirect('message');
153 if (!DBA::delete('mail', ['id' => DI::args()->getArgv()[2], 'uid' => DI::userSession()->getLocalUserId()])) {
154 DI::sysmsg()->addNotice(DI::l10n()->t('Message was not deleted.'));
157 $conversation = DBA::selectFirst('mail', ['id'], ['convid' => $message['convid'], 'uid' => DI::userSession()->getLocalUserId()]);
158 if(!DBA::isResult($conversation)){
159 DI::baseUrl()->redirect('message');
162 DI::baseUrl()->redirect('message/' . $conversation['id'] );
164 $parentmail = DBA::selectFirst('mail', ['parent-uri'], ['id' => DI::args()->getArgv()[2], 'uid' => DI::userSession()->getLocalUserId()]);
165 if (DBA::isResult($parentmail)) {
166 $parent = $parentmail['parent-uri'];
168 if (!DBA::delete('mail', ['parent-uri' => $parent, 'uid' => DI::userSession()->getLocalUserId()])) {
169 DI::sysmsg()->addNotice(DI::l10n()->t('Conversation was not removed.'));
172 DI::baseUrl()->redirect('message');
176 if ((DI::args()->getArgc() > 1) && (DI::args()->getArgv()[1] === 'new')) {
179 $tpl = Renderer::getMarkupTemplate('msg-header.tpl');
180 DI::page()['htmlhead'] .= Renderer::replaceMacros($tpl, [
181 '$nickname' => $a->getLoggedInUserNickname(),
182 '$linkurl' => DI::l10n()->t('Please enter a link URL:')
185 $recipientId = DI::args()->getArgv()[2] ?? null;
187 $select = ACL::getMessageContactSelectHTML($recipientId);
189 $tpl = Renderer::getMarkupTemplate('prv_message.tpl');
190 $o .= Renderer::replaceMacros($tpl, [
191 '$header' => DI::l10n()->t('Send Private Message'),
192 '$to' => DI::l10n()->t('To:'),
193 '$subject' => DI::l10n()->t('Subject:'),
194 '$subjtxt' => $_REQUEST['subject'] ?? '',
195 '$text' => $_REQUEST['body'] ?? '',
197 '$yourmessage' => DI::l10n()->t('Your message:'),
198 '$select' => $select,
200 '$upload' => DI::l10n()->t('Upload photo'),
201 '$insert' => DI::l10n()->t('Insert web link'),
202 '$wait' => DI::l10n()->t('Please wait'),
203 '$submit' => DI::l10n()->t('Submit')
209 $_SESSION['return_path'] = DI::args()->getQueryString();
211 if (DI::args()->getArgc() == 1) {
217 $total = DBA::count('mail', ['uid' => DI::userSession()->getLocalUserId()], ['distinct' => true, 'expression' => 'parent-uri']);
219 $pager = new Pager(DI::l10n(), DI::args()->getQueryString());
221 $r = get_messages(DI::userSession()->getLocalUserId(), $pager->getStart(), $pager->getItemsPerPage());
223 if (!DBA::isResult($r)) {
224 DI::sysmsg()->addNotice(DI::l10n()->t('No messages.'));
228 $o .= render_messages($r, 'mail_list.tpl');
230 $o .= $pager->renderFull($total);
235 if ((DI::args()->getArgc() > 1) && (intval(DI::args()->getArgv()[1]))) {
239 $message = DBA::fetchFirst("
240 SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
242 LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
243 WHERE `mail`.`uid` = ? AND `mail`.`id` = ?
245 DI::userSession()->getLocalUserId(),
246 DI::args()->getArgv()[1]
248 if (DBA::isResult($message)) {
249 $contact_id = $message['contact-id'];
252 DI::userSession()->getLocalUserId(),
253 $message['parent-uri']
256 if ($message['convid']) {
257 $sql_extra = "AND (`mail`.`parent-uri` = ? OR `mail`.`convid` = ?)";
258 $params[] = $message['convid'];
260 $sql_extra = "AND `mail`.`parent-uri` = ?";
262 $messages_stmt = DBA::p("
263 SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
265 LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
266 WHERE `mail`.`uid` = ?
268 ORDER BY `mail`.`created` ASC",
272 $messages = DBA::toArray($messages_stmt);
274 DBA::update('mail', ['seen' => 1], ['parent-uri' => $message['parent-uri'], 'uid' => DI::userSession()->getLocalUserId()]);
279 if (!DBA::isResult($messages)) {
280 DI::sysmsg()->addNotice(DI::l10n()->t('Message not available.'));
284 $tpl = Renderer::getMarkupTemplate('msg-header.tpl');
285 DI::page()['htmlhead'] .= Renderer::replaceMacros($tpl, [
286 '$nickname' => $a->getLoggedInUserNickname(),
287 '$linkurl' => DI::l10n()->t('Please enter a link URL:')
294 foreach ($messages as $message) {
295 if ($message['unknown']) {
299 if ($message['from-url'] == $myprofile) {
300 $from_url = $myprofile;
303 $from_url = Contact::magicLink($message['from-url']);
304 $sparkle = ' sparkle';
307 $from_name_e = $message['from-name'];
308 $subject_e = $message['title'];
309 $body_e = BBCode::convertForUriId($message['uri-id'], $message['body']);
310 $to_name_e = $message['name'];
312 $contact = Contact::getByURL($message['from-url'], false, ['thumb', 'addr', 'id', 'avatar', 'url']);
313 $from_photo = Contact::getThumb($contact);
316 'id' => $message['id'],
317 'from_name' => $from_name_e,
318 'from_url' => $from_url,
319 'from_addr' => $contact['addr'] ?? $from_url,
320 'sparkle' => $sparkle,
321 'from_photo' => $from_photo,
322 'subject' => $subject_e,
324 'delete' => DI::l10n()->t('Delete message'),
325 'to_name' => $to_name_e,
326 'date' => DateTimeFormat::local($message['created'], DI::l10n()->t('D, d M Y - g:i A')),
327 'ago' => Temporal::getRelativeDate($message['created']),
330 $seen = $message['seen'];
333 $select = $message['name'] . '<input type="hidden" name="recipient" value="' . $contact_id . '" />';
334 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
336 $tpl = Renderer::getMarkupTemplate('mail_display.tpl');
337 $o = Renderer::replaceMacros($tpl, [
338 '$thread_id' => DI::args()->getArgv()[1],
339 '$thread_subject' => $message['title'],
340 '$thread_seen' => $seen,
341 '$delete' => DI::l10n()->t('Delete conversation'),
342 '$canreply' => (($unknown) ? false : '1'),
343 '$unknown_text' => DI::l10n()->t("No secure communications available. You <strong>may</strong> be able to respond from the sender's profile page."),
346 '$header' => DI::l10n()->t('Send Reply'),
347 '$to' => DI::l10n()->t('To:'),
348 '$subject' => DI::l10n()->t('Subject:'),
349 '$subjtxt' => $message['title'],
350 '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
351 '$yourmessage' => DI::l10n()->t('Your message:'),
353 '$select' => $select,
354 '$parent' => $parent,
355 '$upload' => DI::l10n()->t('Upload photo'),
356 '$insert' => DI::l10n()->t('Insert web link'),
357 '$submit' => DI::l10n()->t('Submit'),
358 '$wait' => DI::l10n()->t('Please wait')
371 function get_messages(int $uid, int $start, int $limit): array
373 return DBA::toArray(DBA::p('SELECT
404 MAX(`created`) AS `mailcreated`,
405 MIN(`seen`) AS `mailseen`
408 GROUP BY `parent-uri`
409 ) m2 ON m.`parent-uri` = m2.`parent-uri` AND m.`id` = m2.`id`
410 LEFT JOIN `contact` c ON m.`contact-id` = c.`id`
412 ORDER BY m2.`mailcreated` DESC
414 , $uid, $uid, $start, $limit));
417 function render_messages(array $msg, string $t): string
421 $tpl = Renderer::getMarkupTemplate($t);
424 $myprofile = DI::baseUrl() . '/profile/' . $a->getLoggedInUserNickname();
426 foreach ($msg as $rr) {
427 if ($rr['unknown']) {
428 $participants = DI::l10n()->t("Unknown sender - %s", $rr['from-name']);
429 } elseif (Strings::compareLink($rr['from-url'], $myprofile)) {
430 $participants = DI::l10n()->t("You and %s", $rr['name']);
432 $participants = DI::l10n()->t("%s and You", $rr['from-name']);
435 $body_e = $rr['body'];
436 $to_name_e = $rr['name'];
438 if (is_null($rr['url'])) {
439 // contact-id is pointing to a non existing contact
443 $contact = Contact::getByURL($rr['url'], false, ['thumb', 'addr', 'id', 'avatar', 'url']);
444 $from_photo = Contact::getThumb($contact);
446 $rslt .= Renderer::replaceMacros($tpl, [
448 '$from_name' => $participants,
449 '$from_url' => Contact::magicLink($rr['url']),
450 '$from_addr' => $contact['addr'] ?? '',
451 '$sparkle' => ' sparkle',
452 '$from_photo' => $from_photo,
453 '$subject' => $rr['title'],
454 '$delete' => DI::l10n()->t('Delete conversation'),
456 '$to_name' => $to_name_e,
457 '$date' => DateTimeFormat::local($rr['mailcreated'], DI::l10n()->t('D, d M Y - g:i A')),
458 '$ago' => Temporal::getRelativeDate($rr['mailcreated']),
459 '$seen' => $rr['mailseen'],
460 '$count' => DI::l10n()->tt('%d message', '%d messages', $rr['count']),