3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
20 * Module for uploading a picture to the profile wall
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="
29 use Friendica\Core\Logger;
30 use Friendica\Core\Session;
31 use Friendica\Database\DBA;
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;
39 function wall_upload_post(App $a, $desktopmode = true)
41 Logger::log("wall upload: starting new upload", Logger::DEBUG);
43 $r_json = (!empty($_GET['response']) && $_GET['response'] == 'json');
44 $album = (!empty($_GET['album']) ? Strings::escapeTags(trim($_GET['album'])) : '');
47 if (empty($_FILES['media'])) {
49 $r = q("SELECT `user`.*, `contact`.`id` FROM `user`
50 INNER JOIN `contact` on `user`.`uid` = `contact`.`uid`
51 WHERE `user`.`nickname` = '%s' AND `user`.`blocked` = 0
52 AND `contact`.`self` = 1 LIMIT 1",
56 if (!DBA::isResult($r)) {
58 echo json_encode(['error' => DI::l10n()->t('Invalid request.')]);
64 $user_info = api_get_user($a);
65 $r = q("SELECT `user`.*, `contact`.`id` FROM `user`
66 INNER JOIN `contact` on `user`.`uid` = `contact`.`uid`
67 WHERE `user`.`nickname` = '%s' AND `user`.`blocked` = 0
68 AND `contact`.`self` = 1 LIMIT 1",
69 DBA::escape($user_info['screen_name'])
74 echo json_encode(['error' => DI::l10n()->t('Invalid request.')]);
81 * Setup permissions structures
86 $page_owner_uid = $r[0]['uid'];
87 $default_cid = $r[0]['id'];
88 $page_owner_nick = $r[0]['nickname'];
89 $community_page = (($r[0]['page-flags'] == User::PAGE_FLAGS_COMMUNITY) ? true : false);
91 if ((local_user()) && (local_user() == $page_owner_uid)) {
93 } elseif ($community_page && !empty(Session::getRemoteContactID($page_owner_uid))) {
94 $contact_id = Session::getRemoteContactID($page_owner_uid);
96 $r = q("SELECT `uid` FROM `contact`
97 WHERE `blocked` = 0 AND `pending` = 0
98 AND `id` = %d AND `uid` = %d LIMIT 1",
100 intval($page_owner_uid)
102 if (DBA::isResult($r)) {
104 $visitor = $contact_id;
110 echo json_encode(['error' => DI::l10n()->t('Permission denied.')]);
113 notice(DI::l10n()->t('Permission denied.') . EOL);
117 if (empty($_FILES['userfile']) && empty($_FILES['media'])) {
119 echo json_encode(['error' => DI::l10n()->t('Invalid request.')]);
128 if (!empty($_FILES['userfile'])) {
129 $src = $_FILES['userfile']['tmp_name'];
130 $filename = basename($_FILES['userfile']['name']);
131 $filesize = intval($_FILES['userfile']['size']);
132 $filetype = $_FILES['userfile']['type'];
134 } elseif (!empty($_FILES['media'])) {
135 if (!empty($_FILES['media']['tmp_name'])) {
136 if (is_array($_FILES['media']['tmp_name'])) {
137 $src = $_FILES['media']['tmp_name'][0];
139 $src = $_FILES['media']['tmp_name'];
143 if (!empty($_FILES['media']['name'])) {
144 if (is_array($_FILES['media']['name'])) {
145 $filename = basename($_FILES['media']['name'][0]);
147 $filename = basename($_FILES['media']['name']);
151 if (!empty($_FILES['media']['size'])) {
152 if (is_array($_FILES['media']['size'])) {
153 $filesize = intval($_FILES['media']['size'][0]);
155 $filesize = intval($_FILES['media']['size']);
159 if (!empty($_FILES['media']['type'])) {
160 if (is_array($_FILES['media']['type'])) {
161 $filetype = $_FILES['media']['type'][0];
163 $filetype = $_FILES['media']['type'];
170 echo json_encode(['error' => DI::l10n()->t('Invalid request.')]);
173 notice(DI::l10n()->t('Invalid request.').EOL);
177 $filetype = Images::getMimeTypeBySource($src, $filename, $filetype);
179 Logger::log("File upload src: " . $src . " - filename: " . $filename .
180 " - size: " . $filesize . " - type: " . $filetype, Logger::DEBUG);
182 $maximagesize = DI::config()->get('system', 'maximagesize');
184 if (($maximagesize) && ($filesize > $maximagesize)) {
185 $msg = DI::l10n()->t('Image exceeds size limit of %s', Strings::formatBytes($maximagesize));
187 echo json_encode(['error' => $msg]);
195 $imagedata = @file_get_contents($src);
196 $Image = new Image($imagedata, $filetype);
198 if (!$Image->isValid()) {
199 $msg = DI::l10n()->t('Unable to process image.');
201 echo json_encode(['error' => $msg]);
209 $Image->orient($src);
212 $max_length = DI::config()->get('system', 'max_image_length');
214 $max_length = MAX_IMAGE_LENGTH;
216 if ($max_length > 0) {
217 $Image->scaleDown($max_length);
218 Logger::log("File upload: Scaling picture to new size " . $max_length, Logger::DEBUG);
221 $width = $Image->getWidth();
222 $height = $Image->getHeight();
224 $resource_id = Photo::newResource();
228 // If we don't have an album name use the Wall Photos album
229 if (!strlen($album)) {
230 $album = DI::l10n()->t('Wall Photos');
233 $defperm = '<' . $default_cid . '>';
235 $r = Photo::store($Image, $page_owner_uid, $visitor, $resource_id, $filename, $album, 0, 0, $defperm);
238 $msg = DI::l10n()->t('Image upload failed.');
240 echo json_encode(['error' => $msg]);
247 if ($width > 640 || $height > 640) {
248 $Image->scaleDown(640);
249 $r = Photo::store($Image, $page_owner_uid, $visitor, $resource_id, $filename, $album, 1, 0, $defperm);
255 if ($width > 320 || $height > 320) {
256 $Image->scaleDown(320);
257 $r = Photo::store($Image, $page_owner_uid, $visitor, $resource_id, $filename, $album, 2, 0, $defperm);
258 if ($r && ($smallest == 0)) {
264 $r = q("SELECT `id`, `datasize`, `width`, `height`, `type` FROM `photo`
265 WHERE `resource-id` = '%s'
266 ORDER BY `width` DESC LIMIT 1",
271 echo json_encode(['error' => '']);
278 $picture["id"] = $r[0]["id"];
279 $picture["size"] = $r[0]["datasize"];
280 $picture["width"] = $r[0]["width"];
281 $picture["height"] = $r[0]["height"];
282 $picture["type"] = $r[0]["type"];
283 $picture["albumpage"] = DI::baseUrl() . '/photos/' . $page_owner_nick . '/image/' . $resource_id;
284 $picture["picture"] = DI::baseUrl() . "/photo/{$resource_id}-0." . $Image->getExt();
285 $picture["preview"] = DI::baseUrl() . "/photo/{$resource_id}-{$smallest}." . $Image->getExt();
288 echo json_encode(['picture' => $picture]);
291 Logger::log("upload done", Logger::DEBUG);
295 Logger::log("upload done", Logger::DEBUG);
298 echo json_encode(['ok' => true]);
302 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";