4 use Friendica\Content\Smilies;
5 use Friendica\Core\System;
6 use Friendica\Database\DBM;
7 use Friendica\Model\Contact;
9 require_once 'include/acl_selectors.php';
10 require_once 'include/message.php';
11 require_once 'include/conversation.php';
13 function message_init(App $a)
17 if ($a->argc > 1 && is_numeric($a->argv[1])) {
18 $tabs = render_messages(get_messages(local_user(), 0, 5), 'mail_list.tpl');
22 'label' => t('New Message'),
23 'url' => 'message/new',
24 'sel' => $a->argc > 1 && $a->argv[1] == 'new',
28 $tpl = get_markup_template('message_side.tpl');
29 $a->page['aside'] = replace_macros($tpl, array(
33 $base = System::baseUrl();
35 $head_tpl = get_markup_template('message-head.tpl');
36 $a->page['htmlhead'] .= replace_macros($head_tpl, array(
37 '$baseurl' => System::baseUrl(true),
41 $end_tpl = get_markup_template('message-end.tpl');
42 $a->page['end'] .= replace_macros($end_tpl, array(
43 '$baseurl' => System::baseUrl(true),
48 function message_post(App $a)
51 notice(t('Permission denied.') . EOL);
55 $replyto = x($_REQUEST, 'replyto') ? notags(trim($_REQUEST['replyto'])) : '';
56 $subject = x($_REQUEST, 'subject') ? notags(trim($_REQUEST['subject'])) : '';
57 $body = x($_REQUEST, 'body') ? escape_tags(trim($_REQUEST['body'])) : '';
58 $recipient = x($_REQUEST, 'messageto') ? intval($_REQUEST['messageto']) : 0;
60 $ret = send_message($recipient, $body, $subject, $replyto);
65 notice(t('No recipient selected.') . EOL);
69 notice(t('Unable to locate contact information.') . EOL);
72 notice(t('Message could not be sent.') . EOL);
75 notice(t('Message collection failure.') . EOL);
78 info(t('Message sent.') . EOL);
81 // fake it to go back to the input form if no recipient listed
86 goaway($_SESSION['return_url']);
90 function message_content(App $a)
93 nav_set_selected('messages');
96 notice(t('Permission denied.') . EOL);
100 $myprofile = System::baseUrl() . '/profile/' . $a->user['nickname'];
102 $tpl = get_markup_template('mail_head.tpl');
103 $header = replace_macros($tpl, array(
104 '$messages' => t('Messages'),
107 if (($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) {
108 if (!intval($a->argv[2])) {
112 // Check if we should do HTML-based delete confirmation
113 if ($_REQUEST['confirm']) {
114 // <form> can't take arguments in its "action" parameter
115 // so add any arguments as hidden inputs
116 $query = explode_querystring($a->query_string);
118 foreach ($query['args'] as $arg) {
119 if (strpos($arg, 'confirm=') === false) {
120 $arg_parts = explode('=', $arg);
121 $inputs[] = array('name' => $arg_parts[0], 'value' => $arg_parts[1]);
125 //$a->page['aside'] = '';
126 return replace_macros(get_markup_template('confirm.tpl'), array(
128 '$message' => t('Do you really want to delete this message?'),
129 '$extra_inputs' => $inputs,
130 '$confirm' => t('Yes'),
131 '$confirm_url' => $query['base'],
132 '$confirm_name' => 'confirmed',
133 '$cancel' => t('Cancel'),
136 // Now check how the user responded to the confirmation query
137 if ($_REQUEST['canceled']) {
138 goaway($_SESSION['return_url']);
142 if ($cmd === 'drop') {
143 $r = q("DELETE FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
148 info(t('Message deleted.') . EOL);
150 //goaway(System::baseUrl(true) . '/message' );
151 goaway($_SESSION['return_url']);
153 $r = q("SELECT `parent-uri`,`convid` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
157 if (DBM::is_result($r)) {
158 $parent = $r[0]['parent-uri'];
159 $convid = $r[0]['convid'];
161 $r = q("DELETE FROM `mail` WHERE `parent-uri` = '%s' AND `uid` = %d ",
166 // remove diaspora conversation pointer
167 // Actually if we do this, we can never receive another reply to that conversation,
168 // as we will never again have the info we need to re-create it.
169 // We'll just have to orphan it.
171 // q("delete from conv where id = %d limit 1",
177 info(t('Conversation removed.') . EOL);
180 //goaway(System::baseUrl(true) . '/message' );
181 goaway($_SESSION['return_url']);
185 if (($a->argc > 1) && ($a->argv[1] === 'new')) {
188 $tpl = get_markup_template('msg-header.tpl');
189 $a->page['htmlhead'] .= replace_macros($tpl, array(
190 '$baseurl' => System::baseUrl(true),
191 '$nickname' => $a->user['nickname'],
192 '$linkurl' => t('Please enter a link URL:')
195 $tpl = get_markup_template('msg-end.tpl');
196 $a->page['end'] .= replace_macros($tpl, array(
197 '$baseurl' => System::baseUrl(true),
198 '$nickname' => $a->user['nickname'],
199 '$linkurl' => t('Please enter a link URL:')
202 $preselect = isset($a->argv[2]) ? array($a->argv[2]) : false;
204 $prename = $preurl = $preid = '';
207 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `id` = %d LIMIT 1",
208 intval(local_user()),
211 if (!DBM::is_result($r)) {
212 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' LIMIT 1",
213 intval(local_user()),
214 dbesc(normalise_link(base64_decode($a->argv[2])))
218 if (!DBM::is_result($r)) {
219 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `addr` = '%s' LIMIT 1",
220 intval(local_user()),
221 dbesc(base64_decode($a->argv[2]))
225 if (DBM::is_result($r)) {
226 $prename = $r[0]['name'];
227 $preurl = $r[0]['url'];
228 $preid = $r[0]['id'];
229 $preselect = array($preid);
235 $prefill = $preselect ? $prename : '';
237 // the ugly select box
238 $select = contact_select('messageto', 'message-to-select', $preselect, 4, true, false, false, 10);
240 $tpl = get_markup_template('prv_message.tpl');
241 $o .= replace_macros($tpl, array(
242 '$header' => t('Send Private Message'),
244 '$showinputs' => 'true',
245 '$prefill' => $prefill,
246 '$autocomp' => $autocomp,
248 '$subject' => t('Subject:'),
249 '$subjtxt' => x($_REQUEST, 'subject') ? strip_tags($_REQUEST['subject']) : '',
250 '$text' => x($_REQUEST, 'body') ? escape_tags(htmlspecialchars($_REQUEST['body'])) : '',
252 '$yourmessage' => t('Your message:'),
253 '$select' => $select,
255 '$upload' => t('Upload photo'),
256 '$insert' => t('Insert web link'),
257 '$wait' => t('Please wait'),
258 '$submit' => t('Submit')
264 $_SESSION['return_url'] = $a->query_string;
272 $r = q("SELECT count(*) AS `total`, ANY_VALUE(`created`) AS `created` FROM `mail`
273 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
277 if (DBM::is_result($r)) {
278 $a->set_pager_total($r[0]['total']);
281 $r = get_messages(local_user(), $a->pager['start'], $a->pager['itemspage']);
283 if (!DBM::is_result($r)) {
284 info(t('No messages.') . EOL);
288 $o .= render_messages($r, 'mail_list.tpl');
295 if (($a->argc > 1) && (intval($a->argv[1]))) {
299 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
300 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
301 WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
302 intval(local_user()),
305 if (DBM::is_result($r)) {
306 $contact_id = $r[0]['contact-id'];
307 $convid = $r[0]['convid'];
309 $sql_extra = sprintf(" and `mail`.`parent-uri` = '%s' ", dbesc($r[0]['parent-uri']));
311 $sql_extra = sprintf(" and ( `mail`.`parent-uri` = '%s' OR `mail`.`convid` = '%d' ) ",
312 dbesc($r[0]['parent-uri']),
316 $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
317 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
318 WHERE `mail`.`uid` = %d $sql_extra ORDER BY `mail`.`created` ASC",
322 if (!count($messages)) {
323 notice(t('Message not available.') . EOL);
327 $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
328 dbesc($r[0]['parent-uri']),
332 require_once("include/bbcode.php");
334 $tpl = get_markup_template('msg-header.tpl');
335 $a->page['htmlhead'] .= replace_macros($tpl, array(
336 '$baseurl' => System::baseUrl(true),
337 '$nickname' => $a->user['nickname'],
338 '$linkurl' => t('Please enter a link URL:')
341 $tpl = get_markup_template('msg-end.tpl');
342 $a->page['end'] .= replace_macros($tpl, array(
343 '$baseurl' => System::baseUrl(true),
344 '$nickname' => $a->user['nickname'],
345 '$linkurl' => t('Please enter a link URL:')
352 foreach ($messages as $message) {
353 if ($message['unknown'])
355 if ($message['from-url'] == $myprofile) {
356 $from_url = $myprofile;
358 } elseif ($message['contact-id'] != 0) {
359 $from_url = 'redir/' . $message['contact-id'];
360 $sparkle = ' sparkle';
362 $from_url = $message['from-url'] . "?zrl=" . urlencode($myprofile);
363 $sparkle = ' sparkle';
366 $extracted = item_extract_images($message['body']);
367 if ($extracted['images']) {
368 $message['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $message['contact-id']);
371 $from_name_e = $message['from-name'];
372 $subject_e = $message['title'];
373 $body_e = Smilies::replace(bbcode($message['body']));
374 $to_name_e = $message['name'];
376 $contact = Contact::getDetailsByURL($message['from-url']);
377 if (isset($contact["thumb"])) {
378 $from_photo = $contact["thumb"];
380 $from_photo = $message['from-photo'];
384 'id' => $message['id'],
385 'from_name' => $from_name_e,
386 'from_url' => $from_url,
387 'from_addr' => $contact['addr'],
388 'sparkle' => $sparkle,
389 'from_photo' => proxy_url($from_photo, false, PROXY_SIZE_THUMB),
390 'subject' => $subject_e,
392 'delete' => t('Delete message'),
393 'to_name' => $to_name_e,
394 'date' => datetime_convert('UTC', date_default_timezone_get(), $message['created'], 'D, d M Y - g:i A'),
395 'ago' => relative_date($message['created']),
398 $seen = $message['seen'];
401 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
402 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
404 $tpl = get_markup_template('mail_display.tpl');
405 $o = replace_macros($tpl, array(
406 '$thread_id' => $a->argv[1],
407 '$thread_subject' => $message['title'],
408 '$thread_seen' => $seen,
409 '$delete' => t('Delete conversation'),
410 '$canreply' => (($unknown) ? false : '1'),
411 '$unknown_text' => t("No secure communications available. You <strong>may</strong> be able to respond from the sender's profile page."),
415 '$header' => t('Send Reply'),
418 '$subject' => t('Subject:'),
419 '$subjtxt' => $message['title'],
420 '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
421 '$yourmessage' => t('Your message:'),
423 '$select' => $select,
424 '$parent' => $parent,
425 '$upload' => t('Upload photo'),
426 '$insert' => t('Insert web link'),
427 '$submit' => t('Submit'),
428 '$wait' => t('Please wait')
435 function get_messages($user, $lstart, $lend)
437 //TODO: rewritte with a sub-query to get the first message of each private thread with certainty
438 return q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`,
439 ANY_VALUE(`mail`.`id`) AS `id`, ANY_VALUE(`mail`.`uid`) AS `uid`, ANY_VALUE(`mail`.`guid`) AS `guid`,
440 ANY_VALUE(`mail`.`from-name`) AS `from-name`, ANY_VALUE(`mail`.`from-photo`) AS `from-photo`,
441 ANY_VALUE(`mail`.`from-url`) AS `from-url`, ANY_VALUE(`mail`.`contact-id`) AS `contact-id`,
442 ANY_VALUE(`mail`.`convid`) AS `convid`, ANY_VALUE(`mail`.`title`) AS `title`, ANY_VALUE(`mail`.`body`) AS `body`,
443 ANY_VALUE(`mail`.`seen`) AS `seen`, ANY_VALUE(`mail`.`reply`) AS `reply`, ANY_VALUE(`mail`.`replied`) AS `replied`,
444 ANY_VALUE(`mail`.`unknown`) AS `unknown`, ANY_VALUE(`mail`.`uri`) AS `uri`,
446 ANY_VALUE(`mail`.`created`) AS `created`, ANY_VALUE(`contact`.`name`) AS `name`, ANY_VALUE(`contact`.`url`) AS `url`,
447 ANY_VALUE(`contact`.`thumb`) AS `thumb`, ANY_VALUE(`contact`.`network`) AS `network`,
448 count( * ) as `count`
449 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
450 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `mailcreated` DESC LIMIT %d , %d ",
451 intval($user), intval($lstart), intval($lend)
455 function render_messages(array $msg, $t)
459 $tpl = get_markup_template($t);
462 $myprofile = System::baseUrl() . '/profile/' . $a->user['nickname'];
464 foreach ($msg as $rr) {
465 if ($rr['unknown']) {
466 $participants = t("Unknown sender - %s", $rr['from-name']);
467 } elseif (link_compare($rr['from-url'], $myprofile)) {
468 $participants = t("You and %s", $rr['name']);
470 $participants = t("%s and You", $rr['from-name']);
473 $subject_e = (($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>');
474 $body_e = $rr['body'];
475 $to_name_e = $rr['name'];
477 $contact = Contact::getDetailsByURL($rr['url']);
478 if (isset($contact["thumb"])) {
479 $from_photo = $contact["thumb"];
481 $from_photo = (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']);
484 $rslt .= replace_macros($tpl, array(
486 '$from_name' => $participants,
487 '$from_url' => (($rr['network'] === NETWORK_DFRN) ? 'redir/' . $rr['contact-id'] : $rr['url']),
488 '$from_addr' => $contact['addr'],
489 '$sparkle' => ' sparkle',
490 '$from_photo' => proxy_url($from_photo, false, PROXY_SIZE_THUMB),
491 '$subject' => $subject_e,
492 '$delete' => t('Delete conversation'),
494 '$to_name' => $to_name_e,
495 '$date' => datetime_convert('UTC', date_default_timezone_get(), $rr['mailcreated'], t('D, d M Y - g:i A')),
496 '$ago' => relative_date($rr['mailcreated']),
497 '$seen' => $rr['mailseen'],
498 '$count' => tt('%d message', '%d messages', $rr['count']),