Cookies   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 79
rs 10
c 0
b 0
f 0
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A all() 0 2 1
A remove() 0 4 2
A set() 0 2 1
A reviveSessions() 0 20 6
A get() 0 6 2
1
<?php
2
3
/*
4
 * This file is part of the Ocrend Framewok 3 package.
5
 *
6
 * (c) Ocrend Software <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ocrend\Kernel\Cookies;
13
use Ocrend\Kernel\Helpers as Helper;
14
15
/**
16
 * Implementación de cookies con POO
17
 *
18
 * @author Brayan Narváez <[email protected]>
19
 */
20
class Cookies {
21
22
    /**
23
     * Establece una cookie
24
     * 
25
     * @param string $cookie : Nombre
26
     * @param mixed $value : Valor
27
     * @param int $time: Tiempo de vida
28
     * @param string $path : Path de creación
29
     * 
30
     * @return void
31
     */
32
    public function set(string $cookie, $value, int $time, string $path = '/') {
33
        setcookie($cookie, $value, time() + $time, $path);
34
    }
35
36
    /**
37
     * Obtiene una cookie
38
     * 
39
     * @param string $cookie : Nombre
40
     * 
41
     * @return mixed
42
     */
43
    public function get(string $cookie) {
44
        if (isset($_COOKIE[$cookie])) { 
45
            return $_COOKIE[$cookie];
46
        }
47
48
        return null;
49
    }
50
51
    /**
52
     * Obtiene todas las cookies
53
     * 
54
     * @return array
55
     */
56
    public function all() : array {
57
        return $_COOKIE;
58
    }
59
60
    /**
61
     * Elimina una cookie
62
     * 
63
     * @param string $cookie : Nombre
64
     * 
65
     * @return void
66
     */
67
    public function remove(string $cookie) {
68
        if (isset($_COOKIE[$cookie])) {
69
            unset($_COOKIE[$cookie]);
70
            setcookie($cookie, null, -1, '/');
71
        }
72
    }
73
74
    /**
75
     * Reestablece la sesión del usuario, en caso de que sea necesario
76
     * 
77
     * @return void
78
     */
79
    public function reviveSessions() {
80
        global $session, $config;
81
82
        # Verificar si está habilitada la opción de sesión de usuario con cookie
83
        if($config['sessions']['user_cookie']['enable']) {
84
            # Obtener
85
            $user_session = $session->get($this->get('session_hash') . '__user_id');
86
            $salt = $this->get('appsalt');
87
            $encrypt = $this->get('appencrypt');
88
89
            # Verificar que no estén vacías y coincidan
90
            if(null == $user_session && null != $salt && null != $encrypt && Helper\Strings::chash($salt ?? '_', $encrypt ?? '/$')) {
91
                # Generar un nuevo session hash
92
                $this->set('session_hash', md5(time()) , $config['sessions']['user_cookie']['lifetime']);
93
94
                # Desencriptar el id del usuario
95
                $id_user = Helper\Strings::ocrend_decode($encrypt, $config['sessions']['user_cookie']['key_encrypt']);
96
97
                # Reestablecer la sesión
98
                $session->set($this->get('session_hash') . '__user_id',(int) $id_user);
99
            }
100
        }       
101
    }
102
}
103