Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   1  <?php
   2  
   3  /**
   4   * Licensed to Jasig under one or more contributor license
   5   * agreements. See the NOTICE file distributed with this work for
   6   * additional information regarding copyright ownership.
   7   *
   8   * Jasig licenses this file to you under the Apache License,
   9   * Version 2.0 (the "License"); you may not use this file except in
  10   * compliance with the License. You may obtain a copy of the License at:
  11   *
  12   * http://www.apache.org/licenses/LICENSE-2.0
  13   *
  14   * Unless required by applicable law or agreed to in writing, software
  15   * distributed under the License is distributed on an "AS IS" BASIS,
  16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17   * See the License for the specific language governing permissions and
  18   * limitations under the License.
  19   *
  20   * PHP Version 5
  21   *
  22   * @file     CAS/Request/CurlRequest.php
  23   * @category Authentication
  24   * @package  PhpCAS
  25   * @author   Adam Franco <afranco@middlebury.edu>
  26   * @license  http://www.apache.org/licenses/LICENSE-2.0  Apache License 2.0
  27   * @link     https://wiki.jasig.org/display/CASC/phpCAS
  28   */
  29  
  30  /**
  31   * Provides support for performing web-requests via curl
  32   *
  33   * @class    CAS_Request_CurlRequest
  34   * @category Authentication
  35   * @package  PhpCAS
  36   * @author   Adam Franco <afranco@middlebury.edu>
  37   * @license  http://www.apache.org/licenses/LICENSE-2.0  Apache License 2.0
  38   * @link     https://wiki.jasig.org/display/CASC/phpCAS
  39   */
  40  class CAS_Request_CurlRequest
  41  extends CAS_Request_AbstractRequest
  42  implements CAS_Request_RequestInterface
  43  {
  44  
  45      /**
  46       * Set additional curl options
  47       *
  48       * @param array $options option to set
  49       *
  50       * @return void
  51       */
  52      public function setCurlOptions (array $options)
  53      {
  54          $this->_curlOptions = $options;
  55      }
  56      private $_curlOptions = array();
  57  
  58      /**
  59       * Send the request and store the results.
  60       *
  61       * @return bool true on success, false on failure.
  62       */
  63      protected function sendRequest ()
  64      {
  65          phpCAS::traceBegin();
  66  
  67          /*********************************************************
  68           * initialize the CURL session
  69          *********************************************************/
  70          $ch = $this->initAndConfigure();
  71  
  72          /*********************************************************
  73           * Perform the query
  74          *********************************************************/
  75          $buf = curl_exec($ch);
  76          if ( $buf === false ) {
  77              phpCAS::trace('curl_exec() failed');
  78              $this->storeErrorMessage(
  79                  'CURL error #'.curl_errno($ch).': '.curl_error($ch)
  80              );
  81              $res = false;
  82          } else {
  83              $this->storeResponseBody($buf);
  84              phpCAS::trace("Response Body: \n".$buf."\n");
  85              $res = true;
  86  
  87          }
  88          // close the CURL session
  89          curl_close($ch);
  90  
  91          phpCAS::traceEnd($res);
  92          return $res;
  93      }
  94  
  95      /**
  96       * Internal method to initialize our cURL handle and configure the request.
  97       * This method should NOT be used outside of the CurlRequest or the
  98       * CurlMultiRequest.
  99       *
 100       * @return resource|false The cURL handle on success, false on failure
 101       */
 102      public function initAndConfigure()
 103      {
 104          /*********************************************************
 105           * initialize the CURL session
 106          *********************************************************/
 107          $ch = curl_init($this->url);
 108  
 109          if (version_compare(PHP_VERSION, '5.1.3', '>=')) {
 110              //only avaible in php5
 111              curl_setopt_array($ch, $this->_curlOptions);
 112          } else {
 113              foreach ($this->_curlOptions as $key => $value) {
 114                  curl_setopt($ch, $key, $value);
 115              }
 116          }
 117  
 118          /*********************************************************
 119           * Set SSL configuration
 120          *********************************************************/
 121          if ($this->caCertPath) {
 122              if ($this->validateCN) {
 123                  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
 124              } else {
 125                  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
 126              }
 127              curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
 128              curl_setopt($ch, CURLOPT_CAINFO, $this->caCertPath);
 129              phpCAS::trace('CURL: Set CURLOPT_CAINFO ' . $this->caCertPath);
 130          } else {
 131              curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
 132              curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
 133          }
 134  
 135          /*********************************************************
 136           * Configure curl to capture our output.
 137          *********************************************************/
 138          // return the CURL output into a variable
 139          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 140  
 141          // get the HTTP header with a callback
 142          curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, '_curlReadHeaders'));
 143  
 144          /*********************************************************
 145           * Add cookie headers to our request.
 146          *********************************************************/
 147          if (count($this->cookies)) {
 148              $cookieStrings = array();
 149              foreach ($this->cookies as $name => $val) {
 150                  $cookieStrings[] = $name.'='.$val;
 151              }
 152              curl_setopt($ch, CURLOPT_COOKIE, implode(';', $cookieStrings));
 153          }
 154  
 155          /*********************************************************
 156           * Add any additional headers
 157          *********************************************************/
 158          if (count($this->headers)) {
 159              curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
 160          }
 161  
 162          /*********************************************************
 163           * Flag and Body for POST requests
 164          *********************************************************/
 165          if ($this->isPost) {
 166              curl_setopt($ch, CURLOPT_POST, 1);
 167              curl_setopt($ch, CURLOPT_POSTFIELDS, $this->postBody);
 168          }
 169  
 170          return $ch;
 171      }
 172  
 173      /**
 174       * Store the response body.
 175       * This method should NOT be used outside of the CurlRequest or the
 176       * CurlMultiRequest.
 177       *
 178       * @param string $body body to stor
 179       *
 180       * @return void
 181       */
 182      private function _storeResponseBody ($body)
 183      {
 184          $this->storeResponseBody($body);
 185      }
 186  
 187      /**
 188       * Internal method for capturing the headers from a curl request.
 189       *
 190       * @param resource $ch     handle of curl
 191       * @param string $header header
 192       *
 193       * @return int
 194       */
 195      private function _curlReadHeaders ($ch, $header)
 196      {
 197          $this->storeResponseHeader($header);
 198          return strlen($header);
 199      }
 200  }