3 * @file mod/wall_upload.php
4 * 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\Config;
13 use Friendica\Core\Logger;
14 use Friendica\Core\Session;
15 use Friendica\Database\DBA;
17 use Friendica\Model\Photo;
18 use Friendica\Model\User;
19 use Friendica\Object\Image;
20 use Friendica\Util\Images;
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' => DI::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' => DI::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)) {
77 } elseif ($community_page && !empty(Session::getRemoteContactID($page_owner_uid))) {
78 $contact_id = Session::getRemoteContactID($page_owner_uid);
80 $r = q("SELECT `uid` FROM `contact`
81 WHERE `blocked` = 0 AND `pending` = 0
82 AND `id` = %d AND `uid` = %d LIMIT 1",
84 intval($page_owner_uid)
86 if (DBA::isResult($r)) {
88 $visitor = $contact_id;
94 echo json_encode(['error' => DI::l10n()->t('Permission denied.')]);
97 notice(DI::l10n()->t('Permission denied.') . EOL);
101 if (empty($_FILES['userfile']) && empty($_FILES['media'])) {
103 echo json_encode(['error' => DI::l10n()->t('Invalid request.')]);
112 if (!empty($_FILES['userfile'])) {
113 $src = $_FILES['userfile']['tmp_name'];
114 $filename = basename($_FILES['userfile']['name']);
115 $filesize = intval($_FILES['userfile']['size']);
116 $filetype = $_FILES['userfile']['type'];
118 } elseif (!empty($_FILES['media'])) {
119 if (!empty($_FILES['media']['tmp_name'])) {
120 if (is_array($_FILES['media']['tmp_name'])) {
121 $src = $_FILES['media']['tmp_name'][0];
123 $src = $_FILES['media']['tmp_name'];
127 if (!empty($_FILES['media']['name'])) {
128 if (is_array($_FILES['media']['name'])) {
129 $filename = basename($_FILES['media']['name'][0]);
131 $filename = basename($_FILES['media']['name']);
135 if (!empty($_FILES['media']['size'])) {
136 if (is_array($_FILES['media']['size'])) {
137 $filesize = intval($_FILES['media']['size'][0]);
139 $filesize = intval($_FILES['media']['size']);
143 if (!empty($_FILES['media']['type'])) {
144 if (is_array($_FILES['media']['type'])) {
145 $filetype = $_FILES['media']['type'][0];
147 $filetype = $_FILES['media']['type'];
154 echo json_encode(['error' => DI::l10n()->t('Invalid request.')]);
157 notice(DI::l10n()->t('Invalid request.').EOL);
161 // This is a special treatment for picture upload from Twidere
162 if (($filename == "octet-stream") && ($filetype != "")) {
163 $filename = $filetype;
167 if ($filetype == "") {
168 $filetype = Images::guessType($filename);
171 // If there is a temp name, then do a manual check
172 // This is more reliable than the provided value
174 $imagedata = getimagesize($src);
176 $filetype = $imagedata['mime'];
179 Logger::log("File upload src: " . $src . " - filename: " . $filename .
180 " - size: " . $filesize . " - type: " . $filetype, Logger::DEBUG);
182 $maximagesize = Config::get('system', 'maximagesize');
184 if (($maximagesize) && ($filesize > $maximagesize)) {
185 $msg = DI::l10n()->t('Image exceeds size limit of %s', Strings::formatBytes($maximagesize));
187 echo json_encode(['error' => $msg]);
195 $imagedata = @file_get_contents($src);
196 $Image = new Image($imagedata, $filetype);
198 if (!$Image->isValid()) {
199 $msg = DI::l10n()->t('Unable to process image.');
201 echo json_encode(['error' => $msg]);
209 $Image->orient($src);
212 $max_length = Config::get('system', 'max_image_length');
214 $max_length = MAX_IMAGE_LENGTH;
216 if ($max_length > 0) {
217 $Image->scaleDown($max_length);
218 Logger::log("File upload: Scaling picture to new size " . $max_length, Logger::DEBUG);
221 $width = $Image->getWidth();
222 $height = $Image->getHeight();
224 $resource_id = Photo::newResource();
228 // If we don't have an album name use the Wall Photos album
229 if (!strlen($album)) {
230 $album = DI::l10n()->t('Wall Photos');
233 $defperm = '<' . $default_cid . '>';
235 $r = Photo::store($Image, $page_owner_uid, $visitor, $resource_id, $filename, $album, 0, 0, $defperm);
238 $msg = DI::l10n()->t('Image upload failed.');
240 echo json_encode(['error' => $msg]);
247 if ($width > 640 || $height > 640) {
248 $Image->scaleDown(640);
249 $r = Photo::store($Image, $page_owner_uid, $visitor, $resource_id, $filename, $album, 1, 0, $defperm);
255 if ($width > 320 || $height > 320) {
256 $Image->scaleDown(320);
257 $r = Photo::store($Image, $page_owner_uid, $visitor, $resource_id, $filename, $album, 2, 0, $defperm);
258 if ($r && ($smallest == 0)) {
264 $r = q("SELECT `id`, `datasize`, `width`, `height`, `type` FROM `photo`
265 WHERE `resource-id` = '%s'
266 ORDER BY `width` DESC LIMIT 1",
271 echo json_encode(['error' => '']);
278 $picture["id"] = $r[0]["id"];
279 $picture["size"] = $r[0]["datasize"];
280 $picture["width"] = $r[0]["width"];
281 $picture["height"] = $r[0]["height"];
282 $picture["type"] = $r[0]["type"];
283 $picture["albumpage"] = DI::baseUrl() . '/photos/' . $page_owner_nick . '/image/' . $resource_id;
284 $picture["picture"] = DI::baseUrl() . "/photo/{$resource_id}-0." . $Image->getExt();
285 $picture["preview"] = DI::baseUrl() . "/photo/{$resource_id}-{$smallest}." . $Image->getExt();
288 echo json_encode(['picture' => $picture]);
291 Logger::log("upload done", Logger::DEBUG);
295 Logger::log("upload done", Logger::DEBUG);
298 echo json_encode(['ok' => true]);
302 echo "\n\n" . '[url=' . DI::baseUrl() . '/photos/' . $page_owner_nick . '/image/' . $resource_id . '][img]' . DI::baseUrl() . "/photo/{$resource_id}-{$smallest}.".$Image->getExt()."[/img][/url]\n\n";