/** * AJAX behavior to process uploaded images * * @author Jonathan Davis * @return string JSON encoded result with thumbnail id and src **/ public static function images() { $context = false; $error = false; $valid_contexts = array('product', 'category'); if (isset($_FILES['Filedata']['error'])) { $error = $_FILES['Filedata']['error']; } if ($error) { die(json_encode(array('error' => Lookup::errors('uploads', $error)))); } if (isset($_REQUEST['type']) && in_array(strtolower($_REQUEST['type']), $valid_contexts)) { $parent = $_REQUEST['parent']; $context = strtolower($_REQUEST['type']); } if (!$context) { die(json_encode(array('error' => Shopp::__('The file could not be saved because the server cannot tell whether to attach the asset to a product or a category.')))); } if (!@is_uploaded_file($_FILES['Filedata']['tmp_name'])) { die(json_encode(array('error' => Shopp::__('The file could not be saved because the upload was not found on the server.')))); } if (0 == $_FILES['Filedata']['size']) { die(json_encode(array('error' => Shopp::__('The file could not be saved because the uploaded file is empty.')))); } // Save the source image if ('category' == $context) { $Image = new CategoryImage(); } else { $Image = new ProductImage(); } $Image->parent = $parent; $Image->type = 'image'; $Image->name = 'original'; $Image->filename = $_FILES['Filedata']['name']; $context = 'upload'; $tempfile = $_FILES['Filedata']['tmp_name']; if (!@is_readable($tempfile)) { $context = 'file'; $tempfile = get_temp_dir() . $Image->filename; if (!@move_uploaded_file($_FILES['Filedata']['tmp_name'], $tempfile)) { die(json_encode(array('error' => Shopp::__('The file could not be saved because the web server does not have permission to read the upload.')))); } } list($Image->width, $Image->height, $Image->mime, $Image->attr) = getimagesize($tempfile); $Image->mime = image_type_to_mime_type($Image->mime); $Image->size = filesize($tempfile); if (!$Image->unique()) { die(json_encode(array('error' => Shopp::__('The image already exists, but a new filename could not be generated.')))); } $Image->store($tempfile, $context); if ('file' == $context) { unlink($tempfile); } $Error = ShoppErrors()->code('storage_engine_save'); if (!empty($Error)) { die(json_encode(array('error' => $Error->message(true)))); } $Image->save(); if (empty($Image->id)) { die(json_encode(array('error' => Shopp::__('The image reference was not saved to the database.')))); } echo json_encode(array('id' => $Image->id)); exit; }