phpCAS version 1.3.6

AbstractRequest.php
Go to the documentation of this file.
1 <?php
2
40 abstract class CAS_Request_AbstractRequest
41 implements CAS_Request_RequestInterface
42 {
43
44 protected $url = null;
45 protected $cookies = array();
46 protected $headers = array();
47 protected $isPost = false;
48 protected $postBody = null;
49 protected $caCertPath = null;
50 protected $validateCN = true;
51 private $_sent = false;
52 private $_responseHeaders = array();
53 private $_responseBody = null;
54 private $_errorMessage = '';
55
56 /*********************************************************
57 * Configure the Request
58 *********************************************************/
59
68 public function setUrl ($url)
69 {
70 if ($this->_sent) {
71 throw new CAS_OutOfSequenceException(
72 'Request has already been sent cannot '.__METHOD__
73 );
74 }
75
76 $this->url = $url;
77 }
78
88 public function addCookie ($name, $value)
89 {
90 if ($this->_sent) {
91 throw new CAS_OutOfSequenceException(
92 'Request has already been sent cannot '.__METHOD__
93 );
94 }
95
96 $this->cookies[$name] = $value;
97 }
98
109 public function addCookies (array $cookies)
110 {
111 if ($this->_sent) {
112 throw new CAS_OutOfSequenceException(
113 'Request has already been sent cannot '.__METHOD__
114 );
115 }
116
117 $this->cookies = array_merge($this->cookies, $cookies);
118 }
119
128 public function addHeader ($header)
129 {
130 if ($this->_sent) {
131 throw new CAS_OutOfSequenceException(
132 'Request has already been sent cannot '.__METHOD__
133 );
134 }
135
136 $this->headers[] = $header;
137 }
138
147 public function addHeaders (array $headers)
148 {
149 if ($this->_sent) {
150 throw new CAS_OutOfSequenceException(
151 'Request has already been sent cannot '.__METHOD__
152 );
153 }
154
155 $this->headers = array_merge($this->headers, $headers);
156 }
157
164 public function makePost ()
165 {
166 if ($this->_sent) {
167 throw new CAS_OutOfSequenceException(
168 'Request has already been sent cannot '.__METHOD__
169 );
170 }
171
172 $this->isPost = true;
173 }
174
183 public function setPostBody ($body)
184 {
185 if ($this->_sent) {
186 throw new CAS_OutOfSequenceException(
187 'Request has already been sent cannot '.__METHOD__
188 );
189 }
190 if (!$this->isPost) {
191 throw new CAS_OutOfSequenceException(
192 'Cannot add a POST body to a GET request, use makePost() first.'
193 );
194 }
195
196 $this->postBody = $body;
197 }
198
208 public function setSslCaCert ($caCertPath,$validate_cn=true)
209 {
210 if ($this->_sent) {
211 throw new CAS_OutOfSequenceException(
212 'Request has already been sent cannot '.__METHOD__
213 );
214 }
215 $this->caCertPath = $caCertPath;
216 $this->validateCN = $validate_cn;
217 }
218
219 /*********************************************************
220 * 2. Send the Request
221 *********************************************************/
222
229 public function send ()
230 {
231 if ($this->_sent) {
232 throw new CAS_OutOfSequenceException(
233 'Request has already been sent cannot send again.'
234 );
235 }
236 if (is_null($this->url) || !$this->url) {
237 throw new CAS_OutOfSequenceException(
238 'A url must be specified via setUrl() before the request can be sent.'
239 );
240 }
241 $this->_sent = true;
242 return $this->sendRequest();
243 }
244
250 abstract protected function sendRequest ();
251
259 protected function storeResponseHeaders (array $headers)
260 {
261 $this->_responseHeaders = array_merge($this->_responseHeaders, $headers);
262 }
263
271 protected function storeResponseHeader ($header)
272 {
273 $this->_responseHeaders[] = $header;
274 }
275
283 protected function storeResponseBody ($body)
284 {
285 $this->_responseBody = $body;
286 }
287
295 protected function storeErrorMessage ($message)
296 {
297 $this->_errorMessage .= $message;
298 }
299
300 /*********************************************************
301 * 3. Access the response
302 *********************************************************/
303
310 public function getResponseHeaders ()
311 {
312 if (!$this->_sent) {
313 throw new CAS_OutOfSequenceException(
314 'Request has not been sent yet. Cannot '.__METHOD__
315 );
316 }
317 return $this->_responseHeaders;
318 }
319
326 public function getResponseStatusCode ()
327 {
328 if (!$this->_sent) {
329 throw new CAS_OutOfSequenceException(
330 'Request has not been sent yet. Cannot '.__METHOD__
331 );
332 }
333
334 if (!preg_match(
335 '/HTTP\/[0-9.]+\s+([0-9]+)\s*(.*)/',
336 $this->_responseHeaders[0], $matches
337 )
338 ) {
339 throw new CAS_Request_Exception(
340 'Bad response, no status code was found in the first line.'
341 );
342 }
343
344 return intval($matches[1]);
345 }
346
353 public function getResponseBody ()
354 {
355 if (!$this->_sent) {
356 throw new CAS_OutOfSequenceException(
357 'Request has not been sent yet. Cannot '.__METHOD__
358 );
359 }
360
361 return $this->_responseBody;
362 }
363
370 public function getErrorMessage ()
371 {
372 if (!$this->_sent) {
373 throw new CAS_OutOfSequenceException(
374 'Request has not been sent yet. Cannot '.__METHOD__
375 );
376 }
377 return $this->_errorMessage;
378 }
379 }
CAS_Request_AbstractRequest\makePost
makePost()
Definition: AbstractRequest.php:164
CAS_Request_AbstractRequest\addCookies
addCookies(array $cookies)
Definition: AbstractRequest.php:109
CAS_Request_AbstractRequest\setSslCaCert
setSslCaCert($caCertPath, $validate_cn=true)
Definition: AbstractRequest.php:208
CAS_Request_AbstractRequest\$isPost
$isPost
Definition: AbstractRequest.php:47
CAS_Request_AbstractRequest\$validateCN
$validateCN
Definition: AbstractRequest.php:50
CAS_Request_AbstractRequest\addCookie
addCookie($name, $value)
Definition: AbstractRequest.php:88
CAS_Request_AbstractRequest\$cookies
$cookies
Definition: AbstractRequest.php:45
CAS_Request_AbstractRequest\$url
$url
Definition: AbstractRequest.php:44
CAS_Request_AbstractRequest\getErrorMessage
getErrorMessage()
Definition: AbstractRequest.php:370
CAS_Request_AbstractRequest\$postBody
$postBody
Definition: AbstractRequest.php:48
CAS_Request_AbstractRequest\setPostBody
setPostBody($body)
Definition: AbstractRequest.php:183
CAS_Request_RequestInterface
Definition: RequestInterface.php:40
CAS_Request_AbstractRequest\setUrl
setUrl($url)
Definition: AbstractRequest.php:68
CAS_Request_AbstractRequest\$_errorMessage
$_errorMessage
Definition: AbstractRequest.php:54
CAS_Request_AbstractRequest
Definition: AbstractRequest.php:40
CAS_Request_AbstractRequest\addHeader
addHeader($header)
Definition: AbstractRequest.php:128
CAS_OutOfSequenceException
Definition: OutOfSequenceException.php:44
CAS_Request_AbstractRequest\getResponseStatusCode
getResponseStatusCode()
Definition: AbstractRequest.php:326
CAS_Request_Exception
Definition: Exception.php:40
CAS_Request_AbstractRequest\storeErrorMessage
storeErrorMessage($message)
Definition: AbstractRequest.php:295
CAS_Request_AbstractRequest\getResponseBody
getResponseBody()
Definition: AbstractRequest.php:353
CAS_Request_AbstractRequest\sendRequest
sendRequest()
CAS_Request_AbstractRequest\$_responseBody
$_responseBody
Definition: AbstractRequest.php:53
CAS_Request_AbstractRequest\$caCertPath
$caCertPath
Definition: AbstractRequest.php:49
CAS_Request_AbstractRequest\addHeaders
addHeaders(array $headers)
Definition: AbstractRequest.php:147
CAS_Request_AbstractRequest\$_responseHeaders
$_responseHeaders
Definition: AbstractRequest.php:52
CAS_Request_AbstractRequest\storeResponseHeaders
storeResponseHeaders(array $headers)
Definition: AbstractRequest.php:259
CAS_Request_AbstractRequest\storeResponseHeader
storeResponseHeader($header)
Definition: AbstractRequest.php:271
CAS_Request_AbstractRequest\storeResponseBody
storeResponseBody($body)
Definition: AbstractRequest.php:283
CAS_Request_AbstractRequest\$headers
$headers
Definition: AbstractRequest.php:46
CAS_Request_AbstractRequest\send
send()
Definition: AbstractRequest.php:229
CAS_Request_AbstractRequest\$_sent
$_sent
Definition: AbstractRequest.php:51
CAS_Request_AbstractRequest\getResponseHeaders
getResponseHeaders()
Definition: AbstractRequest.php:310