|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EventEspresso\core\domain\values\session; |
|
4
|
|
|
|
|
5
|
|
|
use DomainException; |
|
6
|
|
|
|
|
7
|
|
|
defined('EVENT_ESPRESSO_VERSION') || exit; |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class SessionLifespan |
|
13
|
|
|
* Value Object for storing and sharing session lifespan information. |
|
14
|
|
|
* This value is filterable by doing something like the following: |
|
15
|
|
|
* add_filter( |
|
16
|
|
|
* 'FHEE__EventEspresso_core_domain_values_session_SessionLifespan__setLifespan___lifespan', |
|
17
|
|
|
* function() { return 15 * MINUTE_IN_SECONDS; } |
|
18
|
|
|
* ); |
|
19
|
|
|
* |
|
20
|
|
|
* @package EventEspresso\core\services\session |
|
21
|
|
|
* @author Brent Christensen |
|
22
|
|
|
* @since $VID:$ |
|
23
|
|
|
*/ |
|
24
|
|
|
class SessionLifespan |
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* how long an EE session lasts in seconds |
|
29
|
|
|
* default session lifespan of 1 hour (for not so instant IPNs) |
|
30
|
|
|
* |
|
31
|
|
|
* @var int $lifespan |
|
32
|
|
|
*/ |
|
33
|
|
|
private $lifespan; |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* SessionLifespan constructor. |
|
38
|
|
|
* |
|
39
|
|
|
* @param int $lifespan |
|
40
|
|
|
* @throws DomainException |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct($lifespan = 0) |
|
43
|
|
|
{ |
|
44
|
|
|
$lifespan = absint($lifespan); |
|
45
|
|
|
$lifespan = $lifespan > 0 ? $lifespan : (int) HOUR_IN_SECONDS; |
|
46
|
|
|
$this->setLifespan($lifespan); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param int $lifespan |
|
52
|
|
|
* @throws DomainException |
|
53
|
|
|
*/ |
|
54
|
|
|
protected function setLifespan($lifespan) |
|
55
|
|
|
{ |
|
56
|
|
|
if($lifespan < 60) { |
|
57
|
|
|
throw new DomainException( |
|
58
|
|
|
esc_html__( |
|
59
|
|
|
'The session lifespan needs to be at least 60 seconds, and even that is extremely short', |
|
60
|
|
|
'event_espresso' |
|
61
|
|
|
) |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
$this->lifespan = apply_filters( |
|
66
|
|
|
'FHEE__EventEspresso_core_domain_values_session_SessionLifespan__setLifespan___lifespan', |
|
67
|
|
|
// apply legacy filter for now but add doing it wrong notice in future |
|
68
|
|
|
apply_filters( |
|
69
|
|
|
'FHEE__EE_Session__construct___lifespan', |
|
70
|
|
|
$lifespan |
|
71
|
|
|
) |
|
72
|
|
|
) + 1; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return int |
|
78
|
|
|
*/ |
|
79
|
|
|
public function inSeconds() |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->lifespan; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param string $separator |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
|
|
public function inHoursMinutesSeconds($separator = ':') |
|
90
|
|
|
{ |
|
91
|
|
|
return sprintf( |
|
92
|
|
|
'%02d%s%02d%s%02d', |
|
93
|
|
|
floor($this->lifespan / 3600), |
|
94
|
|
|
$separator, |
|
95
|
|
|
($this->lifespan / 60) % 60, |
|
96
|
|
|
$separator, |
|
97
|
|
|
$this->lifespan % 60 |
|
98
|
|
|
); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Returns a timestamp for when the session would expire based on this lifespan |
|
104
|
|
|
* |
|
105
|
|
|
* @param bool $utc If true, displays expiration in UTC |
|
106
|
|
|
* If false, displays expiration in local time |
|
107
|
|
|
* @return float |
|
108
|
|
|
*/ |
|
109
|
|
|
public function expiration($utc = true) |
|
110
|
|
|
{ |
|
111
|
|
|
return current_time('timestamp', $utc) - $this->lifespan; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return string |
|
117
|
|
|
*/ |
|
118
|
|
|
public function __toString() |
|
119
|
|
|
{ |
|
120
|
|
|
return (string) $this->inSeconds(); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|