Beispiel #1
0
/**
 * Function tries to get MIME type by different ways.
 *
 * @param string $filename Full path with name to file
 * @param boolean $check_by_extension Try to get MIME type by extension of the file
 * @param string $not_available_result MIME type that will be returned in case all checks fail
 * @return string MIME type of the given file.
 */
function fn_get_mime_content_type($filename, $check_by_extension = true, $not_available_result = 'application/octet-stream')
{
    $type = '';
    if (class_exists('finfo')) {
        $finfo_handler = @finfo_open(FILEINFO_MIME);
        if ($finfo_handler !== false) {
            $type = @finfo_file($finfo_handler, $filename);
            list($type) = explode(';', $type);
            @finfo_close($finfo_handler);
        }
    }
    if (empty($type) && function_exists('mime_content_type')) {
        $type = @mime_content_type($filename);
    }
    if (empty($type) && $check_by_extension && strpos(fn_basename($filename), '.') !== false) {
        $type = fn_get_file_type(fn_basename($filename), $not_available_result);
    }
    return !empty($type) ? $type : $not_available_result;
}
Beispiel #2
0
/**
 * Download the file
 *
 * @param string $path path to the file
 * @param string $filename file name to be displayed in download dialog
 */
function fn_get_file($path, $filename = '')
{
    $fd = fopen($path, 'r');
    if ($fd) {
        //Fixes: Filenames can't be sent to IE if there is any kind of traffic compression enabled on the server side
        if (function_exists('apache_setenv')) {
            apache_setenv('no-gzip', '1');
        }
        ini_set("zlib.output_compression", "Off");
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Cache-Control: private", false);
        header("Content-type: " . fn_get_file_type($path));
        header("Content-Length: " . filesize($path));
        if (empty($filename)) {
            //Fixes: Non-ASCII filenames containing spaces and underscore characters are chunked
            setlocale(LC_ALL, 'en_US.UTF8');
            $filename = basename($path);
        }
        if (USER_AGENT == 'ie') {
            //Fixes: During the file download with IE, non-ASCII filenames appears with a broken encoding
            $filename = rawurlencode($filename);
        }
        header("Content-disposition: attachment; filename=\"{$filename}\"");
        while (!feof($fd)) {
            echo fread($fd, 30000);
            // Read by 30k blocks to avoid memory leaks
            fn_flush();
        }
        exit;
        // stop script execution after reading file contents
    }
    return true;
}
Beispiel #3
0
    $image_path = 'sess_data/' . fn_basename($_REQUEST['image']);
    if (Storage::instance('custom_files')->isExist($image_path)) {
        $real_path = Storage::instance('custom_files')->getAbsolutePath($image_path);
        list(, , $image_type, $tmp_path) = fn_get_image_size($real_path);
        if ($type == 'T') {
            $thumb_path = $image_path . '_thumb';
            if (!Storage::instance('custom_files')->isExist($thumb_path)) {
                // Output a thumbnail image
                list($cont, $format) = fn_resize_image($tmp_path, Registry::get('settings.Thumbnails.product_lists_thumbnail_width'), Registry::get('settings.Thumbnails.product_lists_thumbnail_height'), Registry::get('settings.Thumbnails.thumbnail_background_color'));
                if (!empty($cont)) {
                    Storage::instance('custom_files')->put($thumb_path, array('contents' => $cont));
                }
            }
            $real_path = Storage::instance('custom_files')->getAbsolutePath($thumb_path);
        }
        header('Content-type: ' . $image_type);
        fn_echo(fn_get_contents($real_path));
        exit;
    }
    // Not image file. Display spacer instead.
    header('Content-type: image/gif');
    readfile(fn_get_theme_path('[themes]/[theme]') . '/media/images/spacer.gif');
    exit;
} elseif ($mode == 'thumbnail') {
    $img = fn_generate_thumbnail($_REQUEST['image_path'], $_REQUEST['w'], $_REQUEST['h']);
    if (!empty($img)) {
        header('Content-type: ' . fn_get_file_type($img));
        fn_echo(fn_get_contents($img));
    }
    exit;
}
Beispiel #4
0
 /**
  * Put directory to storage
  *
  * @param  string  $dir    directory to get files from
  * @param  array   $params additional parameters
  * @return boolean true of success, false on fail
  */
 public function putDir($dir, $params = array())
 {
     $s3 = $this->s3();
     // get object to initialize class and get access to contstants below
     $i = 0;
     $max_batch = 10;
     $files = fn_get_dir_contents($dir, false, true, '', '', true);
     fn_set_progress('step_scale', sizeof($files));
     foreach ($files as $source_file) {
         fn_set_progress('echo', '.');
         $i++;
         $data = array('acl' => \AmazonS3::ACL_PUBLIC, 'headers' => array());
         // File can not be accessible via direct link
         if ($this->getOption('secured')) {
             $data['headers']['Content-disposition'] = 'attachment; filename="' . fn_basename($source_file) . '"';
             $data['acl'] = \AmazonS3::ACL_PRIVATE;
         }
         $data['contentType'] = fn_get_file_type($source_file);
         $data['fileUpload'] = $dir . '/' . $source_file;
         $res = $s3->batch()->create_object($this->getOption('bucket'), $this->prefix($source_file), $data);
         if ($i == $max_batch) {
             $s3->batch()->send();
             $i = 0;
         }
     }
     if (!empty($i)) {
         $s3->batch()->send();
         // send the rest of the batch
     }
     return true;
 }
Beispiel #5
0
function fn_price_list_print_product_data($product, &$worksheet, $row, &$width, $selected_fields, $styles, $options_variants = array())
{
    $col = 'A';
    foreach ($selected_fields as $field => $active) {
        $worksheet->getStyle($col . $row)->applyFromArray($row % 2 == 0 ? $styles['field_simple_odd'] : $styles['field_simple']);
        if ($field == 'image') {
            $image_data = fn_image_to_display($product['main_pair'], Registry::get('settings.Thumbnails.product_lists_thumbnail_width'), Registry::get('settings.Thumbnails.product_lists_thumbnail_height'));
            if (!empty($image_data)) {
                $mime_type = fn_get_file_type($image_data['absolute_path']);
                $src = $image_data['absolute_path'];
                $image_width = $image_data['width'];
                $image_height = $image_data['height'];
                if ($mime_type == 'image/gif' && function_exists('imagecreatefromgif')) {
                    $img_res = imagecreatefromgif($src);
                } elseif ($mime_type == 'image/jpeg' && function_exists('imagecreatefromjpeg')) {
                    $img_res = imagecreatefromjpeg($src);
                } elseif ($mime_type == 'image/png' && function_exists('imagecreatefrompng')) {
                    $img_res = imagecreatefrompng($src);
                } else {
                    $img_res = false;
                }
                if ($img_res) {
                    if (!isset($width[$col]) || $width[$col] < $image_width) {
                        $width[$col] = $image_width * IMAGE_WIDTH_PERCENT;
                    }
                    $img_descr = $image_data['alt'];
                    $drawing = new PHPExcel_Worksheet_MemoryDrawing();
                    $drawing->setName($img_descr);
                    $drawing->setDescription($img_descr);
                    $drawing->setImageResource($img_res);
                    $drawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_JPEG);
                    $drawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT);
                    $drawing->setHeight($image_height);
                    $drawing->setWorksheet($worksheet);
                    $worksheet->getRowDimension($row)->setRowHeight($image_height * IMAGE_HEIGHT_PERCENT);
                    $drawing->setCoordinates($col . $row);
                }
            }
        } else {
            if ($field == 'price') {
                $product[$field] = fn_format_price($product[$field], CART_PRIMARY_CURRENCY, null, false);
            }
            if (!isset($width[$col]) || $width[$col] < strlen($product[$field])) {
                $width[$col] = strlen($product[$field]);
            }
            if (!empty($options_variants) && $field == 'product') {
                $options = array();
                foreach ($options_variants as $option_id => $variant_id) {
                    $option = $product['product_options'][$option_id]['option_name'] . ': ' . $product['product_options'][$option_id]['variants'][$variant_id]['variant_name'];
                    $options[] = $option;
                    if ($width[$col] < strlen($option)) {
                        $width[$col] = strlen($options);
                    }
                }
                $options = implode("\n", $options);
                $worksheet->setCellValue($col . $row, $product['product'] . "\n" . $options);
            } elseif ($field == 'price') {
                $worksheet->getCell($col . $row)->setValueExplicit($product[$field], PHPExcel_Cell_DataType::TYPE_STRING);
            } else {
                $worksheet->setCellValue($col . $row, $product[$field]);
            }
        }
        $col++;
    }
    return true;
}
Beispiel #6
0
/**
 * Get data from url
 *
 * @param string $val
 * @return array $val
 */
function fn_get_url_data($val)
{
    if (!preg_match('/:\\/\\//', $val)) {
        $val = 'http://' . $val;
    }
    $result = false;
    $_data = fn_get_contents($val);
    if (!empty($_data)) {
        $result = array('name' => fn_basename($val));
        // Check if the file is dynamically generated
        if (strpos($result['name'], '&') !== false || strpos($result['name'], '?') !== false) {
            $result['name'] = 'url_uploaded_file_' . uniqid(TIME);
        }
        $result['path'] = fn_create_temp_file();
        $result['size'] = strlen($_data);
        $fd = fopen($result['path'], 'wb');
        fwrite($fd, $_data, $result['size']);
        fclose($fd);
        @chmod($result['path'], DEFAULT_FILE_PERMISSIONS);
        $result['type'] = fn_get_mime_content_type($result['path'], false, '');
        if (empty($result['type'])) {
            $result['type'] = fn_get_file_type($result['name']);
        }
        $cache = Registry::get('temp_fs_data');
        if (!isset($cache[$result['path']])) {
            // cache file to allow multiple usage
            $cache[$result['path']] = $result['path'];
            Registry::set('temp_fs_data', $cache);
        }
    }
    return $result;
}