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