• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

setup/classes/class.ilHttpRequest.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003 +-----------------------------------------------------------------------------+
00004 | ILIAS open source                                                           |
00005 +-----------------------------------------------------------------------------+
00006 | Copyright (c) 1998-2001 ILIAS open source, University of Cologne            |
00007 |                                                                             |
00008 | This program is free software; you can redistribute it and/or               |
00009 | modify it under the terms of the GNU General Public License                 |
00010 | as published by the Free Software Foundation; either version 2              |
00011 | of the License, or (at your option) any later version.                      |
00012 |                                                                             |
00013 | This program is distributed in the hope that it will be useful,             |
00014 | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00015 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00016 | GNU General Public License for more details.                                |
00017 |                                                                             |
00018 | You should have received a copy of the GNU General Public License           |
00019 | along with this program; if not, write to the Free Software                 |
00020 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
00021 +-----------------------------------------------------------------------------+
00022 */
00023 
00033 class ilHttpRequest
00034 {
00035         var $_fp;        // HTTP socket
00036         var $_url;        // full URL
00037         var $_host;        // HTTP host
00038         var $_protocol;    // protocol (HTTP/HTTPS)
00039         var $_uri;        // request URI
00040         var $_port;        // port
00041         
00042         // scan url
00043         function _scan_url()
00044         {
00045                 $req = $this->_url;
00046                 
00047                 $pos = strpos($req, '://');
00048                 $this->_protocol = strtolower(substr($req, 0, $pos));
00049                 
00050                 $req = substr($req, $pos+3);
00051                 $pos = strpos($req, '/');
00052                 if($pos === false) $pos = strlen($req);
00053                 $host = substr($req, 0, $pos);
00054                 
00055                 if(strpos($host, ':') !== false)
00056                 {
00057                         list($this->_host, $this->_port) = explode(':', $host);
00058                 }
00059                 else
00060                 {
00061                         $this->_host = $host;
00062                         $this->_port = ($this->_protocol == 'https') ? 443 : 80;
00063                 }
00064                 
00065                 $this->_uri = substr($req, $pos);
00066                 if($this->_uri == '') $this->_uri = '/';
00067         }
00068         
00069         // constructor
00070         function ilHttpRequest($url)
00071         {
00072                 $this->_url = $url;
00073                 $this->_scan_url();
00074         }
00075         
00076         // download URL to string
00077         function downloadToString()
00078         {
00079                 $crlf = "\r\n";
00080                 
00081                 // generate request
00082                 $req = 'GET ' . $this->_uri . ' HTTP/1.0' . $crlf
00083                 . 'Host: ' . $this->_host . $crlf
00084                 .  $crlf;
00085 
00086                 // fetch
00087                 $this->_fp = fsockopen(($this->_protocol == 'https' ? 'ssl://' : '') . $this->_host, $this->_port);
00088                 fwrite($this->_fp, $req);
00089                 while(is_resource($this->_fp) && $this->_fp && !feof($this->_fp))
00090                 {
00091                         $response .= fread($this->_fp, 1024);
00092                 }
00093                 fclose($this->_fp);
00094                 
00095                 // split header and body
00096                 $pos = strpos($response, $crlf . $crlf);
00097                 if($pos === false) return($response);
00098                 $header = substr($response, 0, $pos);
00099                 $body = substr($response, $pos + 2 * strlen($crlf));
00100                 
00101                 // parse headers
00102                 $headers = array();
00103                 $lines = explode($crlf, $header);
00104                 foreach($lines as $line)
00105                 {
00106                         if(($pos = strpos($line, ':')) !== false)
00107                         {
00108                                 $headers[strtolower(trim(substr($line, 0, $pos)))] = trim(substr($line, $pos+1));
00109                         }
00110                 }
00111                 
00112                 // redirection?
00113                 if(isset($headers['location']))
00114                 {
00115                         $http = new ilHttpRequest($headers['location']);
00116                         return($http->DownloadToString($http));
00117                 }
00118                 else
00119                 {
00120                         return($body);
00121                 }
00122         }
00123 }
00124 ?>

Generated on Fri Dec 13 2013 17:57:03 for ILIAS Release_3_9_x_branch .rev 46835 by  doxygen 1.7.1