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