Session::start()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 1
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Session
5
 *
6
 * Manage PHP sessions.
7
 *
8
 * @package core
9
 * @author [email protected]
10
 * @copyright Caffeina srl - 2015 - http://caffeina.com
11
 */
12
13
class Session {
14
    use Module, Events;
15
16
	/**
17
	 * Start session handler
18
	 *
19
	 * @access public
20
	 * @static
21
	 * @return void
22
	 */
23
	static public function start($name=null){
24
		if (isset($_SESSION)) return;
25
		$ln = static::name($name);
26
    // Obfuscate IDs
27
    ini_set('session.hash_function', 'whirlpool');
28
		session_cache_limiter('must-revalidate');
29
    if (session_status() == PHP_SESSION_NONE) {
30
		  @session_start();
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
31
    }
32
    static::trigger("start", $name?:$ln);
33
	}
34
35
   /**
36
   * Get/Set Session's cookie params
37
   *
38
   * @access public
39
   * @static
40
   * @param array $args Array of cookie's parameters
41
   * @see http://php.net/manual/en/function.session-set-cookie-params.php
42
   * @example $args = [
43
   *            "lifetime"  => 600,
44
   *            "path"      => "/",
45
   *            "domain"    => ".caffeina.com",
46
   *            "secure"    => true,
47
   *            "httponly"  => false
48
   *          ];
49
   *
50
   * @return array The cookie's parameters
51
   * @see http://php.net/manual/en/function.session-get-cookie-params.php
52
   */
53
  static public function cookieParams($args = []) {
54
    if (empty($args)) return session_get_cookie_params();
55
    $args = array_merge(session_get_cookie_params(), $args);
56
    session_set_cookie_params($args["lifetime"], $args["path"], $args["domain"], $args["secure"], $args["httponly"]);
57
    return $args;
58
  }
59
60
	/**
61
	 * Get/Set Session name
62
	 *
63
	 * @access public
64
	 * @static
65
	 * @param string $key The session name
66
	 * @return string The session value
67
	 */
68
	static public function name($name=null){
69
		return $name ? session_name($name) : session_name();
70
	}
71
72
	/**
73
	 * Get a session variable reference
74
	 *
75
	 * @access public
76
	 * @static
77
	 * @param mixed $key The variable name
78
	 * @return mixed The variable value
79
	 */
80
	static public function get($key,$default=null){
81
                if (($active = static::active()) && isset($_SESSION[$key])) {
82
			return $_SESSION[$key];
83
                } else if ($active) {
84
                	return $_SESSION[$key] = (is_callable($default)?call_user_func($default):$default);
85
                } else {
86
                 	return (is_callable($default)?call_user_func($default):$default);
87
                }
88
	}
89
90
	/**
91
	 * Set a session variable
92
	 *
93
	 * @access public
94
	 * @static
95
	 * @param mixed $key The variable name
96
	 * @param mixed $value The variable value
97
	 * @return void
98
	 */
99
	static public function set($key,$value=null){
100
		static::start();
101
		if($value==null && is_array($key)){
102
			foreach($key as $k=>$v) $_SESSION[$k]=$v;
103
		} else {
104
			$_SESSION[$key] = $value;
105
		}
106
	}
107
108
	/**
109
	 * Delete a session variable
110
	 *
111
	 * @access public
112
	 * @static
113
	 * @param mixed $key The variable name
114
	 * @return void
115
	 */
116
	static public function delete($key){
117
		static::start();
118
		unset($_SESSION[$key]);
119
	}
120
121
122
	/**
123
	 * Delete all session variables
124
	 *
125
	 * @access public
126
	 * @static
127
	 * @return void
128
	 */
129
	static public function clear(){
130
		static::start();
131
		session_unset();
132
		session_destroy();
133
    static::trigger("end");
134
	}
135
136
	/**
137
	 * Check if session is active
138
	 *
139
	 * @access public
140
	 * @static
141
	 * @return void
142
	 */
143
	static public function active(){
144
		return session_status() == PHP_SESSION_ACTIVE;
145
	}
146
147
	/**
148
	 * Check if a session variable exists
149
	 *
150
	 * @access public
151
	 * @static
152
	 * @param mixed $key The variable name
153
	 * @return bool
154
	 */
155
	static public function exists($key){
156
		static::start();
157
		return isset($_SESSION[$key]);
158
	}
159
160
	/**
161
	 * Return a read-only accessor to session variables for in-view use.
162
	 * @return SessionReadOnly
163
	 */
164
	static public function readOnly(){
165
		return new SessionReadOnly;
166
	}
167
168
}  /* End of class */
169
170
171
172
/**
173
 * Read-only Session accessor class
174
 */
175
176
class SessionReadOnly {
177
178
	/**
179
	 * Get a session variable reference
180
	 *
181
	 * @access public
182
	 * @param mixed $key The variable name
183
	 * @return mixed The variable value
184
	 */
185
	public function get($key){
186
		return Session::get($key);
187
	}
188
	public function __get($key){
189
		return Session::get($key);
190
	}
191
192
	public function name(){
193
		return Session::name();
194
	}
195
196
	/**
197
	 * Check if a session variable exists
198
	 *
199
	 * @access public
200
	 * @param mixed $key The variable name
201
	 * @return bool
202
	 */
203
	public function exists($key){
204
		return Session::exists($key);
205
	}
206
	public function __isset($key){
207
		return Session::exists($key);
208
	}
209
210
}  /* End of class */
211