]> git.mxchange.org Git - friendica.git/blob - mod/wall_attach.php
Merge pull request #2166 from rabuzarus/1012_vier-forum
[friendica.git] / mod / wall_attach.php
1 <?php
2
3 require_once('include/attach.php');
4 require_once('include/datetime.php');
5
6 function wall_attach_post(&$a) {
7
8         $r_json = (x($_GET,'response') && $_GET['response']=='json');
9
10         if($a->argc > 1) {
11                 $nick = $a->argv[1];
12                 $r = q("SELECT `user`.*, `contact`.`id` FROM `user` LEFT JOIN `contact` on `user`.`uid` = `contact`.`uid`  WHERE `user`.`nickname` = '%s' AND `user`.`blocked` = 0 and `contact`.`self` = 1 LIMIT 1",
13                         dbesc($nick)
14                 );
15                 if(! count($r)){
16                         if ($r_json) {
17                             echo json_encode(array('error'=>t('Invalid request.')));
18                             killme();
19                         }
20                         return;
21         }
22
23         } else {
24                 if ($r_json) {
25                     echo json_encode(array('error'=>t('Invalid request.')));
26                     killme();
27                 }
28                 return;
29     }
30
31         $can_post  = false;
32         $visitor   = 0;
33
34         $page_owner_uid   = $r[0]['uid'];
35         $page_owner_cid   = $r[0]['id'];
36         $page_owner_nick  = $r[0]['nickname'];
37         $community_page   = (($r[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
38
39         if((local_user()) && (local_user() == $page_owner_uid))
40                 $can_post = true;
41         else {
42                 if($community_page && remote_user()) {
43                         $cid = 0;
44                         if(is_array($_SESSION['remote'])) {
45                                 foreach($_SESSION['remote'] as $v) {
46                                         if($v['uid'] == $page_owner_uid) {
47                                                 $cid = $v['cid'];
48                                                 break;
49                                         }
50                                 }
51                         }
52                         if($cid) {
53
54                                 $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
55                                         intval($cid),
56                                         intval($page_owner_uid)
57                                 );
58                                 if(count($r)) {
59                                         $can_post = true;
60                                         $visitor = $cid;
61                                 }
62                         }
63                 }
64         }
65         if(! $can_post) {
66                 if ($r_json) {
67                     echo json_encode(array('error'=>t('Permission denied.')));
68                     killme();
69                 }
70                 notice( t('Permission denied.') . EOL );
71                 killme();
72         }
73
74         if(! x($_FILES,'userfile')) {
75                 if ($r_json) {
76                     echo json_encode(array('error'=>t('Invalid request.')));
77                 }
78                 killme();
79         }
80
81         $src      = $_FILES['userfile']['tmp_name'];
82         $filename = basename($_FILES['userfile']['name']);
83         $filesize = intval($_FILES['userfile']['size']);
84
85         $maxfilesize = get_config('system','maxfilesize');
86
87         /* Found html code written in text field of form,
88          * when trying to upload a file with filesize
89          * greater than upload_max_filesize. Cause is unknown.
90          * Then Filesize gets <= 0.
91          */
92
93         if($filesize <=0) {
94                 $msg = t('Sorry, maybe your upload is bigger than the PHP configuration allows') . EOL .(t('Or - did you try to upload an empty file?'));
95                 if ($r_json) {
96                         echo json_encode(array('error'=>$msg));
97                 } else {
98                         notice( $msg. EOL );
99                 }
100                 @unlink($src);
101                 killme();
102         }
103
104         if(($maxfilesize) && ($filesize > $maxfilesize)) {
105                 $msg = sprintf(t('File exceeds size limit of %s'), formatBytes($maxfilesize));
106                 if ($r_json) {
107                         echo json_encode(array('error'=>$msg));
108                 } else {
109                         echo  $msg. EOL ;
110                 }
111                 @unlink($src);
112                 killme();
113         }
114
115         $r = q("select sum(octet_length(data)) as total from attach where uid = %d ",
116                 intval($page_owner_uid)
117         );
118
119         $limit = service_class_fetch($page_owner_uid,'attach_upload_limit');
120
121         if(($limit !== false) && (($r[0]['total'] + strlen($imagedata)) > $limit)) {
122                 $msg = upgrade_message(true);
123                 if ($r_json) {
124                         echo json_encode(array('error'=>$msg));
125                 } else {
126                         echo  $msg. EOL ;
127                 }
128                 @unlink($src);
129                 killme();
130         }
131
132
133         $filedata = @file_get_contents($src);
134         $mimetype = z_mime_content_type($filename);
135         $hash = get_guid(64);
136         $created = datetime_convert();
137         $r = q("INSERT INTO `attach` ( `uid`, `hash`, `filename`, `filetype`, `filesize`, `data`, `created`, `edited`, `allow_cid`, `allow_gid`,`deny_cid`, `deny_gid` )
138                 VALUES ( %d, '%s', '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s' ) ",
139                 intval($page_owner_uid),
140                 dbesc($hash),
141                 dbesc($filename),
142                 dbesc($mimetype),
143                 intval($filesize),
144                 dbesc($filedata),
145                 dbesc($created),
146                 dbesc($created),
147                 dbesc('<' . $page_owner_cid . '>'),
148                 dbesc(''),
149                 dbesc(''),
150                 dbesc('')
151         );
152
153         @unlink($src);
154
155         if(! $r) {
156                 $msg =  t('File upload failed.');
157                 if ($r_json) {
158                         echo json_encode(array('error'=>$msg));
159                 } else {
160                         echo  $msg. EOL ;
161                 }
162                 killme();
163         }
164
165         $r = q("SELECT `id` FROM `attach` WHERE `uid` = %d AND `created` = '%s' AND `hash` = '%s' LIMIT 1",
166                 intval($page_owner_uid),
167                 dbesc($created),
168                 dbesc($hash)
169         );
170
171         if(! count($r)) {
172                 $msg = t('File upload failed.');
173                 if ($r_json) {
174                         echo json_encode(array('error'=>$msg));
175                 } else {
176                         echo  $msg. EOL ;
177                 }
178                 killme();
179         }
180
181         if ($r_json) {
182             echo json_encode(array('ok'=>true));
183             killme();
184         }
185
186         $lf = "\n";
187
188         echo  $lf . $lf . '[attachment]' . $r[0]['id'] . '[/attachment]' . $lf;
189
190         killme();
191         // NOTREACHED
192 }