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->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),
49 function message_post(App $a) {
52 notice( t('Permission denied.') . EOL);
56 $replyto = ((x($_REQUEST,'replyto')) ? notags(trim($_REQUEST['replyto'])) : '');
57 $subject = ((x($_REQUEST,'subject')) ? notags(trim($_REQUEST['subject'])) : '');
58 $body = ((x($_REQUEST,'body')) ? escape_tags(trim($_REQUEST['body'])) : '');
59 $recipient = ((x($_REQUEST,'messageto')) ? intval($_REQUEST['messageto']) : 0 );
61 $ret = send_message($recipient, $body, $subject, $replyto);
66 notice( t('No recipient selected.') . EOL );
70 notice( t('Unable to locate contact information.') . EOL );
73 notice( t('Message could not be sent.') . EOL );
76 notice( t('Message collection failure.') . EOL );
79 info( t('Message sent.') . EOL );
82 // 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, array(
106 '$messages' => t('Messages'),
107 '$tab_content' => $tab_content
111 if (($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) {
112 if (! intval($a->argv[2]))
115 // Check if we should do HTML-based delete confirmation
116 if ($_REQUEST['confirm']) {
117 // <form> can't take arguments in its "action" parameter
118 // so add any arguments as hidden inputs
119 $query = explode_querystring($a->query_string);
121 foreach($query['args'] as $arg) {
122 if (strpos($arg, 'confirm=') === false) {
123 $arg_parts = explode('=', $arg);
124 $inputs[] = array('name' => $arg_parts[0], 'value' => $arg_parts[1]);
128 //$a->page['aside'] = '';
129 return replace_macros(get_markup_template('confirm.tpl'), array(
131 '$message' => t('Do you really want to delete this message?'),
132 '$extra_inputs' => $inputs,
133 '$confirm' => t('Yes'),
134 '$confirm_url' => $query['base'],
135 '$confirm_name' => 'confirmed',
136 '$cancel' => t('Cancel'),
139 // Now check how the user responded to the confirmation query
140 if ($_REQUEST['canceled']) {
141 goaway($_SESSION['return_url']);
145 if ($cmd === 'drop') {
146 $r = q("DELETE FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
151 info( t('Message deleted.') . EOL );
153 //goaway(System::baseUrl(true) . '/message' );
154 goaway($_SESSION['return_url']);
156 $r = q("SELECT `parent-uri`,`convid` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
160 if (DBM::is_result($r)) {
161 $parent = $r[0]['parent-uri'];
162 $convid = $r[0]['convid'];
164 $r = q("DELETE FROM `mail` WHERE `parent-uri` = '%s' AND `uid` = %d ",
169 // remove diaspora conversation pointer
170 // Actually if we do this, we can never receive another reply to that conversation,
171 // as we will never again have the info we need to re-create it.
172 // We'll just have to orphan it.
175 // q("delete from conv where id = %d limit 1",
181 info( t('Conversation removed.') . EOL );
183 //goaway(System::baseUrl(true) . '/message' );
184 goaway($_SESSION['return_url']);
189 if (($a->argc > 1) && ($a->argv[1] === 'new')) {
193 $tpl = get_markup_template('msg-header.tpl');
194 $a->page['htmlhead'] .= replace_macros($tpl, array(
195 '$baseurl' => System::baseUrl(true),
196 '$nickname' => $a->user['nickname'],
197 '$linkurl' => t('Please enter a link URL:')
200 $tpl = get_markup_template('msg-end.tpl');
201 $a->page['end'] .= replace_macros($tpl, array(
202 '$baseurl' => System::baseUrl(true),
203 '$nickname' => $a->user['nickname'],
204 '$linkurl' => t('Please enter a link URL:')
207 $preselect = (isset($a->argv[2])?array($a->argv[2]):false);
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 (!DBM::is_result($r)) {
218 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' LIMIT 1",
219 intval(local_user()),
220 dbesc(normalise_link(base64_decode($a->argv[2])))
224 if (!DBM::is_result($r)) {
225 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `addr` = '%s' LIMIT 1",
226 intval(local_user()),
227 dbesc(base64_decode($a->argv[2]))
231 if (DBM::is_result($r)) {
232 $prename = $r[0]['name'];
233 $preurl = $r[0]['url'];
234 $preid = $r[0]['id'];
235 $preselect = array($preid);
240 $prefill = (($preselect) ? $prename : '');
242 // the ugly select box
244 $select = contact_select('messageto','message-to-select', $preselect, 4, true, false, false, 10);
246 $tpl = get_markup_template('prv_message.tpl');
247 $o .= replace_macros($tpl,array(
248 '$header' => t('Send Private Message'),
250 '$showinputs' => 'true',
251 '$prefill' => $prefill,
252 '$autocomp' => $autocomp,
254 '$subject' => t('Subject:'),
255 '$subjtxt' => ((x($_REQUEST,'subject')) ? strip_tags($_REQUEST['subject']) : ''),
256 '$text' => ((x($_REQUEST,'body')) ? escape_tags(htmlspecialchars($_REQUEST['body'])) : ''),
258 '$yourmessage' => t('Your message:'),
259 '$select' => $select,
261 '$upload' => t('Upload photo'),
262 '$insert' => t('Insert web link'),
263 '$wait' => t('Please wait'),
264 '$submit' => t('Submit')
270 $_SESSION['return_url'] = $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",
283 if (DBM::is_result($r)) {
284 $a->set_pager_total($r[0]['total']);
287 $r = get_messages(local_user(), $a->pager['start'], $a->pager['itemspage']);
289 if (! DBM::is_result($r)) {
290 info( t('No messages.') . EOL);
294 $o .= render_messages($r, 'mail_list.tpl');
301 if (($a->argc > 1) && (intval($a->argv[1]))) {
305 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
306 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
307 WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
308 intval(local_user()),
311 if (DBM::is_result($r)) {
312 $contact_id = $r[0]['contact-id'];
313 $convid = $r[0]['convid'];
315 $sql_extra = sprintf(" and `mail`.`parent-uri` = '%s' ", dbesc($r[0]['parent-uri']));
317 $sql_extra = sprintf(" and ( `mail`.`parent-uri` = '%s' OR `mail`.`convid` = '%d' ) ",
318 dbesc($r[0]['parent-uri']),
322 $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
323 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
324 WHERE `mail`.`uid` = %d $sql_extra ORDER BY `mail`.`created` ASC",
328 if (! count($messages)) {
329 notice( t('Message not available.') . EOL );
333 $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
334 dbesc($r[0]['parent-uri']),
338 require_once("include/bbcode.php");
340 $tpl = get_markup_template('msg-header.tpl');
341 $a->page['htmlhead'] .= replace_macros($tpl, array(
342 '$baseurl' => System::baseUrl(true),
343 '$nickname' => $a->user['nickname'],
344 '$linkurl' => t('Please enter a link URL:')
347 $tpl = get_markup_template('msg-end.tpl');
348 $a->page['end'] .= replace_macros($tpl, array(
349 '$baseurl' => System::baseUrl(true),
350 '$nickname' => $a->user['nickname'],
351 '$linkurl' => t('Please enter a link URL:')
358 foreach($messages as $message) {
359 if ($message['unknown'])
361 if ($message['from-url'] == $myprofile) {
362 $from_url = $myprofile;
364 } elseif ($message['contact-id'] != 0) {
365 $from_url = 'redir/'.$message['contact-id'];
366 $sparkle = ' sparkle';
368 $from_url = $message['from-url']."?zrl=".urlencode($myprofile);
369 $sparkle = ' sparkle';
373 $extracted = item_extract_images($message['body']);
374 if ($extracted['images'])
375 $message['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $message['contact-id']);
377 $from_name_e = $message['from-name'];
378 $subject_e = $message['title'];
379 $body_e = Smilies::replace(bbcode($message['body']));
380 $to_name_e = $message['name'];
382 $contact = Contact::getDetailsByURL($message['from-url']);
383 if (isset($contact["thumb"]))
384 $from_photo = $contact["thumb"];
386 $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' => t('Delete message'),
398 'to_name' => $to_name_e,
399 'date' => datetime_convert('UTC',date_default_timezone_get(),$message['created'],'D, d M Y - g:i A'),
400 'ago' => relative_date($message['created']),
403 $seen = $message['seen'];
407 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
408 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
410 $tpl = get_markup_template('mail_display.tpl');
412 $subjtxt_e = $message['title'];
414 $o = replace_macros($tpl, array(
415 '$thread_id' => $a->argv[1],
416 '$thread_subject' => $message['title'],
417 '$thread_seen' => $seen,
418 '$delete' => t('Delete conversation'),
419 '$canreply' => (($unknown) ? false : '1'),
420 '$unknown_text' => t("No secure communications available. You <strong>may</strong> be able to respond from the sender's profile page."),
424 '$header' => t('Send Reply'),
427 '$subject' => t('Subject:'),
428 '$subjtxt' => $subjtxt_e,
429 '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
430 '$yourmessage' => t('Your message:'),
432 '$select' => $select,
433 '$parent' => $parent,
434 '$upload' => t('Upload photo'),
435 '$insert' => t('Insert web link'),
436 '$submit' => t('Submit'),
437 '$wait' => t('Please wait')
445 function get_messages($user, $lstart, $lend) {
446 //TODO: rewritte with a sub-query to get the first message of each private thread with certainty
447 return q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`,
448 ANY_VALUE(`mail`.`id`) AS `id`, ANY_VALUE(`mail`.`uid`) AS `uid`, ANY_VALUE(`mail`.`guid`) AS `guid`,
449 ANY_VALUE(`mail`.`from-name`) AS `from-name`, ANY_VALUE(`mail`.`from-photo`) AS `from-photo`,
450 ANY_VALUE(`mail`.`from-url`) AS `from-url`, ANY_VALUE(`mail`.`contact-id`) AS `contact-id`,
451 ANY_VALUE(`mail`.`convid`) AS `convid`, ANY_VALUE(`mail`.`title`) AS `title`, ANY_VALUE(`mail`.`body`) AS `body`,
452 ANY_VALUE(`mail`.`seen`) AS `seen`, ANY_VALUE(`mail`.`reply`) AS `reply`, ANY_VALUE(`mail`.`replied`) AS `replied`,
453 ANY_VALUE(`mail`.`unknown`) AS `unknown`, ANY_VALUE(`mail`.`uri`) AS `uri`,
455 ANY_VALUE(`mail`.`created`) AS `created`, ANY_VALUE(`contact`.`name`) AS `name`, ANY_VALUE(`contact`.`url`) AS `url`,
456 ANY_VALUE(`contact`.`thumb`) AS `thumb`, ANY_VALUE(`contact`.`network`) AS `network`,
457 count( * ) as `count`
458 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
459 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `mailcreated` DESC LIMIT %d , %d ",
460 intval($user), intval($lstart), intval($lend)
464 function render_messages(array $msg, $t) {
468 $tpl = get_markup_template($t);
471 $myprofile = System::baseUrl().'/profile/' . $a->user['nickname'];
473 foreach($msg as $rr) {
476 $participants = sprintf( t("Unknown sender - %s"),$rr['from-name']);
477 elseif (link_compare($rr['from-url'], $myprofile))
478 $participants = sprintf( t("You and %s"), $rr['name']);
480 $participants = sprintf(t("%s and You"), $rr['from-name']);
482 $subject_e = (($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>');
483 $body_e = $rr['body'];
484 $to_name_e = $rr['name'];
486 $contact = Contact::getDetailsByURL($rr['url']);
487 if (isset($contact["thumb"]))
488 $from_photo = $contact["thumb"];
490 $from_photo = (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']);
492 $rslt .= replace_macros($tpl, array(
494 '$from_name' => $participants,
495 '$from_url' => (($rr['network'] === NETWORK_DFRN) ? 'redir/' . $rr['contact-id'] : $rr['url']),
496 '$from_addr' => $contact['addr'],
497 '$sparkle' => ' sparkle',
498 '$from_photo' => proxy_url($from_photo, false, PROXY_SIZE_THUMB),
499 '$subject' => $subject_e,
500 '$delete' => t('Delete conversation'),
502 '$to_name' => $to_name_e,
503 '$date' => datetime_convert('UTC',date_default_timezone_get(),$rr['mailcreated'], t('D, d M Y - g:i A')),
504 '$ago' => relative_date($rr['mailcreated']),
505 '$seen' => $rr['mailseen'],
506 '$count' => sprintf( tt('%d message', '%d messages', $rr['count']), $rr['count']),