Example #1
0
    }
} elseif ($mode == 'get_file') {
    if (empty($_REQUEST['file_id']) || empty($_REQUEST['ekey']) && empty($_REQUEST['preview'])) {
        return array(CONTROLLER_STATUS_NO_PAGE);
    }
    $ekey = !empty($_REQUEST['ekey']) ? $_REQUEST['ekey'] : '';
    if (fn_get_product_file($_REQUEST['file_id'], !empty($_REQUEST['preview']), $ekey) == false) {
        return array(CONTROLLER_STATUS_DENIED);
    }
    exit;
    //
    // Display list of files for downloadable product
    //
} elseif ($mode == 'download') {
    if (!empty($_REQUEST['ekey'])) {
        $ekey_info = fn_get_product_edp_info($_REQUEST['product_id'], $_REQUEST['ekey']);
        if (empty($ekey_info)) {
            return array(CONTROLLER_STATUS_DENIED);
        }
        $product = array('ekey' => $_REQUEST['ekey'], 'product_id' => $ekey_info['product_id']);
        if (!empty($product['product_id'])) {
            $product['product'] = db_get_field("SELECT product FROM ?:product_descriptions WHERE product_id = ?i AND lang_code = ?s", $product['product_id'], CART_LANGUAGE);
            $params = array('product_id' => $product['product_id'], 'order_id' => $ekey_info['order_id']);
            $product['files'] = fn_get_product_files($params);
        }
    }
    if (!empty($auth['user_id'])) {
        fn_add_breadcrumb(__('downloads'), "profiles.downloads");
    }
    fn_add_breadcrumb($product['product'], "products.view?product_id={$product['product_id']}");
    fn_add_breadcrumb(__('download'));
/**
 * Download product file
 *
 * @param int $file_id file ID
 * @param boolean $is_preview flag indicates that we download file itself or just preview
 * @param string $ekey temporary key to download file from customer area
 * @param string $area current working area
 * @return file starts to download on success, boolean false in case of fail
 */
function fn_get_product_file($file_id, $is_preview = false, $ekey = '', $area = AREA)
{
    if (!empty($file_id)) {
        $column = $is_preview ? 'preview_path' : 'file_path';
        $file_data = db_get_row("SELECT {$column}, product_id FROM ?:product_files WHERE file_id = ?i", $file_id);
        if (fn_allowed_for('MULTIVENDOR') && $area == 'A' && !fn_company_products_check($file_data['product_id'], true)) {
            return false;
        }
        if (!empty($ekey)) {
            $ekey_info = fn_get_product_edp_info($file_data['product_id'], $ekey);
            if (empty($ekey_info) || $ekey_info['file_id'] != $file_id) {
                return false;
            }
            // Increase downloads for this file
            $max_downloads = db_get_field("SELECT max_downloads FROM ?:product_files WHERE file_id = ?i", $file_id);
            $file_downloads = db_get_field("SELECT downloads FROM ?:product_file_ekeys WHERE ekey = ?s AND file_id = ?i", $ekey, $file_id);
            if (!empty($max_downloads)) {
                if ($file_downloads >= $max_downloads) {
                    return false;
                }
            }
            db_query('UPDATE ?:product_file_ekeys SET ?u WHERE file_id = ?i AND product_id = ?i AND order_id = ?i', array('downloads' => $file_downloads + 1), $file_id, $file_data['product_id'], $ekey_info['order_id']);
        }
        Storage::instance('downloads')->get($file_data['product_id'] . '/' . $file_data[$column]);
    }
    return false;
}
Example #3
0
/**
 * Return files attached to object
 *
 * @param int $product_id ID of product
 * @param bool $preview_check get files only with preview
 * @param int $order_id get order ekeys for the files
 * @return array files
 */
function fn_get_product_files($product_id, $preview_check = false, $order_id = 0)
{
    $fields = array('?:product_files.*', '?:product_file_descriptions.file_name', '?:product_file_descriptions.license', '?:product_file_descriptions.readme');
    $join = db_quote(" LEFT JOIN ?:product_file_descriptions ON ?:product_file_descriptions.file_id = ?:product_files.file_id AND ?:product_file_descriptions.lang_code = ?s", CART_LANGUAGE);
    if (!empty($order_id)) {
        $fields[] = '?:product_file_ekeys.active';
        $fields[] = '?:product_file_ekeys.downloads';
        $fields[] = '?:product_file_ekeys.ekey';
        $join .= db_quote(" LEFT JOIN ?:product_file_ekeys ON ?:product_file_ekeys.file_id = ?:product_files.file_id AND ?:product_file_ekeys.order_id = ?i", $order_id);
        $join .= AREA == 'C' ? " AND ?:product_file_ekeys.active = 'Y'" : '';
    }
    $condition = db_quote("WHERE ?:product_files.product_id = ?i", $product_id);
    if ($preview_check == true) {
        $condition .= " AND preview_path != ''";
    }
    if (AREA == 'C') {
        $condition .= " AND ?:product_files.status = 'A'";
    }
    $files = db_get_array("SELECT " . implode(', ', $fields) . " FROM ?:product_files ?p ?p ORDER BY position", $join, $condition);
    if (!empty($files)) {
        foreach ($files as $k => $file) {
            if (!empty($file['license']) && $file['agreement'] == 'Y') {
                $files[$k]['agreements'] = array($file);
            }
            if (!empty($file['product_id']) && !empty($file['ekey'])) {
                $files[$k]['edp_info'] = fn_get_product_edp_info($file['product_id'], $file['ekey']);
            }
        }
    }
    return $files;
}