]> git.mxchange.org Git - friendica.git/blob - mod/message.php
4821a45d263bc0aa5a1bb12d05ce7e84e2a3c44c
[friendica.git] / mod / message.php
1 <?php
2
3 require_once('include/acl_selectors.php');
4
5 function message_post(&$a) {
6
7         if(! local_user()) {
8                 notice( t('Permission denied.') . EOL);
9                 return;
10         }
11
12         $replyto   = ((x($_POST,'replyto'))   ? notags(trim($_POST['replyto']))   : '');
13         $subject   = ((x($_POST,'subject'))   ? notags(trim($_POST['subject']))   : '');
14         $body      = ((x($_POST,'body'))      ? escape_tags(trim($_POST['body'])) : '');
15         $recipient = ((x($_POST,'messageto')) ? intval($_POST['messageto'])       : 0 );
16
17         if(! $recipient) {
18                 notice( t('No recipient selected.') . EOL );
19                 return;
20         }
21
22         if(! strlen($subject))
23                 $subject = t('[no subject]');
24
25         $me = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
26                 intval(local_user())
27         );
28         $contact = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
29                         intval($recipient),
30                         intval(local_user())
31         );
32
33         if(! (count($me) && (count($contact)))) {
34                 notice( t('Unable to locate contact information.') . EOL );
35                 return;
36         }
37
38         $hash = random_string();
39         $uri = 'urn:X-dfrn:' . $a->get_baseurl() . ':' . local_user() . ':' . $hash ;
40
41         if(! strlen($replyto))
42                 $replyto = $uri;
43
44         $r = q("INSERT INTO `mail` ( `uid`, `from-name`, `from-photo`, `from-url`, 
45                 `contact-id`, `title`, `body`, `seen`, `replied`, `uri`, `parent-uri`, `created`)
46                 VALUES ( %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, '%s', '%s', '%s' )",
47                 intval(local_user()),
48                 dbesc($me[0]['name']),
49                 dbesc($me[0]['thumb']),
50                 dbesc($me[0]['url']),
51                 intval($recipient),
52                 dbesc($subject),
53                 dbesc($body),
54                 1,
55                 0,
56                 dbesc($uri),
57                 dbesc($replyto),
58                 datetime_convert()
59         );
60         $r = q("SELECT * FROM `mail` WHERE `uri` = '%s' and `uid` = %d LIMIT 1",
61                 dbesc($uri),
62                 intval(local_user())
63         );
64         if(count($r))
65                 $post_id = $r[0]['id'];
66
67         /**
68          *
69          * When a photo was uploaded into the message using the (profile wall) ajax 
70          * uploader, The permissions are initially set to disallow anybody but the
71          * owner from seeing it. This is because the permissions may not yet have been
72          * set for the post. If it's private, the photo permissions should be set
73          * appropriately. But we didn't know the final permissions on the post until
74          * now. So now we'll look for links of uploaded messages that are in the
75          * post and set them to the same permissions as the post itself.
76          *
77          */
78
79         $match = null;
80
81         if(preg_match_all("/\[img\](.+?)\[\/img\]/",$body,$match)) {
82                 $images = $match[1];
83                 if(count($images)) {
84                         foreach($images as $image) {
85                                 if(! stristr($image,$a->get_baseurl() . '/photo/'))
86                                         continue;
87                                 $image_uri = substr($image,strrpos($image,'/') + 1);
88                                 $image_uri = substr($image_uri,0, strpos($image_uri,'-'));
89                                 $r = q("UPDATE `photo` SET `allow_cid` = '%s'
90                                         WHERE `resource-id` = '%s' AND `album` = '%s' AND `uid` = %d ",
91                                         dbesc('<' . $recipient . '>'),
92                                         dbesc($image_uri),
93                                         dbesc( t('Wall Photos')),
94                                         intval(local_user())
95                                 ); 
96                         }
97                 }
98         }
99         
100         if($post_id) {
101                 proc_run('php',"include/notifier.php","mail","$post_id");
102                 notice( t('Message sent.') . EOL );
103         }
104         else {
105                 notice( t('Message could not be sent.') . EOL );
106         }
107         return;
108 }
109
110 function message_content(&$a) {
111
112         $o = '';
113         $o .= '<script> $(document).ready(function() { $(\'#nav-messages-link\').addClass(\'nav-selected\'); });</script>';
114
115         if(! local_user()) {
116                 notice( t('Permission denied.') . EOL);
117                 return;
118         }
119
120         $myprofile = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
121
122
123         $tpl = load_view_file('view/mail_head.tpl');
124         $header = replace_macros($tpl, array(
125                 '$messages' => t('Messages'),
126                 '$inbox' => t('Inbox'),
127                 '$outbox' => t('Outbox'),
128                 '$new' => t('New Message')
129         ));
130
131
132         if(($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) {
133                 if(! intval($a->argv[2]))
134                         return;
135                 $cmd = $a->argv[1];
136                 if($cmd === 'drop') {
137                         $r = q("DELETE FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
138                                 intval($a->argv[2]),
139                                 intval(local_user())
140                         );
141                         if($r) {
142                                 notice( t('Message deleted.') . EOL );
143                         }
144                         goaway($a->get_baseurl() . '/message' );
145                 }
146                 else {
147                         $r = q("SELECT `parent-uri` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
148                                 intval($a->argv[2]),
149                                 intval(local_user())
150                         );
151                         if(count($r)) {
152                                 $parent = $r[0]['parent-uri'];
153                                 $r = q("DELETE FROM `mail` WHERE `parent-uri` = '%s' AND `uid` = %d ",
154                                         dbesc($parent),
155                                         intval(local_user())
156                                 );
157                                 if($r)
158                                         notice( t('Conversation removed.') . EOL );
159                         } 
160                         goaway($a->get_baseurl() . '/message' );
161                 }       
162         
163         }
164
165         if(($a->argc > 1) && ($a->argv[1] === 'new')) {
166                 
167                 $tpl = load_view_file('view/msg-header.tpl');
168
169                 $a->page['htmlhead'] .= replace_macros($tpl, array(
170                         '$baseurl' => $a->get_baseurl(),
171                         '$nickname' => $a->user['nickname']
172                 ));
173         
174                 $select = contact_select('messageto','message-to-select', false, 4, true);
175                 $tpl = load_view_file('view/prv_message.tpl');
176                 $o .= replace_macros($tpl,array(
177                         '$header' => t('Send Private Message'),
178                         '$to' => t('To:'),
179                         '$subject' => t('Subject:'),
180                         '$subjtxt' => '',
181                         '$readonly' => '',
182                         '$yourmessage' => t('Your message:'),
183                         '$select' => $select,
184                         '$parent' => '',
185                         '$upload' => t('Upload photo'),
186                         '$insert' => t('Insert web link'),
187                         '$wait' => t('Please wait')
188
189                 ));
190
191                 return $o;
192         }
193
194         if(($a->argc == 1) || ($a->argc == 2 && $a->argv[1] === 'sent')) {
195
196                 $o .= $header;
197                 
198                 if($a->argc == 2)
199                         $eq = '='; // I'm not going to bother escaping this.
200                 else
201                         $eq = '!='; // or this.
202
203                 $r = q("SELECT count(*) AS `total` FROM `mail` 
204                         WHERE `mail`.`uid` = %d AND `from-url` $eq '%s' GROUP BY `parent-uri` ORDER BY `created` DESC",
205                         intval(local_user()),
206                         dbesc($myprofile)
207                 );
208                 if(count($r))
209                         $a->set_pager_total($r[0]['total']);
210         
211                 $r = q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`, 
212                         `mail`.* , `contact`.`name`, `contact`.`url`, `contact`.`thumb` 
213                         FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id` 
214                         WHERE `mail`.`uid` = %d AND `from-url` $eq '%s' GROUP BY `parent-uri` ORDER BY `created` DESC  LIMIT %d , %d ",
215                         intval(local_user()),
216                         dbesc($myprofile),
217                         intval($a->pager['start']),
218                         intval($a->pager['itemspage'])
219                 );
220                 if(! count($r)) {
221                         notice( t('No messages.') . EOL);
222                         return $o;
223                 }
224
225                 $tpl = load_view_file('view/mail_list.tpl');
226                 foreach($r as $rr) {
227                         $o .= replace_macros($tpl, array(
228                                 '$id' => $rr['id'],
229                                 '$from_name' =>$rr['from-name'],
230                                 '$from_url' => $a->get_baseurl() . '/redir/' . $rr['contact-id'],
231                                 '$sparkle' => ' sparkle',
232                                 '$from_photo' => $rr['thumb'],
233                                 '$subject' => (($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>'),
234                                 '$delete' => t('Delete conversation'),
235                                 '$body' => $rr['body'],
236                                 '$to_name' => $rr['name'],
237                                 '$date' => datetime_convert('UTC',date_default_timezone_get(),$rr['mailcreated'],'D, d M Y - g:i A')
238                         ));
239                 }
240                 $o .= paginate($a);     
241                 return $o;
242         }
243
244         if(($a->argc > 1) && (intval($a->argv[1]))) {
245
246                 $o .= $header;
247
248                 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb` 
249                         FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id` 
250                         WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
251                         intval(local_user()),
252                         intval($a->argv[1])
253                 );
254                 if(count($r)) { 
255                         $contact_id = $r[0]['contact-id'];
256                         $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb` 
257                                 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id` 
258                                 WHERE `mail`.`uid` = %d AND `mail`.`parent-uri` = '%s' ORDER BY `mail`.`created` ASC",
259                                 intval(local_user()),
260                                 dbesc($r[0]['parent-uri'])
261                         );
262                 }
263                 if(! count($messages)) {
264                         notice( t('Message not available.') . EOL );
265                         return $o;
266                 }
267
268                 $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
269                         dbesc($r[0]['parent-uri']),
270                         intval(local_user())
271                 );
272
273                 require_once("include/bbcode.php");
274
275                 $tpl = load_view_file('view/msg-header.tpl');
276         
277                 $a->page['htmlhead'] .= replace_macros($tpl, array(
278                         '$nickname' => $a->user['nickname'],
279                         '$baseurl' => $a->get_baseurl()
280                 ));
281
282
283                 $tpl = load_view_file('view/mail_conv.tpl');
284                 foreach($messages as $message) {
285                         if($message['from-url'] == $myprofile) {
286                                 $from_url = $myprofile;
287                                 $sparkle = '';
288                         }
289                         else {
290                                 $from_url = $a->get_baseurl() . '/redir/' . $message['contact-id'];
291                                 $sparkle = ' sparkle';
292                         }
293                         $o .= replace_macros($tpl, array(
294                                 '$id' => $message['id'],
295                                 '$from_name' =>$message['from-name'],
296                                 '$from_url' => $from_url,
297                                 '$sparkle' => $sparkle,
298                                 '$from_photo' => $message['from-photo'],
299                                 '$subject' => $message['title'],
300                                 '$body' => smilies(bbcode($message['body'])),
301                                 '$delete' => t('Delete message'),
302                                 '$to_name' => $message['name'],
303                                 '$date' => datetime_convert('UTC',date_default_timezone_get(),$message['created'],'D, d M Y - g:i A')
304                         ));
305                                 
306                 }
307                 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
308                 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
309                 $tpl = load_view_file('view/prv_message.tpl');
310                 $o .= replace_macros($tpl,array(
311                         '$header' => t('Send Reply'),
312                         '$to' => t('To:'),
313                         '$subject' => t('Subject:'),
314                         '$subjtxt' => $message['title'],
315                         '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
316                         '$yourmessage' => t('Your message:'),
317                         '$select' => $select,
318                         '$parent' => $parent,
319                         '$upload' => t('Upload photo'),
320                         '$insert' => t('Insert web link'),
321                         '$wait' => t('Please wait')
322
323                 ));
324
325                 return $o;
326         }
327
328 }