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