1 <?php
2 /**
3 * Session.php
4 *
5 * @author Elvyrra SAS
6 * @license http://rem.mit-license.org/ MIT
7 */
8
9 namespace Hawk;
10
11 /**
12 * This trait is used to manage the user's sessions. It's declared as trait
13 * to be used in custom classes developer could want to develop to extend
14 * the session management (manage licences, number of simultaneous connections, ...)
15 *
16 * @package Core
17 */
18 class Session extends Singleton{
19 /**
20 * Static variable that registers the logged state of the current user
21 *
22 * @var boolean
23 */
24 private $logged,
25
26 /**
27 * Static variable that registers the current user of the session
28 *
29 * @var User
30 */
31 $user,
32
33
34 /**
35 * The session data, stored in $_SESSION
36 *
37 * @var array
38 */
39 $data = array();
40
41
42 /**
43 * The session instance
44 *
45 * @var array
46 */
47 protected static $instance;
48
49
50 /**
51 * Initialize the session user
52 */
53 public function init() {
54 if (!$this->getData('user.id')) {
55 $this->setData('user.id', 0);
56 }
57
58 if (App::conf()->has('db')) {
59 // Get the user from the database
60 $this->user = User::getById($this->getData('user.id'));
61 }
62 else {
63 // The database does not exists yet. Create a 'fake' guest user
64 $this->user = new User(
65 array(
66 'id' => User::GUEST_USER_ID,
67 'username' => 'guest',
68 'active' => 0,
69 )
70 );
71
72 $this->logged = false;
73 }
74
75 $this->logged = $this->user->isLogged();
76 }
77
78 /**
79 * Get the user of the current session
80 *
81 * @return User the user of the current session
82 */
83 public function getUser() {
84 return $this->user;
85 }
86
87 /**
88 * Returns if the current user is logged or not
89 *
90 * @return bool true if the user is logged, else returns false
91 */
92 public function isLogged() {
93 return $this->logged;
94 }
95
96
97 /**
98 * Check if the user is allowed to make action
99 *
100 * @param string $action The name of the permission to check
101 *
102 * @return boolean True if the user is allowed to perform this action, False in other case
103 */
104 public function isAllowed($action) {
105 return $this->getUser()->isAllowed($action);
106 }
107
108
109 /**
110 * Set data in session
111 *
112 * @param array $name The variable name to set in the session.
113 * To set a parameter in a sub array, type 'index1.index2'.
114 * For example, to set the parameter $data['user']['name'], type 'user.name'
115 * @param mixed $value The value to set
116 */
117 public function setData($name, $value) {
118 $fields = explode('.', $name);
119 $tmp = &$this->data;
120 foreach($fields as $field){
121 $tmp = &$tmp[$field];
122 }
123 $tmp = $value;
124
125 $_SESSION = $this->data;
126 }
127
128 /**
129 * Get session data
130 *
131 * @param string $name The variable name to get in session. For a multidimentionnal data, write 'level1.level2'
132 */
133 public function getData($name = null) {
134 $this->data = $_SESSION;
135
136 if (!isset($this->data)) {
137 return null;
138 }
139
140 if (!$name) {
141 return $this->data;
142 }
143 else {
144 $fields = explode('.', $name);
145 $tmp = $this->data;
146 foreach($fields as $field){
147 if (isset($tmp[$field])) {
148 $tmp = $tmp[$field];
149 }
150 else {
151 return null;
152 }
153 }
154 return $tmp;
155 }
156 }
157 }
158
159