+ return array($headers, $content);
+}
+
+/**
+ * Retrieve HTTP headers, following n redirections (temporary and permanent ones).
+ *
+ * @param string $url initial URL to reach.
+ * @param int $redirectionLimit max redirection follow.
+ *
+ * @return array HTTP headers, or false if it failed.
+ */
+function get_redirected_headers($url, $redirectionLimit = 3)
+{
+ $headers = get_headers($url, 1);
+ if (!empty($headers['location']) && empty($headers['Location'])) {
+ $headers['Location'] = $headers['location'];
+ }
+
+ // Headers found, redirection found, and limit not reached.
+ if ($redirectionLimit-- > 0
+ && !empty($headers)
+ && (strpos($headers[0], '301') !== false || strpos($headers[0], '302') !== false)
+ && !empty($headers['Location'])) {
+
+ $redirection = is_array($headers['Location']) ? end($headers['Location']) : $headers['Location'];
+ if ($redirection != $url) {
+ $redirection = getAbsoluteUrl($url, $redirection);
+ return get_redirected_headers($redirection, $redirectionLimit);
+ }
+ }
+
+ return array($headers, $url);
+}
+
+/**
+ * Get an absolute URL from a complete one, and another absolute/relative URL.
+ *
+ * @param string $originalUrl The original complete URL.
+ * @param string $newUrl The new one, absolute or relative.
+ *
+ * @return string Final URL:
+ * - $newUrl if it was already an absolute URL.
+ * - if it was relative, absolute URL from $originalUrl path.
+ */
+function getAbsoluteUrl($originalUrl, $newUrl)
+{
+ $newScheme = parse_url($newUrl, PHP_URL_SCHEME);
+ // Already an absolute URL.
+ if (!empty($newScheme)) {
+ return $newUrl;
+ }
+
+ $parts = parse_url($originalUrl);
+ $final = $parts['scheme'] .'://'. $parts['host'];
+ $final .= (!empty($parts['port'])) ? $parts['port'] : '';
+ $final .= '/';
+ if ($newUrl[0] != '/') {
+ $final .= substr(ltrim($parts['path'], '/'), 0, strrpos($parts['path'], '/'));
+ }
+ $final .= ltrim($newUrl, '/');
+ return $final;
+}
+
+/**
+ * Returns the server's base URL: scheme://domain.tld[:port]
+ *
+ * @param array $server the $_SERVER array
+ *
+ * @return string the server's base URL
+ *
+ * @see http://www.ietf.org/rfc/rfc7239.txt
+ * @see http://www.ietf.org/rfc/rfc6648.txt
+ * @see http://stackoverflow.com/a/3561399
+ * @see http://stackoverflow.com/q/452375
+ */
+function server_url($server)
+{
+ $scheme = 'http';
+ $port = '';
+
+ // Shaarli is served behind a proxy
+ if (isset($server['HTTP_X_FORWARDED_PROTO'])) {
+ // Keep forwarded scheme
+ if (strpos($server['HTTP_X_FORWARDED_PROTO'], ',') !== false) {
+ $schemes = explode(',', $server['HTTP_X_FORWARDED_PROTO']);
+ $scheme = trim($schemes[0]);
+ } else {
+ $scheme = $server['HTTP_X_FORWARDED_PROTO'];
+ }
+
+ if (isset($server['HTTP_X_FORWARDED_PORT'])) {
+ // Keep forwarded port
+ if (strpos($server['HTTP_X_FORWARDED_PORT'], ',') !== false) {
+ $ports = explode(',', $server['HTTP_X_FORWARDED_PORT']);
+ $port = trim($ports[0]);
+ } else {
+ $port = $server['HTTP_X_FORWARDED_PORT'];
+ }
+
+ // This is a workaround for proxies that don't forward the scheme properly.
+ // Connecting over port 443 has to be in HTTPS.
+ // See https://github.com/shaarli/Shaarli/issues/1022
+ if ($port == '443') {
+ $scheme = 'https';
+ }
+
+ 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.'://'.$host.$port;
+ }
+
+ // SSL detection
+ if ((! empty($server['HTTPS']) && strtolower($server['HTTPS']) == 'on')
+ || (isset($server['SERVER_PORT']) && $server['SERVER_PORT'] == '443')) {
+ $scheme = 'https';
+ }
+
+ // Do not append standard port values
+ if (($scheme == 'http' && $server['SERVER_PORT'] != '80')
+ || ($scheme == 'https' && $server['SERVER_PORT'] != '443')) {
+ $port = ':'.$server['SERVER_PORT'];
+ }
+
+ return $scheme.'://'.$server['SERVER_NAME'].$port;
+}
+
+/**
+ * Returns the absolute URL of the current script, without the query
+ *
+ * If the resource is "index.php", then it is removed (for better-looking URLs)
+ *
+ * @param array $server the $_SERVER array
+ *
+ * @return string the absolute URL of the current script, without the query
+ */
+function index_url($server)
+{
+ $scriptname = $server['SCRIPT_NAME'];
+ if (endsWith($scriptname, 'index.php')) {
+ $scriptname = substr($scriptname, 0, -9);
+ }
+ return server_url($server) . $scriptname;
+}
+
+/**
+ * Returns the absolute URL of the current script, with the query
+ *
+ * If the resource is "index.php", then it is removed (for better-looking URLs)
+ *
+ * @param array $server the $_SERVER array
+ *
+ * @return string the absolute URL of the current script, with the query
+ */
+function page_url($server)
+{
+ if (! empty($server['QUERY_STRING'])) {
+ return index_url($server).'?'.$server['QUERY_STRING'];
+ }
+ 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);
+}
+
+
+/**
+ * Return an identifier based on the advertised client IP address(es)
+ *
+ * This aims at preventing session hijacking from users behind the same proxy
+ * by relying on HTTP headers.
+ *
+ * See:
+ * - https://secure.php.net/manual/en/reserved.variables.server.php
+ * - https://stackoverflow.com/questions/3003145/how-to-get-the-client-ip-address-in-php
+ * - https://stackoverflow.com/questions/12233406/preventing-session-hijacking
+ * - https://stackoverflow.com/questions/21354859/trusting-x-forwarded-for-to-identify-a-visitor
+ *
+ * @param array $server The $_SERVER array
+ *
+ * @return string An identifier based on client IP address information
+ */
+function client_ip_id($server)
+{
+ $ip = $server['REMOTE_ADDR'];
+
+ if (isset($server['HTTP_X_FORWARDED_FOR'])) {
+ $ip = $ip . '_' . $server['HTTP_X_FORWARDED_FOR'];
+ }
+ if (isset($server['HTTP_CLIENT_IP'])) {
+ $ip = $ip . '_' . $server['HTTP_CLIENT_IP'];
+ }
+ return $ip;
+}
+
+
+/**
+ * Returns true if Shaarli's currently browsed in HTTPS.
+ * Supports reverse proxies (if the headers are correctly set).
+ *
+ * @param array $server $_SERVER.
+ *
+ * @return bool true if HTTPS, false otherwise.
+ */
+function is_https($server)
+{
+
+ if (isset($server['HTTP_X_FORWARDED_PORT'])) {
+ // Keep forwarded port
+ if (strpos($server['HTTP_X_FORWARDED_PORT'], ',') !== false) {
+ $ports = explode(',', $server['HTTP_X_FORWARDED_PORT']);
+ $port = trim($ports[0]);
+ } else {
+ $port = $server['HTTP_X_FORWARDED_PORT'];
+ }
+
+ if ($port == '443') {
+ return true;
+ }