3 * @file mod/message.php
7 use Friendica\Content\Nav;
8 use Friendica\Content\Smilies;
9 use Friendica\Content\Text\BBCode;
10 use Friendica\Core\ACL;
11 use Friendica\Core\L10n;
12 use Friendica\Core\System;
13 use Friendica\Database\DBM;
14 use Friendica\Model\Contact;
15 use Friendica\Model\Mail;
16 use Friendica\Util\DateTimeFormat;
17 use Friendica\Util\Temporal;
19 require_once 'include/conversation.php';
21 function message_init(App $a)
25 if ($a->argc > 1 && is_numeric($a->argv[1])) {
26 $tabs = render_messages(get_messages(local_user(), 0, 5), 'mail_list.tpl');
30 'label' => L10n::t('New Message'),
31 'url' => 'message/new',
32 'sel' => $a->argc > 1 && $a->argv[1] == 'new',
36 $tpl = get_markup_template('message_side.tpl');
37 $a->page['aside'] = replace_macros($tpl, [
41 $base = System::baseUrl();
43 $head_tpl = get_markup_template('message-head.tpl');
44 $a->page['htmlhead'] .= replace_macros($head_tpl, [
45 '$baseurl' => System::baseUrl(true),
49 $end_tpl = get_markup_template('message-end.tpl');
50 $a->page['end'] .= replace_macros($end_tpl, [
51 '$baseurl' => System::baseUrl(true),
56 function message_post(App $a)
59 notice(L10n::t('Permission denied.') . EOL);
63 $replyto = x($_REQUEST, 'replyto') ? notags(trim($_REQUEST['replyto'])) : '';
64 $subject = x($_REQUEST, 'subject') ? notags(trim($_REQUEST['subject'])) : '';
65 $body = x($_REQUEST, 'body') ? escape_tags(trim($_REQUEST['body'])) : '';
66 $recipient = x($_REQUEST, 'messageto') ? intval($_REQUEST['messageto']) : 0;
68 $ret = Mail::send($recipient, $body, $subject, $replyto);
73 notice(L10n::t('No recipient selected.') . EOL);
77 notice(L10n::t('Unable to locate contact information.') . EOL);
80 notice(L10n::t('Message could not be sent.') . EOL);
83 notice(L10n::t('Message collection failure.') . EOL);
86 info(L10n::t('Message sent.') . EOL);
89 // fake it to go back to the input form if no recipient listed
94 goaway($_SESSION['return_url']);
98 function message_content(App $a)
101 Nav::setSelected('messages');
104 notice(L10n::t('Permission denied.') . EOL);
108 $myprofile = System::baseUrl() . '/profile/' . $a->user['nickname'];
110 $tpl = get_markup_template('mail_head.tpl');
111 $header = replace_macros($tpl, [
112 '$messages' => L10n::t('Messages'),
115 if (($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) {
116 if (!intval($a->argv[2])) {
120 // Check if we should do HTML-based delete confirmation
121 if ($_REQUEST['confirm']) {
122 // <form> can't take arguments in its "action" parameter
123 // so add any arguments as hidden inputs
124 $query = explode_querystring($a->query_string);
126 foreach ($query['args'] as $arg) {
127 if (strpos($arg, 'confirm=') === false) {
128 $arg_parts = explode('=', $arg);
129 $inputs[] = ['name' => $arg_parts[0], 'value' => $arg_parts[1]];
133 //$a->page['aside'] = '';
134 return replace_macros(get_markup_template('confirm.tpl'), [
136 '$message' => L10n::t('Do you really want to delete this message?'),
137 '$extra_inputs' => $inputs,
138 '$confirm' => L10n::t('Yes'),
139 '$confirm_url' => $query['base'],
140 '$confirm_name' => 'confirmed',
141 '$cancel' => L10n::t('Cancel'),
144 // Now check how the user responded to the confirmation query
145 if ($_REQUEST['canceled']) {
146 goaway($_SESSION['return_url']);
150 if ($cmd === 'drop') {
151 $r = q("DELETE FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
156 info(L10n::t('Message deleted.') . EOL);
158 //goaway(System::baseUrl(true) . '/message' );
159 goaway($_SESSION['return_url']);
161 $r = q("SELECT `parent-uri`,`convid` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
165 if (DBM::is_result($r)) {
166 $parent = $r[0]['parent-uri'];
167 $convid = $r[0]['convid'];
169 $r = q("DELETE FROM `mail` WHERE `parent-uri` = '%s' AND `uid` = %d ",
174 // remove diaspora conversation pointer
175 // Actually if we do this, we can never receive another reply to that conversation,
176 // as we will never again have the info we need to re-create it.
177 // We'll just have to orphan it.
179 // q("delete from conv where id = %d limit 1",
185 info(L10n::t('Conversation removed.') . EOL);
188 //goaway(System::baseUrl(true) . '/message' );
189 goaway($_SESSION['return_url']);
193 if (($a->argc > 1) && ($a->argv[1] === 'new')) {
196 $tpl = get_markup_template('msg-header.tpl');
197 $a->page['htmlhead'] .= replace_macros($tpl, [
198 '$baseurl' => System::baseUrl(true),
199 '$nickname' => $a->user['nickname'],
200 '$linkurl' => L10n::t('Please enter a link URL:')
203 $tpl = get_markup_template('msg-end.tpl');
204 $a->page['end'] .= replace_macros($tpl, [
205 '$baseurl' => System::baseUrl(true),
206 '$nickname' => $a->user['nickname'],
207 '$linkurl' => L10n::t('Please enter a link URL:')
210 $preselect = isset($a->argv[2]) ? [$a->argv[2]] : [];
212 $prename = $preurl = $preid = '';
215 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `id` = %d LIMIT 1",
216 intval(local_user()),
219 if (!DBM::is_result($r)) {
220 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' LIMIT 1",
221 intval(local_user()),
222 dbesc(normalise_link(base64_decode($a->argv[2])))
226 if (!DBM::is_result($r)) {
227 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `addr` = '%s' LIMIT 1",
228 intval(local_user()),
229 dbesc(base64_decode($a->argv[2]))
233 if (DBM::is_result($r)) {
234 $prename = $r[0]['name'];
235 $preurl = $r[0]['url'];
236 $preid = $r[0]['id'];
237 $preselect = [$preid];
243 $prefill = $preselect ? $prename : '';
245 // the ugly select box
246 $select = ACL::getMessageContactSelectHTML('messageto', 'message-to-select', $preselect, 4, 10);
248 $tpl = get_markup_template('prv_message.tpl');
249 $o .= replace_macros($tpl, [
250 '$header' => L10n::t('Send Private Message'),
251 '$to' => L10n::t('To:'),
252 '$showinputs' => 'true',
253 '$prefill' => $prefill,
255 '$subject' => L10n::t('Subject:'),
256 '$subjtxt' => x($_REQUEST, 'subject') ? strip_tags($_REQUEST['subject']) : '',
257 '$text' => x($_REQUEST, 'body') ? escape_tags(htmlspecialchars($_REQUEST['body'])) : '',
259 '$yourmessage' => L10n::t('Your message:'),
260 '$select' => $select,
262 '$upload' => L10n::t('Upload photo'),
263 '$insert' => L10n::t('Insert web link'),
264 '$wait' => L10n::t('Please wait'),
265 '$submit' => L10n::t('Submit')
271 $_SESSION['return_url'] = $a->query_string;
279 $r = q("SELECT count(*) AS `total`, ANY_VALUE(`created`) AS `created` FROM `mail`
280 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
284 if (DBM::is_result($r)) {
285 $a->set_pager_total($r[0]['total']);
288 $r = get_messages(local_user(), $a->pager['start'], $a->pager['itemspage']);
290 if (!DBM::is_result($r)) {
291 info(L10n::t('No messages.') . EOL);
295 $o .= render_messages($r, 'mail_list.tpl');
302 if (($a->argc > 1) && (intval($a->argv[1]))) {
306 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
307 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
308 WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
309 intval(local_user()),
312 if (DBM::is_result($r)) {
313 $contact_id = $r[0]['contact-id'];
314 $convid = $r[0]['convid'];
316 $sql_extra = sprintf(" and `mail`.`parent-uri` = '%s' ", dbesc($r[0]['parent-uri']));
318 $sql_extra = sprintf(" and ( `mail`.`parent-uri` = '%s' OR `mail`.`convid` = '%d' ) ",
319 dbesc($r[0]['parent-uri']),
323 $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
324 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
325 WHERE `mail`.`uid` = %d $sql_extra ORDER BY `mail`.`created` ASC",
329 if (!count($messages)) {
330 notice(L10n::t('Message not available.') . EOL);
334 $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
335 dbesc($r[0]['parent-uri']),
339 $tpl = get_markup_template('msg-header.tpl');
340 $a->page['htmlhead'] .= replace_macros($tpl, [
341 '$baseurl' => System::baseUrl(true),
342 '$nickname' => $a->user['nickname'],
343 '$linkurl' => L10n::t('Please enter a link URL:')
346 $tpl = get_markup_template('msg-end.tpl');
347 $a->page['end'] .= replace_macros($tpl, [
348 '$baseurl' => System::baseUrl(true),
349 '$nickname' => $a->user['nickname'],
350 '$linkurl' => L10n::t('Please enter a link URL:')
357 foreach ($messages as $message) {
358 if ($message['unknown'])
360 if ($message['from-url'] == $myprofile) {
361 $from_url = $myprofile;
363 } elseif ($message['contact-id'] != 0) {
364 $from_url = 'redir/' . $message['contact-id'];
365 $sparkle = ' sparkle';
367 $from_url = $message['from-url'] . "?zrl=" . urlencode($myprofile);
368 $sparkle = ' sparkle';
371 $extracted = item_extract_images($message['body']);
372 if ($extracted['images']) {
373 $message['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $message['contact-id']);
376 $from_name_e = $message['from-name'];
377 $subject_e = $message['title'];
378 $body_e = Smilies::replace(BBCode::convert($message['body']));
379 $to_name_e = $message['name'];
381 $contact = Contact::getDetailsByURL($message['from-url']);
382 if (isset($contact["thumb"])) {
383 $from_photo = $contact["thumb"];
385 $from_photo = $message['from-photo'];
389 'id' => $message['id'],
390 'from_name' => $from_name_e,
391 'from_url' => $from_url,
392 'from_addr' => $contact['addr'],
393 'sparkle' => $sparkle,
394 'from_photo' => proxy_url($from_photo, false, PROXY_SIZE_THUMB),
395 'subject' => $subject_e,
397 'delete' => L10n::t('Delete message'),
398 'to_name' => $to_name_e,
399 'date' => DateTimeFormat::local($message['created'], L10n::t('D, d M Y - g:i A')),
400 'ago' => Temporal::getRelativeDate($message['created']),
403 $seen = $message['seen'];
406 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
407 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
409 $tpl = get_markup_template('mail_display.tpl');
410 $o = replace_macros($tpl, [
411 '$thread_id' => $a->argv[1],
412 '$thread_subject' => $message['title'],
413 '$thread_seen' => $seen,
414 '$delete' => L10n::t('Delete conversation'),
415 '$canreply' => (($unknown) ? false : '1'),
416 '$unknown_text' => L10n::t("No secure communications available. You <strong>may</strong> be able to respond from the sender's profile page."),
420 '$header' => L10n::t('Send Reply'),
421 '$to' => L10n::t('To:'),
423 '$subject' => L10n::t('Subject:'),
424 '$subjtxt' => $message['title'],
425 '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
426 '$yourmessage' => L10n::t('Your message:'),
428 '$select' => $select,
429 '$parent' => $parent,
430 '$upload' => L10n::t('Upload photo'),
431 '$insert' => L10n::t('Insert web link'),
432 '$submit' => L10n::t('Submit'),
433 '$wait' => L10n::t('Please wait')
440 function get_messages($user, $lstart, $lend)
442 //TODO: rewritte with a sub-query to get the first message of each private thread with certainty
443 return q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`,
444 ANY_VALUE(`mail`.`id`) AS `id`, ANY_VALUE(`mail`.`uid`) AS `uid`, ANY_VALUE(`mail`.`guid`) AS `guid`,
445 ANY_VALUE(`mail`.`from-name`) AS `from-name`, ANY_VALUE(`mail`.`from-photo`) AS `from-photo`,
446 ANY_VALUE(`mail`.`from-url`) AS `from-url`, ANY_VALUE(`mail`.`contact-id`) AS `contact-id`,
447 ANY_VALUE(`mail`.`convid`) AS `convid`, ANY_VALUE(`mail`.`title`) AS `title`, ANY_VALUE(`mail`.`body`) AS `body`,
448 ANY_VALUE(`mail`.`seen`) AS `seen`, ANY_VALUE(`mail`.`reply`) AS `reply`, ANY_VALUE(`mail`.`replied`) AS `replied`,
449 ANY_VALUE(`mail`.`unknown`) AS `unknown`, ANY_VALUE(`mail`.`uri`) AS `uri`,
451 ANY_VALUE(`mail`.`created`) AS `created`, ANY_VALUE(`contact`.`name`) AS `name`, ANY_VALUE(`contact`.`url`) AS `url`,
452 ANY_VALUE(`contact`.`thumb`) AS `thumb`, ANY_VALUE(`contact`.`network`) AS `network`,
453 count( * ) as `count`
454 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
455 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `mailcreated` DESC LIMIT %d , %d ",
456 intval($user), intval($lstart), intval($lend)
460 function render_messages(array $msg, $t)
464 $tpl = get_markup_template($t);
467 $myprofile = System::baseUrl() . '/profile/' . $a->user['nickname'];
469 foreach ($msg as $rr) {
470 if ($rr['unknown']) {
471 $participants = L10n::t("Unknown sender - %s", $rr['from-name']);
472 } elseif (link_compare($rr['from-url'], $myprofile)) {
473 $participants = L10n::t("You and %s", $rr['name']);
475 $participants = L10n::t("%s and You", $rr['from-name']);
478 $subject_e = (($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>');
479 $body_e = $rr['body'];
480 $to_name_e = $rr['name'];
482 $contact = Contact::getDetailsByURL($rr['url']);
483 if (isset($contact["thumb"])) {
484 $from_photo = $contact["thumb"];
486 $from_photo = (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']);
489 $rslt .= replace_macros($tpl, [
491 '$from_name' => $participants,
492 '$from_url' => (($rr['network'] === NETWORK_DFRN) ? 'redir/' . $rr['contact-id'] : $rr['url']),
493 '$from_addr' => $contact['addr'],
494 '$sparkle' => ' sparkle',
495 '$from_photo' => proxy_url($from_photo, false, PROXY_SIZE_THUMB),
496 '$subject' => $subject_e,
497 '$delete' => L10n::t('Delete conversation'),
499 '$to_name' => $to_name_e,
500 '$date' => DateTimeFormat::local($rr['mailcreated'], L10n::t('D, d M Y - g:i A')),
501 '$ago' => Temporal::getRelativeDate($rr['mailcreated']),
502 '$seen' => $rr['mailseen'],
503 '$count' => L10n::tt('%d message', '%d messages', $rr['count']),