Native_session::set_flashdata()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 6
nop 2
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
if (!defined('BASEPATH')) {
4
    exit('No direct script access allowed');
5
}
6
/**
7
 * Code Igniter
8
 *
9
 * An open source application development framework for PHP 4.3.2 or newer
10
 *
11
 * @package        CodeIgniter
12
 * @author        Dariusz Debowczyk
13
 * @copyright    Copyright (c) 2006, D.Debowczyk
14
 * @license        http://www.codeignitor.com/user_guide/license.html
15
 * @link        http://www.codeigniter.com
16
 * @since        Version 1.0
17
 * @filesource
18
 */
19
// ------------------------------------------------------------------------
20
21
/**
22
 * Session class using native PHP session features and hardened against session fixation.
23
 *
24
 * @package        CodeIgniter
25
 * @subpackage    Libraries
26
 * @category    Sessions
27
 * @author        Dariusz Debowczyk
28
 * @link        http://www.codeigniter.com/user_guide/libraries/sessions.html
29
 */
30
class Native_session
31
{
32
33
    var $session_id_ttl = 7200; // session id time to live (TTL) in seconds
34
35
    var $flash_key = 'flash'; // prefix for "flash" variables (eg. flash:new:message)
36
37
    public function __construct() {
38
39
        log_message('debug', 'Native_session Class Initialized');
40
41
        $CI = & get_instance();
42
        $this->session_id_ttl = $CI->config->item('sess_expiration');
43
44
        $this->_sess_run();
45
    }
46
47
    /**
48
     * Regenerates session id
49
     */
50
    public function regenerate_id() {
51
52
        // copy old session data, including its id
53
        $old_session_id = session_id();
54
        $old_session_data = $_SESSION;
55
56
        // regenerate session id and store it
57
        session_regenerate_id();
58
        $new_session_id = session_id();
59
60
        // switch to the old session and destroy its storage
61
        session_id($old_session_id);
62
        session_destroy();
63
64
        // switch back to the new session id and send the cookie
65
        session_id($new_session_id);
66
        session_start();
67
68
        // restore the old session data into the new session
69
        $_SESSION = $old_session_data;
70
71
        // update the session creation time
72
        $_SESSION['regenerated'] = time();
73
74
        // session_write_close() patch based on this thread
75
        // http://www.codeigniter.com/forums/viewthread/1624/
76
        // there is a question mark ?? as to side affects
77
        // end the current session and store session data.
78
        session_write_close();
79
    }
80
81
    public function all_userdata() {
82
83
        return $_SESSION;
84
    }
85
86
    /**
87
     * Destroys the session and erases session storage
88
     */
89 View Code Duplication
    public function destroy() {
90
91
        unset($_SESSION);
92
93
        if (isset($_COOKIE[session_name()])) {
94
            setcookie(session_name(), '', time() - 42000, '/');
95
        }
96
97
        session_destroy();
98
    }
99
100
    /**
101
     * Destroys the session and erases session storage
102
     */
103 View Code Duplication
    public function sess_destroy() {
104
105
        unset($_SESSION);
106
        if (isset($_COOKIE[session_name()])) {
107
            setcookie(session_name(), '', time() - 42000, '/');
108
        }
109
        session_destroy();
110
    }
111
112
    /**
113
     * Reads given session attribute value
114
     */
115
    public function userdata($item) {
116
117
        if ($item == 'session_id') { //added for backward-compatibility
118
            return session_id();
119
        } else {
120
            return (!isset($_SESSION[$item])) ? false : $_SESSION[$item];
121
        }
122
    }
123
124
    /**
125
     * Sets session attributes to the given values
126
     */
127 View Code Duplication
    public function set_userdata($newdata = [], $newval = '') {
128
129
        if (is_string($newdata)) {
130
            $newdata = [$newdata => $newval];
131
        }
132
133
        if (count($newdata) > 0) {
134
            foreach ($newdata as $key => $val) {
135
                $_SESSION[$key] = $val;
136
            }
137
        }
138
    }
139
140
    /**
141
     * Erases given session attributes
142
     */
143 View Code Duplication
    public function unset_userdata($newdata = []) {
144
145
        if (is_string($newdata)) {
146
            $newdata = [$newdata => ''];
147
        }
148
149
        if (count($newdata) > 0) {
150
            foreach ($newdata as $key => $val) {
151
                unset($_SESSION[$key]);
152
            }
153
        }
154
    }
155
156
    /**
157
     * Starts up the session system for current request
158
     */
159
    public function _sess_run() {
160
161
        @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...
162
163
        // check if session id needs regeneration
164
        if ($this->_session_id_expired()) {
165
            // regenerate session id (session data stays the
166
            // same, but old session storage is destroyed)
167
            $this->regenerate_id();
168
        }
169
170
        // delete old flashdata (from last request)
171
        $this->_flashdata_sweep();
172
173
        // mark all new flashdata as old (data will be deleted before next request)
174
        $this->_flashdata_mark();
175
    }
176
177
    /**
178
     * Checks if session has expired
179
     */
180
    public function _session_id_expired() {
181
182
        if (!isset($_SESSION['regenerated'])) {
183
            $_SESSION['regenerated'] = time();
184
            return false;
185
        }
186
187
        $expiry_time = time() - $this->session_id_ttl;
188
189
        if ($_SESSION['regenerated'] <= $expiry_time) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return $_SESSION['regenerated'] <= $expiry_time;.
Loading history...
190
            return true;
191
        }
192
193
        return false;
194
    }
195
196
    /**
197
     * Sets "flash" data which will be available only in next request (then it will
198
     * be deleted from session). You can use it to implement "Save succeeded" messages
199
     * after redirect.
200
     */
201
    public function set_flashdata($newdata = [], $newval = '') {
202
203
        // $flash_key = $this->flash_key.':new:'.$key;
204
        // $this->set_userdata($flash_key, $value);
205
206
        if (is_string($newdata)) {
207
            $newdata = [$newdata => $newval];
208
        }
209
210
        if (count($newdata) > 0) {
211
            foreach ($newdata as $key => $val) {
212
                $flashdata_key = $this->flash_key . ':new:' . $key;
213
                $this->set_userdata($flashdata_key, $val);
0 ignored issues
show
Documentation introduced by
$flashdata_key is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
214
            }
215
        }
216
    }
217
218
    /**
219
     * Keeps existing "flash" data available to next request.
220
     */
221
    public function keep_flashdata($key) {
222
223
        $old_flash_key = $this->flash_key . ':old:' . $key;
224
        $value = $this->userdata($old_flash_key);
225
226
        $new_flash_key = $this->flash_key . ':new:' . $key;
227
        $this->set_userdata($new_flash_key, $value);
0 ignored issues
show
Documentation introduced by
$new_flash_key is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
228
    }
229
230
    /**
231
     * Returns "flash" data for the given key.
232
     */
233
    public function flashdata($key) {
234
235
        $flash_key = $this->flash_key . ':old:' . $key;
236
        return $this->userdata($flash_key);
237
    }
238
239
    /**
240
     * PRIVATE: Internal method - marks "flash" session attributes as 'old'
241
     */
242
    public function _flashdata_mark() {
243
244
        foreach ($_SESSION as $name => $value) {
245
            $parts = explode(':new:', $name);
246
            if (is_array($parts) && count($parts) == 2) {
247
                $new_name = $this->flash_key . ':old:' . $parts[1];
248
                $this->set_userdata($new_name, $value);
0 ignored issues
show
Documentation introduced by
$new_name is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
249
                $this->unset_userdata($name);
0 ignored issues
show
Documentation introduced by
$name is of type integer|string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
250
            }
251
        }
252
    }
253
254
    /**
255
     * PRIVATE: Internal method - removes "flash" session marked as 'old'
256
     */
257
    public function _flashdata_sweep() {
258
259
        foreach ($_SESSION as $name => $value) {
260
            $parts = explode(':old:', $name);
261
            if (is_array($parts) && count($parts) == 2 && $parts[0] == $this->flash_key) {
262
                $this->unset_userdata($name);
0 ignored issues
show
Documentation introduced by
$name is of type integer|string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
263
            }
264
        }
265
    }
266
267
}