ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Sapi.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Sabre\HTTP;
4 
31 class Sapi {
32 
39  static function getRequest() {
40 
41  $r = self::createFromServerArray($_SERVER);
42  $r->setBody(fopen('php://input', 'r'));
43  $r->setPostData($_POST);
44  return $r;
45 
46  }
47 
57 
58  header('HTTP/' . $response->getHttpVersion() . ' ' . $response->getStatus() . ' ' . $response->getStatusText());
59  foreach ($response->getHeaders() as $key => $value) {
60 
61  foreach ($value as $k => $v) {
62  if ($k === 0) {
63  header($key . ': ' . $v);
64  } else {
65  header($key . ': ' . $v, false);
66  }
67  }
68 
69  }
70 
71  $body = $response->getBody();
72  if (is_null($body)) return;
73 
74  $contentLength = $response->getHeader('Content-Length');
75  if ($contentLength !== null) {
76  $output = fopen('php://output', 'wb');
77  if (is_resource($body) && get_resource_type($body) == 'stream') {
78  if (PHP_INT_SIZE !== 4){
79  // use the dedicated function on 64 Bit systems
80  stream_copy_to_stream($body, $output, $contentLength);
81  } else {
82  // workaround for 32 Bit systems to avoid stream_copy_to_stream
83  while (!feof($body)) {
84  fwrite($output, fread($body, 8192));
85  }
86  }
87  } else {
88  fwrite($output, $body, $contentLength);
89  }
90  } else {
91  file_put_contents('php://output', $body);
92  }
93 
94  if (is_resource($body)) {
95  fclose($body);
96  }
97 
98  }
99 
107  static function createFromServerArray(array $serverArray) {
108 
109  $headers = [];
110  $method = null;
111  $url = null;
112  $httpVersion = '1.1';
113 
114  $protocol = 'http';
115  $hostName = 'localhost';
116 
117  foreach ($serverArray as $key => $value) {
118 
119  switch ($key) {
120 
121  case 'SERVER_PROTOCOL' :
122  if ($value === 'HTTP/1.0') {
123  $httpVersion = '1.0';
124  }
125  break;
126  case 'REQUEST_METHOD' :
127  $method = $value;
128  break;
129  case 'REQUEST_URI' :
130  $url = $value;
131  break;
132 
133  // These sometimes show up without a HTTP_ prefix
134  case 'CONTENT_TYPE' :
135  $headers['Content-Type'] = $value;
136  break;
137  case 'CONTENT_LENGTH' :
138  $headers['Content-Length'] = $value;
139  break;
140 
141  // mod_php on apache will put credentials in these variables.
142  // (fast)cgi does not usually do this, however.
143  case 'PHP_AUTH_USER' :
144  if (isset($serverArray['PHP_AUTH_PW'])) {
145  $headers['Authorization'] = 'Basic ' . base64_encode($value . ':' . $serverArray['PHP_AUTH_PW']);
146  }
147  break;
148 
149  // Similarly, mod_php may also screw around with digest auth.
150  case 'PHP_AUTH_DIGEST' :
151  $headers['Authorization'] = 'Digest ' . $value;
152  break;
153 
154  // Apache may prefix the HTTP_AUTHORIZATION header with
155  // REDIRECT_, if mod_rewrite was used.
156  case 'REDIRECT_HTTP_AUTHORIZATION' :
157  $headers['Authorization'] = $value;
158  break;
159 
160  case 'HTTP_HOST' :
161  $hostName = $value;
162  $headers['Host'] = $value;
163  break;
164 
165  case 'HTTPS' :
166  if (!empty($value) && $value !== 'off') {
167  $protocol = 'https';
168  }
169  break;
170 
171  default :
172  if (substr($key, 0, 5) === 'HTTP_') {
173  // It's a HTTP header
174 
175  // Normalizing it to be prettier
176  $header = strtolower(substr($key, 5));
177 
178  // Transforming dashes into spaces, and uppercasing
179  // every first letter.
180  $header = ucwords(str_replace('_', ' ', $header));
181 
182  // Turning spaces into dashes.
183  $header = str_replace(' ', '-', $header);
184  $headers[$header] = $value;
185 
186  }
187  break;
188 
189 
190  }
191 
192  }
193 
194  $r = new Request($method, $url, $headers);
195  $r->setHttpVersion($httpVersion);
196  $r->setRawServerData($serverArray);
197  $r->setAbsoluteUrl($protocol . '://' . $hostName . $url);
198  return $r;
199 
200  }
201 
202 }
This interface represents a HTTP response.
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']
The Request class represents a single HTTP request.
Definition: Request.php:18
static getRequest()
This static method will create a new Request object, based on the current PHP request.
Definition: Sapi.php:39
PHP SAPI.
Definition: Sapi.php:31
$r
Definition: example_031.php:79
if(preg_match('#\.( $contentLength[^/\.]+)$#D', $path, $type)) if($contentType===null)
Definition: module.php:165
getHeaders()
Returns all the HTTP headers as an array.
getHeader($name)
Returns a specific HTTP header, based on it&#39;s name.
static sendResponse(ResponseInterface $response)
Sends the HTTP response back to a HTTP client.
Definition: Sapi.php:56
$url
static createFromServerArray(array $serverArray)
This static method will create a new Request object, based on a PHP $_SERVER array.
Definition: Sapi.php:107
$response
$key
Definition: croninfo.php:18
$_POST["username"]
getHttpVersion()
Returns the HTTP version.
getStatus()
Returns the current HTTP status code.
getBody()
Returns the message body, as it&#39;s internal representation.
getStatusText()
Returns the human-readable status string.