3 * @file mod/wall_upload.php
4 * @brief Module for uploading a picture to the profile wall
6 * By default the picture will be stored in the photo album with the name Wall Photos.
7 * You can specify a different album by adding an optional query string "album="
12 use Friendica\Core\L10n;
13 use Friendica\Core\System;
14 use Friendica\Core\Config;
15 use Friendica\Database\DBA;
16 use Friendica\Model\Contact;
17 use Friendica\Model\Photo;
18 use Friendica\Object\Image;
20 function wall_upload_post(App $a, $desktopmode = true)
22 logger("wall upload: starting new upload", LOGGER_DEBUG);
24 $r_json = (x($_GET, 'response') && $_GET['response'] == 'json');
25 $album = (x($_GET, 'album') ? notags(trim($_GET['album'])) : '');
28 if (!x($_FILES, 'media')) {
30 $r = q("SELECT `user`.*, `contact`.`id` FROM `user`
31 INNER JOIN `contact` on `user`.`uid` = `contact`.`uid`
32 WHERE `user`.`nickname` = '%s' AND `user`.`blocked` = 0
33 AND `contact`.`self` = 1 LIMIT 1",
37 if (!DBA::isResult($r)) {
39 echo json_encode(['error' => L10n::t('Invalid request.')]);
45 $user_info = api_get_user($a);
46 $r = q("SELECT `user`.*, `contact`.`id` FROM `user`
47 INNER JOIN `contact` on `user`.`uid` = `contact`.`uid`
48 WHERE `user`.`nickname` = '%s' AND `user`.`blocked` = 0
49 AND `contact`.`self` = 1 LIMIT 1",
50 DBA::escape($user_info['screen_name'])
55 echo json_encode(['error' => L10n::t('Invalid request.')]);
62 * Setup permissions structures
67 $page_owner_uid = $r[0]['uid'];
68 $default_cid = $r[0]['id'];
69 $page_owner_nick = $r[0]['nickname'];
70 $community_page = (($r[0]['page-flags'] == Contact::PAGE_COMMUNITY) ? true : false);
72 if ((local_user()) && (local_user() == $page_owner_uid)) {
75 if ($community_page && remote_user()) {
77 if (is_array($_SESSION['remote'])) {
78 foreach ($_SESSION['remote'] as $v) {
79 if ($v['uid'] == $page_owner_uid) {
80 $contact_id = $v['cid'];
87 $r = q("SELECT `uid` FROM `contact`
88 WHERE `blocked` = 0 AND `pending` = 0
89 AND `id` = %d AND `uid` = %d LIMIT 1",
91 intval($page_owner_uid)
93 if (DBA::isResult($r)) {
95 $visitor = $contact_id;
104 echo json_encode(['error' => L10n::t('Permission denied.')]);
107 notice(L10n::t('Permission denied.') . EOL);
111 if (!x($_FILES, 'userfile') && !x($_FILES, 'media')) {
113 echo json_encode(['error' => L10n::t('Invalid request.')]);
122 if (x($_FILES, 'userfile')) {
123 $src = $_FILES['userfile']['tmp_name'];
124 $filename = basename($_FILES['userfile']['name']);
125 $filesize = intval($_FILES['userfile']['size']);
126 $filetype = $_FILES['userfile']['type'];
128 } elseif (x($_FILES, 'media')) {
129 if (!empty($_FILES['media']['tmp_name'])) {
130 if (is_array($_FILES['media']['tmp_name'])) {
131 $src = $_FILES['media']['tmp_name'][0];
133 $src = $_FILES['media']['tmp_name'];
137 if (!empty($_FILES['media']['name'])) {
138 if (is_array($_FILES['media']['name'])) {
139 $filename = basename($_FILES['media']['name'][0]);
141 $filename = basename($_FILES['media']['name']);
145 if (!empty($_FILES['media']['size'])) {
146 if (is_array($_FILES['media']['size'])) {
147 $filesize = intval($_FILES['media']['size'][0]);
149 $filesize = intval($_FILES['media']['size']);
153 if (!empty($_FILES['media']['type'])) {
154 if (is_array($_FILES['media']['type'])) {
155 $filetype = $_FILES['media']['type'][0];
157 $filetype = $_FILES['media']['type'];
164 echo json_encode(['error' => L10n::t('Invalid request.')]);
167 notice(L10n::t('Invalid request.').EOL);
171 // This is a special treatment for picture upload from Twidere
172 if (($filename == "octet-stream") && ($filetype != "")) {
173 $filename = $filetype;
177 if ($filetype == "") {
178 $filetype = Image::guessType($filename);
181 // If there is a temp name, then do a manual check
182 // This is more reliable than the provided value
184 $imagedata = getimagesize($src);
186 $filetype = $imagedata['mime'];
189 logger("File upload src: " . $src . " - filename: " . $filename .
190 " - size: " . $filesize . " - type: " . $filetype, LOGGER_DEBUG);
192 $maximagesize = Config::get('system', 'maximagesize');
194 if (($maximagesize) && ($filesize > $maximagesize)) {
195 $msg = L10n::t('Image exceeds size limit of %s', formatBytes($maximagesize));
197 echo json_encode(['error' => $msg]);
205 $imagedata = @file_get_contents($src);
206 $Image = new Image($imagedata, $filetype);
208 if (!$Image->isValid()) {
209 $msg = L10n::t('Unable to process image.');
211 echo json_encode(['error' => $msg]);
219 $Image->orient($src);
222 $max_length = Config::get('system', 'max_image_length');
224 $max_length = MAX_IMAGE_LENGTH;
226 if ($max_length > 0) {
227 $Image->scaleDown($max_length);
228 logger("File upload: Scaling picture to new size " . $max_length, LOGGER_DEBUG);
231 $width = $Image->getWidth();
232 $height = $Image->getHeight();
234 $hash = Photo::newResource();
238 // If we don't have an album name use the Wall Photos album
239 if (!strlen($album)) {
240 $album = L10n::t('Wall Photos');
243 $defperm = '<' . $default_cid . '>';
245 $r = Photo::store($Image, $page_owner_uid, $visitor, $hash, $filename, $album, 0, 0, $defperm);
248 $msg = L10n::t('Image upload failed.');
250 echo json_encode(['error' => $msg]);
257 if ($width > 640 || $height > 640) {
258 $Image->scaleDown(640);
259 $r = Photo::store($Image, $page_owner_uid, $visitor, $hash, $filename, $album, 1, 0, $defperm);
265 if ($width > 320 || $height > 320) {
266 $Image->scaleDown(320);
267 $r = Photo::store($Image, $page_owner_uid, $visitor, $hash, $filename, $album, 2, 0, $defperm);
268 if ($r && ($smallest == 0)) {
273 $basename = basename($filename);
276 $r = q("SELECT `id`, `datasize`, `width`, `height`, `type` FROM `photo`
277 WHERE `resource-id` = '%s'
278 ORDER BY `width` DESC LIMIT 1",
283 echo json_encode(['error' => '']);
290 $picture["id"] = $r[0]["id"];
291 $picture["size"] = $r[0]["datasize"];
292 $picture["width"] = $r[0]["width"];
293 $picture["height"] = $r[0]["height"];
294 $picture["type"] = $r[0]["type"];
295 $picture["albumpage"] = System::baseUrl() . '/photos/' . $page_owner_nick . '/image/' . $hash;
296 $picture["picture"] = System::baseUrl() . "/photo/{$hash}-0." . $Image->getExt();
297 $picture["preview"] = System::baseUrl() . "/photo/{$hash}-{$smallest}." . $Image->getExt();
300 echo json_encode(['picture' => $picture]);
303 logger("upload done", LOGGER_DEBUG);
307 logger("upload done", LOGGER_DEBUG);
310 echo json_encode(['ok' => true]);
314 echo "\n\n" . '[url=' . System::baseUrl() . '/photos/' . $page_owner_nick . '/image/' . $hash . '][img]' . System::baseUrl() . "/photo/{$hash}-{$smallest}.".$Image->getExt()."[/img][/url]\n\n";