1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Never5\DownloadMonitor\Shop\Session; |
4
|
|
|
|
5
|
|
|
use Never5\DownloadMonitor\Shop\Services\Services; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Cookie |
9
|
|
|
* @package Never5\DownloadMonitor\Shop\Session |
10
|
|
|
* |
11
|
|
|
* This class handles all cookie related things for the session cookie |
12
|
|
|
*/ |
13
|
|
|
class Cookie { |
14
|
|
|
|
15
|
|
|
const COOKIE_NAME = 'dlm_session'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Get session from cookie |
19
|
|
|
* |
20
|
|
|
* @return Session |
21
|
|
|
*/ |
22
|
|
|
public function get_session_from_cookie() { |
23
|
|
|
$session = null; |
24
|
|
|
|
25
|
|
|
if ( isset( $_COOKIE[ self::COOKIE_NAME ] ) ) { |
26
|
|
|
$cookie_data = json_decode( base64_decode( $_COOKIE[ self::COOKIE_NAME ] ), true ); |
27
|
|
|
|
28
|
|
|
if ( is_array( $cookie_data ) && ! empty( $cookie_data['key'] ) && ! empty( $cookie_data['hash'] ) ) { |
29
|
|
|
|
30
|
|
|
try { |
31
|
|
|
$session = Services::get()->service( 'session_repository' )->retrieve( $cookie_data['key'], $cookie_data['hash'] ); |
32
|
|
|
} catch ( \Exception $exception ) { |
|
|
|
|
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return $session; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Set the session cookie |
42
|
|
|
* |
43
|
|
|
* @param Session $session |
44
|
|
|
*/ |
45
|
|
|
public function set_session_cookie( $session ) { |
46
|
|
|
setcookie( self::COOKIE_NAME, base64_encode( json_encode( array( |
47
|
|
|
'key' => $session->get_key(), |
48
|
|
|
'hash' => $session->get_hash() |
49
|
|
|
) ) ), $session->get_expiry()->getTimestamp(), COOKIEPATH ? COOKIEPATH : '/', COOKIE_DOMAIN, false, true ); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Destroy the session cookie |
54
|
|
|
*/ |
55
|
|
|
public function destroy_session_cookie() { |
56
|
|
|
setcookie( self::COOKIE_NAME, "", 1, COOKIEPATH ? COOKIEPATH : '/', COOKIE_DOMAIN, false, true ); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Returns true if it's safe to set cookie, false if it's not |
61
|
|
|
* |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
|
|
public function is_cookie_allowed() { |
65
|
|
|
if ( headers_sent( $file, $line ) ) { |
66
|
|
|
return false; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return true; |
70
|
|
|
} |
71
|
|
|
} |