+ * @return void
+ */
+ public static function initDatabaseInstance () {
+ // Get application instance
+ $applicationInstance = ApplicationHelper::getSelfInstance();
+
+ // Is the database instance already set?
+ if ($applicationInstance instanceof DatabaseConnector) {
+ // Yes, then abort here
+ throw new BadMethodCallException('Method called twice.');
+ } // END - if
+
+ // Initialize database layer
+ $databaseInstance = ObjectFactory::createObjectByConfiguredName(FrameworkConfiguration::getSelfInstance()->getConfigEntry('database_type') . '_class');
+
+ // Prepare database instance
+ $connectionInstance = DatabaseConnection::createDatabaseConnection(DebugMiddleware::getSelfInstance(), $databaseInstance);
+
+ // Set it in application helper
+ $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;
+ }
+
+ /**
+ * 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 = sprintf('%s://%s%s', $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
+ * SPL auto-load feature. Remember that you can register additional methods
+ * (not functions, please) for other libraries.
+ *
+ * Yes, I know about Composer, but I like to keep my class loader around.
+ * You can always use mine as long as your classes have a namespace
+ * according naming-convention: Vendor\Project\Group[\SubGroup]
+ *
+ * @return void
+ */
+ private static function scanFrameworkClasses () {
+ // Include the class loader function
+ require FrameworkConfiguration::getSelfInstance()->getConfigEntry('framework_base_path') . 'loader/class_ClassLoader.php';
+
+ // Register auto-load function with the SPL
+ spl_autoload_register('CoreFramework\Loader\ClassLoader::autoLoad');
+
+ // Scan for all framework classes, exceptions and interfaces
+ ClassLoader::scanFrameworkClasses();
+ }
+
+ /**
+ * 2) Determines request/response type and stores the created
+ * request/response instances in this object for later usage.
+ *
+ * @return void
+ */
+ private static function determineRequestType () {
+ // Determine request type
+ $request = self::getRequestTypeFromSystem();
+ $requestType = self::getRequestTypeFromSystem();
+
+ // Create a new request object
+ $requestInstance = ObjectFactory::createObjectByName(sprintf('CoreFramework\Request\%sRequest', BaseFrameworkSystem::convertToClassName($request)));
+
+ // Remember request instance here
+ self::setRequestInstance($requestInstance);
+
+ // Do we have another response?
+ if ($requestInstance->isRequestElementSet('request')) {
+ // Then use it
+ $request = strtolower($requestInstance->getRequestElement('request'));
+ $requestType = $request;
+ } // END - if
+
+ // ... and a new response object
+ $responseClass = sprintf('CoreFramework\Response\%sResponse', BaseFrameworkSystem::convertToClassName($request));
+ $responseInstance = ObjectFactory::createObjectByName($responseClass);
+
+ // Remember response instance here
+ self::setResponseInstance($responseInstance);
+ }
+
+ /**
+ * 3) Validate parameter 'app' if it is set and the application is there.
+ *
+ * @return void
+ */
+ private static function validateApplicationParameter () {
+ // Is the parameter set?
+ if (!self::getRequestInstance()->isRequestElementSet('app')) {
+ /*
+ * Don't continue here, the application 'selector' is no longer
+ * supported and only existed as an idea to select the proper
+ * application (by user).
+ */
+ ApplicationEntryPoint::exitApplication('No application specified. Please provide a parameter "app" and retry.');
+ } // END - if
+
+ // Get it for local usage
+ $application = self::getRequestInstance()->getRequestElement('app');
+
+ // Secure it, by keeping out tags
+ $application = htmlentities(strip_tags($application), ENT_QUOTES);
+
+ // Secure it a little more with a reg.exp.
+ $application = preg_replace('/([^a-z0-9_-])+/i', '', $application);
+
+ // Construct FQPN (Full-Qualified Path Name) for ApplicationHelper class
+ $applicationPath = sprintf(
+ '%s%s%s',
+ FrameworkConfiguration::getSelfInstance()->getConfigEntry('application_base_path'),
+ $application,
+ DIRECTORY_SEPARATOR
+ );
+
+ // Full path for application
+ // Is the path there? This secures a bit the parameter (from untrusted source).
+ if ((!is_dir($applicationPath)) || (!is_readable($applicationPath))) {
+ // Not found or not readable
+ ApplicationEntryPoint::exitApplication(sprintf('Application "%s" not found.', $application));
+ } // END - if
+
+ // Set the detected application's name and full path for later usage
+ FrameworkConfiguration::getSelfInstance()->setConfigEntry('detected_full_app_path', $applicationPath);
+ FrameworkConfiguration::getSelfInstance()->setConfigEntry('detected_app_name' , $application);
+ }
+ /**
+ * Setter for request instance
+ *
+ * @param $requestInstance An instance of a Requestable class
+ * @return void
+ */
+ private static function setRequestInstance (Requestable $requestInstance) {
+ self::$requestInstance = $requestInstance;
+ }
+
+ /**
+ * Setter for response instance
+ *
+ * @param $responseInstance An instance of a Responseable class
+ * @return void