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\DBM;
16 use Friendica\Model\Photo;
17 use Friendica\Object\Image;
19 function wall_upload_post(App $a, $desktopmode = true)
21 logger("wall upload: starting new upload", LOGGER_DEBUG);
23 $r_json = (x($_GET, 'response') && $_GET['response'] == 'json');
24 $album = (x($_GET, 'album') ? notags(trim($_GET['album'])) : '');
27 if (!x($_FILES, 'media')) {
29 $r = q("SELECT `user`.*, `contact`.`id` FROM `user`
30 INNER JOIN `contact` on `user`.`uid` = `contact`.`uid`
31 WHERE `user`.`nickname` = '%s' AND `user`.`blocked` = 0
32 AND `contact`.`self` = 1 LIMIT 1",
36 if (!DBM::is_result($r)) {
38 echo json_encode(['error' => L10n::t('Invalid request.')]);
44 $user_info = api_get_user($a);
45 $r = q("SELECT `user`.*, `contact`.`id` FROM `user`
46 INNER JOIN `contact` on `user`.`uid` = `contact`.`uid`
47 WHERE `user`.`nickname` = '%s' AND `user`.`blocked` = 0
48 AND `contact`.`self` = 1 LIMIT 1",
49 dbesc($user_info['screen_name'])
54 echo json_encode(['error' => L10n::t('Invalid request.')]);
61 * Setup permissions structures
66 $page_owner_uid = $r[0]['uid'];
67 $default_cid = $r[0]['id'];
68 $page_owner_nick = $r[0]['nickname'];
69 $community_page = (($r[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
71 if ((local_user()) && (local_user() == $page_owner_uid)) {
74 if ($community_page && remote_user()) {
76 if (is_array($_SESSION['remote'])) {
77 foreach ($_SESSION['remote'] as $v) {
78 if ($v['uid'] == $page_owner_uid) {
79 $contact_id = $v['cid'];
86 $r = q("SELECT `uid` FROM `contact`
87 WHERE `blocked` = 0 AND `pending` = 0
88 AND `id` = %d AND `uid` = %d LIMIT 1",
90 intval($page_owner_uid)
92 if (DBM::is_result($r)) {
94 $visitor = $contact_id;
103 echo json_encode(['error' => L10n::t('Permission denied.')]);
106 notice(L10n::t('Permission denied.') . EOL);
110 if (!x($_FILES, 'userfile') && !x($_FILES, 'media')) {
112 echo json_encode(['error' => L10n::t('Invalid request.')]);
121 if (x($_FILES, 'userfile')) {
122 $src = $_FILES['userfile']['tmp_name'];
123 $filename = basename($_FILES['userfile']['name']);
124 $filesize = intval($_FILES['userfile']['size']);
125 $filetype = $_FILES['userfile']['type'];
127 } elseif (x($_FILES, 'media')) {
128 if (is_array($_FILES['media']['tmp_name'])) {
129 $src = $_FILES['media']['tmp_name'][0];
131 $src = $_FILES['media']['tmp_name'];
134 if (is_array($_FILES['media']['name'])) {
135 $filename = basename($_FILES['media']['name'][0]);
137 $filename = basename($_FILES['media']['name']);
140 if (is_array($_FILES['media']['size'])) {
141 $filesize = intval($_FILES['media']['size'][0]);
143 $filesize = intval($_FILES['media']['size']);
146 if (is_array($_FILES['media']['type'])) {
147 $filetype = $_FILES['media']['type'][0];
149 $filetype = $_FILES['media']['type'];
155 echo json_encode(['error' => L10n::t('Invalid request.')]);
158 notice(L10n::t('Invalid request.').EOL);
162 // This is a special treatment for picture upload from Twidere
163 if (($filename == "octet-stream") && ($filetype != "")) {
164 $filename = $filetype;
168 if ($filetype == "") {
169 $filetype = Image::guessType($filename);
172 // If there is a temp name, then do a manual check
173 // This is more reliable than the provided value
175 $imagedata = getimagesize($src);
177 $filetype = $imagedata['mime'];
180 logger("File upload src: " . $src . " - filename: " . $filename .
181 " - size: " . $filesize . " - type: " . $filetype, LOGGER_DEBUG);
183 $maximagesize = Config::get('system', 'maximagesize');
185 if (($maximagesize) && ($filesize > $maximagesize)) {
186 $msg = L10n::t('Image exceeds size limit of %s', formatBytes($maximagesize));
188 echo json_encode(['error' => $msg]);
196 $imagedata = @file_get_contents($src);
197 $Image = new Image($imagedata, $filetype);
199 if (!$Image->isValid()) {
200 $msg = L10n::t('Unable to process image.');
202 echo json_encode(['error' => $msg]);
210 $Image->orient($src);
213 $max_length = Config::get('system', 'max_image_length');
215 $max_length = MAX_IMAGE_LENGTH;
217 if ($max_length > 0) {
218 $Image->scaleDown($max_length);
219 logger("File upload: Scaling picture to new size " . $max_length, LOGGER_DEBUG);
222 $width = $Image->getWidth();
223 $height = $Image->getHeight();
225 $hash = Photo::newResource();
229 // If we don't have an album name use the Wall Photos album
230 if (!strlen($album)) {
231 $album = L10n::t('Wall Photos');
234 $defperm = '<' . $default_cid . '>';
236 $r = Photo::store($Image, $page_owner_uid, $visitor, $hash, $filename, $album, 0, 0, $defperm);
239 $msg = L10n::t('Image upload failed.');
241 echo json_encode(['error' => $msg]);
248 if ($width > 640 || $height > 640) {
249 $Image->scaleDown(640);
250 $r = Photo::store($Image, $page_owner_uid, $visitor, $hash, $filename, $album, 1, 0, $defperm);
256 if ($width > 320 || $height > 320) {
257 $Image->scaleDown(320);
258 $r = Photo::store($Image, $page_owner_uid, $visitor, $hash, $filename, $album, 2, 0, $defperm);
259 if ($r && ($smallest == 0)) {
264 $basename = basename($filename);
267 $r = q("SELECT `id`, `datasize`, `width`, `height`, `type` FROM `photo`
268 WHERE `resource-id` = '%s'
269 ORDER BY `width` DESC LIMIT 1",
274 echo json_encode(['error' => '']);
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"] = System::baseUrl() . '/photos/' . $page_owner_nick . '/image/' . $hash;
287 $picture["picture"] = System::baseUrl() . "/photo/{$hash}-0." . $Image->getExt();
288 $picture["preview"] = System::baseUrl() . "/photo/{$hash}-{$smallest}." . $Image->getExt();
291 echo json_encode(['picture' => $picture]);
294 logger("upload done", LOGGER_DEBUG);
298 logger("upload done", LOGGER_DEBUG);
301 echo json_encode(['ok' => true]);
305 echo "\n\n" . '[url=' . System::baseUrl() . '/photos/' . $page_owner_nick . '/image/' . $hash . '][img]' . System::baseUrl() . "/photo/{$hash}-{$smallest}.".$Image->getExt()."[/img][/url]\n\n";