1
0
Fork 0
forked from lino/radar-wp

Initial import.

This commit is contained in:
ekes 2015-02-24 16:25:12 +01:00
commit 86383280c9
428 changed files with 68738 additions and 0 deletions

View file

@ -0,0 +1,131 @@
<?php
namespace Guzzle\Parser\Cookie;
/**
* Default Guzzle implementation of a Cookie parser
*/
class CookieParser implements CookieParserInterface
{
/** @var array Cookie part names to snake_case array values */
protected static $cookieParts = array(
'domain' => 'Domain',
'path' => 'Path',
'max_age' => 'Max-Age',
'expires' => 'Expires',
'version' => 'Version',
'secure' => 'Secure',
'port' => 'Port',
'discard' => 'Discard',
'comment' => 'Comment',
'comment_url' => 'Comment-Url',
'http_only' => 'HttpOnly'
);
public function parseCookie($cookie, $host = null, $path = null, $decode = false)
{
// Explode the cookie string using a series of semicolons
$pieces = array_filter(array_map('trim', explode(';', $cookie)));
// The name of the cookie (first kvp) must include an equal sign.
if (empty($pieces) || !strpos($pieces[0], '=')) {
return false;
}
// Create the default return array
$data = array_merge(array_fill_keys(array_keys(self::$cookieParts), null), array(
'cookies' => array(),
'data' => array(),
'path' => null,
'http_only' => false,
'discard' => false,
'domain' => $host
));
$foundNonCookies = 0;
// Add the cookie pieces into the parsed data array
foreach ($pieces as $part) {
$cookieParts = explode('=', $part, 2);
$key = trim($cookieParts[0]);
if (count($cookieParts) == 1) {
// Can be a single value (e.g. secure, httpOnly)
$value = true;
} else {
// Be sure to strip wrapping quotes
$value = trim($cookieParts[1], " \n\r\t\0\x0B\"");
if ($decode) {
$value = urldecode($value);
}
}
// Only check for non-cookies when cookies have been found
if (!empty($data['cookies'])) {
foreach (self::$cookieParts as $mapValue => $search) {
if (!strcasecmp($search, $key)) {
$data[$mapValue] = $mapValue == 'port' ? array_map('trim', explode(',', $value)) : $value;
$foundNonCookies++;
continue 2;
}
}
}
// If cookies have not yet been retrieved, or this value was not found in the pieces array, treat it as a
// cookie. IF non-cookies have been parsed, then this isn't a cookie, it's cookie data. Cookies then data.
$data[$foundNonCookies ? 'data' : 'cookies'][$key] = $value;
}
// Calculate the expires date
if (!$data['expires'] && $data['max_age']) {
$data['expires'] = time() + (int) $data['max_age'];
}
// Check path attribute according RFC6265 http://tools.ietf.org/search/rfc6265#section-5.2.4
// "If the attribute-value is empty or if the first character of the
// attribute-value is not %x2F ("/"):
// Let cookie-path be the default-path.
// Otherwise:
// Let cookie-path be the attribute-value."
if (!$data['path'] || substr($data['path'], 0, 1) !== '/') {
$data['path'] = $this->getDefaultPath($path);
}
return $data;
}
/**
* Get default cookie path according to RFC 6265
* http://tools.ietf.org/search/rfc6265#section-5.1.4 Paths and Path-Match
*
* @param string $path Request uri-path
*
* @return string
*/
protected function getDefaultPath($path) {
// "The user agent MUST use an algorithm equivalent to the following algorithm
// to compute the default-path of a cookie:"
// "2. If the uri-path is empty or if the first character of the uri-path is not
// a %x2F ("/") character, output %x2F ("/") and skip the remaining steps.
if (empty($path) || substr($path, 0, 1) !== '/') {
return '/';
}
// "3. If the uri-path contains no more than one %x2F ("/") character, output
// %x2F ("/") and skip the remaining step."
if ($path === "/") {
return $path;
}
$rightSlashPos = strrpos($path, '/');
if ($rightSlashPos === 0) {
return "/";
}
// "4. Output the characters of the uri-path from the first character up to,
// but not including, the right-most %x2F ("/")."
return substr($path, 0, $rightSlashPos);
}
}

View file

@ -0,0 +1,33 @@
<?php
namespace Guzzle\Parser\Cookie;
/**
* Cookie parser interface
*/
interface CookieParserInterface
{
/**
* Parse a cookie string as set in a Set-Cookie HTTP header and return an associative array of data.
*
* @param string $cookie Cookie header value to parse
* @param string $host Host of an associated request
* @param string $path Path of an associated request
* @param bool $decode Set to TRUE to urldecode cookie values
*
* @return array|bool Returns FALSE on failure or returns an array of arrays, with each of the sub arrays including:
* - domain (string) - Domain of the cookie
* - path (string) - Path of the cookie
* - cookies (array) - Associative array of cookie names and values
* - max_age (int) - Lifetime of the cookie in seconds
* - version (int) - Version of the cookie specification. RFC 2965 is 1
* - secure (bool) - Whether or not this is a secure cookie
* - discard (bool) - Whether or not this is a discardable cookie
* - custom (string) - Custom cookie data array
* - comment (string) - How the cookie is intended to be used
* - comment_url (str)- URL that contains info on how it will be used
* - port (array|str) - Array of ports or null
* - http_only (bool) - HTTP only cookie
*/
public function parseCookie($cookie, $host = null, $path = null, $decode = false);
}

View file

@ -0,0 +1,58 @@
<?php
namespace Guzzle\Parser\Message;
/**
* Implements shared message parsing functionality
*/
abstract class AbstractMessageParser implements MessageParserInterface
{
/**
* Create URL parts from HTTP message parts
*
* @param string $requestUrl Associated URL
* @param array $parts HTTP message parts
*
* @return array
*/
protected function getUrlPartsFromMessage($requestUrl, array $parts)
{
// Parse the URL information from the message
$urlParts = array(
'path' => $requestUrl,
'scheme' => 'http'
);
// Check for the Host header
if (isset($parts['headers']['Host'])) {
$urlParts['host'] = $parts['headers']['Host'];
} elseif (isset($parts['headers']['host'])) {
$urlParts['host'] = $parts['headers']['host'];
} else {
$urlParts['host'] = null;
}
if (false === strpos($urlParts['host'], ':')) {
$urlParts['port'] = '';
} else {
$hostParts = explode(':', $urlParts['host']);
$urlParts['host'] = trim($hostParts[0]);
$urlParts['port'] = (int) trim($hostParts[1]);
if ($urlParts['port'] == 443) {
$urlParts['scheme'] = 'https';
}
}
// Check if a query is present
$path = $urlParts['path'];
$qpos = strpos($path, '?');
if ($qpos) {
$urlParts['query'] = substr($path, $qpos + 1);
$urlParts['path'] = substr($path, 0, $qpos);
} else {
$urlParts['query'] = '';
}
return $urlParts;
}
}

View file

@ -0,0 +1,110 @@
<?php
namespace Guzzle\Parser\Message;
/**
* Default request and response parser used by Guzzle. Optimized for speed.
*/
class MessageParser extends AbstractMessageParser
{
public function parseRequest($message)
{
if (!$message) {
return false;
}
$parts = $this->parseMessage($message);
// Parse the protocol and protocol version
if (isset($parts['start_line'][2])) {
$startParts = explode('/', $parts['start_line'][2]);
$protocol = strtoupper($startParts[0]);
$version = isset($startParts[1]) ? $startParts[1] : '1.1';
} else {
$protocol = 'HTTP';
$version = '1.1';
}
$parsed = array(
'method' => strtoupper($parts['start_line'][0]),
'protocol' => $protocol,
'version' => $version,
'headers' => $parts['headers'],
'body' => $parts['body']
);
$parsed['request_url'] = $this->getUrlPartsFromMessage($parts['start_line'][1], $parsed);
return $parsed;
}
public function parseResponse($message)
{
if (!$message) {
return false;
}
$parts = $this->parseMessage($message);
list($protocol, $version) = explode('/', trim($parts['start_line'][0]));
return array(
'protocol' => $protocol,
'version' => $version,
'code' => $parts['start_line'][1],
'reason_phrase' => isset($parts['start_line'][2]) ? $parts['start_line'][2] : '',
'headers' => $parts['headers'],
'body' => $parts['body']
);
}
/**
* Parse a message into parts
*
* @param string $message Message to parse
*
* @return array
*/
protected function parseMessage($message)
{
$startLine = null;
$headers = array();
$body = '';
// Iterate over each line in the message, accounting for line endings
$lines = preg_split('/(\\r?\\n)/', $message, -1, PREG_SPLIT_DELIM_CAPTURE);
for ($i = 0, $totalLines = count($lines); $i < $totalLines; $i += 2) {
$line = $lines[$i];
// If two line breaks were encountered, then this is the end of body
if (empty($line)) {
if ($i < $totalLines - 1) {
$body = implode('', array_slice($lines, $i + 2));
}
break;
}
// Parse message headers
if (!$startLine) {
$startLine = explode(' ', $line, 3);
} elseif (strpos($line, ':')) {
$parts = explode(':', $line, 2);
$key = trim($parts[0]);
$value = isset($parts[1]) ? trim($parts[1]) : '';
if (!isset($headers[$key])) {
$headers[$key] = $value;
} elseif (!is_array($headers[$key])) {
$headers[$key] = array($headers[$key], $value);
} else {
$headers[$key][] = $value;
}
}
}
return array(
'start_line' => $startLine,
'headers' => $headers,
'body' => $body
);
}
}

View file

@ -0,0 +1,27 @@
<?php
namespace Guzzle\Parser\Message;
/**
* HTTP message parser interface used to parse HTTP messages into an array
*/
interface MessageParserInterface
{
/**
* Parse an HTTP request message into an associative array of parts.
*
* @param string $message HTTP request to parse
*
* @return array|bool Returns false if the message is invalid
*/
public function parseRequest($message);
/**
* Parse an HTTP response message into an associative array of parts.
*
* @param string $message HTTP response to parse
*
* @return array|bool Returns false if the message is invalid
*/
public function parseResponse($message);
}

View file

@ -0,0 +1,48 @@
<?php
namespace Guzzle\Parser\Message;
/**
* Pecl HTTP message parser
*/
class PeclHttpMessageParser extends AbstractMessageParser
{
public function parseRequest($message)
{
if (!$message) {
return false;
}
$parts = http_parse_message($message);
$parsed = array(
'method' => $parts->requestMethod,
'protocol' => 'HTTP',
'version' => number_format($parts->httpVersion, 1),
'headers' => $parts->headers,
'body' => $parts->body
);
$parsed['request_url'] = $this->getUrlPartsFromMessage($parts->requestUrl, $parsed);
return $parsed;
}
public function parseResponse($message)
{
if (!$message) {
return false;
}
$parts = http_parse_message($message);
return array(
'protocol' => 'HTTP',
'version' => number_format($parts->httpVersion, 1),
'code' => $parts->responseCode,
'reason_phrase' => $parts->responseStatus,
'headers' => $parts->headers,
'body' => $parts->body
);
}
}

View file

@ -0,0 +1,75 @@
<?php
namespace Guzzle\Parser;
/**
* Registry of parsers used by the application
*/
class ParserRegistry
{
/** @var ParserRegistry Singleton instance */
protected static $instance;
/** @var array Array of parser instances */
protected $instances = array();
/** @var array Mapping of parser name to default class */
protected $mapping = array(
'message' => 'Guzzle\\Parser\\Message\\MessageParser',
'cookie' => 'Guzzle\\Parser\\Cookie\\CookieParser',
'url' => 'Guzzle\\Parser\\Url\\UrlParser',
'uri_template' => 'Guzzle\\Parser\\UriTemplate\\UriTemplate',
);
/**
* @return self
* @codeCoverageIgnore
*/
public static function getInstance()
{
if (!self::$instance) {
self::$instance = new static;
}
return self::$instance;
}
public function __construct()
{
// Use the PECL URI template parser if available
if (extension_loaded('uri_template')) {
$this->mapping['uri_template'] = 'Guzzle\\Parser\\UriTemplate\\PeclUriTemplate';
}
}
/**
* Get a parser by name from an instance
*
* @param string $name Name of the parser to retrieve
*
* @return mixed|null
*/
public function getParser($name)
{
if (!isset($this->instances[$name])) {
if (!isset($this->mapping[$name])) {
return null;
}
$class = $this->mapping[$name];
$this->instances[$name] = new $class();
}
return $this->instances[$name];
}
/**
* Register a custom parser by name with the register
*
* @param string $name Name or handle of the parser to register
* @param mixed $parser Instantiated parser to register
*/
public function registerParser($name, $parser)
{
$this->instances[$name] = $parser;
}
}

View file

@ -0,0 +1,26 @@
<?php
namespace Guzzle\Parser\UriTemplate;
use Guzzle\Common\Exception\RuntimeException;
/**
* Expands URI templates using the uri_template pecl extension (pecl install uri_template-beta)
*
* @link http://pecl.php.net/package/uri_template
* @link https://github.com/ioseb/uri-template
*/
class PeclUriTemplate implements UriTemplateInterface
{
public function __construct()
{
if (!extension_loaded('uri_template')) {
throw new RuntimeException('uri_template PECL extension must be installed to use PeclUriTemplate');
}
}
public function expand($template, array $variables)
{
return uri_template($template, $variables);
}
}

View file

@ -0,0 +1,254 @@
<?php
namespace Guzzle\Parser\UriTemplate;
/**
* Expands URI templates using an array of variables
*
* @link http://tools.ietf.org/html/draft-gregorio-uritemplate-08
*/
class UriTemplate implements UriTemplateInterface
{
const DEFAULT_PATTERN = '/\{([^\}]+)\}/';
/** @var string URI template */
private $template;
/** @var array Variables to use in the template expansion */
private $variables;
/** @var string Regex used to parse expressions */
private $regex = self::DEFAULT_PATTERN;
/** @var array Hash for quick operator lookups */
private static $operatorHash = array(
'+' => true, '#' => true, '.' => true, '/' => true, ';' => true, '?' => true, '&' => true
);
/** @var array Delimiters */
private static $delims = array(
':', '/', '?', '#', '[', ']', '@', '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '='
);
/** @var array Percent encoded delimiters */
private static $delimsPct = array(
'%3A', '%2F', '%3F', '%23', '%5B', '%5D', '%40', '%21', '%24', '%26', '%27', '%28', '%29', '%2A', '%2B', '%2C',
'%3B', '%3D'
);
public function expand($template, array $variables)
{
if ($this->regex == self::DEFAULT_PATTERN && false === strpos($template, '{')) {
return $template;
}
$this->template = $template;
$this->variables = $variables;
return preg_replace_callback($this->regex, array($this, 'expandMatch'), $this->template);
}
/**
* Set the regex patten used to expand URI templates
*
* @param string $regexPattern
*/
public function setRegex($regexPattern)
{
$this->regex = $regexPattern;
}
/**
* Parse an expression into parts
*
* @param string $expression Expression to parse
*
* @return array Returns an associative array of parts
*/
private function parseExpression($expression)
{
// Check for URI operators
$operator = '';
if (isset(self::$operatorHash[$expression[0]])) {
$operator = $expression[0];
$expression = substr($expression, 1);
}
$values = explode(',', $expression);
foreach ($values as &$value) {
$value = trim($value);
$varspec = array();
$substrPos = strpos($value, ':');
if ($substrPos) {
$varspec['value'] = substr($value, 0, $substrPos);
$varspec['modifier'] = ':';
$varspec['position'] = (int) substr($value, $substrPos + 1);
} elseif (substr($value, -1) == '*') {
$varspec['modifier'] = '*';
$varspec['value'] = substr($value, 0, -1);
} else {
$varspec['value'] = (string) $value;
$varspec['modifier'] = '';
}
$value = $varspec;
}
return array(
'operator' => $operator,
'values' => $values
);
}
/**
* Process an expansion
*
* @param array $matches Matches met in the preg_replace_callback
*
* @return string Returns the replacement string
*/
private function expandMatch(array $matches)
{
static $rfc1738to3986 = array(
'+' => '%20',
'%7e' => '~'
);
$parsed = self::parseExpression($matches[1]);
$replacements = array();
$prefix = $parsed['operator'];
$joiner = $parsed['operator'];
$useQueryString = false;
if ($parsed['operator'] == '?') {
$joiner = '&';
$useQueryString = true;
} elseif ($parsed['operator'] == '&') {
$useQueryString = true;
} elseif ($parsed['operator'] == '#') {
$joiner = ',';
} elseif ($parsed['operator'] == ';') {
$useQueryString = true;
} elseif ($parsed['operator'] == '' || $parsed['operator'] == '+') {
$joiner = ',';
$prefix = '';
}
foreach ($parsed['values'] as $value) {
if (!array_key_exists($value['value'], $this->variables) || $this->variables[$value['value']] === null) {
continue;
}
$variable = $this->variables[$value['value']];
$actuallyUseQueryString = $useQueryString;
$expanded = '';
if (is_array($variable)) {
$isAssoc = $this->isAssoc($variable);
$kvp = array();
foreach ($variable as $key => $var) {
if ($isAssoc) {
$key = rawurlencode($key);
$isNestedArray = is_array($var);
} else {
$isNestedArray = false;
}
if (!$isNestedArray) {
$var = rawurlencode($var);
if ($parsed['operator'] == '+' || $parsed['operator'] == '#') {
$var = $this->decodeReserved($var);
}
}
if ($value['modifier'] == '*') {
if ($isAssoc) {
if ($isNestedArray) {
// Nested arrays must allow for deeply nested structures
$var = strtr(http_build_query(array($key => $var)), $rfc1738to3986);
} else {
$var = $key . '=' . $var;
}
} elseif ($key > 0 && $actuallyUseQueryString) {
$var = $value['value'] . '=' . $var;
}
}
$kvp[$key] = $var;
}
if (empty($variable)) {
$actuallyUseQueryString = false;
} elseif ($value['modifier'] == '*') {
$expanded = implode($joiner, $kvp);
if ($isAssoc) {
// Don't prepend the value name when using the explode modifier with an associative array
$actuallyUseQueryString = false;
}
} else {
if ($isAssoc) {
// When an associative array is encountered and the explode modifier is not set, then the
// result must be a comma separated list of keys followed by their respective values.
foreach ($kvp as $k => &$v) {
$v = $k . ',' . $v;
}
}
$expanded = implode(',', $kvp);
}
} else {
if ($value['modifier'] == ':') {
$variable = substr($variable, 0, $value['position']);
}
$expanded = rawurlencode($variable);
if ($parsed['operator'] == '+' || $parsed['operator'] == '#') {
$expanded = $this->decodeReserved($expanded);
}
}
if ($actuallyUseQueryString) {
if (!$expanded && $joiner != '&') {
$expanded = $value['value'];
} else {
$expanded = $value['value'] . '=' . $expanded;
}
}
$replacements[] = $expanded;
}
$ret = implode($joiner, $replacements);
if ($ret && $prefix) {
return $prefix . $ret;
}
return $ret;
}
/**
* Determines if an array is associative
*
* @param array $array Array to check
*
* @return bool
*/
private function isAssoc(array $array)
{
return (bool) count(array_filter(array_keys($array), 'is_string'));
}
/**
* Removes percent encoding on reserved characters (used with + and # modifiers)
*
* @param string $string String to fix
*
* @return string
*/
private function decodeReserved($string)
{
return str_replace(self::$delimsPct, self::$delims, $string);
}
}

View file

@ -0,0 +1,21 @@
<?php
namespace Guzzle\Parser\UriTemplate;
/**
* Expands URI templates using an array of variables
*
* @link http://tools.ietf.org/html/rfc6570
*/
interface UriTemplateInterface
{
/**
* Expand the URI template using the supplied variables
*
* @param string $template URI Template to expand
* @param array $variables Variables to use with the expansion
*
* @return string Returns the expanded template
*/
public function expand($template, array $variables);
}

View file

@ -0,0 +1,48 @@
<?php
namespace Guzzle\Parser\Url;
use Guzzle\Common\Version;
/**
* Parses URLs into parts using PHP's built-in parse_url() function
* @deprecated Just use parse_url. UTF-8 characters should be percent encoded anyways.
* @codeCoverageIgnore
*/
class UrlParser implements UrlParserInterface
{
/** @var bool Whether or not to work with UTF-8 strings */
protected $utf8 = false;
/**
* Set whether or not to attempt to handle UTF-8 strings (still WIP)
*
* @param bool $utf8 Set to TRUE to handle UTF string
*/
public function setUtf8Support($utf8)
{
$this->utf8 = $utf8;
}
public function parseUrl($url)
{
Version::warn(__CLASS__ . ' is deprecated. Just use parse_url()');
static $defaults = array('scheme' => null, 'host' => null, 'path' => null, 'port' => null, 'query' => null,
'user' => null, 'pass' => null, 'fragment' => null);
$parts = parse_url($url);
// Need to handle query parsing specially for UTF-8 requirements
if ($this->utf8 && isset($parts['query'])) {
$queryPos = strpos($url, '?');
if (isset($parts['fragment'])) {
$parts['query'] = substr($url, $queryPos + 1, strpos($url, '#') - $queryPos - 1);
} else {
$parts['query'] = substr($url, $queryPos + 1);
}
}
return $parts + $defaults;
}
}

View file

@ -0,0 +1,19 @@
<?php
namespace Guzzle\Parser\Url;
/**
* URL parser interface
*/
interface UrlParserInterface
{
/**
* Parse a URL using special handling for a subset of UTF-8 characters in the query string if needed.
*
* @param string $url URL to parse
*
* @return array Returns an array identical to what is returned from parse_url(). When an array key is missing from
* this array, you must fill it in with NULL to avoid warnings in calling code.
*/
public function parseUrl($url);
}

View file

@ -0,0 +1,19 @@
{
"name": "guzzle/parser",
"homepage": "http://guzzlephp.org/",
"description": "Interchangeable parsers used by Guzzle",
"keywords": ["HTTP", "message", "cookie", "URL", "URI Template"],
"license": "MIT",
"require": {
"php": ">=5.3.2"
},
"autoload": {
"psr-0": { "Guzzle\\Parser": "" }
},
"target-dir": "Guzzle/Parser",
"extra": {
"branch-alias": {
"dev-master": "3.7-dev"
}
}
}