3 require_once('include/Photo.php');
5 function wall_upload_post(App $a, $desktopmode = true) {
7 logger("wall upload: starting new upload", LOGGER_DEBUG);
9 $r_json = (x($_GET,'response') && $_GET['response']=='json');
12 if(! x($_FILES,'media')) {
14 $r = q("SELECT `user`.*, `contact`.`id` FROM `user` INNER JOIN `contact` on `user`.`uid` = `contact`.`uid` WHERE `user`.`nickname` = '%s' AND `user`.`blocked` = 0 and `contact`.`self` = 1 LIMIT 1",
18 if (! dbm::is_result($r)) {
20 echo json_encode(array('error'=>t('Invalid request.')));
26 $user_info = api_get_user($a);
27 $r = q("SELECT `user`.*, `contact`.`id` FROM `user` INNER JOIN `contact` on `user`.`uid` = `contact`.`uid` WHERE `user`.`nickname` = '%s' AND `user`.`blocked` = 0 and `contact`.`self` = 1 LIMIT 1",
28 dbesc($user_info['screen_name'])
33 echo json_encode(array('error'=>t('Invalid request.')));
42 $page_owner_uid = $r[0]['uid'];
43 $default_cid = $r[0]['id'];
44 $page_owner_nick = $r[0]['nickname'];
45 $community_page = (($r[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
47 if((local_user()) && (local_user() == $page_owner_uid))
50 if($community_page && remote_user()) {
52 if(is_array($_SESSION['remote'])) {
53 foreach($_SESSION['remote'] as $v) {
54 if($v['uid'] == $page_owner_uid) {
55 $contact_id = $v['cid'];
62 $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
64 intval($page_owner_uid)
66 if (dbm::is_result($r)) {
68 $visitor = $contact_id;
77 echo json_encode(array('error'=>t('Permission denied.')));
80 notice( t('Permission denied.') . EOL );
84 if(! x($_FILES,'userfile') && ! x($_FILES,'media')){
86 echo json_encode(array('error'=>t('Invalid request.')));
92 if(x($_FILES,'userfile')) {
93 $src = $_FILES['userfile']['tmp_name'];
94 $filename = basename($_FILES['userfile']['name']);
95 $filesize = intval($_FILES['userfile']['size']);
96 $filetype = $_FILES['userfile']['type'];
98 elseif(x($_FILES,'media')) {
99 if (is_array($_FILES['media']['tmp_name']))
100 $src = $_FILES['media']['tmp_name'][0];
102 $src = $_FILES['media']['tmp_name'];
104 if (is_array($_FILES['media']['name']))
105 $filename = basename($_FILES['media']['name'][0]);
107 $filename = basename($_FILES['media']['name']);
109 if (is_array($_FILES['media']['size']))
110 $filesize = intval($_FILES['media']['size'][0]);
112 $filesize = intval($_FILES['media']['size']);
114 if (is_array($_FILES['media']['type']))
115 $filetype = $_FILES['media']['type'][0];
117 $filetype = $_FILES['media']['type'];
122 echo json_encode(array('error'=>t('Invalid request.')));
125 notice(t('Invalid request.').EOL);
129 // This is a special treatment for picture upload from Twidere
130 if (($filename == "octet-stream") AND ($filetype != "")) {
131 $filename = $filetype;
136 $filetype=guess_image_type($filename);
138 // If there is a temp name, then do a manual check
139 // This is more reliable than the provided value
141 $imagedata = getimagesize($src);
143 $filetype = $imagedata['mime'];
145 logger("File upload src: ".$src." - filename: ".$filename.
146 " - size: ".$filesize." - type: ".$filetype, LOGGER_DEBUG);
148 $maximagesize = get_config('system','maximagesize');
150 if(($maximagesize) && ($filesize > $maximagesize)) {
151 $msg = sprintf( t('Image exceeds size limit of %s'), formatBytes($maximagesize));
153 echo json_encode(array('error'=>$msg));
162 $limit = service_class_fetch($page_owner_uid,'photo_upload_limit');
165 $r = q("select sum(octet_length(data)) as total from photo where uid = %d and scale = 0 and album != 'Contact Photos' ",
166 intval($page_owner_uid)
168 $size = $r[0]['total'];
170 if (($size + strlen($imagedata)) > $limit) {
171 $msg = upgrade_message(true);
173 echo json_encode(array('error'=>$msg));
182 $imagedata = @file_get_contents($src);
183 $ph = new Photo($imagedata, $filetype);
185 if(! $ph->is_valid()) {
186 $msg = t('Unable to process image.');
188 echo json_encode(array('error'=>$msg));
199 $max_length = get_config('system','max_image_length');
201 $max_length = MAX_IMAGE_LENGTH;
202 if($max_length > 0) {
203 $ph->scaleImage($max_length);
204 logger("File upload: Scaling picture to new size ".$max_length, LOGGER_DEBUG);
207 $width = $ph->getWidth();
208 $height = $ph->getHeight();
210 $hash = photo_new_resource();
214 $defperm = '<' . $default_cid . '>';
216 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, t('Wall Photos'), 0, 0, $defperm);
219 $msg = t('Image upload failed.');
221 echo json_encode(array('error'=>$msg));
228 if($width > 640 || $height > 640) {
229 $ph->scaleImage(640);
230 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, t('Wall Photos'), 1, 0, $defperm);
235 if($width > 320 || $height > 320) {
236 $ph->scaleImage(320);
237 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, t('Wall Photos'), 2, 0, $defperm);
238 if($r AND ($smallest == 0))
242 $basename = basename($filename);
246 $r = q("SELECT `id`, `datasize`, `width`, `height`, `type` FROM `photo` WHERE `resource-id` = '%s' ORDER BY `width` DESC LIMIT 1", $hash);
249 echo json_encode(array('error'=>''));
256 $picture["id"] = $r[0]["id"];
257 $picture["size"] = $r[0]["datasize"];
258 $picture["width"] = $r[0]["width"];
259 $picture["height"] = $r[0]["height"];
260 $picture["type"] = $r[0]["type"];
261 $picture["albumpage"] = App::get_baseurl().'/photos/'.$page_owner_nick.'/image/'.$hash;
262 $picture["picture"] = App::get_baseurl()."/photo/{$hash}-0.".$ph->getExt();
263 $picture["preview"] = App::get_baseurl()."/photo/{$hash}-{$smallest}.".$ph->getExt();
266 echo json_encode(array('picture'=>$picture));
274 echo json_encode(array('ok'=>true));
278 /* mod Waitman Gobble NO WARRANTY */
280 // if we get the signal then return the image url info in BBCODE
281 if ($_REQUEST['hush']!='yeah') {
282 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";
284 $m = '[url='.App::get_baseurl().'/photos/'.$page_owner_nick.'/image/'.$hash.'][img]'.App::get_baseurl()."/photo/{$hash}-{$smallest}.".$ph->getExt()."[/img][/url]";
287 /* mod Waitman Gobble NO WARRANTY */