ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
Abstract.php
Go to the documentation of this file.
1<?php
2
42{
48 protected $requestHandler;
49
55 private $_cookieJar;
56
65 public function __construct(
67 CAS_CookieJar $cookieJar
68 ) {
69 $this->requestHandler = $requestHandler;
70 $this->_cookieJar = $cookieJar;
71 }
72
77 private $_url;
78
85 public function getServiceUrl()
86 {
87 if (empty($this->_url)) {
89 'No URL set via ' . get_class($this) . '->setUrl($url).'
90 );
91 }
92
93 return $this->_url;
94 }
95
96 /*********************************************************
97 * Configure the Request
98 *********************************************************/
99
108 public function setUrl($url)
109 {
110 if ($this->hasBeenSent()) {
112 'Cannot set the URL, request already sent.'
113 );
114 }
115 if (!is_string($url)) {
116 throw new CAS_InvalidArgumentException('$url must be a string.');
117 }
118
119 $this->_url = $url;
120 }
121
122 /*********************************************************
123 * 2. Send the Request
124 *********************************************************/
125
139 public function send()
140 {
141 if ($this->hasBeenSent()) {
143 'Cannot send, request already sent.'
144 );
145 }
146
148
149 // Get our proxy ticket and append it to our URL.
150 $this->initializeProxyTicket();
151 $url = $this->getServiceUrl();
152 if (strstr($url, '?') === false) {
153 $url = $url . '?ticket=' . $this->getProxyTicket();
154 } else {
155 $url = $url . '&ticket=' . $this->getProxyTicket();
156 }
157
158 try {
159 $this->makeRequest($url);
160 } catch (Exception $e) {
162 throw $e;
163 }
164 }
165
171 private $_numRequests = 0;
172
178 private $_responseHeaders = array();
179
186
192 private $_responseBody = '';
193
208 protected function makeRequest($url)
209 {
210 // Verify that we are not in a redirect loop
211 $this->_numRequests++;
212 if ($this->_numRequests > 4) {
213 $message = 'Exceeded the maximum number of redirects (3) in proxied service request.';
216 }
217
218 // Create a new request.
219 $request = clone $this->requestHandler;
220 $request->setUrl($url);
221
222 // Add any cookies to the request.
223 $request->addCookies($this->_cookieJar->getCookies($url));
224
225 // Add any other parts of the request needed by concrete classes
226 $this->populateRequest($request);
227
228 // Perform the request.
229 phpCAS::trace('Performing proxied service request to \'' . $url . '\'');
230 if (!$request->send()) {
231 $message = 'Could not perform proxied service request to URL`'
232 . $url . '\'. ' . $request->getErrorMessage();
233 phpCAS::trace($message);
234 throw new CAS_ProxiedService_Exception($message);
235 }
236
237 // Store any cookies from the response;
238 $this->_cookieJar->storeCookies($url, $request->getResponseHeaders());
239
240 // Follow any redirects
241 if ($redirectUrl = $this->getRedirectUrl($request->getResponseHeaders())
242 ) {
243 phpCAS::trace('Found redirect:' . $redirectUrl);
244 $this->makeRequest($redirectUrl);
245 } else {
246 $this->_responseHeaders = $request->getResponseHeaders();
247 $this->_responseBody = $request->getResponseBody();
248 $this->_responseStatusCode = $request->getResponseStatusCode();
249 }
250 }
251
259 abstract protected function populateRequest(
260 CAS_Request_RequestInterface $request
261 );
262
270 protected function getRedirectUrl(array $responseHeaders)
271 {
272 // Check for the redirect after authentication
273 foreach ($responseHeaders as $header) {
274 if (preg_match('/^(Location:|URI:)\s*([^\s]+.*)$/', $header, $matches)
275 ) {
276 return trim(array_pop($matches));
277 }
278 }
279 return null;
280 }
281
282 /*********************************************************
283 * 3. Access the response
284 *********************************************************/
285
291 protected function hasBeenSent()
292 {
293 return ($this->_numRequests > 0);
294 }
295
302 public function getResponseHeaders()
303 {
304 if (!$this->hasBeenSent()) {
305 throw new CAS_OutOfSequenceException(
306 'Cannot access response, request not sent yet.'
307 );
308 }
309
310 return $this->_responseHeaders;
311 }
312
319 public function getResponseStatusCode()
320 {
321 if (!$this->hasBeenSent()) {
322 throw new CAS_OutOfSequenceException(
323 'Cannot access response, request not sent yet.'
324 );
325 }
326
327 return $this->_responseStatusCode;
328 }
329
336 public function getResponseBody()
337 {
338 if (!$this->hasBeenSent()) {
339 throw new CAS_OutOfSequenceException(
340 'Cannot access response, request not sent yet.'
341 );
342 }
343
344 return $this->_responseBody;
345 }
346
353 public function getCookies()
354 {
355 return $this->_cookieJar->getCookies($this->getServiceUrl());
356 }
357}
This class provides access to service cookies and handles parsing of response headers to pull out coo...
Definition: CookieJar.php:42
An exception for terminatinating execution or to throw for unit testing.
Exception that denotes invalid arguments were passed.
This class defines Exceptions that should be thrown when the sequence of operations is invalid.
This class implements common methods for ProxiedService implementations included with phpCAS.
Definition: Abstract.php:42
initializeProxyTicket()
Fetch our proxy ticket.
Definition: Abstract.php:132
getProxyTicket()
Answer the proxy ticket to be used when making requests.
Definition: Abstract.php:82
An Exception for problems communicating with a proxied service.
Definition: Exception.php:41
This class implements common methods for ProxiedService implementations included with phpCAS.
Definition: Abstract.php:42
$requestHandler
The HTTP request mechanism talking to the target service.
Definition: Abstract.php:48
$_numRequests
Indicator of the number of requests (including redirects performed.
Definition: Abstract.php:171
send()
Perform the request.
Definition: Abstract.php:139
makeRequest($url)
Build and perform a request, following redirects.
Definition: Abstract.php:208
hasBeenSent()
Answer true if our request has been sent yet.
Definition: Abstract.php:291
setUrl($url)
Set the URL of the Request.
Definition: Abstract.php:108
$_responseStatusCode
The response status code.
Definition: Abstract.php:185
$_responseBody
The response headers.
Definition: Abstract.php:192
__construct(CAS_Request_RequestInterface $requestHandler, CAS_CookieJar $cookieJar)
Constructor.
Definition: Abstract.php:65
getServiceUrl()
Answer a service identifier (URL) for whom we should fetch a proxy ticket.
Definition: Abstract.php:85
$_cookieJar
The storage mechanism for cookies set by the target service.
Definition: Abstract.php:55
populateRequest(CAS_Request_RequestInterface $request)
Add any other parts of the request needed by concrete classes.
$_url
The target service url.
Definition: Abstract.php:77
$_responseHeaders
The response headers.
Definition: Abstract.php:178
static trace($str)
This method is used to log something in debug mode.
Definition: CAS.php:599
static traceEnd($res='')
This method is used to indicate the end of the execution of a function in debug mode.
Definition: CAS.php:658
static traceBegin()
This method is used to indicate the start of the execution of a function in debug mode.
Definition: CAS.php:611
This interface defines methods that clients should use for configuring, sending, and receiving proxie...
Definition: Http.php:42
This interface defines a class library for performing web requests.
$url
$message
Definition: xapiexit.php:14