root/trunk/src/main/php/net/stubbles/ipo/session/stubNoneDurableSession.php

Revision 1563, 4.4 kB (checked in by mikey, 2 months ago)

enhanced net::stubbles::ipo::session::stubNoneDurableSession to be more session-like

Line 
1 <?php
2 /**
3  * Session class that is not durable for more than one request.
4  *
5  * @author      Frank Kleine <mikey@stubbles.net>
6  * @package     stubbles
7  * @subpackage  ipo_session
8  */
9 stubClassLoader::load('net::stubbles::ipo::request::validator::stubRegexValidator',
10                       'net::stubbles::ipo::session::stubAbstractSession'
11 );
12 /**
13  * Session class that is not durable for more than one request.
14  *
15  * @package     stubbles
16  * @subpackage  ipo_session
17  */
18 class stubNoneDurableSession extends stubAbstractSession
19 {
20     /**
21      * the session id
22      *
23      * @var  int
24      */
25     protected $id;
26     /**
27      * the data
28      *
29      * @var  array
30      */
31     protected $data        = array();
32     /**
33      * the response instance
34      *
35      * @var  stubResponse
36      */
37     protected $response;
38     /**
39      * regular expression to validate the session id
40      */
41     const REGEX_SESSION_ID = '/^([a-zA-Z0-9]{32})$/D';
42
43     /**
44      * template method for child classes to do the real construction
45      *
46      * @param   stubRequest   $request      request instance
47      * @param   stubResponse  $response     response instance
48      * @param   string        $sessionName  name of the session
49      * @return  bool
50      */
51     protected function doConstruct(stubRequest $request, stubResponse $response, $sessionName)
52     {
53         $this->response = $response;
54         if ($request->hasValue($sessionName) === true) {
55             $this->id = $request->getValidatedValue(new stubRegexValidator(self::REGEX_SESSION_ID), $sessionName);
56             $this->data = array(stubSession::START_TIME  => time(),
57                                 stubSession::FINGERPRINT => '',
58                                 stubSession::NEXT_TOKEN  => ''
59                           );
60         } elseif ($request->hasValue($sessionName, stubRequest::SOURCE_COOKIE) === true) {
61             $this->id = $request->getValidatedValue(new stubRegexValidator(self::REGEX_SESSION_ID), $sessionName, stubRequest::SOURCE_COOKIE);
62             $this->data = array(stubSession::START_TIME  => time(),
63                                 stubSession::FINGERPRINT => '',
64                                 stubSession::NEXT_TOKEN  => ''
65                           );
66         } else {
67             $this->id = md5(uniqid(rand(), true));
68         }
69         
70         $this->response->setCookie(stubCookie::create($sessionName, $this->id)->forPath('/'));
71         return true;
72     }
73
74     /**
75      * returns fingerprint for user: has to use same user agent all over the session
76      *
77      * @return  string
78      */
79     protected function getFingerprint()
80     {
81         return '';
82     }
83
84     /**
85      * returns session id
86      *
87      * @return  string  the session id
88      */
89     public function getId()
90     {
91         return $this->id;
92     }
93
94     /**
95      * regenerates the session id but leaves session data
96      */
97     public function regenerateId()
98     {
99         $this->id = md5(uniqid(rand(), true));
100         $this->response->setCookie(stubCookie::create($this->sessionName, $this->id)->forPath('/'));
101     }
102
103     /**
104      * invalidates current session and creates a new one
105      */
106     public function invalidate()
107     {
108         $this->data = array();
109     }
110
111     /**
112      * stores a value associated with the key
113      *
114      * @param  string  $key    key to store value under
115      * @param  mixed   $value  data to store
116      */
117     protected function doPutValue($key, $value)
118     {
119         $this->data[$key] = $value;
120     }
121
122     /**
123      * returns a value associated with the key or the default value
124      *
125      * @param   string  $key  key where value is stored under
126      * @return  mixed
127      */
128     protected function doGetValue($key)
129     {
130         return $this->data[$key];
131     }
132
133     /**
134      * checks whether a value associated with key exists
135      *
136      * @param   string  $key  key where value is stored under
137      * @return  bool
138      */
139     public function hasValue($key)
140     {
141         return isset($this->data[$key]);
142     }
143
144     /**
145      * removes a value from the session
146      *
147      * @param   string  $key  key where value is stored under
148      * @return  bool    true if value existed and was removed, else false
149      */
150     protected function doRemoveValue($key)
151     {
152         unset($this->data[$key]);
153         return true;
154     }
155
156     /**
157      * return an array of all keys registered in this session
158      *
159      * @return  array<string>
160      */
161     protected function doGetValueKeys()
162     {
163         return array_keys($this->data);
164     }
165 }
166 ?>
Note: See TracBrowser for help on using the browser.