Passed
Push — develop ( a88cb3...66d0e3 )
by Mykola
05:02
created

Session::open()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 2
1
<?php
2
/* 	Sunrise CMS - Open source CMS for widespread use.
3
    Copyright (c) 2019 Mykola Burakov ([email protected])
4
5
    See SOURCE.txt for other and additional information.
6
7
    This file is part of Sunrise CMS.
8
9
    This program is free software: you can redistribute it and/or modify
10
    it under the terms of the GNU General Public License as published by
11
    the Free Software Foundation, either version 3 of the License, or
12
    (at your option) any later version.
13
14
    This program is distributed in the hope that it will be useful,
15
    but WITHOUT ANY WARRANTY; without even the implied warranty of
16
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
    GNU General Public License for more details.
18
19
    You should have received a copy of the GNU General Public License
20
    along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22
namespace Sunrise\Engine\Library;
23
24
class Session implements \SessionHandlerInterface
25
{
26
    public $data = array();
27
    // public $expire;
28
29
    public function __construct($registry)
30
    {
31
        // доступ к БД
32
        $this->db = $registry->get('db');
0 ignored issues
show
Bug Best Practice introduced by
The property db does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
33
34
        // $expire = ini_get('session.gc_maxlifetime');
35
36
        // Устанавливает пользовательские обработчики хранения сессии
37
        // https://www.php.net/manual/ru/function.session-set-save-handler.php
38
        // if (!session_id()) {
39
40
        // https://www.php.net/manual/en/session.configuration.php#ini.session.cookie-secure
41
        ini_set('session.cookie_secure', '1');
42
43
        // https://www.php.net/manual/en/session.configuration.php#ini.session.cookie-httponly
44
        ini_set('session.cookie_httponly', '1');
45
46
        // https://www.php.net/manual/ru/function.session-set-cookie-params.php
47
        // session_set_cookie_params(0, '/');
48
49
        // стартуем сессию
50
        session_start();
51
        // }
52
    }
53
54
    // public function __construct($registry)
55
    // {
56
    //     $this->db = $registry->get('db');
57
    //     $this->expire = ini_get('session.gc_maxlifetime');
58
    // }
59
60
    public function start($key = 'default', $value = '')
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

60
    public function start(/** @scrutinizer ignore-unused */ $key = 'default', $value = '')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $value is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

60
    public function start($key = 'default', /** @scrutinizer ignore-unused */ $value = '')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
61
    {
62
        // по-умолчанию передается пустое значение
63
        // if ($value) {
64
        // $this->session_id = $value;
65
        // если в куках уже существует значение
66
        // } elseif (isset($_COOKIE[$key])) {
67
        // делаем session_id таким же
68
        // $this->session_id = $_COOKIE[$key];
69
        // если в куках значения нет, то создаем новое значение методом createSessionId()
70
        // } else {
71
        $this->session_id = $this->createSessionId();
0 ignored issues
show
Bug Best Practice introduced by
The property session_id does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
72
        // }
73
74
        // if (!isset($_SESSION[$this->session_id])) {
75
        //     $_SESSION[$this->session_id] = array();
76
        // }
77
78
        // $this->data = &$_SESSION[$this->session_id];
79
80
        // https://opencartforum.com/topic/82248-nastroyki-sessii/?do=findComment&comment=935102
81
        // if ($key != 'PHPSESSID') {
82
        //     setcookie(
83
        //         $key,
84
        //         $this->session_id,
85
        //         time() + ini_get('session.cookie_lifetime'),
86
        //         ini_get('session.cookie_path'),
87
        //         ini_get('session.cookie_domain'),
88
        //         ini_get('session.cookie_secure'),
89
        //         ini_get('session.cookie_httponly')
90
        //     );
91
        // }
92
93
        return $this->session_id;
94
    }
95
96
    public function open($path, $name)
97
    {
98
        return parent::open($path, $name);
99
    }
100
101
    public function close()
102
    {
103
        return parent::close();
104
    }
105
106
    public function read($session_id)
107
    {
108
        return parent::read($session_id);
109
    }
110
111
    // public function read($session_id)
112
    // {
113
    //     $query = $this->db->query("
114
    //         SELECT data 
115
    //         FROM session 
116
    //         WHERE session_id = '" . $this->db->escape($session_id) . "' 
117
    //             AND expire > '" . $this->db->escape(date('Y-m-d H:i:s', time())) . "'
118
    //     ");
119
120
    //     if ($query->num_rows) {
121
    //         return json_decode($query->row['data'], true);
122
    //     } else {
123
    //         return false;
124
    //     }
125
    // }
126
127
    public function write($session_id, $data)
128
    {
129
        return parent::write($session_id, $data);
130
    }
131
132
    // public function write($session_id, $data)
133
    // {
134
    //     if ($session_id) {
135
    //         $this->db->query("
136
    //             REPLACE INTO session 
137
    //             SET session_id = '" . $this->db->escape($session_id) . "', 
138
    //                 data = '" . $this->db->escape(json_encode($data)) . "', 
139
    //                 expire = '" . $this->db->escape(date('Y-m-d H:i:s', time() + $this->expire)) . "'
140
    //         ");
141
    //     }
142
143
    //     return true;
144
    // }
145
146
    public function destroy($session_id)
147
    {
148
        return parent::destroy($session_id);
149
    }
150
151
    // public function destroy($key = 'default')
152
    // {
153
    //     if (isset($_SESSION[$key])) {
154
    //         unset($_SESSION[$key]);
155
    //     }
156
157
    //     setcookie(
158
    //         $key,
159
    //         '',
160
    //         time() - 42000,
161
    //         ini_get('session.cookie_path'),
162
    //         ini_get('session.cookie_domain')
163
    //     );
164
    // }
165
166
    // public function destroy($session_id)
167
    // {
168
    //     $this->db->query("
169
    //         DELETE FROM session 
170
    //         WHERE session_id = '" . $this->db->escape($session_id) . "'
171
    //     ");
172
173
    //     return true;
174
    // }
175
176
    public function gc($maxlifetime)
177
    {
178
        return parent::gc($maxlifetime);
179
    }
180
181
    // public function gc($expire)
182
    // {
183
    //     $this->db->query("
184
    //         DELETE FROM session 
185
    //         WHERE expire < '" . $this->db->escape(date('Y-m-d H:i:s', time())) . "'
186
    //     ");
187
188
    //     return true;
189
    // }
190
191
    public function create_sid()
0 ignored issues
show
Coding Style introduced by
Method name "Session::create_sid" is not in camel caps format
Loading history...
192
    {
193
        $id = substr(bin2hex(openssl_random_pseudo_bytes(48)), 0, 48);
194
195
        return $id;
196
    }
197
198
    public function createSessionId()
199
    {
200
        $id = substr(bin2hex(openssl_random_pseudo_bytes(26)), 0, 26);
201
202
        return $id;
203
    }
204
205
    public function getSessionId()
206
    {
207
        return $this->session_id;
208
    }
209
}
210