openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

Startup.class.php (5759B)


      1 <?php
      2 // OpenRat Content Management System
      3 // Copyright (C) 2002-2009 Jan Dankert, cms@jandankert.de
      4 //
      5 // This program is free software; you can redistribute it and/or
      6 // modify it under the terms of the GNU General Public License
      7 // as published by the Free Software Foundation; version 2.
      8 //
      9 // This program is distributed in the hope that it will be useful,
     10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12 // GNU General Public License for more details.
     13 //
     14 // You should have received a copy of the GNU General Public License
     15 // along with this program; if not, write to the Free Software
     16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
     17 
     18 namespace cms\base;
     19 
     20 use ErrorException;
     21 use logger\Logger;
     22 use util\exception\ValidationException;
     23 use util\Request;
     24 use util\Session;
     25 
     26 class Startup {
     27 
     28     private static $START_TIME;
     29 
     30 	const MIN_VERSION = '5.6'; // minimum required PHP version.
     31 	const API_LEVEL   = '2';   // public API version.
     32 	const CHARSET     = 'UTF-8'; // Everything is UTF-8.
     33 
     34 	const IMG_EXT      = '.gif';
     35 	const IMG_ICON_EXT = '.png';
     36 	const FILE_SEP     = '/';
     37 
     38 	const HTML_MODULES_DIR = './modules/';
     39 	const THEMES_DIR       = './modules/cms/ui/themes/';
     40 	const CSS_PREFIX       = 'or-';
     41 	const DEFAULT_CONFIG_FILE = __DIR__ . '/../../../config/config.yml';
     42 	const APP_DIR             = __DIR__ . '/../../../';
     43 	const MODULE_DIR          = self::APP_DIR .'modules/';
     44 
     45 	/**
     46 	 * This is the application name.
     47 	 * It can be overwritten in the configuration.
     48 	 */
     49 	const TITLE   = 'OpenRat CMS';
     50 
     51 	/**
     52 	 * Version number.
     53 	 */
     54 	const VERSION = Version::VERSION;
     55 
     56 	/**
     57 	 * Build date.
     58 	 */
     59 	const DATE    = Version::DATE;
     60 
     61 	/**
     62 	 * Initialize.
     63 	 *
     64 	 * @throws ErrorException if something happens
     65 	 */
     66 	public static function initialize()
     67 	{
     68 		self::checkPHPVersion();
     69 		self::securityCheck();
     70 		self::setErrorHandler();
     71 		self::setStartTime();
     72 		self::checkDatabaseDriver();
     73 		self::checkMultibyteSupport();
     74 		self::setCookiePath();
     75 
     76 		// in some situations we want to know, if the CMS is really started up.
     77 		define('APP_STARTED','1');
     78 	}
     79 
     80 
     81 	/**
     82 	 * Setting the cookie path.
     83 	 */
     84 	protected static function setCookiePath() {
     85 
     86 		$scriptPath = dirname($_SERVER['SCRIPT_NAME']);
     87 
     88 		if   ( substr($scriptPath,-1 ) != '/' )
     89 			$scriptPath = $scriptPath.'/'; // Add trailing slash
     90 
     91 		// Cookie path
     92 		define('COOKIE_PATH',$scriptPath);
     93 	}
     94 
     95 
     96 	/**
     97 	 * Checking the minimum PHP version.
     98 	 *
     99 	 * @throws ErrorException
    100 	 */
    101 	protected static function checkPHPVersion()
    102 	{
    103 		if (version_compare(phpversion(), self::MIN_VERSION, "<"))
    104 			throw new ErrorException('This version of PHP ' . phpversion() . ' is not supported any more. Minimum required: ' . self::MIN_VERSION);
    105 	}
    106 
    107 
    108 	protected static function checkDatabaseDriver() {
    109 		if (!defined('PDO::ATTR_DRIVER_NAME')) {
    110 			throw new ErrorException('PDO is not available');
    111 		}
    112 	}
    113 
    114 
    115 	protected static function checkMultibyteSupport() {
    116 
    117 		if   ( function_exists('mb_substr'          ) &&
    118 			   function_exists('mb_convert_encoding')    )
    119 			; // ok, Multibyte is available
    120 		else
    121 			throw new ErrorException('Multibyte functions are not available');
    122 	}
    123 
    124 	public static function getStartTime() {
    125 		return self::$START_TIME;
    126 	}
    127 
    128 
    129 	protected static function setStartTime() {
    130 
    131 		self::$START_TIME = time();
    132 	}
    133 
    134 
    135 	protected static function setErrorHandler() {
    136 
    137 		/**
    138 		 * Wandelt jeden Fehler in eine ErrorException um.
    139 		 */
    140 		$exceptionErrorHandler = function ($severity, $message, $file, $line) {
    141 			if	( !(error_reporting() & $severity) )
    142 			{
    143 				// Dieser Fehlercode ist nicht in error_reporting enthalten
    144 				return;
    145 			}
    146 			throw new ErrorException($message, 0, $severity, $file, $line);
    147 		};
    148 
    149 		set_error_handler($exceptionErrorHandler);
    150 
    151 
    152 		/**
    153 		 * Ermöglicht das Loggen von Fatal-Errors.
    154 		 */
    155 		$fatalHandler = function() {
    156 
    157 			$error = error_get_last();
    158 
    159 			if( $error )
    160 			{
    161 				$errno   = @$error["type"];
    162 				$errfile = @$error["file"];
    163 				$errline = @$error["line"];
    164 				$errstr  = @$error["message"];
    165 
    166 				$message = 'Error '.$errno .' '. $errstr.' in '. $errfile.':'. $errline;
    167 				if(class_exists('logger\Logger'))
    168 					Logger::error( $message);
    169 
    170 				error_log($message);
    171 
    172 				// It is not possibile to throw an exception out of a shutdown function!
    173 				// PHP will exit the request directly after executing this method, so a
    174 				// Exception would never reach a caller.
    175 
    176 				header('HTTP/1.0 503 Internal CMS fatal error');
    177 				header('Content-Type: text/html; charset=utf-8');
    178 				echo "<h1>Fatal error</h1><pre>$errstr</pre><hr />".Startup::TITLE;
    179 			}
    180 
    181 		};
    182 
    183 		register_shutdown_function( $fatalHandler );
    184 
    185 	}
    186 
    187 
    188 	/**
    189 	 * Check for some stupid security impacts.
    190 	 */
    191 	protected static function securityCheck()
    192 	{
    193 		// REGISTER_GLOBALS
    194 		// This feature has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 5.4.0.
    195 
    196 		// MAGIC_QUOTES
    197 		// This feature has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 5.4.0.
    198 	}
    199 
    200 
    201 
    202 	/**
    203 	 * Stellt fest, ob das System in einem schreibgeschuetzten Zustand ist.
    204 	 *
    205 	 * @return boolean true, falls schreibgeschuetzt, sonst false
    206 	 */
    207 	public static function readonly() {
    208 
    209 		// Gesamtes CMS ist readonly.
    210 		if (Configuration::subset( ['security'] )->is('readonly',false))
    211 			return true;
    212 
    213 		// Aktuelle Datenbankverbindung ist readonly.
    214 		//$db = DB::get();
    215 		$db = Request::getDatabase();
    216 
    217 		if ($db && isset($db->conf['readonly']) && $db->conf['readonly'])
    218 			return true;
    219 
    220 		return false;
    221 	}
    222 
    223 
    224 	/**
    225 	 * Ermittelt die aktuelle Systemzeit als Unix-Timestamp.<br>
    226 	 * Unix-Timestamp ist immer bezogen auf GMT.
    227 	 * -
    228 	 * @return Unix-Timestamp der aktuellen Zeit
    229 	 */
    230 	public static function now()
    231 	{
    232 		return time();
    233 	}
    234 
    235 
    236 }