Twitter Delicious Facebook Digg Stumbleupon Favorites More

Thứ Bảy

Source for file cookies.php

  1. <?php
  2. /**
  3.  *  Base include file for SimpleTest
  4.  *  @package    SimpleTest
  5.  *  @subpackage WebTester
  6.  *  @version    $Id: cookies.php 1784 2008-04-26 13:07:14Z pp11 $
  7.  */
  8.  
  9. /**#@+
  10.  *  include other SimpleTest class files
  11.  */
  12. require_once(dirname(__FILE__'/url.php');
  13. /**#@-*/
  14.  
  15. /**
  16.  *    Cookie data holder. Cookie rules are full of pretty
  17.  *    arbitary stuff. I have used...
  18.  *    http://wp.netscape.com/newsref/std/cookie_spec.html
  19.  *    http://www.cookiecentral.com/faq/
  20.  *    @package SimpleTest
  21.  *    @subpackage WebTester
  22.  */
  23. class SimpleCookie {
  24.     private $host;
  25.     private $name;
  26.     private $value;
  27.     private $path;
  28.     private $expiry;
  29.     private $is_secure;
  30.     
  31.     /**
  32.      *    Constructor. Sets the stored values.
  33.      *    @param string $name            Cookie key.
  34.      *    @param string $value           Value of cookie.
  35.      *    @param string $path            Cookie path if not host wide.
  36.      *    @param string $expiry          Expiry date as string.
  37.      *    @param boolean $is_secure      Currently ignored.
  38.      */
  39.     function __construct($name$value false$path false$expiry false$is_secure false{
  40.         $this->host false;
  41.         $this->name $name;
  42.         $this->value $value;
  43.         $this->path ($path $this->fixPath($path"/");
  44.         $this->expiry false;
  45.         if (is_string($expiry)) {
  46.             $this->expiry = strtotime($expiry);
  47.         elseif (is_integer($expiry)) {
  48.             $this->expiry $expiry;
  49.         }
  50.         $this->is_secure $is_secure;
  51.     }
  52.     
  53.     /**
  54.      *    Sets the host. The cookie rules determine
  55.      *    that the first two parts are taken for
  56.      *    certain TLDs and three for others. If the
  57.      *    new host does not match these rules then the
  58.      *    call will fail.
  59.      *    @param string $host       New hostname.
  60.      *    @return boolean           True if hostname is valid.
  61.      *    @access public
  62.      */
  63.     function setHost($host{
  64.         if ($host $this->truncateHost($host)) {
  65.             $this->host $host;
  66.             return true;
  67.         }
  68.         return false;
  69.     }
  70.     
  71.     /**
  72.      *    Accessor for the truncated host to which this
  73.      *    cookie applies.
  74.      *    @return string       Truncated hostname.
  75.      *    @access public
  76.      */
  77.     function getHost({
  78.         return $this->host;
  79.     }
  80.     
  81.     /**
  82.      *    Test for a cookie being valid for a host name.
  83.      *    @param string $host    Host to test against.
  84.      *    @return boolean        True if the cookie would be valid
  85.      *                            here.
  86.      */
  87.     function isValidHost($host{
  88.         return ($this->truncateHost($host=== $this->getHost());
  89.     }
  90.     
  91.     /**
  92.      *    Extracts just the domain part that determines a
  93.      *    cookie's host validity.
  94.      *    @param string $host    Host name to truncate.
  95.      *    @return string        Domain or false on a bad host.
  96.      *    @access private
  97.      */
  98.     protected function truncateHost($host{
  99.         $tlds SimpleUrl::getAllTopLevelDomains();
  100.         if (preg_match('/[a-z\-]+\.(' $tlds ')$/i'$host$matches)) {
  101.             return $matches[0];
  102.         elseif (preg_match('/[a-z\-]+\.[a-z\-]+\.[a-z\-]+$/i'$host$matches)) {
  103.             return $matches[0];
  104.         }
  105.         return false;
  106.     }
  107.     
  108.     /**
  109.      *    Accessor for name.
  110.      *    @return string       Cookie key.
  111.      *    @access public
  112.      */
  113.     function getName({
  114.         return $this->name;
  115.     }
  116.     
  117.     /**
  118.      *    Accessor for value. A deleted cookie will
  119.      *    have an empty string for this.
  120.      *    @return string       Cookie value.
  121.      *    @access public
  122.      */
  123.     function getValue({
  124.         return $this->value;
  125.     }
  126.     
  127.     /**
  128.      *    Accessor for path.
  129.      *    @return string       Valid cookie path.
  130.      *    @access public
  131.      */
  132.     function getPath({
  133.         return $this->path;
  134.     }
  135.     
  136.     /**
  137.      *    Tests a path to see if the cookie applies
  138.      *    there. The test path must be longer or
  139.      *    equal to the cookie path.
  140.      *    @param string $path       Path to test against.
  141.      *    @return boolean           True if cookie valid here.
  142.      *    @access public
  143.      */
  144.     function isValidPath($path{
  145.         return (strncmp(
  146.                 $this->fixPath($path),
  147.                 $this->getPath(),
  148.                 strlen($this->getPath())) == 0);
  149.     }
  150.     
  151.     /**
  152.      *    Accessor for expiry.
  153.      *    @return string       Expiry string.
  154.      *    @access public
  155.      */
  156.     function getExpiry({
  157.         if ($this->expiry{
  158.             return false;
  159.         }
  160.         return gmdate("D, d M Y H:i:s"$this->expiry" GMT";
  161.     }
  162.     
  163.     /**
  164.      *    Test to see if cookie is expired against
  165.      *    the cookie format time or timestamp.
  166.      *    Will give true for a session cookie.
  167.      *    @param integer/string $now  Time to test against. Result
  168.      *                                 will be false if this time
  169.      *                                 is later than the cookie expiry.
  170.      *                                 Can be either a timestamp integer
  171.      *                                 or a cookie format date.
  172.      *    @access public
  173.      */
  174.     function isExpired($now{
  175.         if ($this->expiry{
  176.             return true;
  177.         }
  178.         if (is_string($now)) {
  179.             $now = strtotime($now);
  180.         }
  181.         return ($this->expiry $now);
  182.     }
  183.     
  184.     /**
  185.      *    Ages the cookie by the specified number of
  186.      *    seconds.
  187.      *    @param integer $interval   In seconds.
  188.      *    @public
  189.      */
  190.     function agePrematurely($interval{
  191.         if ($this->expiry{
  192.             $this->expiry -= $interval;
  193.         }
  194.     }
  195.     
  196.     /**
  197.      *    Accessor for the secure flag.
  198.      *    @return boolean       True if cookie needs SSL.
  199.      *    @access public
  200.      */
  201.     function isSecure({
  202.         return $this->is_secure;
  203.     }
  204.     
  205.     /**
  206.      *    Adds a trailing and leading slash to the path
  207.      *    if missing.
  208.      *    @param string $path            Path to fix.
  209.      *    @access private
  210.      */
  211.     protected function fixPath($path{
  212.         if (substr($path01!= '/'{
  213.             $path '/' $path;
  214.         }
  215.         if (substr($path-11!= '/'{
  216.             $path .= '/';
  217.         }
  218.         return $path;
  219.     }
  220. }
  221.  
  222. /**
  223.  *    Repository for cookies. This stuff is a
  224.  *    tiny bit browser dependent.
  225.  *    @package SimpleTest
  226.  *    @subpackage WebTester
  227.  */
  228. class SimpleCookieJar {
  229.     private $cookies;
  230.     
  231.     /**
  232.      *    Constructor. Jar starts empty.
  233.      *    @access public
  234.      */
  235.     function __construct({
  236.         $this->cookies array();
  237.     }
  238.     
  239.     /**
  240.      *    Removes expired and temporary cookies as if
  241.      *    the browser was closed and re-opened.
  242.      *    @param string/integer $now   Time to test expiry against.
  243.      *    @access public
  244.      */
  245.     function restartSession($date false{
  246.         $surviving_cookies array();
  247.         for ($i 0$i < count($this->cookies)$i++{
  248.             if ($this->cookies[$i]->getValue()) {
  249.                 continue;
  250.             }
  251.             if ($this->cookies[$i]->getExpiry()) {
  252.                 continue;
  253.             }
  254.             if ($date && $this->cookies[$i]->isExpired($date)) {
  255.                 continue;
  256.             }
  257.             $surviving_cookies[$this->cookies[$i];
  258.         }
  259.         $this->cookies $surviving_cookies;
  260.     }
  261.     
  262.     /**
  263.      *    Ages all cookies in the cookie jar.
  264.      *    @param integer $interval     The old session is moved
  265.      *                                  into the past by this number
  266.      *                                  of seconds. Cookies now over
  267.      *                                  age will be removed.
  268.      *    @access public
  269.      */
  270.     function agePrematurely($interval{
  271.         for ($i 0$i < count($this->cookies)$i++{
  272.             $this->cookies[$i]->agePrematurely($interval);
  273.         }
  274.     }
  275.     
  276.     /**
  277.      *    Sets an additional cookie. If a cookie has
  278.      *    the same name and path it is replaced.
  279.      *    @param string $name       Cookie key.
  280.      *    @param string $value      Value of cookie.
  281.      *    @param string $host       Host upon which the cookie is valid.
  282.      *    @param string $path       Cookie path if not host wide.
  283.      *    @param string $expiry     Expiry date.
  284.      *    @access public
  285.      */
  286.     function setCookie($name$value$host false$path '/'$expiry false{
  287.         $cookie new SimpleCookie($name$value$path$expiry);
  288.         if ($host{
  289.             $cookie->setHost($host);
  290.         }
  291.         $this->cookies[$this->findFirstMatch($cookie)$cookie;
  292.     }
  293.     
  294.     /**
  295.      *    Finds a matching cookie to write over or the
  296.      *    first empty slot if none.
  297.      *    @param SimpleCookie $cookie    Cookie to write into jar.
  298.      *    @return integer                Available slot.
  299.      *    @access private
  300.      */
  301.     protected function findFirstMatch($cookie{
  302.         for ($i 0$i < count($this->cookies)$i++{
  303.             $is_match $this->isMatch(
  304.                     $cookie,
  305.                     $this->cookies[$i]->getHost(),
  306.                     $this->cookies[$i]->getPath(),
  307.                     $this->cookies[$i]->getName());
  308.             if ($is_match{
  309.                 return $i;
  310.             }
  311.         }
  312.         return count($this->cookies);
  313.     }
  314.     
  315.     /**
  316.      *    Reads the most specific cookie value from the
  317.      *    browser cookies. Looks for the longest path that
  318.      *    matches.
  319.      *    @param string $host        Host to search.
  320.      *    @param string $path        Applicable path.
  321.      *    @param string $name        Name of cookie to read.
  322.      *    @return string             False if not present, else the
  323.      *                                value as a string.
  324.      *    @access public
  325.      */
  326.     function getCookieValue($host$path$name{
  327.         $longest_path '';
  328.         foreach ($this->cookies as $cookie{
  329.             if ($this->isMatch($cookie$host$path$name)) {
  330.                 if (strlen($cookie->getPath()) > strlen($longest_path)) {
  331.                     $value $cookie->getValue();
  332.                     $longest_path $cookie->getPath();
  333.                 }
  334.             }
  335.         }
  336.         return (isset($value$value false);
  337.     }
  338.     
  339.     /**
  340.      *    Tests cookie for matching against search
  341.      *    criteria.
  342.      *    @param SimpleTest $cookie    Cookie to test.
  343.      *    @param string $host          Host must match.
  344.      *    @param string $path          Cookie path must be shorter than
  345.      *                                  this path.
  346.      *    @param string $name          Name must match.
  347.      *    @return boolean              True if matched.
  348.      *    @access private
  349.      */
  350.     protected function isMatch($cookie$host$path$name{
  351.         if ($cookie->getName(!= $name{
  352.             return false;
  353.         }
  354.         if ($host && $cookie->getHost(&& $cookie->isValidHost($host)) {
  355.             return false;
  356.         }
  357.         if ($cookie->isValidPath($path)) {
  358.             return false;
  359.         }
  360.         return true;
  361.     }
  362.     
  363.     /**
  364.      *    Uses a URL to sift relevant cookies by host and
  365.      *    path. Results are list of strings of form "name=value".
  366.      *    @param SimpleUrl $url       Url to select by.
  367.      *    @return array               Valid name and value pairs.
  368.      *    @access public
  369.      */
  370.     function selectAsPairs($url{
  371.         $pairs array();
  372.         foreach ($this->cookies as $cookie{
  373.             if ($this->isMatch($cookie$url->getHost()$url->getPath()$cookie->getName())) {
  374.                 $pairs[$cookie->getName('=' $cookie->getValue();
  375.             }
  376.         }
  377.         return $pairs;
  378.     }
  379. }
  380. ?>

0 nhận xét:

Đăng nhận xét

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Blogger Templates