1 | 2 | simandl | <?php |
2 | | | |
3 | | | namespace Phem\Environment; |
4 | | | |
5 | | | use Phem\Core\Object; |
6 | | | |
7 | | | /** |
8 | | | * Class representing the session |
9 | | | * |
10 | | | * @author Jakub PetrĹžĂlka <petrzilka@czweb.net> |
11 | | | */ |
12 | | | class Session extends Object |
13 | | | { |
14 | | | |
15 | | | private $writeLock = false; |
16 | | | |
17 | | | public function init() |
18 | | | { |
19 | | | /* Start session if not started already (nested application) */ |
20 | | | if (session_status() != PHP_SESSION_ACTIVE) |
21 | | | { |
22 | | | session_start(); |
23 | | | } |
24 | | | } |
25 | | | |
26 | | | public function isWriteLocked() |
27 | | | { |
28 | | | return $this->writeLock; |
29 | | | } |
30 | | | |
31 | | | /** |
32 | | | * Returns a variable from session array |
33 | | | * |
34 | | | * @param string $name Name of the variable |
35 | | | * @param mixed $defaultValue The default value which is used if session variable with given name is not set |
36 | | | * @return mixed Value of the variable |
37 | | | */ |
38 | | | function getVar($name, $defaultValue = null) |
39 | | | { |
40 | | | if (isset($_SESSION[$name])) |
41 | | | return $_SESSION[$name]; |
42 | | | else |
43 | | | return $defaultValue; |
44 | | | } |
45 | | | |
46 | | | /** |
47 | | | * Sets the session variable to the given value |
48 | | | * |
49 | | | * @param string $name Name of the variable |
50 | | | * @param mixed $value The value |
51 | | | */ |
52 | | | function setVar($name, $value) |
53 | | | { |
54 | | | if (!$this->writeLock) |
55 | | | $_SESSION[$name] = $value; |
56 | | | else |
57 | | | throw new SessionException('Trying to write session variable ' . $name . ' after writing closed'); |
58 | | | } |
59 | | | |
60 | | | /** |
61 | | | * Destroys all data registered in session |
62 | | | */ |
63 | | | function destroy() |
64 | | | { |
65 | | | session_destroy(); |
66 | | | } |
67 | | | |
68 | | | function getId() |
69 | | | { |
70 | | | return session_id(); |
71 | | | } |
72 | | | |
73 | | | function writeClose() |
74 | | | { |
75 | | | $this->writeLock = true; |
76 | | | session_write_close(); |
77 | | | } |
78 | | | |
79 | | | function reopen() |
80 | | | { |
81 | | | ini_set('session.use_only_cookies', false); |
82 | | | ini_set('session.use_cookies', false); |
83 | | | ini_set('session.use_trans_sid', false); |
84 | | | ini_set('session.cache_limiter', null); |
85 | | | session_start(); |
86 | | | $this->writeLock = false; |
87 | | | } |
88 | | | |
89 | | | } |