3 require_once('include/acl_selectors.php');
4 require_once('include/message.php');
5 require_once('include/Smilies.php');
7 function message_init(App $a) {
11 if ($a->argc >1 && is_numeric($a->argv[1])) {
12 $tabs = render_messages(get_messages(local_user(),0,5), 'mail_list.tpl');
16 'label' => t('New Message'),
17 'url' => 'message/new',
18 'sel'=> ($a->argv[1] == 'new'),
22 $tpl = get_markup_template('message_side.tpl');
23 $a->page['aside'] = replace_macros($tpl, array(
27 $base = App::get_baseurl();
29 $head_tpl = get_markup_template('message-head.tpl');
30 $a->page['htmlhead'] .= replace_macros($head_tpl,array(
31 '$baseurl' => App::get_baseurl(true),
35 $end_tpl = get_markup_template('message-end.tpl');
36 $a->page['end'] .= replace_macros($end_tpl,array(
37 '$baseurl' => App::get_baseurl(true),
43 function message_post(App $a) {
46 notice( t('Permission denied.') . EOL);
50 $replyto = ((x($_REQUEST,'replyto')) ? notags(trim($_REQUEST['replyto'])) : '');
51 $subject = ((x($_REQUEST,'subject')) ? notags(trim($_REQUEST['subject'])) : '');
52 $body = ((x($_REQUEST,'body')) ? escape_tags(trim($_REQUEST['body'])) : '');
53 $recipient = ((x($_REQUEST,'messageto')) ? intval($_REQUEST['messageto']) : 0 );
55 $ret = send_message($recipient, $body, $subject, $replyto);
60 notice( t('No recipient selected.') . EOL );
64 notice( t('Unable to locate contact information.') . EOL );
67 notice( t('Message could not be sent.') . EOL );
70 notice( t('Message collection failure.') . EOL );
73 info( t('Message sent.') . EOL );
76 // fake it to go back to the input form if no recipient listed
82 goaway($_SESSION['return_url']);
87 // Note: the code in 'item_extract_images' and 'item_redir_and_replace_images'
88 // is identical to the code in include/conversation.php
89 if (! function_exists('item_extract_images')) {
90 function item_extract_images($body) {
92 $saved_image = array();
97 $img_start = strpos($orig_body, '[img');
98 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
99 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
100 while (($img_st_close !== false) && ($img_end !== false)) {
102 $img_st_close++; // make it point to AFTER the closing bracket
103 $img_end += $img_start;
105 if (! strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
106 // This is an embedded image
108 $saved_image[$cnt] = substr($orig_body, $img_start + $img_st_close, $img_end - ($img_start + $img_st_close));
109 $new_body = $new_body . substr($orig_body, 0, $img_start) . '[!#saved_image' . $cnt . '#!]';
113 $new_body = $new_body . substr($orig_body, 0, $img_end + strlen('[/img]'));
116 $orig_body = substr($orig_body, $img_end + strlen('[/img]'));
118 if ($orig_body === false) {// in case the body ends on a closing image tag
122 $img_start = strpos($orig_body, '[img');
123 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
124 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
127 $new_body = $new_body . $orig_body;
129 return array('body' => $new_body, 'images' => $saved_image);
132 if (! function_exists('item_redir_and_replace_images')) {
133 function item_redir_and_replace_images($body, $images, $cid) {
138 for ($i = 0; $i < count($images); $i++) {
139 $search = '/\[url\=(.*?)\]\[!#saved_image' . $i . '#!\]\[\/url\]' . '/is';
140 $replace = '[url=' . z_path() . '/redir/' . $cid
141 . '?f=1&url=' . '$1' . '][!#saved_image' . $i . '#!][/url]' ;
143 $img_end = strpos($origbody, '[!#saved_image' . $i . '#!][/url]') + strlen('[!#saved_image' . $i . '#!][/url]');
144 $process_part = substr($origbody, 0, $img_end);
145 $origbody = substr($origbody, $img_end);
147 $process_part = preg_replace($search, $replace, $process_part);
148 $newbody = $newbody . $process_part;
150 $newbody = $newbody . $origbody;
153 foreach ($images as $image) {
154 // We're depending on the property of 'foreach' (specified on the PHP website) that
155 // it loops over the array starting from the first element and going sequentially
156 // to the last element
157 $newbody = str_replace('[!#saved_image' . $cnt . '#!]', '[img]' . $image . '[/img]', $newbody);
166 function message_content(App $a) {
169 nav_set_selected('messages');
171 if (! local_user()) {
172 notice( t('Permission denied.') . EOL);
176 $myprofile = App::get_baseurl().'/profile/' . $a->user['nickname'];
178 $tpl = get_markup_template('mail_head.tpl');
179 $header = replace_macros($tpl, array(
180 '$messages' => t('Messages'),
181 '$tab_content' => $tab_content
185 if (($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) {
186 if (! intval($a->argv[2]))
189 // Check if we should do HTML-based delete confirmation
190 if ($_REQUEST['confirm']) {
191 // <form> can't take arguments in its "action" parameter
192 // so add any arguments as hidden inputs
193 $query = explode_querystring($a->query_string);
195 foreach ($query['args'] as $arg) {
196 if (strpos($arg, 'confirm=') === false) {
197 $arg_parts = explode('=', $arg);
198 $inputs[] = array('name' => $arg_parts[0], 'value' => $arg_parts[1]);
202 //$a->page['aside'] = '';
203 return replace_macros(get_markup_template('confirm.tpl'), array(
205 '$message' => t('Do you really want to delete this message?'),
206 '$extra_inputs' => $inputs,
207 '$confirm' => t('Yes'),
208 '$confirm_url' => $query['base'],
209 '$confirm_name' => 'confirmed',
210 '$cancel' => t('Cancel'),
213 // Now check how the user responded to the confirmation query
214 if ($_REQUEST['canceled']) {
215 goaway($_SESSION['return_url']);
219 if ($cmd === 'drop') {
220 $r = q("DELETE FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
225 info( t('Message deleted.') . EOL );
227 //goaway(App::get_baseurl(true) . '/message' );
228 goaway($_SESSION['return_url']);
231 $r = q("SELECT `parent-uri`,`convid` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
235 if (dbm::is_result($r)) {
236 $parent = $r[0]['parent-uri'];
237 $convid = $r[0]['convid'];
239 $r = q("DELETE FROM `mail` WHERE `parent-uri` = '%s' AND `uid` = %d ",
244 // remove diaspora conversation pointer
245 // Actually if we do this, we can never receive another reply to that conversation,
246 // as we will never again have the info we need to re-create it.
247 // We'll just have to orphan it.
250 // q("delete from conv where id = %d limit 1",
256 info( t('Conversation removed.') . EOL );
259 //goaway(App::get_baseurl(true) . '/message' );
260 goaway($_SESSION['return_url']);
265 if (($a->argc > 1) && ($a->argv[1] === 'new')) {
269 $tpl = get_markup_template('msg-header.tpl');
270 $a->page['htmlhead'] .= replace_macros($tpl, array(
271 '$baseurl' => App::get_baseurl(true),
272 '$nickname' => $a->user['nickname'],
273 '$linkurl' => t('Please enter a link URL:')
276 $tpl = get_markup_template('msg-end.tpl');
277 $a->page['end'] .= replace_macros($tpl, array(
278 '$baseurl' => App::get_baseurl(true),
279 '$nickname' => $a->user['nickname'],
280 '$linkurl' => t('Please enter a link URL:')
283 $preselect = (isset($a->argv[2])?array($a->argv[2]):false);
286 $prename = $preurl = $preid = '';
289 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `id` = %d LIMIT 1",
290 intval(local_user()),
293 if (!dbm::is_result($r)) {
294 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' LIMIT 1",
295 intval(local_user()),
296 dbesc(normalise_link(base64_decode($a->argv[2])))
300 if (!dbm::is_result($r)) {
301 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `addr` = '%s' LIMIT 1",
302 intval(local_user()),
303 dbesc(base64_decode($a->argv[2]))
307 if (dbm::is_result($r)) {
308 $prename = $r[0]['name'];
309 $preurl = $r[0]['url'];
310 $preid = $r[0]['id'];
311 $preselect = array($preid);
316 $prefill = (($preselect) ? $prename : '');
318 // the ugly select box
320 $select = contact_select('messageto','message-to-select', $preselect, 4, true, false, false, 10);
322 $tpl = get_markup_template('prv_message.tpl');
323 $o .= replace_macros($tpl,array(
324 '$header' => t('Send Private Message'),
326 '$showinputs' => 'true',
327 '$prefill' => $prefill,
328 '$autocomp' => $autocomp,
330 '$subject' => t('Subject:'),
331 '$subjtxt' => ((x($_REQUEST,'subject')) ? strip_tags($_REQUEST['subject']) : ''),
332 '$text' => ((x($_REQUEST,'body')) ? escape_tags(htmlspecialchars($_REQUEST['body'])) : ''),
334 '$yourmessage' => t('Your message:'),
335 '$select' => $select,
337 '$upload' => t('Upload photo'),
338 '$insert' => t('Insert web link'),
339 '$wait' => t('Please wait'),
340 '$submit' => t('Submit')
346 $_SESSION['return_url'] = $a->query_string;
354 $r = q("SELECT count(*) AS `total` FROM `mail`
355 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
359 if (dbm::is_result($r)) {
360 $a->set_pager_total($r[0]['total']);
363 $r = get_messages(local_user(), $a->pager['start'], $a->pager['itemspage']);
365 if (! dbm::is_result($r)) {
366 info( t('No messages.') . EOL);
370 $o .= render_messages($r, 'mail_list.tpl');
377 if (($a->argc > 1) && (intval($a->argv[1]))) {
381 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
382 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
383 WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
384 intval(local_user()),
387 if (dbm::is_result($r)) {
388 $contact_id = $r[0]['contact-id'];
389 $convid = $r[0]['convid'];
391 $sql_extra = sprintf(" and `mail`.`parent-uri` = '%s' ", dbesc($r[0]['parent-uri']));
393 $sql_extra = sprintf(" and ( `mail`.`parent-uri` = '%s' OR `mail`.`convid` = '%d' ) ",
394 dbesc($r[0]['parent-uri']),
398 $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
399 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
400 WHERE `mail`.`uid` = %d $sql_extra ORDER BY `mail`.`created` ASC",
404 if (! count($messages)) {
405 notice( t('Message not available.') . EOL );
409 $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
410 dbesc($r[0]['parent-uri']),
414 require_once("include/bbcode.php");
416 $tpl = get_markup_template('msg-header.tpl');
417 $a->page['htmlhead'] .= replace_macros($tpl, array(
418 '$baseurl' => App::get_baseurl(true),
419 '$nickname' => $a->user['nickname'],
420 '$linkurl' => t('Please enter a link URL:')
423 $tpl = get_markup_template('msg-end.tpl');
424 $a->page['end'] .= replace_macros($tpl, array(
425 '$baseurl' => App::get_baseurl(true),
426 '$nickname' => $a->user['nickname'],
427 '$linkurl' => t('Please enter a link URL:')
434 foreach ($messages as $message) {
435 if ($message['unknown']) {
438 if ($message['from-url'] == $myprofile) {
439 $from_url = $myprofile;
441 } elseif ($message['contact-id'] != 0) {
442 $from_url = 'redir/'.$message['contact-id'];
443 $sparkle = ' sparkle';
445 $from_url = $message['from-url']."?zrl=".urlencode($myprofile);
446 $sparkle = ' sparkle';
450 $extracted = item_extract_images($message['body']);
451 if ($extracted['images']) {
452 $message['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $message['contact-id']);
455 if ($a->theme['template_engine'] === 'internal') {
456 $from_name_e = template_escape($message['from-name']);
457 $subject_e = template_escape($message['title']);
458 $body_e = template_escape(Smilies::replace(bbcode($message['body'])));
459 $to_name_e = template_escape($message['name']);
461 $from_name_e = $message['from-name'];
462 $subject_e = $message['title'];
463 $body_e = Smilies::replace(bbcode($message['body']));
464 $to_name_e = $message['name'];
467 $contact = get_contact_details_by_url($message['from-url']);
468 if (isset($contact["thumb"])) {
469 $from_photo = $contact["thumb"];
471 $from_photo = $message['from-photo'];
475 'id' => $message['id'],
476 'from_name' => $from_name_e,
477 'from_url' => $from_url,
478 'sparkle' => $sparkle,
479 'from_photo' => proxy_url($from_photo, false, PROXY_SIZE_THUMB),
480 'subject' => $subject_e,
482 'delete' => t('Delete message'),
483 'to_name' => $to_name_e,
484 'date' => datetime_convert('UTC',date_default_timezone_get(),$message['created'],'D, d M Y - g:i A'),
485 'ago' => relative_date($message['created']),
488 $seen = $message['seen'];
492 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
493 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
495 $tpl = get_markup_template('mail_display.tpl');
497 if ($a->theme['template_engine'] === 'internal') {
498 $subjtxt_e = template_escape($message['title']);
500 $subjtxt_e = $message['title'];
503 $o = replace_macros($tpl, array(
504 '$thread_id' => $a->argv[1],
505 '$thread_subject' => $message['title'],
506 '$thread_seen' => $seen,
507 '$delete' => t('Delete conversation'),
508 '$canreply' => (($unknown) ? false : '1'),
509 '$unknown_text' => t("No secure communications available. You <strong>may</strong> be able to respond from the sender's profile page."),
513 '$header' => t('Send Reply'),
516 '$subject' => t('Subject:'),
517 '$subjtxt' => $subjtxt_e,
518 '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
519 '$yourmessage' => t('Your message:'),
521 '$select' => $select,
522 '$parent' => $parent,
523 '$upload' => t('Upload photo'),
524 '$insert' => t('Insert web link'),
525 '$submit' => t('Submit'),
526 '$wait' => t('Please wait')
534 function get_messages($user, $lstart, $lend) {
536 return q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`,
537 `mail`.* , `contact`.`name`, `contact`.`url`, `contact`.`thumb` , `contact`.`network`,
539 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
540 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `mailcreated` DESC LIMIT %d , %d ",
541 intval($user), intval($lstart), intval($lend)
545 function render_messages(array $msg, $t) {
549 $tpl = get_markup_template($t);
552 $myprofile = App::get_baseurl().'/profile/' . $a->user['nickname'];
554 foreach ($msg as $rr) {
557 $participants = sprintf( t("Unknown sender - %s"),$rr['from-name']);
558 elseif (link_compare($rr['from-url'], $myprofile))
559 $participants = sprintf( t("You and %s"), $rr['name']);
561 $participants = sprintf(t("%s and You"), $rr['from-name']);
563 if ($a->theme['template_engine'] === 'internal') {
564 $subject_e = template_escape((($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>'));
565 $body_e = template_escape($rr['body']);
566 $to_name_e = template_escape($rr['name']);
569 $subject_e = (($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>');
570 $body_e = $rr['body'];
571 $to_name_e = $rr['name'];
574 $contact = get_contact_details_by_url($rr['url']);
575 if (isset($contact["thumb"]))
576 $from_photo = $contact["thumb"];
578 $from_photo = (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']);
580 $rslt .= replace_macros($tpl, array(
582 '$from_name' => $participants,
583 '$from_url' => (($rr['network'] === NETWORK_DFRN) ? 'redir/' . $rr['contact-id'] : $rr['url']),
584 '$sparkle' => ' sparkle',
585 '$from_photo' => proxy_url($from_photo, false, PROXY_SIZE_THUMB),
586 '$subject' => $subject_e,
587 '$delete' => t('Delete conversation'),
589 '$to_name' => $to_name_e,
590 '$date' => datetime_convert('UTC',date_default_timezone_get(),$rr['mailcreated'], t('D, d M Y - g:i A')),
591 '$ago' => relative_date($rr['mailcreated']),
592 '$seen' => $rr['mailseen'],
593 '$count' => sprintf( tt('%d message', '%d messages', $rr['count']), $rr['count']),