3 require_once('view/acl_selectors.php');
5 function message_init(&$a) {
10 function message_post(&$a) {
13 notice( t('Permission denied.') . EOL);
17 $replyto = notags(trim($_POST['replyto']));
18 $recipient = intval($_POST['messageto']);
19 $subject = notags(trim($_POST['subject']));
20 $body = escape_tags(trim($_POST['body']));
23 notice( t('No recipient selected.') . EOL );
27 $me = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
28 intval($_SESSION['uid'])
30 $contact = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
32 intval($_SESSION['uid'])
35 if(! (count($me) && (count($contact)))) {
36 notice( t('Unable to locate contact information.') . EOL );
40 $hash = random_string();
41 $uri = 'urn:X-dfrn:' . $a->get_baseurl() . ':' . $_SESSION['uid'] . ':' . $hash ;
43 if(! strlen($replyto))
46 $r = q("INSERT INTO `mail` ( `uid`, `from-name`, `from-photo`, `from-url`,
47 `contact-id`, `title`, `body`, `delivered`, `seen`, `replied`, `uri`, `parent-uri`, `created`)
48 VALUES ( %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, %d, '%s', '%s', '%s' )",
49 intval($_SESSION['uid']),
50 dbesc($me[0]['name']),
51 dbesc($me[0]['thumb']),
63 $r = q("SELECT * FROM `mail` WHERE `uri` = '%s' and `uid` = %d LIMIT 1",
65 intval($_SESSION['uid'])
68 $post_id = $r[0]['id'];
70 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
73 proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"mail\" \"$post_id\" &",
75 notice( t('Message sent.') . EOL );
78 notice( t('Message could not be sent.') . EOL );
84 function message_content(&$a) {
85 $o .= '<script> $(document).ready(function() { $(\'#nav-messages-link\').addClass(\'nav-selected\'); });</script>';
88 notice( t('Permission denied.') . EOL);
92 $myprofile = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
95 $tpl = file_get_contents('view/mail_head.tpl');
96 $header = replace_macros($tpl, array(
97 '$messages' => t('Messages'),
98 '$inbox' => t('Inbox'),
99 '$outbox' => t('Outbox'),
100 '$new' => t('New Message')
104 if(($a->argc == 3) && ($a->argv[1] == 'drop' || $a->argv[1] == 'dropconv')) {
105 if(! intval($a->argv[2]))
109 $r = q("DELETE FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
111 intval($_SESSION['uid'])
114 notice( t('Message deleted.') . EOL );
116 goaway($a->get_baseurl() . '/message' );
119 $r = q("SELECT `parent-uri` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
121 intval($_SESSION['uid'])
124 $parent = $r[0]['parent-uri'];
125 $r = q("DELETE FROM `mail` WHERE `parent-uri` = '%s' AND `uid` = %d ",
127 intval($_SESSION['uid'])
130 notice( t('Conversation removed.') . EOL );
132 goaway($a->get_baseurl() . '/message' );
136 if(($a->argc > 2) && ($a->argv[1] == 'redeliver') && intval($a->argv[2])) {
137 $post_id = intval($a->argv[2]);
138 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
140 proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"mail\" \"$post_id\" &",
142 goaway($a->get_baseurl() . '/message' );
147 if(($a->argc > 1) && ($a->argv[1] == 'new')) {
149 $tpl = file_get_contents('view/msg-header.tpl');
151 $a->page['htmlhead'] .= replace_macros($tpl, array('$baseurl' => $a->get_baseurl()));
153 $select .= contact_select('messageto','message-to-select', false, 4, true);
154 $tpl = file_get_contents('view/prv_message.tpl');
155 $o .= replace_macros($tpl,array(
156 '$header' => t('Send Private Message'),
158 '$subject' => t('Subject:'),
161 '$yourmessage' => t('Your message:'),
162 '$select' => $select,
164 '$upload' => t('Upload photo'),
165 '$insert' => t('Insert web link'),
166 '$wait' => t('Please wait')
173 if(($a->argc == 1) || ($a->argc == 2 && $a->argv[1] == 'sent')) {
178 $eq = '='; // I'm not going to bother escaping this.
180 $eq = '!='; // or this.
182 $r = q("SELECT count(*) AS `total` FROM `mail`
183 WHERE `mail`.`uid` = %d AND `from-url` $eq '%s' GROUP BY `parent-uri` ORDER BY `created` DESC",
184 intval($_SESSION['uid']),
188 $a->set_pager_total($r[0]['total']);
190 $r = q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`,
191 `mail`.* , `contact`.`name`, `contact`.`url`, `contact`.`thumb`
192 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
193 WHERE `mail`.`uid` = %d AND `from-url` $eq '%s' GROUP BY `parent-uri` ORDER BY `created` DESC LIMIT %d , %d ",
194 intval($_SESSION['uid']),
196 intval($a->pager['start']),
197 intval($a->pager['itemspage'])
200 notice( t('No messages.') . EOL);
204 $tpl = file_get_contents('view/mail_list.tpl');
206 $o .= replace_macros($tpl, array(
208 '$from_name' =>$rr['from-name'],
209 '$from_url' => $a->get_baseurl() . '/redir/' . $rr['contact-id'],
210 '$from_photo' => $rr['from-photo'],
211 '$subject' => (($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>'),
212 '$delete' => t('Delete conversation'),
213 '$body' => $rr['body'],
214 '$to_name' => $rr['name'],
215 '$date' => datetime_convert('UTC',date_default_timezone_get(),$rr['mailcreated'],'D, d M Y - g:i A')
222 if(($a->argc > 1) && (intval($a->argv[1]))) {
226 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
227 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
228 WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
229 intval($_SESSION['uid']),
233 $contact_id = $r[0]['contact-id'];
234 $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
235 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
236 WHERE `mail`.`uid` = %d AND `mail`.`parent-uri` = '%s' ORDER BY `mail`.`created` ASC",
237 intval($_SESSION['uid']),
238 dbesc($r[0]['parent-uri'])
241 if(! count($messages)) {
242 notice( t('Message not available.') . EOL );
246 $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
247 dbesc($r[0]['parent-uri']),
248 intval($_SESSION['uid'])
251 require_once("include/bbcode.php");
253 $tpl = file_get_contents('view/msg-header.tpl');
255 $a->page['htmlhead'] .= replace_macros($tpl, array('$baseurl' => $a->get_baseurl()));
258 $tpl = file_get_contents('view/mail_conv.tpl');
259 foreach($messages as $message) {
260 $o .= replace_macros($tpl, array(
261 '$id' => $message['id'],
262 '$from_name' =>$message['from-name'],
263 '$from_url' => (($message['from-url'] == $myprofile)
264 ? $myprofile : $a->get_baseurl() . '/redir/' . $message['contact-id']),
265 '$from_photo' => $message['from-photo'],
266 '$subject' => $message['title'],
267 '$body' => bbcode($message['body']),
268 '$delete' => t('Delete message'),
269 '$to_name' => $message['name'],
270 '$date' => datetime_convert('UTC',date_default_timezone_get(),$message['created'],'D, d M Y - g:i A')
274 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
275 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
276 $tpl = file_get_contents('view/prv_message.tpl');
277 $o .= replace_macros($tpl,array(
278 '$header' => t('Send Reply'),
280 '$subject' => t('Subject:'),
281 '$subjtxt' => $message['title'],
282 '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
283 '$yourmessage' => t('Your message:'),
284 '$select' => $select,
285 '$parent' => $parent,
286 '$upload' => t('Upload photo'),
287 '$insert' => t('Insert web link'),
288 '$wait' => t('Please wait')