@@ -52,6 +52,8 @@ class Session
5252 private static $ ssl_enabled = true ;
5353
5454 public static $ write = false ;
55+
56+ public static $ id = '' ;
5557
5658 private static function init ()
5759 {
@@ -131,33 +133,34 @@ private static function init()
131133 *
132134 * @param string $id
133135 */
134- public static function id (string $ id)
136+ public static function id (string $ id = '' ): string
135137 {
136138 if (empty (self ::$ initialized ))
137139 {
138140 self ::init ();
139141 }
140142
141- if (self ::$ started )
142- {
143- throw new \RuntimeException ('Session is active. The session id must be set before Session::start(). ' );
144- }
145- elseif (strlen ($ id ) > 250 )
146- {
147- throw new \RuntimeException ('Session id cant be above 250 characters long ' );
148- }
149- elseif (headers_sent ($ filename , $ line_num ))
143+ if ($ id != '' )
150144 {
151- throw new \RuntimeException (sprintf ('ID must be set before any output is sent to the browser (file: %s, line: %s) ' , $ filename , $ line_num ));
152- }
153- elseif (preg_match ('/^[\w-,]{1,128}$/ ' , $ id ) < 1 )
154- {
155- throw new \InvalidArgumentException ('Invalid Session ID ' );
156- }
157- else
158- {
159- session_id ($ id );
145+ if (self ::$ started )
146+ {
147+ throw new \RuntimeException ('Session is active. The session id must be set before Session::start(). ' );
148+ }
149+ elseif (headers_sent ($ filename , $ line_num ))
150+ {
151+ throw new \RuntimeException (sprintf ('ID must be set before any output is sent to the browser (file: %s, line: %s) ' , $ filename , $ line_num ));
152+ }
153+ elseif (preg_match ('/^[-,a-zA-Z0-9]{1,128}$/ ' , $ id ) < 1 )
154+ {
155+ throw new \InvalidArgumentException ('Invalid Session ID. ' );
156+ }
157+ else
158+ {
159+ session_id ($ id );
160+ }
160161 }
162+
163+ return self ::$ id ;
161164 }
162165
163166
@@ -222,18 +225,18 @@ public static function decrypt(string $data): string
222225 $ ct = substr ($ data , 16 );
223226
224227 $ rounds = 3 ; // depends on key length
225- $ data00 = $ password. $ salt ;
226- $ hash = array () ;
228+ $ data00 = $ password . $ salt ;
229+ $ hash = [] ;
227230 $ hash [0 ] = hash ('sha256 ' , $ data00 , true );
228231 $ result = $ hash [0 ];
229232 for ($ i = 1 ; $ i < $ rounds ; $ i ++)
230233 {
231- $ hash [$ i ] = hash ('sha256 ' , $ hash [$ i - 1 ]. $ data00 , true );
234+ $ hash [$ i ] = hash ('sha256 ' , $ hash [$ i - 1 ] . $ data00 , true );
232235 $ result .= $ hash [$ i ];
233236 }
234237 $ key = substr ($ result , 0 , 32 );
235238 $ iv = substr ($ result , 32 ,16 );
236- $ decrypted = openssl_decrypt ($ ct , 'AES-256-CBC ' , $ key , true , $ iv );
239+ $ decrypted = openssl_decrypt ($ ct , 'AES-256-CBC ' , $ key , 1 , $ iv );
237240
238241 return ( ! $ decrypted ) ? '' : $ decrypted ;
239242 }
@@ -259,14 +262,14 @@ public static function encrypt(string $data): string
259262 // Salt the key(32) and iv(16) = 48
260263 while (strlen ($ salted ) < 48 )
261264 {
262- $ dx = hash ('sha256 ' , $ dx. $ password. $ salt , true );
265+ $ dx = hash ('sha256 ' , $ dx . $ password . $ salt , true );
263266 $ salted .= $ dx ;
264267 }
265268
266269 $ key = substr ($ salted , 0 , 32 );
267270 $ iv = substr ($ salted , 32 ,16 );
268271
269- $ encrypted_data = openssl_encrypt ($ data , 'AES-256-CBC ' , $ key , true , $ iv );
272+ $ encrypted_data = openssl_encrypt ($ data , 'AES-256-CBC ' , $ key , 1 , $ iv );
270273 return base64_encode ($ salt . $ encrypted_data );
271274 }
272275}
0 commit comments