Sensei.class.php 6.8 KB
Newer Older
doctrine's avatar
doctrine committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
<?php
class Sensei_Group extends Doctrine_Record { }
class Sensei_Company extends Sensei_Group { }
class Sensei_User extends Doctrine_Record { }
class Sensei_Customer extends Sensei_User { }
class Sensei_Entity extends Doctrine_Record {
    /**
     * setTableDefinition
     * initializes column definitions
     *
     * @return void
     */
    public function setTableDefinition() {
        $this->hasColumn("loginname","string",32,"unique");
        $this->hasColumn("password","string",32);
    }
}
class Sensei_Variable extends Doctrine_Record {
    /**
     * setUp
     * initializes relations and options
     *
     * @return void
     */
    public function setUp() {
        //$this->setAttribute(Doctrine::ATTR_COLL_KEY, "name");
    }
    /**
     * setTableDefinition
     * initializes column definitions
     *
     * @return void
     */
    public function setTableDefinition() {
        $this->hasColumn("name","string",50,"unique");
        $this->hasColumn("value","string",10000);
        $this->hasColumn("session_id","integer");
    }
}
class Sensei_Session extends Doctrine_Record {
    /**
     * setUp
     * initializes relations and options
     *
     * @return void
     */
    public function setUp() {
        $this->ownsMany("Sensei_Variable","Sensei_Variable.session_id");
        $this->hasOne("Sensei_Entity","Sensei_Session.entity_id");
    }
    /**
     * setTableDefinition
     * initializes column definitions
     *
     * @return void
     */
    public function setTableDefinition() {
        $this->hasColumn("session_id","string",32);
        $this->hasColumn("logged_in","integer",1);
        $this->hasColumn("entity_id","integer");
        $this->hasColumn("user_agent","string",200);
        $this->hasColumn("updated","integer");
        $this->hasColumn("created","integer");
    }
}
class Sensei_Exception extends Exception { }
class Sensei extends Doctrine_Access {
    const ATTR_LIFESPAN = 0;
    /**
     * @var Sensei_Session $record
     */
    private $record;
    /**
     * @var Doctrine_Session $session
     */
    private $session;
    /**
     * @var Doctrine_Table $table
     */
    private $table;
    /**
     * @var array $attributes
     */
    private $attributes = array();
    /**
     * @var Doctrine_Collection $vars
     */
    private $vars;


    public function __construct() {
        if(headers_sent())
            throw new Sensei_Exception("Headers already sent. Couldn't initialize session.");

        $this->session = Doctrine_Manager::getInstance()->getCurrentSession();
        $this->table   = $this->session->getTable("Sensei_session");
        $this->init();


        $this->gc(1);

        if( ! isset($_SESSION))
            session_start();
            
    }
    /**
     * getRecord
     */
    public function getRecord() {
        return $this->record;
    }
    /**
     * init
     */
    private function init() {
        session_set_save_handler(
            array($this,"open"),
            array($this,"close"),
            array($this,"read"),
            array($this,"write"),
            array($this,"destroy"),
            array($this,"gc")
        );
    }
    /**
     * @param string $username
     * @param string $password
     * @return boolean
     */
    public function login($username,$password) {
        $coll = $this->session->query("FROM Sensei_Entity WHERE Sensei_Entity.loginname = ? && Sensei_Entity.password = ?",array($username,$password));
        if(count($coll) > 0) {
            $this->record->logged_in = 1;
            $this->record->entity_id = $coll[0]->getID();
            $this->record->save();
            return true;
        }
        return false;
    }
    /**
     * logout
     * @return boolean
     */
    public function logout() {
        if( $this->record->logged_in == true) {
            $this->record->logged_in = 0;
            $this->record->entity_id = 0;
            return true;
        } else {
            return false;
        }
    }
    /**
     * returns a session variable
     *
     * @param mixed $name
     * @return mixed
     */
    public function get($name) {
        foreach($this->vars as $var) {
            if($var->name == $name) {
                return $var->value;
            }
        }
    }
    /**
     * sets a session variable
     * returns true on success, false on failure
     *
     * @param mixed $name
     * @param mixed $value
     * @return boolean
     */
    public function set($name,$value) {
        foreach($this->vars as $var) {
            if($var->name == $name) {
                $var->value = $value;
                return true;
            }
        }
        return false;
    }
    /**
     * @param integer $attr
     * @param mixed $value
     */
    public function setAttribute($attr, $value) {
        switch($attr):
            case Sensei::ATTR_LIFESPAN:

            break;
            default:
                throw new Sensei_Exception("Unknown attribute");
        endswitch;
        
        $this->attributes[$attr] = $value;
    }
    /**
     * @return boolean
     */
    private function open($save_path,$session_name) {
        return true;
    }
    /**
     * @return boolean
     */
    public function close() {
        return true;
    }
    /**
     * always returns an empty string
     *
     * @param string $id        php session identifier
     * @return string
     */
    private function read($id) {
        $coll = $this->session->query("FROM Sensei_Session, Sensei_Session.Sensei_Variable WHERE Sensei_Session.session_id = ?",array($id));
        $this->record = $coll[0];
        $this->record->user_agent = $_SERVER['HTTP_USER_AGENT'];
        $this->record->updated    = time();
        $this->record->session_id = $id;
        $this->vars = $this->record->Sensei_Variable;

        if($this->record->getState() == Doctrine_Record::STATE_TDIRTY) {
            $this->record->created = time();
            $this->record->save();
        }
        return "";
    }
    /**
     * @return boolean
     */
    public function write($id,$sess_data) {
        return true;
    }
    /**
     * @param string $id            php session identifier
     * @return Doctrine_Record
     */
    private function destroy($id) {
        $this->record->delete();
        return $this->record;
    }
    /**
     * @param integer $maxlifetime
     */
    private function gc($maxlifetime) {
        return true;
    }
    /**
     * flush
     * makes all changes persistent
     */
    public function flush() {
        $this->record->save();
    }
    /**
     * destructor
     */
    public function __destruct() {
        $this->flush();
    }
}
?>