phpCAS  version 1.4.0
AbstractRequest.php
Go to the documentation of this file.
1 <?php
2 
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) {
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) {
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  }
318  }
319 
327  public function getResponseStatusCode ()
328  {
329  if (!$this->_sent) {
330  throw new CAS_OutOfSequenceException(
331  'Request has not been sent yet. Cannot '.__METHOD__
332  );
333  }
334 
335  if (!preg_match(
336  '/HTTP\/[0-9.]+\s+([0-9]+)\s*(.*)/',
337  $this->_responseHeaders[0], $matches
338  )
339  ) {
340  throw new CAS_Request_Exception(
341  'Bad response, no status code was found in the first line.'
342  );
343  }
344 
345  return intval($matches[1]);
346  }
347 
354  public function getResponseBody ()
355  {
356  if (!$this->_sent) {
357  throw new CAS_OutOfSequenceException(
358  'Request has not been sent yet. Cannot '.__METHOD__
359  );
360  }
361 
362  return $this->_responseBody;
363  }
364 
371  public function getErrorMessage ()
372  {
373  if (!$this->_sent) {
374  throw new CAS_OutOfSequenceException(
375  'Request has not been sent yet. Cannot '.__METHOD__
376  );
377  }
378  return $this->_errorMessage;
379  }
380 }
setSslCaCert($caCertPath, $validate_cn=true)
storeResponseHeaders(array $headers)