|
1
|
|
|
<?php |
|
2
|
|
|
namespace Aws\CloudFront; |
|
3
|
|
|
|
|
4
|
|
|
use GuzzleHttp\Psr7; |
|
5
|
|
|
use GuzzleHttp\Psr7\Uri; |
|
6
|
|
|
use Psr\Http\Message\UriInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Creates signed URLs for Amazon CloudFront resources. |
|
10
|
|
|
*/ |
|
11
|
|
|
class UrlSigner |
|
12
|
|
|
{ |
|
13
|
|
|
private $signer; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @param $keyPairId string ID of the key pair |
|
17
|
|
|
* @param $privateKey string Path to the private key used for signing |
|
18
|
|
|
* |
|
19
|
|
|
* @throws \RuntimeException if the openssl extension is missing |
|
20
|
|
|
* @throws \InvalidArgumentException if the private key cannot be found. |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct($keyPairId, $privateKey) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->signer = new Signer($keyPairId, $privateKey); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Create a signed Amazon CloudFront URL. |
|
29
|
|
|
* |
|
30
|
|
|
* Keep in mind that URLs meant for use in media/flash players may have |
|
31
|
|
|
* different requirements for URL formats (e.g. some require that the |
|
32
|
|
|
* extension be removed, some require the file name to be prefixed |
|
33
|
|
|
* - mp4:<path>, some require you to add "/cfx/st" into your URL). |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $url URL to sign (can include query |
|
36
|
|
|
* string string and wildcards) |
|
37
|
|
|
* @param string|integer|null $expires UTC Unix timestamp used when signing |
|
38
|
|
|
* with a canned policy. Not required |
|
39
|
|
|
* when passing a custom $policy. |
|
40
|
|
|
* @param string $policy JSON policy. Use this option when |
|
41
|
|
|
* creating a signed URL for a custom |
|
42
|
|
|
* policy. |
|
43
|
|
|
* |
|
44
|
|
|
* @return string The file URL with authentication parameters |
|
45
|
|
|
* @throws \InvalidArgumentException if the URL provided is invalid |
|
46
|
|
|
* @link http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/WorkingWithStreamingDistributions.html |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getSignedUrl($url, $expires = null, $policy = null) |
|
49
|
|
|
{ |
|
50
|
|
|
// Determine the scheme of the url |
|
51
|
|
|
$urlSections = explode('://', $url); |
|
52
|
|
|
|
|
53
|
|
|
if (count($urlSections) < 2) { |
|
54
|
|
|
throw new \InvalidArgumentException("Invalid URL: {$url}"); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
// Get the real scheme by removing wildcards from the scheme |
|
58
|
|
|
$scheme = str_replace('*', '', $urlSections[0]); |
|
59
|
|
|
$uri = new Uri($scheme . '://' . $urlSections[1]); |
|
60
|
|
|
$query = Psr7\parse_query($uri->getQuery(), PHP_QUERY_RFC3986); |
|
|
|
|
|
|
61
|
|
|
$signature = $this->signer->getSignature( |
|
62
|
|
|
$this->createResource($scheme, (string) $uri), |
|
63
|
|
|
$expires, |
|
64
|
|
|
$policy |
|
65
|
|
|
); |
|
66
|
|
|
$uri = $uri->withQuery( |
|
67
|
|
|
http_build_query($query + $signature, null, '&', PHP_QUERY_RFC3986) |
|
|
|
|
|
|
68
|
|
|
); |
|
69
|
|
|
|
|
70
|
|
|
return $scheme === 'rtmp' |
|
71
|
|
|
? $this->createRtmpUrl($uri) |
|
72
|
|
|
: (string) $uri; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
private function createRtmpUrl(UriInterface $uri) |
|
76
|
|
|
{ |
|
77
|
|
|
// Use a relative URL when creating Flash player URLs |
|
78
|
|
|
$result = ltrim($uri->getPath(), '/'); |
|
79
|
|
|
|
|
80
|
|
|
if ($query = $uri->getQuery()) { |
|
81
|
|
|
$result .= '?' . $query; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $result; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param $scheme |
|
89
|
|
|
* @param $url |
|
90
|
|
|
* |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
|
|
private function createResource($scheme, $url) |
|
94
|
|
|
{ |
|
95
|
|
|
switch ($scheme) { |
|
96
|
|
|
case 'http': |
|
97
|
|
|
case 'http*': |
|
98
|
|
|
case 'https': |
|
99
|
|
|
return $url; |
|
100
|
|
|
case 'rtmp': |
|
101
|
|
|
$parts = parse_url($url); |
|
102
|
|
|
$pathParts = pathinfo($parts['path']); |
|
103
|
|
|
$resource = ltrim( |
|
104
|
|
|
$pathParts['dirname'] . '/' . $pathParts['basename'], |
|
105
|
|
|
'/' |
|
106
|
|
|
); |
|
107
|
|
|
|
|
108
|
|
|
// Add a query string if present. |
|
109
|
|
|
if (isset($parts['query'])) { |
|
110
|
|
|
$resource .= "?{$parts['query']}"; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $resource; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
throw new \InvalidArgumentException("Invalid URI scheme: {$scheme}. " |
|
117
|
|
|
. "Scheme must be one of: http, https, or rtmp"); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|