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;
20 use Friendica\Util\Strings;
22 function wall_upload_post(App $a, $desktopmode = true)
24 Logger::log("wall upload: starting new upload", Logger::DEBUG);
26 $r_json = (!empty($_GET['response']) && $_GET['response'] == 'json');
27 $album = (!empty($_GET['album']) ? Strings::escapeTags(trim($_GET['album'])) : '');
30 if (empty($_FILES['media'])) {
32 $r = q("SELECT `user`.*, `contact`.`id` FROM `user`
33 INNER JOIN `contact` on `user`.`uid` = `contact`.`uid`
34 WHERE `user`.`nickname` = '%s' AND `user`.`blocked` = 0
35 AND `contact`.`self` = 1 LIMIT 1",
39 if (!DBA::isResult($r)) {
41 echo json_encode(['error' => L10n::t('Invalid request.')]);
47 $user_info = api_get_user($a);
48 $r = q("SELECT `user`.*, `contact`.`id` FROM `user`
49 INNER JOIN `contact` on `user`.`uid` = `contact`.`uid`
50 WHERE `user`.`nickname` = '%s' AND `user`.`blocked` = 0
51 AND `contact`.`self` = 1 LIMIT 1",
52 DBA::escape($user_info['screen_name'])
57 echo json_encode(['error' => L10n::t('Invalid request.')]);
64 * Setup permissions structures
69 $page_owner_uid = $r[0]['uid'];
70 $default_cid = $r[0]['id'];
71 $page_owner_nick = $r[0]['nickname'];
72 $community_page = (($r[0]['page-flags'] == Contact::PAGE_COMMUNITY) ? true : false);
74 if ((local_user()) && (local_user() == $page_owner_uid)) {
77 if ($community_page && remote_user()) {
79 if (is_array($_SESSION['remote'])) {
80 foreach ($_SESSION['remote'] as $v) {
81 if ($v['uid'] == $page_owner_uid) {
82 $contact_id = $v['cid'];
89 $r = q("SELECT `uid` FROM `contact`
90 WHERE `blocked` = 0 AND `pending` = 0
91 AND `id` = %d AND `uid` = %d LIMIT 1",
93 intval($page_owner_uid)
95 if (DBA::isResult($r)) {
97 $visitor = $contact_id;
106 echo json_encode(['error' => L10n::t('Permission denied.')]);
109 notice(L10n::t('Permission denied.') . EOL);
113 if (empty($_FILES['userfile']) && empty($_FILES['media'])) {
115 echo json_encode(['error' => L10n::t('Invalid request.')]);
124 if (!empty($_FILES['userfile'])) {
125 $src = $_FILES['userfile']['tmp_name'];
126 $filename = basename($_FILES['userfile']['name']);
127 $filesize = intval($_FILES['userfile']['size']);
128 $filetype = $_FILES['userfile']['type'];
130 } elseif (!empty($_FILES['media'])) {
131 if (!empty($_FILES['media']['tmp_name'])) {
132 if (is_array($_FILES['media']['tmp_name'])) {
133 $src = $_FILES['media']['tmp_name'][0];
135 $src = $_FILES['media']['tmp_name'];
139 if (!empty($_FILES['media']['name'])) {
140 if (is_array($_FILES['media']['name'])) {
141 $filename = basename($_FILES['media']['name'][0]);
143 $filename = basename($_FILES['media']['name']);
147 if (!empty($_FILES['media']['size'])) {
148 if (is_array($_FILES['media']['size'])) {
149 $filesize = intval($_FILES['media']['size'][0]);
151 $filesize = intval($_FILES['media']['size']);
155 if (!empty($_FILES['media']['type'])) {
156 if (is_array($_FILES['media']['type'])) {
157 $filetype = $_FILES['media']['type'][0];
159 $filetype = $_FILES['media']['type'];
166 echo json_encode(['error' => L10n::t('Invalid request.')]);
169 notice(L10n::t('Invalid request.').EOL);
173 // This is a special treatment for picture upload from Twidere
174 if (($filename == "octet-stream") && ($filetype != "")) {
175 $filename = $filetype;
179 if ($filetype == "") {
180 $filetype = Image::guessType($filename);
183 // If there is a temp name, then do a manual check
184 // This is more reliable than the provided value
186 $imagedata = getimagesize($src);
188 $filetype = $imagedata['mime'];
191 Logger::log("File upload src: " . $src . " - filename: " . $filename .
192 " - size: " . $filesize . " - type: " . $filetype, Logger::DEBUG);
194 $maximagesize = Config::get('system', 'maximagesize');
196 if (($maximagesize) && ($filesize > $maximagesize)) {
197 $msg = L10n::t('Image exceeds size limit of %s', Strings::formatBytes($maximagesize));
199 echo json_encode(['error' => $msg]);
207 $imagedata = @file_get_contents($src);
208 $Image = new Image($imagedata, $filetype);
210 if (!$Image->isValid()) {
211 $msg = L10n::t('Unable to process image.');
213 echo json_encode(['error' => $msg]);
221 $Image->orient($src);
224 $max_length = Config::get('system', 'max_image_length');
226 $max_length = MAX_IMAGE_LENGTH;
228 if ($max_length > 0) {
229 $Image->scaleDown($max_length);
230 Logger::log("File upload: Scaling picture to new size " . $max_length, Logger::DEBUG);
233 $width = $Image->getWidth();
234 $height = $Image->getHeight();
236 $hash = Photo::newResource();
240 // If we don't have an album name use the Wall Photos album
241 if (!strlen($album)) {
242 $album = L10n::t('Wall Photos');
245 $defperm = '<' . $default_cid . '>';
247 $r = Photo::store($Image, $page_owner_uid, $visitor, $hash, $filename, $album, 0, 0, $defperm);
250 $msg = L10n::t('Image upload failed.');
252 echo json_encode(['error' => $msg]);
259 if ($width > 640 || $height > 640) {
260 $Image->scaleDown(640);
261 $r = Photo::store($Image, $page_owner_uid, $visitor, $hash, $filename, $album, 1, 0, $defperm);
267 if ($width > 320 || $height > 320) {
268 $Image->scaleDown(320);
269 $r = Photo::store($Image, $page_owner_uid, $visitor, $hash, $filename, $album, 2, 0, $defperm);
270 if ($r && ($smallest == 0)) {
275 $basename = basename($filename);
278 $r = q("SELECT `id`, `datasize`, `width`, `height`, `type` FROM `photo`
279 WHERE `resource-id` = '%s'
280 ORDER BY `width` DESC LIMIT 1",
285 echo json_encode(['error' => '']);
292 $picture["id"] = $r[0]["id"];
293 $picture["size"] = $r[0]["datasize"];
294 $picture["width"] = $r[0]["width"];
295 $picture["height"] = $r[0]["height"];
296 $picture["type"] = $r[0]["type"];
297 $picture["albumpage"] = System::baseUrl() . '/photos/' . $page_owner_nick . '/image/' . $hash;
298 $picture["picture"] = System::baseUrl() . "/photo/{$hash}-0." . $Image->getExt();
299 $picture["preview"] = System::baseUrl() . "/photo/{$hash}-{$smallest}." . $Image->getExt();
302 echo json_encode(['picture' => $picture]);
305 Logger::log("upload done", Logger::DEBUG);
309 Logger::log("upload done", Logger::DEBUG);
312 echo json_encode(['ok' => true]);
316 echo "\n\n" . '[url=' . System::baseUrl() . '/photos/' . $page_owner_nick . '/image/' . $hash . '][img]' . System::baseUrl() . "/photo/{$hash}-{$smallest}.".$Image->getExt()."[/img][/url]\n\n";