123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- <?php
- namespace Aws\CloudFront;
- use GuzzleHttp\Psr7;
- use GuzzleHttp\Psr7\Uri;
- use Psr\Http\Message\UriInterface;
- class UrlSigner
- {
- private $signer;
-
- public function __construct($keyPairId, $privateKey)
- {
- $this->signer = new Signer($keyPairId, $privateKey);
- }
-
- public function getSignedUrl($url, $expires = null, $policy = null)
- {
-
- $urlSections = explode('://', $url);
- if (count($urlSections) < 2) {
- throw new \InvalidArgumentException("Invalid URL: {$url}");
- }
-
- $scheme = str_replace('*', '', $urlSections[0]);
- $uri = new Uri($scheme . '://' . $urlSections[1]);
- $query = Psr7\Query::parse($uri->getQuery(), PHP_QUERY_RFC3986);
- $signature = $this->signer->getSignature(
- $this->createResource($scheme, (string) $uri),
- $expires,
- $policy
- );
- $uri = $uri->withQuery(
- http_build_query($query + $signature, '', '&', PHP_QUERY_RFC3986)
- );
- return $scheme === 'rtmp'
- ? $this->createRtmpUrl($uri)
- : (string) $uri;
- }
- private function createRtmpUrl(UriInterface $uri)
- {
-
- $result = ltrim($uri->getPath(), '/');
- if ($query = $uri->getQuery()) {
- $result .= '?' . $query;
- }
- return $result;
- }
-
- private function createResource($scheme, $url)
- {
- switch ($scheme) {
- case 'http':
- case 'http*':
- case 'https':
- return $url;
- case 'rtmp':
- $parts = parse_url($url);
- $pathParts = pathinfo($parts['path']);
- $resource = ltrim(
- $pathParts['dirname'] . '/' . $pathParts['basename'],
- '/'
- );
-
- if (isset($parts['query'])) {
- $resource .= "?{$parts['query']}";
- }
- return $resource;
- }
- throw new \InvalidArgumentException("Invalid URI scheme: {$scheme}. "
- . "Scheme must be one of: http, https, or rtmp");
- }
- }
|