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 function message_content(&$a) {
94 nav_set_selected('messages');
97 notice( t('Permission denied.') . EOL);
101 $myprofile = $a->get_baseurl(true) . '/profile/' . $a->user['nickname'];
103 $tpl = get_markup_template('mail_head.tpl');
104 $header = replace_macros($tpl, array(
105 '$messages' => t('Messages'),
106 '$tab_content' => $tab_content
110 if(($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) {
111 if(! intval($a->argv[2]))
114 if($cmd === 'drop') {
115 $r = q("DELETE FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
120 info( t('Message deleted.') . EOL );
122 goaway($a->get_baseurl(true) . '/message' );
125 $r = q("SELECT `parent-uri`,`convid` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
130 $parent = $r[0]['parent-uri'];
131 $convid = $r[0]['convid'];
133 $r = q("DELETE FROM `mail` WHERE `parent-uri` = '%s' AND `uid` = %d ",
138 // remove diaspora conversation pointer
139 // Actually if we do this, we can never receive another reply to that conversation,
140 // as we will never again have the info we need to re-create it.
141 // We'll just have to orphan it.
144 // q("delete from conv where id = %d limit 1",
150 info( t('Conversation removed.') . EOL );
152 goaway($a->get_baseurl(true) . '/message' );
157 if(($a->argc > 1) && ($a->argv[1] === 'new')) {
162 if(intval(get_pconfig(local_user(),'system','plaintext')))
166 $tpl = get_markup_template('msg-header.tpl');
168 $a->page['htmlhead'] .= replace_macros($tpl, array(
169 '$baseurl' => $a->get_baseurl(true),
170 '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
171 '$nickname' => $a->user['nickname'],
172 '$linkurl' => t('Please enter a link URL:')
175 $preselect = (isset($a->argv[2])?array($a->argv[2]):false);
178 $prename = $preurl = $preid = '';
181 $r = q("select name, url, id from contact where uid = %d and id = %d limit 1",
182 intval(local_user()),
186 $prename = $r[0]['name'];
187 $preurl = $r[0]['url'];
188 $preid = $r[0]['id'];
192 $prefill = (($preselect) ? $prename : '');
194 // the ugly select box
196 $select = contact_select('messageto','message-to-select', $preselect, 4, true, false, false, 10);
198 $tpl = get_markup_template('prv_message.tpl');
199 $o .= replace_macros($tpl,array(
200 '$header' => t('Send Private Message'),
202 '$showinputs' => 'true',
203 '$prefill' => $prefill,
204 '$autocomp' => $autocomp,
206 '$subject' => t('Subject:'),
207 '$subjtxt' => ((x($_REQUEST,'subject')) ? strip_tags($_REQUEST['subject']) : ''),
208 '$text' => ((x($_REQUEST,'body')) ? escape_tags(htmlspecialchars($_REQUEST['body'])) : ''),
210 '$yourmessage' => t('Your message:'),
211 '$select' => $select,
213 '$upload' => t('Upload photo'),
214 '$insert' => t('Insert web link'),
215 '$wait' => t('Please wait'),
216 '$submit' => t('Submit')
229 $r = q("SELECT count(*) AS `total` FROM `mail`
230 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
231 intval(local_user()),
235 $a->set_pager_total($r[0]['total']);
237 $r = q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`,
238 `mail`.* , `contact`.`name`, `contact`.`url`, `contact`.`thumb` , `contact`.`network`,
240 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
241 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `mailcreated` DESC LIMIT %d , %d ",
242 intval(local_user()),
244 intval($a->pager['start']),
245 intval($a->pager['itemspage'])
249 info( t('No messages.') . EOL);
253 $tpl = get_markup_template('mail_list.tpl');
256 $partecipants = sprintf( t("Unknown sender - %s"),$rr['from-name']);
258 elseif (link_compare($rr['from-url'],$myprofile)){
259 $partecipants = sprintf( t("You and %s"), $rr['name']);
262 $partecipants = sprintf( t("%s and You"), $rr['from-name']);
265 $o .= replace_macros($tpl, array(
267 '$from_name' => $partecipants,
268 '$from_url' => (($rr['network'] === NETWORK_DFRN) ? $a->get_baseurl(true) . '/redir/' . $rr['contact-id'] : $rr['url']),
269 '$sparkle' => ' sparkle',
270 '$from_photo' => (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']),
271 '$subject' => template_escape((($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>')),
272 '$delete' => t('Delete conversation'),
273 '$body' => template_escape($rr['body']),
274 '$to_name' => template_escape($rr['name']),
275 '$date' => datetime_convert('UTC',date_default_timezone_get(),$rr['mailcreated'], t('D, d M Y - g:i A')),
276 '$seen' => $rr['mailseen'],
277 '$count' => sprintf( tt('%d message', '%d messages', $rr['count']), $rr['count']),
284 if(($a->argc > 1) && (intval($a->argv[1]))) {
288 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
289 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
290 WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
291 intval(local_user()),
295 $contact_id = $r[0]['contact-id'];
296 $convid = $r[0]['convid'];
298 $sql_extra = sprintf(" and `mail`.`parent-uri` = '%s' ", dbesc($r[0]['parent-uri']));
300 $sql_extra = sprintf(" and ( `mail`.`parent-uri` = '%s' OR `mail`.`convid` = '%d' ) ",
301 dbesc($r[0]['parent-uri']),
305 $messages = 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 $sql_extra ORDER BY `mail`.`created` ASC",
311 if(! count($messages)) {
312 notice( t('Message not available.') . EOL );
316 $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
317 dbesc($r[0]['parent-uri']),
321 require_once("include/bbcode.php");
323 $tpl = get_markup_template('msg-header.tpl');
325 $a->page['htmlhead'] .= replace_macros($tpl, array(
326 '$nickname' => $a->user['nickname'],
327 '$baseurl' => $a->get_baseurl(true)
335 foreach($messages as $message) {
336 if($message['unknown'])
338 if($message['from-url'] == $myprofile) {
339 $from_url = $myprofile;
343 $from_url = $a->get_baseurl(true) . '/redir/' . $message['contact-id'];
344 $sparkle = ' sparkle';
348 $Text = $message['body'];
350 $img_start = strpos($Text,'[img]data:');
351 $img_end = strpos($Text,'[/img]');
353 if($img_start !== false && $img_end !== false && $img_end > $img_start) {
354 $start_fragment = substr($Text,0,$img_start);
355 $img_start += strlen('[img]');
356 $saved_image = substr($Text,$img_start,$img_end - $img_start);
357 $end_fragment = substr($Text,$img_end + strlen('[/img]'));
358 $Text = $start_fragment . '[!#saved_image#!]' . $end_fragment;
359 $search = '/\[url\=(.*?)\]\[!#saved_image#!\]\[\/url\]' . '/is';
360 $replace = '[url=' . z_path() . '/redir/' . $message['contact-id']
361 . '?f=1&url=' . '$1' . '][!#saved_image#!][/url]' ;
363 $Text = preg_replace($search,$replace,$Text);
365 if(strlen($saved_image))
366 $message['body'] = str_replace('[!#saved_image#!]', '[img]' . $saved_image . '[/img]',$Text);
370 'id' => $message['id'],
371 'from_name' => template_escape($message['from-name']),
372 'from_url' => $from_url,
373 'sparkle' => $sparkle,
374 'from_photo' => $message['from-photo'],
375 'subject' => template_escape($message['title']),
376 'body' => template_escape(smilies(bbcode($message['body']))),
377 'delete' => t('Delete message'),
378 'to_name' => template_escape($message['name']),
379 'date' => datetime_convert('UTC',date_default_timezone_get(),$message['created'],'D, d M Y - g:i A'),
382 $seen = $message['seen'];
386 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
387 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
389 $tpl = get_markup_template('mail_display.tpl');
390 $o = replace_macros($tpl, array(
391 '$thread_id' => $a->argv[1],
392 '$thread_subject' => $message['title'],
393 '$thread_seen' => $seen,
394 '$delete' => t('Delete conversation'),
395 '$canreply' => (($unknown) ? false : '1'),
396 '$unknown_text' => t("No secure communications available. You <strong>may</strong> be able to respond from the sender's profile page."),
400 '$header' => t('Send Reply'),
403 '$subject' => t('Subject:'),
404 '$subjtxt' => template_escape($message['title']),
405 '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
406 '$yourmessage' => t('Your message:'),
408 '$select' => $select,
409 '$parent' => $parent,
410 '$upload' => t('Upload photo'),
411 '$insert' => t('Insert web link'),
412 '$submit' => t('Submit'),
413 '$wait' => t('Please wait')