File modules/cms/base/Startup.class.php

Last commit: Tue Feb 14 00:21:26 2023 +0100	Jan Dankert	Show fatal error messages in the UI notice window.
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 id=\"cms-error-log\">$errstr\non line $errline\nin file $errfile</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 }
Download modules/cms/base/Startup.class.php
History Tue, 14 Feb 2023 00:21:26 +0100 Jan Dankert Show fatal error messages in the UI notice window. Fri, 15 Apr 2022 14:51:22 +0200 dankert Refactoring: User,Config and Database info is now stored in the Request, because so there is no session required for clients which are using Basic Authorization. Thu, 10 Mar 2022 13:09:06 +0100 dankert New: Remember some user inputs in the browser local storage. Mon, 7 Feb 2022 21:44:42 +0100 dankert New: Authenticate API users with the HTTP authorization header. Wed, 2 Feb 2022 01:12:42 +0100 dankert Better support for API requests. Sun, 30 Jan 2022 23:38:42 +0100 dankert Refactoring: Only 1 http-endpoint for both the UI and the API. Path "/api" is not available any more, all API data is served under "/". Tue, 16 Nov 2021 22:34:56 +0100 Jan Dankert Now required: PHP 5.6 because of concatenating strings with constants while defining a constant. Thu, 7 Oct 2021 23:54:09 +0200 Jan Dankert New: Location of config file is able to be overwritten by environment. Wed, 11 Nov 2020 20:52:26 +0100 Jan Dankert Fix: Advanced view for folder was broken. Sun, 1 Nov 2020 00:36:50 +0100 Jan Dankert Refactoring: Only using the configuration object. Mon, 26 Oct 2020 16:55:36 +0100 Jan Dankert Always write fatal error to the standard error log. Mon, 26 Oct 2020 09:48:01 +0100 Jan Dankert No trace-output in the API in production mode. Sat, 24 Oct 2020 17:12:58 +0200 Jan Dankert Documentation Fri, 23 Oct 2020 07:47:22 +0200 Jan Dankert New: Check for necessary modules at startup. Wed, 21 Oct 2020 23:32:28 +0200 Jan Dankert Fix: Magic quotes was removed a long time ago. Wed, 21 Oct 2020 23:13:01 +0200 Jan Dankert Externalize constants. Sat, 10 Oct 2020 01:29:41 +0200 Jan Dankert Refactoring: Only using CSS classes with the 'or-'-prefix. Mon, 5 Oct 2020 23:32:06 +0200 Jan Dankert UI: Nicer buttons Tue, 29 Sep 2020 01:14:17 +0200 Jan Dankert Fix: now() must be a static function (because it is called static). Sat, 26 Sep 2020 13:11:23 +0200 Jan Dankert Refactoring: No global variables any more. All constants are capsulated by classes. Sat, 26 Sep 2020 12:20:43 +0200 Jan Dankert Refactoring: No global variables like $SESS any more. All constants are capsulated by classes. Sat, 26 Sep 2020 04:26:55 +0200 Jan Dankert Refactoring: read configuration values with a class. Sat, 26 Sep 2020 04:03:53 +0200 Jan Dankert Refactoring: read language keys with a class. Sat, 26 Sep 2020 03:03:47 +0200 Jan Dankert Refactoring: less global functions. Sat, 26 Sep 2020 01:41:20 +0200 Jan Dankert Refactoring: Removing old require.php files. With class autoloading, they are not necessary any more.