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