/**
* A class which checks and contains the basic
- * environment for the BaseURL (url, urlpath, ssl_policy, hostname)
+ * environment for the BaseURL (url, urlpath, ssl_policy, hostname, scheme)
*/
class BaseURL
{
*/
const SSL_POLICY_SELFSIGN = 2;
+ /**
+ * Define the Default SSL scheme
+ */
+ const DEFAULT_SSL_SCHEME = self::SSL_POLICY_SELFSIGN;
+
/**
* The Friendica Config
* @var Configuration
*/
public function get($ssl = false)
{
- return (!$ssl ? $this->url : $this->returnBaseURL($ssl));
+ if ($this->sslPolicy === self::SSL_POLICY_SELFSIGN && $ssl) {
+ return Network::switchScheme($this->url);
+ }
+
+ return $this->url;
}
/**
*/
public function save($hostname = null, $sslPolicy = null, $urlPath = null)
{
- $success = true;
+ $currHostname = $this->hostname;
+ $currSSLPolicy = $this->sslPolicy;
+ $currURLPath = $this->urlPath;
- if (!empty($hostname)) {
- $this->hostname = $hostname;
- if (!$this->config->set('config', 'hostname', $this->hostname)) {
- $success = false;
+ if (!empty($hostname) && $hostname !== $this->hostname) {
+ if ($this->config->set('config', 'hostname', $hostname)) {
+ $this->hostname = $hostname;
+ } else {
+ return false;
}
}
- if (isset($sslPolicy)) {
- $this->sslPolicy = $sslPolicy;
- if (!$this->config->set('system', 'ssl_policy', $this->sslPolicy)) {
- $success = false;
+ if (isset($sslPolicy) && $sslPolicy !== $this->sslPolicy) {
+ if ($this->config->set('system', 'ssl_policy', $sslPolicy)) {
+ $this->sslPolicy = $sslPolicy;
+ } else {
+ $this->hostname = $currHostname;
+ $this->config->set('config', 'hostname', $this->hostname);
+ return false;
}
}
- if (isset($urlPath)) {
- $this->urlPath = $urlPath;
- if (!$this->config->set('system', 'urlpath', $this->urlPath)) {
- $success = false;
+ if (isset($urlPath) && $urlPath !== $this->urlPath) {
+ if ($this->config->set('system', 'urlpath', $urlPath)) {
+ $this->urlPath = $urlPath;
+ } else {
+ $this->hostname = $currHostname;
+ $this->sslPolicy = $currSSLPolicy;
+ $this->config->set('config', 'hostname', $this->hostname);
+ $this->config->set('system', 'ssl_policy', $this->sslPolicy);
+ return false;
}
}
$this->determineBaseUrl();
if (!$this->config->set('system', 'url', $this->url)) {
- $success = false;
+ $this->hostname = $currHostname;
+ $this->sslPolicy = $currSSLPolicy;
+ $this->urlPath = $currURLPath;
+ $this->determineBaseUrl();
+
+ $this->config->set('config', 'hostname', $this->hostname);
+ $this->config->set('system', 'ssl_policy', $this->sslPolicy);
+ $this->config->set('system', 'urlpath', $this->urlPath);
+ return false;
}
- return $success;
+ return true;
}
/**
$urlPath = trim($parsed['path'], '\\/');
}
- return $this->save($hostname, null, $urlPath);
+ $sslPolicy = null;
+ if (!empty($parsed['scheme'])) {
+ if ($parsed['scheme'] == 'https') {
+ $sslPolicy = BaseURL::SSL_POLICY_FULL;
+ }
+ }
+
+ return $this->save($hostname, $sslPolicy, $urlPath);
+ }
+
+ /**
+ * Checks, if a redirect to the HTTPS site would be necessary
+ *
+ * @return bool
+ */
+ public function checkRedirectHttps()
+ {
+ return $this->config->get('system', 'force_ssl')
+ && ($this->getScheme() == "http")
+ && intval($this->getSSLPolicy()) == BaseURL::SSL_POLICY_FULL
+ && strpos($this->get(), 'https://') === 0
+ && !empty($this->server['REQUEST_METHOD'])
+ && $this->server['REQUEST_METHOD'] === 'GET';
}
/**
$this->config = $config;
$this->server = $server;
- $this->checkConfig();
$this->determineSchema();
+ $this->checkConfig();
}
/**
*/
public function checkConfig()
{
- $this->hostname = $this->config->get('config', 'hostname', null);
- $this->urlPath = $this->config->get('system', 'urlpath', null);
- $this->sslPolicy = $this->config->get('system', 'ssl_policy', null);
- $this->url = $this->config->get('system', 'url', null);
+ $this->hostname = $this->config->get('config', 'hostname');
+ $this->urlPath = $this->config->get('system', 'urlpath');
+ $this->sslPolicy = $this->config->get('system', 'ssl_policy');
+ $this->url = $this->config->get('system', 'url');
if (empty($this->hostname)) {
$this->determineHostname();
}
if (!isset($this->sslPolicy)) {
- $this->sslPolicy = self::SSL_POLICY_NONE;
+ if ($this->scheme == 'https') {
+ $this->sslPolicy = self::SSL_POLICY_FULL;
+ } else {
+ $this->sslPolicy = self::DEFAULT_SSL_SCHEME;
+ }
$this->config->set('system', 'ssl_policy', $this->sslPolicy);
}
if (empty($this->url)) {
$this->determineBaseUrl();
- if (!empty($url)) {
+ if (!empty($this->url)) {
$this->config->set('system', 'url', $this->url);
}
}
$this->scheme = 'https';
}
}
-
- /**
- * Returns the URL based on the current used ssl setting
- *
- * @param bool $ssl true, if ssl should be used
- *
- * @return string
- */
- private function returnBaseURL($ssl)
- {
- if ($this->sslPolicy == self::SSL_POLICY_SELFSIGN && $ssl) {
- return Network::switchScheme($this->url);
- }
-
- return $this->url;
- }
}