PluginProbe
Media Cloud Sync / 1.2.13
Media Cloud Sync v1.2.13
1.4.2 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 All 36 releases
media-cloud-sync / includes / sdk / s3 / Aws / EndpointV2 / Ruleset / RulesetStandardLibrary.php

RulesetStandardLibrary.php in Media Cloud Sync 1.2.13, at includes/sdk/s3/Aws/EndpointV2/Ruleset/RulesetStandardLibrary.php

337 lines 12.1 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\EndpointV2\Ruleset;
4
5 use Dudlewebs\WPMCS\s3\Aws\Exception\UnresolvedEndpointException;
6 /**
7 * Provides functions and actions to be performed for endpoint evaluation.
8 * This is an internal only class and is not subject to backwards-compatibility guarantees.
9 *
10 * @internal
11 */
12 class RulesetStandardLibrary
13 {
14 const IPV4_RE = '/^(?:[0-9]{1,3}\\.){3}[0-9]{1,3}$/';
15 const IPV6_RE = '/([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|
16 . ([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]
17 . {1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:)
18 . {1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|
19 . [0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:
20 . (:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|
21 . 1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]
22 . {1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]
23 . |1{0,1}[0-9]){0,1}[0-9])/';
24 const TEMPLATE_ESCAPE_RE = '/{\\{\\s*(.*?)\\s*\\}\\}/';
25 const TEMPLATE_SEARCH_RE = '/\\{[a-zA-Z#]+\\}/';
26 const TEMPLATE_PARSE_RE = '#\\{((?>[^\\{\\}]+)|(?R))*\\}#x';
27 const HOST_LABEL_RE = '/^(?!-)[a-zA-Z\\d-]{1,63}(?<!-)$/';
28 private $partitions;
29 public function __construct($partitions)
30 {
31 $this->partitions = $partitions;
32 }
33 /**
34 * Determines if a value is set.
35 *
36 * @return boolean
37 */
38 public function is_set($value)
39 {
40 return isset($value);
41 }
42 /**
43 * Function implementation of logical operator `not`
44 *
45 * @return boolean
46 */
47 public function not($value)
48 {
49 return !$value;
50 }
51 /**
52 * Find an attribute within a value given a path string.
53 *
54 * @return mixed
55 */
56 public function getAttr($from, $path)
57 {
58 $parts = \explode('.', $path);
59 foreach ($parts as $part) {
60 $sliceIdx = \strpos($part, '[');
61 if ($sliceIdx !== \false) {
62 if (\substr($part, -1) !== ']') {
63 return null;
64 }
65 $slice = \intval(\substr($part, $sliceIdx + 1, \strlen($part) - 1));
66 $from = isset($from[\substr($part, 0, $sliceIdx)][$slice]) ? $from[\substr($part, 0, $sliceIdx)][$slice] : null;
67 } else {
68 $from = $from[$part];
69 }
70 }
71 return $from;
72 }
73 /**
74 * Computes a substring given the start index and end index. If `reverse` is
75 * true, slice the string from the end instead.
76 *
77 * @return mixed
78 */
79 public function substring($input, $start, $stop, $reverse)
80 {
81 if (!\is_string($input)) {
82 throw new UnresolvedEndpointException('Input passed to `substring` must be `string`.');
83 }
84 if (\preg_match('/[^\\x00-\\x7F]/', $input)) {
85 return null;
86 }
87 if ($start >= $stop or \strlen($input) < $stop) {
88 return null;
89 }
90 if (!$reverse) {
91 return \substr($input, $start, $stop - $start);
92 } else {
93 $offset = \strlen($input) - $stop;
94 $length = $stop - $start;
95 return \substr($input, $offset, $length);
96 }
97 }
98 /**
99 * Evaluates two strings for equality.
100 *
101 * @return boolean
102 */
103 public function stringEquals($string1, $string2)
104 {
105 if (!\is_string($string1) || !\is_string($string2)) {
106 throw new UnresolvedEndpointException('Values passed to StringEquals must be `string`.');
107 }
108 return $string1 === $string2;
109 }
110 /**
111 * Evaluates two booleans for equality.
112 *
113 * @return boolean
114 */
115 public function booleanEquals($boolean1, $boolean2)
116 {
117 return \filter_var($boolean1, \FILTER_VALIDATE_BOOLEAN) === \filter_var($boolean2, \FILTER_VALIDATE_BOOLEAN);
118 }
119 /**
120 * Percent-encodes an input string.
121 *
122 * @return mixed
123 */
124 public function uriEncode($input)
125 {
126 if (\is_null($input)) {
127 return null;
128 }
129 return \str_replace('%7E', '~', \rawurlencode($input));
130 }
131 /**
132 * Parses URL string into components.
133 *
134 * @return mixed
135 */
136 public function parseUrl($url)
137 {
138 if (\is_null($url)) {
139 return null;
140 }
141 $parsed = \parse_url($url);
142 if ($parsed === \false || !empty($parsed['query'])) {
143 return null;
144 } elseif (!isset($parsed['scheme'])) {
145 return null;
146 }
147 if ($parsed['scheme'] !== 'http' && $parsed['scheme'] !== 'https') {
148 return null;
149 }
150 $urlInfo = [];
151 $urlInfo['scheme'] = $parsed['scheme'];
152 $urlInfo['authority'] = isset($parsed['host']) ? $parsed['host'] : '';
153 if (isset($parsed['port'])) {
154 $urlInfo['authority'] = $urlInfo['authority'] . ":" . $parsed['port'];
155 }
156 $urlInfo['path'] = isset($parsed['path']) ? $parsed['path'] : '';
157 $urlInfo['normalizedPath'] = !empty($parsed['path']) ? \rtrim($urlInfo['path'] ?: '', '/' . "/") . '/' : '/';
158 $urlInfo['isIp'] = !isset($parsed['host']) ? 'false' : $this->isValidIp($parsed['host']);
159 return $urlInfo;
160 }
161 /**
162 * Evaluates whether a value is a valid host label per
163 * RFC 1123. If allow_subdomains is true, split on `.` and validate
164 * each subdomain separately.
165 *
166 * @return boolean
167 */
168 public function isValidHostLabel($hostLabel, $allowSubDomains)
169 {
170 if (!isset($hostLabel) || !$allowSubDomains && \strpos($hostLabel, '.') != \false) {
171 return \false;
172 }
173 if ($allowSubDomains) {
174 foreach (\explode('.', $hostLabel) as $subdomain) {
175 if (!$this->validateHostLabel($subdomain)) {
176 return \false;
177 }
178 }
179 return \true;
180 } else {
181 return $this->validateHostLabel($hostLabel);
182 }
183 }
184 /**
185 * Parse and validate string for ARN components.
186 *
187 * @return array|null
188 */
189 public function parseArn($arnString)
190 {
191 if (\is_null($arnString) || \substr($arnString, 0, 3) !== "arn") {
192 return null;
193 }
194 $arn = [];
195 $parts = \explode(':', $arnString, 6);
196 if (\sizeof($parts) < 6) {
197 return null;
198 }
199 $arn['partition'] = isset($parts[1]) ? $parts[1] : null;
200 $arn['service'] = isset($parts[2]) ? $parts[2] : null;
201 $arn['region'] = isset($parts[3]) ? $parts[3] : null;
202 $arn['accountId'] = isset($parts[4]) ? $parts[4] : null;
203 $arn['resourceId'] = isset($parts[5]) ? $parts[5] : null;
204 if (empty($arn['partition']) || empty($arn['service']) || empty($arn['resourceId'])) {
205 return null;
206 }
207 $resource = $arn['resourceId'];
208 $arn['resourceId'] = \preg_split("/[:\\/]/", $resource);
209 return $arn;
210 }
211 /**
212 * Matches a region string to an AWS partition.
213 *
214 * @return mixed
215 */
216 public function partition($region)
217 {
218 if (!\is_string($region)) {
219 throw new UnresolvedEndpointException('Value passed to `partition` must be `string`.');
220 }
221 $partitions = $this->partitions;
222 foreach ($partitions['partitions'] as $partition) {
223 if (\array_key_exists($region, $partition['regions']) || \preg_match("/{$partition['regionRegex']}/", $region)) {
224 return $partition['outputs'];
225 }
226 }
227 //return `aws` partition if no match is found.
228 return $partitions['partitions'][0]['outputs'];
229 }
230 /**
231 * Evaluates whether a value is a valid bucket name for virtual host
232 * style bucket URLs.
233 *
234 * @return boolean
235 */
236 public function isVirtualHostableS3Bucket($bucketName, $allowSubdomains)
237 {
238 if (\is_null($bucketName) || (\strlen($bucketName) < 3 || \strlen($bucketName) > 63) || \preg_match(self::IPV4_RE, $bucketName) || \strtolower($bucketName) !== $bucketName) {
239 return \false;
240 }
241 if ($allowSubdomains) {
242 $labels = \explode('.', $bucketName);
243 $results = [];
244 foreach ($labels as $label) {
245 $results[] = $this->isVirtualHostableS3Bucket($label, \false);
246 }
247 return !\in_array(\false, $results);
248 }
249 return $this->isValidHostLabel($bucketName, \false);
250 }
251 public function callFunction($funcCondition, &$inputParameters)
252 {
253 $funcArgs = [];
254 foreach ($funcCondition['argv'] as $arg) {
255 $funcArgs[] = $this->resolveValue($arg, $inputParameters);
256 }
257 $funcName = \str_replace('aws.', '', $funcCondition['fn']);
258 if ($funcName === 'isSet') {
259 $funcName = 'is_set';
260 }
261 $result = \call_user_func_array([RulesetStandardLibrary::class, $funcName], $funcArgs);
262 if (isset($funcCondition['assign'])) {
263 $assign = $funcCondition['assign'];
264 if (isset($inputParameters[$assign])) {
265 throw new UnresolvedEndpointException("Assignment `{$assign}` already exists in input parameters" . " or has already been assigned by an endpoint rule and cannot be overwritten.");
266 }
267 $inputParameters[$assign] = $result;
268 }
269 return $result;
270 }
271 public function resolveValue($value, $inputParameters)
272 {
273 //Given a value, check if it's a function, reference or template.
274 //returns resolved value
275 if ($this->isFunc($value)) {
276 return $this->callFunction($value, $inputParameters);
277 } elseif ($this->isRef($value)) {
278 return isset($inputParameters[$value['ref']]) ? $inputParameters[$value['ref']] : null;
279 } elseif ($this->isTemplate($value)) {
280 return $this->resolveTemplateString($value, $inputParameters);
281 }
282 return $value;
283 }
284 public function isFunc($arg)
285 {
286 return \is_array($arg) && isset($arg['fn']);
287 }
288 public function isRef($arg)
289 {
290 return \is_array($arg) && isset($arg['ref']);
291 }
292 public function isTemplate($arg)
293 {
294 return \is_string($arg) && !empty(\preg_match(self::TEMPLATE_SEARCH_RE, $arg));
295 }
296 public function resolveTemplateString($value, $inputParameters)
297 {
298 return \preg_replace_callback(self::TEMPLATE_PARSE_RE, function ($match) use($inputParameters) {
299 if (\preg_match(self::TEMPLATE_ESCAPE_RE, $match[0])) {
300 return $match[1];
301 }
302 $notFoundMessage = 'Resolved value was null. Please check rules and ' . 'input parameters and try again.';
303 $parts = \explode("#", $match[1]);
304 if (\count($parts) > 1) {
305 $resolvedValue = $inputParameters;
306 foreach ($parts as $part) {
307 if (!isset($resolvedValue[$part])) {
308 throw new UnresolvedEndpointException($notFoundMessage);
309 }
310 $resolvedValue = $resolvedValue[$part];
311 }
312 return $resolvedValue;
313 } else {
314 if (!isset($inputParameters[$parts[0]])) {
315 throw new UnresolvedEndpointException($notFoundMessage);
316 }
317 return $inputParameters[$parts[0]];
318 }
319 }, $value);
320 }
321 private function validateHostLabel($hostLabel)
322 {
323 if (empty($hostLabel) || \strlen($hostLabel) > 63) {
324 return \false;
325 }
326 if (\preg_match(self::HOST_LABEL_RE, $hostLabel)) {
327 return \true;
328 }
329 return \false;
330 }
331 private function isValidIp($hostName)
332 {
333 $isWrapped = \strpos($hostName, '[') === 0 && \strrpos($hostName, ']') === \strlen($hostName) - 1;
334 return \preg_match(self::IPV4_RE, $hostName) || $isWrapped && \preg_match(self::IPV6_RE, $hostName) ? 'true' : 'false';
335 }
336 }
337