2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * This file contains the class moodle_google_curlio.
20 * @package core_google
21 * @copyright 2013 Frédéric Massart
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
26 require_once($CFG->libdir
. '/filelib.php');
27 require_once($CFG->libdir
. '/google/Google/IO/Curl.php');
28 require_once($CFG->libdir
. '/google/Google/IO/Exception.php');
31 * Class moodle_google_curlio.
33 * The initial purpose of this class is to add support for our
34 * class curl in Google_IO_Curl. It mostly entirely overrides it.
36 * @package core_google
37 * @copyright 2013 Frédéric Massart
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class moodle_google_curlio
extends Google_IO_Curl
{
42 /** @var array associate array of constant value and their name. */
43 private static $constants = null;
45 /** @var array options. */
46 private $options = array();
49 * Send the request via our curl object.
51 * @param curl $curl prepared curl object.
52 * @param Google_HttpRequest $request The request.
53 * @return string result of the request.
55 private function do_request($curl, $request) {
56 $url = $request->getUrl();
57 $method = $request->getRequestMethod();
58 switch (strtoupper($method)) {
60 $ret = $curl->post($url, $request->getPostBody());
63 $ret = $curl->get($url);
66 $ret = $curl->head($url);
69 $ret = $curl->put($url);
72 throw new coding_exception('Unknown request type: ' . $method);
79 * Execute an API request.
81 * This is a copy/paste from the parent class that uses Moodle's implementation
82 * of curl. Portions have been removed or altered.
84 * @param Google_Http_Request $request the http request to be executed
85 * @return Google_Http_Request http request with the response http code, response
86 * headers and response body filled in
87 * @throws Google_IO_Exception on curl or IO error
89 public function executeRequest(Google_Http_Request
$request) {
92 if ($request->getPostBody()) {
93 $curl->setopt(array('CURLOPT_POSTFIELDS' => $request->getPostBody()));
96 $requestHeaders = $request->getRequestHeaders();
97 if ($requestHeaders && is_array($requestHeaders)) {
98 $curlHeaders = array();
99 foreach ($requestHeaders as $k => $v) {
100 $curlHeaders[] = "$k: $v";
102 $curl->setopt(array('CURLOPT_HTTPHEADER' => $curlHeaders));
105 $curl->setopt(array('CURLOPT_URL' => $request->getUrl()));
107 $curl->setopt(array('CURLOPT_CUSTOMREQUEST' => $request->getRequestMethod()));
108 $curl->setopt(array('CURLOPT_USERAGENT' => $request->getUserAgent()));
110 $curl->setopt(array('CURLOPT_FOLLOWLOCATION' => false));
111 $curl->setopt(array('CURLOPT_SSL_VERIFYPEER' => true));
112 $curl->setopt(array('CURLOPT_RETURNTRANSFER' => true));
113 $curl->setopt(array('CURLOPT_HEADER' => true));
115 if ($request->canGzip()) {
116 $curl->setopt(array('CURLOPT_ENCODING' => 'gzip,deflate'));
119 $curl->setopt($this->options
);
120 $respdata = $this->do_request($curl, $request);
122 $infos = $curl->get_info();
123 $respheadersize = $infos['header_size'];
124 $resphttpcode = (int) $infos['http_code'];
125 $curlerrornum = $curl->get_errno();
126 $curlerror = $curl->error
;
128 if ($respdata != CURLE_OK
) {
129 throw new Google_IO_Exception($curlerror);
132 list($responseHeaders, $responseBody) = $this->parseHttpResponse($respdata, $respheadersize);
133 return array($responseBody, $responseHeaders, $resphttpcode);
139 * We overwrite this method to ensure that the data passed meets
140 * the requirement of our curl implementation and so that the keys
141 * are strings, and not curl constants.
143 * @param array $optparams Multiple options used by a cURL session.
146 public function setOptions($optparams) {
147 $safeparams = array();
148 foreach ($optparams as $name => $value) {
149 if (!is_string($name)) {
150 $name = $this->get_option_name_from_constant($name);
152 $safeparams[$name] = $value;
154 $this->options
= $options +
$this->options
;
158 * Set the maximum request time in seconds.
160 * Overridden to use the right option key.
162 * @param $timeout in seconds
164 public function setTimeout($timeout) {
165 // Since this timeout is really for putting a bound on the time
166 // we'll set them both to the same. If you need to specify a longer
167 // CURLOPT_TIMEOUT, or a tigher CONNECTTIMEOUT, the best thing to
168 // do is use the setOptions method for the values individually.
169 $this->options
['CURLOPT_CONNECTTIMEOUT'] = $timeout;
170 $this->options
['CURLOPT_TIMEOUT'] = $timeout;
174 * Get the maximum request time in seconds.
176 * Overridden to use the right option key.
178 * @return timeout in seconds.
180 public function getTimeout() {
181 return $this->options
['CURLOPT_TIMEOUT'];
185 * Return the name of an option based on the constant value.
187 * @param int $constant value of a CURL constant.
188 * @return string name of the constant if found, or throws exception.
189 * @throws coding_exception when the constant is not found.
192 public function get_option_name_from_constant($constant) {
193 if (is_null(self
::$constants)) {
194 $constants = get_defined_constants(true);
195 $constants = isset($constants['curl']) ?
$constants['curl'] : array();
196 $constants = array_flip($constants);
197 self
::$constants = $constants;
199 if (isset(self
::$constants[$constant])) {
200 return self
::$constants[$constant];
202 throw new coding_exception('Unknown curl constant value: ' . $constant);