Version 4.5.0

This commit is contained in:
Neo committed 2025-12-19 05:51:58 -08:00
1 parent 1a0c7fd3c2
commit f79dcf067a
3791 files changed
+248032 -76372

No files matched your search

+42 -21
View File
@@ -11,7 +11,7 @@
namespace IPS\Http\Request;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
@@ -53,6 +53,11 @@ class _Curl
* @brief Follow redirects?
*/
protected $followRedirects = TRUE;
/**
* @brief Allowed protocols
*/
protected $allowedProtocols = array();
/**
* @brief Data sent
@@ -66,16 +71,18 @@ class _Curl
* @param int $timeout Timeout (in seconds)
* @param string $httpVersion HTTP Version
* @param bool|int $followRedirects Automatically follow redirects? If a number is provided, will follow up to that number of redirects
* @param array|null $allowedProtocols Protocols allowed (if NULL we default to array( 'http', 'https', 'ftp', 'scp', 'sftp', 'ftps' ))
* @return void
*/
public function __construct( $url, $timeout=5, $httpVersion=NULL, $followRedirects=TRUE )
public function __construct( $url, $timeout=5, $httpVersion=NULL, $followRedirects=TRUE, $allowedProtocols=NULL )
{
/* Init */
$this->url = $url;
$this->curl = curl_init();
$this->httpVersion = $httpVersion ?: '1.1';
$this->timeout = $timeout;
$this->followRedirects = $followRedirects;
$this->url = $url;
$this->curl = curl_init();
$this->httpVersion = $httpVersion ?: '1.1';
$this->timeout = $timeout;
$this->followRedirects = $followRedirects;
$this->allowedProtocols = $allowedProtocols ?: array( 'http', 'https', 'ftp', 'scp', 'sftp', 'ftps' );
/* Need to adjust if this is FTP */
$user = null;
@@ -155,7 +162,7 @@ class _Curl
*
* @param string $username Username
* @param string $password Password
* @return \IPS\Http\Request\Curl
* @return static
*/
public function login( $username, $password )
{
@@ -171,7 +178,7 @@ class _Curl
* Set Headers
*
* @param array $headers Key/Value pair of headers
* @return \IPS\Http\Request\Curl
* @return static
*/
public function setHeaders( $headers )
{
@@ -220,7 +227,7 @@ class _Curl
* Toggle SSL checks
*
* @param boolean $value True will enable SSL checks, false will disable them
* @return \IPS\Http\Request\Socket
* @return static
*/
public function sslCheck( $value=TRUE )
{
@@ -235,7 +242,7 @@ class _Curl
/**
* Force TLS
*
* @return \IPS\Http\Request\Socket
* @return static
*/
public function forceTls()
{
@@ -244,17 +251,17 @@ class _Curl
{
if ( version_compare( $openSSLVersionData[1], '1.0.1', '>=' ) )
{
if ( !defined('CURL_SSLVERSION_TLSv1_2') ) // constant not defined in PHP < 5.5
if ( !\defined('CURL_SSLVERSION_TLSv1_2') ) // constant not defined in PHP < 5.5
{
define( 'CURL_SSLVERSION_TLSv1_2', 6 );
\define( 'CURL_SSLVERSION_TLSv1_2', 6 );
}
curl_setopt( $this->curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2 );
}
else
{
if ( !defined('CURL_SSLVERSION_TLSv1') ) // constant not defined in PHP < 5.5
if ( !\defined('CURL_SSLVERSION_TLSv1') ) // constant not defined in PHP < 5.5
{
define( 'CURL_SSLVERSION_TLSv1', 1 );
\define( 'CURL_SSLVERSION_TLSv1', 1 );
}
curl_setopt( $this->curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1 );
}
@@ -266,6 +273,7 @@ class _Curl
/**
* HTTP GET
*
* @param mixed $data Data to send with the GET request
* @return \IPS\Http\Response
* @throws \IPS\Http\Request\CurlException
*/
@@ -353,7 +361,7 @@ class _Curl
protected function _dataToSend( $data=NULL )
{
$this->dataForLog = $data;
if ( !$this->modifiedContentType and is_array( $data ) )
if ( !$this->modifiedContentType and \is_array( $data ) )
{
$data = http_build_query( $data, '', '&' );
}
@@ -372,7 +380,7 @@ class _Curl
$output = curl_exec( $this->curl );
/* Log - but because the output can be large, only do this if we explicitly have debug logging enabled */
if ( defined('\IPS\DEBUG_LOG') and \IPS\DEBUG_LOG )
if ( \defined('\IPS\DEBUG_LOG') and \IPS\DEBUG_LOG )
{
\IPS\Log::debug( "\n\n------------------------------------\ncURL REQUEST: {$this->url}\n------------------------------------\n\n" . var_export( $this->dataForLog, TRUE ) . "\n\n------------------------------------\nRESPONSE\n------------------------------------\n\n" . $output, 'request' );
}
@@ -380,7 +388,7 @@ class _Curl
/* Errors? */
if ( $output === FALSE )
{
throw new CurlException( curl_error( $this->curl ), curl_errno( $this->curl ) );
throw new CurlException( $this->url . "\n" . curl_error( $this->curl ), curl_errno( $this->curl ) );
}
/* If this is FTP we need to fudge the headers a little */
@@ -407,17 +415,30 @@ class _Curl
$response = $this->_execute();
/* Either return it or follow it */
if ( $this->followRedirects and in_array( $response->httpResponseCode, array( 301, 302, 303, 307, 308 ) ) )
if ( $this->followRedirects and \in_array( $response->httpResponseCode, array( 301, 302, 303, 307, 308 ) ) )
{
/* Fix missing hostname in location */
$location = $response->httpHeaders['Location'];
foreach( $response->httpHeaders as $k => $v )
{
if( mb_strtolower( $k ) == 'location' )
{
$location = $v;
}
}
if( parse_url( $location, PHP_URL_HOST ) === NULL )
{
$location = $this->url->data['scheme'] . '://' . $this->url->data['host'] . $location;
}
$newRequest = \IPS\Http\Url::external( $location )->request( $this->timeout, $this->httpVersion, is_int( $this->followRedirects ) ? ( $this->followRedirects - 1 ) : $this->followRedirects );
$newRequest = \IPS\Http\Url::external( $location );
if( !\in_array( $newRequest->data['scheme'], $this->allowedProtocols ) )
{
throw new \IPS\Http\Request\Exception( 'protocol_not_followed' );
}
$newRequest = $newRequest->request( $this->timeout, $this->httpVersion, \is_int( $this->followRedirects ) ? ( $this->followRedirects - 1 ) : $this->followRedirects );
return $newRequest->$method( $params );
}
return $response;