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="
12 use Friendica\Core\Config;
14 require_once 'include/Photo.php';
16 function wall_upload_post(App $a, $desktopmode = true) {
18 logger("wall upload: starting new upload", LOGGER_DEBUG);
20 $r_json = (x($_GET, 'response') && $_GET['response'] == 'json');
21 $album = (x($_GET, 'album') ? notags(trim($_GET['album'])) : '');
24 if (! x($_FILES, 'media')) {
26 $r = q("SELECT `user`.*, `contact`.`id` FROM `user`
27 INNER JOIN `contact` on `user`.`uid` = `contact`.`uid`
28 WHERE `user`.`nickname` = '%s' AND `user`.`blocked` = 0
29 AND `contact`.`self` = 1 LIMIT 1",
33 if (! dbm::is_result($r)) {
35 echo json_encode(array('error'=>t('Invalid request.')));
41 $user_info = api_get_user($a);
42 $r = q("SELECT `user`.*, `contact`.`id` FROM `user`
43 INNER JOIN `contact` on `user`.`uid` = `contact`.`uid`
44 WHERE `user`.`nickname` = '%s' AND `user`.`blocked` = 0
45 AND `contact`.`self` = 1 LIMIT 1",
46 dbesc($user_info['screen_name'])
51 echo json_encode(array('error'=>t('Invalid request.')));
58 * Setup permissions structures
63 $page_owner_uid = $r[0]['uid'];
64 $default_cid = $r[0]['id'];
65 $page_owner_nick = $r[0]['nickname'];
66 $community_page = (($r[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
68 if ((local_user()) && (local_user() == $page_owner_uid)) {
71 if ($community_page && remote_user()) {
73 if (is_array($_SESSION['remote'])) {
74 foreach ($_SESSION['remote'] as $v) {
75 if ($v['uid'] == $page_owner_uid) {
76 $contact_id = $v['cid'];
83 $r = q("SELECT `uid` FROM `contact`
84 WHERE `blocked` = 0 AND `pending` = 0
85 AND `id` = %d AND `uid` = %d LIMIT 1",
87 intval($page_owner_uid)
89 if (dbm::is_result($r)) {
91 $visitor = $contact_id;
100 echo json_encode(array('error'=>t('Permission denied.')));
103 notice(t('Permission denied.') . EOL);
107 if (! x($_FILES, 'userfile') && ! x($_FILES, 'media')) {
109 echo json_encode(array('error'=>t('Invalid request.')));
115 if (x($_FILES, 'userfile')) {
116 $src = $_FILES['userfile']['tmp_name'];
117 $filename = basename($_FILES['userfile']['name']);
118 $filesize = intval($_FILES['userfile']['size']);
119 $filetype = $_FILES['userfile']['type'];
121 } elseif (x($_FILES, 'media')) {
122 if (is_array($_FILES['media']['tmp_name'])) {
123 $src = $_FILES['media']['tmp_name'][0];
125 $src = $_FILES['media']['tmp_name'];
128 if (is_array($_FILES['media']['name'])) {
129 $filename = basename($_FILES['media']['name'][0]);
131 $filename = basename($_FILES['media']['name']);
134 if (is_array($_FILES['media']['size'])) {
135 $filesize = intval($_FILES['media']['size'][0]);
137 $filesize = intval($_FILES['media']['size']);
140 if (is_array($_FILES['media']['type'])) {
141 $filetype = $_FILES['media']['type'][0];
143 $filetype = $_FILES['media']['type'];
149 echo json_encode(array('error'=>t('Invalid request.')));
152 notice(t('Invalid request.').EOL);
156 // This is a special treatment for picture upload from Twidere
157 if (($filename == "octet-stream") && ($filetype != "")) {
158 $filename = $filetype;
163 $filetype=guess_image_type($filename);
166 // If there is a temp name, then do a manual check
167 // This is more reliable than the provided value
169 $imagedata = getimagesize($src);
171 $filetype = $imagedata['mime'];
174 logger("File upload src: " . $src . " - filename: " . $filename .
175 " - size: " . $filesize . " - type: " . $filetype, LOGGER_DEBUG);
177 $maximagesize = Config::get('system', 'maximagesize');
179 if (($maximagesize) && ($filesize > $maximagesize)) {
180 $msg = sprintf(t('Image exceeds size limit of %s'), formatBytes($maximagesize));
182 echo json_encode(array('error'=>$msg));
191 $limit = service_class_fetch($page_owner_uid, 'photo_upload_limit');
194 $r = q("SELECT SUM(OCTET_LENGTH(`data`)) AS `total` FROM `photo`
195 WHERE `uid` = %d AND `scale` = 0
196 AND `album` != 'Contact Photos' ",
197 intval($page_owner_uid)
199 $size = $r[0]['total'];
201 if (($size + strlen($imagedata)) > $limit) {
202 $msg = upgrade_message(true);
204 echo json_encode(array('error'=>$msg));
213 $imagedata = @file_get_contents($src);
214 $ph = new Photo($imagedata, $filetype);
216 if (! $ph->is_valid()) {
217 $msg = t('Unable to process image.');
219 echo json_encode(array('error'=>$msg));
230 $max_length = Config::get('system', 'max_image_length');
232 $max_length = MAX_IMAGE_LENGTH;
234 if ($max_length > 0) {
235 $ph->scaleImage($max_length);
236 logger("File upload: Scaling picture to new size " . $max_length, LOGGER_DEBUG);
239 $width = $ph->getWidth();
240 $height = $ph->getHeight();
242 $hash = photo_new_resource();
246 // If we don't have an album name use the Wall Photos album
247 if (! strlen($album)) {
248 $album = t('Wall Photos');
251 $defperm = '<' . $default_cid . '>';
253 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, $album, 0, 0, $defperm);
256 $msg = t('Image upload failed.');
258 echo json_encode(array('error'=>$msg));
265 if ($width > 640 || $height > 640) {
266 $ph->scaleImage(640);
267 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, $album, 1, 0, $defperm);
273 if ($width > 320 || $height > 320) {
274 $ph->scaleImage(320);
275 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, $album, 2, 0, $defperm);
276 if ($r && ($smallest == 0)) {
281 $basename = basename($filename);
284 $r = q("SELECT `id`, `datasize`, `width`, `height`, `type` FROM `photo`
285 WHERE `resource-id` = '%s'
286 ORDER BY `width` DESC LIMIT 1",
291 echo json_encode(array('error'=>''));
298 $picture["id"] = $r[0]["id"];
299 $picture["size"] = $r[0]["datasize"];
300 $picture["width"] = $r[0]["width"];
301 $picture["height"] = $r[0]["height"];
302 $picture["type"] = $r[0]["type"];
303 $picture["albumpage"] = App::get_baseurl() . '/photos/' . $page_owner_nick . '/image/' . $hash;
304 $picture["picture"] = App::get_baseurl() . "/photo/{$hash}-0." . $ph->getExt();
305 $picture["preview"] = App::get_baseurl() . "/photo/{$hash}-{$smallest}." . $ph->getExt();
308 echo json_encode(array('picture'=>$picture));
316 echo json_encode(array('ok'=>true));
320 /* mod Waitman Gobble NO WARRANTY */
321 // if we get the signal then return the image url info in BBCODE
322 if ($_REQUEST['hush']!='yeah') {
323 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";
325 $m = '[url='.App::get_baseurl().'/photos/'.$page_owner_nick.'/image/'.$hash.'][img]'.App::get_baseurl()."/photo/{$hash}-{$smallest}.".$ph->getExt()."[/img][/url]";
328 /* mod Waitman Gobble NO WARRANTY */