Version 4.1.19

This commit is contained in:
Neo committed 2025-12-19 05:38:56 -08:00
1 parent 28bd025b35
commit 2bd5025018
2674 files changed
+233518 -77766

No files matched your search

+136 -73
View File
@@ -2,9 +2,9 @@
/**
* @brief cURL REST Class
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
* @copyright (c) 2001 - SVN_YYYY Invision Power Services, Inc.
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
* @license http://www.invisionpower.com/legal/standards/
* @package IPS Social Suite
* @package IPS Community Suite
* @since 18 Mar 2013
* @version SVN_VERSION_NUMBER
*/
@@ -128,7 +128,7 @@ class _Curl
}
$httpVersion = ( $httpVersion == '1.1' ? CURL_HTTP_VERSION_1_1 : CURL_HTTP_VERSION_1_0 );
/* Set our basic settings */
curl_setopt_array( $this->curl, array(
CURLOPT_HEADER => TRUE, // Specifies that we want the headers
@@ -215,62 +215,7 @@ class _Curl
return $this;
}
/**
* HTTP GET
*
* @return \IPS\Http\Response
* @throws \IPS\Http\Request\CurlException
*/
public function get()
{
/* Specify that this is a GET request */
curl_setopt( $this->curl, CURLOPT_HTTPGET, TRUE );
$this->dataForLog = NULL;
/* Execute */
$response = $this->_execute();
/* Either return it or follow it */
if ( $this->followRedirects and ( $response->httpResponseCode == 302 OR $response->httpResponseCode == 301 ) )
{
$newRequest = \IPS\Http\Url::external( $response->httpHeaders['Location'] )->request( $this->timeout, $this->httpVersion, is_int( $this->followRedirects ) ? ( $this->followRedirects - 1 ) : $this->followRedirects );
return $newRequest->get();
}
return $response;
}
/**
* HTTP POST
*
* @param mixed $data Data to post (can be array or string)
* @return \IPS\Http\Response
* @throws \IPS\Http\Request\CurlException
*/
public function post( $data=NULL )
{
/* Specify that this is a POST request */
curl_setopt( $this->curl, CURLOPT_POST, TRUE );
/* Set the data */
$this->dataForLog = $data;
if ( !$this->modifiedContentType and is_array( $data ) )
{
$data = http_build_query( $data, '', '&' );
}
curl_setopt( $this->curl, CURLOPT_POSTFIELDS, $data );
/* Execute */
$response = $this->_execute();
/* Either return it or follow it */
if ( $this->followRedirects and ( $response->httpResponseCode == 302 OR $response->httpResponseCode == 301 ) )
{
$newRequest = \IPS\Http\Url::external( $response->httpHeaders['Location'] )->request( $this->timeout, $this->httpVersion, is_int( $this->followRedirects ) ? ( $this->followRedirects - 1 ) : $this->followRedirects );
return call_user_func_array( array( $newRequest, 'post' ), $data );
}
return $response;
}
/**
* Toggle SSL checks
@@ -288,6 +233,89 @@ class _Curl
return $this;
}
/**
* Force TLS
*
* @return \IPS\Http\Request\Socket
*/
public function forceTls()
{
$curlVersionData = curl_version();
if ( preg_match( '/^OpenSSL\/(\d+\.\d+\.\d+)/', $curlVersionData['ssl_version'], $openSSLVersionData ) )
{
if ( version_compare( $openSSLVersionData[1], '1.0.1', '>=' ) )
{
if ( !defined('CURL_SSLVERSION_TLSv1_2') ) // constant not defined in PHP < 5.5
{
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
{
define( 'CURL_SSLVERSION_TLSv1', 1 );
}
curl_setopt( $this->curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1 );
}
}
return $this;
}
/**
* HTTP GET
*
* @return \IPS\Http\Response
* @throws \IPS\Http\Request\CurlException
*/
public function get()
{
/* Specify that this is a GET request */
curl_setopt( $this->curl, CURLOPT_HTTPGET, TRUE );
$this->dataForLog = NULL;
return $this->_executeAndFollowRedirects( 'GET', NULL );
}
/**
* HTTP POST
*
* @param mixed $data Data to post (can be array or string)
* @return \IPS\Http\Response
* @throws \IPS\Http\Request\CurlException
*/
public function post( $data=NULL )
{
/* Specify that this is a POST request */
curl_setopt( $this->curl, CURLOPT_POST, TRUE );
/* Set the data */
curl_setopt( $this->curl, CURLOPT_POSTFIELDS, $this->_dataToSend( $data ) );
/* Execute */
return $this->_executeAndFollowRedirects( 'POST', $data );
}
/**
* HTTP HEAD
*
* @return \IPS\Http\Response
* @throws \IPS\Http\Request\CurlException
*/
public function head()
{
/* Specify the request method */
curl_setopt( $this->curl, CURLOPT_CUSTOMREQUEST, 'HEAD' );
/* For HEAD requests, do not try to fetch the body or curl times out */
curl_setopt( $this->curl, CURLOPT_NOBODY, true );
/* Execute */
return $this->_executeAndFollowRedirects( 'HEAD', NULL );
}
/**
* Magic Method: __call
* Used for other HTTP methods (like PUT and DELETE)
@@ -301,25 +329,31 @@ class _Curl
{
/* Specify the request method */
curl_setopt( $this->curl, CURLOPT_CUSTOMREQUEST, mb_strtoupper( $method ) );
/* If we have any data to send, set it */
if ( isset( $params[0] ) )
{
curl_setopt( $this->curl, CURLOPT_POSTFIELDS, $params[0] );
$this->dataForLog = $params[0];
curl_setopt( $this->curl, CURLOPT_POSTFIELDS, $this->_dataToSend( $params[0] ) );
}
/* Execute */
$response = $this->_execute();
/* Either return it or follow it */
if ( $this->followRedirects and ( $response->httpResponseCode == 302 OR $response->httpResponseCode == 301 ) )
return $this->_executeAndFollowRedirects( mb_strtoupper( $method ), $params );
}
/**
* Data to send
*
* @param mixed $data Data to post (can be array or string)
* @return mixed
*/
protected function _dataToSend( $data=NULL )
{
$this->dataForLog = $data;
if ( !$this->modifiedContentType and is_array( $data ) )
{
$newRequest = \IPS\Http\Url::external( $response->httpHeaders['Location'] )->request( $this->timeout, $this->httpVersion, is_int( $this->followRedirects ) ? ( $this->followRedirects - 1 ) : $this->followRedirects );
return call_user_func_array( array( $newRequest, $method ), $params );
$data = http_build_query( $data, '', '&' );
}
return $response;
return $data;
}
/**
@@ -333,12 +367,11 @@ class _Curl
/* Execute */
$output = curl_exec( $this->curl );
/* Log */
try
/* 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 )
{
\IPS\Log::i( LOG_DEBUG )->write( "\n\n------------------------------------\ncURL REQUEST: {$this->url}\n------------------------------------\n\n" . var_export( $this->dataForLog, TRUE ) . "\n\n------------------------------------\nRESPONSE\n------------------------------------\n\n" . $output, 'request' );
\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' );
}
catch( \Exception $e ){}
/* Errors? */
if ( $output === FALSE )
@@ -355,6 +388,36 @@ class _Curl
/* Return it */
return new \IPS\Http\Response( $output );
}
/**
* Execute the request and follow redirects id necessary
*
* @param string $method Request method to use
* @param NULL|array $params Parameters to send with request
* @return \IPS\Http\Response
* @throws \IPS\Http\Request\CurlException
*/
protected function _executeAndFollowRedirects( $method, $params=NULL )
{
/* Execute */
$response = $this->_execute();
/* Either return it or follow it */
if ( $this->followRedirects and in_array( $response->httpResponseCode, array( 301, 302, 303, 307, 308 ) ) )
{
/* Fix missing hostname in location */
$location = $response->httpHeaders['Location'];
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 );
return call_user_func_array( array( $newRequest, $method ), $params );
}
return $response;
}
}
/**
+3 -3
View File
@@ -2,9 +2,9 @@
/**
* @brief Request Exception Class
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
* @copyright (c) 2001 - SVN_YYYY Invision Power Services, Inc.
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
* @license http://www.invisionpower.com/legal/standards/
* @package IPS Social Suite
* @package IPS Community Suite
* @since 26 Aug 2013
* @version SVN_VERSION_NUMBER
*/
@@ -21,4 +21,4 @@ if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
/**
* Request Exception Class
*/
abstract class _Exception extends \RuntimeException { }
class _Exception extends \RuntimeException { }
+121 -46
View File
@@ -2,9 +2,9 @@
/**
* @brief Sockets REST Class
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
* @copyright (c) 2001 - SVN_YYYY Invision Power Services, Inc.
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
* @license http://www.invisionpower.com/legal/standards/
* @package IPS Social Suite
* @package IPS Community Suite
* @since 18 Mar 2013
* @version SVN_VERSION_NUMBER
*/
@@ -22,17 +22,22 @@ if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
* Sockets REST Class
*/
class _Sockets
{
{
/**
* @brief URL
*/
protected $url = NULL;
/**
* @brief Stream context
*/
protected $context;
/**
* @brief HTTP Version
*/
protected $httpVersion = '1.1';
/**
* @brief Timeout
*/
@@ -42,7 +47,7 @@ class _Sockets
* @brief Headers
*/
protected $headers = array();
/**
* @brief Follow redirects?
*/
@@ -60,18 +65,31 @@ class _Sockets
public function __construct( $url, $timeout, $httpVersion, $followRedirects )
{
$this->url = $url;
$this->context = stream_context_create();
$this->httpVersion = $httpVersion ?: '1.1';
$this->timeout = $timeout;
$this->followRedirects = $followRedirects;
$this->contextOptions['http']['ignore_errors'] = TRUE;
/* Set our basic settings */
stream_context_set_option( $this->context, array(
'http' => array(
'protocol_version' => $httpVersion,
'follow_location' => $followRedirects,
'timeout' => $timeout,
'ignore_errors' => TRUE,
),
'ssl' => array(
'verify_peer' => FALSE,
)
) );
}
/**
* Login
*
* @param string Username
* @param string Password
* @retrun \IPS\Http\Request\Socket (for daisy chaining)
* @return \IPS\Http\Request\Socket (for daisy chaining)
*/
public function login( $username, $password )
{
@@ -99,19 +117,56 @@ class _Sockets
*/
public function sslCheck( $value=TRUE )
{
/* This is here for compatibility with curl */
stream_context_set_option( $this->context, array(
'ssl' => array(
'verify_peer_name' => ( $value ) ? 2 : FALSE,
'verify_peer' => (boolean) $value,
)
) );
return $this;
}
/**
* Magic Method: __call
* Force TLS
*
* @param string $method
* @param array Params
* @return \IPS\Http\Request\Socket
*/
public function forceTls()
{
if ( defined('STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT') )
{
stream_context_set_option( $this->context, array(
'ssl' => array(
'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT
)
) );
}
elseif ( defined('STREAM_CRYPTO_METHOD_TLS_CLIENT') )
{
stream_context_set_option( $this->context, array(
'ssl' => array(
'crypto_method' => STREAM_CRYPTO_METHOD_TLS_CLIENT
)
) );
}
return $this;
}
/**
* Magic Method: __call
* Used for other HTTP methods (like PUT and DELETE)
*
* @param string $method Method (A HTTP method)
* @param array $params Parameters (a single parameter with data to post, which can be an array or a string)
* @return \IPS\Http\Response
* @throws \IPS\Http\Request\CurlException
*/
public function __call( $method, $params )
{
$method = mb_strtoupper( $method );
/* The data (string or array) will be the first parameter */
if ( isset( $params[0] ) && is_array( $params[0] ) )
{
@@ -123,87 +178,107 @@ class _Sockets
$data = ( isset( $params[0] ) ? $params[0] : NULL );
}
/* Set the method */
$this->contextOptions['http']['method'] = mb_strtoupper( $method );
/* Set the method and the Content-Length header if this is a POST, PUT or PATCH request */
stream_context_set_option( $this->context, 'http', 'method', $method );
/* Set the Content-Length header automatically if this is a POST */
if( $this->contextOptions['http']['method'] == 'POST' )
if( in_array( $method, array( 'POST', 'PUT', 'PATCH' ) ) )
{
$this->setHeaders( array( 'Content-Length' => \strlen( $data ) ) );
}
/* Parse URL */
/* Parse URL */
if ( isset( $this->url->data['user'] ) or isset( $this->url->data['pass'] ) )
{
$this->login( isset( $this->url->data['user'] ) ? $this->url->data['user'] : NULL, isset( $this->url->data['pass'] ) ? $this->url->data['pass'] : NULL );
}
$hostname = sprintf( '%s%s:%d',
( $this->url->data['scheme'] === 'https' ) ? 'tls://' : '',
$this->url->data['host'],
isset( $this->url->data['port'] )
? $this->url->data['port']
: ( $this->url->data['scheme'] === 'http' ? 80 : 443 )
);
/* Open connection */
$resource = @\fsockopen( ( $this->url->data['scheme'] === 'https' ? 'ssl://' : '' ) . $this->url->data['host'], isset( $this->url->data['port'] ) ? $this->url->data['port'] : ( $this->url->data['scheme'] === 'http' ? 80 : 443 ), $errno, $errstr, $this->timeout );
try
{
$resource = stream_socket_client( $hostname, $errno, $errstr, $this->timeout, \STREAM_CLIENT_CONNECT, $this->context );
}
/* Catch issues that may arise, such as DNS failure */
catch( \ErrorException $e )
{
throw new SocketsException( $e->getMessage(), $e->getCode() );
}
if ( $resource === FALSE )
{
throw new SocketsException( $errstr, $errno );
}
/* Work out location to use. Ideally use it exactly as $this->url has it as some servers (Google is one) will be sensitive about the encoding
and we will want to use whatever was passed to \IPS\Url::__construct. If we can't figure it out, construct manually */
$pathPos = ( isset( $this->url->data['path'] ) ) ? mb_strpos( $this->url, $this->url->data['path'] ) : FALSE;
if ( $pathPos !== FALSE )
{
$location = mb_substr( $this->url, $pathPos );
}
else
{
$location = ( isset( $this->url->data['path'] ) ? $this->url->data['path'] : '' ) . ( !empty( $this->url->queryString ) ? ( '?' . http_build_query( $this->url->queryString ) ) : '' ) . ( isset( $this->url->data['fragment'] ) ? "#{$this->url->data['fragment']}" : '' );
}
/* Send request */
$request = mb_strtoupper( $method ) . ' ' . $location . " HTTP/{$this->httpVersion}\r\n";
$request .= "Host: {$this->url->data['host']}\r\n";
/* Get the location */
$location = $this->url->data['path'] ? \IPS\Http\Url::encodeComponent( \IPS\Http\Url::COMPONENT_PATH, $this->url->data['path'] ) : '';
$location .= $this->url->data['query'] ? '?' . \IPS\Http\Url::encodeComponent( \IPS\Http\Url::COMPONENT_QUERY, $this->url->data['query'] ) : '';
$location .= $this->url->data['fragment'] ? '#' . \IPS\Http\Url::encodeComponent( \IPS\Http\Url::COMPONENT_FRAGMENT, $this->url->data['fragment'] ) : '';
/* Send request */
$request = mb_strtoupper( $method ) . ' /' . ltrim( $location, '/' ) . " HTTP/{$this->httpVersion}\r\n";
$request .= "Host: {$this->url->data['host']}" . ( isset( $this->url->data['port'] ) ? ":{$this->url->data['port']}" : '' ) . "\r\n";
foreach ( $this->headers as $k => $v )
{
$request .= "{$k}: {$v}\r\n";
}
$request .= "Connection: Close\r\n";
$request .= "\r\n";
if ( $data )
{
$request .= $data;
}
\fwrite( $resource, $request );
/* Read response */
stream_set_timeout( $resource, $this->timeout );
$status = stream_get_meta_data( $resource );
$response = '';
while( !feof($resource) and !$status['timed_out'] )
while( !feof($resource) and !$status['timed_out'] )
{
$response .= \fgets( $resource, 8192 );
$status = stream_get_meta_data( $resource );
}
/* Close connection */
\fclose( $resource );
/* Log */
try
/* 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 )
{
\IPS\Log::i( LOG_DEBUG )->write( "\n\n------------------------------------\nSOCKETS REQUEST: {$this->url}\n------------------------------------\n\n{$request}\n\n------------------------------------\nRESPONSE\n------------------------------------\n\n" . $response, 'request' );
\IPS\Log::debug( "\n\n------------------------------------\nSOCKETS REQUEST: {$this->url}\n------------------------------------\n\n{$request}\n\n------------------------------------\nRESPONSE\n------------------------------------\n\n" . $response, 'request' );
}
catch( \Exception $e ){}
/* Interpret response */
$response = new \IPS\Http\Response( $response );
/* Either return it or follow it */
if ( $this->followRedirects and ( $response->httpResponseCode == 302 OR $response->httpResponseCode == 301 ) )
if ( $this->followRedirects and in_array( $response->httpResponseCode, array( 301, 302, 303, 307, 308 ) ) )
{
$newRequest = \IPS\Http\Url::external( $response->httpHeaders['Location'] )->request( $this->timeout, $this->httpVersion, is_int( $this->followRedirects ) ? ( $this->followRedirects - 1 ) : $this->followRedirects );
/* Fix missing hostname in location */
$location = $response->httpHeaders['Location'];
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 );
return call_user_func_array( array( $newRequest, $method ), $params );
}
return $response;
}
}
/**