3 require_once('include/acl_selectors.php');
4 require_once('include/message.php');
5 require_once('include/Smilies.php');
7 function message_init(&$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 = $a->get_baseurl();
29 $head_tpl = get_markup_template('message-head.tpl');
30 $a->page['htmlhead'] .= replace_macros($head_tpl,array(
31 '$baseurl' => $a->get_baseurl(true),
35 $end_tpl = get_markup_template('message-end.tpl');
36 $a->page['end'] .= replace_macros($end_tpl,array(
37 '$baseurl' => $a->get_baseurl(true),
43 function message_post(&$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 // Work around doubled linefeeds in Tinymce 3.5b2
57 /* $plaintext = intval(get_pconfig(local_user(),'system','plaintext') && !feature_enabled(local_user(),'richtext'));
59 $body = fix_mce_lf($body);
61 $plaintext = intval(!feature_enabled(local_user(),'richtext'));
63 $body = fix_mce_lf($body);
66 $ret = send_message($recipient, $body, $subject, $replyto);
71 notice( t('No recipient selected.') . EOL );
75 notice( t('Unable to locate contact information.') . EOL );
78 notice( t('Message could not be sent.') . EOL );
81 notice( t('Message collection failure.') . EOL );
84 info( t('Message sent.') . EOL );
87 // fake it to go back to the input form if no recipient listed
94 goaway($_SESSION['return_url']);
98 // Note: the code in 'item_extract_images' and 'item_redir_and_replace_images'
99 // is identical to the code in include/conversation.php
100 if(! function_exists('item_extract_images')) {
101 function item_extract_images($body) {
103 $saved_image = array();
108 $img_start = strpos($orig_body, '[img');
109 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
110 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
111 while(($img_st_close !== false) && ($img_end !== false)) {
113 $img_st_close++; // make it point to AFTER the closing bracket
114 $img_end += $img_start;
116 if(! strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
117 // This is an embedded image
119 $saved_image[$cnt] = substr($orig_body, $img_start + $img_st_close, $img_end - ($img_start + $img_st_close));
120 $new_body = $new_body . substr($orig_body, 0, $img_start) . '[!#saved_image' . $cnt . '#!]';
125 $new_body = $new_body . substr($orig_body, 0, $img_end + strlen('[/img]'));
127 $orig_body = substr($orig_body, $img_end + strlen('[/img]'));
129 if($orig_body === false) // in case the body ends on a closing image tag
132 $img_start = strpos($orig_body, '[img');
133 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
134 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
137 $new_body = $new_body . $orig_body;
139 return array('body' => $new_body, 'images' => $saved_image);
142 if(! function_exists('item_redir_and_replace_images')) {
143 function item_redir_and_replace_images($body, $images, $cid) {
148 for($i = 0; $i < count($images); $i++) {
149 $search = '/\[url\=(.*?)\]\[!#saved_image' . $i . '#!\]\[\/url\]' . '/is';
150 $replace = '[url=' . z_path() . '/redir/' . $cid
151 . '?f=1&url=' . '$1' . '][!#saved_image' . $i . '#!][/url]' ;
153 $img_end = strpos($origbody, '[!#saved_image' . $i . '#!][/url]') + strlen('[!#saved_image' . $i . '#!][/url]');
154 $process_part = substr($origbody, 0, $img_end);
155 $origbody = substr($origbody, $img_end);
157 $process_part = preg_replace($search, $replace, $process_part);
158 $newbody = $newbody . $process_part;
160 $newbody = $newbody . $origbody;
163 foreach($images as $image) {
164 // We're depending on the property of 'foreach' (specified on the PHP website) that
165 // it loops over the array starting from the first element and going sequentially
166 // to the last element
167 $newbody = str_replace('[!#saved_image' . $cnt . '#!]', '[img]' . $image . '[/img]', $newbody);
176 function message_content(&$a) {
179 nav_set_selected('messages');
182 notice( t('Permission denied.') . EOL);
186 $myprofile = $a->get_baseurl().'/profile/' . $a->user['nickname'];
188 $tpl = get_markup_template('mail_head.tpl');
189 $header = replace_macros($tpl, array(
190 '$messages' => t('Messages'),
191 '$tab_content' => $tab_content
195 if(($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) {
196 if(! intval($a->argv[2]))
199 // Check if we should do HTML-based delete confirmation
200 if($_REQUEST['confirm']) {
201 // <form> can't take arguments in its "action" parameter
202 // so add any arguments as hidden inputs
203 $query = explode_querystring($a->query_string);
205 foreach($query['args'] as $arg) {
206 if(strpos($arg, 'confirm=') === false) {
207 $arg_parts = explode('=', $arg);
208 $inputs[] = array('name' => $arg_parts[0], 'value' => $arg_parts[1]);
212 //$a->page['aside'] = '';
213 return replace_macros(get_markup_template('confirm.tpl'), array(
215 '$message' => t('Do you really want to delete this message?'),
216 '$extra_inputs' => $inputs,
217 '$confirm' => t('Yes'),
218 '$confirm_url' => $query['base'],
219 '$confirm_name' => 'confirmed',
220 '$cancel' => t('Cancel'),
223 // Now check how the user responded to the confirmation query
224 if($_REQUEST['canceled']) {
225 goaway($_SESSION['return_url']);
229 if($cmd === 'drop') {
230 $r = q("DELETE FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
235 info( t('Message deleted.') . EOL );
237 //goaway($a->get_baseurl(true) . '/message' );
238 goaway($_SESSION['return_url']);
241 $r = q("SELECT `parent-uri`,`convid` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
246 $parent = $r[0]['parent-uri'];
247 $convid = $r[0]['convid'];
249 $r = q("DELETE FROM `mail` WHERE `parent-uri` = '%s' AND `uid` = %d ",
254 // remove diaspora conversation pointer
255 // Actually if we do this, we can never receive another reply to that conversation,
256 // as we will never again have the info we need to re-create it.
257 // We'll just have to orphan it.
260 // q("delete from conv where id = %d limit 1",
266 info( t('Conversation removed.') . EOL );
268 //goaway($a->get_baseurl(true) . '/message' );
269 goaway($_SESSION['return_url']);
274 if(($a->argc > 1) && ($a->argv[1] === 'new')) {
278 /* $plaintext = false;
279 if(intval(get_pconfig(local_user(),'system','plaintext')))
282 if( local_user() && feature_enabled(local_user(),'richtext') )
286 $tpl = get_markup_template('msg-header.tpl');
287 $a->page['htmlhead'] .= replace_macros($tpl, array(
288 '$baseurl' => $a->get_baseurl(true),
289 '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
290 '$nickname' => $a->user['nickname'],
291 '$linkurl' => t('Please enter a link URL:')
294 $tpl = get_markup_template('msg-end.tpl');
295 $a->page['end'] .= replace_macros($tpl, array(
296 '$baseurl' => $a->get_baseurl(true),
297 '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
298 '$nickname' => $a->user['nickname'],
299 '$linkurl' => t('Please enter a link URL:')
302 $preselect = (isset($a->argv[2])?array($a->argv[2]):false);
305 $prename = $preurl = $preid = '';
308 $r = q("select name, url, id from contact where uid = %d and id = %d limit 1",
309 intval(local_user()),
313 $prename = $r[0]['name'];
314 $preurl = $r[0]['url'];
315 $preid = $r[0]['id'];
319 $prefill = (($preselect) ? $prename : '');
321 // the ugly select box
323 $select = contact_select('messageto','message-to-select', $preselect, 4, true, false, false, 10);
325 $tpl = get_markup_template('prv_message.tpl');
326 $o .= replace_macros($tpl,array(
327 '$header' => t('Send Private Message'),
329 '$showinputs' => 'true',
330 '$prefill' => $prefill,
331 '$autocomp' => $autocomp,
333 '$subject' => t('Subject:'),
334 '$subjtxt' => ((x($_REQUEST,'subject')) ? strip_tags($_REQUEST['subject']) : ''),
335 '$text' => ((x($_REQUEST,'body')) ? escape_tags(htmlspecialchars($_REQUEST['body'])) : ''),
337 '$yourmessage' => t('Your message:'),
338 '$select' => $select,
340 '$upload' => t('Upload photo'),
341 '$insert' => t('Insert web link'),
342 '$wait' => t('Please wait'),
343 '$submit' => t('Submit')
350 $_SESSION['return_url'] = $a->query_string;
358 $r = q("SELECT count(*) AS `total` FROM `mail`
359 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
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;
449 } elseif ($message['contact-id'] != 0) {
450 $from_url = 'redir/'.$message['contact-id'];
451 $sparkle = ' sparkle';
453 $from_url = $message['from-url']."?zrl=".urlencode($myprofile);
454 $sparkle = ' sparkle';
458 $extracted = item_extract_images($message['body']);
459 if($extracted['images'])
460 $message['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $message['contact-id']);
462 if($a->theme['template_engine'] === 'internal') {
463 $from_name_e = template_escape($message['from-name']);
464 $subject_e = template_escape($message['title']);
465 $body_e = template_escape(Smilies::replace(bbcode($message['body'])));
466 $to_name_e = template_escape($message['name']);
468 $from_name_e = $message['from-name'];
469 $subject_e = $message['title'];
470 $body_e = Smilies::replace(bbcode($message['body']));
471 $to_name_e = $message['name'];
475 'id' => $message['id'],
476 'from_name' => $from_name_e,
477 'from_url' => $from_url,
478 'sparkle' => $sparkle,
479 'from_photo' => $message['from-photo'],
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']);
501 $subjtxt_e = $message['title'];
504 $o = replace_macros($tpl, array(
505 '$thread_id' => $a->argv[1],
506 '$thread_subject' => $message['title'],
507 '$thread_seen' => $seen,
508 '$delete' => t('Delete conversation'),
509 '$canreply' => (($unknown) ? false : '1'),
510 '$unknown_text' => t("No secure communications available. You <strong>may</strong> be able to respond from the sender's profile page."),
514 '$header' => t('Send Reply'),
517 '$subject' => t('Subject:'),
518 '$subjtxt' => $subjtxt_e,
519 '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
520 '$yourmessage' => t('Your message:'),
522 '$select' => $select,
523 '$parent' => $parent,
524 '$upload' => t('Upload photo'),
525 '$insert' => t('Insert web link'),
526 '$submit' => t('Submit'),
527 '$wait' => t('Please wait')
535 function get_messages($user, $lstart, $lend) {
537 return q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`,
538 `mail`.* , `contact`.`name`, `contact`.`url`, `contact`.`thumb` , `contact`.`network`,
540 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
541 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `mailcreated` DESC LIMIT %d , %d ",
542 intval($user), intval($lstart), intval($lend)
546 function render_messages($msg, $t) {
550 $tpl = get_markup_template($t);
553 $myprofile = $a->get_baseurl().'/profile/' . $a->user['nickname'];
555 foreach($msg as $rr) {
558 $participants = sprintf( t("Unknown sender - %s"),$rr['from-name']);
559 elseif (link_compare($rr['from-url'], $myprofile))
560 $participants = sprintf( t("You and %s"), $rr['name']);
562 $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) ? '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']),