]> git.mxchange.org Git - friendica.git/blob - mod/wall_upload.php
Use short form array syntax everywhere
[friendica.git] / mod / wall_upload.php
1 <?php
2
3 /**
4  * @file mod/wall_upload.php
5  * @brief Module for uploading a picture to the profile wall
6  *
7  * By default the picture will be stored in the photo album with the name Wall Photos.
8  * You can specify a different album by adding an optional query string "album="
9  * to the url
10  */
11
12 use Friendica\App;
13 use Friendica\Core\System;
14 use Friendica\Core\Config;
15 use Friendica\Database\DBM;
16 use Friendica\Model\Photo;
17 use Friendica\Object\Image;
18
19 function wall_upload_post(App $a, $desktopmode = true) {
20
21         logger("wall upload: starting new upload", LOGGER_DEBUG);
22
23         $r_json = (x($_GET, 'response') && $_GET['response'] == 'json');
24         $album = (x($_GET, 'album') ? notags(trim($_GET['album'])) : '');
25
26         if ($a->argc > 1) {
27                 if (! x($_FILES, 'media')) {
28                         $nick = $a->argv[1];
29                         $r = q("SELECT `user`.*, `contact`.`id` FROM `user`
30                                 INNER JOIN `contact` on `user`.`uid` = `contact`.`uid`
31                                 WHERE `user`.`nickname` = '%s' AND `user`.`blocked` = 0
32                                 AND `contact`.`self` = 1 LIMIT 1",
33                                 dbesc($nick)
34                         );
35
36                         if (! DBM::is_result($r)) {
37                                 if ($r_json) {
38                                         echo json_encode(['error'=>t('Invalid request.')]);
39                                         killme();
40                                 }
41                                 return;
42                         }
43                 } else {
44                         $user_info = api_get_user($a);
45                         $r = q("SELECT `user`.*, `contact`.`id` FROM `user`
46                                 INNER JOIN `contact` on `user`.`uid` = `contact`.`uid`
47                                 WHERE `user`.`nickname` = '%s' AND `user`.`blocked` = 0
48                                 AND `contact`.`self` = 1 LIMIT 1",
49                                 dbesc($user_info['screen_name'])
50                         );
51                 }
52         } else {
53                 if ($r_json) {
54                         echo json_encode(['error'=>t('Invalid request.')]);
55                         killme();
56                 }
57                 return;
58         }
59
60         /*
61          * Setup permissions structures
62          */
63         $can_post  = false;
64         $visitor   = 0;
65
66         $page_owner_uid   = $r[0]['uid'];
67         $default_cid      = $r[0]['id'];
68         $page_owner_nick  = $r[0]['nickname'];
69         $community_page   = (($r[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
70
71         if ((local_user()) && (local_user() == $page_owner_uid)) {
72                 $can_post = true;
73         } else {
74                 if ($community_page && remote_user()) {
75                         $contact_id = 0;
76                         if (is_array($_SESSION['remote'])) {
77                                 foreach ($_SESSION['remote'] as $v) {
78                                         if ($v['uid'] == $page_owner_uid) {
79                                                 $contact_id = $v['cid'];
80                                                 break;
81                                         }
82                                 }
83                         }
84
85                         if ($contact_id) {
86                                 $r = q("SELECT `uid` FROM `contact`
87                                         WHERE `blocked` = 0 AND `pending` = 0
88                                         AND `id` = %d AND `uid` = %d LIMIT 1",
89                                         intval($contact_id),
90                                         intval($page_owner_uid)
91                                 );
92                                 if (DBM::is_result($r)) {
93                                         $can_post = true;
94                                         $visitor = $contact_id;
95                                 }
96                         }
97                 }
98         }
99
100
101         if (! $can_post) {
102                 if ($r_json) {
103                         echo json_encode(['error'=>t('Permission denied.')]);
104                         killme();
105                 }
106                 notice(t('Permission denied.') . EOL);
107                 killme();
108         }
109
110         if (! x($_FILES, 'userfile') && ! x($_FILES, 'media')) {
111                 if ($r_json) {
112                         echo json_encode(['error'=>t('Invalid request.')]);
113                 }
114                 killme();
115         }
116
117         $src = "";
118         if (x($_FILES, 'userfile')) {
119                 $src      = $_FILES['userfile']['tmp_name'];
120                 $filename = basename($_FILES['userfile']['name']);
121                 $filesize = intval($_FILES['userfile']['size']);
122                 $filetype = $_FILES['userfile']['type'];
123
124         } elseif (x($_FILES, 'media')) {
125                 if (is_array($_FILES['media']['tmp_name'])) {
126                         $src = $_FILES['media']['tmp_name'][0];
127                 } else {
128                         $src = $_FILES['media']['tmp_name'];
129                 }
130
131                 if (is_array($_FILES['media']['name'])) {
132                         $filename = basename($_FILES['media']['name'][0]);
133                 } else {
134                         $filename = basename($_FILES['media']['name']);
135                 }
136
137                 if (is_array($_FILES['media']['size'])) {
138                         $filesize = intval($_FILES['media']['size'][0]);
139                 } else {
140                         $filesize = intval($_FILES['media']['size']);
141                 }
142
143                 if (is_array($_FILES['media']['type'])) {
144                         $filetype = $_FILES['media']['type'][0];
145                 } else {
146                         $filetype = $_FILES['media']['type'];
147                 }
148         }
149
150         if ($src=="") {
151                 if ($r_json) {
152                         echo json_encode(['error'=>t('Invalid request.')]);
153                         killme();
154                 }
155                 notice(t('Invalid request.').EOL);
156                 killme();
157         }
158
159         // This is a special treatment for picture upload from Twidere
160         if (($filename == "octet-stream") && ($filetype != "")) {
161                 $filename = $filetype;
162                 $filetype = "";
163         }
164
165         if ($filetype == "") {
166                 $filetype = Image::guessType($filename);
167         }
168
169         // If there is a temp name, then do a manual check
170         // This is more reliable than the provided value
171
172         $imagedata = getimagesize($src);
173         if ($imagedata) {
174                 $filetype = $imagedata['mime'];
175         }
176
177         logger("File upload src: " . $src . " - filename: " . $filename .
178                 " - size: " . $filesize . " - type: " . $filetype, LOGGER_DEBUG);
179
180         $maximagesize = Config::get('system', 'maximagesize');
181
182         if (($maximagesize) && ($filesize > $maximagesize)) {
183                 $msg = sprintf(t('Image exceeds size limit of %s'), formatBytes($maximagesize));
184                 if ($r_json) {
185                         echo json_encode(['error'=>$msg]);
186                 } else {
187                         echo  $msg. EOL;
188                 }
189                 @unlink($src);
190                 killme();
191         }
192
193         $imagedata = @file_get_contents($src);
194         $Image = new Image($imagedata, $filetype);
195
196         if (! $Image->isValid()) {
197                 $msg = t('Unable to process image.');
198                 if ($r_json) {
199                         echo json_encode(['error'=>$msg]);
200                 } else {
201                         echo  $msg. EOL;
202                 }
203                 @unlink($src);
204                 killme();
205         }
206
207         $Image->orient($src);
208         @unlink($src);
209
210         $max_length = Config::get('system', 'max_image_length');
211         if (! $max_length) {
212                 $max_length = MAX_IMAGE_LENGTH;
213         }
214         if ($max_length > 0) {
215                 $Image->scaleDown($max_length);
216                 logger("File upload: Scaling picture to new size " . $max_length, LOGGER_DEBUG);
217         }
218
219         $width = $Image->getWidth();
220         $height = $Image->getHeight();
221
222         $hash = photo_new_resource();
223
224         $smallest = 0;
225
226         // If we don't have an album name use the Wall Photos album
227         if (! strlen($album)) {
228                 $album = t('Wall Photos');
229         }
230
231         $defperm = '<' . $default_cid . '>';
232
233         $r = Photo::store($Image, $page_owner_uid, $visitor, $hash, $filename, $album, 0, 0, $defperm);
234
235         if (! $r) {
236                 $msg = t('Image upload failed.');
237                 if ($r_json) {
238                         echo json_encode(['error'=>$msg]);
239                 } else {
240                         echo  $msg. EOL;
241                 }
242                 killme();
243         }
244
245         if ($width > 640 || $height > 640) {
246                 $Image->scaleDown(640);
247                 $r = Photo::store($Image, $page_owner_uid, $visitor, $hash, $filename, $album, 1, 0, $defperm);
248                 if ($r) {
249                         $smallest = 1;
250                 }
251         }
252
253         if ($width > 320 || $height > 320) {
254                 $Image->scaleDown(320);
255                 $r = Photo::store($Image, $page_owner_uid, $visitor, $hash, $filename, $album, 2, 0, $defperm);
256                 if ($r && ($smallest == 0)) {
257                         $smallest = 2;
258                 }
259         }
260
261         $basename = basename($filename);
262
263         if (!$desktopmode) {
264                 $r = q("SELECT `id`, `datasize`, `width`, `height`, `type` FROM `photo`
265                         WHERE `resource-id` = '%s'
266                         ORDER BY `width` DESC LIMIT 1",
267                         $hash
268                 );
269                 if (!$r) {
270                         if ($r_json) {
271                                 echo json_encode(['error'=>'']);
272                                 killme();
273                         }
274                         return false;
275                 }
276                 $picture = [];
277
278                 $picture["id"]        = $r[0]["id"];
279                 $picture["size"]      = $r[0]["datasize"];
280                 $picture["width"]     = $r[0]["width"];
281                 $picture["height"]    = $r[0]["height"];
282                 $picture["type"]      = $r[0]["type"];
283                 $picture["albumpage"] = System::baseUrl() . '/photos/' . $page_owner_nick . '/image/' . $hash;
284                 $picture["picture"]   = System::baseUrl() . "/photo/{$hash}-0." . $Image->getExt();
285                 $picture["preview"]   = System::baseUrl() . "/photo/{$hash}-{$smallest}." . $Image->getExt();
286
287                 if ($r_json) {
288                         echo json_encode(['picture'=>$picture]);
289                         killme();
290                 }
291                 return $picture;
292         }
293
294
295         if ($r_json) {
296                 echo json_encode(['ok'=>true]);
297                 killme();
298         }
299
300 /* mod Waitman Gobble NO WARRANTY */
301         // if we get the signal then return the image url info in BBCODE
302         if ($_REQUEST['hush']!='yeah') {
303                 echo  "\n\n" . '[url=' . System::baseUrl() . '/photos/' . $page_owner_nick . '/image/' . $hash . '][img]' . System::baseUrl() . "/photo/{$hash}-{$smallest}.".$Image->getExt()."[/img][/url]\n\n";
304         } else {
305                 $m = '[url='.System::baseUrl().'/photos/'.$page_owner_nick.'/image/'.$hash.'][img]'.System::baseUrl()."/photo/{$hash}-{$smallest}.".$Image->getExt()."[/img][/url]";
306                 return($m);
307         }
308 /* mod Waitman Gobble NO WARRANTY */
309
310         killme();
311         // NOTREACHED
312 }