$content = substr($response, $headSize);
$headers = array();
foreach (preg_split('~[\r\n]+~', $rawHeadersLastRedir) as $line) {
- if (empty($line) or ctype_space($line)) {
+ if (empty($line) || ctype_space($line)) {
continue;
}
$splitLine = explode(': ', $line, 2);
// Keep forwarded port
if (strpos($server['HTTP_X_FORWARDED_PORT'], ',') !== false) {
$ports = explode(',', $server['HTTP_X_FORWARDED_PORT']);
- $port = ':' . trim($ports[0]);
+ $port = trim($ports[0]);
} else {
- $port = ':' . $server['HTTP_X_FORWARDED_PORT'];
+ $port = $server['HTTP_X_FORWARDED_PORT'];
}
+
+ if (($scheme == 'http' && $port != '80')
+ || ($scheme == 'https' && $port != '443')
+ ) {
+ $port = ':' . $port;
+ } else {
+ $port = '';
+ }
+ }
+
+ if (isset($server['HTTP_X_FORWARDED_HOST'])) {
+ // Keep forwarded host
+ if (strpos($server['HTTP_X_FORWARDED_HOST'], ',') !== false) {
+ $hosts = explode(',', $server['HTTP_X_FORWARDED_HOST']);
+ $host = trim($hosts[0]);
+ } else {
+ $host = $server['HTTP_X_FORWARDED_HOST'];
+ }
+ } else {
+ $host = $server['SERVER_NAME'];
}
- return $scheme.'://'.$server['SERVER_NAME'].$port;
+ return $scheme.'://'.$host.$port;
}
// SSL detection
}
return index_url($server);
}
+
+/**
+ * Retrieve the initial IP forwarded by the reverse proxy.
+ *
+ * Inspired from: https://github.com/zendframework/zend-http/blob/master/src/PhpEnvironment/RemoteAddress.php
+ *
+ * @param array $server $_SERVER array which contains HTTP headers.
+ * @param array $trustedIps List of trusted IP from the configuration.
+ *
+ * @return string|bool The forwarded IP, or false if none could be extracted.
+ */
+function getIpAddressFromProxy($server, $trustedIps)
+{
+ $forwardedIpHeader = 'HTTP_X_FORWARDED_FOR';
+ if (empty($server[$forwardedIpHeader])) {
+ return false;
+ }
+
+ $ips = preg_split('/\s*,\s*/', $server[$forwardedIpHeader]);
+ $ips = array_diff($ips, $trustedIps);
+ if (empty($ips)) {
+ return false;
+ }
+
+ return array_pop($ips);
+}