PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/google/curlio.php

https://gitlab.com/unofficial-mirrors/moodle
PHP | 204 lines | 94 code | 23 blank | 87 comment | 11 complexity | e04506c0dca1028c0163cc741082f625 MD5 | raw file
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * This file contains the class moodle_google_curlio.
  18. *
  19. * @package core_google
  20. * @copyright 2013 Frédéric Massart
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. defined('MOODLE_INTERNAL') || die();
  24. require_once($CFG->libdir . '/filelib.php');
  25. /**
  26. * Class moodle_google_curlio.
  27. *
  28. * The initial purpose of this class is to add support for our
  29. * class curl in Google_IO_Curl. It mostly entirely overrides it.
  30. *
  31. * @package core_google
  32. * @copyright 2013 Frédéric Massart
  33. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34. */
  35. class moodle_google_curlio extends Google_IO_Curl {
  36. /** @var array associate array of constant value and their name. */
  37. private static $constants = null;
  38. /** @var array options. */
  39. private $options = array();
  40. /**
  41. * Send the request via our curl object.
  42. *
  43. * @param curl $curl prepared curl object.
  44. * @param Google_HttpRequest $request The request.
  45. * @return string result of the request.
  46. */
  47. private function do_request($curl, $request) {
  48. $url = $request->getUrl();
  49. $method = $request->getRequestMethod();
  50. switch (strtoupper($method)) {
  51. case 'POST':
  52. $ret = $curl->post($url, $request->getPostBody());
  53. break;
  54. case 'GET':
  55. $ret = $curl->get($url);
  56. break;
  57. case 'HEAD':
  58. $ret = $curl->head($url);
  59. break;
  60. case 'PUT':
  61. $ret = $curl->put($url);
  62. break;
  63. default:
  64. throw new coding_exception('Unknown request type: ' . $method);
  65. break;
  66. }
  67. return $ret;
  68. }
  69. /**
  70. * Execute an API request.
  71. *
  72. * This is a copy/paste from the parent class that uses Moodle's implementation
  73. * of curl. Portions have been removed or altered.
  74. *
  75. * @param Google_Http_Request $request the http request to be executed
  76. * @return Google_Http_Request http request with the response http code, response
  77. * headers and response body filled in
  78. * @throws Google_IO_Exception on curl or IO error
  79. */
  80. public function executeRequest(Google_Http_Request $request) {
  81. $curl = new curl();
  82. if ($request->getPostBody()) {
  83. $curl->setopt(array('CURLOPT_POSTFIELDS' => $request->getPostBody()));
  84. }
  85. $requestHeaders = $request->getRequestHeaders();
  86. if ($requestHeaders && is_array($requestHeaders)) {
  87. $curlHeaders = array();
  88. foreach ($requestHeaders as $k => $v) {
  89. $curlHeaders[] = "$k: $v";
  90. }
  91. $curl->setopt(array('CURLOPT_HTTPHEADER' => $curlHeaders));
  92. }
  93. $curl->setopt(array('CURLOPT_URL' => $request->getUrl()));
  94. $curl->setopt(array('CURLOPT_CUSTOMREQUEST' => $request->getRequestMethod()));
  95. $curl->setopt(array('CURLOPT_USERAGENT' => $request->getUserAgent()));
  96. $curl->setopt(array('CURLOPT_FOLLOWLOCATION' => false));
  97. $curl->setopt(array('CURLOPT_SSL_VERIFYPEER' => true));
  98. $curl->setopt(array('CURLOPT_RETURNTRANSFER' => true));
  99. $curl->setopt(array('CURLOPT_HEADER' => true));
  100. if ($request->canGzip()) {
  101. $curl->setopt(array('CURLOPT_ENCODING' => 'gzip,deflate'));
  102. }
  103. $curl->setopt($this->options);
  104. $respdata = $this->do_request($curl, $request);
  105. $infos = $curl->get_info();
  106. $respheadersize = $infos['header_size'];
  107. $resphttpcode = (int) $infos['http_code'];
  108. $curlerrornum = $curl->get_errno();
  109. $curlerror = $curl->error;
  110. if ($respdata != CURLE_OK) {
  111. throw new Google_IO_Exception($curlerror);
  112. }
  113. list($responseHeaders, $responseBody) = $this->parseHttpResponse($respdata, $respheadersize);
  114. return array($responseBody, $responseHeaders, $resphttpcode);
  115. }
  116. /**
  117. * Set curl options.
  118. *
  119. * We overwrite this method to ensure that the data passed meets
  120. * the requirement of our curl implementation and so that the keys
  121. * are strings, and not curl constants.
  122. *
  123. * @param array $optparams Multiple options used by a cURL session.
  124. * @return void
  125. */
  126. public function setOptions($optparams) {
  127. $safeparams = array();
  128. foreach ($optparams as $name => $value) {
  129. if (!is_string($name)) {
  130. $name = $this->get_option_name_from_constant($name);
  131. }
  132. $safeparams[$name] = $value;
  133. }
  134. $this->options = $options + $this->options;
  135. }
  136. /**
  137. * Set the maximum request time in seconds.
  138. *
  139. * Overridden to use the right option key.
  140. *
  141. * @param $timeout in seconds
  142. */
  143. public function setTimeout($timeout) {
  144. // Since this timeout is really for putting a bound on the time
  145. // we'll set them both to the same. If you need to specify a longer
  146. // CURLOPT_TIMEOUT, or a tigher CONNECTTIMEOUT, the best thing to
  147. // do is use the setOptions method for the values individually.
  148. $this->options['CURLOPT_CONNECTTIMEOUT'] = $timeout;
  149. $this->options['CURLOPT_TIMEOUT'] = $timeout;
  150. }
  151. /**
  152. * Get the maximum request time in seconds.
  153. *
  154. * Overridden to use the right option key.
  155. *
  156. * @return timeout in seconds.
  157. */
  158. public function getTimeout() {
  159. return $this->options['CURLOPT_TIMEOUT'];
  160. }
  161. /**
  162. * Return the name of an option based on the constant value.
  163. *
  164. * @param int $constant value of a CURL constant.
  165. * @return string name of the constant if found, or throws exception.
  166. * @throws coding_exception when the constant is not found.
  167. * @since Moodle 2.5
  168. */
  169. public function get_option_name_from_constant($constant) {
  170. if (is_null(self::$constants)) {
  171. $constants = get_defined_constants(true);
  172. $constants = isset($constants['curl']) ? $constants['curl'] : array();
  173. $constants = array_flip($constants);
  174. self::$constants = $constants;
  175. }
  176. if (isset(self::$constants[$constant])) {
  177. return self::$constants[$constant];
  178. }
  179. throw new coding_exception('Unknown curl constant value: ' . $constant);
  180. }
  181. }