first commit

This commit is contained in:
alazhar
2020-01-02 22:20:31 +07:00
commit 10eb3340ad
5753 changed files with 631345 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View 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();
}
}

View File

@ -0,0 +1 @@
<!DOCTYPE html><title></title>

View 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');
}
}

View 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');
}
}