PluginProbe
Media Cloud Sync / 1.4.1
Media Cloud Sync v1.4.1
1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 All 35 releases
media-cloud-sync / includes / sdk / s3 / Aws / CloudFront / UrlSigner.php

UrlSigner.php in Media Cloud Sync 1.4.1, at includes/sdk/s3/Aws/CloudFront/UrlSigner.php

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