openrat-cms

OpenRat Content Management System
git clone http://git.code.weiherhei.de/openrat-cms.git
Log | Files | Refs | README

Startup.class.php (4817B)


      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 
     24 class Startup {
     25 
     26     private static $START_TIME;
     27 
     28 	const MIN_VERSION = '5.4'; // minimum required PHP version.
     29 	const API_LEVEL   = '2';   // public API version.
     30 
     31 	const IMG_EXT      = '.gif';
     32 	const IMG_ICON_EXT = '.png';
     33 	const FILE_SEP     = '/';
     34 
     35 	const HTML_MODULES_DIR = './modules/';
     36 	const THEMES_DIR       = './modules/cms/ui/themes/';
     37 	const CSS_PREFIX       = 'or-';
     38 
     39 	/**
     40 	 * This is the application name.
     41 	 * It can be overwritten in the configuration.
     42 	 */
     43 	const TITLE   = 'OpenRat CMS';
     44 
     45 	/**
     46 	 * Version number.
     47 	 */
     48 	const VERSION = Version::VERSION;
     49 
     50 	/**
     51 	 * Build date.
     52 	 */
     53 	const DATE    = Version::DATE;
     54 
     55 	/**
     56 	 * Initialize.
     57 	 */
     58 	public static function initialize()
     59 	{
     60 		self::checkPHPVersion();
     61 		self::securityCheck();
     62 		self::setErrorHandler();
     63 		self::setStartTime();
     64 		self::checkDatabaseDriver();
     65 		self::checkMultibyteSupport();
     66 
     67 		// in some situations we want to know, if the CMS is really started up.
     68 		define('APP_STARTED','1');
     69 	}
     70 
     71 	protected static function checkPHPVersion()
     72 	{
     73 		if (version_compare(phpversion(), self::MIN_VERSION, "<"))
     74 			throw new ErrorException('This version of PHP ' . phpversion() . ' is not supported any more. Minimum required: ' . self::MIN_VERSION);
     75 	}
     76 
     77 
     78 	protected static function checkDatabaseDriver() {
     79 		if (!defined('PDO::ATTR_DRIVER_NAME')) {
     80 			throw new ErrorException('PDO is not available');
     81 		}
     82 	}
     83 
     84 
     85 	protected static function checkMultibyteSupport() {
     86 
     87 		if   ( function_exists('mb_substr'          ) &&
     88 			   function_exists('mb_convert_encoding')    )
     89 			; // ok, Multibyte is available
     90 		else
     91 			throw new ErrorException('Multibyte functions are not available');
     92 	}
     93 
     94 	public static function getStartTime() {
     95 		return self::$START_TIME;
     96 	}
     97 
     98 
     99 	protected static function setStartTime() {
    100 
    101 		self::$START_TIME = time();
    102 	}
    103 
    104 
    105 	protected static function setErrorHandler() {
    106 
    107 		/**
    108 		 * Wandelt jeden Fehler in eine ErrorException um.
    109 		 */
    110 		$exceptionErrorHandler = function ($severity, $message, $file, $line) {
    111 			if	( !(error_reporting() & $severity) )
    112 			{
    113 				// Dieser Fehlercode ist nicht in error_reporting enthalten
    114 				return;
    115 			}
    116 			throw new ErrorException($message, 0, $severity, $file, $line);
    117 		};
    118 
    119 		set_error_handler($exceptionErrorHandler);
    120 
    121 
    122 		/**
    123 		 * Ermöglicht das Loggen von Fatal-Errors.
    124 		 */
    125 		$fatalHandler = function() {
    126 
    127 			$error = error_get_last();
    128 
    129 			if( $error )
    130 			{
    131 				$errno   = @$error["type"];
    132 				$errfile = @$error["file"];
    133 				$errline = @$error["line"];
    134 				$errstr  = @$error["message"];
    135 
    136 				$message = 'Error '.$errno .' '. $errstr.' in '. $errfile.':'. $errline;
    137 				if(class_exists('logger\Logger'))
    138 					Logger::error( $message);
    139 
    140 				error_log($message);
    141 
    142 				// It is not possibile to throw an exception out of a shutdown function!
    143 				// PHP will exit the request directly after executing this method, so a
    144 				// Exception would never reach a caller.
    145 			}
    146 
    147 		};
    148 
    149 		register_shutdown_function( $fatalHandler );
    150 
    151 	}
    152 
    153 
    154 	/**
    155 	 * Check for some stupid security impacts.
    156 	 */
    157 	protected static function securityCheck()
    158 	{
    159 		// REGISTER_GLOBALS
    160 		// This feature has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 5.4.0.
    161 
    162 		// MAGIC_QUOTES
    163 		// This feature has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 5.4.0.
    164 	}
    165 
    166 
    167 
    168 	/**
    169 	 * Stellt fest, ob das System in einem schreibgeschuetzten Zustand ist.
    170 	 *
    171 	 * @return boolean true, falls schreibgeschuetzt, sonst false
    172 	 */
    173 	public static function readonly() {
    174 
    175 		// Gesamtes CMS ist readonly.
    176 		if (Configuration::subset( ['security'] )->is('readonly',false))
    177 			return true;
    178 
    179 		// Aktuelle Datenbankverbindung ist readonly.
    180 		$db = DB::get();
    181 		if (isset($db->conf['readonly']) && $db->conf['readonly'])
    182 			return true;
    183 
    184 		return false;
    185 	}
    186 
    187 
    188 	/**
    189 	 * Ermittelt die aktuelle Systemzeit als Unix-Timestamp.<br>
    190 	 * Unix-Timestamp ist immer bezogen auf GMT.
    191 	 * -
    192 	 * @return Unix-Timestamp der aktuellen Zeit
    193 	 */
    194 	public static function now()
    195 	{
    196 		return time();
    197 	}
    198 
    199 
    200 }