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\Logger;
14 use Friendica\Core\System;
15 use Friendica\Core\Config;
16 use Friendica\Database\DBA;
17 use Friendica\Model\Contact;
18 use Friendica\Model\Photo;
19 use Friendica\Object\Image;
21 function wall_upload_post(App $a, $desktopmode = true)
23 Logger::log("wall upload: starting new upload", Logger::DEBUG);
25 $r_json = (x($_GET, 'response') && $_GET['response'] == 'json');
26 $album = (x($_GET, 'album') ? notags(trim($_GET['album'])) : '');
29 if (!x($_FILES, 'media')) {
31 $r = q("SELECT `user`.*, `contact`.`id` FROM `user`
32 INNER JOIN `contact` on `user`.`uid` = `contact`.`uid`
33 WHERE `user`.`nickname` = '%s' AND `user`.`blocked` = 0
34 AND `contact`.`self` = 1 LIMIT 1",
38 if (!DBA::isResult($r)) {
40 echo json_encode(['error' => L10n::t('Invalid request.')]);
46 $user_info = api_get_user($a);
47 $r = q("SELECT `user`.*, `contact`.`id` FROM `user`
48 INNER JOIN `contact` on `user`.`uid` = `contact`.`uid`
49 WHERE `user`.`nickname` = '%s' AND `user`.`blocked` = 0
50 AND `contact`.`self` = 1 LIMIT 1",
51 DBA::escape($user_info['screen_name'])
56 echo json_encode(['error' => L10n::t('Invalid request.')]);
63 * Setup permissions structures
68 $page_owner_uid = $r[0]['uid'];
69 $default_cid = $r[0]['id'];
70 $page_owner_nick = $r[0]['nickname'];
71 $community_page = (($r[0]['page-flags'] == Contact::PAGE_COMMUNITY) ? true : false);
73 if ((local_user()) && (local_user() == $page_owner_uid)) {
76 if ($community_page && remote_user()) {
78 if (is_array($_SESSION['remote'])) {
79 foreach ($_SESSION['remote'] as $v) {
80 if ($v['uid'] == $page_owner_uid) {
81 $contact_id = $v['cid'];
88 $r = q("SELECT `uid` FROM `contact`
89 WHERE `blocked` = 0 AND `pending` = 0
90 AND `id` = %d AND `uid` = %d LIMIT 1",
92 intval($page_owner_uid)
94 if (DBA::isResult($r)) {
96 $visitor = $contact_id;
105 echo json_encode(['error' => L10n::t('Permission denied.')]);
108 notice(L10n::t('Permission denied.') . EOL);
112 if (!x($_FILES, 'userfile') && !x($_FILES, 'media')) {
114 echo json_encode(['error' => L10n::t('Invalid request.')]);
123 if (x($_FILES, 'userfile')) {
124 $src = $_FILES['userfile']['tmp_name'];
125 $filename = basename($_FILES['userfile']['name']);
126 $filesize = intval($_FILES['userfile']['size']);
127 $filetype = $_FILES['userfile']['type'];
129 } elseif (x($_FILES, 'media')) {
130 if (!empty($_FILES['media']['tmp_name'])) {
131 if (is_array($_FILES['media']['tmp_name'])) {
132 $src = $_FILES['media']['tmp_name'][0];
134 $src = $_FILES['media']['tmp_name'];
138 if (!empty($_FILES['media']['name'])) {
139 if (is_array($_FILES['media']['name'])) {
140 $filename = basename($_FILES['media']['name'][0]);
142 $filename = basename($_FILES['media']['name']);
146 if (!empty($_FILES['media']['size'])) {
147 if (is_array($_FILES['media']['size'])) {
148 $filesize = intval($_FILES['media']['size'][0]);
150 $filesize = intval($_FILES['media']['size']);
154 if (!empty($_FILES['media']['type'])) {
155 if (is_array($_FILES['media']['type'])) {
156 $filetype = $_FILES['media']['type'][0];
158 $filetype = $_FILES['media']['type'];
165 echo json_encode(['error' => L10n::t('Invalid request.')]);
168 notice(L10n::t('Invalid request.').EOL);
172 // This is a special treatment for picture upload from Twidere
173 if (($filename == "octet-stream") && ($filetype != "")) {
174 $filename = $filetype;
178 if ($filetype == "") {
179 $filetype = Image::guessType($filename);
182 // If there is a temp name, then do a manual check
183 // This is more reliable than the provided value
185 $imagedata = getimagesize($src);
187 $filetype = $imagedata['mime'];
190 Logger::log("File upload src: " . $src . " - filename: " . $filename .
191 " - size: " . $filesize . " - type: " . $filetype, Logger::DEBUG);
193 $maximagesize = Config::get('system', 'maximagesize');
195 if (($maximagesize) && ($filesize > $maximagesize)) {
196 $msg = L10n::t('Image exceeds size limit of %s', formatBytes($maximagesize));
198 echo json_encode(['error' => $msg]);
206 $imagedata = @file_get_contents($src);
207 $Image = new Image($imagedata, $filetype);
209 if (!$Image->isValid()) {
210 $msg = L10n::t('Unable to process image.');
212 echo json_encode(['error' => $msg]);
220 $Image->orient($src);
223 $max_length = Config::get('system', 'max_image_length');
225 $max_length = MAX_IMAGE_LENGTH;
227 if ($max_length > 0) {
228 $Image->scaleDown($max_length);
229 Logger::log("File upload: Scaling picture to new size " . $max_length, Logger::DEBUG);
232 $width = $Image->getWidth();
233 $height = $Image->getHeight();
235 $hash = Photo::newResource();
239 // If we don't have an album name use the Wall Photos album
240 if (!strlen($album)) {
241 $album = L10n::t('Wall Photos');
244 $defperm = '<' . $default_cid . '>';
246 $r = Photo::store($Image, $page_owner_uid, $visitor, $hash, $filename, $album, 0, 0, $defperm);
249 $msg = L10n::t('Image upload failed.');
251 echo json_encode(['error' => $msg]);
258 if ($width > 640 || $height > 640) {
259 $Image->scaleDown(640);
260 $r = Photo::store($Image, $page_owner_uid, $visitor, $hash, $filename, $album, 1, 0, $defperm);
266 if ($width > 320 || $height > 320) {
267 $Image->scaleDown(320);
268 $r = Photo::store($Image, $page_owner_uid, $visitor, $hash, $filename, $album, 2, 0, $defperm);
269 if ($r && ($smallest == 0)) {
274 $basename = basename($filename);
277 $r = q("SELECT `id`, `datasize`, `width`, `height`, `type` FROM `photo`
278 WHERE `resource-id` = '%s'
279 ORDER BY `width` DESC LIMIT 1",
284 echo json_encode(['error' => '']);
291 $picture["id"] = $r[0]["id"];
292 $picture["size"] = $r[0]["datasize"];
293 $picture["width"] = $r[0]["width"];
294 $picture["height"] = $r[0]["height"];
295 $picture["type"] = $r[0]["type"];
296 $picture["albumpage"] = System::baseUrl() . '/photos/' . $page_owner_nick . '/image/' . $hash;
297 $picture["picture"] = System::baseUrl() . "/photo/{$hash}-0." . $Image->getExt();
298 $picture["preview"] = System::baseUrl() . "/photo/{$hash}-{$smallest}." . $Image->getExt();
301 echo json_encode(['picture' => $picture]);
304 Logger::log("upload done", Logger::DEBUG);
308 Logger::log("upload done", Logger::DEBUG);
311 echo json_encode(['ok' => true]);
315 echo "\n\n" . '[url=' . System::baseUrl() . '/photos/' . $page_owner_nick . '/image/' . $hash . '][img]' . System::baseUrl() . "/photo/{$hash}-{$smallest}.".$Image->getExt()."[/img][/url]\n\n";