Continued with unit tests:
[core.git] / framework / bootstrap / class_FrameworkBootstrap.php
index 16e5863d0e3e4d6589f044e70088dc04e880137e..2af601e0d5971c06195dc2cc9107dbc296153cf5 100644 (file)
@@ -6,8 +6,10 @@ namespace CoreFramework\Bootstrap;
 use CoreFramework\Configuration\FrameworkConfiguration;
 use CoreFramework\Connection\Database\DatabaseConnection;
 use CoreFramework\Connector\Database\DatabaseConnector;
+use CoreFramework\Console\Tools\ConsoleTools;
 use CoreFramework\EntryPoint\ApplicationEntryPoint;
 use CoreFramework\Factory\ObjectFactory;
+use CoreFramework\Generic\NullPointerException;
 use CoreFramework\Helper\Application\ApplicationHelper;
 use CoreFramework\Loader\ClassLoader;
 use CoreFramework\Manager\ManageableApplication;
@@ -19,6 +21,7 @@ use CoreFramework\Response\Responseable;
 
 // Import SPL stuff
 use \BadMethodCallException;
+use \InvalidArgumentException;
 
 /**
  * A framework-bootstrap class which helps the frameworks to bootstrap ... ;-)
@@ -44,6 +47,11 @@ use \BadMethodCallException;
  */
 final class FrameworkBootstrap {
 
+       /**
+        * Detected server address
+        */
+       private static $serverAddress = NULL;
+
        /**
         * Instance of a Requestable class
         */
@@ -381,6 +389,169 @@ final class FrameworkBootstrap {
                $applicationInstance->setDatabaseInstance($connectionInstance);
        }
 
+       /**
+        * 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 static function detectServerAddress () {
+               // Is the entry set?
+               if (!isset(self::$serverAddress)) {
+                       // Is it set in $_SERVER?
+                       if (!empty($_SERVER['SERVER_ADDR'])) {
+                               // Set it from $_SERVER
+                               self::$serverAddress = $_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
+                               self::$serverAddress = $serverIp;
+                       } else {
+                               // Run auto-detecting through console tools lib
+                               self::$serverAddress = ConsoleTools::acquireSelfIpAddress();
+                       }
+               } // END - if
+
+               // Return it
+               return self::$serverAddress;
+       }
+
+       /**
+        * Setter for default time zone (must be correct!)
+        *
+        * @param       $timezone       The timezone string (e.g. Europe/Berlin)
+        * @return      $success        If timezone was accepted
+        * @throws      NullPointerException    If $timezone is NULL
+        * @throws      InvalidArgumentException        If $timezone is empty
+        */
+       public static function setDefaultTimezone ($timezone) {
+               // Is it null?
+               if (is_null($timezone)) {
+                       // Throw NPE
+                       throw new NullPointerException(NULL, BaseFrameworkSystem::EXCEPTION_IS_NULL_POINTER);
+               } elseif (!is_string($timezone)) {
+                       // Is not a string
+                       throw new InvalidArgumentException(sprintf('timezone[]=%s is not a string', gettype($timezone)));
+               } elseif ((is_string($timezone)) && (empty($timezone))) {
+                       // Entry is empty
+                       throw new InvalidArgumentException('timezone is empty');
+               }
+
+               // Default success
+               $success = FALSE;
+
+               /*
+                * Set desired time zone to prevent date() and related functions to
+                * issue an E_WARNING.
+                */
+               $success = date_default_timezone_set($timezone);
+
+               // Return status
+               return $success;
+       }
+
+       /**
+        * Detects the HTTPS flag
+        *
+        * @return      $https  The detected HTTPS flag or null if failed
+        */
+       public static function detectHttpSecured () {
+               // Default is null
+               $https = NULL;
+
+               // Is HTTPS set?
+               if (self::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 static function isHttpSecured () {
+               return (isset($_SERVER['HTTPS']));
+       }
+
+       /**
+        * Dectect and return the base URL for all URLs and forms
+        *
+        * @return      $baseUrl        Detected base URL
+        */
+       public static function detectBaseUrl () {
+               // Initialize the URL
+               $protocol = 'http';
+
+               // Do we have HTTPS?
+               if (self::isHttpSecured()) {
+                       // Add the >s< for HTTPS
+                       $protocol = 's';
+               } // END - if
+
+               // Construct the full URL and secure it against CSRF attacks
+               $baseUrl = $protocol . '://' . self::detectDomain() . self::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 static 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 static 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;
+       }
+
        /**
         * 1) Loads class scanner and scans all framework's classes and interfaces.
         * This method also registers the class loader's method autoLoad() for the