Version 4.0.13.1
This commit is contained in:
1 parent
f9e857d255
commit
c6a86f0d9b
7517 files changed
+597400
-668041
No files matched your search
@@ -0,0 +1,347 @@
|
||||
<?php
|
||||
/**
|
||||
* @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.
|
||||
* @license http://www.invisionpower.com/legal/standards/
|
||||
* @package IPS Social Suite
|
||||
* @since 18 Mar 2013
|
||||
* @version SVN_VERSION_NUMBER
|
||||
*/
|
||||
|
||||
namespace IPS\Http\Request;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* cURL REST Class
|
||||
*/
|
||||
class _Curl
|
||||
{
|
||||
/**
|
||||
* @brief URL
|
||||
*/
|
||||
protected $url = NULL;
|
||||
|
||||
/**
|
||||
* @brief Curl Handle
|
||||
*/
|
||||
protected $curl = NULL;
|
||||
|
||||
/**
|
||||
* @brief Has the Content-Type header been set?
|
||||
* @note Because cURL will automatically set the Content-Type header to multipart/form-data if we send a POST request with an array, we need to change it to a string if we want to send a different Content-Type
|
||||
* @see <a href='http://www.php.net/manual/en/function.curl-setopt.php'>PHP: curl_setopt - Manual</a>
|
||||
*/
|
||||
protected $modifiedContentType = FALSE;
|
||||
|
||||
/**
|
||||
* @brief HTTP Version
|
||||
*/
|
||||
protected $httpVersion = '1.1';
|
||||
|
||||
/**
|
||||
* @brief Timeout
|
||||
*/
|
||||
protected $timeout = 5;
|
||||
|
||||
/**
|
||||
* @brief Follow redirects?
|
||||
*/
|
||||
protected $followRedirects = TRUE;
|
||||
|
||||
/**
|
||||
* @brief Data sent
|
||||
*/
|
||||
protected $dataForLog = NULL;
|
||||
|
||||
/**
|
||||
* Contructor
|
||||
*
|
||||
* @param \IPS\Http\Url $url URL
|
||||
* @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
|
||||
* @return void
|
||||
*/
|
||||
public function __construct( $url, $timeout, $httpVersion, $followRedirects )
|
||||
{
|
||||
/* Init */
|
||||
$this->url = $url;
|
||||
$this->curl = curl_init();
|
||||
$this->httpVersion = $httpVersion ?: '1.1';
|
||||
$this->timeout = $timeout;
|
||||
$this->followRedirects = $followRedirects;
|
||||
|
||||
/* Need to adjust if this is FTP */
|
||||
$user = null;
|
||||
$pass = null;
|
||||
|
||||
if( isset( $this->url->data['scheme'] ) AND $this->url->data['scheme'] == 'ftp' )
|
||||
{
|
||||
if( isset( $this->url->data['user'] ) AND $this->url->data['user'] AND isset( $this->url->data['pass'] ) AND $this->url->data['pass'] )
|
||||
{
|
||||
$user = $this->url->data['user'];
|
||||
$pass = $this->url->data['pass'];
|
||||
|
||||
$this->url->data['user'] = null;
|
||||
$this->url->data['pass'] = null;
|
||||
$this->url = $this->url->setFragment( null );
|
||||
}
|
||||
|
||||
/* Set our basic settings */
|
||||
curl_setopt_array( $this->curl, array(
|
||||
CURLOPT_HEADER => TRUE, // Specifies that we want the headers
|
||||
CURLOPT_RETURNTRANSFER => TRUE, // Specifies that we want the response
|
||||
CURLOPT_SSL_VERIFYPEER => FALSE, // Specifies that we don't need to validate the SSL certificate, if applicable (causes issues with, for example, API calls to CPanel in Nexus)
|
||||
CURLOPT_TIMEOUT => $timeout, // The timeout
|
||||
CURLOPT_URL => (string) $this->url, // The URL we're requesting
|
||||
) );
|
||||
|
||||
/* Need to set user and pass if this is FTP */
|
||||
if( $user !== null AND $pass !== null )
|
||||
{
|
||||
curl_setopt( $this->curl, CURLOPT_USERPWD, $user . ':' . $pass );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Work out HTTP version */
|
||||
if( $httpVersion === null )
|
||||
{
|
||||
$version = curl_version();
|
||||
|
||||
/* Before 7.36 there are some issues handling chunked-encoded data */
|
||||
if( version_compare( $version['version'], '7.36', '>=' ) )
|
||||
{
|
||||
$httpVersion = '1.1';
|
||||
}
|
||||
else
|
||||
{
|
||||
$httpVersion = '1.0';
|
||||
}
|
||||
}
|
||||
|
||||
$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
|
||||
CURLOPT_HTTP_VERSION => $httpVersion, // Sets the HTTP version
|
||||
CURLOPT_RETURNTRANSFER => TRUE, // Specifies that we want the response
|
||||
CURLOPT_SSL_VERIFYPEER => FALSE, // Specifies that we don't need to validate the SSL certificate, if applicable (causes issues with, for example, API calls to CPanel in Nexus)
|
||||
CURLOPT_TIMEOUT => $timeout, // The timeout
|
||||
CURLOPT_URL => (string) $this->url, // The URL we're requesting
|
||||
) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructor
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
curl_close( $this->curl );
|
||||
}
|
||||
|
||||
/**
|
||||
* Login
|
||||
*
|
||||
* @param string $username Username
|
||||
* @param string $password Password
|
||||
* @return \IPS\Http\Request\Curl
|
||||
*/
|
||||
public function login( $username, $password )
|
||||
{
|
||||
curl_setopt_array( $this->curl, array(
|
||||
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
|
||||
CURLOPT_USERPWD => "{$username}:{$password}"
|
||||
) );
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Headers
|
||||
*
|
||||
* @param array $headers Key/Value pair of headers
|
||||
* @return \IPS\Http\Request\Curl
|
||||
*/
|
||||
public function setHeaders( $headers )
|
||||
{
|
||||
$extra = array();
|
||||
|
||||
foreach ( $headers as $k => $v )
|
||||
{
|
||||
switch ( $k )
|
||||
{
|
||||
case 'Cookie':
|
||||
curl_setopt( $this->curl, CURLOPT_COOKIE, $v );
|
||||
break;
|
||||
|
||||
case 'Accept-Encoding':
|
||||
curl_setopt( $this->curl, CURLOPT_ENCODING, $v );
|
||||
break;
|
||||
|
||||
case 'Referer':
|
||||
curl_setopt( $this->curl, CURLOPT_REFERER, $v );
|
||||
break;
|
||||
|
||||
case 'User-Agent':
|
||||
curl_setopt( $this->curl, CURLOPT_USERAGENT, $v );
|
||||
break;
|
||||
|
||||
default:
|
||||
if ( $k === 'Content-Type' )
|
||||
{
|
||||
$this->modifiedContentType = TRUE;
|
||||
}
|
||||
$extra[] = "{$k}: {$v}";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ( !empty( $extra ) )
|
||||
{
|
||||
curl_setopt( $this->curl, CURLOPT_HTTPHEADER, $extra );
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 )
|
||||
{
|
||||
/* 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];
|
||||
}
|
||||
|
||||
/* 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, $method ), $params );
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the request
|
||||
*
|
||||
* @return \IPS\Http\Response
|
||||
* @throws \IPS\Http\Request\CurlException
|
||||
*/
|
||||
protected function _execute()
|
||||
{
|
||||
/* Execute */
|
||||
$output = curl_exec( $this->curl );
|
||||
|
||||
/* Log */
|
||||
try
|
||||
{
|
||||
\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' );
|
||||
}
|
||||
catch( \Exception $e ){}
|
||||
|
||||
/* Errors? */
|
||||
if ( $output === FALSE )
|
||||
{
|
||||
throw new CurlException( curl_error( $this->curl ), curl_errno( $this->curl ) );
|
||||
}
|
||||
|
||||
/* If this is FTP we need to fudge the headers a little */
|
||||
if( $this->url->data['scheme'] == 'ftp' )
|
||||
{
|
||||
$output = "HTTP/1.1 200 OK\nFTP: True\r\n\r\n" . $output;
|
||||
}
|
||||
|
||||
/* Return it */
|
||||
return new \IPS\Http\Response( $output );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* CURL Exception Class
|
||||
*/
|
||||
class CurlException extends \IPS\Http\Request\Exception { }
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
/**
|
||||
* @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.
|
||||
* @license http://www.invisionpower.com/legal/standards/
|
||||
* @package IPS Social Suite
|
||||
* @since 26 Aug 2013
|
||||
* @version SVN_VERSION_NUMBER
|
||||
*/
|
||||
|
||||
namespace IPS\Http\Request;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Request Exception Class
|
||||
*/
|
||||
abstract class _Exception extends \RuntimeException { }
|
||||
@@ -0,0 +1,200 @@
|
||||
<?php
|
||||
/**
|
||||
* @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.
|
||||
* @license http://www.invisionpower.com/legal/standards/
|
||||
* @package IPS Social Suite
|
||||
* @since 18 Mar 2013
|
||||
* @version SVN_VERSION_NUMBER
|
||||
*/
|
||||
|
||||
namespace IPS\Http\Request;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sockets REST Class
|
||||
*/
|
||||
class _Sockets
|
||||
{
|
||||
/**
|
||||
* @brief URL
|
||||
*/
|
||||
protected $url = NULL;
|
||||
|
||||
/**
|
||||
* @brief HTTP Version
|
||||
*/
|
||||
protected $httpVersion = '1.1';
|
||||
|
||||
/**
|
||||
* @brief Timeout
|
||||
*/
|
||||
protected $timeout = 5;
|
||||
|
||||
/**
|
||||
* @brief Headers
|
||||
*/
|
||||
protected $headers = array();
|
||||
|
||||
/**
|
||||
* @brief Follow redirects?
|
||||
*/
|
||||
protected $followRedirects = TRUE;
|
||||
|
||||
/**
|
||||
* Contructor
|
||||
*
|
||||
* @param \IPS\Http\Url $url URL
|
||||
* @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
|
||||
* @return void
|
||||
*/
|
||||
public function __construct( $url, $timeout, $httpVersion, $followRedirects )
|
||||
{
|
||||
$this->url = $url;
|
||||
$this->httpVersion = $httpVersion ?: '1.1';
|
||||
$this->timeout = $timeout;
|
||||
$this->followRedirects = $followRedirects;
|
||||
$this->contextOptions['http']['ignore_errors'] = TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Login
|
||||
*
|
||||
* @param string Username
|
||||
* @param string Password
|
||||
* @retrun \IPS\Http\Request\Socket (for daisy chaining)
|
||||
*/
|
||||
public function login( $username, $password )
|
||||
{
|
||||
$this->setHeaders( array( 'Authorization' => 'Basic ' . base64_encode( "{$username}:{$password}" ) ) );
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Headers
|
||||
*
|
||||
* @param array Key/Value pair of headers
|
||||
* @return \IPS\Http\Request\Socket
|
||||
*/
|
||||
public function setHeaders( $headers )
|
||||
{
|
||||
$this->headers = array_merge( $this->headers, $headers );
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic Method: __call
|
||||
*
|
||||
* @param string $method
|
||||
* @param array Params
|
||||
* @return \IPS\Http\Response
|
||||
*/
|
||||
public function __call( $method, $params )
|
||||
{
|
||||
/* The data (string or array) will be the first parameter */
|
||||
if ( isset( $params[0] ) && is_array( $params[0] ) )
|
||||
{
|
||||
$this->setHeaders( array( 'Content-Type' => 'application/x-www-form-urlencoded' ) );
|
||||
$data = http_build_query( $params[0], '', '&' );
|
||||
}
|
||||
else
|
||||
{
|
||||
$data = ( isset( $params[0] ) ? $params[0] : NULL );
|
||||
}
|
||||
|
||||
/* Set the method */
|
||||
$this->contextOptions['http']['method'] = mb_strtoupper( $method );
|
||||
|
||||
/* Set the Content-Length header automatically if this is a POST */
|
||||
if( $this->contextOptions['http']['method'] == 'POST' )
|
||||
{
|
||||
$this->setHeaders( array( 'Content-Length' => \strlen( $data ) ) );
|
||||
}
|
||||
|
||||
/* 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 );
|
||||
}
|
||||
|
||||
/* 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 );
|
||||
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";
|
||||
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'] )
|
||||
{
|
||||
$response .= \fgets( $resource, 8192 );
|
||||
$status = stream_get_meta_data( $resource );
|
||||
}
|
||||
|
||||
/* Close connection */
|
||||
\fclose( $resource );
|
||||
|
||||
/* Log */
|
||||
try
|
||||
{
|
||||
\IPS\Log::i( LOG_DEBUG )->write( "\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 ) )
|
||||
{
|
||||
$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 );
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Sockets Exception Class
|
||||
*/
|
||||
class SocketsException extends \IPS\Http\Request\Exception { }
|
||||
Whitespace-only changes.
Reference in new issue
Block a user