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