Mercurial > nebulaweb3
comparison default/assets/vendors/theme-widgets/vendor/abraham/twitteroauth/src/Request.php @ 0:1d038bc9b3d2 default tip
Up:default
author | Liny <dev@neowd.com> |
---|---|
date | Sat, 31 May 2025 09:21:51 +0800 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:1d038bc9b3d2 |
---|---|
1 <?php | |
2 /** | |
3 * The MIT License | |
4 * Copyright (c) 2007 Andy Smith | |
5 */ | |
6 namespace Abraham\TwitterOAuth; | |
7 | |
8 class Request | |
9 { | |
10 protected $parameters; | |
11 protected $httpMethod; | |
12 protected $httpUrl; | |
13 public static $version = '1.0'; | |
14 | |
15 /** | |
16 * Constructor | |
17 * | |
18 * @param string $httpMethod | |
19 * @param string $httpUrl | |
20 * @param array|null $parameters | |
21 */ | |
22 public function __construct($httpMethod, $httpUrl, array $parameters = []) | |
23 { | |
24 $parameters = array_merge(Util::parseParameters(parse_url($httpUrl, PHP_URL_QUERY)), $parameters); | |
25 $this->parameters = $parameters; | |
26 $this->httpMethod = $httpMethod; | |
27 $this->httpUrl = $httpUrl; | |
28 } | |
29 | |
30 /** | |
31 * pretty much a helper function to set up the request | |
32 * | |
33 * @param Consumer $consumer | |
34 * @param Token $token | |
35 * @param string $httpMethod | |
36 * @param string $httpUrl | |
37 * @param array $parameters | |
38 * | |
39 * @return Request | |
40 */ | |
41 public static function fromConsumerAndToken( | |
42 Consumer $consumer, | |
43 Token $token = null, | |
44 $httpMethod, | |
45 $httpUrl, | |
46 array $parameters = [] | |
47 ) { | |
48 $defaults = [ | |
49 "oauth_version" => Request::$version, | |
50 "oauth_nonce" => Request::generateNonce(), | |
51 "oauth_timestamp" => time(), | |
52 "oauth_consumer_key" => $consumer->key | |
53 ]; | |
54 if (null !== $token) { | |
55 $defaults['oauth_token'] = $token->key; | |
56 } | |
57 | |
58 $parameters = array_merge($defaults, $parameters); | |
59 | |
60 return new Request($httpMethod, $httpUrl, $parameters); | |
61 } | |
62 | |
63 /** | |
64 * @param string $name | |
65 * @param string $value | |
66 */ | |
67 public function setParameter($name, $value) | |
68 { | |
69 $this->parameters[$name] = $value; | |
70 } | |
71 | |
72 /** | |
73 * @param $name | |
74 * | |
75 * @return string|null | |
76 */ | |
77 public function getParameter($name) | |
78 { | |
79 return isset($this->parameters[$name]) ? $this->parameters[$name] : null; | |
80 } | |
81 | |
82 /** | |
83 * @return array | |
84 */ | |
85 public function getParameters() | |
86 { | |
87 return $this->parameters; | |
88 } | |
89 | |
90 /** | |
91 * @param $name | |
92 */ | |
93 public function removeParameter($name) | |
94 { | |
95 unset($this->parameters[$name]); | |
96 } | |
97 | |
98 /** | |
99 * The request parameters, sorted and concatenated into a normalized string. | |
100 * | |
101 * @return string | |
102 */ | |
103 public function getSignableParameters() | |
104 { | |
105 // Grab all parameters | |
106 $params = $this->parameters; | |
107 | |
108 // Remove oauth_signature if present | |
109 // Ref: Spec: 9.1.1 ("The oauth_signature parameter MUST be excluded.") | |
110 if (isset($params['oauth_signature'])) { | |
111 unset($params['oauth_signature']); | |
112 } | |
113 | |
114 return Util::buildHttpQuery($params); | |
115 } | |
116 | |
117 /** | |
118 * Returns the base string of this request | |
119 * | |
120 * The base string defined as the method, the url | |
121 * and the parameters (normalized), each urlencoded | |
122 * and the concated with &. | |
123 * | |
124 * @return string | |
125 */ | |
126 public function getSignatureBaseString() | |
127 { | |
128 $parts = [ | |
129 $this->getNormalizedHttpMethod(), | |
130 $this->getNormalizedHttpUrl(), | |
131 $this->getSignableParameters() | |
132 ]; | |
133 | |
134 $parts = Util::urlencodeRfc3986($parts); | |
135 | |
136 return implode('&', $parts); | |
137 } | |
138 | |
139 /** | |
140 * Returns the HTTP Method in uppercase | |
141 * | |
142 * @return string | |
143 */ | |
144 public function getNormalizedHttpMethod() | |
145 { | |
146 return strtoupper($this->httpMethod); | |
147 } | |
148 | |
149 /** | |
150 * parses the url and rebuilds it to be | |
151 * scheme://host/path | |
152 * | |
153 * @return string | |
154 */ | |
155 public function getNormalizedHttpUrl() | |
156 { | |
157 $parts = parse_url($this->httpUrl); | |
158 | |
159 $scheme = $parts['scheme']; | |
160 $host = strtolower($parts['host']); | |
161 $path = $parts['path']; | |
162 | |
163 return "$scheme://$host$path"; | |
164 } | |
165 | |
166 /** | |
167 * Builds a url usable for a GET request | |
168 * | |
169 * @return string | |
170 */ | |
171 public function toUrl() | |
172 { | |
173 $postData = $this->toPostdata(); | |
174 $out = $this->getNormalizedHttpUrl(); | |
175 if ($postData) { | |
176 $out .= '?' . $postData; | |
177 } | |
178 return $out; | |
179 } | |
180 | |
181 /** | |
182 * Builds the data one would send in a POST request | |
183 * | |
184 * @return string | |
185 */ | |
186 public function toPostdata() | |
187 { | |
188 return Util::buildHttpQuery($this->parameters); | |
189 } | |
190 | |
191 /** | |
192 * Builds the Authorization: header | |
193 * | |
194 * @return string | |
195 * @throws TwitterOAuthException | |
196 */ | |
197 public function toHeader() | |
198 { | |
199 $first = true; | |
200 $out = 'Authorization: OAuth'; | |
201 foreach ($this->parameters as $k => $v) { | |
202 if (substr($k, 0, 5) != "oauth") { | |
203 continue; | |
204 } | |
205 if (is_array($v)) { | |
206 throw new TwitterOAuthException('Arrays not supported in headers'); | |
207 } | |
208 $out .= ($first) ? ' ' : ', '; | |
209 $out .= Util::urlencodeRfc3986($k) . '="' . Util::urlencodeRfc3986($v) . '"'; | |
210 $first = false; | |
211 } | |
212 return $out; | |
213 } | |
214 | |
215 /** | |
216 * @return string | |
217 */ | |
218 public function __toString() | |
219 { | |
220 return $this->toUrl(); | |
221 } | |
222 | |
223 /** | |
224 * @param SignatureMethod $signatureMethod | |
225 * @param Consumer $consumer | |
226 * @param Token $token | |
227 */ | |
228 public function signRequest(SignatureMethod $signatureMethod, Consumer $consumer, Token $token = null) | |
229 { | |
230 $this->setParameter("oauth_signature_method", $signatureMethod->getName()); | |
231 $signature = $this->buildSignature($signatureMethod, $consumer, $token); | |
232 $this->setParameter("oauth_signature", $signature); | |
233 } | |
234 | |
235 /** | |
236 * @param SignatureMethod $signatureMethod | |
237 * @param Consumer $consumer | |
238 * @param Token $token | |
239 * | |
240 * @return string | |
241 */ | |
242 public function buildSignature(SignatureMethod $signatureMethod, Consumer $consumer, Token $token = null) | |
243 { | |
244 return $signatureMethod->buildSignature($this, $consumer, $token); | |
245 } | |
246 | |
247 /** | |
248 * @return string | |
249 */ | |
250 public static function generateNonce() | |
251 { | |
252 return md5(microtime() . mt_rand()); | |
253 } | |
254 } |