v1.2.7.416

This commit is contained in:
RainLoop Team 2013-09-30 21:01:08 +04:00
parent 54e2645fcf
commit 3b27abbe9a
434 changed files with 51 additions and 72 deletions

View file

@ -0,0 +1,216 @@
<?php
namespace Buzz\Util;
use Buzz\Message\RequestInterface;
class Cookie
{
const ATTR_DOMAIN = 'domain';
const ATTR_PATH = 'path';
const ATTR_SECURE = 'secure';
const ATTR_MAX_AGE = 'max-age';
const ATTR_EXPIRES = 'expires';
protected $name;
protected $value;
protected $attributes = array();
protected $createdAt;
/**
* Constructor.
*/
public function __construct()
{
$this->createdAt = time();
}
/**
* Returns true if the current cookie matches the supplied request.
*
* @return boolean
*/
public function matchesRequest(RequestInterface $request)
{
// domain
if (!$this->matchesDomain(parse_url($request->getHost(), PHP_URL_HOST))) {
return false;
}
// path
if (!$this->matchesPath($request->getResource())) {
return false;
}
// secure
if ($this->hasAttribute(static::ATTR_SECURE) && !$request->isSecure()) {
return false;
}
return true;
}
/**
* Returns true of the current cookie has expired.
*
* Checks the max-age and expires attributes.
*
* @return boolean Whether the current cookie has expired
*/
public function isExpired()
{
$maxAge = $this->getAttribute(static::ATTR_MAX_AGE);
if ($maxAge && time() - $this->getCreatedAt() > $maxAge) {
return true;
}
$expires = $this->getAttribute(static::ATTR_EXPIRES);
if ($expires && strtotime($expires) < time()) {
return true;
}
return false;
}
/**
* Returns true if the current cookie matches the supplied domain.
*
* @param string $domain A domain hostname
*
* @return boolean
*/
public function matchesDomain($domain)
{
$cookieDomain = $this->getAttribute(static::ATTR_DOMAIN);
if (0 === strpos($cookieDomain, '.')) {
$pattern = '/\b'.preg_quote(substr($cookieDomain, 1), '/').'$/i';
return (boolean) preg_match($pattern, $domain);
} else {
return 0 == strcasecmp($cookieDomain, $domain);
}
}
/**
* Returns true if the current cookie matches the supplied path.
*
* @param string $path A path
*
* @return boolean
*/
public function matchesPath($path)
{
$needle = $this->getAttribute(static::ATTR_PATH);
return null === $needle || 0 === strpos($path, $needle);
}
/**
* Populates the current cookie with data from the supplied Set-Cookie header.
*
* @param string $header A Set-Cookie header
* @param string $issuingDomain The domain that issued the header
*/
public function fromSetCookieHeader($header, $issuingDomain)
{
list($this->name, $header) = explode('=', $header, 2);
if (false === strpos($header, ';')) {
$this->value = $header;
$header = null;
} else {
list($this->value, $header) = explode(';', $header, 2);
}
$this->clearAttributes();
foreach (array_map('trim', explode(';', trim($header))) as $pair) {
if (false === strpos($pair, '=')) {
$name = $pair;
$value = null;
} else {
list($name, $value) = explode('=', $pair);
}
$this->setAttribute($name, $value);
}
if (!$this->getAttribute(static::ATTR_DOMAIN)) {
$this->setAttribute(static::ATTR_DOMAIN, $issuingDomain);
}
}
/**
* Formats a Cookie header for the current cookie.
*
* @return string An HTTP request Cookie header
*/
public function toCookieHeader()
{
return 'Cookie: '.$this->getName().'='.$this->getValue();
}
public function setName($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
public function setValue($value)
{
$this->value = $value;
}
public function getValue()
{
return $this->value;
}
public function setAttributes(array $attributes)
{
// attributes are case insensitive
$this->attributes = array_change_key_case($attributes);
}
public function setAttribute($name, $value)
{
$this->attributes[strtolower($name)] = $value;
}
public function getAttributes()
{
return $this->attributes;
}
public function getAttribute($name)
{
$name = strtolower($name);
if (isset($this->attributes[$name])) {
return $this->attributes[$name];
}
}
public function hasAttribute($name)
{
return array_key_exists($name, $this->attributes);
}
public function clearAttributes()
{
$this->setAttributes(array());
}
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
}
public function getCreatedAt()
{
return $this->createdAt;
}
}

View file

@ -0,0 +1,79 @@
<?php
namespace Buzz\Util;
use Buzz\Message\MessageInterface;
use Buzz\Message\RequestInterface;
class CookieJar
{
protected $cookies = array();
public function setCookies($cookies)
{
$this->cookies = array();
foreach ($cookies as $cookie) {
$this->addCookie($cookie);
}
}
public function getCookies()
{
return $this->cookies;
}
/**
* Adds a cookie to the current cookie jar.
*
* @param Cookie $cookie A cookie object
*/
public function addCookie(Cookie $cookie)
{
$this->cookies[] = $cookie;
}
/**
* Adds Cookie headers to the supplied request.
*
* @param RequestInterface $request A request object
*/
public function addCookieHeaders(RequestInterface $request)
{
foreach ($this->cookies as $cookie) {
if ($cookie->matchesRequest($request)) {
$request->addHeader($cookie->toCookieHeader());
}
}
}
/**
* Processes Set-Cookie headers from a request/response pair.
*
* @param RequestInterface $request A request object
* @param MessageInterface $response A response object
*/
public function processSetCookieHeaders(RequestInterface $request, MessageInterface $response)
{
foreach ($response->getHeader('Set-Cookie', false) as $header) {
$cookie = new Cookie();
$cookie->fromSetCookieHeader($header, parse_url($request->getHost(), PHP_URL_HOST));
$this->addCookie($cookie);
}
}
/**
* Removes expired cookies.
*/
public function clearExpiredCookies()
{
foreach ($this->cookies as $i => $cookie) {
if ($cookie->isExpired()) {
unset($this->cookies[$i]);
}
}
// reset array keys
$this->cookies = array_values($this->cookies);
}
}

View file

@ -0,0 +1,190 @@
<?php
namespace Buzz\Util;
use Buzz\Message\RequestInterface;
use Buzz\Exception\InvalidArgumentException;
class Url
{
private static $defaultPorts = array(
'http' => 80,
'https' => 443,
);
private $url;
private $components;
/**
* Constructor.
*
* @param string $url The URL
*
* @throws InvalidArgumentException If the URL is invalid
*/
public function __construct($url)
{
$components = parse_url($url);
if (false === $components) {
throw new InvalidArgumentException(sprintf('The URL "%s" is invalid.', $url));
}
// support scheme-less URLs
if (!isset($components['host']) && isset($components['path'])) {
$pos = strpos($components['path'], '/');
if (false === $pos) {
$components['host'] = $components['path'];
unset($components['path']);
} elseif (0 !== $pos) {
list($host, $path) = explode('/', $components['path'], 2);
$components['host'] = $host;
$components['path'] = '/'.$path;
}
}
// default port
if (isset($components['scheme']) && !isset($components['port']) && isset(self::$defaultPorts[$components['scheme']])) {
$components['port'] = self::$defaultPorts[$components['scheme']];
}
$this->url = $url;
$this->components = $components;
}
public function getScheme()
{
return $this->parseUrl('scheme');
}
public function getHostname()
{
return $this->parseUrl('host');
}
public function getPort()
{
return $this->parseUrl('port');
}
public function getUser()
{
return $this->parseUrl('user');
}
public function getPassword()
{
return $this->parseUrl('pass');
}
public function getPath()
{
return $this->parseUrl('path');
}
public function getQueryString()
{
return $this->parseUrl('query');
}
public function getFragment()
{
return $this->parseUrl('fragment');
}
/**
* Returns a host string that combines scheme, hostname and port.
*
* @return string A host value for an HTTP message
*/
public function getHost()
{
if ($hostname = $this->parseUrl('host')) {
$host = $scheme = $this->parseUrl('scheme', 'http');
$host .= '://';
$host .= $hostname;
$port = $this->parseUrl('port');
if ($port && (!isset(self::$defaultPorts[$scheme]) || self::$defaultPorts[$scheme] != $port)) {
$host .= ':'.$port;
}
return $host;
}
}
/**
* Returns a resource string that combines path and query string.
*
* @return string A resource value for an HTTP message
*/
public function getResource()
{
$resource = $this->parseUrl('path', '/');
if ($query = $this->parseUrl('query')) {
$resource .= '?'.$query;
}
return $resource;
}
/**
* Returns a formatted URL.
*/
public function format($pattern)
{
static $map = array(
's' => 'getScheme',
'u' => 'getUser',
'a' => 'getPassword',
'h' => 'getHostname',
'o' => 'getPort',
'p' => 'getPath',
'q' => 'getQueryString',
'f' => 'getFragment',
'H' => 'getHost',
'R' => 'getResource',
);
$url = '';
$parts = str_split($pattern);
while ($part = current($parts)) {
if (isset($map[$part])) {
$method = $map[$part];
$url .= $this->$method();
} elseif ('\\' == $part) {
$url .= next($parts);
} elseif (!ctype_alpha($part)) {
$url .= $part;
} else {
throw new InvalidArgumentException(sprintf('The format character "%s" is invalid.', $part));
}
next($parts);
}
return $url;
}
/**
* Applies the current URL to the supplied request.
*/
public function applyToRequest(RequestInterface $request)
{
$request->setResource($this->getResource());
$request->setHost($this->getHost());
}
private function parseUrl($component = null, $default = null)
{
if (null === $component) {
return $this->components;
} elseif (isset($this->components[$component])) {
return $this->components[$component];
} else {
return $default;
}
}
}