You've already forked joomla_test
first commit
This commit is contained in:
103
libraries/joomla/http/factory.php
Normal file
103
libraries/joomla/http/factory.php
Normal file
@ -0,0 +1,103 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Joomla.Platform
|
||||
* @subpackage HTTP
|
||||
*
|
||||
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE
|
||||
*/
|
||||
|
||||
defined('JPATH_PLATFORM') or die;
|
||||
|
||||
/**
|
||||
* HTTP factory class.
|
||||
*
|
||||
* @package Joomla.Platform
|
||||
* @subpackage HTTP
|
||||
* @since 12.1
|
||||
*/
|
||||
class JHttpFactory
|
||||
{
|
||||
/**
|
||||
* method to receive Http instance.
|
||||
*
|
||||
* @param JRegistry $options Client options object.
|
||||
* @param mixed $adapters Adapter (string) or queue of adapters (array) to use for communication.
|
||||
*
|
||||
* @return JHttp Joomla Http class
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public static function getHttp(JRegistry $options = null, $adapters = null)
|
||||
{
|
||||
if (empty($options))
|
||||
{
|
||||
$options = new JRegistry;
|
||||
}
|
||||
return new JHttp($options, self::getAvailableDriver($options, $adapters));
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds an available http transport object for communication
|
||||
*
|
||||
* @param JRegistry $options Option for creating http transport object
|
||||
* @param mixed $default Adapter (string) or queue of adapters (array) to use
|
||||
*
|
||||
* @return JHttpTransport Interface sub-class
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public static function getAvailableDriver(JRegistry $options, $default = null)
|
||||
{
|
||||
if (is_null($default))
|
||||
{
|
||||
$availableAdapters = self::getHttpTransports();
|
||||
}
|
||||
else
|
||||
{
|
||||
settype($default, 'array');
|
||||
$availableAdapters = $default;
|
||||
}
|
||||
// Check if there is available http transport adapters
|
||||
if (!count($availableAdapters))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
foreach ($availableAdapters as $adapter)
|
||||
{
|
||||
$class = 'JHttpTransport' . ucfirst($adapter);
|
||||
|
||||
if ($class::isSupported())
|
||||
{
|
||||
return new $class($options);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the http transport handlers
|
||||
*
|
||||
* @return array An array of available transport handlers
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
public static function getHttpTransports()
|
||||
{
|
||||
$names = array();
|
||||
$iterator = new DirectoryIterator(__DIR__ . '/transport');
|
||||
foreach ($iterator as $file)
|
||||
{
|
||||
$fileName = $file->getFilename();
|
||||
|
||||
// Only load for php files.
|
||||
// Note: DirectoryIterator::getExtension only available PHP >= 5.3.6
|
||||
if ($file->isFile() && substr($fileName, strrpos($fileName, '.') + 1) == 'php')
|
||||
{
|
||||
$names[] = substr($fileName, 0, strrpos($fileName, '.'));
|
||||
}
|
||||
}
|
||||
|
||||
return $names;
|
||||
}
|
||||
}
|
337
libraries/joomla/http/http.php
Normal file
337
libraries/joomla/http/http.php
Normal file
@ -0,0 +1,337 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Joomla.Platform
|
||||
* @subpackage HTTP
|
||||
*
|
||||
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE
|
||||
*/
|
||||
|
||||
defined('JPATH_PLATFORM') or die;
|
||||
|
||||
/**
|
||||
* HTTP client class.
|
||||
*
|
||||
* @package Joomla.Platform
|
||||
* @subpackage HTTP
|
||||
* @since 11.3
|
||||
*/
|
||||
class JHttp
|
||||
{
|
||||
/**
|
||||
* @var JRegistry Options for the HTTP client.
|
||||
* @since 11.3
|
||||
*/
|
||||
protected $options;
|
||||
|
||||
/**
|
||||
* @var JHttpTransport The HTTP transport object to use in sending HTTP requests.
|
||||
* @since 11.3
|
||||
*/
|
||||
protected $transport;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param JRegistry $options Client options object. If the registry contains any headers.* elements,
|
||||
* these will be added to the request headers.
|
||||
* @param JHttpTransport $transport The HTTP transport object.
|
||||
*
|
||||
* @since 11.3
|
||||
*/
|
||||
public function __construct(JRegistry $options = null, JHttpTransport $transport = null)
|
||||
{
|
||||
$this->options = isset($options) ? $options : new JRegistry;
|
||||
$this->transport = isset($transport) ? $transport : JHttpFactory::getAvailableDriver($this->options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an option from the HTTP client.
|
||||
*
|
||||
* @param string $key The name of the option to get.
|
||||
*
|
||||
* @return mixed The option value.
|
||||
*
|
||||
* @since 11.3
|
||||
*/
|
||||
public function getOption($key)
|
||||
{
|
||||
return $this->options->get($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an option for the HTTP client.
|
||||
*
|
||||
* @param string $key The name of the option to set.
|
||||
* @param mixed $value The option value to set.
|
||||
*
|
||||
* @return JHttp This object for method chaining.
|
||||
*
|
||||
* @since 11.3
|
||||
*/
|
||||
public function setOption($key, $value)
|
||||
{
|
||||
$this->options->set($key, $value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to send the OPTIONS command to the server.
|
||||
*
|
||||
* @param string $url Path to the resource.
|
||||
* @param array $headers An array of name-value pairs to include in the header of the request.
|
||||
* @param integer $timeout Read timeout in seconds.
|
||||
*
|
||||
* @return JHttpResponse
|
||||
*
|
||||
* @since 11.3
|
||||
*/
|
||||
public function options($url, array $headers = null, $timeout = null)
|
||||
{
|
||||
// Look for headers set in the options.
|
||||
$temp = (array) $this->options->get('headers');
|
||||
foreach ($temp as $key => $val)
|
||||
{
|
||||
if (!isset($headers[$key]))
|
||||
{
|
||||
$headers[$key] = $val;
|
||||
}
|
||||
}
|
||||
|
||||
// Look for timeout set in the options.
|
||||
if ($timeout === null && $this->options->exists('timeout'))
|
||||
{
|
||||
$timeout = $this->options->get('timeout');
|
||||
}
|
||||
|
||||
return $this->transport->request('OPTIONS', new JUri($url), null, $headers, $timeout, $this->options->get('userAgent', null));
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to send the HEAD command to the server.
|
||||
*
|
||||
* @param string $url Path to the resource.
|
||||
* @param array $headers An array of name-value pairs to include in the header of the request.
|
||||
* @param integer $timeout Read timeout in seconds.
|
||||
*
|
||||
* @return JHttpResponse
|
||||
*
|
||||
* @since 11.3
|
||||
*/
|
||||
public function head($url, array $headers = null, $timeout = null)
|
||||
{
|
||||
// Look for headers set in the options.
|
||||
$temp = (array) $this->options->get('headers');
|
||||
foreach ($temp as $key => $val)
|
||||
{
|
||||
if (!isset($headers[$key]))
|
||||
{
|
||||
$headers[$key] = $val;
|
||||
}
|
||||
}
|
||||
|
||||
// Look for timeout set in the options.
|
||||
if ($timeout === null && $this->options->exists('timeout'))
|
||||
{
|
||||
$timeout = $this->options->get('timeout');
|
||||
}
|
||||
|
||||
return $this->transport->request('HEAD', new JUri($url), null, $headers, $timeout, $this->options->get('userAgent', null));
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to send the GET command to the server.
|
||||
*
|
||||
* @param string $url Path to the resource.
|
||||
* @param array $headers An array of name-value pairs to include in the header of the request.
|
||||
* @param integer $timeout Read timeout in seconds.
|
||||
*
|
||||
* @return JHttpResponse
|
||||
*
|
||||
* @since 11.3
|
||||
*/
|
||||
public function get($url, array $headers = null, $timeout = null)
|
||||
{
|
||||
// Look for headers set in the options.
|
||||
$temp = (array) $this->options->get('headers');
|
||||
foreach ($temp as $key => $val)
|
||||
{
|
||||
if (!isset($headers[$key]))
|
||||
{
|
||||
$headers[$key] = $val;
|
||||
}
|
||||
}
|
||||
|
||||
// Look for timeout set in the options.
|
||||
if ($timeout === null && $this->options->exists('timeout'))
|
||||
{
|
||||
$timeout = $this->options->get('timeout');
|
||||
}
|
||||
|
||||
return $this->transport->request('GET', new JUri($url), null, $headers, $timeout, $this->options->get('userAgent', null));
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to send the POST command to the server.
|
||||
*
|
||||
* @param string $url Path to the resource.
|
||||
* @param mixed $data Either an associative array or a string to be sent with the request.
|
||||
* @param array $headers An array of name-value pairs to include in the header of the request
|
||||
* @param integer $timeout Read timeout in seconds.
|
||||
*
|
||||
* @return JHttpResponse
|
||||
*
|
||||
* @since 11.3
|
||||
*/
|
||||
public function post($url, $data, array $headers = null, $timeout = null)
|
||||
{
|
||||
// Look for headers set in the options.
|
||||
$temp = (array) $this->options->get('headers');
|
||||
foreach ($temp as $key => $val)
|
||||
{
|
||||
if (!isset($headers[$key]))
|
||||
{
|
||||
$headers[$key] = $val;
|
||||
}
|
||||
}
|
||||
|
||||
// Look for timeout set in the options.
|
||||
if ($timeout === null && $this->options->exists('timeout'))
|
||||
{
|
||||
$timeout = $this->options->get('timeout');
|
||||
}
|
||||
|
||||
return $this->transport->request('POST', new JUri($url), $data, $headers, $timeout, $this->options->get('userAgent', null));
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to send the PUT command to the server.
|
||||
*
|
||||
* @param string $url Path to the resource.
|
||||
* @param mixed $data Either an associative array or a string to be sent with the request.
|
||||
* @param array $headers An array of name-value pairs to include in the header of the request.
|
||||
* @param integer $timeout Read timeout in seconds.
|
||||
*
|
||||
* @return JHttpResponse
|
||||
*
|
||||
* @since 11.3
|
||||
*/
|
||||
public function put($url, $data, array $headers = null, $timeout = null)
|
||||
{
|
||||
// Look for headers set in the options.
|
||||
$temp = (array) $this->options->get('headers');
|
||||
foreach ($temp as $key => $val)
|
||||
{
|
||||
if (!isset($headers[$key]))
|
||||
{
|
||||
$headers[$key] = $val;
|
||||
}
|
||||
}
|
||||
|
||||
// Look for timeout set in the options.
|
||||
if ($timeout === null && $this->options->exists('timeout'))
|
||||
{
|
||||
$timeout = $this->options->get('timeout');
|
||||
}
|
||||
|
||||
return $this->transport->request('PUT', new JUri($url), $data, $headers, $timeout, $this->options->get('userAgent', null));
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to send the DELETE command to the server.
|
||||
*
|
||||
* @param string $url Path to the resource.
|
||||
* @param array $headers An array of name-value pairs to include in the header of the request.
|
||||
* @param integer $timeout Read timeout in seconds.
|
||||
*
|
||||
* @return JHttpResponse
|
||||
*
|
||||
* @since 11.3
|
||||
*/
|
||||
public function delete($url, array $headers = null, $timeout = null)
|
||||
{
|
||||
// Look for headers set in the options.
|
||||
$temp = (array) $this->options->get('headers');
|
||||
foreach ($temp as $key => $val)
|
||||
{
|
||||
if (!isset($headers[$key]))
|
||||
{
|
||||
$headers[$key] = $val;
|
||||
}
|
||||
}
|
||||
|
||||
// Look for timeout set in the options.
|
||||
if ($timeout === null && $this->options->exists('timeout'))
|
||||
{
|
||||
$timeout = $this->options->get('timeout');
|
||||
}
|
||||
|
||||
return $this->transport->request('DELETE', new JUri($url), null, $headers, $timeout, $this->options->get('userAgent', null));
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to send the TRACE command to the server.
|
||||
*
|
||||
* @param string $url Path to the resource.
|
||||
* @param array $headers An array of name-value pairs to include in the header of the request.
|
||||
* @param integer $timeout Read timeout in seconds.
|
||||
*
|
||||
* @return JHttpResponse
|
||||
*
|
||||
* @since 11.3
|
||||
*/
|
||||
public function trace($url, array $headers = null, $timeout = null)
|
||||
{
|
||||
// Look for headers set in the options.
|
||||
$temp = (array) $this->options->get('headers');
|
||||
foreach ($temp as $key => $val)
|
||||
{
|
||||
if (!isset($headers[$key]))
|
||||
{
|
||||
$headers[$key] = $val;
|
||||
}
|
||||
}
|
||||
|
||||
// Look for timeout set in the options.
|
||||
if ($timeout === null && $this->options->exists('timeout'))
|
||||
{
|
||||
$timeout = $this->options->get('timeout');
|
||||
}
|
||||
|
||||
return $this->transport->request('TRACE', new JUri($url), null, $headers, $timeout, $this->options->get('userAgent', null));
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to send the PATCH command to the server.
|
||||
*
|
||||
* @param string $url Path to the resource.
|
||||
* @param mixed $data Either an associative array or a string to be sent with the request.
|
||||
* @param array $headers An array of name-value pairs to include in the header of the request.
|
||||
* @param integer $timeout Read timeout in seconds.
|
||||
*
|
||||
* @return JHttpResponse
|
||||
*
|
||||
* @since 12.2
|
||||
*/
|
||||
public function patch($url, $data, array $headers = null, $timeout = null)
|
||||
{
|
||||
// Look for headers set in the options.
|
||||
$temp = (array) $this->options->get('headers');
|
||||
foreach ($temp as $key => $val)
|
||||
{
|
||||
if (!isset($headers[$key]))
|
||||
{
|
||||
$headers[$key] = $val;
|
||||
}
|
||||
}
|
||||
|
||||
// Look for timeout set in the options.
|
||||
if ($timeout === null && $this->options->exists('timeout'))
|
||||
{
|
||||
$timeout = $this->options->get('timeout');
|
||||
}
|
||||
|
||||
return $this->transport->request('PATCH', new JUri($url), $data, $headers, $timeout, $this->options->get('userAgent', null));
|
||||
}
|
||||
}
|
1
libraries/joomla/http/index.html
Normal file
1
libraries/joomla/http/index.html
Normal file
@ -0,0 +1 @@
|
||||
<!DOCTYPE html><title></title>
|
38
libraries/joomla/http/response.php
Normal file
38
libraries/joomla/http/response.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Joomla.Platform
|
||||
* @subpackage HTTP
|
||||
*
|
||||
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE
|
||||
*/
|
||||
|
||||
defined('JPATH_PLATFORM') or die;
|
||||
|
||||
/**
|
||||
* HTTP response data object class.
|
||||
*
|
||||
* @package Joomla.Platform
|
||||
* @subpackage HTTP
|
||||
* @since 11.3
|
||||
*/
|
||||
class JHttpResponse
|
||||
{
|
||||
/**
|
||||
* @var integer The server response code.
|
||||
* @since 11.3
|
||||
*/
|
||||
public $code;
|
||||
|
||||
/**
|
||||
* @var array Response headers.
|
||||
* @since 11.3
|
||||
*/
|
||||
public $headers = array();
|
||||
|
||||
/**
|
||||
* @var string Server response body.
|
||||
* @since 11.3
|
||||
*/
|
||||
public $body;
|
||||
}
|
54
libraries/joomla/http/transport.php
Normal file
54
libraries/joomla/http/transport.php
Normal file
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Joomla.Platform
|
||||
* @subpackage HTTP
|
||||
*
|
||||
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE
|
||||
*/
|
||||
|
||||
defined('JPATH_PLATFORM') or die;
|
||||
|
||||
/**
|
||||
* HTTP transport class interface.
|
||||
*
|
||||
* @package Joomla.Platform
|
||||
* @subpackage HTTP
|
||||
* @since 11.3
|
||||
*/
|
||||
interface JHttpTransport
|
||||
{
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param JRegistry $options Client options object.
|
||||
*
|
||||
* @since 11.3
|
||||
*/
|
||||
public function __construct(JRegistry $options);
|
||||
|
||||
/**
|
||||
* Send a request to the server and return a JHttpResponse object with the response.
|
||||
*
|
||||
* @param string $method The HTTP method for sending the request.
|
||||
* @param JUri $uri The URI to the resource to request.
|
||||
* @param mixed $data Either an associative array or a string to be sent with the request.
|
||||
* @param array $headers An array of request headers to send with the request.
|
||||
* @param integer $timeout Read timeout in seconds.
|
||||
* @param string $userAgent The optional user agent string to send with the request.
|
||||
*
|
||||
* @return JHttpResponse
|
||||
*
|
||||
* @since 11.3
|
||||
*/
|
||||
public function request($method, JUri $uri, $data = null, array $headers = null, $timeout = null, $userAgent = null);
|
||||
|
||||
/**
|
||||
* method to check if http transport layer available for using
|
||||
*
|
||||
* @return bool true if available else false
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
static public function isSupported();
|
||||
}
|
3721
libraries/joomla/http/transport/cacert.pem
Normal file
3721
libraries/joomla/http/transport/cacert.pem
Normal file
File diff suppressed because it is too large
Load Diff
230
libraries/joomla/http/transport/curl.php
Normal file
230
libraries/joomla/http/transport/curl.php
Normal file
@ -0,0 +1,230 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Joomla.Platform
|
||||
* @subpackage HTTP
|
||||
*
|
||||
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE
|
||||
*/
|
||||
|
||||
defined('JPATH_PLATFORM') or die;
|
||||
|
||||
/**
|
||||
* HTTP transport class for using cURL.
|
||||
*
|
||||
* @package Joomla.Platform
|
||||
* @subpackage HTTP
|
||||
* @since 11.3
|
||||
*/
|
||||
class JHttpTransportCurl implements JHttpTransport
|
||||
{
|
||||
/**
|
||||
* @var JRegistry The client options.
|
||||
* @since 11.3
|
||||
*/
|
||||
protected $options;
|
||||
|
||||
/**
|
||||
* Constructor. CURLOPT_FOLLOWLOCATION must be disabled when open_basedir or safe_mode are enabled.
|
||||
*
|
||||
* @param JRegistry $options Client options object.
|
||||
*
|
||||
* @see http://www.php.net/manual/en/function.curl-setopt.php
|
||||
* @since 11.3
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function __construct(JRegistry $options)
|
||||
{
|
||||
if (!function_exists('curl_init') || !is_callable('curl_init'))
|
||||
{
|
||||
throw new RuntimeException('Cannot use a cURL transport when curl_init() is not available.');
|
||||
}
|
||||
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a request to the server and return a JHttpResponse object with the response.
|
||||
*
|
||||
* @param string $method The HTTP method for sending the request.
|
||||
* @param JUri $uri The URI to the resource to request.
|
||||
* @param mixed $data Either an associative array or a string to be sent with the request.
|
||||
* @param array $headers An array of request headers to send with the request.
|
||||
* @param integer $timeout Read timeout in seconds.
|
||||
* @param string $userAgent The optional user agent string to send with the request.
|
||||
*
|
||||
* @return JHttpResponse
|
||||
*
|
||||
* @since 11.3
|
||||
*/
|
||||
public function request($method, JUri $uri, $data = null, array $headers = null, $timeout = null, $userAgent = null)
|
||||
{
|
||||
// Setup the cURL handle.
|
||||
$ch = curl_init();
|
||||
|
||||
// Set the request method.
|
||||
$options[CURLOPT_CUSTOMREQUEST] = strtoupper($method);
|
||||
|
||||
// Don't wait for body when $method is HEAD
|
||||
$options[CURLOPT_NOBODY] = ($method === 'HEAD');
|
||||
|
||||
// Initialize the certificate store
|
||||
$options[CURLOPT_CAINFO] = $this->options->get('curl.certpath', __DIR__ . '/cacert.pem');
|
||||
|
||||
// If data exists let's encode it and make sure our Content-type header is set.
|
||||
if (isset($data))
|
||||
{
|
||||
// If the data is a scalar value simply add it to the cURL post fields.
|
||||
if (is_scalar($data) || (isset($headers['Content-Type']) && strpos($headers['Content-Type'], 'multipart/form-data') === 0))
|
||||
{
|
||||
$options[CURLOPT_POSTFIELDS] = $data;
|
||||
}
|
||||
// Otherwise we need to encode the value first.
|
||||
else
|
||||
{
|
||||
$options[CURLOPT_POSTFIELDS] = http_build_query($data);
|
||||
}
|
||||
|
||||
if (!isset($headers['Content-Type']))
|
||||
{
|
||||
$headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8';
|
||||
}
|
||||
|
||||
// Add the relevant headers.
|
||||
if (is_scalar($options[CURLOPT_POSTFIELDS]))
|
||||
{
|
||||
$headers['Content-Length'] = strlen($options[CURLOPT_POSTFIELDS]);
|
||||
}
|
||||
}
|
||||
|
||||
// Build the headers string for the request.
|
||||
$headerArray = array();
|
||||
if (isset($headers))
|
||||
{
|
||||
foreach ($headers as $key => $value)
|
||||
{
|
||||
$headerArray[] = $key . ': ' . $value;
|
||||
}
|
||||
|
||||
// Add the headers string into the stream context options array.
|
||||
$options[CURLOPT_HTTPHEADER] = $headerArray;
|
||||
}
|
||||
|
||||
// If an explicit timeout is given user it.
|
||||
if (isset($timeout))
|
||||
{
|
||||
$options[CURLOPT_TIMEOUT] = (int) $timeout;
|
||||
$options[CURLOPT_CONNECTTIMEOUT] = (int) $timeout;
|
||||
}
|
||||
|
||||
// If an explicit user agent is given use it.
|
||||
if (isset($userAgent))
|
||||
{
|
||||
$headers[CURLOPT_USERAGENT] = $userAgent;
|
||||
}
|
||||
|
||||
// Set the request URL.
|
||||
$options[CURLOPT_URL] = (string) $uri;
|
||||
|
||||
// We want our headers. :-)
|
||||
$options[CURLOPT_HEADER] = true;
|
||||
|
||||
// Return it... echoing it would be tacky.
|
||||
$options[CURLOPT_RETURNTRANSFER] = true;
|
||||
|
||||
// Override the Expect header to prevent cURL from confusing itself in its own stupidity.
|
||||
// Link: http://the-stickman.com/web-development/php-and-curl-disabling-100-continue-header/
|
||||
$options[CURLOPT_HTTPHEADER][] = 'Expect:';
|
||||
|
||||
// Follow redirects.
|
||||
$options[CURLOPT_FOLLOWLOCATION] = (bool) $this->options->get('follow_location', true);
|
||||
|
||||
// Set the cURL options.
|
||||
curl_setopt_array($ch, $options);
|
||||
|
||||
// Execute the request and close the connection.
|
||||
$content = curl_exec($ch);
|
||||
|
||||
// Get the request information.
|
||||
$info = curl_getinfo($ch);
|
||||
|
||||
// Close the connection.
|
||||
curl_close($ch);
|
||||
|
||||
return $this->getResponse($content, $info);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get a response object from a server response.
|
||||
*
|
||||
* @param string $content The complete server response, including headers.
|
||||
* @param array $info The cURL request information.
|
||||
*
|
||||
* @return JHttpResponse
|
||||
*
|
||||
* @since 11.3
|
||||
* @throws UnexpectedValueException
|
||||
*/
|
||||
protected function getResponse($content, $info)
|
||||
{
|
||||
// Create the response object.
|
||||
$return = new JHttpResponse;
|
||||
|
||||
// Check if the content is actually a string.
|
||||
if (!is_string($content))
|
||||
{
|
||||
throw new UnexpectedValueException('No HTTP response received.');
|
||||
}
|
||||
|
||||
// Get the number of redirects that occurred.
|
||||
$redirects = isset($info['redirect_count']) ? $info['redirect_count'] : 0;
|
||||
|
||||
/*
|
||||
* Split the response into headers and body. If cURL encountered redirects, the headers for the redirected requests will
|
||||
* also be included. So we split the response into header + body + the number of redirects and only use the last two
|
||||
* sections which should be the last set of headers and the actual body.
|
||||
*/
|
||||
$response = explode("\r\n\r\n", $content, 2 + $redirects);
|
||||
|
||||
// Set the body for the response.
|
||||
$return->body = array_pop($response);
|
||||
|
||||
// Get the last set of response headers as an array.
|
||||
$headers = explode("\r\n", array_pop($response));
|
||||
|
||||
// Get the response code from the first offset of the response headers.
|
||||
preg_match('/[0-9]{3}/', array_shift($headers), $matches);
|
||||
|
||||
$code = count($matches) ? $matches[0] : null;
|
||||
if (is_numeric($code))
|
||||
{
|
||||
$return->code = (int) $code;
|
||||
}
|
||||
// No valid response code was detected.
|
||||
else
|
||||
{
|
||||
throw new UnexpectedValueException('No HTTP response code found.');
|
||||
}
|
||||
|
||||
// Add the response headers to the response object.
|
||||
foreach ($headers as $header)
|
||||
{
|
||||
$pos = strpos($header, ':');
|
||||
$return->headers[trim(substr($header, 0, $pos))] = trim(substr($header, ($pos + 1)));
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to check if HTTP transport cURL is available for use
|
||||
*
|
||||
* @return boolean true if available, else false
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
static public function isSupported()
|
||||
{
|
||||
return function_exists('curl_version') && curl_version();
|
||||
}
|
||||
}
|
1
libraries/joomla/http/transport/index.html
Normal file
1
libraries/joomla/http/transport/index.html
Normal file
@ -0,0 +1 @@
|
||||
<!DOCTYPE html><title></title>
|
281
libraries/joomla/http/transport/socket.php
Normal file
281
libraries/joomla/http/transport/socket.php
Normal file
@ -0,0 +1,281 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Joomla.Platform
|
||||
* @subpackage HTTP
|
||||
*
|
||||
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE
|
||||
*/
|
||||
|
||||
defined('JPATH_PLATFORM') or die;
|
||||
|
||||
/**
|
||||
* HTTP transport class for using sockets directly.
|
||||
*
|
||||
* @package Joomla.Platform
|
||||
* @subpackage HTTP
|
||||
* @since 11.3
|
||||
*/
|
||||
class JHttpTransportSocket implements JHttpTransport
|
||||
{
|
||||
/**
|
||||
* @var array Reusable socket connections.
|
||||
* @since 11.3
|
||||
*/
|
||||
protected $connections;
|
||||
|
||||
/**
|
||||
* @var JRegistry The client options.
|
||||
* @since 11.3
|
||||
*/
|
||||
protected $options;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param JRegistry $options Client options object.
|
||||
*
|
||||
* @since 11.3
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function __construct(JRegistry $options)
|
||||
{
|
||||
if (!self::isSupported())
|
||||
{
|
||||
throw new RuntimeException('Cannot use a socket transport when fsockopen() is not available.');
|
||||
}
|
||||
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a request to the server and return a JHttpResponse object with the response.
|
||||
*
|
||||
* @param string $method The HTTP method for sending the request.
|
||||
* @param JUri $uri The URI to the resource to request.
|
||||
* @param mixed $data Either an associative array or a string to be sent with the request.
|
||||
* @param array $headers An array of request headers to send with the request.
|
||||
* @param integer $timeout Read timeout in seconds.
|
||||
* @param string $userAgent The optional user agent string to send with the request.
|
||||
*
|
||||
* @return JHttpResponse
|
||||
*
|
||||
* @since 11.3
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function request($method, JUri $uri, $data = null, array $headers = null, $timeout = null, $userAgent = null)
|
||||
{
|
||||
$connection = $this->connect($uri, $timeout);
|
||||
|
||||
// Make sure the connection is alive and valid.
|
||||
if (is_resource($connection))
|
||||
{
|
||||
// Make sure the connection has not timed out.
|
||||
$meta = stream_get_meta_data($connection);
|
||||
if ($meta['timed_out'])
|
||||
{
|
||||
throw new RuntimeException('Server connection timed out.');
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new RuntimeException('Not connected to server.');
|
||||
}
|
||||
|
||||
// Get the request path from the URI object.
|
||||
$path = $uri->toString(array('path', 'query'));
|
||||
|
||||
// If we have data to send make sure our request is setup for it.
|
||||
if (!empty($data))
|
||||
{
|
||||
// If the data is not a scalar value encode it to be sent with the request.
|
||||
if (!is_scalar($data))
|
||||
{
|
||||
$data = http_build_query($data);
|
||||
}
|
||||
|
||||
if (!isset($headers['Content-Type']))
|
||||
{
|
||||
$headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8';
|
||||
}
|
||||
|
||||
// Add the relevant headers.
|
||||
$headers['Content-Length'] = strlen($data);
|
||||
}
|
||||
|
||||
// Build the request payload.
|
||||
$request = array();
|
||||
$request[] = strtoupper($method) . ' ' . ((empty($path)) ? '/' : $path) . ' HTTP/1.0';
|
||||
$request[] = 'Host: ' . $uri->getHost();
|
||||
|
||||
// If an explicit user agent is given use it.
|
||||
if (isset($userAgent))
|
||||
{
|
||||
$headers['User-Agent'] = $userAgent;
|
||||
}
|
||||
|
||||
// If there are custom headers to send add them to the request payload.
|
||||
if (is_array($headers))
|
||||
{
|
||||
foreach ($headers as $k => $v)
|
||||
{
|
||||
$request[] = $k . ': ' . $v;
|
||||
}
|
||||
}
|
||||
|
||||
// If we have data to send add it to the request payload.
|
||||
if (!empty($data))
|
||||
{
|
||||
$request[] = null;
|
||||
$request[] = $data;
|
||||
}
|
||||
|
||||
// Send the request to the server.
|
||||
fwrite($connection, implode("\r\n", $request) . "\r\n\r\n");
|
||||
|
||||
// Get the response data from the server.
|
||||
$content = '';
|
||||
|
||||
while (!feof($connection))
|
||||
{
|
||||
$content .= fgets($connection, 4096);
|
||||
}
|
||||
|
||||
return $this->getResponse($content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get a response object from a server response.
|
||||
*
|
||||
* @param string $content The complete server response, including headers.
|
||||
*
|
||||
* @return JHttpResponse
|
||||
*
|
||||
* @since 11.3
|
||||
* @throws UnexpectedValueException
|
||||
*/
|
||||
protected function getResponse($content)
|
||||
{
|
||||
// Create the response object.
|
||||
$return = new JHttpResponse;
|
||||
|
||||
// Split the response into headers and body.
|
||||
$response = explode("\r\n\r\n", $content, 2);
|
||||
|
||||
// Get the response headers as an array.
|
||||
$headers = explode("\r\n", $response[0]);
|
||||
|
||||
// Set the body for the response.
|
||||
$return->body = $response[1];
|
||||
|
||||
// Get the response code from the first offset of the response headers.
|
||||
preg_match('/[0-9]{3}/', array_shift($headers), $matches);
|
||||
$code = $matches[0];
|
||||
if (is_numeric($code))
|
||||
{
|
||||
$return->code = (int) $code;
|
||||
}
|
||||
// No valid response code was detected.
|
||||
else
|
||||
{
|
||||
throw new UnexpectedValueException('No HTTP response code found.');
|
||||
}
|
||||
|
||||
// Add the response headers to the response object.
|
||||
foreach ($headers as $header)
|
||||
{
|
||||
$pos = strpos($header, ':');
|
||||
$return->headers[trim(substr($header, 0, $pos))] = trim(substr($header, ($pos + 1)));
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to connect to a server and get the resource.
|
||||
*
|
||||
* @param JUri $uri The URI to connect with.
|
||||
* @param integer $timeout Read timeout in seconds.
|
||||
*
|
||||
* @return resource Socket connection resource.
|
||||
*
|
||||
* @since 11.3
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
protected function connect(JUri $uri, $timeout = null)
|
||||
{
|
||||
$errno = null;
|
||||
$err = null;
|
||||
|
||||
// Get the host from the uri.
|
||||
$host = ($uri->isSSL()) ? 'ssl://' . $uri->getHost() : $uri->getHost();
|
||||
|
||||
// If the port is not explicitly set in the URI detect it.
|
||||
if (!$uri->getPort())
|
||||
{
|
||||
$port = ($uri->getScheme() == 'https') ? 443 : 80;
|
||||
}
|
||||
// Use the set port.
|
||||
else
|
||||
{
|
||||
$port = $uri->getPort();
|
||||
}
|
||||
|
||||
// Build the connection key for resource memory caching.
|
||||
$key = md5($host . $port);
|
||||
|
||||
// If the connection already exists, use it.
|
||||
if (!empty($this->connections[$key]) && is_resource($this->connections[$key]))
|
||||
{
|
||||
// Connection reached EOF, cannot be used anymore
|
||||
$meta = stream_get_meta_data($this->connections[$key]);
|
||||
if ($meta['eof'])
|
||||
{
|
||||
if (!fclose($this->connections[$key]))
|
||||
{
|
||||
throw new RuntimeException('Cannot close connection');
|
||||
}
|
||||
}
|
||||
// Make sure the connection has not timed out.
|
||||
elseif (!$meta['timed_out'])
|
||||
{
|
||||
return $this->connections[$key];
|
||||
}
|
||||
}
|
||||
|
||||
if (!is_numeric($timeout))
|
||||
{
|
||||
$timeout = ini_get("default_socket_timeout");
|
||||
}
|
||||
// Attempt to connect to the server.
|
||||
$connection = fsockopen($host, $port, $errno, $err, $timeout);
|
||||
if (!$connection)
|
||||
{
|
||||
throw new RuntimeException($err, $errno);
|
||||
}
|
||||
|
||||
// Since the connection was successful let's store it in case we need to use it later.
|
||||
$this->connections[$key] = $connection;
|
||||
|
||||
// If an explicit timeout is set, set it.
|
||||
if (isset($timeout))
|
||||
{
|
||||
stream_set_timeout($this->connections[$key], (int) $timeout);
|
||||
}
|
||||
|
||||
return $this->connections[$key];
|
||||
}
|
||||
|
||||
/**
|
||||
* method to check if http transport socket available for using
|
||||
*
|
||||
* @return bool true if available else false
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
static public function isSupported()
|
||||
{
|
||||
return function_exists('fsockopen') && is_callable('fsockopen');
|
||||
}
|
||||
|
||||
}
|
216
libraries/joomla/http/transport/stream.php
Normal file
216
libraries/joomla/http/transport/stream.php
Normal file
@ -0,0 +1,216 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Joomla.Platform
|
||||
* @subpackage HTTP
|
||||
*
|
||||
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE
|
||||
*/
|
||||
|
||||
defined('JPATH_PLATFORM') or die;
|
||||
|
||||
/**
|
||||
* HTTP transport class for using PHP streams.
|
||||
*
|
||||
* @package Joomla.Platform
|
||||
* @subpackage HTTP
|
||||
* @since 11.3
|
||||
*/
|
||||
class JHttpTransportStream implements JHttpTransport
|
||||
{
|
||||
/**
|
||||
* @var JRegistry The client options.
|
||||
* @since 11.3
|
||||
*/
|
||||
protected $options;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param JRegistry $options Client options object.
|
||||
*
|
||||
* @since 11.3
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function __construct(JRegistry $options)
|
||||
{
|
||||
// Verify that fopen() is available.
|
||||
if (!self::isSupported())
|
||||
{
|
||||
throw new RuntimeException('Cannot use a stream transport when fopen() is not available.');
|
||||
}
|
||||
|
||||
// Verify that URLs can be used with fopen();
|
||||
if (!ini_get('allow_url_fopen'))
|
||||
{
|
||||
throw new RuntimeException('Cannot use a stream transport when "allow_url_fopen" is disabled.');
|
||||
}
|
||||
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a request to the server and return a JHttpResponse object with the response.
|
||||
*
|
||||
* @param string $method The HTTP method for sending the request.
|
||||
* @param JUri $uri The URI to the resource to request.
|
||||
* @param mixed $data Either an associative array or a string to be sent with the request.
|
||||
* @param array $headers An array of request headers to send with the request.
|
||||
* @param integer $timeout Read timeout in seconds.
|
||||
* @param string $userAgent The optional user agent string to send with the request.
|
||||
*
|
||||
* @return JHttpResponse
|
||||
*
|
||||
* @since 11.3
|
||||
*/
|
||||
public function request($method, JUri $uri, $data = null, array $headers = null, $timeout = null, $userAgent = null)
|
||||
{
|
||||
// Create the stream context options array with the required method offset.
|
||||
$options = array('method' => strtoupper($method));
|
||||
|
||||
// If data exists let's encode it and make sure our Content-Type header is set.
|
||||
if (isset($data))
|
||||
{
|
||||
// If the data is a scalar value simply add it to the stream context options.
|
||||
if (is_scalar($data))
|
||||
{
|
||||
$options['content'] = $data;
|
||||
}
|
||||
// Otherwise we need to encode the value first.
|
||||
else
|
||||
{
|
||||
$options['content'] = http_build_query($data);
|
||||
}
|
||||
|
||||
if (!isset($headers['Content-Type']))
|
||||
{
|
||||
$headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8';
|
||||
}
|
||||
|
||||
// Add the relevant headers.
|
||||
$headers['Content-Length'] = strlen($options['content']);
|
||||
}
|
||||
|
||||
// Build the headers string for the request.
|
||||
$headerString = null;
|
||||
if (isset($headers))
|
||||
{
|
||||
foreach ($headers as $key => $value)
|
||||
{
|
||||
$headerString .= $key . ': ' . $value . "\r\n";
|
||||
}
|
||||
|
||||
// Add the headers string into the stream context options array.
|
||||
$options['header'] = trim($headerString, "\r\n");
|
||||
}
|
||||
|
||||
// If an explicit timeout is given user it.
|
||||
if (isset($timeout))
|
||||
{
|
||||
$options['timeout'] = (int) $timeout;
|
||||
}
|
||||
|
||||
// If an explicit user agent is given use it.
|
||||
if (isset($userAgent))
|
||||
{
|
||||
$options['user_agent'] = $userAgent;
|
||||
}
|
||||
|
||||
// Ignore HTTP errors so that we can capture them.
|
||||
$options['ignore_errors'] = 1;
|
||||
|
||||
// Follow redirects.
|
||||
$options['follow_location'] = (int) $this->options->get('follow_location', 1);
|
||||
|
||||
// Create the stream context for the request.
|
||||
$context = stream_context_create(array('http' => $options));
|
||||
|
||||
// Open the stream for reading.
|
||||
$stream = @fopen((string) $uri, 'r', false, $context);
|
||||
|
||||
// Check if the stream is open.
|
||||
if (!$stream)
|
||||
{
|
||||
throw new RuntimeException(sprintf('Could not connect to resource: %s', $uri));
|
||||
}
|
||||
|
||||
// Get the metadata for the stream, including response headers.
|
||||
$metadata = stream_get_meta_data($stream);
|
||||
|
||||
// Get the contents from the stream.
|
||||
$content = stream_get_contents($stream);
|
||||
|
||||
// Close the stream.
|
||||
fclose($stream);
|
||||
|
||||
if (isset($metadata['wrapper_data']['headers']))
|
||||
{
|
||||
$headers = $metadata['wrapper_data']['headers'];
|
||||
}
|
||||
elseif (isset($metadata['wrapper_data']))
|
||||
{
|
||||
$headers = $metadata['wrapper_data'];
|
||||
}
|
||||
else
|
||||
{
|
||||
$headers = array();
|
||||
}
|
||||
|
||||
return $this->getResponse($headers, $content);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get a response object from a server response.
|
||||
*
|
||||
* @param array $headers The response headers as an array.
|
||||
* @param string $body The response body as a string.
|
||||
*
|
||||
* @return JHttpResponse
|
||||
*
|
||||
* @since 11.3
|
||||
* @throws UnexpectedValueException
|
||||
*/
|
||||
protected function getResponse(array $headers, $body)
|
||||
{
|
||||
// Create the response object.
|
||||
$return = new JHttpResponse;
|
||||
|
||||
// Set the body for the response.
|
||||
$return->body = $body;
|
||||
|
||||
// Get the response code from the first offset of the response headers.
|
||||
preg_match('/[0-9]{3}/', array_shift($headers), $matches);
|
||||
$code = $matches[0];
|
||||
if (is_numeric($code))
|
||||
{
|
||||
$return->code = (int) $code;
|
||||
}
|
||||
// No valid response code was detected.
|
||||
else
|
||||
{
|
||||
throw new UnexpectedValueException('No HTTP response code found.');
|
||||
}
|
||||
|
||||
// Add the response headers to the response object.
|
||||
foreach ($headers as $header)
|
||||
{
|
||||
$pos = strpos($header, ':');
|
||||
$return->headers[trim(substr($header, 0, $pos))] = trim(substr($header, ($pos + 1)));
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* method to check if http transport stream available for using
|
||||
*
|
||||
* @return bool true if available else false
|
||||
*
|
||||
* @since 12.1
|
||||
*/
|
||||
static public function isSupported()
|
||||
{
|
||||
return function_exists('fopen') && is_callable('fopen');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user