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\Config;
15 require_once 'include/Photo.php';
17 function wall_upload_post(App $a, $desktopmode = true) {
19 logger("wall upload: starting new upload", LOGGER_DEBUG);
21 $r_json = (x($_GET, 'response') && $_GET['response'] == 'json');
22 $album = (x($_GET, 'album') ? notags(trim($_GET['album'])) : '');
25 if (! x($_FILES, 'media')) {
27 $r = q("SELECT `user`.*, `contact`.`id` FROM `user`
28 INNER JOIN `contact` on `user`.`uid` = `contact`.`uid`
29 WHERE `user`.`nickname` = '%s' AND `user`.`blocked` = 0
30 AND `contact`.`self` = 1 LIMIT 1",
34 if (! dbm::is_result($r)) {
36 echo json_encode(array('error'=>t('Invalid request.')));
42 $user_info = api_get_user($a);
43 $r = q("SELECT `user`.*, `contact`.`id` FROM `user`
44 INNER JOIN `contact` on `user`.`uid` = `contact`.`uid`
45 WHERE `user`.`nickname` = '%s' AND `user`.`blocked` = 0
46 AND `contact`.`self` = 1 LIMIT 1",
47 dbesc($user_info['screen_name'])
52 echo json_encode(array('error'=>t('Invalid request.')));
59 * Setup permissions structures
64 $page_owner_uid = $r[0]['uid'];
65 $default_cid = $r[0]['id'];
66 $page_owner_nick = $r[0]['nickname'];
67 $community_page = (($r[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
69 if ((local_user()) && (local_user() == $page_owner_uid)) {
72 if ($community_page && remote_user()) {
74 if (is_array($_SESSION['remote'])) {
75 foreach ($_SESSION['remote'] as $v) {
76 if ($v['uid'] == $page_owner_uid) {
77 $contact_id = $v['cid'];
84 $r = q("SELECT `uid` FROM `contact`
85 WHERE `blocked` = 0 AND `pending` = 0
86 AND `id` = %d AND `uid` = %d LIMIT 1",
88 intval($page_owner_uid)
90 if (dbm::is_result($r)) {
92 $visitor = $contact_id;
101 echo json_encode(array('error'=>t('Permission denied.')));
104 notice(t('Permission denied.') . EOL);
108 if (! x($_FILES, 'userfile') && ! x($_FILES, 'media')) {
110 echo json_encode(array('error'=>t('Invalid request.')));
116 if (x($_FILES, 'userfile')) {
117 $src = $_FILES['userfile']['tmp_name'];
118 $filename = basename($_FILES['userfile']['name']);
119 $filesize = intval($_FILES['userfile']['size']);
120 $filetype = $_FILES['userfile']['type'];
122 } elseif (x($_FILES, 'media')) {
123 if (is_array($_FILES['media']['tmp_name'])) {
124 $src = $_FILES['media']['tmp_name'][0];
126 $src = $_FILES['media']['tmp_name'];
129 if (is_array($_FILES['media']['name'])) {
130 $filename = basename($_FILES['media']['name'][0]);
132 $filename = basename($_FILES['media']['name']);
135 if (is_array($_FILES['media']['size'])) {
136 $filesize = intval($_FILES['media']['size'][0]);
138 $filesize = intval($_FILES['media']['size']);
141 if (is_array($_FILES['media']['type'])) {
142 $filetype = $_FILES['media']['type'][0];
144 $filetype = $_FILES['media']['type'];
150 echo json_encode(array('error'=>t('Invalid request.')));
153 notice(t('Invalid request.').EOL);
157 // This is a special treatment for picture upload from Twidere
158 if (($filename == "octet-stream") && ($filetype != "")) {
159 $filename = $filetype;
164 $filetype=guess_image_type($filename);
167 // If there is a temp name, then do a manual check
168 // This is more reliable than the provided value
170 $imagedata = getimagesize($src);
172 $filetype = $imagedata['mime'];
175 logger("File upload src: " . $src . " - filename: " . $filename .
176 " - size: " . $filesize . " - type: " . $filetype, LOGGER_DEBUG);
178 $maximagesize = Config::get('system', 'maximagesize');
180 if (($maximagesize) && ($filesize > $maximagesize)) {
181 $msg = sprintf(t('Image exceeds size limit of %s'), formatBytes($maximagesize));
183 echo json_encode(array('error'=>$msg));
192 $limit = service_class_fetch($page_owner_uid, 'photo_upload_limit');
195 $r = q("SELECT SUM(OCTET_LENGTH(`data`)) AS `total` FROM `photo`
196 WHERE `uid` = %d AND `scale` = 0
197 AND `album` != 'Contact Photos' ",
198 intval($page_owner_uid)
200 $size = $r[0]['total'];
202 if (($size + strlen($imagedata)) > $limit) {
203 $msg = upgrade_message(true);
205 echo json_encode(array('error'=>$msg));
214 $imagedata = @file_get_contents($src);
215 $ph = new Photo($imagedata, $filetype);
217 if (! $ph->is_valid()) {
218 $msg = t('Unable to process image.');
220 echo json_encode(array('error'=>$msg));
231 $max_length = Config::get('system', 'max_image_length');
233 $max_length = MAX_IMAGE_LENGTH;
235 if ($max_length > 0) {
236 $ph->scaleImage($max_length);
237 logger("File upload: Scaling picture to new size " . $max_length, LOGGER_DEBUG);
240 $width = $ph->getWidth();
241 $height = $ph->getHeight();
243 $hash = photo_new_resource();
247 // If we don't have an album name use the Wall Photos album
248 if (! strlen($album)) {
249 $album = t('Wall Photos');
252 $defperm = '<' . $default_cid . '>';
254 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, $album, 0, 0, $defperm);
257 $msg = t('Image upload failed.');
259 echo json_encode(array('error'=>$msg));
266 if ($width > 640 || $height > 640) {
267 $ph->scaleImage(640);
268 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, $album, 1, 0, $defperm);
274 if ($width > 320 || $height > 320) {
275 $ph->scaleImage(320);
276 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, $album, 2, 0, $defperm);
277 if ($r && ($smallest == 0)) {
282 $basename = basename($filename);
285 $r = q("SELECT `id`, `datasize`, `width`, `height`, `type` FROM `photo`
286 WHERE `resource-id` = '%s'
287 ORDER BY `width` DESC LIMIT 1",
292 echo json_encode(array('error'=>''));
299 $picture["id"] = $r[0]["id"];
300 $picture["size"] = $r[0]["datasize"];
301 $picture["width"] = $r[0]["width"];
302 $picture["height"] = $r[0]["height"];
303 $picture["type"] = $r[0]["type"];
304 $picture["albumpage"] = App::get_baseurl() . '/photos/' . $page_owner_nick . '/image/' . $hash;
305 $picture["picture"] = App::get_baseurl() . "/photo/{$hash}-0." . $ph->getExt();
306 $picture["preview"] = App::get_baseurl() . "/photo/{$hash}-{$smallest}." . $ph->getExt();
309 echo json_encode(array('picture'=>$picture));
317 echo json_encode(array('ok'=>true));
321 /* mod Waitman Gobble NO WARRANTY */
322 // if we get the signal then return the image url info in BBCODE
323 if ($_REQUEST['hush']!='yeah') {
324 echo "\n\n" . '[url=' . App::get_baseurl() . '/photos/' . $page_owner_nick . '/image/' . $hash . '][img]' . App::get_baseurl() . "/photo/{$hash}-{$smallest}.".$ph->getExt()."[/img][/url]\n\n";
326 $m = '[url='.App::get_baseurl().'/photos/'.$page_owner_nick.'/image/'.$hash.'][img]'.App::get_baseurl()."/photo/{$hash}-{$smallest}.".$ph->getExt()."[/img][/url]";
329 /* mod Waitman Gobble NO WARRANTY */