• Main Page
  • Related Pages
  • Modules
  • Classes
  • Files
  • Examples
  • File List
  • File Members

CAS/Request/CurlRequest.php

Go to the documentation of this file.
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                         curl_setopt($ch, CURLOPT_COOKIE, implode(';', $this->cookies));
00102                 }
00103 
00104                 /*********************************************************
00105                  * Add any additional headers
00106                  *********************************************************/
00107                 if (count($this->headers)) {
00108                         curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
00109                 }
00110 
00111                 /*********************************************************
00112                  * Flag and Body for POST requests
00113                  *********************************************************/
00114                 if ($this->isPost) {
00115                         curl_setopt($ch, CURLOPT_POST, 1);
00116                         curl_setopt($ch, CURLOPT_POSTFIELDS, $this->postBody);
00117                 }
00118 
00119                 /*********************************************************
00120                  * Perform the query
00121                  *********************************************************/
00122                 $buf = curl_exec ($ch);
00123                 if ( $buf === FALSE ) {
00124                         phpCAS::trace('curl_exec() failed');
00125                         $this->storeErrorMessage('CURL error #'.curl_errno($ch).': '.curl_error($ch));
00126                         $res = FALSE;
00127                 } else {
00128                         $this->storeResponseBody($buf);
00129                         phpCAS::trace("Response Body: \n".$buf."\n");
00130                         $res = TRUE;
00131 
00132                 }
00133                 // close the CURL session
00134                 curl_close ($ch);
00135 
00136                 phpCAS::traceEnd($res);
00137                 return $res;
00138         }
00139 
00147         public function _curlReadHeaders ($ch, $header) {
00148                 $this->storeResponseHeader($header);
00149                 return strlen($header);
00150         }
00151 }

Generated on Sat Mar 26 2011 12:11:03 for phpCAS by  doxygen 1.7.1