/CAS/CAS-1.3.2/CAS/Request/CurlRequest.php

https://github.com/medieteknik/Medieteknik.nu · PHP · 197 lines · 78 code · 19 blank · 100 comment · 11 complexity · 67728f65bc87b4b2906ac15677cbf1d2 MD5 · raw file

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