3 require_once('include/acl_selectors.php');
4 require_once('include/message.php');
6 function message_init(&$a) {
10 if ($a->argc >1 && is_numeric($a->argv[1])) {
11 $tabs = render_messages(get_messages(local_user(),0,5), 'mail_list.tpl');
15 'label' => t('New Message'),
16 'url' => $a->get_baseurl(true) . '/message/new',
17 'sel'=> ($a->argv[1] == 'new'),
21 $tpl = get_markup_template('message_side.tpl');
22 $a->page['aside'] = replace_macros($tpl, array(
26 $base = $a->get_baseurl();
28 $head_tpl = get_markup_template('message-head.tpl');
29 $a->page['htmlhead'] .= replace_macros($head_tpl,array(
30 '$baseurl' => $a->get_baseurl(true),
34 $end_tpl = get_markup_template('message-end.tpl');
35 $a->page['end'] .= replace_macros($end_tpl,array(
36 '$baseurl' => $a->get_baseurl(true),
42 function message_post(&$a) {
45 notice( t('Permission denied.') . EOL);
49 $replyto = ((x($_REQUEST,'replyto')) ? notags(trim($_REQUEST['replyto'])) : '');
50 $subject = ((x($_REQUEST,'subject')) ? notags(trim($_REQUEST['subject'])) : '');
51 $body = ((x($_REQUEST,'body')) ? escape_tags(trim($_REQUEST['body'])) : '');
52 $recipient = ((x($_REQUEST,'messageto')) ? intval($_REQUEST['messageto']) : 0 );
54 // Work around doubled linefeeds in Tinymce 3.5b2
56 /* $plaintext = intval(get_pconfig(local_user(),'system','plaintext') && !feature_enabled(local_user(),'richtext'));
58 $body = fix_mce_lf($body);
60 $plaintext = intval(!feature_enabled(local_user(),'richtext'));
62 $body = fix_mce_lf($body);
65 $ret = send_message($recipient, $body, $subject, $replyto);
70 notice( t('No recipient selected.') . EOL );
74 notice( t('Unable to locate contact information.') . EOL );
77 notice( t('Message could not be sent.') . EOL );
80 notice( t('Message collection failure.') . EOL );
83 info( t('Message sent.') . EOL );
86 // fake it to go back to the input form if no recipient listed
93 goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
97 // Note: the code in 'item_extract_images' and 'item_redir_and_replace_images'
98 // is identical to the code in include/conversation.php
99 if(! function_exists('item_extract_images')) {
100 function item_extract_images($body) {
102 $saved_image = array();
107 $img_start = strpos($orig_body, '[img');
108 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
109 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
110 while(($img_st_close !== false) && ($img_end !== false)) {
112 $img_st_close++; // make it point to AFTER the closing bracket
113 $img_end += $img_start;
115 if(! strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
116 // This is an embedded image
118 $saved_image[$cnt] = substr($orig_body, $img_start + $img_st_close, $img_end - ($img_start + $img_st_close));
119 $new_body = $new_body . substr($orig_body, 0, $img_start) . '[!#saved_image' . $cnt . '#!]';
124 $new_body = $new_body . substr($orig_body, 0, $img_end + strlen('[/img]'));
126 $orig_body = substr($orig_body, $img_end + strlen('[/img]'));
128 if($orig_body === false) // in case the body ends on a closing image tag
131 $img_start = strpos($orig_body, '[img');
132 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
133 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
136 $new_body = $new_body . $orig_body;
138 return array('body' => $new_body, 'images' => $saved_image);
141 if(! function_exists('item_redir_and_replace_images')) {
142 function item_redir_and_replace_images($body, $images, $cid) {
147 for($i = 0; $i < count($images); $i++) {
148 $search = '/\[url\=(.*?)\]\[!#saved_image' . $i . '#!\]\[\/url\]' . '/is';
149 $replace = '[url=' . z_path() . '/redir/' . $cid
150 . '?f=1&url=' . '$1' . '][!#saved_image' . $i . '#!][/url]' ;
152 $img_end = strpos($origbody, '[!#saved_image' . $i . '#!][/url]') + strlen('[!#saved_image' . $i . '#!][/url]');
153 $process_part = substr($origbody, 0, $img_end);
154 $origbody = substr($origbody, $img_end);
156 $process_part = preg_replace($search, $replace, $process_part);
157 $newbody = $newbody . $process_part;
159 $newbody = $newbody . $origbody;
162 foreach($images as $image) {
163 // We're depending on the property of 'foreach' (specified on the PHP website) that
164 // it loops over the array starting from the first element and going sequentially
165 // to the last element
166 $newbody = str_replace('[!#saved_image' . $cnt . '#!]', '[img]' . $image . '[/img]', $newbody);
175 function message_content(&$a) {
178 nav_set_selected('messages');
181 notice( t('Permission denied.') . EOL);
185 $myprofile = $a->get_baseurl(true) . '/profile/' . $a->user['nickname'];
187 $tpl = get_markup_template('mail_head.tpl');
188 $header = replace_macros($tpl, array(
189 '$messages' => t('Messages'),
190 '$tab_content' => $tab_content
194 if(($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) {
195 if(! intval($a->argv[2]))
198 // Check if we should do HTML-based delete confirmation
199 if($_REQUEST['confirm']) {
200 // <form> can't take arguments in its "action" parameter
201 // so add any arguments as hidden inputs
202 $query = explode_querystring($a->query_string);
204 foreach($query['args'] as $arg) {
205 if(strpos($arg, 'confirm=') === false) {
206 $arg_parts = explode('=', $arg);
207 $inputs[] = array('name' => $arg_parts[0], 'value' => $arg_parts[1]);
211 //$a->page['aside'] = '';
212 return replace_macros(get_markup_template('confirm.tpl'), array(
214 '$message' => t('Do you really want to delete this message?'),
215 '$extra_inputs' => $inputs,
216 '$confirm' => t('Yes'),
217 '$confirm_url' => $query['base'],
218 '$confirm_name' => 'confirmed',
219 '$cancel' => t('Cancel'),
222 // Now check how the user responded to the confirmation query
223 if($_REQUEST['canceled']) {
224 goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
228 if($cmd === 'drop') {
229 $r = q("DELETE FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
234 info( t('Message deleted.') . EOL );
236 //goaway($a->get_baseurl(true) . '/message' );
237 goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
240 $r = q("SELECT `parent-uri`,`convid` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
245 $parent = $r[0]['parent-uri'];
246 $convid = $r[0]['convid'];
248 $r = q("DELETE FROM `mail` WHERE `parent-uri` = '%s' AND `uid` = %d ",
253 // remove diaspora conversation pointer
254 // Actually if we do this, we can never receive another reply to that conversation,
255 // as we will never again have the info we need to re-create it.
256 // We'll just have to orphan it.
259 // q("delete from conv where id = %d limit 1",
265 info( t('Conversation removed.') . EOL );
267 //goaway($a->get_baseurl(true) . '/message' );
268 goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
273 if(($a->argc > 1) && ($a->argv[1] === 'new')) {
277 /* $plaintext = false;
278 if(intval(get_pconfig(local_user(),'system','plaintext')))
281 if( local_user() && feature_enabled(local_user(),'richtext') )
285 $tpl = get_markup_template('msg-header.tpl');
286 $a->page['htmlhead'] .= replace_macros($tpl, array(
287 '$baseurl' => $a->get_baseurl(true),
288 '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
289 '$nickname' => $a->user['nickname'],
290 '$linkurl' => t('Please enter a link URL:')
293 $tpl = get_markup_template('msg-end.tpl');
294 $a->page['end'] .= replace_macros($tpl, array(
295 '$baseurl' => $a->get_baseurl(true),
296 '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
297 '$nickname' => $a->user['nickname'],
298 '$linkurl' => t('Please enter a link URL:')
301 $preselect = (isset($a->argv[2])?array($a->argv[2]):false);
304 $prename = $preurl = $preid = '';
307 $r = q("select name, url, id from contact where uid = %d and id = %d limit 1",
308 intval(local_user()),
312 $prename = $r[0]['name'];
313 $preurl = $r[0]['url'];
314 $preid = $r[0]['id'];
318 $prefill = (($preselect) ? $prename : '');
320 // the ugly select box
322 $select = contact_select('messageto','message-to-select', $preselect, 4, true, false, false, 10);
324 $tpl = get_markup_template('prv_message.tpl');
325 $o .= replace_macros($tpl,array(
326 '$header' => t('Send Private Message'),
328 '$showinputs' => 'true',
329 '$prefill' => $prefill,
330 '$autocomp' => $autocomp,
332 '$subject' => t('Subject:'),
333 '$subjtxt' => ((x($_REQUEST,'subject')) ? strip_tags($_REQUEST['subject']) : ''),
334 '$text' => ((x($_REQUEST,'body')) ? escape_tags(htmlspecialchars($_REQUEST['body'])) : ''),
336 '$yourmessage' => t('Your message:'),
337 '$select' => $select,
339 '$upload' => t('Upload photo'),
340 '$insert' => t('Insert web link'),
341 '$wait' => t('Please wait'),
342 '$submit' => t('Submit')
349 $_SESSION['return_url'] = $a->query_string;
357 $r = q("SELECT count(*) AS `total` FROM `mail`
358 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
359 intval(local_user()),
363 if(count($r)) $a->set_pager_total($r[0]['total']);
365 $r = get_messages(local_user(), $a->pager['start'], $a->pager['itemspage']);
368 info( t('No messages.') . EOL);
372 $o .= render_messages($r, 'mail_list.tpl');
379 if(($a->argc > 1) && (intval($a->argv[1]))) {
384 if( local_user() && feature_enabled(local_user(),'richtext') )
387 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
388 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
389 WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
390 intval(local_user()),
394 $contact_id = $r[0]['contact-id'];
395 $convid = $r[0]['convid'];
397 $sql_extra = sprintf(" and `mail`.`parent-uri` = '%s' ", dbesc($r[0]['parent-uri']));
399 $sql_extra = sprintf(" and ( `mail`.`parent-uri` = '%s' OR `mail`.`convid` = '%d' ) ",
400 dbesc($r[0]['parent-uri']),
404 $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
405 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
406 WHERE `mail`.`uid` = %d $sql_extra ORDER BY `mail`.`created` ASC",
410 if(! count($messages)) {
411 notice( t('Message not available.') . EOL );
415 $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
416 dbesc($r[0]['parent-uri']),
420 require_once("include/bbcode.php");
422 $tpl = get_markup_template('msg-header.tpl');
423 $a->page['htmlhead'] .= replace_macros($tpl, array(
424 '$baseurl' => $a->get_baseurl(true),
425 '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
426 '$nickname' => $a->user['nickname'],
427 '$linkurl' => t('Please enter a link URL:')
430 $tpl = get_markup_template('msg-end.tpl');
431 $a->page['end'] .= replace_macros($tpl, array(
432 '$baseurl' => $a->get_baseurl(true),
433 '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
434 '$nickname' => $a->user['nickname'],
435 '$linkurl' => t('Please enter a link URL:')
443 foreach($messages as $message) {
444 if($message['unknown'])
446 if($message['from-url'] == $myprofile) {
447 $from_url = $myprofile;
451 $from_url = $a->get_baseurl(true) . '/redir/' . $message['contact-id'];
452 $sparkle = ' sparkle';
456 $extracted = item_extract_images($message['body']);
457 if($extracted['images'])
458 $message['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $message['contact-id']);
460 if($a->theme['template_engine'] === 'internal') {
461 $from_name_e = template_escape($message['from-name']);
462 $subject_e = template_escape($message['title']);
463 $body_e = template_escape(smilies(bbcode($message['body'])));
464 $to_name_e = template_escape($message['name']);
467 $from_name_e = $message['from-name'];
468 $subject_e = $message['title'];
469 $body_e = smilies(bbcode($message['body']));
470 $to_name_e = $message['name'];
474 'id' => $message['id'],
475 'from_name' => $from_name_e,
476 'from_url' => $from_url,
477 'sparkle' => $sparkle,
478 'from_photo' => $message['from-photo'],
479 'subject' => $subject_e,
481 'delete' => t('Delete message'),
482 'to_name' => $to_name_e,
483 'date' => datetime_convert('UTC',date_default_timezone_get(),$message['created'],'D, d M Y - g:i A'),
484 'ago' => relative_date($message['created']),
487 $seen = $message['seen'];
491 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
492 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
494 $tpl = get_markup_template('mail_display.tpl');
496 if($a->theme['template_engine'] === 'internal') {
497 $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($msg, $t) {
549 $tpl = get_markup_template($t);
552 foreach($msg as $rr) {
555 $participants = sprintf( t("Unknown sender - %s"),$rr['from-name']);
557 elseif (link_compare($rr['from-url'], $myprofile)){
558 $participants = sprintf( t("You and %s"), $rr['name']);
561 $participants = sprintf( t("%s and You"), $rr['from-name']);
564 if($a->theme['template_engine'] === 'internal') {
565 $subject_e = template_escape((($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>'));
566 $body_e = template_escape($rr['body']);
567 $to_name_e = template_escape($rr['name']);
570 $subject_e = (($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>');
571 $body_e = $rr['body'];
572 $to_name_e = $rr['name'];
575 $rslt .= replace_macros($tpl, array(
577 '$from_name' => $participants,
578 '$from_url' => (($rr['network'] === NETWORK_DFRN) ? $a->get_baseurl(true) . '/redir/' . $rr['contact-id'] : $rr['url']),
579 '$sparkle' => ' sparkle',
580 '$from_photo' => (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']),
581 '$subject' => $subject_e,
582 '$delete' => t('Delete conversation'),
584 '$to_name' => $to_name_e,
585 '$date' => datetime_convert('UTC',date_default_timezone_get(),$rr['mailcreated'], t('D, d M Y - g:i A')),
586 '$ago' => relative_date($rr['mailcreated']),
587 '$seen' => $rr['mailseen'],
588 '$count' => sprintf( tt('%d message', '%d messages', $rr['count']), $rr['count']),