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;
16 use Friendica\Model\Contact;
17 use Friendica\Model\Mail;
18 use Friendica\Module\Login;
19 use Friendica\Util\DateTimeFormat;
20 use Friendica\Util\Proxy as ProxyUtils;
21 use Friendica\Util\Strings;
22 use Friendica\Util\Temporal;
24 function message_init(App $a)
28 if ($a->argc > 1 && is_numeric($a->argv[1])) {
29 $tabs = render_messages(get_messages(local_user(), 0, 5), 'mail_list.tpl');
33 'label' => L10n::t('New Message'),
34 'url' => 'message/new',
35 'sel' => $a->argc > 1 && $a->argv[1] == 'new',
39 $tpl = Renderer::getMarkupTemplate('message_side.tpl');
40 $a->page['aside'] = Renderer::replaceMacros($tpl, [
44 $base = System::baseUrl();
46 $head_tpl = Renderer::getMarkupTemplate('message-head.tpl');
47 $a->page['htmlhead'] .= Renderer::replaceMacros($head_tpl, [
48 '$baseurl' => System::baseUrl(true),
53 function message_post(App $a)
56 notice(L10n::t('Permission denied.') . EOL);
60 $replyto = !empty($_REQUEST['replyto']) ? Strings::escapeTags(trim($_REQUEST['replyto'])) : '';
61 $subject = !empty($_REQUEST['subject']) ? Strings::escapeTags(trim($_REQUEST['subject'])) : '';
62 $body = !empty($_REQUEST['body']) ? Strings::escapeHtml(trim($_REQUEST['body'])) : '';
63 $recipient = !empty($_REQUEST['messageto']) ? intval($_REQUEST['messageto']) : 0;
65 $ret = Mail::send($recipient, $body, $subject, $replyto);
70 notice(L10n::t('No recipient selected.') . EOL);
74 notice(L10n::t('Unable to locate contact information.') . EOL);
77 notice(L10n::t('Message could not be sent.') . EOL);
80 notice(L10n::t('Message collection failure.') . EOL);
83 info(L10n::t('Message sent.') . EOL);
86 // fake it to go back to the input form if no recipient listed
91 $a->internalRedirect($a->cmd . '/' . $ret);
95 function message_content(App $a)
98 Nav::setSelected('messages');
101 notice(L10n::t('Permission denied.') . EOL);
102 return Login::form();
105 $myprofile = System::baseUrl() . '/profile/' . $a->user['nickname'];
107 $tpl = Renderer::getMarkupTemplate('mail_head.tpl');
108 if ($a->argc > 1 && $a->argv[1] == 'new') {
110 'label' => L10n::t('Discard'),
116 'label' => L10n::t('New Message'),
117 'url' => '/message/new',
122 $header = Renderer::replaceMacros($tpl, [
123 '$messages' => L10n::t('Messages'),
124 '$button' => $button,
127 if (($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) {
128 if (!intval($a->argv[2])) {
132 // Check if we should do HTML-based delete confirmation
133 if (!empty($_REQUEST['confirm'])) {
134 // <form> can't take arguments in its "action" parameter
135 // so add any arguments as hidden inputs
136 $query = explode_querystring($a->query_string);
138 foreach ($query['args'] as $arg) {
139 if (strpos($arg, 'confirm=') === false) {
140 $arg_parts = explode('=', $arg);
141 $inputs[] = ['name' => $arg_parts[0], 'value' => $arg_parts[1]];
145 //$a->page['aside'] = '';
146 return Renderer::replaceMacros(Renderer::getMarkupTemplate('confirm.tpl'), [
148 '$message' => L10n::t('Do you really want to delete this message?'),
149 '$extra_inputs' => $inputs,
150 '$confirm' => L10n::t('Yes'),
151 '$confirm_url' => $query['base'],
152 '$confirm_name' => 'confirmed',
153 '$cancel' => L10n::t('Cancel'),
157 // Now check how the user responded to the confirmation query
158 if (!empty($_REQUEST['canceled'])) {
159 $a->internalRedirect('message');
163 if ($cmd === 'drop') {
164 $message = DBA::selectFirst('mail', ['convid'], ['id' => $a->argv[2], 'uid' => local_user()]);
165 if(!DBA::isResult($message)){
166 info(L10n::t('Conversation not found.') . EOL);
167 $a->internalRedirect('message');
170 if (DBA::delete('mail', ['id' => $a->argv[2], 'uid' => local_user()])) {
171 info(L10n::t('Message deleted.') . EOL);
174 $conversation = DBA::selectFirst('mail', ['id'], ['convid' => $message['convid'], 'uid' => local_user()]);
175 if(!DBA::isResult($conversation)){
176 info(L10n::t('Conversation removed.') . EOL);
177 $a->internalRedirect('message');
180 $a->internalRedirect('message/' . $conversation['id'] );
182 $r = q("SELECT `parent-uri`,`convid` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
186 if (DBA::isResult($r)) {
187 $parent = $r[0]['parent-uri'];
188 $convid = $r[0]['convid'];
190 if (DBA::delete('mail', ['parent-uri' => $parent, 'uid' => local_user()])) {
191 info(L10n::t('Conversation removed.') . EOL);
194 $a->internalRedirect('message');
198 if (($a->argc > 1) && ($a->argv[1] === 'new')) {
201 $tpl = Renderer::getMarkupTemplate('msg-header.tpl');
202 $a->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 $preurl = $r[0]['url'];
234 $preid = $r[0]['id'];
235 $preselect = [$preid];
241 $prefill = $preselect ? $prename : '';
243 // the ugly select box
244 $select = ACL::getMessageContactSelectHTML('messageto', 'message-to-select', $preselect, 4, 10);
246 $tpl = Renderer::getMarkupTemplate('prv_message.tpl');
247 $o .= Renderer::replaceMacros($tpl, [
248 '$header' => L10n::t('Send Private Message'),
249 '$to' => L10n::t('To:'),
250 '$showinputs' => 'true',
251 '$prefill' => $prefill,
253 '$subject' => L10n::t('Subject:'),
254 '$subjtxt' => defaults($_REQUEST, 'subject', ''),
255 '$text' => defaults($_REQUEST, 'body', ''),
257 '$yourmessage'=> L10n::t('Your message:'),
258 '$select' => $select,
260 '$upload' => L10n::t('Upload photo'),
261 '$insert' => L10n::t('Insert web link'),
262 '$wait' => L10n::t('Please wait'),
263 '$submit' => L10n::t('Submit')
269 $_SESSION['return_path'] = $a->query_string;
278 $r = q("SELECT count(*) AS `total`, ANY_VALUE(`created`) AS `created` FROM `mail`
279 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
282 if (DBA::isResult($r)) {
283 $total = $r[0]['total'];
286 $pager = new Pager($a->query_string);
288 $r = get_messages(local_user(), $pager->getStart(), $pager->getItemsPerPage());
290 if (!DBA::isResult($r)) {
291 info(L10n::t('No messages.') . EOL);
295 $o .= render_messages($r, 'mail_list.tpl');
297 $o .= $pager->renderFull($total);
302 if (($a->argc > 1) && (intval($a->argv[1]))) {
306 $message = DBA::fetchFirst("
307 SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
309 LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
310 WHERE `mail`.`uid` = ? AND `mail`.`id` = ?
315 if (DBA::isResult($message)) {
316 $contact_id = $message['contact-id'];
320 $message['parent-uri']
323 if ($message['convid']) {
324 $sql_extra = "AND (`mail`.`parent-uri` = ? OR `mail`.`convid` = ?)";
325 $params[] = $message['convid'];
327 $sql_extra = "AND `mail`.`parent-uri` = ?";
329 $messages_stmt = DBA::p("
330 SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
332 LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
333 WHERE `mail`.`uid` = ?
335 ORDER BY `mail`.`created` ASC",
339 $messages = DBA::toArray($messages_stmt);
341 DBA::update('mail', ['seen' => 1], ['parent-uri' => $message['parent-uri'], 'uid' => local_user()]);
343 if ($message['convid']) {
344 // Clear Diaspora private message notifications
345 DBA::update('notify', ['seen' => 1], ['type' => NOTIFY_MAIL, 'parent' => $message['convid'], 'uid' => local_user()]);
347 // Clear DFRN private message notifications
348 DBA::update('notify', ['seen' => 1], ['type' => NOTIFY_MAIL, 'parent' => $message['parent-uri'], 'uid' => local_user()]);
353 if (!DBA::isResult($messages)) {
354 notice(L10n::t('Message not available.') . EOL);
358 $tpl = Renderer::getMarkupTemplate('msg-header.tpl');
359 $a->page['htmlhead'] .= Renderer::replaceMacros($tpl, [
360 '$baseurl' => System::baseUrl(true),
361 '$nickname' => $a->user['nickname'],
362 '$linkurl' => L10n::t('Please enter a link URL:')
369 foreach ($messages as $message) {
370 if ($message['unknown']) {
374 if ($message['from-url'] == $myprofile) {
375 $from_url = $myprofile;
378 $from_url = Contact::magicLink($message['from-url']);
379 $sparkle = ' sparkle';
382 $extracted = item_extract_images($message['body']);
383 if ($extracted['images']) {
384 $message['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $message['contact-id']);
387 $from_name_e = $message['from-name'];
388 $subject_e = $message['title'];
389 $body_e = Smilies::replace(BBCode::convert($message['body']));
390 $to_name_e = $message['name'];
392 $contact = Contact::getDetailsByURL($message['from-url']);
393 if (isset($contact["thumb"])) {
394 $from_photo = $contact["thumb"];
396 $from_photo = $message['from-photo'];
400 'id' => $message['id'],
401 'from_name' => $from_name_e,
402 'from_url' => $from_url,
403 'from_addr' => $contact['addr'],
404 'sparkle' => $sparkle,
405 'from_photo' => ProxyUtils::proxifyUrl($from_photo, false, ProxyUtils::SIZE_THUMB),
406 'subject' => $subject_e,
408 'delete' => L10n::t('Delete message'),
409 'to_name' => $to_name_e,
410 'date' => DateTimeFormat::local($message['created'], L10n::t('D, d M Y - g:i A')),
411 'ago' => Temporal::getRelativeDate($message['created']),
414 $seen = $message['seen'];
417 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
418 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
420 $tpl = Renderer::getMarkupTemplate('mail_display.tpl');
421 $o = Renderer::replaceMacros($tpl, [
422 '$thread_id' => $a->argv[1],
423 '$thread_subject' => $message['title'],
424 '$thread_seen' => $seen,
425 '$delete' => L10n::t('Delete conversation'),
426 '$canreply' => (($unknown) ? false : '1'),
427 '$unknown_text' => L10n::t("No secure communications available. You <strong>may</strong> be able to respond from the sender's profile page."),
431 '$header' => L10n::t('Send Reply'),
432 '$to' => L10n::t('To:'),
434 '$subject' => L10n::t('Subject:'),
435 '$subjtxt' => $message['title'],
436 '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
437 '$yourmessage' => L10n::t('Your message:'),
439 '$select' => $select,
440 '$parent' => $parent,
441 '$upload' => L10n::t('Upload photo'),
442 '$insert' => L10n::t('Insert web link'),
443 '$submit' => L10n::t('Submit'),
444 '$wait' => L10n::t('Please wait')
457 function get_messages($uid, $start, $limit)
459 return DBA::toArray(DBA::p('SELECT
490 MAX(`created`) AS `mailcreated`,
491 MIN(`seen`) AS `mailseen`
494 GROUP BY `parent-uri`
495 ) m2 ON m.`parent-uri` = m2.`parent-uri` AND m.`id` = m2.`id`
496 LEFT JOIN `contact` c ON m.`contact-id` = c.`id`
498 ORDER BY m2.`mailcreated` DESC
500 , $uid, $uid, $start, $limit));
503 function render_messages(array $msg, $t)
507 $tpl = Renderer::getMarkupTemplate($t);
510 $myprofile = System::baseUrl() . '/profile/' . $a->user['nickname'];
512 foreach ($msg as $rr) {
513 if ($rr['unknown']) {
514 $participants = L10n::t("Unknown sender - %s", $rr['from-name']);
515 } elseif (Strings::compareLink($rr['from-url'], $myprofile)) {
516 $participants = L10n::t("You and %s", $rr['name']);
518 $participants = L10n::t("%s and You", $rr['from-name']);
521 $body_e = $rr['body'];
522 $to_name_e = $rr['name'];
524 $contact = Contact::getDetailsByURL($rr['url']);
525 if (isset($contact["thumb"])) {
526 $from_photo = $contact["thumb"];
528 $from_photo = (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']);
531 $rslt .= Renderer::replaceMacros($tpl, [
533 '$from_name' => $participants,
534 '$from_url' => Contact::magicLink($rr['url']),
535 '$from_addr' => defaults($contact, 'addr', ''),
536 '$sparkle' => ' sparkle',
537 '$from_photo' => ProxyUtils::proxifyUrl($from_photo, false, ProxyUtils::SIZE_THUMB),
538 '$subject' => $rr['title'],
539 '$delete' => L10n::t('Delete conversation'),
541 '$to_name' => $to_name_e,
542 '$date' => DateTimeFormat::local($rr['mailcreated'], L10n::t('D, d M Y - g:i A')),
543 '$ago' => Temporal::getRelativeDate($rr['mailcreated']),
544 '$seen' => $rr['mailseen'],
545 '$count' => L10n::tt('%d message', '%d messages', $rr['count']),