[1575] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * DOMPDF - PHP5 HTML to PDF renderer |
---|
| 4 | * |
---|
| 5 | * File: $RCSfile: image_cache.cls.php,v $ |
---|
| 6 | * Created on: 2004-08-08 |
---|
| 7 | * |
---|
| 8 | * Copyright (c) 2004 - Benj Carson <benjcarson@digitaljunkies.ca> |
---|
| 9 | * |
---|
| 10 | * This library is free software; you can redistribute it and/or |
---|
| 11 | * modify it under the terms of the GNU Lesser General Public |
---|
| 12 | * License as published by the Free Software Foundation; either |
---|
| 13 | * version 2.1 of the License, or (at your option) any later version. |
---|
| 14 | * |
---|
| 15 | * This library is distributed in the hope that it will be useful, |
---|
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 18 | * Lesser General Public License for more details. |
---|
| 19 | * |
---|
| 20 | * You should have received a copy of the GNU Lesser General Public License |
---|
| 21 | * along with this library in the file LICENSE.LGPL; if not, write to the |
---|
| 22 | * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
---|
| 23 | * 02111-1307 USA |
---|
| 24 | * |
---|
| 25 | * Alternatively, you may distribute this software under the terms of the |
---|
| 26 | * PHP License, version 3.0 or later. A copy of this license should have |
---|
| 27 | * been distributed with this file in the file LICENSE.PHP . If this is not |
---|
| 28 | * the case, you can obtain a copy at http://www.php.net/license/3_0.txt. |
---|
| 29 | * |
---|
| 30 | * The latest version of DOMPDF might be available at: |
---|
| 31 | * http://www.digitaljunkies.ca/dompdf |
---|
| 32 | * |
---|
| 33 | * @link http://www.digitaljunkies.ca/dompdf |
---|
| 34 | * @copyright 2004 Benj Carson |
---|
| 35 | * @author Benj Carson <benjcarson@digitaljunkies.ca> |
---|
| 36 | * @package dompdf |
---|
| 37 | * @version 0.5.1 |
---|
| 38 | */ |
---|
| 39 | |
---|
| 40 | /* $Id */ |
---|
| 41 | |
---|
| 42 | /** |
---|
| 43 | * Static class that resolves image urls and downloads and caches |
---|
| 44 | * remote images if required. |
---|
| 45 | * |
---|
| 46 | * @access private |
---|
| 47 | * @package dompdf |
---|
| 48 | */ |
---|
| 49 | class Image_Cache { |
---|
| 50 | |
---|
| 51 | /** |
---|
| 52 | * Array of downloaded images. Cached so that identical images are |
---|
| 53 | * not needlessly downloaded. |
---|
| 54 | * |
---|
| 55 | * @var array |
---|
| 56 | */ |
---|
| 57 | static protected $_cache = array(); |
---|
| 58 | |
---|
| 59 | |
---|
| 60 | /** |
---|
| 61 | * Resolve and fetch an image for use. |
---|
| 62 | * |
---|
| 63 | * @param string $url The url of the image |
---|
| 64 | * @param string $proto Default protocol if none specified in $url |
---|
| 65 | * @param string $host Default host if none specified in $url |
---|
| 66 | * @param string $base_path Default path if none specified in $url |
---|
| 67 | * @return array An array with two elements: The local path to the image and the image extension |
---|
| 68 | */ |
---|
| 69 | static function resolve_url($url, $proto, $host, $base_path) { |
---|
| 70 | global $_dompdf_warnings; |
---|
| 71 | |
---|
| 72 | |
---|
| 73 | $resolved_url = null; |
---|
| 74 | |
---|
| 75 | // Remove dynamic part of url to determine the file extension |
---|
| 76 | $tmp = preg_replace('/\?.*/','',$url); |
---|
| 77 | |
---|
| 78 | // We need to preserve the file extenstion |
---|
| 79 | $i = mb_strrpos($tmp, "."); |
---|
| 80 | if ( $i === false ) |
---|
| 81 | throw new DOMPDF_Exception("Unknown image type: $url."); |
---|
| 82 | |
---|
| 83 | $ext = mb_strtolower(mb_substr($tmp, $i+1)); |
---|
| 84 | |
---|
| 85 | $parsed_url = explode_url($url); |
---|
| 86 | |
---|
| 87 | $remote = ($proto != "" && $proto != "file://"); |
---|
| 88 | $remote = $remote || ($parsed_url['protocol'] != ""); |
---|
| 89 | |
---|
| 90 | if ( !DOMPDF_ENABLE_REMOTE && $remote ) { |
---|
| 91 | $resolved_url = DOMPDF_LIB_DIR . "/res/broken_image.png"; |
---|
| 92 | $ext = "png"; |
---|
| 93 | |
---|
| 94 | } else if ( DOMPDF_ENABLE_REMOTE && $remote ) { |
---|
| 95 | // Download remote files to a temporary directory |
---|
| 96 | $url = build_url($proto, $host, $base_path, $url); |
---|
| 97 | |
---|
| 98 | if ( isset(self::$_cache[$url]) ) { |
---|
| 99 | list($resolved_url,$ext) = self::$_cache[$url]; |
---|
| 100 | //echo "Using cached image $url (" . $resolved_url . ")\n"; |
---|
| 101 | |
---|
| 102 | } else { |
---|
| 103 | |
---|
| 104 | //echo "Downloading file $url to temporary location: "; |
---|
| 105 | $resolved_url = tempnam(DOMPDF_TEMP_DIR, "dompdf_img_"); |
---|
| 106 | //echo $resolved_url . "\n"; |
---|
| 107 | |
---|
| 108 | $old_err = set_error_handler("record_warnings"); |
---|
| 109 | $image = file_get_contents($url); |
---|
| 110 | restore_error_handler(); |
---|
| 111 | |
---|
| 112 | if ( strlen($image) == 0 ) { |
---|
| 113 | $image = file_get_contents(DOMPDF_LIB_DIR . "/res/broken_image.png"); |
---|
| 114 | $ext = "png"; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | file_put_contents($resolved_url, $image); |
---|
| 118 | |
---|
| 119 | self::$_cache[$url] = array($resolved_url,$ext); |
---|
| 120 | |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | } else { |
---|
| 124 | |
---|
| 125 | $resolved_url = build_url($proto, $host, $base_path, $url); |
---|
| 126 | |
---|
| 127 | //echo $resolved_url . "\n"; |
---|
| 128 | |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | if ( !is_readable($resolved_url) || !filesize($resolved_url) ) { |
---|
| 132 | $_dompdf_warnings[] = "File " .$resolved_url . " is not readable or is an empty file.\n"; |
---|
| 133 | $resolved_url = DOMPDF_LIB_DIR . "/res/broken_image.png"; |
---|
| 134 | $ext = "png"; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | // Assume for now that all dynamic images are pngs |
---|
| 138 | if ( $ext == "php" ) |
---|
| 139 | $ext = "png"; |
---|
| 140 | |
---|
| 141 | return array($resolved_url, $ext); |
---|
| 142 | |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | /** |
---|
| 146 | * Unlink all cached images (i.e. temporary images either downloaded |
---|
| 147 | * or converted) |
---|
| 148 | */ |
---|
| 149 | static function clear() { |
---|
| 150 | if ( count(self::$_cache) ) { |
---|
| 151 | foreach (self::$_cache as $entry) { |
---|
| 152 | list($file, $ext) = $entry; |
---|
| 153 | unlink($file); |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | } |
---|
| 159 | ?> |
---|