]> git.mxchange.org Git - friendica.git/blob - mod/wall_upload.php
Remove album name bin2hex conversion in file browser
[friendica.git] / mod / wall_upload.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  * Module for uploading a picture to the profile wall
21  *
22  * By default the picture will be stored in the photo album with the name Wall Photos.
23  * You can specify a different album by adding an optional query string "album="
24  * to the url
25  *
26  */
27
28 use Friendica\App;
29 use Friendica\Core\Logger;
30 use Friendica\Core\Session;
31 use Friendica\Database\DBA;
32 use Friendica\DI;
33 use Friendica\Model\Photo;
34 use Friendica\Model\User;
35 use Friendica\Object\Image;
36 use Friendica\Util\Images;
37 use Friendica\Util\Strings;
38
39 function wall_upload_post(App $a, $desktopmode = true)
40 {
41         Logger::log("wall upload: starting new upload", Logger::DEBUG);
42
43         $r_json = (!empty($_GET['response']) && $_GET['response'] == 'json');
44         $album = trim($_GET['album'] ?? '');
45
46         if ($a->argc > 1) {
47                 if (empty($_FILES['media'])) {
48                         $nick = $a->argv[1];
49                         $r = q("SELECT `user`.*, `contact`.`id` FROM `user`
50                                 INNER JOIN `contact` on `user`.`uid` = `contact`.`uid`
51                                 WHERE `user`.`nickname` = '%s' AND `user`.`blocked` = 0
52                                 AND `contact`.`self` = 1 LIMIT 1",
53                                 DBA::escape($nick)
54                         );
55
56                         if (!DBA::isResult($r)) {
57                                 if ($r_json) {
58                                         echo json_encode(['error' => DI::l10n()->t('Invalid request.')]);
59                                         exit();
60                                 }
61                                 return;
62                         }
63                 } else {
64                         $user_info = api_get_user($a);
65                         $r = q("SELECT `user`.*, `contact`.`id` FROM `user`
66                                 INNER JOIN `contact` on `user`.`uid` = `contact`.`uid`
67                                 WHERE `user`.`nickname` = '%s' AND `user`.`blocked` = 0
68                                 AND `contact`.`self` = 1 LIMIT 1",
69                                 DBA::escape($user_info['screen_name'])
70                         );
71                 }
72         } else {
73                 if ($r_json) {
74                         echo json_encode(['error' => DI::l10n()->t('Invalid request.')]);
75                         exit();
76                 }
77                 return;
78         }
79
80         /*
81          * Setup permissions structures
82          */
83         $can_post  = false;
84         $visitor   = 0;
85
86         $page_owner_uid   = $r[0]['uid'];
87         $default_cid      = $r[0]['id'];
88         $page_owner_nick  = $r[0]['nickname'];
89         $community_page   = (($r[0]['page-flags'] == User::PAGE_FLAGS_COMMUNITY) ? true : false);
90
91         if ((local_user()) && (local_user() == $page_owner_uid)) {
92                 $can_post = true;
93         } elseif ($community_page && !empty(Session::getRemoteContactID($page_owner_uid))) {
94                 $contact_id = Session::getRemoteContactID($page_owner_uid);
95
96                 $r = q("SELECT `uid` FROM `contact`
97                         WHERE `blocked` = 0 AND `pending` = 0
98                         AND `id` = %d AND `uid` = %d LIMIT 1",
99                         intval($contact_id),
100                         intval($page_owner_uid)
101                 );
102                 if (DBA::isResult($r)) {
103                         $can_post = true;
104                         $visitor = $contact_id;
105                 }
106         }
107
108         if (!$can_post) {
109                 if ($r_json) {
110                         echo json_encode(['error' => DI::l10n()->t('Permission denied.')]);
111                         exit();
112                 }
113                 notice(DI::l10n()->t('Permission denied.') . EOL);
114                 exit();
115         }
116
117         if (empty($_FILES['userfile']) && empty($_FILES['media'])) {
118                 if ($r_json) {
119                         echo json_encode(['error' => DI::l10n()->t('Invalid request.')]);
120                 }
121                 exit();
122         }
123
124         $src = '';
125         $filename = '';
126         $filesize = 0;
127         $filetype = '';
128         if (!empty($_FILES['userfile'])) {
129                 $src      = $_FILES['userfile']['tmp_name'];
130                 $filename = basename($_FILES['userfile']['name']);
131                 $filesize = intval($_FILES['userfile']['size']);
132                 $filetype = $_FILES['userfile']['type'];
133
134         } elseif (!empty($_FILES['media'])) {
135                 if (!empty($_FILES['media']['tmp_name'])) {
136                         if (is_array($_FILES['media']['tmp_name'])) {
137                                 $src = $_FILES['media']['tmp_name'][0];
138                         } else {
139                                 $src = $_FILES['media']['tmp_name'];
140                         }
141                 }
142
143                 if (!empty($_FILES['media']['name'])) {
144                         if (is_array($_FILES['media']['name'])) {
145                                 $filename = basename($_FILES['media']['name'][0]);
146                         } else {
147                                 $filename = basename($_FILES['media']['name']);
148                         }
149                 }
150
151                 if (!empty($_FILES['media']['size'])) {
152                         if (is_array($_FILES['media']['size'])) {
153                                 $filesize = intval($_FILES['media']['size'][0]);
154                         } else {
155                                 $filesize = intval($_FILES['media']['size']);
156                         }
157                 }
158
159                 if (!empty($_FILES['media']['type'])) {
160                         if (is_array($_FILES['media']['type'])) {
161                                 $filetype = $_FILES['media']['type'][0];
162                         } else {
163                                 $filetype = $_FILES['media']['type'];
164                         }
165                 }
166         }
167
168         if ($src == "") {
169                 if ($r_json) {
170                         echo json_encode(['error' => DI::l10n()->t('Invalid request.')]);
171                         exit();
172                 }
173                 notice(DI::l10n()->t('Invalid request.').EOL);
174                 exit();
175         }
176
177         $filetype = Images::getMimeTypeBySource($src, $filename, $filetype);
178
179         Logger::log("File upload src: " . $src . " - filename: " . $filename .
180                 " - size: " . $filesize . " - type: " . $filetype, Logger::DEBUG);
181
182         $maximagesize = DI::config()->get('system', 'maximagesize');
183
184         if (($maximagesize) && ($filesize > $maximagesize)) {
185                 $msg = DI::l10n()->t('Image exceeds size limit of %s', Strings::formatBytes($maximagesize));
186                 if ($r_json) {
187                         echo json_encode(['error' => $msg]);
188                 } else {
189                         echo  $msg. EOL;
190                 }
191                 @unlink($src);
192                 exit();
193         }
194
195         $imagedata = @file_get_contents($src);
196         $Image = new Image($imagedata, $filetype);
197
198         if (!$Image->isValid()) {
199                 $msg = DI::l10n()->t('Unable to process image.');
200                 if ($r_json) {
201                         echo json_encode(['error' => $msg]);
202                 } else {
203                         echo  $msg. EOL;
204                 }
205                 @unlink($src);
206                 exit();
207         }
208
209         $Image->orient($src);
210         @unlink($src);
211
212         $max_length = DI::config()->get('system', 'max_image_length');
213         if (!$max_length) {
214                 $max_length = MAX_IMAGE_LENGTH;
215         }
216         if ($max_length > 0) {
217                 $Image->scaleDown($max_length);
218                 Logger::log("File upload: Scaling picture to new size " . $max_length, Logger::DEBUG);
219         }
220
221         $width = $Image->getWidth();
222         $height = $Image->getHeight();
223
224         $resource_id = Photo::newResource();
225
226         $smallest = 0;
227
228         // If we don't have an album name use the Wall Photos album
229         if (!strlen($album)) {
230                 $album = DI::l10n()->t('Wall Photos');
231         }
232
233         $defperm = '<' . $default_cid . '>';
234
235         $r = Photo::store($Image, $page_owner_uid, $visitor, $resource_id, $filename, $album, 0, 0, $defperm);
236
237         if (!$r) {
238                 $msg = DI::l10n()->t('Image upload failed.');
239                 if ($r_json) {
240                         echo json_encode(['error' => $msg]);
241                 } else {
242                         echo  $msg. EOL;
243                 }
244                 exit();
245         }
246
247         if ($width > 640 || $height > 640) {
248                 $Image->scaleDown(640);
249                 $r = Photo::store($Image, $page_owner_uid, $visitor, $resource_id, $filename, $album, 1, 0, $defperm);
250                 if ($r) {
251                         $smallest = 1;
252                 }
253         }
254
255         if ($width > 320 || $height > 320) {
256                 $Image->scaleDown(320);
257                 $r = Photo::store($Image, $page_owner_uid, $visitor, $resource_id, $filename, $album, 2, 0, $defperm);
258                 if ($r && ($smallest == 0)) {
259                         $smallest = 2;
260                 }
261         }
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                         $resource_id
268                 );
269                 if (!$r) {
270                         if ($r_json) {
271                                 echo json_encode(['error' => '']);
272                                 exit();
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"] = DI::baseUrl() . '/photos/' . $page_owner_nick . '/image/' . $resource_id;
284                 $picture["picture"]   = DI::baseUrl() . "/photo/{$resource_id}-0." . $Image->getExt();
285                 $picture["preview"]   = DI::baseUrl() . "/photo/{$resource_id}-{$smallest}." . $Image->getExt();
286
287                 if ($r_json) {
288                         echo json_encode(['picture' => $picture]);
289                         exit();
290                 }
291                 Logger::log("upload done", Logger::DEBUG);
292                 return $picture;
293         }
294
295         Logger::log("upload done", Logger::DEBUG);
296
297         if ($r_json) {
298                 echo json_encode(['ok' => true]);
299                 exit();
300         }
301
302         echo  "\n\n" . '[url=' . DI::baseUrl() . '/photos/' . $page_owner_nick . '/image/' . $resource_id . '][img]' . DI::baseUrl() . "/photo/{$resource_id}-{$smallest}.".$Image->getExt()."[/img][/url]\n\n";
303         exit();
304         // NOTREACHED
305 }