3 require_once('include/Photo.php');
5 if(! function_exists('wall_upload_post')) {
6 function wall_upload_post(&$a, $desktopmode = true) {
8 logger("wall upload: starting new upload", LOGGER_DEBUG);
10 $r_json = (x($_GET,'response') && $_GET['response']=='json');
13 if(! x($_FILES,'media')) {
15 $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",
21 echo json_encode(array('error'=>t('Invalid request.')));
27 $user_info = api_get_user($a);
28 $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",
29 dbesc($user_info['screen_name'])
34 echo json_encode(array('error'=>t('Invalid request.')));
43 $page_owner_uid = $r[0]['uid'];
44 $default_cid = $r[0]['id'];
45 $page_owner_nick = $r[0]['nickname'];
46 $community_page = (($r[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
48 if((local_user()) && (local_user() == $page_owner_uid))
51 if($community_page && remote_user()) {
53 if(is_array($_SESSION['remote'])) {
54 foreach($_SESSION['remote'] as $v) {
55 if($v['uid'] == $page_owner_uid) {
63 $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
65 intval($page_owner_uid)
78 echo json_encode(array('error'=>t('Permission denied.')));
81 notice( t('Permission denied.') . EOL );
85 if(! x($_FILES,'userfile') && ! x($_FILES,'media')){
87 echo json_encode(array('error'=>t('Invalid request.')));
93 if(x($_FILES,'userfile')) {
94 $src = $_FILES['userfile']['tmp_name'];
95 $filename = basename($_FILES['userfile']['name']);
96 $filesize = intval($_FILES['userfile']['size']);
97 $filetype = $_FILES['userfile']['type'];
99 elseif(x($_FILES,'media')) {
100 if (is_array($_FILES['media']['tmp_name']))
101 $src = $_FILES['media']['tmp_name'][0];
103 $src = $_FILES['media']['tmp_name'];
105 if (is_array($_FILES['media']['name']))
106 $filename = basename($_FILES['media']['name'][0]);
108 $filename = basename($_FILES['media']['name']);
110 if (is_array($_FILES['media']['size']))
111 $filesize = intval($_FILES['media']['size'][0]);
113 $filesize = intval($_FILES['media']['size']);
115 if (is_array($_FILES['media']['type']))
116 $filetype = $_FILES['media']['type'][0];
118 $filetype = $_FILES['media']['type'];
123 echo json_encode(array('error'=>t('Invalid request.')));
126 notice(t('Invalid request.').EOL);
130 // This is a special treatment for picture upload from Twidere
131 if (($filename == "octet-stream") AND ($filetype != "")) {
132 $filename = $filetype;
137 $filetype=guess_image_type($filename);
139 // If there is a temp name, then do a manual check
140 // This is more reliable than the provided value
142 $imagedata = getimagesize($src);
144 $filetype = $imagedata['mime'];
146 logger("File upload src: ".$src." - filename: ".$filename.
147 " - size: ".$filesize." - type: ".$filetype, LOGGER_DEBUG);
149 $maximagesize = get_config('system','maximagesize');
151 if(($maximagesize) && ($filesize > $maximagesize)) {
152 $msg = sprintf( t('Image exceeds size limit of %s'), formatBytes($maximagesize));
154 echo json_encode(array('error'=>$msg));
163 $limit = service_class_fetch($page_owner_uid,'photo_upload_limit');
166 $r = q("select sum(octet_length(data)) as total from photo where uid = %d and scale = 0 and album != 'Contact Photos' ",
167 intval($page_owner_uid)
169 $size = $r[0]['total'];
173 if(($limit !== false) && (($size + strlen($imagedata)) > $limit)) {
174 $msg = upgrade_message(true);
176 echo json_encode(array('error'=>$msg));
185 $imagedata = @file_get_contents($src);
186 $ph = new Photo($imagedata, $filetype);
188 if(! $ph->is_valid()) {
189 $msg = t('Unable to process image.');
191 echo json_encode(array('error'=>$msg));
202 $max_length = get_config('system','max_image_length');
204 $max_length = MAX_IMAGE_LENGTH;
205 if($max_length > 0) {
206 $ph->scaleImage($max_length);
207 logger("File upload: Scaling picture to new size ".$max_length, LOGGER_DEBUG);
210 $width = $ph->getWidth();
211 $height = $ph->getHeight();
213 $hash = photo_new_resource();
217 $defperm = '<' . $default_cid . '>';
219 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, t('Wall Photos'), 0, 0, $defperm);
222 $msg = t('Image upload failed.');
224 echo json_encode(array('error'=>$msg));
231 if($width > 640 || $height > 640) {
232 $ph->scaleImage(640);
233 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, t('Wall Photos'), 1, 0, $defperm);
238 if($width > 320 || $height > 320) {
239 $ph->scaleImage(320);
240 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, t('Wall Photos'), 2, 0, $defperm);
241 if($r AND ($smallest == 0))
245 $basename = basename($filename);
249 $r = q("SELECT `id`, `datasize`, `width`, `height`, `type` FROM `photo` WHERE `resource-id` = '%s' ORDER BY `width` DESC LIMIT 1", $hash);
252 echo json_encode(array('error'=>''));
259 $picture["id"] = $r[0]["id"];
260 $picture["size"] = $r[0]["datasize"];
261 $picture["width"] = $r[0]["width"];
262 $picture["height"] = $r[0]["height"];
263 $picture["type"] = $r[0]["type"];
264 $picture["albumpage"] = $a->get_baseurl().'/photos/'.$page_owner_nick.'/image/'.$hash;
265 $picture["picture"] = $a->get_baseurl()."/photo/{$hash}-0.".$ph->getExt();
266 $picture["preview"] = $a->get_baseurl()."/photo/{$hash}-{$smallest}.".$ph->getExt();
269 echo json_encode(array('picture'=>$picture));
277 echo json_encode(array('ok'=>true));
281 /* mod Waitman Gobble NO WARRANTY */
283 //if we get the signal then return the image url info in BBCODE, otherwise this outputs the info and bails (for the ajax image uploader on wall post)
284 if ($_REQUEST['hush']!='yeah') {
285 if(local_user() && (! feature_enabled(local_user(),'richtext') || x($_REQUEST['nomce'])) ) {
286 echo "\n\n" . '[url=' . $a->get_baseurl() . '/photos/' . $page_owner_nick . '/image/' . $hash . '][img]' . $a->get_baseurl() . "/photo/{$hash}-{$smallest}.".$ph->getExt()."[/img][/url]\n\n";
289 echo '<br /><br /><a href="' . $a->get_baseurl() . '/photos/' . $page_owner_nick . '/image/' . $hash . '" ><img src="' . $a->get_baseurl() . "/photo/{$hash}-{$smallest}.".$ph->getExt()."\" alt=\"$basename\" /></a><br /><br />";
293 $m = '[url='.$a->get_baseurl().'/photos/'.$page_owner_nick.'/image/'.$hash.'][img]'.$a->get_baseurl()."/photo/{$hash}-{$smallest}.".$ph->getExt()."[/img][/url]";
296 /* mod Waitman Gobble NO WARRANTY */