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",
19 if ($r_json) { echo json_encode(['error'=>t('Invalid request.')]); killme(); }
23 $user_info = api_get_user($a);
24 $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",
25 dbesc($user_info['screen_name'])
29 if ($r_json) { echo json_encode(['error'=>t('Invalid request.')]); killme(); }
36 $page_owner_uid = $r[0]['uid'];
37 $default_cid = $r[0]['id'];
38 $page_owner_nick = $r[0]['nickname'];
39 $community_page = (($r[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
41 if((local_user()) && (local_user() == $page_owner_uid))
44 if($community_page && remote_user()) {
46 if(is_array($_SESSION['remote'])) {
47 foreach($_SESSION['remote'] as $v) {
48 if($v['uid'] == $page_owner_uid) {
56 $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
58 intval($page_owner_uid)
70 if ($r_json) { echo json_encode(['error'=>t('Permission denied.')]); killme(); }
71 notice( t('Permission denied.') . EOL );
75 if(! x($_FILES,'userfile') && ! x($_FILES,'media')){
76 if ($r_json) { echo json_encode(['error'=>t('Invalid request.')]); killme(); }
81 if(x($_FILES,'userfile')) {
82 $src = $_FILES['userfile']['tmp_name'];
83 $filename = basename($_FILES['userfile']['name']);
84 $filesize = intval($_FILES['userfile']['size']);
85 $filetype = $_FILES['userfile']['type'];
87 elseif(x($_FILES,'media')) {
88 if (is_array($_FILES['media']['tmp_name']))
89 $src = $_FILES['media']['tmp_name'][0];
91 $src = $_FILES['media']['tmp_name'];
93 if (is_array($_FILES['media']['name']))
94 $filename = basename($_FILES['media']['name'][0]);
96 $filename = basename($_FILES['media']['name']);
98 if (is_array($_FILES['media']['size']))
99 $filesize = intval($_FILES['media']['size'][0]);
101 $filesize = intval($_FILES['media']['size']);
103 if (is_array($_FILES['media']['type']))
104 $filetype = $_FILES['media']['type'][0];
106 $filetype = $_FILES['media']['type'];
110 if ($r_json) { echo json_encode(['error'=>t('Invalid request.')]); killme(); }
111 notice(t('Invalid request.').EOL);
115 // This is a special treatment for picture upload from Twidere
116 if (($filename == "octet-stream") AND ($filetype != "")) {
117 $filename = $filetype;
122 $filetype=guess_image_type($filename);
124 // If there is a temp name, then do a manual check
125 // This is more reliable than the provided value
127 $imagedata = getimagesize($src);
129 $filetype = $imagedata['mime'];
131 logger("File upload src: ".$src." - filename: ".$filename.
132 " - size: ".$filesize." - type: ".$filetype, LOGGER_DEBUG);
134 $maximagesize = get_config('system','maximagesize');
136 if(($maximagesize) && ($filesize > $maximagesize)) {
137 $msg = sprintf( t('Image exceeds size limit of %s'), formatBytes($maximagesize));
139 echo json_encode(['error'=>$msg]);
147 $r = q("select sum(octet_length(data)) as total from photo where uid = %d and scale = 0 and album != 'Contact Photos' ",
148 intval($page_owner_uid)
151 $limit = service_class_fetch($page_owner_uid,'photo_upload_limit');
153 if(($limit !== false) && (($r[0]['total'] + strlen($imagedata)) > $limit)) {
154 $msg = upgrade_message(true);
156 echo json_encode(['error'=>$msg]);
165 $imagedata = @file_get_contents($src);
166 $ph = new Photo($imagedata, $filetype);
168 if(! $ph->is_valid()) {
169 $msg = t('Unable to process image.');
171 echo json_encode(['error'=>$msg]);
182 $max_length = get_config('system','max_image_length');
184 $max_length = MAX_IMAGE_LENGTH;
185 if($max_length > 0) {
186 $ph->scaleImage($max_length);
187 logger("File upload: Scaling picture to new size ".$max_length, LOGGER_DEBUG);
190 $width = $ph->getWidth();
191 $height = $ph->getHeight();
193 $hash = photo_new_resource();
197 $defperm = '<' . $default_cid . '>';
199 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, t('Wall Photos'), 0, 0, $defperm);
202 $msg = t('Image upload failed.');
204 echo json_encode(['error'=>$msg]);
211 if($width > 640 || $height > 640) {
212 $ph->scaleImage(640);
213 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, t('Wall Photos'), 1, 0, $defperm);
218 if($width > 320 || $height > 320) {
219 $ph->scaleImage(320);
220 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, t('Wall Photos'), 2, 0, $defperm);
221 if($r AND ($smallest == 0))
225 $basename = basename($filename);
229 $r = q("SELECT `id`, `datasize`, `width`, `height`, `type` FROM `photo` WHERE `resource-id` = '%s' ORDER BY `width` DESC LIMIT 1", $hash);
231 if ($r_json) { echo json_encode(['error'=>'']); killme(); }
236 $picture["id"] = $r[0]["id"];
237 $picture["size"] = $r[0]["datasize"];
238 $picture["width"] = $r[0]["width"];
239 $picture["height"] = $r[0]["height"];
240 $picture["type"] = $r[0]["type"];
241 $picture["albumpage"] = $a->get_baseurl().'/photos/'.$page_owner_nick.'/image/'.$hash;
242 $picture["picture"] = $a->get_baseurl()."/photo/{$hash}-0.".$ph->getExt();
243 $picture["preview"] = $a->get_baseurl()."/photo/{$hash}-{$smallest}.".$ph->getExt();
245 if ($r_json) { echo json_encode(['picture'=>$picture]); killme(); }
249 if ($r_json) { echo json_encode(['ok'=>true]); killme(); }
251 /* mod Waitman Gobble NO WARRANTY */
253 //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)
254 if ($_REQUEST['hush']!='yeah') {
255 if(local_user() && (! feature_enabled(local_user(),'richtext') || x($_REQUEST['nomce'])) ) {
256 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";
259 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 />";
263 $m = '[url='.$a->get_baseurl().'/photos/'.$page_owner_nick.'/image/'.$hash.'][img]'.$a->get_baseurl()."/photo/{$hash}-{$smallest}.".$ph->getExt()."[/img][/url]";
266 /* mod Waitman Gobble NO WARRANTY */