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\Config;
13 use Friendica\Core\L10n;
14 use Friendica\Core\Logger;
15 use Friendica\Core\Session;
16 use Friendica\Core\System;
17 use Friendica\Database\DBA;
18 use Friendica\Model\Photo;
19 use Friendica\Model\User;
20 use Friendica\Object\Image;
21 use Friendica\Util\Images;
22 use Friendica\Util\Strings;
24 function wall_upload_post(App $a, $desktopmode = true)
26 Logger::log("wall upload: starting new upload", Logger::DEBUG);
28 $r_json = (!empty($_GET['response']) && $_GET['response'] == 'json');
29 $album = (!empty($_GET['album']) ? Strings::escapeTags(trim($_GET['album'])) : '');
32 if (empty($_FILES['media'])) {
34 $r = q("SELECT `user`.*, `contact`.`id` FROM `user`
35 INNER JOIN `contact` on `user`.`uid` = `contact`.`uid`
36 WHERE `user`.`nickname` = '%s' AND `user`.`blocked` = 0
37 AND `contact`.`self` = 1 LIMIT 1",
41 if (!DBA::isResult($r)) {
43 echo json_encode(['error' => L10n::t('Invalid request.')]);
49 $user_info = api_get_user($a);
50 $r = q("SELECT `user`.*, `contact`.`id` FROM `user`
51 INNER JOIN `contact` on `user`.`uid` = `contact`.`uid`
52 WHERE `user`.`nickname` = '%s' AND `user`.`blocked` = 0
53 AND `contact`.`self` = 1 LIMIT 1",
54 DBA::escape($user_info['screen_name'])
59 echo json_encode(['error' => L10n::t('Invalid request.')]);
66 * Setup permissions structures
71 $page_owner_uid = $r[0]['uid'];
72 $default_cid = $r[0]['id'];
73 $page_owner_nick = $r[0]['nickname'];
74 $community_page = (($r[0]['page-flags'] == User::PAGE_FLAGS_COMMUNITY) ? true : false);
76 if ((local_user()) && (local_user() == $page_owner_uid)) {
78 } elseif ($community_page && !empty(Session::getRemoteContactID($page_owner_uid))) {
79 $contact_id = Session::getRemoteContactID($page_owner_uid);
81 $r = q("SELECT `uid` FROM `contact`
82 WHERE `blocked` = 0 AND `pending` = 0
83 AND `id` = %d AND `uid` = %d LIMIT 1",
85 intval($page_owner_uid)
87 if (DBA::isResult($r)) {
89 $visitor = $contact_id;
95 echo json_encode(['error' => L10n::t('Permission denied.')]);
98 notice(L10n::t('Permission denied.') . EOL);
102 if (empty($_FILES['userfile']) && empty($_FILES['media'])) {
104 echo json_encode(['error' => L10n::t('Invalid request.')]);
113 if (!empty($_FILES['userfile'])) {
114 $src = $_FILES['userfile']['tmp_name'];
115 $filename = basename($_FILES['userfile']['name']);
116 $filesize = intval($_FILES['userfile']['size']);
117 $filetype = $_FILES['userfile']['type'];
119 } elseif (!empty($_FILES['media'])) {
120 if (!empty($_FILES['media']['tmp_name'])) {
121 if (is_array($_FILES['media']['tmp_name'])) {
122 $src = $_FILES['media']['tmp_name'][0];
124 $src = $_FILES['media']['tmp_name'];
128 if (!empty($_FILES['media']['name'])) {
129 if (is_array($_FILES['media']['name'])) {
130 $filename = basename($_FILES['media']['name'][0]);
132 $filename = basename($_FILES['media']['name']);
136 if (!empty($_FILES['media']['size'])) {
137 if (is_array($_FILES['media']['size'])) {
138 $filesize = intval($_FILES['media']['size'][0]);
140 $filesize = intval($_FILES['media']['size']);
144 if (!empty($_FILES['media']['type'])) {
145 if (is_array($_FILES['media']['type'])) {
146 $filetype = $_FILES['media']['type'][0];
148 $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 = Images::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::log("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', Strings::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::log("File upload: Scaling picture to new size " . $max_length, Logger::DEBUG);
222 $width = $Image->getWidth();
223 $height = $Image->getHeight();
225 $resource_id = 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, $resource_id, $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, $resource_id, $filename, $album, 1, 0, $defperm);
256 if ($width > 320 || $height > 320) {
257 $Image->scaleDown(320);
258 $r = Photo::store($Image, $page_owner_uid, $visitor, $resource_id, $filename, $album, 2, 0, $defperm);
259 if ($r && ($smallest == 0)) {
265 $r = q("SELECT `id`, `datasize`, `width`, `height`, `type` FROM `photo`
266 WHERE `resource-id` = '%s'
267 ORDER BY `width` DESC LIMIT 1",
272 echo json_encode(['error' => '']);
279 $picture["id"] = $r[0]["id"];
280 $picture["size"] = $r[0]["datasize"];
281 $picture["width"] = $r[0]["width"];
282 $picture["height"] = $r[0]["height"];
283 $picture["type"] = $r[0]["type"];
284 $picture["albumpage"] = System::baseUrl() . '/photos/' . $page_owner_nick . '/image/' . $resource_id;
285 $picture["picture"] = System::baseUrl() . "/photo/{$resource_id}-0." . $Image->getExt();
286 $picture["preview"] = System::baseUrl() . "/photo/{$resource_id}-{$smallest}." . $Image->getExt();
289 echo json_encode(['picture' => $picture]);
292 Logger::log("upload done", Logger::DEBUG);
296 Logger::log("upload done", Logger::DEBUG);
299 echo json_encode(['ok' => true]);
303 echo "\n\n" . '[url=' . System::baseUrl() . '/photos/' . $page_owner_nick . '/image/' . $resource_id . '][img]' . System::baseUrl() . "/photo/{$resource_id}-{$smallest}.".$Image->getExt()."[/img][/url]\n\n";