3 namespace Friendica\Util;
5 use Friendica\Core\Config\Configuration;
8 * A class which checks and contains the basic
9 * environment for the BaseURL (url, urlpath, ssl_policy, hostname, scheme)
16 const SSL_POLICY_NONE = 0;
21 const SSL_POLICY_FULL = 1;
24 * SSL is optional, but preferred
26 const SSL_POLICY_SELFSIGN = 2;
29 * Define the Default SSL scheme
31 const DEFAULT_SSL_SCHEME = self::SSL_POLICY_SELFSIGN;
34 * The Friendica Config
40 * The server side variables
46 * The hostname of the Base URL
52 * The SSL_POLICY of the Base URL
58 * The URL sub-path of the Base URL
70 * The current scheme of this call
76 * Returns the hostname of this node
79 public function getHostname()
81 return $this->hostname;
85 * Returns the current scheme of this call
88 public function getScheme()
94 * Returns the SSL policy of this node
97 public function getSSLPolicy()
99 return $this->sslPolicy;
103 * Returns the sub-path of this URL
106 public function getUrlPath()
108 return $this->urlPath;
112 * Returns the full URL of this call
114 * Note: $ssl parameter value doesn't directly correlate with the resulting protocol
116 * @param bool $ssl True, if ssl should get used
120 public function get($ssl = false)
122 if ($this->sslPolicy === self::SSL_POLICY_SELFSIGN && $ssl) {
123 return Network::switchScheme($this->url);
130 * Save current parts of the base Url
132 * @param string? $hostname
133 * @param int? $sslPolicy
134 * @param string? $urlPath
136 * @return bool true, if successful
138 public function save($hostname = null, $sslPolicy = null, $urlPath = null)
142 if (!empty($hostname) && $hostname !== $this->hostname) {
143 $this->hostname = $hostname;
144 if (!$this->config->set('config', 'hostname', $this->hostname)) {
149 if (isset($sslPolicy) && $sslPolicy !== $this->sslPolicy) {
150 $this->sslPolicy = $sslPolicy;
151 if (!$this->config->set('system', 'ssl_policy', $this->sslPolicy)) {
156 if (isset($urlPath) && $urlPath !== $this->urlPath) {
157 $this->urlPath = $urlPath;
158 if (!$this->config->set('system', 'urlpath', $this->urlPath)) {
163 $this->determineBaseUrl();
164 if (!$this->config->set('system', 'url', $this->url)) {
172 * Save the current url as base URL
176 * @return bool true, if the save was successful
178 public function saveByURL($url)
180 $parsed = @parse_url($url);
182 if (empty($parsed)) {
186 $hostname = $parsed['host'];
187 if (!empty($hostname) && !empty($parsed['port'])) {
188 $hostname .= ':' . $parsed['port'];
192 if (!empty($parsed['path'])) {
193 $urlPath = trim($parsed['path'], '\\/');
197 if (!empty($parsed['scheme'])) {
198 if ($parsed['scheme'] == 'https') {
199 $sslPolicy = BaseURL::SSL_POLICY_FULL;
203 return $this->save($hostname, $sslPolicy, $urlPath);
207 * Checks, if a redirect to the HTTPS site would be necessary
211 public function checkRedirectHttps()
213 return $this->config->get('system', 'force_ssl')
214 && ($this->getScheme() == "http")
215 && intval($this->getSSLPolicy()) == BaseURL::SSL_POLICY_FULL
216 && strpos($this->get(), 'https://') === 0
217 && !empty($this->server['REQUEST_METHOD'])
218 && $this->server['REQUEST_METHOD'] === 'GET';
222 * @param Configuration $config The Friendica configuration
223 * @param array $server The $_SERVER array
225 public function __construct(Configuration $config, array $server)
227 $this->config = $config;
228 $this->server = $server;
230 $this->determineSchema();
231 $this->checkConfig();
235 * Check the current config during loading
237 public function checkConfig()
239 $this->hostname = $this->config->get('config', 'hostname');
240 $this->urlPath = $this->config->get('system', 'urlpath');
241 $this->sslPolicy = $this->config->get('system', 'ssl_policy');
242 $this->url = $this->config->get('system', 'url');
244 if (empty($this->hostname)) {
245 $this->determineHostname();
247 if (!empty($this->hostname)) {
248 $this->config->set('config', 'hostname', $this->hostname);
252 if (!isset($this->urlPath)) {
253 $this->determineURLPath();
254 $this->config->set('system', 'urlpath', $this->urlPath);
257 if (!isset($this->sslPolicy)) {
258 if ($this->scheme == 'https') {
259 $this->sslPolicy = self::SSL_POLICY_FULL;
261 $this->sslPolicy = self::DEFAULT_SSL_SCHEME;
263 $this->config->set('system', 'ssl_policy', $this->sslPolicy);
266 if (empty($this->url)) {
267 $this->determineBaseUrl();
270 $this->config->set('system', 'url', $this->url);
276 * Determines the hostname of this node if not set already
278 private function determineHostname()
280 $this->hostname = '';
282 if (!empty($this->server['SERVER_NAME'])) {
283 $this->hostname = $this->server['SERVER_NAME'];
285 if (!empty($this->server['SERVER_PORT']) && $this->server['SERVER_PORT'] != 80 && $this->server['SERVER_PORT'] != 443) {
286 $this->hostname .= ':' . $this->server['SERVER_PORT'];
292 * Figure out if we are running at the top of a domain or in a sub-directory
294 private function determineURLPath()
299 * The automatic path detection in this function is currently deactivated,
300 * see issue https://github.com/friendica/friendica/issues/6679
302 * The problem is that the function seems to be confused with some url.
303 * These then confuses the detection which changes the url path.
306 /* Relative script path to the web server root
307 * Not all of those $_SERVER properties can be present, so we do by inverse priority order
309 $relative_script_path = '';
310 $relative_script_path = defaults($this->server, 'REDIRECT_URL', $relative_script_path);
311 $relative_script_path = defaults($this->server, 'REDIRECT_URI', $relative_script_path);
312 $relative_script_path = defaults($this->server, 'REDIRECT_SCRIPT_URL', $relative_script_path);
313 $relative_script_path = defaults($this->server, 'SCRIPT_URL', $relative_script_path);
314 $relative_script_path = defaults($this->server, 'REQUEST_URI', $relative_script_path);
316 /* $relative_script_path gives /relative/path/to/friendica/module/parameter
317 * QUERY_STRING gives pagename=module/parameter
319 * To get /relative/path/to/friendica we perform dirname() for as many levels as there are slashes in the QUERY_STRING
321 if (!empty($relative_script_path)) {
323 if (!empty($this->server['QUERY_STRING'])) {
324 $this->urlPath = trim(rdirname($relative_script_path, substr_count(trim($this->server['QUERY_STRING'], '/'), '/') + 1), '/');
327 $this->urlPath = trim($relative_script_path, '/');
333 * Determine the full URL based on all parts
335 private function determineBaseUrl()
339 if ($this->sslPolicy == self::SSL_POLICY_FULL) {
343 $this->url = $scheme . '://' . $this->hostname . (!empty($this->urlPath) ? '/' . $this->urlPath : '' );
347 * Determine the scheme of the current used link
349 private function determineSchema()
351 $this->scheme = 'http';
353 if (!empty($this->server['HTTPS']) ||
354 !empty($this->server['HTTP_FORWARDED']) && preg_match('/proto=https/', $this->server['HTTP_FORWARDED']) ||
355 !empty($this->server['HTTP_X_FORWARDED_PROTO']) && $this->server['HTTP_X_FORWARDED_PROTO'] == 'https' ||
356 !empty($this->server['HTTP_X_FORWARDED_SSL']) && $this->server['HTTP_X_FORWARDED_SSL'] == 'on' ||
357 !empty($this->server['FRONT_END_HTTPS']) && $this->server['FRONT_END_HTTPS'] == 'on' ||
358 !empty($this->server['SERVER_PORT']) && (intval($this->server['SERVER_PORT']) == 443) // XXX: reasonable assumption, but isn't this hardcoding too much?
360 $this->scheme = 'https';