1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SKien\PNServer; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class representing subscrition. |
8
|
|
|
* |
9
|
|
|
* @package PNServer |
10
|
|
|
* @author Stefanius <[email protected]> |
11
|
|
|
* @copyright MIT License - see the LICENSE file for details |
12
|
|
|
*/ |
13
|
|
|
class PNSubscription |
14
|
|
|
{ |
15
|
|
|
use PNServerHelper; |
16
|
|
|
|
17
|
|
|
/** @var string the endpoint URL for the push notification */ |
18
|
|
|
protected string $strEndpoint = ''; |
19
|
|
|
/** @var string public key */ |
20
|
|
|
protected string $strPublicKey = ''; |
21
|
|
|
/** @var string authentification token */ |
22
|
|
|
protected string $strAuth = ''; |
23
|
|
|
/** @var int unix timesatmp of expiration (0, if no expiration defined) */ |
24
|
|
|
protected int $timeExpiration = 0; |
25
|
|
|
/** @var string encoding ('aesgcm' / 'aes128gcm') */ |
26
|
|
|
protected string $strEncoding = ''; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Use static method PNSubscription::fromJSON() instead of new-operator |
30
|
|
|
* if data is available as JSON-string |
31
|
|
|
* @param string $strEndpoint |
32
|
|
|
* @param string $strPublicKey |
33
|
|
|
* @param string $strAuth |
34
|
|
|
* @param int $timeExpiration |
35
|
|
|
* @param string $strEncoding |
36
|
|
|
*/ |
37
|
|
|
public function __construct(string $strEndpoint, string $strPublicKey, string $strAuth, int $timeExpiration = 0, string $strEncoding = 'aesgcm') |
38
|
|
|
{ |
39
|
|
|
$this->strEndpoint = $strEndpoint; |
40
|
|
|
$this->strPublicKey = $strPublicKey; |
41
|
|
|
$this->strAuth = $strAuth; |
42
|
|
|
$this->timeExpiration = $timeExpiration; |
43
|
|
|
$this->strEncoding = $strEncoding; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $strJSON subscription as valid JSON string |
48
|
|
|
* @return PNSubscription |
49
|
|
|
*/ |
50
|
|
|
public static function fromJSON(string $strJSON) : PNSubscription |
51
|
|
|
{ |
52
|
|
|
$strEndpoint = ''; |
53
|
|
|
$strPublicKey = ''; |
54
|
|
|
$strAuth = ''; |
55
|
|
|
$timeExpiration = 0; |
56
|
|
|
$aJSON = json_decode($strJSON, true); |
57
|
|
|
if (isset($aJSON['endpoint'])) { |
58
|
|
|
$strEndpoint = $aJSON['endpoint']; |
59
|
|
|
} |
60
|
|
|
if (isset($aJSON['expirationTime'])) { |
61
|
|
|
$timeExpiration = intval(bcdiv($aJSON['expirationTime'], '1000')); |
62
|
|
|
} |
63
|
|
|
if (isset($aJSON['keys'])) { |
64
|
|
|
if (isset($aJSON['keys']['p256dh'])) { |
65
|
|
|
$strPublicKey = $aJSON['keys']['p256dh']; |
66
|
|
|
} |
67
|
|
|
if (isset($aJSON['keys']['auth'])) { |
68
|
|
|
$strAuth = $aJSON['keys']['auth']; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
return new self($strEndpoint, $strPublicKey, $strAuth, $timeExpiration); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* basic check if object containing valid data |
76
|
|
|
* - endpoint, public key and auth token must be set |
77
|
|
|
* - only encoding 'aesgcm' or 'aes128gcm' supported |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
|
|
public function isValid() : bool |
81
|
|
|
{ |
82
|
|
|
$bValid = false; |
83
|
|
|
if (!$this->isExpired()) { |
84
|
|
|
$bValid = ( |
85
|
|
|
isset($this->strEndpoint) && strlen($this->strEndpoint) > 0 && |
86
|
|
|
isset($this->strPublicKey) && strlen($this->strPublicKey) > 0 && |
87
|
|
|
isset($this->strAuth) && strlen($this->strAuth) > 0 && |
88
|
|
|
($this->strEncoding == 'aesgcm' || $this->strEncoding == 'aes128gcm') |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
return $bValid; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public function getEndpoint() : string |
98
|
|
|
{ |
99
|
|
|
return $this->strEndpoint; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
public function getPublicKey() : string |
106
|
|
|
{ |
107
|
|
|
return $this->strPublicKey; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public function getAuth() : string |
114
|
|
|
{ |
115
|
|
|
return $this->strAuth; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
public function getEncoding() : string |
122
|
|
|
{ |
123
|
|
|
return $this->strEncoding; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param string $strEndpoint |
128
|
|
|
*/ |
129
|
|
|
public function setEndpoint(string $strEndpoint) : void |
130
|
|
|
{ |
131
|
|
|
$this->strEndpoint = $strEndpoint; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string $strPublicKey |
136
|
|
|
*/ |
137
|
|
|
public function setPublicKey(string $strPublicKey) : void |
138
|
|
|
{ |
139
|
|
|
$this->strPublicKey = $strPublicKey; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param string $strAuth |
144
|
|
|
*/ |
145
|
|
|
public function setAuth(string $strAuth) : void |
146
|
|
|
{ |
147
|
|
|
$this->strAuth = $strAuth; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param int $timeExpiration |
152
|
|
|
*/ |
153
|
|
|
public function setExpiration(int $timeExpiration) : void |
154
|
|
|
{ |
155
|
|
|
$this->timeExpiration = $timeExpiration; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param string $strEncoding |
160
|
|
|
*/ |
161
|
|
|
public function setEncoding(string $strEncoding) : void |
162
|
|
|
{ |
163
|
|
|
$this->strEncoding = $strEncoding; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return bool |
168
|
|
|
*/ |
169
|
|
|
public function isExpired() : bool |
170
|
|
|
{ |
171
|
|
|
return ($this->timeExpiration != 0 && $this->timeExpiration < time()); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* extract origin from endpoint |
176
|
|
|
* @param string $strEndpoint endpoint URL |
177
|
|
|
* @return string |
178
|
|
|
*/ |
179
|
|
|
public static function getOrigin(string $strEndpoint) : string |
180
|
|
|
{ |
181
|
|
|
return parse_url($strEndpoint, PHP_URL_SCHEME) . '://' . parse_url($strEndpoint, PHP_URL_HOST); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|