]> git.mxchange.org Git - friendica.git/blob - mod/wall_upload.php
Merge pull request #12065 from Quix0r/cleanup/renaming-more-logging
[friendica.git] / mod / wall_upload.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
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\System;
31 use Friendica\Database\DBA;
32 use Friendica\DI;
33 use Friendica\Model\Photo;
34 use Friendica\Model\User;
35 use Friendica\Module\BaseApi;
36 use Friendica\Object\Image;
37 use Friendica\Util\Images;
38 use Friendica\Util\Strings;
39
40 function wall_upload_post(App $a, $desktopmode = true)
41 {
42         Logger::info('wall upload: starting new upload');
43
44         $isJson = (!empty($_GET['response']) && $_GET['response'] == 'json');
45         $album  = trim($_GET['album'] ?? '');
46
47         if (DI::args()->getArgc() > 1) {
48                 if (empty($_FILES['media'])) {
49                         $nick = DI::args()->getArgv()[1];
50                         $user = DBA::selectFirst('owner-view', ['id', 'uid', 'nickname', 'page-flags'], ['nickname' => $nick, 'blocked' => false]);
51                         if (!DBA::isResult($user)) {
52                                 Logger::warning('wall upload: user instance is not valid', ['user' => $user, 'nickname' => $nick]);
53                                 if ($isJson) {
54                                         System::jsonExit(['error' => DI::l10n()->t('Invalid request.')]);
55                                 }
56                                 return;
57                         }
58                 } else {
59                         $user = DBA::selectFirst('owner-view', ['id', 'uid', 'nickname', 'page-flags'], ['uid' => BaseApi::getCurrentUserID(), 'blocked' => false]);
60                 }
61         } else {
62                 Logger:warning('Argument count is zero or one (invalid)');
63                 if ($isJson) {
64                         System::jsonExit(['error' => DI::l10n()->t('Invalid request.')]);
65                 }
66                 return;
67         }
68
69         /*
70          * Setup permissions structures
71          */
72         $can_post = false;
73         $visitor  = 0;
74
75         $page_owner_uid  = $user['uid'];
76         $default_cid     = $user['id'];
77         $page_owner_nick = $user['nickname'];
78         $community_page  = ($user['page-flags'] == User::PAGE_FLAGS_COMMUNITY);
79
80         if ((DI::userSession()->getLocalUserId()) && (DI::userSession()->getLocalUserId() == $page_owner_uid)) {
81                 $can_post = true;
82         } elseif ($community_page && !empty(DI::userSession()->getRemoteContactID($page_owner_uid))) {
83                 $contact_id = DI::userSession()->getRemoteContactID($page_owner_uid);
84                 $can_post   = DBA::exists('contact', ['blocked' => false, 'pending' => false, 'id' => $contact_id, 'uid' => $page_owner_uid]);
85                 $visitor    = $contact_id;
86         }
87
88         if (!$can_post) {
89                 Logger::warning('No permission to upload files', ['contact_id' => $contact_id, 'page_owner_uid' => $page_owner_uid]);
90                 $msg = DI::l10n()->t('Permission denied.');
91                 if ($isJson) {
92                         System::jsonExit(['error' => $msg]);
93                 }
94                 DI::sysmsg()->addNotice($msg);
95                 System::exit();
96         }
97
98         if (empty($_FILES['userfile']) && empty($_FILES['media'])) {
99                 Logger::warning('Empty "userfile" and "media" field');
100                 if ($isJson) {
101                         System::jsonExit(['error' => DI::l10n()->t('Invalid request.')]);
102                 }
103                 System::exit();
104         }
105
106         $src      = '';
107         $filename = '';
108         $filesize = 0;
109         $filetype = '';
110
111         if (!empty($_FILES['userfile'])) {
112                 $src      = $_FILES['userfile']['tmp_name'];
113                 $filename = basename($_FILES['userfile']['name']);
114                 $filesize = intval($_FILES['userfile']['size']);
115                 $filetype = $_FILES['userfile']['type'];
116         } elseif (!empty($_FILES['media'])) {
117                 if (!empty($_FILES['media']['tmp_name'])) {
118                         if (is_array($_FILES['media']['tmp_name'])) {
119                                 $src = $_FILES['media']['tmp_name'][0];
120                         } else {
121                                 $src = $_FILES['media']['tmp_name'];
122                         }
123                 }
124
125                 if (!empty($_FILES['media']['name'])) {
126                         if (is_array($_FILES['media']['name'])) {
127                                 $filename = basename($_FILES['media']['name'][0]);
128                         } else {
129                                 $filename = basename($_FILES['media']['name']);
130                         }
131                 }
132
133                 if (!empty($_FILES['media']['size'])) {
134                         if (is_array($_FILES['media']['size'])) {
135                                 $filesize = intval($_FILES['media']['size'][0]);
136                         } else {
137                                 $filesize = intval($_FILES['media']['size']);
138                         }
139                 }
140
141                 if (!empty($_FILES['media']['type'])) {
142                         if (is_array($_FILES['media']['type'])) {
143                                 $filetype = $_FILES['media']['type'][0];
144                         } else {
145                                 $filetype = $_FILES['media']['type'];
146                         }
147                 }
148         }
149
150         if ($src == '') {
151                 Logger::warning('File source (temporary file) cannot be determined');
152                 $msg = DI::l10n()->t('Invalid request.');
153                 if ($isJson) {
154                         System::jsonExit(['error' => $msg]);
155                 }
156                 DI::sysmsg()->addNotice($msg);
157                 System::exit();
158         }
159
160         $filetype = Images::getMimeTypeBySource($src, $filename, $filetype);
161
162         Logger::info('File upload:', [
163                 'src'      => $src,
164                 'filename' => $filename,
165                 'filesize' => $filesize,
166                 'filetype' => $filetype,
167         ]);
168
169         $imagedata = @file_get_contents($src);
170         $image     = new Image($imagedata, $filetype);
171
172         if (!$image->isValid()) {
173                 $msg = DI::l10n()->t('Unable to process image.');
174                 Logger::warning($msg, ['imagedata[]' => gettype($imagedata), 'filetype' => $filetype]);
175                 @unlink($src);
176                 if ($isJson) {
177                         System::jsonExit(['error' => $msg]);
178                 } else {
179                         echo $msg . '<br />';
180                 }
181                 System::exit();
182         }
183
184         $image->orient($src);
185         @unlink($src);
186
187         $max_length = DI::config()->get('system', 'max_image_length');
188         if ($max_length > 0) {
189                 $image->scaleDown($max_length);
190                 $filesize = strlen($image->asString());
191                 Logger::info('File upload: Scaling picture to new size', ['max_length' => $max_length]);
192         }
193
194         $width  = $image->getWidth();
195         $height = $image->getHeight();
196
197         $maximagesize = DI::config()->get('system', 'maximagesize');
198
199         if (!empty($maximagesize) && ($filesize > $maximagesize)) {
200                 // Scale down to multiples of 640 until the maximum size isn't exceeded anymore
201                 foreach ([5120, 2560, 1280, 640] as $pixels) {
202                         if (($filesize > $maximagesize) && (max($width, $height) > $pixels)) {
203                                 Logger::info('Resize', ['size' => $filesize, 'width' => $width, 'height' => $height, 'max' => $maximagesize, 'pixels' => $pixels]);
204                                 $image->scaleDown($pixels);
205                                 $filesize = strlen($image->asString());
206                                 $width    = $image->getWidth();
207                                 $height   = $image->getHeight();
208                         }
209                 }
210                 if ($filesize > $maximagesize) {
211                         Logger::notice('Image size is too big', ['size' => $filesize, 'max' => $maximagesize]);
212                         $msg = DI::l10n()->t('Image exceeds size limit of %s', Strings::formatBytes($maximagesize));
213                         @unlink($src);
214                         if ($isJson) {
215                                 System::jsonExit(['error' => $msg]);
216                         } else {
217                                 echo  $msg . '<br />';
218                         }
219                         System::exit();
220                 }
221         }
222
223         $resource_id = Photo::newResource();
224
225         $smallest = 0;
226
227         // If we don't have an album name use the Wall Photos album
228         if (!strlen($album)) {
229                 $album = DI::l10n()->t('Wall Photos');
230         }
231
232         $defperm = '<' . $default_cid . '>';
233
234         $r = Photo::store($image, $page_owner_uid, $visitor, $resource_id, $filename, $album, 0, Photo::DEFAULT, $defperm);
235
236         if (!$r) {
237                 $msg = DI::l10n()->t('Image upload failed.');
238                 Logger::warning('Photo::store() failed', ['r' => $r]);
239                 if ($isJson) {
240                         System::jsonExit(['error' => $msg]);
241                 } else {
242                         echo  $msg . '<br />';
243                 }
244                 System::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, Photo::DEFAULT, $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, Photo::DEFAULT, $defperm);
258                 if ($r && ($smallest == 0)) {
259                         $smallest = 2;
260                 }
261         }
262
263         if (!$desktopmode) {
264                 $photo = Photo::selectFirst(['id', 'datasize', 'width', 'height', 'type'], ['resource-id' => $resource_id], ['order' => ['width']]);
265                 if (!$photo) {
266                         Logger::warning('Cannot find photo in database', ['resource-id' => $resource_id]);
267                         if ($isJson) {
268                                 System::jsonExit(['error' => 'Cannot find photo']);
269                         }
270                         return false;
271                 }
272
273                 $picture = [
274                         'id'        => $photo['id'],
275                         'size'      => $photo['datasize'],
276                         'width'     => $photo['width'],
277                         'height'    => $photo['height'],
278                         'type'      => $photo['type'],
279                         'albumpage' => DI::baseUrl() . '/photos/' . $page_owner_nick . '/image/' . $resource_id,
280                         'picture'   => DI::baseUrl() . "/photo/{$resource_id}-0." . $image->getExt(),
281                         'preview'   => DI::baseUrl() . "/photo/{$resource_id}-{$smallest}." . $image->getExt(),
282                 ];
283
284                 if ($isJson) {
285                         System::jsonExit(['picture' => $picture]);
286                 }
287                 Logger::info('upload done');
288                 return $picture;
289         }
290
291         Logger::info('upload done');
292
293         if ($isJson) {
294                 System::jsonExit(['ok' => true]);
295         }
296
297         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";
298         System::exit();
299         // NOTREACHED
300 }