ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilHttpRequest.php
Go to the documentation of this file.
1 <?php
2 /*
3 +-----------------------------------------------------------------------------+
4 | ILIAS open source |
5 +-----------------------------------------------------------------------------+
6 | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7 | |
8 | This program is free software; you can redistribute it and/or |
9 | modify it under the terms of the GNU General Public License |
10 | as published by the Free Software Foundation; either version 2 |
11 | of the License, or (at your option) any later version. |
12 | |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software |
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21 +-----------------------------------------------------------------------------+
22 */
23 
34 {
35  var $_fp; // HTTP socket
36  var $_url; // full URL
37  var $_host; // HTTP host
38  var $_protocol; // protocol (HTTP/HTTPS)
39  var $_uri; // request URI
40  var $_port; // port
41 
42  // scan url
43  function _scan_url()
44  {
45  $req = $this->_url;
46 
47  $pos = strpos($req, '://');
48  $this->_protocol = strtolower(substr($req, 0, $pos));
49 
50  $req = substr($req, $pos+3);
51  $pos = strpos($req, '/');
52  if($pos === false) $pos = strlen($req);
53  $host = substr($req, 0, $pos);
54 
55  if(strpos($host, ':') !== false)
56  {
57  list($this->_host, $this->_port) = explode(':', $host);
58  }
59  else
60  {
61  $this->_host = $host;
62  $this->_port = ($this->_protocol == 'https') ? 443 : 80;
63  }
64 
65  $this->_uri = substr($req, $pos);
66  if($this->_uri == '') $this->_uri = '/';
67  }
68 
69  // constructor
70  function ilHttpRequest($url)
71  {
72  $this->_url = $url;
73  $this->_scan_url();
74  }
75 
76  // download URL to string
77  function downloadToString()
78  {
79  $crlf = "\r\n";
80 
81  // generate request
82  $req = 'GET ' . $this->_uri . ' HTTP/1.0' . $crlf
83  . 'Host: ' . $this->_host . $crlf
84  . $crlf;
85 
86  // fetch
87  $this->_fp = fsockopen(($this->_protocol == 'https' ? 'ssl://' : '') . $this->_host, $this->_port);
88  fwrite($this->_fp, $req);
89  while(is_resource($this->_fp) && $this->_fp && !feof($this->_fp))
90  {
91  $response .= fread($this->_fp, 1024);
92  }
93  fclose($this->_fp);
94 
95  // split header and body
96  $pos = strpos($response, $crlf . $crlf);
97  if($pos === false) return($response);
98  $header = substr($response, 0, $pos);
99  $body = substr($response, $pos + 2 * strlen($crlf));
100 
101  // parse headers
102  $headers = array();
103  $lines = explode($crlf, $header);
104  foreach($lines as $line)
105  {
106  if(($pos = strpos($line, ':')) !== false)
107  {
108  $headers[strtolower(trim(substr($line, 0, $pos)))] = trim(substr($line, $pos+1));
109  }
110  }
111 
112  // redirection?
113  if(isset($headers['location']))
114  {
115  $http = new ilHttpRequest($headers['location']);
116  return($http->DownloadToString($http));
117  }
118  else
119  {
120  return($body);
121  }
122  }
123 }
124 ?>