3 require_once('include/acl_selectors.php');
4 require_once('include/message.php');
6 function message_init(&$a) {
11 'url'=> $a->get_baseurl(true) . '/message',
12 'sel'=> ($a->argc == 1),
16 'url' => $a->get_baseurl(true) . '/message/sent',
17 'sel'=> ($a->argv[1] == 'sent'),
22 'label' => t('New Message'),
23 'url' => $a->get_baseurl(true) . '/message/new',
24 'sel'=> ($a->argv[1] == 'new'),
27 $tpl = get_markup_template('message_side.tpl');
28 $a->page['aside'] = replace_macros($tpl, array(
35 function message_post(&$a) {
38 notice( t('Permission denied.') . EOL);
42 $replyto = ((x($_REQUEST,'replyto')) ? notags(trim($_REQUEST['replyto'])) : '');
43 $subject = ((x($_REQUEST,'subject')) ? notags(trim($_REQUEST['subject'])) : '');
44 $body = ((x($_REQUEST,'body')) ? escape_tags(trim($_REQUEST['body'])) : '');
45 $recipient = ((x($_REQUEST,'messageto')) ? intval($_REQUEST['messageto']) : 0 );
47 // Work around doubled linefeeds in Tinymce 3.5b2
49 $plaintext = intval(get_pconfig(local_user(),'system','plaintext'));
51 $body = str_replace("\r\n","\n",$body);
52 $body = str_replace("\n\n","\n",$body);
55 $ret = send_message($recipient, $body, $subject, $replyto);
60 notice( t('No recipient selected.') . EOL );
64 notice( t('Unable to locate contact information.') . EOL );
67 notice( t('Message could not be sent.') . EOL );
70 notice( t('Message collection failure.') . EOL );
73 info( t('Message sent.') . EOL );
76 // fake it to go back to the input form if no recipient listed
85 function message_content(&$a) {
88 nav_set_selected('messages');
91 notice( t('Permission denied.') . EOL);
95 $myprofile = $a->get_baseurl(true) . '/profile/' . $a->user['nickname'];
101 $tpl = get_markup_template('mail_head.tpl');
102 $header = replace_macros($tpl, array(
103 '$messages' => t('Messages'),
104 '$tab_content' => $tab_content
108 if(($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) {
109 if(! intval($a->argv[2]))
112 if($cmd === 'drop') {
113 $r = q("DELETE FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
118 info( t('Message deleted.') . EOL );
120 goaway($a->get_baseurl(true) . '/message' );
123 $r = q("SELECT `parent-uri`,`convid` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
128 $parent = $r[0]['parent-uri'];
129 $convid = $r[0]['convid'];
131 $r = q("DELETE FROM `mail` WHERE `parent-uri` = '%s' AND `uid` = %d ",
136 // remove diaspora conversation pointer
137 // Actually if we do this, we can never receive another reply to that conversation,
138 // as we will never again have the info we need to re-create it.
139 // We'll just have to orphan it.
142 // q("delete from conv where id = %d limit 1",
148 info( t('Conversation removed.') . EOL );
150 goaway($a->get_baseurl(true) . '/message' );
155 if(($a->argc > 1) && ($a->argv[1] === 'new')) {
160 if(intval(get_pconfig(local_user(),'system','plaintext')))
164 $tpl = get_markup_template('msg-header.tpl');
166 $a->page['htmlhead'] .= replace_macros($tpl, array(
167 '$baseurl' => $a->get_baseurl(true),
168 '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
169 '$nickname' => $a->user['nickname'],
170 '$linkurl' => t('Please enter a link URL:')
173 $preselect = (isset($a->argv[2])?array($a->argv[2]):false);
175 $select = contact_select('messageto','message-to-select', $preselect, 4, true, false, false, 10);
176 $tpl = get_markup_template('prv_message.tpl');
177 $o .= replace_macros($tpl,array(
178 '$header' => t('Send Private Message'),
180 '$subject' => t('Subject:'),
181 '$subjtxt' => ((x($_REQUEST,'subject')) ? strip_tags($_REQUEST['subject']) : ''),
182 '$text' => ((x($_REQUEST,'body')) ? escape_tags(htmlspecialchars($_REQUEST['body'])) : ''),
184 '$yourmessage' => t('Your message:'),
185 '$select' => $select,
187 '$upload' => t('Upload photo'),
188 '$insert' => t('Insert web link'),
189 '$wait' => t('Please wait')
195 if(($a->argc == 1) || ($a->argc == 2 && $a->argv[1] === 'sent')) {
200 $eq = sprintf( "AND `from-url` = '%s'", dbesc($myprofile));
204 $r = q("SELECT count(*) AS `total` FROM `mail`
205 WHERE `mail`.`uid` = %d AND `from-url` $eq '%s' GROUP BY `parent-uri` ORDER BY `created` DESC",
206 intval(local_user()),
210 $a->set_pager_total($r[0]['total']);
212 $r = q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`,
213 `mail`.* , `contact`.`name`, `contact`.`url`, `contact`.`thumb` , `contact`.`network`,
215 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
216 WHERE `mail`.`uid` = %d $eq GROUP BY `parent-uri` ORDER BY `mailcreated` DESC LIMIT %d , %d ",
217 intval(local_user()),
219 intval($a->pager['start']),
220 intval($a->pager['itemspage'])
223 info( t('No messages.') . EOL);
227 $tpl = get_markup_template('mail_list.tpl');
229 if ($rr['from-url'] == $myprofile){
230 $partecipants = sprintf( t("You and %s"), $rr['name']);
232 $partecipants = sprintf( t("%s and You"), $rr['from-name']);
235 $o .= replace_macros($tpl, array(
237 '$from_name' => $partecipants,
238 '$from_url' => (($rr['network'] === NETWORK_DFRN) ? $a->get_baseurl(true) . '/redir/' . $rr['contact-id'] : $rr['url']),
239 '$sparkle' => ' sparkle',
240 '$from_photo' => $rr['thumb'],
241 '$subject' => template_escape((($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>')),
242 '$delete' => t('Delete conversation'),
243 '$body' => template_escape($rr['body']),
244 '$to_name' => template_escape($rr['name']),
245 '$date' => datetime_convert('UTC',date_default_timezone_get(),$rr['mailcreated'], t('D, d M Y - g:i A')),
246 '$seen' => $rr['mailseen'],
247 '$count' => sprintf( tt('%d message', '%d messages', $rr['count']), $rr['count']),
254 if(($a->argc > 1) && (intval($a->argv[1]))) {
258 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
259 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
260 WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
261 intval(local_user()),
265 $contact_id = $r[0]['contact-id'];
266 $convid = $r[0]['convid'];
268 $sql_extra = sprintf(" and `mail`.`parent-uri` = '%s' ", dbesc($r[0]['parent-uri']));
270 $sql_extra = sprintf(" and ( `mail`.`parent-uri` = '%s' OR `mail`.`convid` = '%d' ) ",
271 dbesc($r[0]['parent-uri']),
275 $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
276 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
277 WHERE `mail`.`uid` = %d $sql_extra ORDER BY `mail`.`created` ASC",
281 if(! count($messages)) {
282 notice( t('Message not available.') . EOL );
286 $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
287 dbesc($r[0]['parent-uri']),
291 require_once("include/bbcode.php");
293 $tpl = get_markup_template('msg-header.tpl');
295 $a->page['htmlhead'] .= replace_macros($tpl, array(
296 '$nickname' => $a->user['nickname'],
297 '$baseurl' => $a->get_baseurl(true)
303 foreach($messages as $message) {
304 if($message['from-url'] == $myprofile) {
305 $from_url = $myprofile;
309 $from_url = $a->get_baseurl(true) . '/redir/' . $message['contact-id'];
310 $sparkle = ' sparkle';
313 'id' => $message['id'],
314 'from_name' => template_escape($message['from-name']),
315 'from_url' => $from_url,
316 'sparkle' => $sparkle,
317 'from_photo' => $message['from-photo'],
318 'subject' => template_escape($message['title']),
319 'body' => template_escape(smilies(bbcode($message['body']))),
320 'delete' => t('Delete message'),
321 'to_name' => template_escape($message['name']),
322 'date' => datetime_convert('UTC',date_default_timezone_get(),$message['created'],'D, d M Y - g:i A'),
325 $seen = $message['seen'];
327 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
328 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
331 $tpl = get_markup_template('mail_display.tpl');
332 $o = replace_macros($tpl, array(
333 '$thread_id' => $a->argv[1],
334 '$thread_subject' => $message['title'],
335 '$thread_seen' => $seen,
336 '$delete' => t('Delete conversation'),
341 '$header' => t('Send Reply'),
343 '$subject' => t('Subject:'),
344 '$subjtxt' => template_escape($message['title']),
345 '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
346 '$yourmessage' => t('Your message:'),
348 '$select' => $select,
349 '$parent' => $parent,
350 '$upload' => t('Upload photo'),
351 '$insert' => t('Insert web link'),
352 '$wait' => t('Please wait')