1 | <?php |
||
12 | abstract class BaseUrlSigner implements UrlSigner |
||
13 | { |
||
14 | /** |
||
15 | * The key that is used to generate secure signatures. |
||
16 | * |
||
17 | * @var string |
||
18 | */ |
||
19 | protected $signatureKey; |
||
20 | |||
21 | /** |
||
22 | * The URL's query parameter name for the expiration. |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $expiresParameter; |
||
27 | |||
28 | /** |
||
29 | * The URL's query parameter name for the signature. |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $signatureParameter; |
||
34 | |||
35 | /** |
||
36 | * @param string $signatureKey |
||
37 | * @param string $expiresParameter |
||
38 | * @param string $signatureParameter |
||
39 | * |
||
40 | * @throws InvalidSignatureKey |
||
41 | */ |
||
42 | public function __construct($signatureKey, $expiresParameter = 'expires', $signatureParameter = 'signature') |
||
43 | { |
||
44 | if ($signatureKey == '') { |
||
45 | throw new InvalidSignatureKey('The signature key is empty'); |
||
46 | } |
||
47 | |||
48 | $this->signatureKey = $signatureKey; |
||
49 | $this->expiresParameter = $expiresParameter; |
||
50 | $this->signatureParameter = $signatureParameter; |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Get a secure URL to a controller action. |
||
55 | * |
||
56 | * @param string $url |
||
57 | * @param \DateTime|int $expiration |
||
58 | * |
||
59 | * @return string |
||
60 | * @throws InvalidExpiration |
||
61 | */ |
||
62 | public function sign($url, $expiration) |
||
71 | |||
72 | /** |
||
73 | * Add expiration and signature query parameters to an url. |
||
74 | * |
||
75 | * @param UriInterface $url |
||
76 | * @param string $expiration |
||
77 | * @param string $signature |
||
78 | * |
||
79 | * @return \League\Url\UrlImmutable |
||
80 | */ |
||
81 | protected function signUrl(UriInterface $url, $expiration, $signature) |
||
90 | |||
91 | /** |
||
92 | * Validate a signed url. |
||
93 | * |
||
94 | * @param string $url |
||
95 | * |
||
96 | * @return bool |
||
97 | */ |
||
98 | public function validate($url) |
||
120 | |||
121 | /** |
||
122 | * Check if a query is missing a necessary parameter. |
||
123 | * |
||
124 | * @param array $query |
||
125 | * |
||
126 | * @return bool |
||
127 | */ |
||
128 | protected function isMissingAQueryParameter(array $query) |
||
140 | |||
141 | /** |
||
142 | * Check if a timestamp is in the future. |
||
143 | * |
||
144 | * @param int $timestamp |
||
145 | * |
||
146 | * @return bool |
||
147 | */ |
||
148 | protected function isFuture($timestamp) |
||
152 | |||
153 | /** |
||
154 | * Retrieve the intended URL by stripping off the UrlSigner specific parameters. |
||
155 | * |
||
156 | * @param UriInterface $url |
||
157 | * |
||
158 | * @return UriInterface |
||
159 | */ |
||
160 | protected function getIntendedUrl(UriInterface $url) |
||
169 | |||
170 | /** |
||
171 | * Retrieve the expiration timestamp for a link based on an absolute DateTime or a relative number of days. |
||
172 | * |
||
173 | * @param \DateTime|int $expiration The expiration date of this link. |
||
174 | * - DateTime: The value will be used as expiration date |
||
175 | * - int: The expiration time will be set to X days from now |
||
176 | * |
||
177 | * @throws \Spatie\UrlSigner\Exceptions\InvalidExpiration |
||
178 | * |
||
179 | * @return string |
||
180 | */ |
||
181 | protected function getExpirationTimestamp($expiration) |
||
197 | |||
198 | /** |
||
199 | * Determine if the url has a forged signature. |
||
200 | * |
||
201 | * @param UriInterface $url |
||
202 | * |
||
203 | * @return bool |
||
204 | */ |
||
205 | protected function hasValidSignature(UriInterface $url) |
||
218 | |||
219 | /** |
||
220 | * Turn a key => value associate array into a query string |
||
221 | * |
||
222 | * @param array $query |
||
223 | * |
||
224 | * @return string|null |
||
225 | */ |
||
226 | protected function buildQueryStringFromArray(array $query) |
||
235 | } |
||
236 |