3 require_once('include/acl_selectors.php');
4 require_once('include/message.php');
6 function message_init(&$a) {
9 'label' => t('New Message'),
10 'url' => $a->get_baseurl(true) . '/message/new',
11 'sel'=> ($a->argv[1] == 'new'),
14 $tpl = get_markup_template('message_side.tpl');
15 $a->page['aside'] = replace_macros($tpl, array(
19 $base = $a->get_baseurl();
21 $a->page['htmlhead'] .= '<script src="' . $a->get_baseurl(true) . '/library/jquery_ac/friendica.complete.js" ></script>';
22 $a->page['htmlhead'] .= <<< EOT
24 <script>$(document).ready(function() {
26 a = $("#recip").autocomplete({
27 serviceUrl: '$base/acl',
30 onSelect: function(value,data) {
31 $("#recip-complete").val(data);
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'));
58 $body = fix_mce_lf($body);
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
91 // Note: the code in 'item_extract_images' and 'item_redir_and_replace_images'
92 // is identical to the code in include/conversation.php
93 if(! function_exists('item_extract_images')) {
94 function item_extract_images($body) {
96 $saved_image = array();
101 $img_start = strpos($orig_body, '[img');
102 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
103 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
104 while(($img_st_close !== false) && ($img_end !== false)) {
106 $img_st_close++; // make it point to AFTER the closing bracket
107 $img_end += $img_start;
109 if(! strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
110 // This is an embedded image
112 $saved_image[$cnt] = substr($orig_body, $img_start + $img_st_close, $img_end - ($img_start + $img_st_close));
113 $new_body = $new_body . substr($orig_body, 0, $img_start) . '[!#saved_image' . $cnt . '#!]';
118 $new_body = $new_body . substr($orig_body, 0, $img_end + strlen('[/img]'));
120 $orig_body = substr($orig_body, $img_end + strlen('[/img]'));
122 if($orig_body === false) // in case the body ends on a closing image tag
125 $img_start = strpos($orig_body, '[img');
126 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
127 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
130 $new_body = $new_body . $orig_body;
132 return array('body' => $new_body, 'images' => $saved_image);
135 if(! function_exists('item_redir_and_replace_images')) {
136 function item_redir_and_replace_images($body, $images, $cid) {
141 for($i = 0; $i < count($images); $i++) {
142 $search = '/\[url\=(.*?)\]\[!#saved_image' . $i . '#!\]\[\/url\]' . '/is';
143 $replace = '[url=' . z_path() . '/redir/' . $cid
144 . '?f=1&url=' . '$1' . '][!#saved_image' . $i . '#!][/url]' ;
146 $img_end = strpos($origbody, '[!#saved_image' . $i . '#!][/url]') + strlen('[!#saved_image' . $i . '#!][/url]');
147 $process_part = substr($origbody, 0, $img_end);
148 $origbody = substr($origbody, $img_end);
150 $process_part = preg_replace($search, $replace, $process_part);
151 $newbody = $newbody . $process_part;
153 $newbody = $newbody . $origbody;
156 foreach($images as $image) {
157 // We're depending on the property of 'foreach' (specified on the PHP website) that
158 // it loops over the array starting from the first element and going sequentially
159 // to the last element
160 $newbody = str_replace('[!#saved_image' . $cnt . '#!]', '[img]' . $image . '[/img]', $newbody);
169 function message_content(&$a) {
172 nav_set_selected('messages');
175 notice( t('Permission denied.') . EOL);
179 $myprofile = $a->get_baseurl(true) . '/profile/' . $a->user['nickname'];
181 $tpl = get_markup_template('mail_head.tpl');
182 $header = replace_macros($tpl, array(
183 '$messages' => t('Messages'),
184 '$tab_content' => $tab_content
188 if(($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) {
189 if(! intval($a->argv[2]))
192 if($cmd === 'drop') {
193 $r = q("DELETE FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
198 info( t('Message deleted.') . EOL );
200 goaway($a->get_baseurl(true) . '/message' );
203 $r = q("SELECT `parent-uri`,`convid` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
208 $parent = $r[0]['parent-uri'];
209 $convid = $r[0]['convid'];
211 $r = q("DELETE FROM `mail` WHERE `parent-uri` = '%s' AND `uid` = %d ",
216 // remove diaspora conversation pointer
217 // Actually if we do this, we can never receive another reply to that conversation,
218 // as we will never again have the info we need to re-create it.
219 // We'll just have to orphan it.
222 // q("delete from conv where id = %d limit 1",
228 info( t('Conversation removed.') . EOL );
230 goaway($a->get_baseurl(true) . '/message' );
235 if(($a->argc > 1) && ($a->argv[1] === 'new')) {
240 if(intval(get_pconfig(local_user(),'system','plaintext')))
244 $tpl = get_markup_template('msg-header.tpl');
246 $a->page['htmlhead'] .= replace_macros($tpl, array(
247 '$baseurl' => $a->get_baseurl(true),
248 '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
249 '$nickname' => $a->user['nickname'],
250 '$linkurl' => t('Please enter a link URL:')
253 $preselect = (isset($a->argv[2])?array($a->argv[2]):false);
256 $prename = $preurl = $preid = '';
259 $r = q("select name, url, id from contact where uid = %d and id = %d limit 1",
260 intval(local_user()),
264 $prename = $r[0]['name'];
265 $preurl = $r[0]['url'];
266 $preid = $r[0]['id'];
270 $prefill = (($preselect) ? $prename : '');
272 // the ugly select box
274 $select = contact_select('messageto','message-to-select', $preselect, 4, true, false, false, 10);
276 $tpl = get_markup_template('prv_message.tpl');
277 $o .= replace_macros($tpl,array(
278 '$header' => t('Send Private Message'),
280 '$showinputs' => 'true',
281 '$prefill' => $prefill,
282 '$autocomp' => $autocomp,
284 '$subject' => t('Subject:'),
285 '$subjtxt' => ((x($_REQUEST,'subject')) ? strip_tags($_REQUEST['subject']) : ''),
286 '$text' => ((x($_REQUEST,'body')) ? escape_tags(htmlspecialchars($_REQUEST['body'])) : ''),
288 '$yourmessage' => t('Your message:'),
289 '$select' => $select,
291 '$upload' => t('Upload photo'),
292 '$insert' => t('Insert web link'),
293 '$wait' => t('Please wait'),
294 '$submit' => t('Submit')
307 $r = q("SELECT count(*) AS `total` FROM `mail`
308 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
309 intval(local_user()),
313 $a->set_pager_total($r[0]['total']);
315 $r = q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`,
316 `mail`.* , `contact`.`name`, `contact`.`url`, `contact`.`thumb` , `contact`.`network`,
318 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
319 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `mailcreated` DESC LIMIT %d , %d ",
320 intval(local_user()),
322 intval($a->pager['start']),
323 intval($a->pager['itemspage'])
327 info( t('No messages.') . EOL);
331 $tpl = get_markup_template('mail_list.tpl');
334 $partecipants = sprintf( t("Unknown sender - %s"),$rr['from-name']);
336 elseif (link_compare($rr['from-url'],$myprofile)){
337 $partecipants = sprintf( t("You and %s"), $rr['name']);
340 $partecipants = sprintf( t("%s and You"), $rr['from-name']);
343 $o .= replace_macros($tpl, array(
345 '$from_name' => $partecipants,
346 '$from_url' => (($rr['network'] === NETWORK_DFRN) ? $a->get_baseurl(true) . '/redir/' . $rr['contact-id'] : $rr['url']),
347 '$sparkle' => ' sparkle',
348 '$from_photo' => (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']),
349 '$subject' => template_escape((($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>')),
350 '$delete' => t('Delete conversation'),
351 '$body' => template_escape($rr['body']),
352 '$to_name' => template_escape($rr['name']),
353 '$date' => datetime_convert('UTC',date_default_timezone_get(),$rr['mailcreated'], t('D, d M Y - g:i A')),
354 '$seen' => $rr['mailseen'],
355 '$count' => sprintf( tt('%d message', '%d messages', $rr['count']), $rr['count']),
362 if(($a->argc > 1) && (intval($a->argv[1]))) {
366 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
367 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
368 WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
369 intval(local_user()),
373 $contact_id = $r[0]['contact-id'];
374 $convid = $r[0]['convid'];
376 $sql_extra = sprintf(" and `mail`.`parent-uri` = '%s' ", dbesc($r[0]['parent-uri']));
378 $sql_extra = sprintf(" and ( `mail`.`parent-uri` = '%s' OR `mail`.`convid` = '%d' ) ",
379 dbesc($r[0]['parent-uri']),
383 $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
384 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
385 WHERE `mail`.`uid` = %d $sql_extra ORDER BY `mail`.`created` ASC",
389 if(! count($messages)) {
390 notice( t('Message not available.') . EOL );
394 $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
395 dbesc($r[0]['parent-uri']),
399 require_once("include/bbcode.php");
401 $tpl = get_markup_template('msg-header.tpl');
403 $a->page['htmlhead'] .= replace_macros($tpl, array(
404 '$nickname' => $a->user['nickname'],
405 '$baseurl' => $a->get_baseurl(true)
413 foreach($messages as $message) {
414 if($message['unknown'])
416 if($message['from-url'] == $myprofile) {
417 $from_url = $myprofile;
421 $from_url = $a->get_baseurl(true) . '/redir/' . $message['contact-id'];
422 $sparkle = ' sparkle';
426 $extracted = item_extract_images($message['body']);
427 if($extracted['images'])
428 $message['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $message['contact-id']);
431 'id' => $message['id'],
432 'from_name' => template_escape($message['from-name']),
433 'from_url' => $from_url,
434 'sparkle' => $sparkle,
435 'from_photo' => $message['from-photo'],
436 'subject' => template_escape($message['title']),
437 'body' => template_escape(smilies(bbcode($message['body']))),
438 'delete' => t('Delete message'),
439 'to_name' => template_escape($message['name']),
440 'date' => datetime_convert('UTC',date_default_timezone_get(),$message['created'],'D, d M Y - g:i A'),
443 $seen = $message['seen'];
447 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
448 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
450 $tpl = get_markup_template('mail_display.tpl');
451 $o = replace_macros($tpl, array(
452 '$thread_id' => $a->argv[1],
453 '$thread_subject' => $message['title'],
454 '$thread_seen' => $seen,
455 '$delete' => t('Delete conversation'),
456 '$canreply' => (($unknown) ? false : '1'),
457 '$unknown_text' => t("No secure communications available. You <strong>may</strong> be able to respond from the sender's profile page."),
461 '$header' => t('Send Reply'),
464 '$subject' => t('Subject:'),
465 '$subjtxt' => template_escape($message['title']),
466 '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
467 '$yourmessage' => t('Your message:'),
469 '$select' => $select,
470 '$parent' => $parent,
471 '$upload' => t('Upload photo'),
472 '$insert' => t('Insert web link'),
473 '$submit' => t('Submit'),
474 '$wait' => t('Please wait')