3 require_once('include/acl_selectors.php');
5 function message_init(&$a) {
10 function message_post(&$a) {
13 notice( t('Permission denied.') . EOL);
17 $replyto = ((x($_POST,'replyto')) ? notags(trim($_POST['replyto'])) : '');
18 $subject = ((x($_POST,'subject')) ? notags(trim($_POST['subject'])) : '');
19 $body = ((x($_POST,'body')) ? escape_tags(trim($_POST['body'])) : '');
20 $recipient = ((x($_POST,'messageto')) ? intval($_POST['messageto']) : 0 );
23 notice( t('No recipient selected.') . EOL );
27 if(! strlen($subject))
28 $subject = t('[no subject]');
30 $me = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
33 $contact = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
38 if(! (count($me) && (count($contact)))) {
39 notice( t('Unable to locate contact information.') . EOL );
43 $hash = random_string();
44 $uri = 'urn:X-dfrn:' . $a->get_baseurl() . ':' . local_user() . ':' . $hash ;
46 if(! strlen($replyto))
49 $r = q("INSERT INTO `mail` ( `uid`, `from-name`, `from-photo`, `from-url`,
50 `contact-id`, `title`, `body`, `seen`, `replied`, `uri`, `parent-uri`, `created`)
51 VALUES ( %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, '%s', '%s', '%s' )",
53 dbesc($me[0]['name']),
54 dbesc($me[0]['thumb']),
65 $r = q("SELECT * FROM `mail` WHERE `uri` = '%s' and `uid` = %d LIMIT 1",
70 $post_id = $r[0]['id'];
72 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
75 proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"mail\" \"$post_id\" &",
77 notice( t('Message sent.') . EOL );
80 notice( t('Message could not be sent.') . EOL );
86 function message_content(&$a) {
89 $o .= '<script> $(document).ready(function() { $(\'#nav-messages-link\').addClass(\'nav-selected\'); });</script>';
92 notice( t('Permission denied.') . EOL);
96 $myprofile = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
99 $tpl = load_view_file('view/mail_head.tpl');
100 $header = replace_macros($tpl, array(
101 '$messages' => t('Messages'),
102 '$inbox' => t('Inbox'),
103 '$outbox' => t('Outbox'),
104 '$new' => t('New Message')
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 notice( t('Message deleted.') . EOL );
120 goaway($a->get_baseurl() . '/message' );
123 $r = q("SELECT `parent-uri` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
128 $parent = $r[0]['parent-uri'];
129 $r = q("DELETE FROM `mail` WHERE `parent-uri` = '%s' AND `uid` = %d ",
134 notice( t('Conversation removed.') . EOL );
136 goaway($a->get_baseurl() . '/message' );
141 if(($a->argc > 1) && ($a->argv[1] === 'new')) {
143 $tpl = load_view_file('view/msg-header.tpl');
145 $a->page['htmlhead'] .= replace_macros($tpl, array('$baseurl' => $a->get_baseurl()));
147 $select = contact_select('messageto','message-to-select', false, 4, true);
148 $tpl = load_view_file('view/prv_message.tpl');
149 $o .= replace_macros($tpl,array(
150 '$header' => t('Send Private Message'),
152 '$subject' => t('Subject:'),
155 '$yourmessage' => t('Your message:'),
156 '$select' => $select,
158 '$upload' => t('Upload photo'),
159 '$insert' => t('Insert web link'),
160 '$wait' => t('Please wait')
167 if(($a->argc == 1) || ($a->argc == 2 && $a->argv[1] === 'sent')) {
172 $eq = '='; // I'm not going to bother escaping this.
174 $eq = '!='; // or this.
176 $r = q("SELECT count(*) AS `total` FROM `mail`
177 WHERE `mail`.`uid` = %d AND `from-url` $eq '%s' GROUP BY `parent-uri` ORDER BY `created` DESC",
178 intval(local_user()),
182 $a->set_pager_total($r[0]['total']);
184 $r = q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`,
185 `mail`.* , `contact`.`name`, `contact`.`url`, `contact`.`thumb`
186 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
187 WHERE `mail`.`uid` = %d AND `from-url` $eq '%s' GROUP BY `parent-uri` ORDER BY `created` DESC LIMIT %d , %d ",
188 intval(local_user()),
190 intval($a->pager['start']),
191 intval($a->pager['itemspage'])
194 notice( t('No messages.') . EOL);
198 $tpl = load_view_file('view/mail_list.tpl');
200 $o .= replace_macros($tpl, array(
202 '$from_name' =>$rr['from-name'],
203 '$from_url' => $a->get_baseurl() . '/redir/' . $rr['contact-id'],
204 '$sparkle' => ' sparkle',
205 '$from_photo' => $rr['from-photo'],
206 '$subject' => (($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>'),
207 '$delete' => t('Delete conversation'),
208 '$body' => $rr['body'],
209 '$to_name' => $rr['name'],
210 '$date' => datetime_convert('UTC',date_default_timezone_get(),$rr['mailcreated'],'D, d M Y - g:i A')
217 if(($a->argc > 1) && (intval($a->argv[1]))) {
221 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
222 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
223 WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
224 intval(local_user()),
228 $contact_id = $r[0]['contact-id'];
229 $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
230 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
231 WHERE `mail`.`uid` = %d AND `mail`.`parent-uri` = '%s' ORDER BY `mail`.`created` ASC",
232 intval(local_user()),
233 dbesc($r[0]['parent-uri'])
236 if(! count($messages)) {
237 notice( t('Message not available.') . EOL );
241 $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
242 dbesc($r[0]['parent-uri']),
246 require_once("include/bbcode.php");
248 $tpl = load_view_file('view/msg-header.tpl');
250 $a->page['htmlhead'] .= replace_macros($tpl, array('$baseurl' => $a->get_baseurl()));
253 $tpl = load_view_file('view/mail_conv.tpl');
254 foreach($messages as $message) {
255 if($message['from-url'] == $myprofile) {
256 $from_url = $myprofile;
260 $from_url = $a->get_baseurl() . '/redir/' . $message['contact-id'];
261 $sparkle = ' sparkle';
263 $o .= replace_macros($tpl, array(
264 '$id' => $message['id'],
265 '$from_name' =>$message['from-name'],
266 '$from_url' => $from_url,
267 '$sparkle' => $sparkle,
268 '$from_photo' => $message['from-photo'],
269 '$subject' => $message['title'],
270 '$body' => bbcode($message['body']),
271 '$delete' => t('Delete message'),
272 '$to_name' => $message['name'],
273 '$date' => datetime_convert('UTC',date_default_timezone_get(),$message['created'],'D, d M Y - g:i A')
277 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
278 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
279 $tpl = load_view_file('view/prv_message.tpl');
280 $o .= replace_macros($tpl,array(
281 '$header' => t('Send Reply'),
283 '$subject' => t('Subject:'),
284 '$subjtxt' => $message['title'],
285 '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
286 '$yourmessage' => t('Your message:'),
287 '$select' => $select,
288 '$parent' => $parent,
289 '$upload' => t('Upload photo'),
290 '$insert' => t('Insert web link'),
291 '$wait' => t('Please wait')