CurlRequest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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(
  73. 'CURL error #'.curl_errno($ch).': '.curl_error($ch)
  74. );
  75. $res = false;
  76. } else {
  77. $this->storeResponseBody($buf);
  78. phpCAS::trace("Response Body: \n".$buf."\n");
  79. $res = true;
  80. }
  81. // close the CURL session
  82. curl_close($ch);
  83. phpCAS::traceEnd($res);
  84. return $res;
  85. }
  86. /**
  87. * Internal method to initialize our cURL handle and configure the request.
  88. * This method should NOT be used outside of the CurlRequest or the
  89. * CurlMultiRequest.
  90. *
  91. * @return resource|false The cURL handle on success, false on failure
  92. */
  93. public function initAndConfigure()
  94. {
  95. /*********************************************************
  96. * initialize the CURL session
  97. *********************************************************/
  98. $ch = curl_init($this->url);
  99. if (version_compare(PHP_VERSION, '5.1.3', '>=')) {
  100. //only avaible in php5
  101. curl_setopt_array($ch, $this->_curlOptions);
  102. } else {
  103. foreach ($this->_curlOptions as $key => $value) {
  104. curl_setopt($ch, $key, $value);
  105. }
  106. }
  107. /*********************************************************
  108. * Set SSL configuration
  109. *********************************************************/
  110. if ($this->caCertPath) {
  111. if ($this->validateCN) {
  112. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  113. } else {
  114. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  115. }
  116. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
  117. curl_setopt($ch, CURLOPT_CAINFO, $this->caCertPath);
  118. phpCAS::trace('CURL: Set CURLOPT_CAINFO ' . $this->caCertPath);
  119. } else {
  120. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  121. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  122. }
  123. /*********************************************************
  124. * Configure curl to capture our output.
  125. *********************************************************/
  126. // return the CURL output into a variable
  127. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  128. // get the HTTP header with a callback
  129. curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, '_curlReadHeaders'));
  130. /*********************************************************
  131. * Add cookie headers to our request.
  132. *********************************************************/
  133. if (count($this->cookies)) {
  134. $cookieStrings = array();
  135. foreach ($this->cookies as $name => $val) {
  136. $cookieStrings[] = $name.'='.$val;
  137. }
  138. curl_setopt($ch, CURLOPT_COOKIE, implode(';', $cookieStrings));
  139. }
  140. /*********************************************************
  141. * Add any additional headers
  142. *********************************************************/
  143. if (count($this->headers)) {
  144. curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
  145. }
  146. /*********************************************************
  147. * Flag and Body for POST requests
  148. *********************************************************/
  149. if ($this->isPost) {
  150. curl_setopt($ch, CURLOPT_POST, 1);
  151. curl_setopt($ch, CURLOPT_POSTFIELDS, $this->postBody);
  152. }
  153. return $ch;
  154. }
  155. /**
  156. * Store the response body.
  157. * This method should NOT be used outside of the CurlRequest or the
  158. * CurlMultiRequest.
  159. *
  160. * @param string $body body to stor
  161. *
  162. * @return void
  163. */
  164. private function _storeResponseBody ($body)
  165. {
  166. $this->storeResponseBody($body);
  167. }
  168. /**
  169. * Internal method for capturing the headers from a curl request.
  170. *
  171. * @param resource $ch handle of curl
  172. * @param string $header header
  173. *
  174. * @return int
  175. */
  176. private function _curlReadHeaders ($ch, $header)
  177. {
  178. $this->storeResponseHeader($header);
  179. return strlen($header);
  180. }
  181. }