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\Model\User;
20 use Friendica\Object\Image;
21 use Friendica\Util\Strings;
23 function wall_upload_post(App $a, $desktopmode = true)
25 Logger::log("wall upload: starting new upload", Logger::DEBUG);
27 $r_json = (!empty($_GET['response']) && $_GET['response'] == 'json');
28 $album = (!empty($_GET['album']) ? Strings::escapeTags(trim($_GET['album'])) : '');
31 if (empty($_FILES['media'])) {
33 $r = q("SELECT `user`.*, `contact`.`id` FROM `user`
34 INNER JOIN `contact` on `user`.`uid` = `contact`.`uid`
35 WHERE `user`.`nickname` = '%s' AND `user`.`blocked` = 0
36 AND `contact`.`self` = 1 LIMIT 1",
40 if (!DBA::isResult($r)) {
42 echo json_encode(['error' => L10n::t('Invalid request.')]);
48 $user_info = api_get_user($a);
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",
53 DBA::escape($user_info['screen_name'])
58 echo json_encode(['error' => L10n::t('Invalid request.')]);
65 * Setup permissions structures
70 $page_owner_uid = $r[0]['uid'];
71 $default_cid = $r[0]['id'];
72 $page_owner_nick = $r[0]['nickname'];
73 $community_page = (($r[0]['page-flags'] == User::PAGE_FLAGS_COMMUNITY) ? true : false);
75 if ((local_user()) && (local_user() == $page_owner_uid)) {
78 if ($community_page && remote_user()) {
80 if (is_array($_SESSION['remote'])) {
81 foreach ($_SESSION['remote'] as $v) {
82 if ($v['uid'] == $page_owner_uid) {
83 $contact_id = $v['cid'];
90 $r = q("SELECT `uid` FROM `contact`
91 WHERE `blocked` = 0 AND `pending` = 0
92 AND `id` = %d AND `uid` = %d LIMIT 1",
94 intval($page_owner_uid)
96 if (DBA::isResult($r)) {
98 $visitor = $contact_id;
107 echo json_encode(['error' => L10n::t('Permission denied.')]);
110 notice(L10n::t('Permission denied.') . EOL);
114 if (empty($_FILES['userfile']) && empty($_FILES['media'])) {
116 echo json_encode(['error' => L10n::t('Invalid request.')]);
125 if (!empty($_FILES['userfile'])) {
126 $src = $_FILES['userfile']['tmp_name'];
127 $filename = basename($_FILES['userfile']['name']);
128 $filesize = intval($_FILES['userfile']['size']);
129 $filetype = $_FILES['userfile']['type'];
131 } elseif (!empty($_FILES['media'])) {
132 if (!empty($_FILES['media']['tmp_name'])) {
133 if (is_array($_FILES['media']['tmp_name'])) {
134 $src = $_FILES['media']['tmp_name'][0];
136 $src = $_FILES['media']['tmp_name'];
140 if (!empty($_FILES['media']['name'])) {
141 if (is_array($_FILES['media']['name'])) {
142 $filename = basename($_FILES['media']['name'][0]);
144 $filename = basename($_FILES['media']['name']);
148 if (!empty($_FILES['media']['size'])) {
149 if (is_array($_FILES['media']['size'])) {
150 $filesize = intval($_FILES['media']['size'][0]);
152 $filesize = intval($_FILES['media']['size']);
156 if (!empty($_FILES['media']['type'])) {
157 if (is_array($_FILES['media']['type'])) {
158 $filetype = $_FILES['media']['type'][0];
160 $filetype = $_FILES['media']['type'];
167 echo json_encode(['error' => L10n::t('Invalid request.')]);
170 notice(L10n::t('Invalid request.').EOL);
174 // This is a special treatment for picture upload from Twidere
175 if (($filename == "octet-stream") && ($filetype != "")) {
176 $filename = $filetype;
180 if ($filetype == "") {
181 $filetype = Image::guessType($filename);
184 // If there is a temp name, then do a manual check
185 // This is more reliable than the provided value
187 $imagedata = getimagesize($src);
189 $filetype = $imagedata['mime'];
192 Logger::log("File upload src: " . $src . " - filename: " . $filename .
193 " - size: " . $filesize . " - type: " . $filetype, Logger::DEBUG);
195 $maximagesize = Config::get('system', 'maximagesize');
197 if (($maximagesize) && ($filesize > $maximagesize)) {
198 $msg = L10n::t('Image exceeds size limit of %s', Strings::formatBytes($maximagesize));
200 echo json_encode(['error' => $msg]);
208 $imagedata = @file_get_contents($src);
209 $Image = new Image($imagedata, $filetype);
211 if (!$Image->isValid()) {
212 $msg = L10n::t('Unable to process image.');
214 echo json_encode(['error' => $msg]);
222 $Image->orient($src);
225 $max_length = Config::get('system', 'max_image_length');
227 $max_length = MAX_IMAGE_LENGTH;
229 if ($max_length > 0) {
230 $Image->scaleDown($max_length);
231 Logger::log("File upload: Scaling picture to new size " . $max_length, Logger::DEBUG);
234 $width = $Image->getWidth();
235 $height = $Image->getHeight();
237 $hash = Photo::newResource();
241 // If we don't have an album name use the Wall Photos album
242 if (!strlen($album)) {
243 $album = L10n::t('Wall Photos');
246 $defperm = '<' . $default_cid . '>';
248 $r = Photo::store($Image, $page_owner_uid, $visitor, $hash, $filename, $album, 0, 0, $defperm);
251 $msg = L10n::t('Image upload failed.');
253 echo json_encode(['error' => $msg]);
260 if ($width > 640 || $height > 640) {
261 $Image->scaleDown(640);
262 $r = Photo::store($Image, $page_owner_uid, $visitor, $hash, $filename, $album, 1, 0, $defperm);
268 if ($width > 320 || $height > 320) {
269 $Image->scaleDown(320);
270 $r = Photo::store($Image, $page_owner_uid, $visitor, $hash, $filename, $album, 2, 0, $defperm);
271 if ($r && ($smallest == 0)) {
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";