- unset($this->config[$configKey]);
- }
-
- /**
- * Detects the server address (SERVER_ADDR) and set it in configuration
- *
- * @return $serverAddress The detected server address
- * @throws UnknownHostnameException If SERVER_NAME cannot be resolved to an IP address
- * @todo Have to check some more entries from $_SERVER here
- */
- public function detectServerAddress () {
- // Is the entry set?
- if (!$this->isConfigurationEntrySet('server_addr')) {
- // Is it set in $_SERVER?
- if (isset($_SERVER['SERVER_ADDR'])) {
- // Set it from $_SERVER
- $this->setServerAddress($_SERVER['SERVER_ADDR']);
- } elseif (isset($_SERVER['SERVER_NAME'])) {
- // Resolve IP address
- $serverIp = ConsoleTools::resolveIpAddress($_SERVER['SERVER_NAME']);
-
- // Is it valid?
- if ($serverIp === false) {
- /*
- * Why is gethostbyname() returning the host name and not
- * false as many other PHP functions are doing? ;-(
- */
- throw new UnknownHostnameException(sprintf('Cannot resolve "%s" to an IP address. Please fix your setup.', $_SERVER['SERVER_NAME']));
- } // END - if
-
- // Al fine, set it
- $this->setServerAddress($serverIp);
- } else {
- // Run auto-detecting through console tools lib
- ConsoleTools::acquireSelfIpAddress();
- }
- } // END - if
-
- // Now get it from configuration
- $serverAddress = $this->getServerAddress();
-
- // Return it
- return $serverAddress;
- }
-
- /**
- * Setter for SERVER_ADDR
- *
- * @param $serverAddress New SERVER_ADDR value to set
- * @return void
- */
- public function setServerAddress ($serverAddress) {
- // Is a valid configuration key key provided?
- if (is_null($serverAddress)) {
- // Configuration key is null
- throw new NullPointerException($this, BaseFrameworkSystem::EXCEPTION_IS_NULL_POINTER);
- } elseif (!is_string($serverAddress)) {
- // Is not a string
- throw new InvalidArgumentException(sprintf('serverAddress[]=%s is not a string', gettype($serverAddress)), self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
- } elseif (empty($serverAddress)) {
- // Entry is empty
- throw new InvalidArgumentException('serverAddress is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
- }
-
- // Set it, please don't do it yourself here
- $this->setConfigEntry('server_addr', (string) $serverAddress);
- }
-
- /**
- * Getter for SERVER_ADDR
- *
- * @return $serverAddress New SERVER_ADDR value to set
- */
- public function getServerAddress () {
- return $this->getConfigEntry('server_addr');
- }
-
- /**
- * Detects the HTTPS flag
- *
- * @return $https The detected HTTPS flag or null if failed
- */
- public function detectHttpSecured () {
- // Default is null
- $https = NULL;
-
- // Is HTTPS set?
- if ($this->isHttpSecured()) {
- // Then use it
- $https = $_SERVER['HTTPS'];
- } // END - if
-
- // Return it
- return $https;
- }
-
- /**
- * Checks whether HTTPS is set in $_SERVER
- *
- * @return $isset Whether HTTPS is set
- * @todo Test more fields
- */
- public function isHttpSecured () {
- return (isset($_SERVER['HTTPS']));
- }
-
- /**
- * Dectect and return the base URL for all URLs and forms
- *
- * @return $baseUrl Detected base URL
- */
- public function detectBaseUrl () {
- // Initialize the URL
- $protocol = 'http';
-
- // Do we have HTTPS?
- if ($this->isHttpSecured()) {
- // Add the >s< for HTTPS
- $protocol = 's';
- } // END - if
-
- // Construct the full URL and secure it against CSRF attacks
- $baseUrl = $protocol . '://' . $this->detectDomain() . $this->detectScriptPath();
-
- // Return the URL
- return $baseUrl;
- }
-
- /**
- * Detect safely and return the full domain where this script is installed
- *
- * @return $fullDomain The detected full domain
- */
- public function detectDomain () {
- // Full domain is localnet.invalid by default
- $fullDomain = 'localnet.invalid';
-
- // Is the server name there?
- if (isset($_SERVER['SERVER_NAME'])) {
- // Detect the full domain
- $fullDomain = htmlentities(strip_tags($_SERVER['SERVER_NAME']), ENT_QUOTES);
- } // END - if
-
- // Return it
- return $fullDomain;
- }
-
- /**
- * Detect safely the script path without trailing slash which is the glue
- * between "http://your-domain.invalid/" and "script-name.php"
- *
- * @return $scriptPath The script path extracted from $_SERVER['SCRIPT_NAME']
- */
- public function detectScriptPath () {
- // Default is empty
- $scriptPath = '';
-
- // Is the scriptname set?
- if (isset($_SERVER['SCRIPT_NAME'])) {
- // Get dirname from it and replace back-slashes with slashes for lame OSes...
- $scriptPath = str_replace("\\", '/', dirname($_SERVER['SCRIPT_NAME']));
- } // END - if
-
- // Return it
- return $scriptPath;