]> git.mxchange.org Git - friendica.git/blob - mod/message.php
hub anomolies & swat0 compliance
[friendica.git] / mod / message.php
1 <?php
2
3 require_once('view/acl_selectors.php');
4
5 function message_init(&$a) {
6
7
8 }
9
10 function message_post(&$a) {
11
12         if(! local_user()) {
13                 notice( t('Permission denied.') . EOL);
14                 return;
15         }
16
17         $replyto = notags(trim($_POST['replyto']));
18         $recipient = intval($_POST['messageto']);
19         $subject = notags(trim($_POST['subject']));
20         $body = escape_tags(trim($_POST['body']));
21
22         if(! $recipient) {
23                 notice( t('No recipient selected.') . EOL );
24                 return;
25         }
26
27         if(! strlen($subject))
28                 $subject = t('[no subject]');
29
30         $me = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
31                 intval($_SESSION['uid'])
32         );
33         $contact = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
34                         intval($recipient),
35                         intval($_SESSION['uid'])
36         );
37
38         if(! (count($me) && (count($contact)))) {
39                 notice( t('Unable to locate contact information.') . EOL );
40                 return;
41         }
42
43         $hash = random_string();
44         $uri = 'urn:X-dfrn:' . $a->get_baseurl() . ':' . $_SESSION['uid'] . ':' . $hash ;
45
46         if(! strlen($replyto))
47                 $replyto = $uri;
48
49         $r = q("INSERT INTO `mail` ( `uid`, `from-name`, `from-photo`, `from-url`, 
50                 `contact-id`, `title`, `body`, `delivered`, `seen`, `replied`, `uri`, `parent-uri`, `created`)
51                 VALUES ( %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, %d, '%s', '%s', '%s' )",
52                 intval($_SESSION['uid']),
53                 dbesc($me[0]['name']),
54                 dbesc($me[0]['thumb']),
55                 dbesc($me[0]['url']),
56                 intval($recipient),
57                 dbesc($subject),
58                 dbesc($body),
59                 0,
60                 1,
61                 0,
62                 dbesc($uri),
63                 dbesc($replyto),
64                 datetime_convert()
65         );
66         $r = q("SELECT * FROM `mail` WHERE `uri` = '%s' and `uid` = %d LIMIT 1",
67                 dbesc($uri),
68                 intval($_SESSION['uid'])
69         );
70         if(count($r))
71                 $post_id = $r[0]['id'];
72
73         $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
74         
75         if($post_id) {
76                 proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"mail\" \"$post_id\" &",
77                         array(),$foo));
78                 notice( t('Message sent.') . EOL );
79         }
80         else {
81                 notice( t('Message could not be sent.') . EOL );
82         }
83         return;
84
85 }
86
87 function message_content(&$a) {
88
89         $o = '';
90         $o .= '<script> $(document).ready(function() { $(\'#nav-messages-link\').addClass(\'nav-selected\'); });</script>';
91
92         if(! local_user()) {
93                 notice( t('Permission denied.') . EOL);
94                 return;
95         }
96
97         $myprofile = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
98
99
100         $tpl = load_view_file('view/mail_head.tpl');
101         $header = replace_macros($tpl, array(
102                 '$messages' => t('Messages'),
103                 '$inbox' => t('Inbox'),
104                 '$outbox' => t('Outbox'),
105                 '$new' => t('New Message')
106         ));
107
108
109         if(($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) {
110                 if(! intval($a->argv[2]))
111                         return;
112                 $cmd = $a->argv[1];
113                 if($cmd === 'drop') {
114                         $r = q("DELETE FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
115                                 intval($a->argv[2]),
116                                 intval($_SESSION['uid'])
117                         );
118                         if($r) {
119                                 notice( t('Message deleted.') . EOL );
120                         }
121                         goaway($a->get_baseurl() . '/message' );
122                 }
123                 else {
124                         $r = q("SELECT `parent-uri` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
125                                 intval($a->argv[2]),
126                                 intval($_SESSION['uid'])
127                         );
128                         if(count($r)) {
129                                 $parent = $r[0]['parent-uri'];
130                                 $r = q("DELETE FROM `mail` WHERE `parent-uri` = '%s' AND `uid` = %d ",
131                                         dbesc($parent),
132                                         intval($_SESSION['uid'])
133                                 );
134                                 if($r)
135                                         notice( t('Conversation removed.') . EOL );
136                         } 
137                         goaway($a->get_baseurl() . '/message' );
138                 }       
139         
140         }
141         if(($a->argc > 2) && ($a->argv[1] === 'redeliver') && intval($a->argv[2])) {
142                 $post_id = intval($a->argv[2]);
143                 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
144
145                 proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"mail\" \"$post_id\" & ",
146                         array(),$foo));
147                 goaway($a->get_baseurl() . '/message' );
148         }
149
150
151
152         if(($a->argc > 1) && ($a->argv[1] === 'new')) {
153                 
154                 $tpl = load_view_file('view/msg-header.tpl');
155         
156                 $a->page['htmlhead'] .= replace_macros($tpl, array('$baseurl' => $a->get_baseurl()));
157
158                 $select = contact_select('messageto','message-to-select', false, 4, true);
159                 $tpl = load_view_file('view/prv_message.tpl');
160                 $o .= replace_macros($tpl,array(
161                         '$header' => t('Send Private Message'),
162                         '$to' => t('To:'),
163                         '$subject' => t('Subject:'),
164                         '$subjtxt' => '',
165                         '$readonly' => '',
166                         '$yourmessage' => t('Your message:'),
167                         '$select' => $select,
168                         '$parent' => '',
169                         '$upload' => t('Upload photo'),
170                         '$insert' => t('Insert web link'),
171                         '$wait' => t('Please wait')
172
173                 ));
174
175                 return $o;
176         }
177
178         if(($a->argc == 1) || ($a->argc == 2 && $a->argv[1] === 'sent')) {
179
180                 $o .= $header;
181                 
182                 if($a->argc == 2)
183                         $eq = '='; // I'm not going to bother escaping this.
184                 else
185                         $eq = '!='; // or this.
186
187                 $r = q("SELECT count(*) AS `total` FROM `mail` 
188                         WHERE `mail`.`uid` = %d AND `from-url` $eq '%s' GROUP BY `parent-uri` ORDER BY `created` DESC",
189                         intval($_SESSION['uid']),
190                         dbesc($myprofile)
191                 );
192                 if(count($r))
193                         $a->set_pager_total($r[0]['total']);
194         
195                 $r = q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`, 
196                         `mail`.* , `contact`.`name`, `contact`.`url`, `contact`.`thumb` 
197                         FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id` 
198                         WHERE `mail`.`uid` = %d AND `from-url` $eq '%s' GROUP BY `parent-uri` ORDER BY `created` DESC  LIMIT %d , %d ",
199                         intval($_SESSION['uid']),
200                         dbesc($myprofile),
201                         intval($a->pager['start']),
202                         intval($a->pager['itemspage'])
203                 );
204                 if(! count($r)) {
205                         notice( t('No messages.') . EOL);
206                         return $o;
207                 }
208
209                 $tpl = load_view_file('view/mail_list.tpl');
210                 foreach($r as $rr) {
211                         $o .= replace_macros($tpl, array(
212                                 '$id' => $rr['id'],
213                                 '$from_name' =>$rr['from-name'],
214                                 '$from_url' => $a->get_baseurl() . '/redir/' . $rr['contact-id'],
215                                 '$sparkle' => ' sparkle',
216                                 '$from_photo' => $rr['from-photo'],
217                                 '$subject' => (($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>'),
218                                 '$delete' => t('Delete conversation'),
219                                 '$body' => $rr['body'],
220                                 '$to_name' => $rr['name'],
221                                 '$date' => datetime_convert('UTC',date_default_timezone_get(),$rr['mailcreated'],'D, d M Y - g:i A')
222                         ));
223                 }
224                 $o .= paginate($a);     
225                 return $o;
226         }
227
228         if(($a->argc > 1) && (intval($a->argv[1]))) {
229
230                 $o .= $header;
231
232                 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb` 
233                         FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id` 
234                         WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
235                         intval($_SESSION['uid']),
236                         intval($a->argv[1])
237                 );
238                 if(count($r)) { 
239                         $contact_id = $r[0]['contact-id'];
240                         $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb` 
241                                 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id` 
242                                 WHERE `mail`.`uid` = %d AND `mail`.`parent-uri` = '%s' ORDER BY `mail`.`created` ASC",
243                                 intval($_SESSION['uid']),
244                                 dbesc($r[0]['parent-uri'])
245                         );
246                 }
247                 if(! count($messages)) {
248                         notice( t('Message not available.') . EOL );
249                         return $o;
250                 }
251
252                 $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
253                         dbesc($r[0]['parent-uri']),
254                         intval($_SESSION['uid'])
255                 );
256
257                 require_once("include/bbcode.php");
258
259                 $tpl = load_view_file('view/msg-header.tpl');
260         
261                 $a->page['htmlhead'] .= replace_macros($tpl, array('$baseurl' => $a->get_baseurl()));
262
263
264                 $tpl = load_view_file('view/mail_conv.tpl');
265                 foreach($messages as $message) {
266                         if($message['from-url'] == $myprofile) {
267                                 $from_url = $myprofile;
268                                 $sparkle = '';
269                         }
270                         else {
271                                 $from_url = $a->get_baseurl() . '/redir/' . $message['contact-id'];
272                                 $sparkle = ' sparkle';
273                         }
274                         $o .= replace_macros($tpl, array(
275                                 '$id' => $message['id'],
276                                 '$from_name' =>$message['from-name'],
277                                 '$from_url' => $from_url,
278                                 '$sparkle' => $sparkle,
279                                 '$from_photo' => $message['from-photo'],
280                                 '$subject' => $message['title'],
281                                 '$body' => bbcode($message['body']),
282                                 '$delete' => t('Delete message'),
283                                 '$to_name' => $message['name'],
284                                 '$date' => datetime_convert('UTC',date_default_timezone_get(),$message['created'],'D, d M Y - g:i A')
285                         ));
286                                 
287                 }
288                 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
289                 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
290                 $tpl = load_view_file('view/prv_message.tpl');
291                 $o .= replace_macros($tpl,array(
292                         '$header' => t('Send Reply'),
293                         '$to' => t('To:'),
294                         '$subject' => t('Subject:'),
295                         '$subjtxt' => $message['title'],
296                         '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
297                         '$yourmessage' => t('Your message:'),
298                         '$select' => $select,
299                         '$parent' => $parent,
300                         '$upload' => t('Upload photo'),
301                         '$insert' => t('Insert web link'),
302                         '$wait' => t('Please wait')
303
304                 ));
305
306                 return $o;
307         }
308
309 }