]> git.mxchange.org Git - friendica.git/blob - mod/wall_upload.php
Merge pull request #10908 from annando/logging
[friendica.git] / mod / wall_upload.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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\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::info("wall upload: starting new upload");
42
43         $r_json = (!empty($_GET['response']) && $_GET['response'] == 'json');
44         $album = trim($_GET['album'] ?? '');
45
46         if (DI::args()->getArgc() > 1) {
47                 if (empty($_FILES['media'])) {
48                         $nick = DI::args()->getArgv()[1];                       
49                         $user = DBA::selectFirst('owner-view', ['id', 'uid', 'nickname', 'page-flags'], ['nickname' => $nick, 'blocked' => false]);
50                         if (!DBA::isResult($user)) {
51                                 if ($r_json) {
52                                         echo json_encode(['error' => DI::l10n()->t('Invalid request.')]);
53                                         exit();
54                                 }
55                                 return;
56                         }
57                 } else {
58                         $user_info = api_get_user($a);
59                         $user = DBA::selectFirst('owner-view', ['id', 'uid', 'nickname', 'page-flags'], ['nickname' => $user_info['screen_name'], 'blocked' => false]);
60                 }
61         } else {
62                 if ($r_json) {
63                         echo json_encode(['error' => DI::l10n()->t('Invalid request.')]);
64                         exit();
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) ? true : false);
79
80         if ((local_user()) && (local_user() == $page_owner_uid)) {
81                 $can_post = true;
82         } elseif ($community_page && !empty(Session::getRemoteContactID($page_owner_uid))) {
83                 $contact_id = Session::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                 if ($r_json) {
90                         echo json_encode(['error' => DI::l10n()->t('Permission denied.')]);
91                         exit();
92                 }
93                 notice(DI::l10n()->t('Permission denied.'));
94                 exit();
95         }
96
97         if (empty($_FILES['userfile']) && empty($_FILES['media'])) {
98                 if ($r_json) {
99                         echo json_encode(['error' => DI::l10n()->t('Invalid request.')]);
100                 }
101                 exit();
102         }
103
104         $src = '';
105         $filename = '';
106         $filesize = 0;
107         $filetype = '';
108         if (!empty($_FILES['userfile'])) {
109                 $src      = $_FILES['userfile']['tmp_name'];
110                 $filename = basename($_FILES['userfile']['name']);
111                 $filesize = intval($_FILES['userfile']['size']);
112                 $filetype = $_FILES['userfile']['type'];
113
114         } elseif (!empty($_FILES['media'])) {
115                 if (!empty($_FILES['media']['tmp_name'])) {
116                         if (is_array($_FILES['media']['tmp_name'])) {
117                                 $src = $_FILES['media']['tmp_name'][0];
118                         } else {
119                                 $src = $_FILES['media']['tmp_name'];
120                         }
121                 }
122
123                 if (!empty($_FILES['media']['name'])) {
124                         if (is_array($_FILES['media']['name'])) {
125                                 $filename = basename($_FILES['media']['name'][0]);
126                         } else {
127                                 $filename = basename($_FILES['media']['name']);
128                         }
129                 }
130
131                 if (!empty($_FILES['media']['size'])) {
132                         if (is_array($_FILES['media']['size'])) {
133                                 $filesize = intval($_FILES['media']['size'][0]);
134                         } else {
135                                 $filesize = intval($_FILES['media']['size']);
136                         }
137                 }
138
139                 if (!empty($_FILES['media']['type'])) {
140                         if (is_array($_FILES['media']['type'])) {
141                                 $filetype = $_FILES['media']['type'][0];
142                         } else {
143                                 $filetype = $_FILES['media']['type'];
144                         }
145                 }
146         }
147
148         if ($src == "") {
149                 if ($r_json) {
150                         echo json_encode(['error' => DI::l10n()->t('Invalid request.')]);
151                         exit();
152                 }
153                 notice(DI::l10n()->t('Invalid request.'));
154                 exit();
155         }
156
157         $filetype = Images::getMimeTypeBySource($src, $filename, $filetype);
158
159         Logger::info("File upload src: " . $src . " - filename: " . $filename .
160                 " - size: " . $filesize . " - type: " . $filetype);
161
162         $imagedata = @file_get_contents($src);
163         $Image = new Image($imagedata, $filetype);
164
165         if (!$Image->isValid()) {
166                 $msg = DI::l10n()->t('Unable to process image.');
167                 if ($r_json) {
168                         echo json_encode(['error' => $msg]);
169                 } else {
170                         echo  $msg. EOL;
171                 }
172                 @unlink($src);
173                 exit();
174         }
175
176         $Image->orient($src);
177         @unlink($src);
178
179         $max_length = DI::config()->get('system', 'max_image_length');
180         if (!$max_length) {
181                 $max_length = MAX_IMAGE_LENGTH;
182         }
183         if ($max_length > 0) {
184                 $Image->scaleDown($max_length);
185                 $filesize = strlen($Image->asString());
186                 Logger::info("File upload: Scaling picture to new size " . $max_length);
187         }
188
189         $width = $Image->getWidth();
190         $height = $Image->getHeight();
191
192         $maximagesize = DI::config()->get('system', 'maximagesize');
193
194         if (!empty($maximagesize) && ($filesize > $maximagesize)) {
195                 // Scale down to multiples of 640 until the maximum size isn't exceeded anymore
196                 foreach ([5120, 2560, 1280, 640] as $pixels) {
197                         if (($filesize > $maximagesize) && (max($width, $height) > $pixels)) {
198                                 Logger::info('Resize', ['size' => $filesize, 'width' => $width, 'height' => $height, 'max' => $maximagesize, 'pixels' => $pixels]);
199                                 $Image->scaleDown($pixels);
200                                 $filesize = strlen($Image->asString());
201                                 $width = $Image->getWidth();
202                                 $height = $Image->getHeight();
203                         }
204                 }
205                 if ($filesize > $maximagesize) {
206                         Logger::notice('Image size is too big', ['size' => $filesize, 'max' => $maximagesize]);
207                         $msg = DI::l10n()->t('Image exceeds size limit of %s', Strings::formatBytes($maximagesize));
208                         if ($r_json) {
209                                 echo json_encode(['error' => $msg]);
210                         } else {
211                                 echo  $msg. EOL;
212                         }
213                         @unlink($src);
214                         exit();
215                 }
216         }
217
218         $resource_id = Photo::newResource();
219
220         $smallest = 0;
221
222         // If we don't have an album name use the Wall Photos album
223         if (!strlen($album)) {
224                 $album = DI::l10n()->t('Wall Photos');
225         }
226
227         $defperm = '<' . $default_cid . '>';
228
229         $r = Photo::store($Image, $page_owner_uid, $visitor, $resource_id, $filename, $album, 0, Photo::DEFAULT, $defperm);
230
231         if (!$r) {
232                 $msg = DI::l10n()->t('Image upload failed.');
233                 if ($r_json) {
234                         echo json_encode(['error' => $msg]);
235                 } else {
236                         echo  $msg. EOL;
237                 }
238                 exit();
239         }
240
241         if ($width > 640 || $height > 640) {
242                 $Image->scaleDown(640);
243                 $r = Photo::store($Image, $page_owner_uid, $visitor, $resource_id, $filename, $album, 1, Photo::DEFAULT, $defperm);
244                 if ($r) {
245                         $smallest = 1;
246                 }
247         }
248
249         if ($width > 320 || $height > 320) {
250                 $Image->scaleDown(320);
251                 $r = Photo::store($Image, $page_owner_uid, $visitor, $resource_id, $filename, $album, 2, Photo::DEFAULT, $defperm);
252                 if ($r && ($smallest == 0)) {
253                         $smallest = 2;
254                 }
255         }
256
257         if (!$desktopmode) {
258                 $photo = Photo::selectFirst(['id', 'datasize', 'width', 'height', 'type'], ['resource-id' => $resource_id], ['order' => ['width']]);
259                 if (!$photo) {
260                         if ($r_json) {
261                                 echo json_encode(['error' => '']);
262                                 exit();
263                         }
264                         return false;
265                 }
266                 $picture = [];
267
268                 $picture["id"]        = $photo["id"];
269                 $picture["size"]      = $photo["datasize"];
270                 $picture["width"]     = $photo["width"];
271                 $picture["height"]    = $photo["height"];
272                 $picture["type"]      = $photo["type"];
273                 $picture["albumpage"] = DI::baseUrl() . '/photos/' . $page_owner_nick . '/image/' . $resource_id;
274                 $picture["picture"]   = DI::baseUrl() . "/photo/{$resource_id}-0." . $Image->getExt();
275                 $picture["preview"]   = DI::baseUrl() . "/photo/{$resource_id}-{$smallest}." . $Image->getExt();
276
277                 if ($r_json) {
278                         echo json_encode(['picture' => $picture]);
279                         exit();
280                 }
281                 Logger::info("upload done");
282                 return $picture;
283         }
284
285         Logger::info("upload done");
286
287         if ($r_json) {
288                 echo json_encode(['ok' => true]);
289                 exit();
290         }
291
292         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";
293         exit();
294         // NOTREACHED
295 }