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