Total Complexity | 67 |
Total Lines | 367 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like session often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use session, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class session |
||
17 | { |
||
18 | private $path_defined = null; |
||
19 | public $sessionCookieName = 'uf'; |
||
20 | public $cookiePath = '/'; |
||
21 | public $cookieDomain = ''; |
||
22 | /** |
||
23 | * Cookie will only be set if a secure HTTPS connection exists. |
||
24 | * |
||
25 | * @var bool |
||
26 | */ |
||
27 | private $cookieSecure = false; |
||
28 | /** |
||
29 | * sid regex expression. |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | private $sidRegexp; |
||
34 | |||
35 | public function __construct(int $timeout = 3600, string $session_folder = null) |
||
36 | { |
||
37 | if (!$this->is_session_started()) { |
||
38 | //$this->configure($timeout, $session_folder); |
||
39 | //$this->start_timeout($timeout); |
||
40 | $this->handle($timeout, $session_folder); |
||
41 | } |
||
42 | } |
||
43 | |||
44 | public function is_session_started() |
||
45 | { |
||
46 | return PHP_SESSION_ACTIVE == session_status(); |
||
47 | } |
||
48 | |||
49 | public function handle(int $timeout, string $folder = null) |
||
50 | { |
||
51 | $name = '_' . $timeout . md5(\MVC\helper::getRequestIP() . \MVC\helper::useragent()); |
||
52 | if (empty(trim($folder)) || !$folder) { |
||
|
|||
53 | $folder = \Filemanager\file::folder(__DIR__ . '/sessions'); |
||
54 | } |
||
55 | session_save_path($folder); |
||
56 | |||
57 | session_set_cookie_params($timeout); |
||
58 | |||
59 | ini_set('session.gc_maxlifetime', $timeout); |
||
60 | ini_set('session.cookie_lifetime', $timeout); |
||
61 | ini_set('session.gc_probability', 100); |
||
62 | ini_set('session.gc_divisor', 100); |
||
63 | |||
64 | session_id($name); |
||
65 | ini_set('session.use_strict_mode', 0); |
||
66 | |||
67 | session_name($name); |
||
68 | ini_set('session.use_strict_mode', 1); |
||
69 | |||
70 | $handler = new FileSessionHandler($folder, 'PHPJS'); |
||
71 | session_set_save_handler( |
||
72 | [$handler, 'open'], |
||
73 | [$handler, 'close'], |
||
74 | [$handler, 'read'], |
||
75 | [$handler, 'write'], |
||
76 | [$handler, 'destroy'], |
||
77 | [$handler, 'gc'] |
||
78 | ); |
||
79 | register_shutdown_function('session_write_close'); |
||
80 | session_start(); |
||
81 | |||
82 | $path = ini_get('session.save_path'); |
||
83 | if (!file_exists($path . '/.htaccess')) { |
||
84 | file::file($path . '/.htaccess', 'deny from all'); |
||
85 | } |
||
86 | if (!isset($_SESSION['session_started'])) { |
||
87 | $_SESSION['session_started'] = $this->now(); |
||
88 | $_SESSION['session_timeout'] = ini_get('session.gc_maxlifetime'); |
||
89 | $_SESSION['cookie_timeout'] = ini_get('session.cookie_lifetime'); |
||
90 | } |
||
91 | } |
||
92 | |||
93 | private function configure(int $timeout, string $folder = null) |
||
191 | } |
||
192 | |||
193 | /*** |
||
194 | * Starts a session with a specific timeout and a specific GC probability. |
||
195 | * @param int $timeout The number of seconds until it should time out. |
||
196 | * @param int $probability The probablity, in int percentage, that the garbage |
||
197 | * collection routine will be triggered right now. |
||
198 | * @param strint $cookie_domain The domain path for the cookie. |
||
199 | */ |
||
200 | public function start_timeout($timeout = 5, $probability = 100) |
||
201 | { |
||
202 | if ($this->is_session_started()) { |
||
203 | throw new \MVC\Exception('Session already started', 1); |
||
204 | } |
||
205 | |||
206 | // Set the chance to trigger the garbage collection. |
||
207 | ini_set('session.gc_probability', $probability); |
||
208 | ini_set('session.gc_divisor', 100); // Should always be 100 |
||
209 | |||
210 | if (!$this->path_defined) { |
||
211 | $this->configure($timeout, null); |
||
212 | } |
||
213 | |||
214 | // Start the session! |
||
215 | session_start(); |
||
216 | |||
217 | // Renew the time left until this session times out. |
||
218 | // If you skip this, the session will time out based |
||
219 | // on the time when it was created, rather than when |
||
220 | // it was last used. |
||
221 | if (isset($_COOKIE[session_name()]) && !headers_sent()) { |
||
222 | setcookie(session_name(), $_COOKIE[session_name()], time() + $timeout, $this->cookiePath, $this->cookieDomain, $this->cookieSecure, true); |
||
223 | } |
||
224 | $this->protect_session($timeout); |
||
225 | } |
||
226 | |||
227 | public function dump() |
||
228 | { |
||
229 | exit(\JSON\json::json( |
||
230 | [ |
||
231 | 'sessions' => [ |
||
232 | 'active' => PHP_SESSION_NONE == session_status(), |
||
233 | 'id' => session_id(), |
||
234 | 'folder' => \MVC\helper::fixSlash(ini_get('session.save_path')), |
||
235 | 'session.gc_maxlifetime' => ini_get('session.gc_maxlifetime'), |
||
236 | 'session.cookie_lifetime' => ini_get('session.cookie_lifetime'), |
||
237 | 'session.gc_probability' => ini_get('session.gc_probability'), |
||
238 | 'session.gc_divisor' => ini_get('session.gc_divisor'), |
||
239 | 'session.hash_function' => ini_get('session.hash_function'), |
||
240 | ], |
||
241 | ] |
||
242 | )); |
||
243 | } |
||
244 | |||
245 | public static function has($key, bool $empty = true) |
||
246 | { |
||
247 | $return = isset($_SESSION[$key]); |
||
248 | if ($return && !$empty) { |
||
249 | $return = $return && !empty($return); |
||
250 | } |
||
251 | |||
252 | return $return; |
||
253 | } |
||
254 | |||
255 | public static function get($key) |
||
256 | { |
||
257 | if (isset($_SESSION[$key])) { |
||
258 | return $_SESSION[$key]; |
||
259 | } |
||
260 | } |
||
261 | |||
262 | public static function gets(array $keys) |
||
263 | { |
||
264 | $result = []; |
||
265 | foreach ($keys as $key) { |
||
266 | if (isset($_SESSION[$key])) { |
||
267 | $result[] = $_SESSION[$key]; |
||
268 | } |
||
269 | } |
||
270 | |||
271 | return $result; |
||
272 | } |
||
273 | |||
274 | public static function all() |
||
275 | { |
||
276 | return $_SESSION; |
||
277 | } |
||
278 | |||
279 | private static $_instance = null; |
||
280 | |||
281 | public static function getInstance() |
||
282 | { |
||
283 | if (null === self::$_instance) { |
||
284 | self::$_instance = new self(); |
||
285 | } |
||
286 | |||
287 | return self::$_instance; |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Unset Session. |
||
292 | * |
||
293 | * @param array|string|Number $name |
||
294 | */ |
||
295 | public static function unses($name) |
||
296 | { |
||
297 | if (is_array($name)) { |
||
298 | foreach ($name as $n) { |
||
299 | if (isset($_SESSION[$n])) { |
||
300 | unset($_SESSION[$n]); |
||
301 | } |
||
302 | } |
||
303 | } elseif (is_string($name)) { |
||
304 | if (isset($_SESSION[$name])) { |
||
305 | unset($_SESSION[$name]); |
||
306 | } |
||
307 | } |
||
308 | } |
||
309 | |||
310 | private function protect_session(int $timeout) |
||
311 | { |
||
312 | //creating htaccess for deny direct access |
||
313 | $path = ini_get('session.save_path'); |
||
314 | if (!file_exists($path . '/.htaccess')) { |
||
315 | file::file($path . '/.htaccess', 'deny from all'); |
||
316 | } |
||
317 | if (!isset($_SESSION['session_started'])) { |
||
318 | $_SESSION['session_started'] = $this->now(); |
||
319 | $_SESSION['session_timeout'] = ini_get('session.gc_maxlifetime'); |
||
320 | $_SESSION['cookie_timeout'] = ini_get('session.cookie_lifetime'); |
||
321 | } elseif (isset($_SESSION['session_started'])) { |
||
322 | /** |
||
323 | * @var DateTime |
||
324 | */ |
||
325 | $started = $_SESSION['session_started']; |
||
326 | $ago = $started->getTimestamp() + $timeout; |
||
327 | $_SESSION['session_expires_in'] = date('D M j G:i:s O Y', $ago); |
||
328 | if ($ago < $this->now()->getTimestamp()) { |
||
329 | session_regenerate_id(isset($_SERVER['HTTP_LOGOUT'])); |
||
330 | } |
||
331 | } |
||
332 | if (isset($_SESSION['login'])) { |
||
333 | if (isset($_REQUEST['dump-session']) && isset($_SESSION['login']['role'])) { |
||
334 | if (preg_match('/admin/s', $_SESSION['login']['role'])) { |
||
335 | $this->dump(); |
||
336 | } |
||
337 | } |
||
338 | } |
||
339 | } |
||
340 | |||
341 | public function now() |
||
342 | { |
||
343 | $now = new \DateTime(null, new \DateTimeZone('Asia/Jakarta')); |
||
344 | |||
345 | return $now; |
||
346 | } |
||
347 | |||
348 | public function is_sess($session_name, $not_found = null) |
||
355 | } |
||
356 | |||
357 | public function sess($key, $val) |
||
360 | } |
||
361 | |||
362 | public static function set_session($data, $value = null) |
||
363 | { |
||
371 | } |
||
372 | } |
||
373 | |||
374 | /** |
||
375 | * Regenerates the session ID. |
||
376 | * |
||
377 | * @param bool $destroy Should old session data be destroyed? |
||
378 | */ |
||
379 | private function regenerate(bool $destroy = false) |
||
383 | } |
||
384 | } |
||
385 |