3 require_once('include/Photo.php');
5 function wall_upload_post(&$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'];
172 if(($limit !== false) && (($size + strlen($imagedata)) > $limit)) {
173 $msg = upgrade_message(true);
175 echo json_encode(array('error'=>$msg));
184 $imagedata = @file_get_contents($src);
185 $ph = new Photo($imagedata, $filetype);
187 if(! $ph->is_valid()) {
188 $msg = t('Unable to process image.');
190 echo json_encode(array('error'=>$msg));
201 $max_length = get_config('system','max_image_length');
203 $max_length = MAX_IMAGE_LENGTH;
204 if($max_length > 0) {
205 $ph->scaleImage($max_length);
206 logger("File upload: Scaling picture to new size ".$max_length, LOGGER_DEBUG);
209 $width = $ph->getWidth();
210 $height = $ph->getHeight();
212 $hash = photo_new_resource();
216 $defperm = '<' . $default_cid . '>';
218 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, t('Wall Photos'), 0, 0, $defperm);
221 $msg = t('Image upload failed.');
223 echo json_encode(array('error'=>$msg));
230 if($width > 640 || $height > 640) {
231 $ph->scaleImage(640);
232 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, t('Wall Photos'), 1, 0, $defperm);
237 if($width > 320 || $height > 320) {
238 $ph->scaleImage(320);
239 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, t('Wall Photos'), 2, 0, $defperm);
240 if($r AND ($smallest == 0))
244 $basename = basename($filename);
248 $r = q("SELECT `id`, `datasize`, `width`, `height`, `type` FROM `photo` WHERE `resource-id` = '%s' ORDER BY `width` DESC LIMIT 1", $hash);
251 echo json_encode(array('error'=>''));
258 $picture["id"] = $r[0]["id"];
259 $picture["size"] = $r[0]["datasize"];
260 $picture["width"] = $r[0]["width"];
261 $picture["height"] = $r[0]["height"];
262 $picture["type"] = $r[0]["type"];
263 $picture["albumpage"] = App::get_baseurl().'/photos/'.$page_owner_nick.'/image/'.$hash;
264 $picture["picture"] = App::get_baseurl()."/photo/{$hash}-0.".$ph->getExt();
265 $picture["preview"] = App::get_baseurl()."/photo/{$hash}-{$smallest}.".$ph->getExt();
268 echo json_encode(array('picture'=>$picture));
276 echo json_encode(array('ok'=>true));
280 /* mod Waitman Gobble NO WARRANTY */
282 //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)
283 if ($_REQUEST['hush']!='yeah') {
284 if(local_user() && (! feature_enabled(local_user(),'richtext') || x($_REQUEST['nomce'])) ) {
285 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";
288 echo '<br /><br /><a href="' . App::get_baseurl() . '/photos/' . $page_owner_nick . '/image/' . $hash . '" ><img src="' . App::get_baseurl() . "/photo/{$hash}-{$smallest}.".$ph->getExt()."\" alt=\"$basename\" /></a><br /><br />";
292 $m = '[url='.App::get_baseurl().'/photos/'.$page_owner_nick.'/image/'.$hash.'][img]'.App::get_baseurl()."/photo/{$hash}-{$smallest}.".$ph->getExt()."[/img][/url]";
295 /* mod Waitman Gobble NO WARRANTY */