static function resolve_url($url, $proto, $host, $base_path) { $parsed_url = explode_url($url); $message = null; $remote = $proto && $proto !== "file://" || $parsed_url['protocol'] != ""; $datauri = strpos($parsed_url['protocol'], "data:") === 0; try { if (!DOMPDF_ENABLE_REMOTE && $remote && !$datauri) { throw new DOMPDF_Image_Exception("DOMPDF_ENABLE_REMOTE is set to FALSE"); } else { if (DOMPDF_ENABLE_REMOTE && $remote || $datauri) { $full_url = dompdf_build_url($proto, $host, $base_path, $url); if (isset(self::$_cache[$full_url])) { $resolved_url = self::$_cache[$full_url]; } else { $resolved_url = tempnam(DOMPDF_TEMP_DIR, "ca_dompdf_img_"); if ($datauri) { if ($parsed_data_uri = parse_data_uri($url)) { $image = $parsed_data_uri['data']; } } else { $old_err = set_error_handler("record_warnings"); $image = wp_remote_get($full_url); $image = isset($image['body']) ? $image['body'] : ''; restore_error_handler(); } if (strlen($image) == 0) { $msg = $datauri ? "Data-URI could not be parsed" : "Image not found"; throw new DOMPDF_Image_Exception($msg); } else { file_put_contents($resolved_url, $image); } } } else { $resolved_url = dompdf_build_url($proto, $host, $base_path, $url); } } if (!is_readable($resolved_url) || !filesize($resolved_url)) { throw new DOMPDF_Image_Exception("Image not readable or empty"); } else { list($width, $height, $type) = dompdf_getimagesize($resolved_url); if ($width && $height && in_array($type, array(IMAGETYPE_GIF, IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_BMP))) { if (DOMPDF_ENABLE_REMOTE && $remote) { self::$_cache[$full_url] = $resolved_url; } } else { throw new DOMPDF_Image_Exception("Image type unknown"); unlink($resolved_url); } } } catch (DOMPDF_Image_Exception $e) { $resolved_url = self::$broken_image; $type = IMAGETYPE_PNG; $message = $e->getMessage() . " \n {$url}"; } return array($resolved_url, $type, $message); }
/** * Resolve and fetch an image for use. * * @param string $url The url of the image * @param string $proto 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 * @return array An array with two elements: The local path to the image and the image extension */ static function resolve_url($url, $proto, $host, $base_path) { $parsed_url = explode_url($url); $message = null; $remote = $proto && $proto !== "file://" || $parsed_url['protocol'] != ""; $datauri = strpos($parsed_url['protocol'], "data:") === 0; try { // Remote not allowed and is not DataURI if (!DOMPDF_ENABLE_REMOTE && $remote && !$datauri) { throw new DOMPDF_Image_Exception("DOMPDF_ENABLE_REMOTE is set to FALSE"); } else { if (DOMPDF_ENABLE_REMOTE && $remote || $datauri) { // Download remote files to a temporary directory $full_url = build_url($proto, $host, $base_path, $url); // From cache if (isset(self::$_cache[$full_url])) { $resolved_url = self::$_cache[$full_url]; } else { $resolved_url = tempnam(DOMPDF_TEMP_DIR, "ca_dompdf_img_"); if ($datauri) { if ($parsed_data_uri = parse_data_uri($url)) { $image = $parsed_data_uri['data']; } } else { $old_err = set_error_handler("record_warnings"); $image = file_get_contents($full_url); restore_error_handler(); } // Image not found or invalid if (strlen($image) == 0) { $msg = $datauri ? "Data-URI could not be parsed" : "Image not found"; throw new DOMPDF_Image_Exception($msg); } else { //e.g. fetch.php?media=url.jpg&cache=1 //- Image file name might be one of the dynamic parts of the url, don't strip off! //- a remote url does not need to have a file extension at all //- local cached file does not have a matching file extension //Therefore get image type from the content file_put_contents($resolved_url, $image); } } } else { $resolved_url = build_url($proto, $host, $base_path, $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"); } else { list($width, $height, $type) = dompdf_getimagesize($resolved_url); // Known image type if ($width && $height && in_array($type, array(IMAGETYPE_GIF, IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_BMP))) { //Don't put replacement image into cache - otherwise it will be deleted on cache cleanup. //Only execute on successfull caching of remote image. if (DOMPDF_ENABLE_REMOTE && $remote) { self::$_cache[$full_url] = $resolved_url; } } else { throw new DOMPDF_Image_Exception("Image type unknown"); unlink($resolved_url); } } } catch (DOMPDF_Image_Exception $e) { $resolved_url = self::$broken_image; $type = IMAGETYPE_PNG; $message = $e->getMessage() . " \n {$url}"; } return array($resolved_url, $type, $message); }
/** * load and parse a CSS file * * @param string $file * @param int $origin */ function load_css_file($file, $origin = self::ORIG_AUTHOR) { if ($origin) { $this->_current_origin = $origin; } // Prevent circular references if (isset($this->_loaded_files[$file])) { return; } $this->_loaded_files[$file] = true; if (strpos($file, "data:") === 0) { $parsed = parse_data_uri($file); $css = $parsed["data"]; } else { $parsed_url = explode_url($file); list($this->_protocol, $this->_base_host, $this->_base_path, $filename) = $parsed_url; // Fix submitted by Nick Oostveen for aliased directory support: if ($this->_protocol == "") { $file = $this->_base_path . $filename; } else { $file = build_url($this->_protocol, $this->_base_host, $this->_base_path, $filename); } set_error_handler("record_warnings"); $css = file_get_contents($file, null, $this->_dompdf->get_http_context()); restore_error_handler(); $good_mime_type = true; // See http://the-stickman.com/web-development/php/getting-http-response-headers-when-using-file_get_contents/ if (isset($http_response_header) && !$this->_dompdf->get_quirksmode()) { foreach ($http_response_header as $_header) { if (preg_match("@Content-Type:\\s*([\\w/]+)@i", $_header, $matches) && $matches[1] !== "text/css") { $good_mime_type = false; } } } if (!$good_mime_type || $css == "") { record_warnings(E_USER_WARNING, "Unable to load css file {$file}", __FILE__, __LINE__); return; } } $this->_parse_css($css); }
/** * Resolve and fetch an image for use. * * @param string $url The url of the image * @param string $proto 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 * @return array An array with two elements: The local path to the image and the image extension */ static function resolve_url($url, $proto, $host, $base_path) { global $_dompdf_warnings; $parsed_url = explode_url($url); $DEBUGPNG = DEBUGPNG; //=DEBUGPNG; Allow override of global setting for ad hoc debug $full_url_dbg = ''; //debugpng if ($DEBUGPNG) { print 'resolve_url(' . $url . ',' . $proto . ',' . $host . ',' . $base_path . ')(' . $parsed_url['protocol'] . ')'; } $remote = $proto != "" && $proto !== "file://"; $remote = $remote || $parsed_url['protocol'] != ""; $datauri = strpos($parsed_url['protocol'], "data:") === 0; if (!DOMPDF_ENABLE_REMOTE && $remote && !$datauri) { $resolved_url = DOMPDF_LIB_DIR . "/res/broken_image.png"; $ext = "png"; //debugpng if ($DEBUGPNG) { $full_url_dbg = '(blockedremote)'; } } else { if (DOMPDF_ENABLE_REMOTE && $remote || $datauri) { // Download remote files to a temporary directory $full_url = build_url($proto, $host, $base_path, $url); if (isset(self::$_cache[$full_url])) { list($resolved_url, $ext) = self::$_cache[$full_url]; //debugpng if ($DEBUGPNG) { $full_url_dbg = $full_url . '(cache)'; } } else { $resolved_url = tempnam(DOMPDF_TEMP_DIR, "ca_dompdf_img_"); //debugpng if ($DEBUGPNG) { echo $resolved_url . "\n"; } if ($datauri) { if ($parsed_data_uri = parse_data_uri($url)) { $image = $parsed_data_uri['data']; list(, $ext) = explode('/', $parsed_data_uri['mime'], 2); } } else { $old_err = set_error_handler("record_warnings"); $image = file_get_contents($full_url); restore_error_handler(); } if (strlen($image) == 0) { //target image not found $resolved_url = DOMPDF_LIB_DIR . "/res/broken_image.png"; $ext = "png"; //debugpng if ($DEBUGPNG) { $full_url_dbg = $full_url . '(missing)'; } } else { file_put_contents($resolved_url, $image); //e.g. fetch.php?media=url.jpg&cache=1 //- Image file name might be one of the dynamic parts of the url, don't strip off! // if ( preg_match("/.*\.(\w+)/",$url,$match) ) $ext = $match[1]; //- a remote url does not need to have a file extension at all //- local cached file does not have a matching file extension //Therefore get image type from the content $imagedim = dompdf_getimagesize($resolved_url); if ($imagedim[0] && $imagedim[1] && in_array($imagedim[2], array(IMAGETYPE_GIF, IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_BMP))) { //target image is valid $imagetypes = array('', 'gif', 'jpeg', 'png', 'swf', 'psd', 'bmp'); $ext = $imagetypes[$imagedim[2]]; if (rename($resolved_url, $resolved_url . '.' . $ext)) { $resolved_url .= '.' . $ext; } //Don't put replacement image into cache - otherwise it will be deleted on cache cleanup. //Only execute on successfull caching of remote image. self::$_cache[$full_url] = array($resolved_url, $ext); } else { //target image is not valid. unlink($resolved_url); $resolved_url = DOMPDF_LIB_DIR . "/res/broken_image.png"; $ext = "png"; } } } } else { $resolved_url = build_url($proto, $host, $base_path, $url); if ($DEBUGPNG) { print 'build_url(' . $proto . ',' . $host . ',' . $base_path . ',' . $url . ')(' . $resolved_url . ')'; } if (!preg_match("/.*\\.(\\w+)/", $url, $match)) { //debugpng if ($DEBUGPNG) { print '[resolve_url exception ' . $url . ']'; } throw new DOMPDF_Exception("Unknown image type: {$url}."); } $ext = $match[1]; //debugpng if ($DEBUGPNG) { $full_url_dbg = '(local)'; } } } if (!is_readable($resolved_url) || !filesize($resolved_url)) { //debugpng if ($DEBUGPNG) { $full_url_dbg .= '(nocache' . $resolved_url . ')'; } $_dompdf_warnings[] = "File " . $resolved_url . " is not readable or is an empty file.\n"; $resolved_url = DOMPDF_LIB_DIR . "/res/broken_image.png"; $ext = "png"; } //debugpng if ($DEBUGPNG) { print '[resolve_url ' . $url . '|' . $full_url_dbg . '|' . $resolved_url . '|' . $ext . ']'; } return array($resolved_url, $ext); }