%PDF- %PDF-
| Direktori : /usr/share/php/dompdf/include/ |
| Current File : //usr/share/php/dompdf/include/image_cache.cls.php |
<?php
/**
* @package dompdf
* @link http://dompdf.github.com/
* @author Benj Carson <benjcarson@digitaljunkies.ca>
* @author Helmut Tischer <htischer@weihenstephan.org>
* @author Fabien Ménager <fabien.menager@gmail.com>
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
*/
/**
* Static class that resolves image urls and downloads and caches
* remote images if required.
*
* @access private
* @package dompdf
*/
class Image_Cache {
/**
* Array of downloaded images. Cached so that identical images are
* not needlessly downloaded.
*
* @var array
*/
static protected $_cache = array();
/**
* The url to the "broken image" used when images can't be loade
*
* @var string
*/
public static $broken_image;
/**
* Resolve and fetch an image for use.
*
* @param string $url The url of the image
* @param string $protocol Default protocol if none specified in $url
* @param string $host Default host if none specified in $url
* @param string $base_path Default path if none specified in $url
* @param DOMPDF $dompdf The DOMPDF instance
*
* @throws DOMPDF_Image_Exception
* @return array An array with two elements: The local path to the image and the image extension
*/
static function resolve_url($url, $protocol, $host, $base_path, DOMPDF $dompdf) {
$tempfile = null;
$resolved_url = null;
$type = null;
$message = null;
$full_url = null;
$parsed_url = null;
$enable_remote = $dompdf->get_option("enable_remote");
try {
$full_url = build_url($protocol, $host, $base_path, $url);
if ($full_url=== null) {
throw new DOMPDF_Image_Exception("Unable to parse image URL $url.");
}
$parsed_url = explode_url($full_url);
$protocol = mb_strtolower($parsed_url["protocol"]);
$is_data_uri = strpos($protocol, "data:") === 0;
if (!$is_data_uri) {
$allowed_protocols = $dompdf->allowed_protocols();
if (!in_array($protocol, $allowed_protocols)) {
throw new DOMPDF_Image_Exception("Permission denied on $url. The communication protocol $protocol is not supported.");
}
}
// Remote not allowed and is not DataURI
$remote = $protocol && !($protocol === "file://" || $protocol === "");
if ( !$enable_remote && $remote && !$is_data_uri ) {
throw new DOMPDF_Image_Exception("DOMPDF_ENABLE_REMOTE is set to FALSE");
}
// Chroot check for local files
if (($protocol == "" || $protocol === "file://") && !$is_data_uri) {
$realfile = realpath(str_replace("file://", "", $full_url));
$chroot = $dompdf->get_option("chroot");
if ( strpos($realfile, $chroot) !== 0 && (strpos($realfile, realpath(DOMPDF_DIR)) !== 0)) {
throw new DOMPDF_Exception("Permission denied on $full_url. The file could not be found under the directory specified by DOMPDF_CHROOT.");
}
if ( !$realfile ) {
throw new DOMPDF_Exception("File '$full_url' not found.");
}
$full_url = $realfile;
}
if ($protocol === "file://") {
$resolved_url = $full_url;
}
// From cache
elseif ( isset(self::$_cache[$full_url]) ) {
$resolved_url = self::$_cache[$full_url];
}
// From remote
else {
$tmp_dir = $dompdf->get_option("temp_dir");
$resolved_url = tempnam($tmp_dir, "ca_dompdf_img_");
$tempfile = $resolved_url;
$image = "";
if ($is_data_uri) {
if ($parsed_data_uri = parse_data_uri($url)) {
$image = $parsed_data_uri['data'];
}
}
else {
set_error_handler("record_warnings");
$image = file_get_contents($full_url, null, $dompdf->get_http_context());
restore_error_handler();
}
// Image not found or invalid
if ( strlen($image) == 0 ) {
$msg = ($is_data_uri ? "Data-URI could not be parsed" : "Image not found");
throw new DOMPDF_Image_Exception($msg);
}
file_put_contents($resolved_url, $image);
self::$_cache[$full_url] = $resolved_url;
}
// Check if the local file is readable
if ( !is_readable($resolved_url) || !filesize($resolved_url) ) {
throw new DOMPDF_Image_Exception("Image not readable or empty");
}
list($width, $height, $type) = dompdf_getimagesize($resolved_url, $dompdf->get_http_context());
if ( ($width && $height && in_array($type, array(IMAGETYPE_GIF, IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_BMP))) === false ) {
throw new DOMPDF_Image_Exception("Image type unknown");
}
}
catch(DOMPDF_Image_Exception $e) {
if ($tempfile) {
unlink($tempfile);
}
$resolved_url = self::$broken_image;
$type = IMAGETYPE_PNG;
$message = "Image not found or type unknown";
$_dompdf_warnings[] = $e->getMessage()." :: $url";
}
return array($resolved_url, $type, $message);
}
/**
* Unlink all cached images (i.e. temporary images either downloaded
* or converted)
*/
static function clear() {
if ( empty(self::$_cache) || DEBUGKEEPTEMP ) return;
foreach ( self::$_cache as $file ) {
if (DEBUGPNG) print "[clear unlink $file]";
if (file_exists($file)) unlink($file);
}
self::$_cache = array();
}
static function detect_type($file, $context = null) {
list(, , $type) = dompdf_getimagesize($file, $context);
return $type;
}
static function type_to_ext($type) {
$image_types = array(
IMAGETYPE_GIF => "gif",
IMAGETYPE_PNG => "png",
IMAGETYPE_JPEG => "jpeg",
IMAGETYPE_BMP => "bmp",
);
return (isset($image_types[$type]) ? $image_types[$type] : null);
}
static function is_broken($url) {
return $url === self::$broken_image;
}
}
Image_Cache::$broken_image = DOMPDF_LIB_DIR . "/res/broken_image.png";