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 require_once dirname(__FILE__).'/AbstractRequest.php'; 00033 00037 class CAS_CurlRequest 00038 extends CAS_AbstractRequest 00039 implements CAS_RequestInterface 00040 { 00041 00048 public function setCurlOptions (array $options) { 00049 $this->curlOptions = $options; 00050 } 00051 private $curlOptions = array(); 00052 00058 protected function _sendRequest () { 00059 phpCAS::traceBegin(); 00060 00061 /********************************************************* 00062 * initialize the CURL session 00063 *********************************************************/ 00064 $ch = curl_init($this->url); 00065 00066 if (version_compare(PHP_VERSION,'5.1.3','>=')) { 00067 //only avaible in php5 00068 curl_setopt_array($ch, $this->curlOptions); 00069 } else { 00070 foreach ($this->curlOptions as $key => $value) { 00071 curl_setopt($ch, $key, $value); 00072 } 00073 } 00074 00075 /********************************************************* 00076 * Set SSL configuration 00077 *********************************************************/ 00078 if ($this->caCertPath) { 00079 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1); 00080 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1); 00081 curl_setopt($ch, CURLOPT_CAINFO, $this->caCertPath); 00082 phpCAS::trace('CURL: Set CURLOPT_CAINFO'); 00083 } else { 00084 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1); 00085 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 00086 } 00087 00088 /********************************************************* 00089 * Configure curl to capture our output. 00090 *********************************************************/ 00091 // return the CURL output into a variable 00092 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 00093 00094 // get the HTTP header with a callback 00095 curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, '_curlReadHeaders')); 00096 00097 /********************************************************* 00098 * Add cookie headers to our request. 00099 *********************************************************/ 00100 if (count($this->cookies)) { 00101 $cookieStrings = array(); 00102 foreach ($this->cookies as $name => $val) { 00103 $cookieStrings[] = $name.'='.$val; 00104 } 00105 curl_setopt($ch, CURLOPT_COOKIE, implode(';', $cookieStrings)); 00106 } 00107 00108 /********************************************************* 00109 * Add any additional headers 00110 *********************************************************/ 00111 if (count($this->headers)) { 00112 curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers); 00113 } 00114 00115 /********************************************************* 00116 * Flag and Body for POST requests 00117 *********************************************************/ 00118 if ($this->isPost) { 00119 curl_setopt($ch, CURLOPT_POST, 1); 00120 curl_setopt($ch, CURLOPT_POSTFIELDS, $this->postBody); 00121 } 00122 00123 /********************************************************* 00124 * Perform the query 00125 *********************************************************/ 00126 $buf = curl_exec ($ch); 00127 if ( $buf === FALSE ) { 00128 phpCAS::trace('curl_exec() failed'); 00129 $this->storeErrorMessage('CURL error #'.curl_errno($ch).': '.curl_error($ch)); 00130 $res = FALSE; 00131 } else { 00132 $this->storeResponseBody($buf); 00133 phpCAS::trace("Response Body: \n".$buf."\n"); 00134 $res = TRUE; 00135 00136 } 00137 // close the CURL session 00138 curl_close ($ch); 00139 00140 phpCAS::traceEnd($res); 00141 return $res; 00142 } 00143 00151 public function _curlReadHeaders ($ch, $header) { 00152 $this->storeResponseHeader($header); 00153 return strlen($header); 00154 } 00155 }