Skip to content

Commit

Permalink
Add K_CURLOPTS config array to set custom cURL options (NOTE: some de…
Browse files Browse the repository at this point in the history
…faults have changed)
  • Loading branch information
nicolaasuni committed Dec 23, 2024
1 parent 3d83609 commit aab43ab
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
- Requires PHP 7.1+.
- Escape error message.
- Use strict time-constant function to compare TCPDF-tag hashes.
- Add K_CURLOPTS config array to set custom cURL options (NOTE: some defaults have changed).


6.7.8 (2024-12-13)
Expand Down
81 changes: 50 additions & 31 deletions include/tcpdf_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,31 @@ class TCPDF_STATIC {
*/
public static $pageboxes = array('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox');

/**
* Array of default cURL options for curl_setopt_array.
*
* @var array<int, bool|int|string> cURL options.
*/
protected const CURLOPT_DEFAULT = [
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_MAXREDIRS => 5,
CURLOPT_PROTOCOLS => CURLPROTO_HTTPS | CURLPROTO_HTTP | CURLPROTO_FTP | CURLPROTO_FTPS,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_TIMEOUT => 30,
CURLOPT_USERAGENT => 'tcpdf',
];

/**
* Array of fixed cURL options for curl_setopt_array.
*
* @var array<int, bool|int|string> cURL options.
*/
protected const CURLOPT_FIXED = [
CURLOPT_FAILONERROR => true,
CURLOPT_RETURNTRANSFER => true,
];

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/**
Expand Down Expand Up @@ -1823,23 +1848,19 @@ public static function fopenLocal($filename, $mode) {
*/
public static function url_exists($url) {
$crs = curl_init();
// encode query params in URL to get right response form the server
$url = self::encodeUrlQuery($url);
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_setopt($crs, CURLOPT_MAXREDIRS, 5);
if (defined('CURLOPT_PROTOCOLS')) {
curl_setopt($crs, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS | CURLPROTO_HTTP | CURLPROTO_FTP | CURLPROTO_FTPS);
}
$curlopts = [];
if (
(ini_get('open_basedir') == '')
&& (ini_get('safe_mode') === ''
|| ini_get('safe_mode') === false)
) {
$curlopts[CURLOPT_FOLLOWLOCATION] = true;
}
$curlopts = array_replace($curlopts, self::CURLOPT_DEFAULT);
$curlopts = array_replace($curlopts, K_CURLOPTS);
$curlopts = array_replace($curlopts, self::CURLOPT_FIXED);
$curlopts[CURLOPT_URL] = $url;
curl_setopt_array($crs, $curlopts);
curl_exec($crs);
$code = curl_getinfo($crs, CURLINFO_HTTP_CODE);
curl_close($crs);
Expand Down Expand Up @@ -1960,21 +1981,19 @@ public static function fileGetContents($file) {
) {
// try to get remote file data using cURL
$crs = curl_init();
curl_setopt($crs, CURLOPT_URL, $path);
curl_setopt($crs, CURLOPT_FAILONERROR, true);
curl_setopt($crs, CURLOPT_RETURNTRANSFER, 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_setopt($crs, CURLOPT_MAXREDIRS, 5);
if (defined('CURLOPT_PROTOCOLS')) {
curl_setopt($crs, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS | CURLPROTO_HTTP | CURLPROTO_FTP | CURLPROTO_FTPS);
$curlopts = [];
if (
(ini_get('open_basedir') == '')
&& (ini_get('safe_mode') === ''
|| ini_get('safe_mode') === false)
) {
$curlopts[CURLOPT_FOLLOWLOCATION] = true;
}
$curlopts = array_replace($curlopts, self::CURLOPT_DEFAULT);
$curlopts = array_replace($curlopts, K_CURLOPTS);
$curlopts = array_replace($curlopts, self::CURLOPT_FIXED);
$curlopts[CURLOPT_URL] = $url;
curl_setopt_array($crs, $curlopts);
$ret = curl_exec($crs);
curl_close($crs);
if ($ret !== false) {
Expand Down
5 changes: 5 additions & 0 deletions tcpdf_autoconfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@
define('K_TIMEZONE', @date_default_timezone_get());
}

// Custom cURL options for curl_setopt_array.
if (!defined('K_CURLOPTS')) {
define('K_CURLOPTS', array());
}

//============================================================+
// END OF FILE
//============================================================+

0 comments on commit aab43ab

Please sign in to comment.