4 * @file mod/wall_upload.php
5 * @brief Module for uploading a picture to the profile wall
7 * By default the picture will be stored in the photo album with the name Wall Photos.
8 * You can specify a different album by adding an optional query string "album="
13 use Friendica\Core\System;
14 use Friendica\Core\Config;
16 require_once 'include/Photo.php';
18 function wall_upload_post(App $a, $desktopmode = true) {
20 logger("wall upload: starting new upload", LOGGER_DEBUG);
22 $r_json = (x($_GET, 'response') && $_GET['response'] == 'json');
23 $album = (x($_GET, 'album') ? notags(trim($_GET['album'])) : '');
26 if (! x($_FILES, 'media')) {
28 $r = q("SELECT `user`.*, `contact`.`id` FROM `user`
29 INNER JOIN `contact` on `user`.`uid` = `contact`.`uid`
30 WHERE `user`.`nickname` = '%s' AND `user`.`blocked` = 0
31 AND `contact`.`self` = 1 LIMIT 1",
35 if (! dbm::is_result($r)) {
37 echo json_encode(array('error'=>t('Invalid request.')));
43 $user_info = api_get_user($a);
44 $r = q("SELECT `user`.*, `contact`.`id` FROM `user`
45 INNER JOIN `contact` on `user`.`uid` = `contact`.`uid`
46 WHERE `user`.`nickname` = '%s' AND `user`.`blocked` = 0
47 AND `contact`.`self` = 1 LIMIT 1",
48 dbesc($user_info['screen_name'])
53 echo json_encode(array('error'=>t('Invalid request.')));
60 * Setup permissions structures
65 $page_owner_uid = $r[0]['uid'];
66 $default_cid = $r[0]['id'];
67 $page_owner_nick = $r[0]['nickname'];
68 $community_page = (($r[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
70 if ((local_user()) && (local_user() == $page_owner_uid)) {
73 if ($community_page && remote_user()) {
75 if (is_array($_SESSION['remote'])) {
76 foreach ($_SESSION['remote'] as $v) {
77 if ($v['uid'] == $page_owner_uid) {
78 $contact_id = $v['cid'];
85 $r = q("SELECT `uid` FROM `contact`
86 WHERE `blocked` = 0 AND `pending` = 0
87 AND `id` = %d AND `uid` = %d LIMIT 1",
89 intval($page_owner_uid)
91 if (dbm::is_result($r)) {
93 $visitor = $contact_id;
102 echo json_encode(array('error'=>t('Permission denied.')));
105 notice(t('Permission denied.') . EOL);
109 if (! x($_FILES, 'userfile') && ! x($_FILES, 'media')) {
111 echo json_encode(array('error'=>t('Invalid request.')));
117 if (x($_FILES, 'userfile')) {
118 $src = $_FILES['userfile']['tmp_name'];
119 $filename = basename($_FILES['userfile']['name']);
120 $filesize = intval($_FILES['userfile']['size']);
121 $filetype = $_FILES['userfile']['type'];
123 } elseif (x($_FILES, 'media')) {
124 if (is_array($_FILES['media']['tmp_name'])) {
125 $src = $_FILES['media']['tmp_name'][0];
127 $src = $_FILES['media']['tmp_name'];
130 if (is_array($_FILES['media']['name'])) {
131 $filename = basename($_FILES['media']['name'][0]);
133 $filename = basename($_FILES['media']['name']);
136 if (is_array($_FILES['media']['size'])) {
137 $filesize = intval($_FILES['media']['size'][0]);
139 $filesize = intval($_FILES['media']['size']);
142 if (is_array($_FILES['media']['type'])) {
143 $filetype = $_FILES['media']['type'][0];
145 $filetype = $_FILES['media']['type'];
151 echo json_encode(array('error'=>t('Invalid request.')));
154 notice(t('Invalid request.').EOL);
158 // This is a special treatment for picture upload from Twidere
159 if (($filename == "octet-stream") && ($filetype != "")) {
160 $filename = $filetype;
165 $filetype=guess_image_type($filename);
168 // If there is a temp name, then do a manual check
169 // This is more reliable than the provided value
171 $imagedata = getimagesize($src);
173 $filetype = $imagedata['mime'];
176 logger("File upload src: " . $src . " - filename: " . $filename .
177 " - size: " . $filesize . " - type: " . $filetype, LOGGER_DEBUG);
179 $maximagesize = Config::get('system', 'maximagesize');
181 if (($maximagesize) && ($filesize > $maximagesize)) {
182 $msg = sprintf(t('Image exceeds size limit of %s'), formatBytes($maximagesize));
184 echo json_encode(array('error'=>$msg));
192 $imagedata = @file_get_contents($src);
193 $ph = new Photo($imagedata, $filetype);
195 if (! $ph->is_valid()) {
196 $msg = t('Unable to process image.');
198 echo json_encode(array('error'=>$msg));
209 $max_length = Config::get('system', 'max_image_length');
211 $max_length = MAX_IMAGE_LENGTH;
213 if ($max_length > 0) {
214 $ph->scaleImage($max_length);
215 logger("File upload: Scaling picture to new size " . $max_length, LOGGER_DEBUG);
218 $width = $ph->getWidth();
219 $height = $ph->getHeight();
221 $hash = photo_new_resource();
225 // If we don't have an album name use the Wall Photos album
226 if (! strlen($album)) {
227 $album = t('Wall Photos');
230 $defperm = '<' . $default_cid . '>';
232 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, $album, 0, 0, $defperm);
235 $msg = t('Image upload failed.');
237 echo json_encode(array('error'=>$msg));
244 if ($width > 640 || $height > 640) {
245 $ph->scaleImage(640);
246 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, $album, 1, 0, $defperm);
252 if ($width > 320 || $height > 320) {
253 $ph->scaleImage(320);
254 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, $album, 2, 0, $defperm);
255 if ($r && ($smallest == 0)) {
260 $basename = basename($filename);
263 $r = q("SELECT `id`, `datasize`, `width`, `height`, `type` FROM `photo`
264 WHERE `resource-id` = '%s'
265 ORDER BY `width` DESC LIMIT 1",
270 echo json_encode(array('error'=>''));
277 $picture["id"] = $r[0]["id"];
278 $picture["size"] = $r[0]["datasize"];
279 $picture["width"] = $r[0]["width"];
280 $picture["height"] = $r[0]["height"];
281 $picture["type"] = $r[0]["type"];
282 $picture["albumpage"] = System::baseUrl() . '/photos/' . $page_owner_nick . '/image/' . $hash;
283 $picture["picture"] = System::baseUrl() . "/photo/{$hash}-0." . $ph->getExt();
284 $picture["preview"] = System::baseUrl() . "/photo/{$hash}-{$smallest}." . $ph->getExt();
287 echo json_encode(array('picture'=>$picture));
295 echo json_encode(array('ok'=>true));
299 /* mod Waitman Gobble NO WARRANTY */
300 // if we get the signal then return the image url info in BBCODE
301 if ($_REQUEST['hush']!='yeah') {
302 echo "\n\n" . '[url=' . System::baseUrl() . '/photos/' . $page_owner_nick . '/image/' . $hash . '][img]' . System::baseUrl() . "/photo/{$hash}-{$smallest}.".$ph->getExt()."[/img][/url]\n\n";
304 $m = '[url='.System::baseUrl().'/photos/'.$page_owner_nick.'/image/'.$hash.'][img]'.System::baseUrl()."/photo/{$hash}-{$smallest}.".$ph->getExt()."[/img][/url]";
307 /* mod Waitman Gobble NO WARRANTY */