3 * @file mod/message.php
6 use Friendica\Content\Smilies;
7 use Friendica\Core\System;
8 use Friendica\Database\DBM;
9 use Friendica\Model\Contact;
10 use Friendica\Model\Mail;
12 require_once 'include/acl_selectors.php';
13 require_once 'include/conversation.php';
15 function message_init(App $a)
19 if ($a->argc > 1 && is_numeric($a->argv[1])) {
20 $tabs = render_messages(get_messages(local_user(), 0, 5), 'mail_list.tpl');
24 'label' => t('New Message'),
25 'url' => 'message/new',
26 'sel' => $a->argc > 1 && $a->argv[1] == 'new',
30 $tpl = get_markup_template('message_side.tpl');
31 $a->page['aside'] = replace_macros($tpl, [
35 $base = System::baseUrl();
37 $head_tpl = get_markup_template('message-head.tpl');
38 $a->page['htmlhead'] .= replace_macros($head_tpl, [
39 '$baseurl' => System::baseUrl(true),
43 $end_tpl = get_markup_template('message-end.tpl');
44 $a->page['end'] .= replace_macros($end_tpl, [
45 '$baseurl' => System::baseUrl(true),
50 function message_post(App $a)
53 notice(t('Permission denied.') . EOL);
57 $replyto = x($_REQUEST, 'replyto') ? notags(trim($_REQUEST['replyto'])) : '';
58 $subject = x($_REQUEST, 'subject') ? notags(trim($_REQUEST['subject'])) : '';
59 $body = x($_REQUEST, 'body') ? escape_tags(trim($_REQUEST['body'])) : '';
60 $recipient = x($_REQUEST, 'messageto') ? intval($_REQUEST['messageto']) : 0;
62 $ret = Mail::send($recipient, $body, $subject, $replyto);
67 notice(t('No recipient selected.') . EOL);
71 notice(t('Unable to locate contact information.') . EOL);
74 notice(t('Message could not be sent.') . EOL);
77 notice(t('Message collection failure.') . EOL);
80 info(t('Message sent.') . EOL);
83 // fake it to go back to the input form if no recipient listed
88 goaway($_SESSION['return_url']);
92 function message_content(App $a)
95 nav_set_selected('messages');
98 notice(t('Permission denied.') . EOL);
102 $myprofile = System::baseUrl() . '/profile/' . $a->user['nickname'];
104 $tpl = get_markup_template('mail_head.tpl');
105 $header = replace_macros($tpl, [
106 '$messages' => t('Messages'),
109 if (($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) {
110 if (!intval($a->argv[2])) {
114 // Check if we should do HTML-based delete confirmation
115 if ($_REQUEST['confirm']) {
116 // <form> can't take arguments in its "action" parameter
117 // so add any arguments as hidden inputs
118 $query = explode_querystring($a->query_string);
120 foreach ($query['args'] as $arg) {
121 if (strpos($arg, 'confirm=') === false) {
122 $arg_parts = explode('=', $arg);
123 $inputs[] = ['name' => $arg_parts[0], 'value' => $arg_parts[1]];
127 //$a->page['aside'] = '';
128 return replace_macros(get_markup_template('confirm.tpl'), [
130 '$message' => t('Do you really want to delete this message?'),
131 '$extra_inputs' => $inputs,
132 '$confirm' => t('Yes'),
133 '$confirm_url' => $query['base'],
134 '$confirm_name' => 'confirmed',
135 '$cancel' => t('Cancel'),
138 // Now check how the user responded to the confirmation query
139 if ($_REQUEST['canceled']) {
140 goaway($_SESSION['return_url']);
144 if ($cmd === 'drop') {
145 $r = q("DELETE FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
150 info(t('Message deleted.') . EOL);
152 //goaway(System::baseUrl(true) . '/message' );
153 goaway($_SESSION['return_url']);
155 $r = q("SELECT `parent-uri`,`convid` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
159 if (DBM::is_result($r)) {
160 $parent = $r[0]['parent-uri'];
161 $convid = $r[0]['convid'];
163 $r = q("DELETE FROM `mail` WHERE `parent-uri` = '%s' AND `uid` = %d ",
168 // remove diaspora conversation pointer
169 // Actually if we do this, we can never receive another reply to that conversation,
170 // as we will never again have the info we need to re-create it.
171 // We'll just have to orphan it.
173 // q("delete from conv where id = %d limit 1",
179 info(t('Conversation removed.') . EOL);
182 //goaway(System::baseUrl(true) . '/message' );
183 goaway($_SESSION['return_url']);
187 if (($a->argc > 1) && ($a->argv[1] === 'new')) {
190 $tpl = get_markup_template('msg-header.tpl');
191 $a->page['htmlhead'] .= replace_macros($tpl, [
192 '$baseurl' => System::baseUrl(true),
193 '$nickname' => $a->user['nickname'],
194 '$linkurl' => t('Please enter a link URL:')
197 $tpl = get_markup_template('msg-end.tpl');
198 $a->page['end'] .= replace_macros($tpl, [
199 '$baseurl' => System::baseUrl(true),
200 '$nickname' => $a->user['nickname'],
201 '$linkurl' => t('Please enter a link URL:')
204 $preselect = isset($a->argv[2]) ? [$a->argv[2]] : false;
206 $prename = $preurl = $preid = '';
209 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `id` = %d LIMIT 1",
210 intval(local_user()),
213 if (!DBM::is_result($r)) {
214 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' LIMIT 1",
215 intval(local_user()),
216 dbesc(normalise_link(base64_decode($a->argv[2])))
220 if (!DBM::is_result($r)) {
221 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `addr` = '%s' LIMIT 1",
222 intval(local_user()),
223 dbesc(base64_decode($a->argv[2]))
227 if (DBM::is_result($r)) {
228 $prename = $r[0]['name'];
229 $preurl = $r[0]['url'];
230 $preid = $r[0]['id'];
231 $preselect = [$preid];
237 $prefill = $preselect ? $prename : '';
239 // the ugly select box
240 $select = contact_select('messageto', 'message-to-select', $preselect, 4, true, false, false, 10);
242 $tpl = get_markup_template('prv_message.tpl');
243 $o .= replace_macros($tpl, [
244 '$header' => t('Send Private Message'),
246 '$showinputs' => 'true',
247 '$prefill' => $prefill,
248 '$autocomp' => $autocomp,
250 '$subject' => t('Subject:'),
251 '$subjtxt' => x($_REQUEST, 'subject') ? strip_tags($_REQUEST['subject']) : '',
252 '$text' => x($_REQUEST, 'body') ? escape_tags(htmlspecialchars($_REQUEST['body'])) : '',
254 '$yourmessage' => t('Your message:'),
255 '$select' => $select,
257 '$upload' => t('Upload photo'),
258 '$insert' => t('Insert web link'),
259 '$wait' => t('Please wait'),
260 '$submit' => t('Submit')
266 $_SESSION['return_url'] = $a->query_string;
274 $r = q("SELECT count(*) AS `total`, ANY_VALUE(`created`) AS `created` FROM `mail`
275 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
279 if (DBM::is_result($r)) {
280 $a->set_pager_total($r[0]['total']);
283 $r = get_messages(local_user(), $a->pager['start'], $a->pager['itemspage']);
285 if (!DBM::is_result($r)) {
286 info(t('No messages.') . EOL);
290 $o .= render_messages($r, 'mail_list.tpl');
297 if (($a->argc > 1) && (intval($a->argv[1]))) {
301 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
302 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
303 WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
304 intval(local_user()),
307 if (DBM::is_result($r)) {
308 $contact_id = $r[0]['contact-id'];
309 $convid = $r[0]['convid'];
311 $sql_extra = sprintf(" and `mail`.`parent-uri` = '%s' ", dbesc($r[0]['parent-uri']));
313 $sql_extra = sprintf(" and ( `mail`.`parent-uri` = '%s' OR `mail`.`convid` = '%d' ) ",
314 dbesc($r[0]['parent-uri']),
318 $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
319 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
320 WHERE `mail`.`uid` = %d $sql_extra ORDER BY `mail`.`created` ASC",
324 if (!count($messages)) {
325 notice(t('Message not available.') . EOL);
329 $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
330 dbesc($r[0]['parent-uri']),
334 require_once("include/bbcode.php");
336 $tpl = get_markup_template('msg-header.tpl');
337 $a->page['htmlhead'] .= replace_macros($tpl, [
338 '$baseurl' => System::baseUrl(true),
339 '$nickname' => $a->user['nickname'],
340 '$linkurl' => t('Please enter a link URL:')
343 $tpl = get_markup_template('msg-end.tpl');
344 $a->page['end'] .= replace_macros($tpl, [
345 '$baseurl' => System::baseUrl(true),
346 '$nickname' => $a->user['nickname'],
347 '$linkurl' => t('Please enter a link URL:')
354 foreach ($messages as $message) {
355 if ($message['unknown'])
357 if ($message['from-url'] == $myprofile) {
358 $from_url = $myprofile;
360 } elseif ($message['contact-id'] != 0) {
361 $from_url = 'redir/' . $message['contact-id'];
362 $sparkle = ' sparkle';
364 $from_url = $message['from-url'] . "?zrl=" . urlencode($myprofile);
365 $sparkle = ' sparkle';
368 $extracted = item_extract_images($message['body']);
369 if ($extracted['images']) {
370 $message['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $message['contact-id']);
373 $from_name_e = $message['from-name'];
374 $subject_e = $message['title'];
375 $body_e = Smilies::replace(bbcode($message['body']));
376 $to_name_e = $message['name'];
378 $contact = Contact::getDetailsByURL($message['from-url']);
379 if (isset($contact["thumb"])) {
380 $from_photo = $contact["thumb"];
382 $from_photo = $message['from-photo'];
386 'id' => $message['id'],
387 'from_name' => $from_name_e,
388 'from_url' => $from_url,
389 'from_addr' => $contact['addr'],
390 'sparkle' => $sparkle,
391 'from_photo' => proxy_url($from_photo, false, PROXY_SIZE_THUMB),
392 'subject' => $subject_e,
394 'delete' => t('Delete message'),
395 'to_name' => $to_name_e,
396 'date' => datetime_convert('UTC', date_default_timezone_get(), $message['created'], 'D, d M Y - g:i A'),
397 'ago' => relative_date($message['created']),
400 $seen = $message['seen'];
403 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
404 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
406 $tpl = get_markup_template('mail_display.tpl');
407 $o = replace_macros($tpl, [
408 '$thread_id' => $a->argv[1],
409 '$thread_subject' => $message['title'],
410 '$thread_seen' => $seen,
411 '$delete' => t('Delete conversation'),
412 '$canreply' => (($unknown) ? false : '1'),
413 '$unknown_text' => t("No secure communications available. You <strong>may</strong> be able to respond from the sender's profile page."),
417 '$header' => t('Send Reply'),
420 '$subject' => t('Subject:'),
421 '$subjtxt' => $message['title'],
422 '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
423 '$yourmessage' => t('Your message:'),
425 '$select' => $select,
426 '$parent' => $parent,
427 '$upload' => t('Upload photo'),
428 '$insert' => t('Insert web link'),
429 '$submit' => t('Submit'),
430 '$wait' => t('Please wait')
437 function get_messages($user, $lstart, $lend)
439 //TODO: rewritte with a sub-query to get the first message of each private thread with certainty
440 return q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`,
441 ANY_VALUE(`mail`.`id`) AS `id`, ANY_VALUE(`mail`.`uid`) AS `uid`, ANY_VALUE(`mail`.`guid`) AS `guid`,
442 ANY_VALUE(`mail`.`from-name`) AS `from-name`, ANY_VALUE(`mail`.`from-photo`) AS `from-photo`,
443 ANY_VALUE(`mail`.`from-url`) AS `from-url`, ANY_VALUE(`mail`.`contact-id`) AS `contact-id`,
444 ANY_VALUE(`mail`.`convid`) AS `convid`, ANY_VALUE(`mail`.`title`) AS `title`, ANY_VALUE(`mail`.`body`) AS `body`,
445 ANY_VALUE(`mail`.`seen`) AS `seen`, ANY_VALUE(`mail`.`reply`) AS `reply`, ANY_VALUE(`mail`.`replied`) AS `replied`,
446 ANY_VALUE(`mail`.`unknown`) AS `unknown`, ANY_VALUE(`mail`.`uri`) AS `uri`,
448 ANY_VALUE(`mail`.`created`) AS `created`, ANY_VALUE(`contact`.`name`) AS `name`, ANY_VALUE(`contact`.`url`) AS `url`,
449 ANY_VALUE(`contact`.`thumb`) AS `thumb`, ANY_VALUE(`contact`.`network`) AS `network`,
450 count( * ) as `count`
451 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
452 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `mailcreated` DESC LIMIT %d , %d ",
453 intval($user), intval($lstart), intval($lend)
457 function render_messages(array $msg, $t)
461 $tpl = get_markup_template($t);
464 $myprofile = System::baseUrl() . '/profile/' . $a->user['nickname'];
466 foreach ($msg as $rr) {
467 if ($rr['unknown']) {
468 $participants = t("Unknown sender - %s", $rr['from-name']);
469 } elseif (link_compare($rr['from-url'], $myprofile)) {
470 $participants = t("You and %s", $rr['name']);
472 $participants = t("%s and You", $rr['from-name']);
475 $subject_e = (($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>');
476 $body_e = $rr['body'];
477 $to_name_e = $rr['name'];
479 $contact = Contact::getDetailsByURL($rr['url']);
480 if (isset($contact["thumb"])) {
481 $from_photo = $contact["thumb"];
483 $from_photo = (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']);
486 $rslt .= replace_macros($tpl, [
488 '$from_name' => $participants,
489 '$from_url' => (($rr['network'] === NETWORK_DFRN) ? 'redir/' . $rr['contact-id'] : $rr['url']),
490 '$from_addr' => $contact['addr'],
491 '$sparkle' => ' sparkle',
492 '$from_photo' => proxy_url($from_photo, false, PROXY_SIZE_THUMB),
493 '$subject' => $subject_e,
494 '$delete' => t('Delete conversation'),
496 '$to_name' => $to_name_e,
497 '$date' => datetime_convert('UTC', date_default_timezone_get(), $rr['mailcreated'], t('D, d M Y - g:i A')),
498 '$ago' => relative_date($rr['mailcreated']),
499 '$seen' => $rr['mailseen'],
500 '$count' => tt('%d message', '%d messages', $rr['count']),