00001 <?php 00002 /* 00003 * Copyright © 2003-2010, The ESUP-Portail consortium & the JA-SIG Collaborative. 00004 * All rights reserved. 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions are met: 00008 * 00009 * * Redistributions of source code must retain the above copyright notice, 00010 * this list of conditions and the following disclaimer. 00011 * * Redistributions in binary form must reproduce the above copyright notice, 00012 * this list of conditions and the following disclaimer in the documentation 00013 * and/or other materials provided with the distribution. 00014 * * Neither the name of the ESUP-Portail consortium & the JA-SIG 00015 * Collaborative nor the names of its contributors may be used to endorse or 00016 * promote products derived from this software without specific prior 00017 * written permission. 00018 00019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 00020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 00023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 00026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 */ 00030 00031 require_once dirname(__FILE__).'/RequestInterface.php'; 00032 00036 abstract class CAS_AbstractRequest 00037 implements CAS_RequestInterface 00038 { 00039 00040 protected $url = null; 00041 protected $cookies = array(); 00042 protected $headers = array(); 00043 protected $isPost = FALSE; 00044 protected $postBody = null; 00045 protected $caCertPath = null; 00046 private $sent = FALSE; 00047 private $responseHeaders = array(); 00048 private $responseBody = null; 00049 private $errorMessage = ''; 00050 00051 /********************************************************* 00052 * Configure the Request 00053 *********************************************************/ 00054 00062 public function setUrl ($url) { 00063 if ($this->sent) 00064 throw new CAS_OutOfSequenceException('Request has already been sent cannot '.__METHOD__); 00065 00066 $this->url = $url; 00067 } 00068 00077 public function addCookie ($name, $value) { 00078 if ($this->sent) 00079 throw new CAS_OutOfSequenceException('Request has already been sent cannot '.__METHOD__); 00080 00081 $this->cookies[$name] = $value; 00082 } 00083 00093 public function addCookies (array $cookies) { 00094 if ($this->sent) 00095 throw new CAS_OutOfSequenceException('Request has already been sent cannot '.__METHOD__); 00096 00097 $this->cookies = array_merge($this->cookies, $cookies); 00098 } 00099 00107 public function addHeader ($header) { 00108 if ($this->sent) 00109 throw new CAS_OutOfSequenceException('Request has already been sent cannot '.__METHOD__); 00110 00111 $this->headers[] = $header; 00112 } 00113 00121 public function addHeaders (array $headers) { 00122 if ($this->sent) 00123 throw new CAS_OutOfSequenceException('Request has already been sent cannot '.__METHOD__); 00124 00125 $this->headers = array_merge($this->headers, $headers); 00126 } 00127 00134 public function makePost () { 00135 if ($this->sent) 00136 throw new CAS_OutOfSequenceException('Request has already been sent cannot '.__METHOD__); 00137 00138 $this->isPost = TRUE; 00139 } 00140 00148 public function setPostBody ($body) { 00149 if ($this->sent) 00150 throw new CAS_OutOfSequenceException('Request has already been sent cannot '.__METHOD__); 00151 if (!$this->isPost) 00152 throw new CAS_OutOfSequenceException('Cannot add a POST body to a GET request, use makePost() first.'); 00153 00154 $this->postBody = $body; 00155 } 00156 00164 public function setSslCaCert ($caCertPath) { 00165 if ($this->sent) 00166 throw new CAS_OutOfSequenceException('Request has already been sent cannot '.__METHOD__); 00167 00168 $this->caCertPath = $caCertPath; 00169 } 00170 00171 /********************************************************* 00172 * 2. Send the Request 00173 *********************************************************/ 00174 00181 public function send () { 00182 if ($this->sent) 00183 throw new CAS_OutOfSequenceException('Request has already been sent cannot send again.'); 00184 if (is_null($this->url) || !$this->url) 00185 throw new CAS_OutOfSequenceException('A url must be specified via setUrl() before the request can be sent.'); 00186 00187 $this->sent = true; 00188 return $this->_sendRequest(); 00189 } 00190 00196 abstract protected function _sendRequest (); 00197 00204 protected function storeResponseHeaders (array $headers) { 00205 $this->responseHeaders = array_merge($this->responseHeaders, $headers); 00206 } 00207 00214 protected function storeResponseHeader ($header) { 00215 $this->responseHeaders[] = $header; 00216 } 00217 00224 protected function storeResponseBody ($body) { 00225 $this->responseBody = $body; 00226 } 00227 00234 protected function storeErrorMessage ($message) { 00235 $this->errorMessage .= $message; 00236 } 00237 00238 /********************************************************* 00239 * 3. Access the response 00240 *********************************************************/ 00241 00248 public function getResponseHeaders () { 00249 if (!$this->sent) 00250 throw new CAS_OutOfSequenceException('Request has not been sent yet. Cannot '.__METHOD__); 00251 00252 return $this->responseHeaders; 00253 } 00254 00261 public function getResponseBody () { 00262 if (!$this->sent) 00263 throw new CAS_OutOfSequenceException('Request has not been sent yet. Cannot '.__METHOD__); 00264 00265 return $this->responseBody; 00266 } 00267 00274 public function getErrorMessage () { 00275 if (!$this->sent) 00276 throw new CAS_OutOfSequenceException('Request has not been sent yet. Cannot '.__METHOD__); 00277 00278 return $this->errorMessage; 00279 } 00280 }