From aee50eb2bf5074eea9bf3d28fa77c988040e659d Mon Sep 17 00:00:00 2001 From: nicolaasuni Date: Sat, 22 Sep 2018 12:50:04 +0100 Subject: [PATCH] Support remote files in file_exists --- CHANGELOG.TXT | 3 +++ composer.json | 2 +- include/tcpdf_static.php | 37 +++++++++++++++++++++++++++++++++---- tcpdf.php | 6 +++--- 4 files changed, 40 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.TXT b/CHANGELOG.TXT index 5843efb..fa6a7a6 100644 --- a/CHANGELOG.TXT +++ b/CHANGELOG.TXT @@ -1,3 +1,6 @@ +6.2.24 + - Support remote urls when checking if file exists. + 6.2.23 - Simplify file_exists function. diff --git a/composer.json b/composer.json index bff3133..9994468 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "tecnickcom/tcpdf", - "version": "6.2.23", + "version": "6.2.24", "homepage": "http://www.tcpdf.org/", "type": "library", "description": "TCPDF is a PHP class for generating PDF documents and barcodes.", diff --git a/include/tcpdf_static.php b/include/tcpdf_static.php index 3e24597..37701d9 100644 --- a/include/tcpdf_static.php +++ b/include/tcpdf_static.php @@ -55,7 +55,7 @@ class TCPDF_STATIC { * Current TCPDF version. * @private static */ - private static $tcpdf_version = '6.2.23'; + private static $tcpdf_version = '6.2.24'; /** * String alias for total number of pages. @@ -1821,6 +1821,31 @@ class TCPDF_STATIC { return fopen($filename, $mode); } + /** + * Check if the URL exist. + * @param url (string) URL to check. + * @return Returns TRUE if the URL exists; FALSE otherwise. + * @public static + */ + public static function url_exists($url) { + $crs = curl_init(); + curl_setopt($crs, CURLOPT_URL, $url); + curl_setopt($crs, CURLOPT_NOBODY, true); + curl_setopt($crs, CURLOPT_FAILONERROR, true); + if ((ini_get('open_basedir') == '') && (!ini_get('safe_mode'))) { + curl_setopt($crs, CURLOPT_FOLLOWLOCATION, true); + } + curl_setopt($crs, CURLOPT_CONNECTTIMEOUT, 5); + curl_setopt($crs, CURLOPT_TIMEOUT, 30); + curl_setopt($crs, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($crs, CURLOPT_SSL_VERIFYHOST, false); + curl_setopt($crs, CURLOPT_USERAGENT, 'tc-lib-file'); + curl_exec($crs); + $code = curl_getinfo($crs, CURLINFO_HTTP_CODE); + curl_close($crs); + return ($code == 200); + } + /** * Wrapper for file_exists. * Checks whether a file or directory exists. @@ -1830,10 +1855,14 @@ class TCPDF_STATIC { * @public static */ public static function file_exists($filename) { - if (strpos($filename, '://') && (preg_match('|^https?://|', $filename) !== 1)) { - return false; + $httpmode = (preg_match('|^https?://|', $filename) == 1); + if (!$httpmode && strpos($filename, '://')) { + return false; // only support http and https wrappers for security reasons } - return @file_exists($filename); + if (@file_exists($filename) || ($httpmode && self::url_exists($filename))) { + return true; + } + return false; } /** diff --git a/tcpdf.php b/tcpdf.php index b89701e..f0f223f 100644 --- a/tcpdf.php +++ b/tcpdf.php @@ -1,7 +1,7 @@ * @package com.tecnick.tcpdf * @author Nicola Asuni - * @version 6.2.23 + * @version 6.2.24 */ // TCPDF configuration @@ -128,7 +128,7 @@ require_once(dirname(__FILE__).'/include/tcpdf_static.php'); * TCPDF project (http://www.tcpdf.org) has been originally derived in 2002 from the Public Domain FPDF class by Olivier Plathey (http://www.fpdf.org), but now is almost entirely rewritten.
* @package com.tecnick.tcpdf * @brief PHP class for generating PDF documents without requiring external extensions. - * @version 6.2.23 + * @version 6.2.24 * @author Nicola Asuni - info@tecnick.com * @IgnoreAnnotation("protected") * @IgnoreAnnotation("public")