Example #1
0
 /**
  * AJAX behavior to process uploaded files intended as digital downloads
  *
  * Handles processing a file upload from a temporary file to a
  * the correct storage container (DB, file system, etc)
  *
  * @author Jonathan Davis
  * @return string JSON encoded result with DB id, filename, type & size
  **/
 public static function downloads()
 {
     $error = false;
     if (isset($_FILES['Filedata']['error'])) {
         $error = $_FILES['Filedata']['error'];
     }
     if ($error) {
         die(json_encode(array('error' => Lookup::errors('uploads', $error))));
     }
     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.'))));
     }
     FileAsset::mimetypes();
     // Save the uploaded file
     $File = new ProductDownload();
     $File->parent = 0;
     $File->context = "price";
     $File->type = "download";
     $File->name = $_FILES['Filedata']['name'];
     $File->filename = $File->name;
     list($extension, $mimetype, $properfile) = wp_check_filetype_and_ext($_FILES['Filedata']['tmp_name'], $File->name);
     if (empty($mimetype)) {
         $mimetype = 'application/octet-stream';
     }
     $File->mime = $mimetype;
     if (!empty($properfile)) {
         $File->name = $File->filename = $properfile;
     }
     $File->size = filesize($_FILES['Filedata']['tmp_name']);
     $File->store($_FILES['Filedata']['tmp_name'], 'upload');
     $Error = ShoppErrors()->code('storage_engine_save');
     if (!empty($Error)) {
         die(json_encode(array('error' => $Error->message(true))));
     }
     $File->save();
     do_action('add_product_download', $File, $_FILES['Filedata']);
     echo json_encode(array('id' => $File->id, 'name' => stripslashes($File->name), 'type' => $File->mime, 'size' => $File->size));
 }
Example #2
0
/**
 * shopp_add_product_download
 *
 * Add product download file to a product/variation.
 *
 * @api
 * @since 1.2
 *
 * @param int $product id of the product the download asset will be added to
 * @param string $file full or correct relative path to the download file asset.
 * @param int $variant id of the variant the download asset will be attached to.  For products with variants, this is a required parameter.
 * @return mixed false of failure, the new download asset id on success
 **/
function shopp_add_product_download($product, $file, $variant = false)
{
    if (empty($product) || empty($file)) {
        shopp_debug(__FUNCTION__ . ' failed: One or more missing parameters.');
        return false;
    }
    $File = new ProductDownload();
    $instore = $File->found($file);
    if (!$instore && (!is_file($file) || !is_readable($file))) {
        shopp_debug(__FUNCTION__ . " failed for file {$file}: File missing or unreadable.");
        return false;
    }
    $Product = new ShoppProduct($product);
    if (empty($Product->id)) {
        shopp_debug(__FUNCTION__ . " failed for file {$file}: No such product with id {$product}.");
        return false;
    }
    $Product->load_data(array('summary', 'prices'));
    if ("on" == $Product->variants && false === $variant) {
        shopp_debug(__FUNCTION__ . " failed for file {$file}: You must specify the variant id parameter for product {$product}.");
        return false;
    }
    $Price = reset($Product->prices);
    if (empty($Price->id)) {
        shopp_debug(__FUNCTION__ . " failed for file {$file}: Failed to load product variants.");
        return false;
    }
    if ($variant) {
        $Price = false;
        foreach ($Product->prices as $Price) {
            if ($variant == $Price->id) {
                break;
            }
        }
        if (false === $Price) {
            shopp_debug(__FUNCTION__ . " failed for file {$file}: You must specify a valid variant id parameter for product {$product}.");
            return false;
        }
    }
    // Save the uploaded file
    $File->load(array('type' => 'download', 'parent' => $Price->id));
    $File->parent = $Price->id;
    $File->context = "price";
    $File->type = "download";
    $File->name = basename($file);
    $File->filename = $File->name;
    if (!$instore) {
        $File->mime = file_mimetype($file, $File->name);
        $File->size = filesize($file);
        $File->store($file, 'file');
    } else {
        $File->uri = $file;
        $File->readmeta();
    }
    $File->save();
    if ($File->id) {
        return $File->id;
    }
    shopp_debug(__FUNCTION__ . " failed for file {$file}");
    return false;
}
Example #3
0
	/**
	 * AJAX behavior to process uploaded files intended as digital downloads
	 *
	 * Handles processing a file upload from a temporary file to a
	 * the correct storage container (DB, file system, etc)
	 *	 
	 * @return string JSON encoded result with DB id, filename, type & size
	 **/
	function downloads () {
		$error = false;
		if (isset($_FILES['Filedata']['error'])) $error = $_FILES['Filedata']['error'];
		if ($error) die(json_encode(array("error" => $this->uploadErrors[$error])));

		if (!is_uploaded_file($_FILES['Filedata']['tmp_name']))
			die(json_encode(array("error" => __('The file could not be saved because the upload was not found on the server.','Ecart'))));

		if (!is_readable($_FILES['Filedata']['tmp_name']))
			die(json_encode(array("error" => __('The file could not be saved because the web server does not have permission to read the upload.','Ecart'))));

		if ($_FILES['Filedata']['size'] == 0)
			die(json_encode(array("error" => __('The file could not be saved because the uploaded file is empty.','Ecart'))));

		// Save the uploaded file
		$File = new ProductDownload();
		$File->parent = 0;
		$File->context = "price";
		$File->type = "download";
		$File->name = $_FILES['Filedata']['name'];
		$File->filename = $File->name;
		$File->mime = file_mimetype($_FILES['Filedata']['tmp_name'],$File->name);
		$File->size = filesize($_FILES['Filedata']['tmp_name']);
		$File->store($_FILES['Filedata']['tmp_name'],'upload');
		$File->save();

		do_action('add_product_download',$File,$_FILES['Filedata']);

		echo json_encode(array("id"=>$File->id,"name"=>stripslashes($File->name),"type"=>$File->mime,"size"=>$File->size));
	}