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));
193 $limit = service_class_fetch($page_owner_uid, 'photo_upload_limit');
196 $r = q("SELECT SUM(OCTET_LENGTH(`data`)) AS `total` FROM `photo`
197 WHERE `uid` = %d AND `scale` = 0
198 AND `album` != 'Contact Photos' ",
199 intval($page_owner_uid)
201 $size = $r[0]['total'];
203 if (($size + strlen($imagedata)) > $limit) {
204 $msg = upgrade_message(true);
206 echo json_encode(array('error'=>$msg));
215 $imagedata = @file_get_contents($src);
216 $ph = new Photo($imagedata, $filetype);
218 if (! $ph->is_valid()) {
219 $msg = t('Unable to process image.');
221 echo json_encode(array('error'=>$msg));
232 $max_length = Config::get('system', 'max_image_length');
234 $max_length = MAX_IMAGE_LENGTH;
236 if ($max_length > 0) {
237 $ph->scaleImage($max_length);
238 logger("File upload: Scaling picture to new size " . $max_length, LOGGER_DEBUG);
241 $width = $ph->getWidth();
242 $height = $ph->getHeight();
244 $hash = photo_new_resource();
248 // If we don't have an album name use the Wall Photos album
249 if (! strlen($album)) {
250 $album = t('Wall Photos');
253 $defperm = '<' . $default_cid . '>';
255 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, $album, 0, 0, $defperm);
258 $msg = t('Image upload failed.');
260 echo json_encode(array('error'=>$msg));
267 if ($width > 640 || $height > 640) {
268 $ph->scaleImage(640);
269 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, $album, 1, 0, $defperm);
275 if ($width > 320 || $height > 320) {
276 $ph->scaleImage(320);
277 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, $album, 2, 0, $defperm);
278 if ($r && ($smallest == 0)) {
283 $basename = basename($filename);
286 $r = q("SELECT `id`, `datasize`, `width`, `height`, `type` FROM `photo`
287 WHERE `resource-id` = '%s'
288 ORDER BY `width` DESC LIMIT 1",
293 echo json_encode(array('error'=>''));
300 $picture["id"] = $r[0]["id"];
301 $picture["size"] = $r[0]["datasize"];
302 $picture["width"] = $r[0]["width"];
303 $picture["height"] = $r[0]["height"];
304 $picture["type"] = $r[0]["type"];
305 $picture["albumpage"] = System::baseUrl() . '/photos/' . $page_owner_nick . '/image/' . $hash;
306 $picture["picture"] = System::baseUrl() . "/photo/{$hash}-0." . $ph->getExt();
307 $picture["preview"] = System::baseUrl() . "/photo/{$hash}-{$smallest}." . $ph->getExt();
310 echo json_encode(array('picture'=>$picture));
318 echo json_encode(array('ok'=>true));
322 /* mod Waitman Gobble NO WARRANTY */
323 // if we get the signal then return the image url info in BBCODE
324 if ($_REQUEST['hush']!='yeah') {
325 echo "\n\n" . '[url=' . System::baseUrl() . '/photos/' . $page_owner_nick . '/image/' . $hash . '][img]' . System::baseUrl() . "/photo/{$hash}-{$smallest}.".$ph->getExt()."[/img][/url]\n\n";
327 $m = '[url='.System::baseUrl().'/photos/'.$page_owner_nick.'/image/'.$hash.'][img]'.System::baseUrl()."/photo/{$hash}-{$smallest}.".$ph->getExt()."[/img][/url]";
330 /* mod Waitman Gobble NO WARRANTY */