[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/google/ -> curlio.php (source)

   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  /**
  18   * This file contains the class moodle_google_curlio.
  19   *
  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
  23   */
  24  
  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');
  29  
  30  /**
  31   * Class moodle_google_curlio.
  32   *
  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.
  35   *
  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
  39  */
  40  class moodle_google_curlio extends Google_IO_Curl {
  41  
  42      /** @var array associate array of constant value and their name. */
  43      private static $constants = null;
  44  
  45      /** @var array options. */
  46      private $options = array();
  47  
  48      /**
  49       * Send the request via our curl object.
  50       *
  51       * @param curl $curl prepared curl object.
  52       * @param Google_HttpRequest $request The request.
  53       * @return string result of the request.
  54       */
  55      private function do_request($curl, $request) {
  56          $url = $request->getUrl();
  57          $method = $request->getRequestMethod();
  58          switch (strtoupper($method)) {
  59              case 'POST':
  60                  $ret = $curl->post($url, $request->getPostBody());
  61                  break;
  62              case 'GET':
  63                  $ret = $curl->get($url);
  64                  break;
  65              case 'HEAD':
  66                  $ret = $curl->head($url);
  67                  break;
  68              case 'PUT':
  69                  $ret = $curl->put($url);
  70                  break;
  71              default:
  72                  throw new coding_exception('Unknown request type: ' . $method);
  73                  break;
  74          }
  75          return $ret;
  76      }
  77  
  78      /**
  79       * Execute an API request.
  80       *
  81       * This is a copy/paste from the parent class that uses Moodle's implementation
  82       * of curl. Portions have been removed or altered.
  83       *
  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
  88       */
  89      public function executeRequest(Google_Http_Request $request) {
  90          $curl = new curl();
  91  
  92          if ($request->getPostBody()) {
  93              $curl->setopt(array('CURLOPT_POSTFIELDS' => $request->getPostBody()));
  94          }
  95  
  96          $requestHeaders = $request->getRequestHeaders();
  97          if ($requestHeaders && is_array($requestHeaders)) {
  98              $curlHeaders = array();
  99              foreach ($requestHeaders as $k => $v) {
 100                  $curlHeaders[] = "$k: $v";
 101              }
 102              $curl->setopt(array('CURLOPT_HTTPHEADER' => $curlHeaders));
 103          }
 104  
 105          $curl->setopt(array('CURLOPT_URL' => $request->getUrl()));
 106  
 107          $curl->setopt(array('CURLOPT_CUSTOMREQUEST' => $request->getRequestMethod()));
 108          $curl->setopt(array('CURLOPT_USERAGENT' => $request->getUserAgent()));
 109  
 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));
 114  
 115          if ($request->canGzip()) {
 116              $curl->setopt(array('CURLOPT_ENCODING' => 'gzip,deflate'));
 117          }
 118  
 119          $curl->setopt($this->options);
 120          $respdata = $this->do_request($curl, $request);
 121  
 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;
 127  
 128          if ($respdata != CURLE_OK) {
 129              throw new Google_IO_Exception($curlerror);
 130          }
 131  
 132          list($responseHeaders, $responseBody) = $this->parseHttpResponse($respdata, $respheadersize);
 133          return array($responseBody, $responseHeaders, $resphttpcode);
 134      }
 135  
 136      /**
 137       * Set curl options.
 138       *
 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.
 142       *
 143       * @param array $optparams Multiple options used by a cURL session.
 144       * @return void
 145       */
 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);
 151              }
 152              $safeparams[$name] = $value;
 153          }
 154          $this->options = $options + $this->options;
 155      }
 156  
 157      /**
 158       * Set the maximum request time in seconds.
 159       *
 160       * Overridden to use the right option key.
 161       *
 162       * @param $timeout in seconds
 163       */
 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;
 171      }
 172  
 173      /**
 174       * Get the maximum request time in seconds.
 175       *
 176       * Overridden to use the right option key.
 177       *
 178       * @return timeout in seconds.
 179       */
 180      public function getTimeout() {
 181         return $this->options['CURLOPT_TIMEOUT'];
 182      }
 183  
 184      /**
 185       * Return the name of an option based on the constant value.
 186       *
 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.
 190       * @since Moodle 2.5
 191       */
 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;
 198          }
 199          if (isset(self::$constants[$constant])) {
 200              return self::$constants[$constant];
 201          }
 202          throw new coding_exception('Unknown curl constant value: ' . $constant);
 203      }
 204  
 205  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1