3 * @file mod/message.php
6 use Friendica\Content\Nav;
7 use Friendica\Content\Smilies;
8 use Friendica\Core\L10n;
9 use Friendica\Core\System;
10 use Friendica\Database\DBM;
11 use Friendica\Model\Contact;
12 use Friendica\Model\Mail;
14 require_once 'include/acl_selectors.php';
15 require_once 'include/conversation.php';
17 function message_init(App $a)
21 if ($a->argc > 1 && is_numeric($a->argv[1])) {
22 $tabs = render_messages(get_messages(local_user(), 0, 5), 'mail_list.tpl');
26 'label' => L10n::t('New Message'),
27 'url' => 'message/new',
28 'sel' => $a->argc > 1 && $a->argv[1] == 'new',
32 $tpl = get_markup_template('message_side.tpl');
33 $a->page['aside'] = replace_macros($tpl, [
37 $base = System::baseUrl();
39 $head_tpl = get_markup_template('message-head.tpl');
40 $a->page['htmlhead'] .= replace_macros($head_tpl, [
41 '$baseurl' => System::baseUrl(true),
45 $end_tpl = get_markup_template('message-end.tpl');
46 $a->page['end'] .= replace_macros($end_tpl, [
47 '$baseurl' => System::baseUrl(true),
52 function message_post(App $a)
55 notice(L10n::t('Permission denied.') . EOL);
59 $replyto = x($_REQUEST, 'replyto') ? notags(trim($_REQUEST['replyto'])) : '';
60 $subject = x($_REQUEST, 'subject') ? notags(trim($_REQUEST['subject'])) : '';
61 $body = x($_REQUEST, 'body') ? escape_tags(trim($_REQUEST['body'])) : '';
62 $recipient = x($_REQUEST, 'messageto') ? intval($_REQUEST['messageto']) : 0;
64 $ret = Mail::send($recipient, $body, $subject, $replyto);
69 notice(L10n::t('No recipient selected.') . EOL);
73 notice(L10n::t('Unable to locate contact information.') . EOL);
76 notice(L10n::t('Message could not be sent.') . EOL);
79 notice(L10n::t('Message collection failure.') . EOL);
82 info(L10n::t('Message sent.') . EOL);
85 // fake it to go back to the input form if no recipient listed
90 goaway($_SESSION['return_url']);
94 function message_content(App $a)
97 Nav::setSelected('messages');
100 notice(L10n::t('Permission denied.') . EOL);
104 $myprofile = System::baseUrl() . '/profile/' . $a->user['nickname'];
106 $tpl = get_markup_template('mail_head.tpl');
107 $header = replace_macros($tpl, [
108 '$messages' => L10n::t('Messages'),
111 if (($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) {
112 if (!intval($a->argv[2])) {
116 // Check if we should do HTML-based delete confirmation
117 if ($_REQUEST['confirm']) {
118 // <form> can't take arguments in its "action" parameter
119 // so add any arguments as hidden inputs
120 $query = explode_querystring($a->query_string);
122 foreach ($query['args'] as $arg) {
123 if (strpos($arg, 'confirm=') === false) {
124 $arg_parts = explode('=', $arg);
125 $inputs[] = ['name' => $arg_parts[0], 'value' => $arg_parts[1]];
129 //$a->page['aside'] = '';
130 return replace_macros(get_markup_template('confirm.tpl'), [
132 '$message' => L10n::t('Do you really want to delete this message?'),
133 '$extra_inputs' => $inputs,
134 '$confirm' => L10n::t('Yes'),
135 '$confirm_url' => $query['base'],
136 '$confirm_name' => 'confirmed',
137 '$cancel' => L10n::t('Cancel'),
140 // Now check how the user responded to the confirmation query
141 if ($_REQUEST['canceled']) {
142 goaway($_SESSION['return_url']);
146 if ($cmd === 'drop') {
147 $r = q("DELETE FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
152 info(L10n::t('Message deleted.') . EOL);
154 //goaway(System::baseUrl(true) . '/message' );
155 goaway($_SESSION['return_url']);
157 $r = q("SELECT `parent-uri`,`convid` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
161 if (DBM::is_result($r)) {
162 $parent = $r[0]['parent-uri'];
163 $convid = $r[0]['convid'];
165 $r = q("DELETE FROM `mail` WHERE `parent-uri` = '%s' AND `uid` = %d ",
170 // remove diaspora conversation pointer
171 // Actually if we do this, we can never receive another reply to that conversation,
172 // as we will never again have the info we need to re-create it.
173 // We'll just have to orphan it.
175 // q("delete from conv where id = %d limit 1",
181 info(L10n::t('Conversation removed.') . EOL);
184 //goaway(System::baseUrl(true) . '/message' );
185 goaway($_SESSION['return_url']);
189 if (($a->argc > 1) && ($a->argv[1] === 'new')) {
192 $tpl = get_markup_template('msg-header.tpl');
193 $a->page['htmlhead'] .= replace_macros($tpl, [
194 '$baseurl' => System::baseUrl(true),
195 '$nickname' => $a->user['nickname'],
196 '$linkurl' => L10n::t('Please enter a link URL:')
199 $tpl = get_markup_template('msg-end.tpl');
200 $a->page['end'] .= replace_macros($tpl, [
201 '$baseurl' => System::baseUrl(true),
202 '$nickname' => $a->user['nickname'],
203 '$linkurl' => L10n::t('Please enter a link URL:')
206 $preselect = isset($a->argv[2]) ? [$a->argv[2]] : false;
208 $prename = $preurl = $preid = '';
211 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `id` = %d LIMIT 1",
212 intval(local_user()),
215 if (!DBM::is_result($r)) {
216 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' LIMIT 1",
217 intval(local_user()),
218 dbesc(normalise_link(base64_decode($a->argv[2])))
222 if (!DBM::is_result($r)) {
223 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `addr` = '%s' LIMIT 1",
224 intval(local_user()),
225 dbesc(base64_decode($a->argv[2]))
229 if (DBM::is_result($r)) {
230 $prename = $r[0]['name'];
231 $preurl = $r[0]['url'];
232 $preid = $r[0]['id'];
233 $preselect = [$preid];
239 $prefill = $preselect ? $prename : '';
241 // the ugly select box
242 $select = contact_select('messageto', 'message-to-select', $preselect, 4, true, false, false, 10);
244 $tpl = get_markup_template('prv_message.tpl');
245 $o .= replace_macros($tpl, [
246 '$header' => L10n::t('Send Private Message'),
247 '$to' => L10n::t('To:'),
248 '$showinputs' => 'true',
249 '$prefill' => $prefill,
250 '$autocomp' => $autocomp,
252 '$subject' => L10n::t('Subject:'),
253 '$subjtxt' => x($_REQUEST, 'subject') ? strip_tags($_REQUEST['subject']) : '',
254 '$text' => x($_REQUEST, 'body') ? escape_tags(htmlspecialchars($_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_url'] = $a->query_string;
276 $r = q("SELECT count(*) AS `total`, ANY_VALUE(`created`) AS `created` FROM `mail`
277 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
281 if (DBM::is_result($r)) {
282 $a->set_pager_total($r[0]['total']);
285 $r = get_messages(local_user(), $a->pager['start'], $a->pager['itemspage']);
287 if (!DBM::is_result($r)) {
288 info(L10n::t('No messages.') . EOL);
292 $o .= render_messages($r, 'mail_list.tpl');
299 if (($a->argc > 1) && (intval($a->argv[1]))) {
303 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
304 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
305 WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
306 intval(local_user()),
309 if (DBM::is_result($r)) {
310 $contact_id = $r[0]['contact-id'];
311 $convid = $r[0]['convid'];
313 $sql_extra = sprintf(" and `mail`.`parent-uri` = '%s' ", dbesc($r[0]['parent-uri']));
315 $sql_extra = sprintf(" and ( `mail`.`parent-uri` = '%s' OR `mail`.`convid` = '%d' ) ",
316 dbesc($r[0]['parent-uri']),
320 $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
321 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
322 WHERE `mail`.`uid` = %d $sql_extra ORDER BY `mail`.`created` ASC",
326 if (!count($messages)) {
327 notice(L10n::t('Message not available.') . EOL);
331 $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
332 dbesc($r[0]['parent-uri']),
336 require_once("include/bbcode.php");
338 $tpl = get_markup_template('msg-header.tpl');
339 $a->page['htmlhead'] .= replace_macros($tpl, [
340 '$baseurl' => System::baseUrl(true),
341 '$nickname' => $a->user['nickname'],
342 '$linkurl' => L10n::t('Please enter a link URL:')
345 $tpl = get_markup_template('msg-end.tpl');
346 $a->page['end'] .= replace_macros($tpl, [
347 '$baseurl' => System::baseUrl(true),
348 '$nickname' => $a->user['nickname'],
349 '$linkurl' => L10n::t('Please enter a link URL:')
356 foreach ($messages as $message) {
357 if ($message['unknown'])
359 if ($message['from-url'] == $myprofile) {
360 $from_url = $myprofile;
362 } elseif ($message['contact-id'] != 0) {
363 $from_url = 'redir/' . $message['contact-id'];
364 $sparkle = ' sparkle';
366 $from_url = $message['from-url'] . "?zrl=" . urlencode($myprofile);
367 $sparkle = ' sparkle';
370 $extracted = item_extract_images($message['body']);
371 if ($extracted['images']) {
372 $message['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $message['contact-id']);
375 $from_name_e = $message['from-name'];
376 $subject_e = $message['title'];
377 $body_e = Smilies::replace(bbcode($message['body']));
378 $to_name_e = $message['name'];
380 $contact = Contact::getDetailsByURL($message['from-url']);
381 if (isset($contact["thumb"])) {
382 $from_photo = $contact["thumb"];
384 $from_photo = $message['from-photo'];
388 'id' => $message['id'],
389 'from_name' => $from_name_e,
390 'from_url' => $from_url,
391 'from_addr' => $contact['addr'],
392 'sparkle' => $sparkle,
393 'from_photo' => proxy_url($from_photo, false, PROXY_SIZE_THUMB),
394 'subject' => $subject_e,
396 'delete' => L10n::t('Delete message'),
397 'to_name' => $to_name_e,
398 'date' => datetime_convert('UTC', date_default_timezone_get(), $message['created'], 'D, d M Y - g:i A'),
399 'ago' => relative_date($message['created']),
402 $seen = $message['seen'];
405 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
406 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
408 $tpl = get_markup_template('mail_display.tpl');
409 $o = replace_macros($tpl, [
410 '$thread_id' => $a->argv[1],
411 '$thread_subject' => $message['title'],
412 '$thread_seen' => $seen,
413 '$delete' => L10n::t('Delete conversation'),
414 '$canreply' => (($unknown) ? false : '1'),
415 '$unknown_text' => L10n::t("No secure communications available. You <strong>may</strong> be able to respond from the sender's profile page."),
419 '$header' => L10n::t('Send Reply'),
420 '$to' => L10n::t('To:'),
422 '$subject' => L10n::t('Subject:'),
423 '$subjtxt' => $message['title'],
424 '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
425 '$yourmessage' => L10n::t('Your message:'),
427 '$select' => $select,
428 '$parent' => $parent,
429 '$upload' => L10n::t('Upload photo'),
430 '$insert' => L10n::t('Insert web link'),
431 '$submit' => L10n::t('Submit'),
432 '$wait' => L10n::t('Please wait')
439 function get_messages($user, $lstart, $lend)
441 //TODO: rewritte with a sub-query to get the first message of each private thread with certainty
442 return q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`,
443 ANY_VALUE(`mail`.`id`) AS `id`, ANY_VALUE(`mail`.`uid`) AS `uid`, ANY_VALUE(`mail`.`guid`) AS `guid`,
444 ANY_VALUE(`mail`.`from-name`) AS `from-name`, ANY_VALUE(`mail`.`from-photo`) AS `from-photo`,
445 ANY_VALUE(`mail`.`from-url`) AS `from-url`, ANY_VALUE(`mail`.`contact-id`) AS `contact-id`,
446 ANY_VALUE(`mail`.`convid`) AS `convid`, ANY_VALUE(`mail`.`title`) AS `title`, ANY_VALUE(`mail`.`body`) AS `body`,
447 ANY_VALUE(`mail`.`seen`) AS `seen`, ANY_VALUE(`mail`.`reply`) AS `reply`, ANY_VALUE(`mail`.`replied`) AS `replied`,
448 ANY_VALUE(`mail`.`unknown`) AS `unknown`, ANY_VALUE(`mail`.`uri`) AS `uri`,
450 ANY_VALUE(`mail`.`created`) AS `created`, ANY_VALUE(`contact`.`name`) AS `name`, ANY_VALUE(`contact`.`url`) AS `url`,
451 ANY_VALUE(`contact`.`thumb`) AS `thumb`, ANY_VALUE(`contact`.`network`) AS `network`,
452 count( * ) as `count`
453 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
454 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `mailcreated` DESC LIMIT %d , %d ",
455 intval($user), intval($lstart), intval($lend)
459 function render_messages(array $msg, $t)
463 $tpl = get_markup_template($t);
466 $myprofile = System::baseUrl() . '/profile/' . $a->user['nickname'];
468 foreach ($msg as $rr) {
469 if ($rr['unknown']) {
470 $participants = L10n::t("Unknown sender - %s", $rr['from-name']);
471 } elseif (link_compare($rr['from-url'], $myprofile)) {
472 $participants = L10n::t("You and %s", $rr['name']);
474 $participants = L10n::t("%s and You", $rr['from-name']);
477 $subject_e = (($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>');
478 $body_e = $rr['body'];
479 $to_name_e = $rr['name'];
481 $contact = Contact::getDetailsByURL($rr['url']);
482 if (isset($contact["thumb"])) {
483 $from_photo = $contact["thumb"];
485 $from_photo = (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']);
488 $rslt .= replace_macros($tpl, [
490 '$from_name' => $participants,
491 '$from_url' => (($rr['network'] === NETWORK_DFRN) ? 'redir/' . $rr['contact-id'] : $rr['url']),
492 '$from_addr' => $contact['addr'],
493 '$sparkle' => ' sparkle',
494 '$from_photo' => proxy_url($from_photo, false, PROXY_SIZE_THUMB),
495 '$subject' => $subject_e,
496 '$delete' => L10n::t('Delete conversation'),
498 '$to_name' => $to_name_e,
499 '$date' => datetime_convert('UTC', date_default_timezone_get(), $rr['mailcreated'], L10n::t('D, d M Y - g:i A')),
500 '$ago' => relative_date($rr['mailcreated']),
501 '$seen' => $rr['mailseen'],
502 '$count' => L10n::tt('%d message', '%d messages', $rr['count']),