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