PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/google-analytics-dashboard-for-wp/tools/src/Google/Http/Request.php

https://gitlab.com/bhargavi_dcw/dflocal
PHP | 498 lines | 256 code | 50 blank | 192 comment | 23 complexity | afa03db51e928b1f72d9e5c86a6d75ca MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright 2010 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. require_once realpath(dirname(__FILE__) . '/../../../autoload.php');
  18. /**
  19. * HTTP Request to be executed by IO classes.
  20. * Upon execution, the
  21. * responseHttpCode, responseHeaders and responseBody will be filled in.
  22. *
  23. * @author Chris Chabot <chabotc@google.com>
  24. * @author Chirag Shah <chirags@google.com>
  25. *
  26. */
  27. class Google_Http_Request
  28. {
  29. const GZIP_UA = " (gzip)";
  30. private $batchHeaders = array(
  31. 'Content-Type' => 'application/http',
  32. 'Content-Transfer-Encoding' => 'binary',
  33. 'MIME-Version' => '1.0'
  34. );
  35. protected $queryParams;
  36. protected $requestMethod;
  37. protected $requestHeaders;
  38. protected $baseComponent = null;
  39. protected $path;
  40. protected $postBody;
  41. protected $userAgent;
  42. protected $canGzip = null;
  43. protected $responseHttpCode;
  44. protected $responseHeaders;
  45. protected $responseBody;
  46. protected $expectedClass;
  47. public $accessKey;
  48. public function __construct($url, $method = 'GET', $headers = array(), $postBody = null)
  49. {
  50. $this->setUrl($url);
  51. $this->setRequestMethod($method);
  52. $this->setRequestHeaders($headers);
  53. $this->setPostBody($postBody);
  54. }
  55. /**
  56. * Misc function that returns the base url component of the $url
  57. * used by the OAuth signing class to calculate the base string
  58. *
  59. * @return string The base url component of the $url.
  60. */
  61. public function getBaseComponent()
  62. {
  63. return $this->baseComponent;
  64. }
  65. /**
  66. * Set the base URL that path and query parameters will be added to.
  67. *
  68. * @param $baseComponent string
  69. */
  70. public function setBaseComponent($baseComponent)
  71. {
  72. $this->baseComponent = $baseComponent;
  73. }
  74. /**
  75. * Enable support for gzipped responses with this request.
  76. */
  77. public function enableGzip()
  78. {
  79. $this->setRequestHeaders(array(
  80. "Accept-Encoding" => "gzip"
  81. ));
  82. $this->canGzip = true;
  83. $this->setUserAgent($this->userAgent);
  84. }
  85. /**
  86. * Disable support for gzip responses with this request.
  87. */
  88. public function disableGzip()
  89. {
  90. if (isset($this->requestHeaders['accept-encoding']) && $this->requestHeaders['accept-encoding'] == "gzip") {
  91. unset($this->requestHeaders['accept-encoding']);
  92. }
  93. $this->canGzip = false;
  94. $this->userAgent = str_replace(self::GZIP_UA, "", $this->userAgent);
  95. }
  96. /**
  97. * Can this request accept a gzip response?
  98. *
  99. * @return bool
  100. */
  101. public function canGzip()
  102. {
  103. return $this->canGzip;
  104. }
  105. /**
  106. * Misc function that returns an array of the query parameters of the current
  107. * url used by the OAuth signing class to calculate the signature
  108. *
  109. * @return array Query parameters in the query string.
  110. */
  111. public function getQueryParams()
  112. {
  113. return $this->queryParams;
  114. }
  115. /**
  116. * Set a new query parameter.
  117. *
  118. * @param $key -
  119. * string to set, does not need to be URL encoded
  120. * @param $value -
  121. * string to set, does not need to be URL encoded
  122. */
  123. public function setQueryParam($key, $value)
  124. {
  125. $this->queryParams[$key] = $value;
  126. }
  127. /**
  128. *
  129. * @return string HTTP Response Code.
  130. */
  131. public function getResponseHttpCode()
  132. {
  133. return (int) $this->responseHttpCode;
  134. }
  135. /**
  136. *
  137. * @param int $responseHttpCode
  138. * HTTP Response Code.
  139. */
  140. public function setResponseHttpCode($responseHttpCode)
  141. {
  142. $this->responseHttpCode = $responseHttpCode;
  143. }
  144. /**
  145. *
  146. * @return $responseHeaders (array) HTTP Response Headers.
  147. */
  148. public function getResponseHeaders()
  149. {
  150. return $this->responseHeaders;
  151. }
  152. /**
  153. *
  154. * @return string HTTP Response Body
  155. */
  156. public function getResponseBody()
  157. {
  158. return $this->responseBody;
  159. }
  160. /**
  161. * Set the class the response to this request should expect.
  162. *
  163. * @param $class string
  164. * the class name
  165. */
  166. public function setExpectedClass($class)
  167. {
  168. $this->expectedClass = $class;
  169. }
  170. /**
  171. * Retrieve the expected class the response should expect.
  172. *
  173. * @return string class name
  174. */
  175. public function getExpectedClass()
  176. {
  177. return $this->expectedClass;
  178. }
  179. /**
  180. *
  181. * @param array $headers
  182. * The HTTP response headers
  183. * to be normalized.
  184. */
  185. public function setResponseHeaders($headers)
  186. {
  187. $headers = Google_Utils::normalize($headers);
  188. if ($this->responseHeaders) {
  189. $headers = array_merge($this->responseHeaders, $headers);
  190. }
  191. $this->responseHeaders = $headers;
  192. }
  193. /**
  194. *
  195. * @param string $key
  196. * @return array|boolean Returns the requested HTTP header or
  197. * false if unavailable.
  198. */
  199. public function getResponseHeader($key)
  200. {
  201. return isset($this->responseHeaders[$key]) ? $this->responseHeaders[$key] : false;
  202. }
  203. /**
  204. *
  205. * @param string $responseBody
  206. * The HTTP response body.
  207. */
  208. public function setResponseBody($responseBody)
  209. {
  210. $this->responseBody = $responseBody;
  211. }
  212. /**
  213. *
  214. * @return string $url The request URL.
  215. */
  216. public function getUrl()
  217. {
  218. return $this->baseComponent . $this->path . (count($this->queryParams) ? "?" . $this->buildQuery($this->queryParams) : '');
  219. }
  220. /**
  221. *
  222. * @return string $method HTTP Request Method.
  223. */
  224. public function getRequestMethod()
  225. {
  226. return $this->requestMethod;
  227. }
  228. /**
  229. *
  230. * @return array $headers HTTP Request Headers.
  231. */
  232. public function getRequestHeaders()
  233. {
  234. return $this->requestHeaders;
  235. }
  236. /**
  237. *
  238. * @param string $key
  239. * @return array|boolean Returns the requested HTTP header or
  240. * false if unavailable.
  241. */
  242. public function getRequestHeader($key)
  243. {
  244. return isset($this->requestHeaders[$key]) ? $this->requestHeaders[$key] : false;
  245. }
  246. /**
  247. *
  248. * @return string $postBody HTTP Request Body.
  249. */
  250. public function getPostBody()
  251. {
  252. return $this->postBody;
  253. }
  254. /**
  255. *
  256. * @param string $url
  257. * the url to set
  258. */
  259. public function setUrl($url)
  260. {
  261. if (substr($url, 0, 4) != 'http') {
  262. // Force the path become relative.
  263. if (substr($url, 0, 1) !== '/') {
  264. $url = '/' . $url;
  265. }
  266. }
  267. $parts = parse_url($url);
  268. if (isset($parts['host'])) {
  269. $this->baseComponent = sprintf("%s%s%s", isset($parts['scheme']) ? $parts['scheme'] . "://" : '', isset($parts['host']) ? $parts['host'] : '', isset($parts['port']) ? ":" . $parts['port'] : '');
  270. }
  271. $this->path = isset($parts['path']) ? $parts['path'] : '';
  272. $this->queryParams = array();
  273. if (isset($parts['query'])) {
  274. $this->queryParams = $this->parseQuery($parts['query']);
  275. }
  276. }
  277. /**
  278. *
  279. * @param string $method
  280. * Set he HTTP Method and normalize
  281. * it to upper-case, as required by HTTP.
  282. *
  283. */
  284. public function setRequestMethod($method)
  285. {
  286. $this->requestMethod = strtoupper($method);
  287. }
  288. /**
  289. *
  290. * @param array $headers
  291. * The HTTP request headers
  292. * to be set and normalized.
  293. */
  294. public function setRequestHeaders($headers)
  295. {
  296. $headers = Google_Utils::normalize($headers);
  297. if ($this->requestHeaders) {
  298. $headers = array_merge($this->requestHeaders, $headers);
  299. }
  300. $this->requestHeaders = $headers;
  301. }
  302. /**
  303. *
  304. * @param string $postBody
  305. * the postBody to set
  306. */
  307. public function setPostBody($postBody)
  308. {
  309. $this->postBody = $postBody;
  310. }
  311. /**
  312. * Set the User-Agent Header.
  313. *
  314. * @param string $userAgent
  315. * The User-Agent.
  316. */
  317. public function setUserAgent($userAgent)
  318. {
  319. $this->userAgent = $userAgent;
  320. if ($this->canGzip) {
  321. $this->userAgent = $userAgent . self::GZIP_UA;
  322. }
  323. }
  324. /**
  325. *
  326. * @return string The User-Agent.
  327. */
  328. public function getUserAgent()
  329. {
  330. return $this->userAgent;
  331. }
  332. /**
  333. * Returns a cache key depending on if this was an OAuth signed request
  334. * in which case it will use the non-signed url and access key to make this
  335. * cache key unique per authenticated user, else use the plain request url
  336. *
  337. * @return string The md5 hash of the request cache key.
  338. */
  339. public function getCacheKey()
  340. {
  341. $key = $this->getUrl();
  342. if (isset($this->accessKey)) {
  343. $key .= $this->accessKey;
  344. }
  345. if (isset($this->requestHeaders['authorization'])) {
  346. $key .= $this->requestHeaders['authorization'];
  347. }
  348. return md5($key);
  349. }
  350. public function getParsedCacheControl()
  351. {
  352. $parsed = array();
  353. $rawCacheControl = $this->getResponseHeader('cache-control');
  354. if ($rawCacheControl) {
  355. $rawCacheControl = str_replace(', ', '&', $rawCacheControl);
  356. parse_str($rawCacheControl, $parsed);
  357. }
  358. return $parsed;
  359. }
  360. /**
  361. *
  362. * @param string $id
  363. * @return string A string representation of the HTTP Request.
  364. */
  365. public function toBatchString($id)
  366. {
  367. $str = '';
  368. $path = parse_url($this->getUrl(), PHP_URL_PATH) . "?" . http_build_query($this->queryParams);
  369. $str .= $this->getRequestMethod() . ' ' . $path . " HTTP/1.1\n";
  370. foreach ($this->getRequestHeaders() as $key => $val) {
  371. $str .= $key . ': ' . $val . "\n";
  372. }
  373. if ($this->getPostBody()) {
  374. $str .= "\n";
  375. $str .= $this->getPostBody();
  376. }
  377. $headers = '';
  378. foreach ($this->batchHeaders as $key => $val) {
  379. $headers .= $key . ': ' . $val . "\n";
  380. }
  381. $headers .= "Content-ID: $id\n";
  382. $str = $headers . "\n" . $str;
  383. return $str;
  384. }
  385. /**
  386. * Our own version of parse_str that allows for multiple variables
  387. * with the same name.
  388. *
  389. * @param $string -
  390. * the query string to parse
  391. */
  392. private function parseQuery($string)
  393. {
  394. $return = array();
  395. $parts = explode("&", $string);
  396. foreach ($parts as $part) {
  397. list ($key, $value) = explode('=', $part, 2);
  398. $value = urldecode($value);
  399. if (isset($return[$key])) {
  400. if (! is_array($return[$key])) {
  401. $return[$key] = array(
  402. $return[$key]
  403. );
  404. }
  405. $return[$key][] = $value;
  406. } else {
  407. $return[$key] = $value;
  408. }
  409. }
  410. return $return;
  411. }
  412. /**
  413. * A version of build query that allows for multiple
  414. * duplicate keys.
  415. *
  416. * @param $parts array
  417. * of key value pairs
  418. */
  419. private function buildQuery($parts)
  420. {
  421. $return = array();
  422. foreach ($parts as $key => $value) {
  423. if (is_array($value)) {
  424. foreach ($value as $v) {
  425. $return[] = urlencode($key) . "=" . urlencode($v);
  426. }
  427. } else {
  428. $return[] = urlencode($key) . "=" . urlencode($value);
  429. }
  430. }
  431. return implode('&', $return);
  432. }
  433. /**
  434. * If we're POSTing and have no body to send, we can send the query
  435. * parameters in there, which avoids length issues with longer query
  436. * params.
  437. */
  438. public function maybeMoveParametersToBody()
  439. {
  440. if ($this->getRequestMethod() == "POST" && empty($this->postBody)) {
  441. $this->setRequestHeaders(array(
  442. "content-type" => "application/x-www-form-urlencoded; charset=UTF-8"
  443. ));
  444. $this->setPostBody($this->buildQuery($this->queryParams));
  445. $this->queryParams = array();
  446. }
  447. }
  448. }