30
30
31
31
use Symfony \Component \HttpFoundation \Session \Session ;
32
32
use Symfony \Component \HttpFoundation \Request ;
33
+ use Defuse \Crypto \Key ;
34
+ use TeampassClasses \SessionManager \EncryptedSessionProxy ;
33
35
34
36
class SessionManager
35
37
{
36
38
private static $ session = null ;
37
39
38
40
public static function getSession ()
39
41
{
40
- if (null === self ::$ session ) {
41
- self ::$ session = new Session ();
42
+ if (null === self ::$ session ) {
43
+ // Load the encryption key
44
+ $ key = Key::loadFromAsciiSafeString (file_get_contents (SECUREPATH . "/ " . SECUREFILE ));
45
+
46
+ // Create an instance of EncryptedSessionProxy
47
+ $ handler = new EncryptedSessionProxy (new \SessionHandler (), $ key );
48
+
49
+ // Create a new session with the encrypted session handler
50
+ self ::$ session = new Session (new \Symfony \Component \HttpFoundation \Session \Storage \NativeSessionStorage ([], $ handler ));
51
+
42
52
if (session_status () === PHP_SESSION_NONE ) {
43
53
$ request = Request::createFromGlobals ();
44
54
$ isSecure = $ request ->isSecure ();
@@ -58,73 +68,73 @@ public static function getSession()
58
68
}
59
69
60
70
public static function addRemoveFromSessionArray ($ key , $ values = [], $ action = 'add ' ) {
61
- // Récupérer le tableau de la session
71
+ // Retrieve the array from the session
62
72
$ sessionArray = self ::getSession ()->get ($ key , []);
63
73
64
74
foreach ($ values as $ value ) {
65
75
if ($ action === 'add ' ) {
66
- // Ajouter la valeur au tableau
76
+ // Add the value to the array
67
77
$ sessionArray [] = $ value ;
68
78
} elseif ($ action === 'remove ' ) {
69
- // Trouver l' index de la valeur dans le tableau
79
+ // Find the index of the value in the array
70
80
$ index = array_search ($ value , $ sessionArray );
71
81
72
- // Si la valeur est trouvée dans le tableau, la supprimer
82
+ // If the value is found in the array, remove it
73
83
if ($ index !== false ) {
74
84
unset($ sessionArray [$ index ]);
75
85
}
76
86
}
77
87
}
78
88
79
- // Réaffecter le tableau à la session
89
+ // Reassign the array to the session
80
90
self ::getSession ()->set ($ key , $ sessionArray );
81
91
}
82
92
83
93
public static function specificOpsOnSessionArray ($ key , $ action = 'pop ' , $ value = null ) {
84
- // Récupérer le tableau de la session
94
+ // Retrieve the array from the session
85
95
$ sessionArray = self ::getSession ()->get ($ key , []);
86
96
87
97
if ($ action === 'pop ' ) {
88
- // Supprimer la dernière valeur du tableau
98
+ // Remove the last value from the array
89
99
array_pop ($ sessionArray );
90
100
} elseif ($ action === 'shift ' ) {
91
- // Supprimer la première valeur du tableau
101
+ // Remove the first value from the array
92
102
array_shift ($ sessionArray );
93
103
} elseif ($ action === 'reset ' ) {
94
- // Réinitialiser le tableau
104
+ // Reset the array
95
105
$ sessionArray = [];
96
106
} elseif ($ action === 'unshift ' && is_null ($ value ) === false ) {
97
- // Ajouter une valeur au début du tableau
107
+ // Add a value to the beginning of the array
98
108
array_unshift ($ sessionArray , $ value );
99
109
}
100
110
101
- // Réaffecter le tableau à la session
111
+ // Reassign the array to the session
102
112
self ::getSession ()->set ($ key , $ sessionArray );
103
113
}
104
114
105
115
public static function addRemoveFromSessionAssociativeArray ($ key , $ values = [], $ action = 'add ' ) {
106
- // Récupérer le tableau de la session
116
+ // Retrieve the array from the session
107
117
$ sessionArray = self ::getSession ()->get ($ key , []);
108
118
109
119
if ($ action === 'add ' ) {
110
- // Ajouter la valeur au tableau
120
+ // Add the value to the array
111
121
array_push ($ sessionArray , $ values );
112
122
} elseif ($ action === 'remove ' ) {
113
- // Si la valeur existe dans le tableau, la supprimer
123
+ // If the value exists in the array, remove it
114
124
if (($ key = array_search ($ values , $ sessionArray )) !== false ) {
115
125
unset($ sessionArray [$ key ]);
116
126
}
117
127
}
118
128
119
- // Réaffecter le tableau à la session
129
+ // Reassign the array to the session
120
130
self ::getSession ()->set ($ key , $ sessionArray );
121
131
}
122
132
123
133
public static function getCookieValue ($ cookieName )
124
134
{
125
135
$ request = Request::createFromGlobals ();
126
136
127
- // Vérifier si le cookie existe
137
+ // Check if the cookie exists
128
138
if ($ request ->cookies ->has ($ cookieName )) {
129
139
return $ request ->cookies ->get ($ cookieName );
130
140
}
0 commit comments