FusionDirectory
class_URL.inc
1 <?php
2 /*
3  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
4 
5  Copyright (C) 2018-2019 FusionDirectory
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21 
27 class URL
28 {
31  public static function sslOn (): bool
32  {
33  if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
34  return (strcasecmp($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') == 0);
35  }
36  if (isset($_SERVER['HTTPS'])) {
37  return (strcasecmp($_SERVER['HTTPS'], 'on') == 0);
38  }
39  return FALSE;
40  }
41 
42  protected static function gatherInfos (): array
43  {
44  $protocol = 'http';
45  if (static::sslOn()) {
46  $protocol .= 's';
47  }
48 
49  if (!empty($_SERVER['HTTP_X_FORWARDED_HOST'])) {
50  $host = $_SERVER['HTTP_X_FORWARDED_HOST'];
51  if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
52  $protocol = $_SERVER['HTTP_X_FORWARDED_PROTO'];
53  }
54  if (isset($_SERVER['HTTP_X_FORWARDED_PORT'])) {
55  $port = $_SERVER['HTTP_X_FORWARDED_PORT'];
56  } else {
57  $port = ($protocol === 'http' ? 80 : 443);
58  }
59  } else {
60  if (!empty($_SERVER['HTTP_HOST'])) {
61  $host = $_SERVER['HTTP_HOST'];
62  } else {
63  $host = $_SERVER['SERVER_NAME'];
64  }
65  $port = $_SERVER['SERVER_PORT'];
66  }
67 
68  return [$protocol, $host, $port];
69  }
70 
73  public static function getSslUrl (): string
74  {
75  list(, $host, $port) = static::gatherInfos();
76  $ssl = 'https://'.$host;
77 
78  if (!empty($_SERVER['REQUEST_URI'])) {
79  $ssl .= $_SERVER['REQUEST_URI'];
80  } elseif (!empty($_SERVER['PATH_INFO'])) {
81  $ssl .= $_SERVER['PATH_INFO'];
82  } else {
83  $ssl .= $_SERVER['PHP_SELF'];
84  }
85  return $ssl;
86  }
87 
90  public static function getPageURL ($queryString = FALSE): string
91  {
92  list($protocol, $host, $port) = static::gatherInfos();
93 
94  $pageURL = $protocol.'://'.$host;
95  if ((($protocol == 'http') && ($port != '80')) || (($protocol == 'https') && ($port != '443'))) {
96  $pageURL .= ':'.$port;
97  }
98  if (!empty($_SERVER['REQUEST_URI']) && $queryString) {
99  $pageURL .= $_SERVER['REQUEST_URI'];
100  } elseif (!empty($_SERVER['PATH_INFO'])) {
101  $pageURL .= $_SERVER['PATH_INFO'];
102  } else {
103  $pageURL .= $_SERVER['PHP_SELF'];
104  }
105 
106  return $pageURL;
107  }
108 
111  public static function getHostName (): string
112  {
113  list($protocol, $host, $port) = static::gatherInfos();
114 
115  if ((($protocol == 'http') && ($port != '80')) || (($protocol == 'https') && ($port != '443'))) {
116  $host .= ':'.$port;
117  }
118 
119  return $host;
120  }
121 
124  public static function buildAbsoluteUrl (string $path): string
125  {
126  return preg_replace('|/[^/]*$|', $path, static::getPageURL(FALSE));
127  }
128 }
static getHostName()
Returns hostname to identify this website.
Definition: class_URL.inc:111
Class URL Static methods to get/build URLs.
Definition: class_URL.inc:27
static getSslUrl()
Returns SSL URL to redirect to.
Definition: class_URL.inc:73
static sslOn()
Returns TRUE if SSL was used to contact FD, whether directly or through a proxy.
Definition: class_URL.inc:31
static buildAbsoluteUrl(string $path)
Returns absolute URL from relative URL.
Definition: class_URL.inc:124
static getPageURL($queryString=FALSE)
Returns current page URL.
Definition: class_URL.inc:90