-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathAssetFinalizerTrait.php
More file actions
349 lines (307 loc) · 10.7 KB
/
Copy pathAssetFinalizerTrait.php
File metadata and controls
349 lines (307 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
/**
* This file is part of the Cloudinary PHP package.
*
* (c) Cloudinary
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cloudinary\Asset;
use Cloudinary\ArrayUtils;
use Cloudinary\Configuration\UrlConfig;
use Cloudinary\Exception\ConfigurationException;
use Cloudinary\StringUtils;
use Cloudinary\Utils;
use GuzzleHttp\Psr7\Uri;
use Psr\Http\Message\UriInterface;
use UnexpectedValueException;
/**
* Trait AssetFinalizerTrait
*
* @property AssetDescriptor $asset
* @property AuthToken $authToken
*/
trait AssetFinalizerTrait
{
/**
*
* Helper function for building hostname of form:
* subDomainPrefix-subDomain-subDomainSuffix.domain
* For example:
* cloudname-res-3.cloudinary.com
*
* @param null $subDomainPrefix
* @param null $subDomainSuffix
*
* @return string Resulting host name
* @internal
*/
private static function buildHostName(
$subDomainPrefix = null,
$subDomainSuffix = null,
?string $subDomain = UrlConfig::DEFAULT_SUB_DOMAIN,
?string $domain = UrlConfig::DEFAULT_DOMAIN
): string {
return implode(
'.',
array_filter(
[
implode('-', array_filter([$subDomainPrefix, $subDomain, $subDomainSuffix])),
$domain,
]
)
);
}
/**
* Computes the domain shard from the source.
*
* @param string $source The source.
*
* @return int between 1 and 5
*/
private static function domainShard(string $source): int
{
return (crc32($source) % 5 + 5) % 5 + 1;
}
/**
* Builds the hostname for the asset distribution.
*
* 1) Customers in shared distribution (e.g. res.cloudinary.com)
* If cdn_domain is true uses res-[1-5].cloudinary.com for both http and https.
* Setting secure_cdn_subdomain to false disables this for https.
* 2) Customers with private cdn
* If cdn_domain is true uses cloudname-res-[1-5].cloudinary.com for http
* If secure_cdn_domain is true uses cloudname-res-[1-5].cloudinary.com for https
* (please contact support if you require this)
* 3) Customers with cname
* If cdn_domain is true uses a[1-5].cname for http.
* For https, uses the same naming scheme as 1 for shared distribution and as 2 for private distribution.
*
*/
protected function finalizeDistribution(): string
{
$useSharedHost = ! $this->urlConfig->privateCdn;
if ($this->urlConfig->secure) {
$protocol = UrlConfig::PROTOCOL_HTTPS;
$hostName = $this->urlConfig->secureCname;
if (empty($hostName)) {
if ($this->urlConfig->privateCdn) {
$hostName = self::buildHostName($this->cloud->cloudName);
} else {
$hostName = UrlConfig::DEFAULT_SHARED_HOST;
$useSharedHost = true;
}
}
$secureCdnSubDomain = $this->urlConfig->secureCdnSubdomain;
if ($useSharedHost && $secureCdnSubDomain === null) {
$secureCdnSubDomain = $this->urlConfig->cdnSubdomain;
}
if ($secureCdnSubDomain) {
$hostName = str_replace(
UrlConfig::DEFAULT_SHARED_HOST,
self::buildHostName(null, self::domainShard($this->asset->publicId())),
$hostName
);
}
} else {
$protocol = UrlConfig::PROTOCOL_HTTP;
if ($this->urlConfig->cname) {
$hostName = self::buildHostName(
null,
null,
$this->urlConfig->cdnSubdomain ? 'a' . self::domainShard($this->asset->publicId()) : null,
$this->urlConfig->cname
);
} else {
$hostName = self::buildHostName(
$this->urlConfig->privateCdn ? $this->cloud->cloudName : null,
$this->urlConfig->cdnSubdomain ? self::domainShard($this->asset->publicId()) : null
);
}
}
$distribution = "$protocol://$hostName";
if ($useSharedHost) {
$distribution .= "/{$this->cloud->cloudName}";
}
return $distribution;
}
/**
* Finalizes the asset type.
*
* Used for SEO optimization.
*
* @see https://cloudinary.com/documentation/advanced_url_delivery_options#seo_friendly_media_asset_urls
*
* @throws UnexpectedValueException
*/
protected function finalizeAssetType(): mixed
{
if (! empty($this->asset->suffix)) {
$suffixSupportedDeliveryTypes = static::getSuffixSupportedDeliveryTypes();
if (! isset($suffixSupportedDeliveryTypes[$this->asset->assetType][$this->asset->deliveryType])) {
$message = "URL Suffix is only supported in {$this->asset->assetType} " .
implode(
',',
array_keys(ArrayUtils::get($suffixSupportedDeliveryTypes, [$this->asset->assetType], []))
);
$this->getLogger()->critical(
$message,
[
'suffix' => $this->asset->suffix,
'assetType' => $this->asset->assetType,
'deliveryType' => $this->asset->deliveryType,
]
);
throw new UnexpectedValueException($message);
}
$finalAssetType = $suffixSupportedDeliveryTypes[$this->asset->assetType][$this->asset->deliveryType];
} else {
$finalAssetType = ArrayUtils::implodeUrl([$this->asset->assetType, $this->asset->deliveryType]);
}
return $finalAssetType;
}
/**
* Finalizes asset source.
*
*/
protected function finalizeSource(): string
{
$source = $this->asset->publicId(true);
if (stripos($source, 'http:/') !== 0 && stripos($source, 'https:/') !== 0) {
$source = rawurldecode($source);
}
$source = StringUtils::smartEscape($source);
if (! empty($this->asset->suffix)) {
$source = "{$source}/{$this->asset->suffix}";
}
if (! empty($this->asset->extension)) {
$source = "{$source}.{$this->asset->extension}";
}
return $source;
}
/**
* Finalizes version part of the asset URL.
*
*/
protected function finalizeVersion(): ?string
{
$version = $this->asset->version;
if (empty($version) && $this->urlConfig->forceVersion
&& ! empty($this->asset->location)
) {
$publicId = $this->asset->publicId();
if (strncmp($publicId, 'http:/', 6) !== 0
&& strncmp($publicId, 'https:/', 7) !== 0
&& ! preg_match('/^v\d+/', $publicId)
) {
$version = '1';
}
}
return $version ? 'v' . $version : null;
}
/**
* Finalizes URL signature.
*
* @see https://cloudinary.com/documentation/advanced_url_delivery_options#generating_delivery_url_signatures
*
* @throws ConfigurationException
*/
protected function finalizeSimpleSignature(): string
{
if (! $this->urlConfig->signUrl || $this->authToken->isEnabled()) {
return '';
}
if (empty($this->cloud->apiSecret)) {
throw new ConfigurationException('Must supply apiSecret');
}
$toSign = $this->asset->publicId();
$signature = StringUtils::base64UrlEncode(
Utils::sign(
$toSign,
$this->cloud->apiSecret,
true,
$this->getSignatureAlgorithm()
)
);
return Utils::formatSimpleSignature(
$signature,
$this->urlConfig->longUrlSignature ? Utils::LONG_URL_SIGNATURE_LENGTH : Utils::SHORT_URL_SIGNATURE_LENGTH
);
}
/**
* Check if passed signatureAlgorithm is supported otherwise return SHA1.
*
*/
protected function getSignatureAlgorithm(): string
{
if ($this->urlConfig->longUrlSignature) {
return Utils::ALGO_SHA256;
}
if (ArrayUtils::inArrayI($this->cloud->signatureAlgorithm, Utils::SUPPORTED_SIGNATURE_ALGORITHMS)) {
return $this->cloud->signatureAlgorithm;
}
return Utils::ALGO_SHA1;
}
/**
* Finalizes URL.
*
* @param string $urlStr The URL to finalize.
*
* @return UriInterface The resulting URL.
*/
protected function finalizeUrl(string $urlStr): UriInterface
{
$urlParts = parse_url($urlStr);
$urlParts = $this->finalizeUrlWithAuthToken($urlParts);
$urlParts = $this->finalizeUrlWithAnalytics($urlParts);
return Uri::fromParts($urlParts);
}
/**
* Finalizes URL signature, when AuthToken is used.
*
* @param array $urlParts The URL parts to sign.
*
* @return array resulting URL parts
*/
protected function finalizeUrlWithAuthToken(array $urlParts): array
{
if (! $this->urlConfig->signUrl || ! $this->authToken->isEnabled()) {
return $urlParts;
}
$token = $this->authToken->generate($urlParts['path']);
$urlParts['query'] = ArrayUtils::implodeAssoc(
ArrayUtils::mergeNonEmpty(
StringUtils::parseQueryString(ArrayUtils::get($urlParts, 'query')),
StringUtils::parseQueryString($token)
),
'='
);
return $urlParts;
}
/**
* Finalizes URL with analytics data.
*
* @param array $urlParts The URL to add analytics to.
*
* @return array resulting URL
*/
protected function finalizeUrlWithAnalytics(array $urlParts): array
{
if (! $this->urlConfig->analytics) {
return $urlParts;
}
// Disable analytics for public IDs containing query params.
if (! empty($urlParts['query']) || StringUtils::contains($this->asset->publicId(), '?')) {
return $urlParts;
}
$urlParts['query'] = ArrayUtils::implodeAssoc(
ArrayUtils::mergeNonEmpty(
StringUtils::parseQueryString(ArrayUtils::get($urlParts, 'query')),
[Analytics::QUERY_KEY, Analytics::sdkAnalyticsSignature()]
),
'='
);
return $urlParts;
}
}