いろいろ準備中

[Zend Framework][Zend_Auth][cookie]Zend_Auth_Storage_Interface を実装してcookieに対応

6月 23rd, 2008 Posted in php, Zend Framework

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);
        }
    }
}
  1. 6 Responses to “[Zend Framework][Zend_Auth][cookie]Zend_Auth_Storage_Interface を実装してcookieに対応”

  2. By heavenshell on 6月 24, 2008

    おお。これは素晴らしい。CodeReposにコミットされるのを楽しみにしてます。

    1点だけ・・クラスのプリフィックスが Zend_ となってますが、
    規約で Zend は Zend 社以外は禁止とあったので、他の名前でコミット頂ければ・・。
    差し出がましいですが、ご検討下さい
    http://framework.zend.com/manual/ja/coding-standard.naming-conventions.html

  3. By wads on 6月 24, 2008

    heavenshellさんご指摘ありがとうございます。
    恥ずかしながら、きちんと規約を読んでいませんでした orz

    早速修正させていただきました。ありがとうございました。

  4. By Sniper on 8月 26, 2008

    Thank you, this storage is what i need. Only one correction – on line 157, you are using $contents instead of $value. Small mistake, bad throw unfriendly error.

  5. By hyd on 10月 4, 2008

    素晴らしいですね.
    いつか作ろうと思いつつもゴリゴリ書いていたりしたので,是非とも使わせていただきたいと思います.

    ところで,このソースのライセンスってどうなっているのでしょうか.

  6. By hyd on 10月 4, 2008

    バグ報告を.
    237-239と254-256で同一のメソッドgetExpire()が定義されているようです.

  7. By wads on 10月 5, 2008

    hydさんこんにちは。ご連絡ありがとうございました。

    ライセンスはZend Frameworkと同じものとします。ソースへ記述しました。

    また、バグも修正いたしました。修正は例のごとくCodeReposへ反映させてあります。

コメントを投稿する