// CHV\Image::uploadToWebsite($source, 'username', [params]) to inject API uploads to a given username - $uploaded_id = CHV\Image::uploadToWebsite($source); + $uploaded_id = CHV\Image::uploadToWebsite($source, $user, array('album_id'=>$album));
/* -------------------------------------------------------------------- This file is part of Chevereto Free. https://chevereto.com/free (c) Rodolfo Berrios <rodolfo@chevereto.com> For the full copyright and license information, please view the LICENSE file that was distributed with this source code. --------------------------------------------------------------------- */
/* API v1 : PLEASE NOTE This API v1 is currently just a bridge to port to Chevereto 3 the API from Chevereto 2. From now on Chevereto 2 API will be named API v1 In future releases there will be an API v2 which will add methods like create user, create albums, etc. */
$route = function ($handler) { try { $version = $handler->request[0]; $action = $handler->request[1]; $user = $_REQUEST['user']; // 新增 $album = $_REQUEST['album']; //新增 if (is_null(CHV\getSetting('api_v1_key')) or CHV\getSetting('api_v1_key') == '') { thrownewException("API v1 key can't be null. Go to your dashboard and set the API v1 key.", 0); }
// Change CHV\getSetting('api_v1_key') to 'something' if you want to use 'something' as key if (!G\timing_safe_compare(CHV\getSetting('api_v1_key'), $_REQUEST['key'])) { thrownewException("Invalid API v1 key.", 100); }
if (!in_array($version, [1])) { thrownewException('Invalid API version.', 110); }
$version_to_actions = [ 1 => ['upload'] ];
if (!in_array($action, $version_to_actions[$version])) { thrownewException('Invalid API action.', 120); }
// From _GET source should be urlencoded base64 if (!G\timing_safe_compare(base64_encode(base64_decode($source)), $source)) { thrownewException('Invalid base64 string.', 120); }
// Set the API temp file $api_temp_file = @tempnam(sys_get_temp_dir(), 'chvtemp');
if (!$api_temp_fileor !@is_writable($api_temp_file)) { thrownewUploadException("Can't get a tempnam.", 200); }
$fh = fopen($api_temp_file, 'w'); stream_filter_append($fh, 'convert.base64-decode', STREAM_FILTER_WRITE); if (!@fwrite($fh, $source)) { thrownewException('Invalid base64 string.', 130); } else { // Since all the validations works with $_FILES, we're going to emulate it. $source = array( 'name' => G\random_string(12).'.jpg', 'type' => 'image/jpeg', 'tmp_name' => $api_temp_file, 'error' => 'UPLOAD_ERR_OK', 'size' => '1' ); } fclose($fh); break; }
// CHV\Image::uploadToWebsite($source, 'username', [params]) to inject API uploads to a given username //$uploaded_id = CHV\Image::uploadToWebsite($source); $uploaded_id = CHV\Image::uploadToWebsite($source, $user, array('album_id'=>$album)); $json_array['status_code'] = 200; $json_array['success'] = array('message' => 'image uploaded', 'code' => 200); $image = CHV\Image::formatArray(CHV\Image::getSingle($uploaded_id, false, false), true); if (!$image['is_approved']) { unset($image['image']['url'], $image['thumb']['url'], $image['medium']['url'], $image['url'], $image['display_url']); } $json_array['image'] = $image;