cookieで認証情報を引き回したかったので作ってみました。まだまだ改良の余地がありますが、とりあえず公開してしまいます。 本来はZend_Http_Cookieをうまく使いたかったのですが、Zene_Http_ClientやZend_Http_CookieJarから使われるのが前提のようなつくりだったのでうまく使えませんでした。折を見て改良していこうと思います。 そのうちCodeReposのコミット権をもらったらそっちにアップしますが、それまではここにソースを貼り付けておきます。 ・2008/06/24 追記 クラス名にZend_というプリフィックスは使えないとご指摘をいただいたので修正しました。名前はここ風にブログの管理者名にしています。 ・08/07/04 追記 CodeReposにアップしました。 ・08/08/26 追記 指摘されたミスを修正しました。CodeReposのほうが最新ですのでそちらを参照ください。 ・08/10/05 追記 ライセンスを明記しました。Zend Frameworkと同じです。 バグの修正しました。 以上修正はCodeReposへ反映しました。 ↓以下のソースは最新ではありませんので注意!

< ?php

require\_once 'Zend/Auth/Storage/Interface.php';

class Wads\_Auth\_Storage\_Cookie implements Zend\_Auth\_Storage\_Interface
{
    /\*\*
     \* Default cookie name
     \*/
    const COOKIENAME\_DEFAULT = 'Wads\_Auth\_Cookie';

    protected $\_name = "";

    protected $\_value = "";

    protected $\_expire = null;

    protected $\_path = "/";

    protected $\_domain = "";

    protected $\_secure = false;

    protected $\_httponly = false;

    public function \_\_construct($option = self::COOKIENAME\_DEFAULT) {
        if (is\_string($option)) {
            $this->setName($option);
        } else if (is\_array($option)) {
            $this->setCookieParams($option);
        } else if ($option instanceof Zend\_Config) {
            $this->setCookieParams($option);
        } else {
            $option = (string)$option;
        }
    }

    public function \_\_get($name) {
        $func = "get".ucfirst($name);

        if(method\_exists($this, $func)) {
            return $func();
        }
        return null;
    }

    public function \_\_set($name, $value) {
        $func = "get".ucfirst($name);

        if(method\_exists($this, $func)) {
            return $func($value);
        } else {
            require\_once 'Zend/Auth/Storage/Exception.php';
            throw new Zend\_Auth\_Storage\_Exception('Cannot set the this value .');
        }
    }

    public function \_\_toString(){
        return "{$this->\_name}={$this->\_value}";
    }

    /\*\*
     \* Defined by Zend\_Auth\_Storage\_Interface
     \*
     \* Returns true if and only if storage is empty
     \*
     \* @throws Zend\_Auth\_Storage\_Exception If it is impossible to determine whether storage is empty
     \* @return boolean
     \*/
    public function isEmpty() {
        return !isset($\_COOKIE\[$this->\_name\]);
    }

    /\*\*
     \* Defined by Zend\_Auth\_Storage\_Interface
     \*
     \* Returns the contents of storage
     \*
     \* Behavior is undefined when storage is empty.
     \*
     \* @throws Zend\_Auth\_Storage\_Exception If reading contents from storage is impossible
     \* @return mixed
     \*/
    public function read() {
        return $\_COOKIE\[$this->\_name\];
    }

    /\*\*
     \* Defined by Zend\_Auth\_Storage\_Interface
     \*
     \* Writes $contents to storage
     \*
     \* @param  mixed $contents
     \* @throws Zend\_Auth\_Storage\_Exception If writing $contents to storage is impossible
     \* @return void
     \*/
    public function write($contents) {
        if(headers\_sent()) {
            require\_once 'Zend/Auth/Storage/Exception.php';
            throw new Zend\_Auth\_Storage\_Exception('Cannot write Cookie because headers have already been sent.');
        }
        $this->setValue($contents);
        $this->\_setcookie($this->\_name, $this->\_value, $this->\_expire, $this->\_path, $this->\_domain, $this->\_secure, $this->\_httponly);
    }

    /\*\*
     \* Defined by Zend\_Auth\_Storage\_Interface
     \*
     \* Clears contents from storage
     \*
     \* @throws Zend\_Auth\_Storage\_Exception If clearing contents from storage is impossible
     \* @return void
     \*/
    public function clear() {
        $this->\_setcookie($this->\_name, "", time()-3600, $this->\_path, $this->\_domain, $this->\_secure, $this->\_httponly);
    }

    public function setCookieParams($params) {
        if ($params instanceof Zend\_Config) {
            $params = $params->toArray();
        } elseif (!is\_array($params)) {
            require\_once 'Zend/Auth/Storage/Exception.php';
            throw new Zend\_Auth\_Storage\_Exception('setCookieParams expects either an array or a Zend\_Config object .');
        }

        foreach($params as $key => $value) {
            $method = 'set' . ucfirst($key);
            if(method\_exists($this, $method)) {
                $this->$method($value);
            }
        }
    }

    public function setName($name) {
       if (!$name = (string)$name) {
            require\_once 'Zend/Auth/Storage/Exception.php';
            throw new Zend\_Auth\_Storage\_Exception('Cookies must have a name');
        }

        if (preg\_match("/\[=,; trn�13�14\]/", $name)) {
            require\_once 'Zend/Auth/Storage/Exception.php';
            throw new Zend\_Auth\_Storage\_Exception("Cookie name cannot contain these characters: =,; trn�13�14 ({$name})");
        }
        $this->\_name = $name;
    }

    public function getName() {
        return $this->\_name;
    }

    public function setValue($value) {
        if (is\_bool($value)) {
            $value = ($value) ? "1" : "0";
        } else if (!is\_string($value)) {
            $value = (string)$value;
        }
        $this->\_value = $value;
    }

    public function getValue() {
        return $this->\_value;
    }

    public function setExpire($expire) {
        if(!is\_numeric($expire) || $expire < 0) {
            require\_once 'Zend/Auth/Storage/Exception.php';
            throw new Zend\_Auth\_Storage\_Exception('You must provide a cookie expire param grater than or equal 0 .');
        }
        $this->\_expire = (int)$expire;
    }

    public function getExpiryTime() {
        return $this->\_expire;
    }
    protected function getExpire() {
        return $this->getExpiryTime();
    }

    public function isExpired($now = null) {
        if ($now === null) $now = time();
        if (is\_int($this->\_expires) && $this->\_expires < $now) {
            return true;
        } else {
            return false;
        }
    }

    public function setPath($path) {
        $this->\_path = $path;
    }

    public function getPath() {
        return $this->\_path;
    }

    public function setDomain($domain) {
        $this->\_domain = $domain;
    }

    public function getDomain() {
        return $this->\_domain;
    }

    public function setSecure($secure) {
        $this->\_secure = ($secure) ? true : false;
    }

    protected function getSecure() {
        return $this->isSecure();
    }

    public function isSecure() {
        return $this->\_secure;
    }

    public function setHttponly($httponly) {
        if (version\_compare(PHP\_VERSION, '5.2.0', '>=')) {
            $this->\_httponly = ($httponly) ? true : false;
        }
    }

    protected function getHttponly() {
        if (version\_compare(PHP\_VERSION, '5.2.0', '>=')) {
            return $this->isHttponly();
        }
    }

    public function isHttponly() {
        if (version\_compare(PHP\_VERSION, '5.2.0', '>=')) {
            return $this->\_httponly;
        }
    }

    protected final function \_setcookie($name, $value, $expire, $path, $domain, $secure, $httponly){
        if (version\_compare(PHP\_VERSION, '5.2.0', '>=')) {
           setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
        } else {
            setcookie($name, $value, $expire, $path, $domain, $secure);
        }
    }
}