/** * Loads an HTML file * Parse errors are stored in the global array _dompdf_warnings. * * @param string $file a filename or url to load * * @throws Exception */ public function loadHtmlFile($file) { $this->saveLocale(); if (!$this->protocol && !$this->baseHost && !$this->basePath) { list($this->protocol, $this->baseHost, $this->basePath) = Helpers::explode_url($file); } if (!in_array($this->protocol, $this->allowedProtocols)) { throw new Exception("Permission denied on {$file}. The communication protocol is not supported."); } if (!$this->options->isRemoteEnabled() && ($this->protocol != "" && $this->protocol !== "file://")) { throw new Exception("Remote file requested, but remote file download is disabled."); } if ($this->protocol == "" || $this->protocol === "file://") { // Get the full path to $file, returns false if the file doesn't exist $realfile = realpath($file); $chroot = $this->options->getChroot(); if (strpos($realfile, $chroot) !== 0) { throw new Exception("Permission denied on {$file}. The file could not be found under the directory specified by Options::chroot."); } $ext = pathinfo($realfile, PATHINFO_EXTENSION); if (!in_array($ext, $this->allowedLocalFileExtensions)) { throw new Exception("Permission denied on {$file}."); } if (!$realfile) { throw new Exception("File '{$file}' not found."); } $file = $realfile; } list($contents, $http_response_header) = Helpers::getFileContent($file, null, $this->httpContext); $encoding = null; // See http://the-stickman.com/web-development/php/getting-http-response-headers-when-using-file_get_contents/ if (isset($http_response_header)) { foreach ($http_response_header as $_header) { if (preg_match("@Content-Type:\\s*[\\w/]+;\\s*?charset=([^\\s]+)@i", $_header, $matches)) { $encoding = strtoupper($matches[1]); break; } } } $this->restoreLocale(); $this->loadHtml($contents, $encoding); }
/** * @param array $style * @param string $remoteFile * @param resource $context * @return bool */ public function registerFont($style, $remoteFile, $context = null) { $fontDir = $this->getOptions()->getFontDir(); $fontname = mb_strtolower($style["family"]); $families = $this->getFontFamilies(); $entry = array(); if (isset($families[$fontname])) { $entry = $families[$fontname]; } $localFile = $fontDir . DIRECTORY_SEPARATOR . md5($remoteFile); $localTempFile = $this->options->get('tempDir') . "/" . md5($remoteFile); $cacheEntry = $localFile; $localFile .= "." . strtolower(pathinfo($remoteFile, PATHINFO_EXTENSION)); $styleString = $this->getType("{$style['weight']} {$style['style']}"); if (!isset($entry[$styleString])) { $entry[$styleString] = $cacheEntry; // Download the remote file $remoteFileContent = @Helpers::getFileContent($remoteFile, null, $context); if (false === $remoteFileContent) { return false; } file_put_contents($localTempFile, $remoteFileContent); $font = Font::load($localTempFile); if (!$font) { unlink($localTempFile); return false; } $font->parse(); $font->saveAdobeFontMetrics("{$cacheEntry}.ufm"); $font->close(); if (file_exists($localTempFile)) { unlink($localTempFile); } if (!file_exists("{$cacheEntry}.ufm")) { return false; } // Save the changes file_put_contents($localFile, $remoteFileContent); if (!file_exists($localFile)) { unlink("{$cacheEntry}.ufm"); return false; } $this->setFontFamily($fontname, $entry); $this->saveFontFamilies(); } return true; }
/** * 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 ImageException * @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) { self::$_dompdf = $dompdf; $protocol = mb_strtolower($protocol); $parsed_url = Helpers::explode_url($url); $message = null; $remote = $protocol && $protocol !== "file://" || $parsed_url['protocol'] != ""; $data_uri = strpos($parsed_url['protocol'], "data:") === 0; $full_url = null; $enable_remote = $dompdf->getOptions()->getIsRemoteEnabled(); try { // Remote not allowed and is not DataURI if (!$enable_remote && $remote && !$data_uri) { throw new ImageException("Remote file access is disabled.", E_WARNING); } else { if ($enable_remote && $remote || $data_uri) { // Download remote files to a temporary directory $full_url = Helpers::build_url($protocol, $host, $base_path, $url); // From cache if (isset(self::$_cache[$full_url])) { $resolved_url = self::$_cache[$full_url]; } else { $tmp_dir = $dompdf->getOptions()->getTempDir(); $resolved_url = tempnam($tmp_dir, "ca_dompdf_img_"); $image = ""; if ($data_uri) { if ($parsed_data_uri = Helpers::parse_data_uri($url)) { $image = $parsed_data_uri['data']; } } else { $image = Helpers::getFileContent($full_url, $dompdf->getHttpContext()); # Helpers::getFileContent returns an array, and the contents are the first item in it. $image = $image[0]; } // Image not found or invalid if (strlen($image) == 0) { $msg = $data_uri ? "Data-URI could not be parsed" : "Image not found"; throw new ImageException($msg, E_WARNING); } 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 = Helpers::build_url($protocol, $host, $base_path, $url); } } // Check if the local file is readable if (!is_readable($resolved_url) || !filesize($resolved_url)) { throw new ImageException("Image not readable or empty", E_WARNING); } else { list($width, $height, $type) = Helpers::dompdf_getimagesize($resolved_url, $dompdf->getHttpContext()); // Known image type if ($width && $height && in_array($type, array("gif", "png", "jpeg", "bmp", "svg"))) { //Don't put replacement image into cache - otherwise it will be deleted on cache cleanup. //Only execute on successful caching of remote image. if ($enable_remote && $remote || $data_uri) { self::$_cache[$full_url] = $resolved_url; } } else { throw new ImageException("Image type unknown", E_WARNING); } } } catch (ImageException $e) { $resolved_url = self::$broken_image; $type = "png"; $message = "Image not found or type unknown"; Helpers::record_warnings($e->getCode(), $e->getMessage() . " \n {$url}", $e->getFile(), $e->getLine()); } 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 = Helpers::parse_data_uri($file); $css = $parsed["data"]; } else { $parsed_url = Helpers::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 = Helpers::build_url($this->_protocol, $this->_base_host, $this->_base_path, $filename); } list($css, $http_response_header) = Helpers::getFileContent($file, $this->_dompdf->get_http_context()); $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 == "") { Helpers::record_warnings(E_USER_WARNING, "Unable to load css file {$file}", __FILE__, __LINE__); return; } } if (substr($file, 0, 4) == "http") { $this->_dompdf->setHttpContext(stream_context_create(['http' => ['method' => "GET", 'header' => "Referer: " . $file]])); } $this->_parse_css($css); }