]> git.mxchange.org Git - friendica.git/blob - mod/wall_upload.php
Added documentation
[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::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                         $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
85                 $r = q("SELECT `uid` FROM `contact`
86                         WHERE `blocked` = 0 AND `pending` = 0
87                         AND `id` = %d AND `uid` = %d LIMIT 1",
88                         intval($contact_id),
89                         intval($page_owner_uid)
90                 );
91                 if (DBA::isResult($r)) {
92                         $can_post = true;
93                         $visitor = $contact_id;
94                 }
95         }
96
97         if (!$can_post) {
98                 if ($r_json) {
99                         echo json_encode(['error' => DI::l10n()->t('Permission denied.')]);
100                         exit();
101                 }
102                 notice(DI::l10n()->t('Permission denied.'));
103                 exit();
104         }
105
106         if (empty($_FILES['userfile']) && empty($_FILES['media'])) {
107                 if ($r_json) {
108                         echo json_encode(['error' => DI::l10n()->t('Invalid request.')]);
109                 }
110                 exit();
111         }
112
113         $src = '';
114         $filename = '';
115         $filesize = 0;
116         $filetype = '';
117         if (!empty($_FILES['userfile'])) {
118                 $src      = $_FILES['userfile']['tmp_name'];
119                 $filename = basename($_FILES['userfile']['name']);
120                 $filesize = intval($_FILES['userfile']['size']);
121                 $filetype = $_FILES['userfile']['type'];
122
123         } elseif (!empty($_FILES['media'])) {
124                 if (!empty($_FILES['media']['tmp_name'])) {
125                         if (is_array($_FILES['media']['tmp_name'])) {
126                                 $src = $_FILES['media']['tmp_name'][0];
127                         } else {
128                                 $src = $_FILES['media']['tmp_name'];
129                         }
130                 }
131
132                 if (!empty($_FILES['media']['name'])) {
133                         if (is_array($_FILES['media']['name'])) {
134                                 $filename = basename($_FILES['media']['name'][0]);
135                         } else {
136                                 $filename = basename($_FILES['media']['name']);
137                         }
138                 }
139
140                 if (!empty($_FILES['media']['size'])) {
141                         if (is_array($_FILES['media']['size'])) {
142                                 $filesize = intval($_FILES['media']['size'][0]);
143                         } else {
144                                 $filesize = intval($_FILES['media']['size']);
145                         }
146                 }
147
148                 if (!empty($_FILES['media']['type'])) {
149                         if (is_array($_FILES['media']['type'])) {
150                                 $filetype = $_FILES['media']['type'][0];
151                         } else {
152                                 $filetype = $_FILES['media']['type'];
153                         }
154                 }
155         }
156
157         if ($src == "") {
158                 if ($r_json) {
159                         echo json_encode(['error' => DI::l10n()->t('Invalid request.')]);
160                         exit();
161                 }
162                 notice(DI::l10n()->t('Invalid request.'));
163                 exit();
164         }
165
166         $filetype = Images::getMimeTypeBySource($src, $filename, $filetype);
167
168         Logger::log("File upload src: " . $src . " - filename: " . $filename .
169                 " - size: " . $filesize . " - type: " . $filetype, Logger::DEBUG);
170
171         $imagedata = @file_get_contents($src);
172         $Image = new Image($imagedata, $filetype);
173
174         if (!$Image->isValid()) {
175                 $msg = DI::l10n()->t('Unable to process image.');
176                 if ($r_json) {
177                         echo json_encode(['error' => $msg]);
178                 } else {
179                         echo  $msg. EOL;
180                 }
181                 @unlink($src);
182                 exit();
183         }
184
185         $Image->orient($src);
186         @unlink($src);
187
188         $max_length = DI::config()->get('system', 'max_image_length');
189         if (!$max_length) {
190                 $max_length = MAX_IMAGE_LENGTH;
191         }
192         if ($max_length > 0) {
193                 $Image->scaleDown($max_length);
194                 $filesize = strlen($Image->asString());
195                 Logger::log("File upload: Scaling picture to new size " . $max_length, Logger::DEBUG);
196         }
197
198         $width = $Image->getWidth();
199         $height = $Image->getHeight();
200
201         $maximagesize = DI::config()->get('system', 'maximagesize');
202
203         if (!empty($maximagesize) && ($filesize > $maximagesize)) {
204                 // Scale down to multiples of 640 until the maximum size isn't exceeded anymore
205                 foreach ([5120, 2560, 1280, 640] as $pixels) {
206                         if (($filesize > $maximagesize) && (max($width, $height) > $pixels)) {
207                                 Logger::info('Resize', ['size' => $filesize, 'width' => $width, 'height' => $height, 'max' => $maximagesize, 'pixels' => $pixels]);
208                                 $Image->scaleDown($pixels);
209                                 $filesize = strlen($Image->asString());
210                                 $width = $Image->getWidth();
211                                 $height = $Image->getHeight();
212                         }
213                 }
214                 if ($filesize > $maximagesize) {
215                         Logger::notice('Image size is too big', ['size' => $filesize, 'max' => $maximagesize]);
216                         $msg = DI::l10n()->t('Image exceeds size limit of %s', Strings::formatBytes($maximagesize));
217                         if ($r_json) {
218                                 echo json_encode(['error' => $msg]);
219                         } else {
220                                 echo  $msg. EOL;
221                         }
222                         @unlink($src);
223                         exit();
224                 }
225         }
226
227         $resource_id = Photo::newResource();
228
229         $smallest = 0;
230
231         // If we don't have an album name use the Wall Photos album
232         if (!strlen($album)) {
233                 $album = DI::l10n()->t('Wall Photos');
234         }
235
236         $defperm = '<' . $default_cid . '>';
237
238         $r = Photo::store($Image, $page_owner_uid, $visitor, $resource_id, $filename, $album, 0, 0, $defperm);
239
240         if (!$r) {
241                 $msg = DI::l10n()->t('Image upload failed.');
242                 if ($r_json) {
243                         echo json_encode(['error' => $msg]);
244                 } else {
245                         echo  $msg. EOL;
246                 }
247                 exit();
248         }
249
250         if ($width > 640 || $height > 640) {
251                 $Image->scaleDown(640);
252                 $r = Photo::store($Image, $page_owner_uid, $visitor, $resource_id, $filename, $album, 1, 0, $defperm);
253                 if ($r) {
254                         $smallest = 1;
255                 }
256         }
257
258         if ($width > 320 || $height > 320) {
259                 $Image->scaleDown(320);
260                 $r = Photo::store($Image, $page_owner_uid, $visitor, $resource_id, $filename, $album, 2, 0, $defperm);
261                 if ($r && ($smallest == 0)) {
262                         $smallest = 2;
263                 }
264         }
265
266         if (!$desktopmode) {
267                 $r = q("SELECT `id`, `datasize`, `width`, `height`, `type` FROM `photo`
268                         WHERE `resource-id` = '%s'
269                         ORDER BY `width` DESC LIMIT 1",
270                         $resource_id
271                 );
272                 if (!$r) {
273                         if ($r_json) {
274                                 echo json_encode(['error' => '']);
275                                 exit();
276                         }
277                         return false;
278                 }
279                 $picture = [];
280
281                 $picture["id"]        = $r[0]["id"];
282                 $picture["size"]      = $r[0]["datasize"];
283                 $picture["width"]     = $r[0]["width"];
284                 $picture["height"]    = $r[0]["height"];
285                 $picture["type"]      = $r[0]["type"];
286                 $picture["albumpage"] = DI::baseUrl() . '/photos/' . $page_owner_nick . '/image/' . $resource_id;
287                 $picture["picture"]   = DI::baseUrl() . "/photo/{$resource_id}-0." . $Image->getExt();
288                 $picture["preview"]   = DI::baseUrl() . "/photo/{$resource_id}-{$smallest}." . $Image->getExt();
289
290                 if ($r_json) {
291                         echo json_encode(['picture' => $picture]);
292                         exit();
293                 }
294                 Logger::log("upload done", Logger::DEBUG);
295                 return $picture;
296         }
297
298         Logger::log("upload done", Logger::DEBUG);
299
300         if ($r_json) {
301                 echo json_encode(['ok' => true]);
302                 exit();
303         }
304
305         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";
306         exit();
307         // NOTREACHED
308 }