[3019] | 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 | * @contributor Helmut Tischer <htischer@weihenstephan.org> |
---|
| 37 | * @package dompdf |
---|
| 38 | * @version 0.5.1 |
---|
| 39 | * |
---|
| 40 | * Changes |
---|
| 41 | * @contributor Helmut Tischer <htischer@weihenstephan.org> |
---|
| 42 | * @version 0.5.1.htischer.20090507 |
---|
| 43 | * - On getting type of images don't require any file endings |
---|
| 44 | * and don't strip off url parameters, |
---|
| 45 | * to allowing dynamically generated sites with image id |
---|
| 46 | * in url parameters and not at end of url or missing file extension |
---|
| 47 | * @contributor Helmut Tischer <htischer@weihenstephan.org> |
---|
| 48 | * @version dompdf_trunk_with_helmut_mods.20090524 |
---|
| 49 | * - Made debug messages more individually configurable |
---|
| 50 | * @version 20090622 |
---|
| 51 | * - don't cache broken image, but refer to original broken image replacement |
---|
| 52 | */ |
---|
| 53 | |
---|
| 54 | /* $Id */ |
---|
| 55 | |
---|
| 56 | /** |
---|
| 57 | * Static class that resolves image urls and downloads and caches |
---|
| 58 | * remote images if required. |
---|
| 59 | * |
---|
| 60 | * @access private |
---|
| 61 | * @package dompdf |
---|
| 62 | */ |
---|
| 63 | class Image_Cache { |
---|
| 64 | |
---|
| 65 | /** |
---|
| 66 | * Array of downloaded images. Cached so that identical images are |
---|
| 67 | * not needlessly downloaded. |
---|
| 68 | * |
---|
| 69 | * @var array |
---|
| 70 | */ |
---|
| 71 | static protected $_cache = array(); |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | /** |
---|
| 75 | * Resolve and fetch an image for use. |
---|
| 76 | * |
---|
| 77 | * @param string $url The url of the image |
---|
| 78 | * @param string $proto Default protocol if none specified in $url |
---|
| 79 | * @param string $host Default host if none specified in $url |
---|
| 80 | * @param string $base_path Default path if none specified in $url |
---|
| 81 | * @return array An array with two elements: The local path to the image and the image extension |
---|
| 82 | */ |
---|
| 83 | static function resolve_url($url, $proto, $host, $base_path) { |
---|
| 84 | global $_dompdf_warnings; |
---|
| 85 | |
---|
| 86 | $parsed_url = explode_url($url); |
---|
| 87 | |
---|
| 88 | $DEBUGPNG=DEBUGPNG; //=DEBUGPNG; Allow override of global setting for ad hoc debug |
---|
| 89 | $full_url_dbg = ''; |
---|
| 90 | |
---|
| 91 | //debugpng |
---|
| 92 | if ($DEBUGPNG) print 'resolve_url('.$url.','.$proto.','.$host.','.$base_path.')('.$parsed_url['protocol'].')'; |
---|
| 93 | |
---|
| 94 | $remote = ($proto != "" && $proto != "file://"); |
---|
| 95 | $remote = $remote || ($parsed_url['protocol'] != ""); |
---|
| 96 | |
---|
| 97 | if ( !DOMPDF_ENABLE_REMOTE && $remote ) { |
---|
| 98 | $resolved_url = DOMPDF_LIB_DIR . "/res/broken_image.png"; |
---|
| 99 | $ext = "png"; |
---|
| 100 | |
---|
| 101 | //debugpng |
---|
| 102 | if ($DEBUGPNG) $full_url_dbg = '(blockedremote)'; |
---|
| 103 | |
---|
| 104 | } else if ( DOMPDF_ENABLE_REMOTE && $remote ) { |
---|
| 105 | // Download remote files to a temporary directory |
---|
| 106 | $full_url = build_url($proto, $host, $base_path, $url); |
---|
| 107 | |
---|
| 108 | if ( isset(self::$_cache[$full_url]) ) { |
---|
| 109 | list($resolved_url,$ext) = self::$_cache[$full_url]; |
---|
| 110 | |
---|
| 111 | //debugpng |
---|
| 112 | if ($DEBUGPNG) $full_url_dbg = $full_url.'(cache)'; |
---|
| 113 | |
---|
| 114 | } else { |
---|
| 115 | |
---|
| 116 | $resolved_url = tempnam(DOMPDF_TEMP_DIR, "ca_dompdf_img_"); |
---|
| 117 | //debugpng |
---|
| 118 | if ($DEBUGPNG) echo $resolved_url . "\n"; |
---|
| 119 | |
---|
| 120 | $old_err = set_error_handler("record_warnings"); |
---|
| 121 | $image = file_get_contents($full_url); |
---|
| 122 | restore_error_handler(); |
---|
| 123 | |
---|
| 124 | if ( strlen($image) == 0 ) { |
---|
| 125 | //target image not found |
---|
| 126 | $resolved_url = DOMPDF_LIB_DIR . "/res/broken_image.png"; |
---|
| 127 | $ext = "png"; |
---|
| 128 | |
---|
| 129 | //debugpng |
---|
| 130 | if ($DEBUGPNG) $full_url_dbg = $full_url.'(missing)'; |
---|
| 131 | |
---|
| 132 | } else { |
---|
| 133 | |
---|
| 134 | file_put_contents($resolved_url, $image); |
---|
| 135 | |
---|
| 136 | //e.g. fetch.php?media=url.jpg&cache=1 |
---|
| 137 | //- Image file name might be one of the dynamic parts of the url, don't strip off! |
---|
| 138 | // if ( preg_match("/.*\.(\w+)/",$url,$match) ) $ext = $match[1]; |
---|
| 139 | //- a remote url does not need to have a file extension at all |
---|
| 140 | //- local cached file does not have a matching file extension |
---|
| 141 | //Therefore get image type from the content |
---|
| 142 | |
---|
| 143 | $imagedim = getimagesize($resolved_url); |
---|
| 144 | if( $imagedim[2] >= 1 && $imagedim[2] <=3 && $imagedim[0] && $imagedim[1] ) { |
---|
| 145 | //target image is valid |
---|
| 146 | |
---|
| 147 | $imagetypes = array('','gif','jpeg','png','swf'); |
---|
| 148 | $ext = $imagetypes[$imagedim[2]]; |
---|
| 149 | if ( rename($resolved_url,$resolved_url.'.'.$ext) ) { |
---|
| 150 | $resolved_url .= '.'.$ext; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | //Don't put replacement image into cache - otherwise it will be deleted on cache cleanup. |
---|
| 154 | //Only execute on successfull caching of remote image. |
---|
| 155 | self::$_cache[$full_url] = array($resolved_url,$ext); |
---|
| 156 | |
---|
| 157 | } else { |
---|
| 158 | //target image is not valid. |
---|
| 159 | unlink($resolved_url); |
---|
| 160 | |
---|
| 161 | $resolved_url = DOMPDF_LIB_DIR . "/res/broken_image.png"; |
---|
| 162 | $ext = "png"; |
---|
| 163 | } |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | } else { |
---|
| 169 | |
---|
| 170 | $resolved_url = build_url($proto, $host, $base_path, $url); |
---|
| 171 | if ($DEBUGPNG) print 'build_url('.$proto.','.$host.','.$base_path.','.$url.')('.$resolved_url.')'; |
---|
| 172 | |
---|
| 173 | if ( !preg_match("/.*\.(\w+)/",$url,$match) ) { |
---|
| 174 | //debugpng |
---|
| 175 | if ($DEBUGPNG) print '[resolve_url exception '.$url.']'; |
---|
| 176 | throw new DOMPDF_Exception("Unknown image type: $url."); |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | $ext = $match[1]; |
---|
| 180 | |
---|
| 181 | //debugpng |
---|
| 182 | if ($DEBUGPNG) $full_url_dbg = '(local)'; |
---|
| 183 | |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | if ( !is_readable($resolved_url) || !filesize($resolved_url) ) { |
---|
| 187 | |
---|
| 188 | //debugpng |
---|
| 189 | if ($DEBUGPNG) $full_url_dbg .= '(nocache'.$resolved_url.')'; |
---|
| 190 | |
---|
| 191 | $_dompdf_warnings[] = "File " .$resolved_url . " is not readable or is an empty file.\n"; |
---|
| 192 | $resolved_url = DOMPDF_LIB_DIR . "/res/broken_image.png"; |
---|
| 193 | $ext = "png"; |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | //debugpng |
---|
| 197 | if ($DEBUGPNG) print '[resolve_url '.$url.'|'.$full_url_dbg.'|'.$resolved_url.'|'.$ext.']'; |
---|
| 198 | |
---|
| 199 | return array($resolved_url, $ext); |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | /** |
---|
| 203 | * Unlink all cached images (i.e. temporary images either downloaded |
---|
| 204 | * or converted) |
---|
| 205 | */ |
---|
| 206 | static function clear() { |
---|
| 207 | if ( count(self::$_cache) ) { |
---|
| 208 | while ($entry = array_shift(self::$_cache)) { |
---|
| 209 | list($file, $ext) = $entry; |
---|
| 210 | //debugpng |
---|
| 211 | if (DEBUGPNG) print '[clear unlink '.$file.']'; |
---|
| 212 | if (!DEBUGKEEPTEMP) |
---|
| 213 | //XXX: Should we have some kind of fallback or warning if unlink() fails? |
---|
| 214 | unlink($file); |
---|
| 215 | } |
---|
| 216 | } |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | } |
---|
| 220 | ?> |
---|