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.
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:
12
* http://www.apache.org/licenses/LICENSE-2.0
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.
22
* @file CAS/Request/CurlRequest.php
23
* @category Authentication
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
31
* Provides support for performing web-requests via curl
33
* @class CAS_Request_CurlRequest
34
* @category Authentication
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
40
class CAS_Request_CurlRequest
41
extends CAS_Request_AbstractRequest
42
implements CAS_Request_RequestInterface
46
* Set additional curl options
48
* @param array $options option to set
52
public function setCurlOptions (array $options)
54
$this->_curlOptions = $options;
56
private $_curlOptions = array();
59
* Send the request and store the results.
61
* @return bool true on success, false on failure.
63
protected function sendRequest ()
67
/*********************************************************
68
* initialize the CURL session
69
*********************************************************/
70
$ch = $this->_initAndConfigure();
72
/*********************************************************
74
*********************************************************/
75
$buf = curl_exec($ch);
76
if ( $buf === false ) {
77
phpCAS::trace('curl_exec() failed');
78
$this->storeErrorMessage('CURL error #'.curl_errno($ch).': '.curl_error($ch));
81
$this->storeResponseBody($buf);
82
phpCAS::trace("Response Body: \n".$buf."\n");
86
// close the CURL session
89
phpCAS::traceEnd($res);
94
* Internal method to initialize our cURL handle and configure the request.
95
* This method should NOT be used outside of the CurlRequest or the
98
* @return resource The cURL handle on success, false on failure
100
private function _initAndConfigure()
102
/*********************************************************
103
* initialize the CURL session
104
*********************************************************/
105
$ch = curl_init($this->url);
107
if (version_compare(PHP_VERSION, '5.1.3', '>=')) {
108
//only avaible in php5
109
curl_setopt_array($ch, $this->_curlOptions);
111
foreach ($this->_curlOptions as $key => $value) {
112
curl_setopt($ch, $key, $value);
116
/*********************************************************
117
* Set SSL configuration
118
*********************************************************/
119
if ($this->caCertPath) {
120
if ($this->validateCN) {
121
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
123
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
125
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
126
curl_setopt($ch, CURLOPT_CAINFO, $this->caCertPath);
127
phpCAS::trace('CURL: Set CURLOPT_CAINFO ' . $this->caCertPath);
129
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
132
/*********************************************************
133
* Configure curl to capture our output.
134
*********************************************************/
135
// return the CURL output into a variable
136
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
138
// get the HTTP header with a callback
139
curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, '_curlReadHeaders'));
141
/*********************************************************
142
* Add cookie headers to our request.
143
*********************************************************/
144
if (count($this->cookies)) {
145
$cookieStrings = array();
146
foreach ($this->cookies as $name => $val) {
147
$cookieStrings[] = $name.'='.$val;
149
curl_setopt($ch, CURLOPT_COOKIE, implode(';', $cookieStrings));
152
/*********************************************************
153
* Add any additional headers
154
*********************************************************/
155
if (count($this->headers)) {
156
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
159
/*********************************************************
160
* Flag and Body for POST requests
161
*********************************************************/
163
curl_setopt($ch, CURLOPT_POST, 1);
164
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->postBody);
171
* Store the response body.
172
* This method should NOT be used outside of the CurlRequest or the
175
* @param string $body body to stor
179
private function _storeResponseBody ($body)
181
$this->storeResponseBody($body);
185
* Internal method for capturing the headers from a curl request.
187
* @param handle $ch handle of curl
188
* @param string $header header
192
private function _curlReadHeaders ($ch, $header)
194
$this->storeResponseHeader($header);
195
return strlen($header);