Passed
Push — develop ( 8daac1...a2b8ae )
by Mykola
04:44
created

Session::gc()   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
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/* 	Divine 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 Divine 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 Divine\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->dbhProperties = $registry->get('dbhProperties');
0 ignored issues
show
Bug Best Practice introduced by
The property dbhProperties does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
33
34
        bdump($this->dbhProperties);
35
36
        // $expire = ini_get('session.gc_maxlifetime');
37
38
        // Устанавливает пользовательские обработчики хранения сессии
39
        // https://www.php.net/manual/ru/function.session-set-save-handler.php
40
        session_set_save_handler(new \RememberMe\RememberMeSessionHandler($this->dbhProperties), true);
41
42
        // if (!session_id()) {
43
44
        // https://www.php.net/manual/en/session.configuration.php#ini.session.cookie-secure
45
        ini_set('session.cookie_secure', '1');
46
47
        // https://www.php.net/manual/en/session.configuration.php#ini.session.cookie-httponly
48
        ini_set('session.cookie_httponly', '1');
49
50
        // https://www.php.net/manual/ru/function.session-set-cookie-params.php
51
        // session_set_cookie_params(0, '/');
52
53
        // стартуем сессию
54
        session_start();
55
        // }
56
    }
57
58
    // public function __construct($registry)
59
    // {
60
    //     $this->db = $registry->get('db');
61
    //     $this->expire = ini_get('session.gc_maxlifetime');
62
    // }
63
64
    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

64
    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

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