PluginProbe
Media Cloud Sync / 1.3.11
Media Cloud Sync v1.3.11
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 1.3.0 All 34 releases
media-cloud-sync / includes / sdk / s3 / Aws / UserAgentMiddleware.php

UserAgentMiddleware.php in Media Cloud Sync 1.3.11, at includes/sdk/s3/Aws/UserAgentMiddleware.php

211 lines 6.2 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;
4
5 use Dudlewebs\WPMCS\s3\Aws\EndpointDiscovery\Configuration;
6 use Closure;
7 use Dudlewebs\WPMCS\s3\Psr\Http\Message\RequestInterface;
8 /**
9 * Builds and injects the user agent header values.
10 * This middleware must be appended into step where all the
11 * metrics to be gathered are already resolved. As of now it should be
12 * after the signing step.
13 */
14 class UserAgentMiddleware
15 {
16 const AGENT_VERSION = 2.1;
17 static $userAgentFnList = ['getSdkVersion', 'getUserAgentVersion', 'getHhvmVersion', 'getOsName', 'getLangVersion', 'getExecEnv', 'getEndpointDiscovery', 'getAppId', 'getMetrics'];
18 /** @var callable */
19 private $nextHandler;
20 /** @var array */
21 private $args;
22 /** @var MetricsBuilder */
23 private $metricsBuilder;
24 /**
25 * Returns a middleware wrapper function.
26 *
27 * @param array $args
28 *
29 * @return Closure
30 */
31 public static function wrap(array $args) : Closure
32 {
33 return function (callable $handler) use($args) {
34 return new self($handler, $args);
35 };
36 }
37 /**
38 * @param callable $nextHandler
39 * @param array $args
40 */
41 public function __construct(callable $nextHandler, array $args = [])
42 {
43 $this->nextHandler = $nextHandler;
44 $this->args = $args;
45 }
46 /**
47 * When invoked, its injects the user agent header into the
48 * request headers.
49 *
50 * @param CommandInterface $command
51 * @param RequestInterface $request
52 *
53 * @return mixed
54 */
55 public function __invoke(CommandInterface $command, RequestInterface $request)
56 {
57 $handler = $this->nextHandler;
58 $this->metricsBuilder = MetricsBuilder::fromCommand($command);
59 $request = $this->requestWithUserAgentHeader($request);
60 return $handler($command, $request);
61 }
62 /**
63 * Builds the user agent header value, and injects it into the request
64 * headers. Then, it returns the mutated request.
65 *
66 * @param RequestInterface $request
67 *
68 * @return RequestInterface
69 */
70 private function requestWithUserAgentHeader(RequestInterface $request) : RequestInterface
71 {
72 $uaAppend = $this->args['ua_append'] ?? [];
73 $userAgentValue = \array_merge($this->buildUserAgentValue(), $uaAppend);
74 // It includes the user agent values just for the User-Agent header.
75 // The reason is that the SEP does not mention appending the
76 // metrics into the X-Amz-User-Agent header.
77 return $request->withHeader('User-Agent', \implode(' ', \array_merge($userAgentValue, $request->getHeader('User-Agent'))));
78 }
79 /**
80 * Builds the different user agent values.
81 *
82 * @return array
83 */
84 private function buildUserAgentValue() : array
85 {
86 $userAgentValue = [];
87 foreach (self::$userAgentFnList as $fn) {
88 $val = $this->{$fn}();
89 if (!empty($val)) {
90 $userAgentValue[] = $val;
91 }
92 }
93 return $userAgentValue;
94 }
95 /**
96 * Returns the user agent value for SDK version.
97 *
98 * @return string
99 */
100 private function getSdkVersion() : string
101 {
102 return 'aws-sdk-php/' . Sdk::VERSION;
103 }
104 /**
105 * Returns the user agent value for the agent version.
106 *
107 * @return string
108 */
109 private function getUserAgentVersion() : string
110 {
111 return 'ua/' . self::AGENT_VERSION;
112 }
113 /**
114 * Returns the user agent value for the hhvm version, but just
115 * when it is defined.
116 *
117 * @return string
118 */
119 private function getHhvmVersion() : string
120 {
121 if (\defined('Dudlewebs\\WPMCS\\s3\\HHVM_VERSION')) {
122 return 'HHVM/' . HHVM_VERSION;
123 }
124 return "";
125 }
126 /**
127 * Returns the user agent value for the os version.
128 *
129 * @return string
130 */
131 private function getOsName() : string
132 {
133 $disabledFunctions = \explode(',', \ini_get('disable_functions'));
134 if (\function_exists('php_uname') && !\in_array('php_uname', $disabledFunctions, \true)) {
135 $osName = "OS/" . \php_uname('s') . '#' . \php_uname('r');
136 if (!empty($osName)) {
137 return $osName;
138 }
139 }
140 return "";
141 }
142 /**
143 * Returns the user agent value for the php language used.
144 *
145 * @return string
146 */
147 private function getLangVersion() : string
148 {
149 return 'lang/php#' . \phpversion();
150 }
151 /**
152 * Returns the user agent value for the execution env.
153 *
154 * @return string
155 */
156 private function getExecEnv() : string
157 {
158 if ($executionEnvironment = \getenv('AWS_EXECUTION_ENV')) {
159 return $executionEnvironment;
160 }
161 return "";
162 }
163 /**
164 * Returns the user agent value for endpoint discovery as cfg.
165 * This feature is deprecated.
166 *
167 * @return string
168 */
169 private function getEndpointDiscovery() : string
170 {
171 $args = $this->args;
172 if (isset($args['endpoint_discovery'])) {
173 if ($args['endpoint_discovery'] instanceof Configuration && $args['endpoint_discovery']->isEnabled()) {
174 return 'cfg/endpoint-discovery';
175 } elseif (\is_array($args['endpoint_discovery']) && isset($args['endpoint_discovery']['enabled']) && $args['endpoint_discovery']['enabled']) {
176 return 'cfg/endpoint-discovery';
177 }
178 }
179 return "";
180 }
181 /**
182 * Returns the user agent value for app id, but just when an
183 * app id was provided as a client argument.
184 *
185 * @return string
186 */
187 private function getAppId() : string
188 {
189 if (empty($this->args['app_id'])) {
190 return "";
191 }
192 return 'app/' . $this->args['app_id'];
193 }
194 /**
195 * Returns the user agent value for metrics.
196 *
197 * @return string
198 */
199 private function getMetrics() : string
200 {
201 // Resolve first metrics related to client arguments.
202 $this->metricsBuilder->resolveAndAppendFromArgs($this->args);
203 // Build the metrics.
204 $metricsEncoded = $this->metricsBuilder->build();
205 if (empty($metricsEncoded)) {
206 return "";
207 }
208 return "m/" . $metricsEncoded;
209 }
210 }
211