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/jquery.autocomplete-min.js" ></script>';
22 $a->page['htmlhead'] .= <<< EOT
24 <script>$(document).ready(function() {
26 a = $("#recip").autocomplete({
27 serviceUrl: '$base/acl',
37 function message_post(&$a) {
40 notice( t('Permission denied.') . EOL);
44 $replyto = ((x($_REQUEST,'replyto')) ? notags(trim($_REQUEST['replyto'])) : '');
45 $subject = ((x($_REQUEST,'subject')) ? notags(trim($_REQUEST['subject'])) : '');
46 $body = ((x($_REQUEST,'body')) ? escape_tags(trim($_REQUEST['body'])) : '');
47 $recipient = ((x($_REQUEST,'messageto')) ? intval($_REQUEST['messageto']) : 0 );
49 // Work around doubled linefeeds in Tinymce 3.5b2
51 $plaintext = intval(get_pconfig(local_user(),'system','plaintext'));
53 $body = fix_mce_lf($body);
56 $ret = send_message($recipient, $body, $subject, $replyto);
61 notice( t('No recipient selected.') . EOL );
65 notice( t('Unable to locate contact information.') . EOL );
68 notice( t('Message could not be sent.') . EOL );
71 notice( t('Message collection failure.') . EOL );
74 info( t('Message sent.') . EOL );
77 // fake it to go back to the input form if no recipient listed
86 function message_content(&$a) {
89 nav_set_selected('messages');
92 notice( t('Permission denied.') . EOL);
96 $myprofile = $a->get_baseurl(true) . '/profile/' . $a->user['nickname'];
102 $tpl = get_markup_template('mail_head.tpl');
103 $header = replace_macros($tpl, array(
104 '$messages' => t('Messages'),
105 '$tab_content' => $tab_content
109 if(($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) {
110 if(! intval($a->argv[2]))
113 if($cmd === 'drop') {
114 $r = q("DELETE FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
119 info( t('Message deleted.') . EOL );
121 goaway($a->get_baseurl(true) . '/message' );
124 $r = q("SELECT `parent-uri`,`convid` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
129 $parent = $r[0]['parent-uri'];
130 $convid = $r[0]['convid'];
132 $r = q("DELETE FROM `mail` WHERE `parent-uri` = '%s' AND `uid` = %d ",
137 // remove diaspora conversation pointer
138 // Actually if we do this, we can never receive another reply to that conversation,
139 // as we will never again have the info we need to re-create it.
140 // We'll just have to orphan it.
143 // q("delete from conv where id = %d limit 1",
149 info( t('Conversation removed.') . EOL );
151 goaway($a->get_baseurl(true) . '/message' );
156 if(($a->argc > 1) && ($a->argv[1] === 'new')) {
161 if(intval(get_pconfig(local_user(),'system','plaintext')))
165 $tpl = get_markup_template('msg-header.tpl');
167 $a->page['htmlhead'] .= replace_macros($tpl, array(
168 '$baseurl' => $a->get_baseurl(true),
169 '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
170 '$nickname' => $a->user['nickname'],
171 '$linkurl' => t('Please enter a link URL:')
174 $preselect = (isset($a->argv[2])?array($a->argv[2]):false);
176 if(defined('EMAIL_AUTOCOMP')) {
178 // here's where we want to do contact autocomplete
179 // just figure out how to make it do the right thing
180 // pictures would be nice, but that didn't work when I tried.
181 // It sort of barely works, but needs help
182 // (the json backend is found in mod/acl.php)
184 $select = '<input type="text" id="recip" name="messageto" value="' . $preselect .'" />';
188 // the ugly select box
190 $select = contact_select('messageto','message-to-select', $preselect, 4, true, false, false, 10);
193 $tpl = get_markup_template('prv_message.tpl');
194 $o .= replace_macros($tpl,array(
195 '$header' => t('Send Private Message'),
197 '$subject' => t('Subject:'),
198 '$subjtxt' => ((x($_REQUEST,'subject')) ? strip_tags($_REQUEST['subject']) : ''),
199 '$text' => ((x($_REQUEST,'body')) ? escape_tags(htmlspecialchars($_REQUEST['body'])) : ''),
201 '$yourmessage' => t('Your message:'),
202 '$select' => $select,
204 '$upload' => t('Upload photo'),
205 '$insert' => t('Insert web link'),
206 '$wait' => t('Please wait')
218 $r = q("SELECT count(*) AS `total` FROM `mail`
219 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
220 intval(local_user()),
224 $a->set_pager_total($r[0]['total']);
226 $r = q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`,
227 `mail`.* , `contact`.`name`, `contact`.`url`, `contact`.`thumb` , `contact`.`network`,
229 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
230 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `mailcreated` DESC LIMIT %d , %d ",
231 intval(local_user()),
233 intval($a->pager['start']),
234 intval($a->pager['itemspage'])
237 info( t('No messages.') . EOL);
241 $tpl = get_markup_template('mail_list.tpl');
244 $partecipants = sprintf( t("Unknown sender - %s"),$rr['from-name']);
246 elseif (link_compare($rr['from-url'],$myprofile)){
247 $partecipants = sprintf( t("You and %s"), $rr['name']);
250 $partecipants = sprintf( t("%s and You"), $rr['from-name']);
253 $o .= replace_macros($tpl, array(
255 '$from_name' => $partecipants,
256 '$from_url' => (($rr['network'] === NETWORK_DFRN) ? $a->get_baseurl(true) . '/redir/' . $rr['contact-id'] : $rr['url']),
257 '$sparkle' => ' sparkle',
258 '$from_photo' => (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']),
259 '$subject' => template_escape((($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>')),
260 '$delete' => t('Delete conversation'),
261 '$body' => template_escape($rr['body']),
262 '$to_name' => template_escape($rr['name']),
263 '$date' => datetime_convert('UTC',date_default_timezone_get(),$rr['mailcreated'], t('D, d M Y - g:i A')),
264 '$seen' => $rr['mailseen'],
265 '$count' => sprintf( tt('%d message', '%d messages', $rr['count']), $rr['count']),
272 if(($a->argc > 1) && (intval($a->argv[1]))) {
276 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
277 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
278 WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
279 intval(local_user()),
283 $contact_id = $r[0]['contact-id'];
284 $convid = $r[0]['convid'];
286 $sql_extra = sprintf(" and `mail`.`parent-uri` = '%s' ", dbesc($r[0]['parent-uri']));
288 $sql_extra = sprintf(" and ( `mail`.`parent-uri` = '%s' OR `mail`.`convid` = '%d' ) ",
289 dbesc($r[0]['parent-uri']),
293 $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
294 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
295 WHERE `mail`.`uid` = %d $sql_extra ORDER BY `mail`.`created` ASC",
299 if(! count($messages)) {
300 notice( t('Message not available.') . EOL );
304 $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
305 dbesc($r[0]['parent-uri']),
309 require_once("include/bbcode.php");
311 $tpl = get_markup_template('msg-header.tpl');
313 $a->page['htmlhead'] .= replace_macros($tpl, array(
314 '$nickname' => $a->user['nickname'],
315 '$baseurl' => $a->get_baseurl(true)
323 foreach($messages as $message) {
324 if($message['unknown'])
326 if($message['from-url'] == $myprofile) {
327 $from_url = $myprofile;
331 $from_url = $a->get_baseurl(true) . '/redir/' . $message['contact-id'];
332 $sparkle = ' sparkle';
336 $Text = $message['body'];
338 $img_start = strpos($Text,'[img]data:');
339 $img_end = strpos($Text,'[/img]');
341 if($img_start !== false && $img_end !== false && $img_end > $img_start) {
342 $start_fragment = substr($Text,0,$img_start);
343 $img_start += strlen('[img]');
344 $saved_image = substr($Text,$img_start,$img_end - $img_start);
345 $end_fragment = substr($Text,$img_end + strlen('[/img]'));
346 $Text = $start_fragment . '[!#saved_image#!]' . $end_fragment;
347 $search = '/\[url\=(.*?)\]\[!#saved_image#!\]\[\/url\]' . '/is';
348 $replace = '[url=' . z_path() . '/redir/' . $message['contact-id']
349 . '?f=1&url=' . '$1' . '][!#saved_image#!][/url]' ;
351 $Text = preg_replace($search,$replace,$Text);
353 if(strlen($saved_image))
354 $message['body'] = str_replace('[!#saved_image#!]', '[img]' . $saved_image . '[/img]',$Text);
358 'id' => $message['id'],
359 'from_name' => template_escape($message['from-name']),
360 'from_url' => $from_url,
361 'sparkle' => $sparkle,
362 'from_photo' => $message['from-photo'],
363 'subject' => template_escape($message['title']),
364 'body' => template_escape(smilies(bbcode($message['body']))),
365 'delete' => t('Delete message'),
366 'to_name' => template_escape($message['name']),
367 'date' => datetime_convert('UTC',date_default_timezone_get(),$message['created'],'D, d M Y - g:i A'),
370 $seen = $message['seen'];
372 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
373 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
376 $tpl = get_markup_template('mail_display.tpl');
377 $o = replace_macros($tpl, array(
378 '$thread_id' => $a->argv[1],
379 '$thread_subject' => $message['title'],
380 '$thread_seen' => $seen,
381 '$delete' => t('Delete conversation'),
382 '$canreply' => (($unknown) ? false : '1'),
383 '$unknown_text' => t("No secure communications available. You <strong>may</strong> be able to respond from the sender's profile page."),
387 '$header' => t('Send Reply'),
389 '$subject' => t('Subject:'),
390 '$subjtxt' => template_escape($message['title']),
391 '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
392 '$yourmessage' => t('Your message:'),
394 '$select' => $select,
395 '$parent' => $parent,
396 '$upload' => t('Upload photo'),
397 '$insert' => t('Insert web link'),
398 '$wait' => t('Please wait')