4 * @file functions.inc.php
6 * Copyright (c) 2003-2008 John Willinsky
7 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
11 * @brief Contains definitions for common functions used system-wide.
12 * Any frequently-used functions that cannot be put into an appropriate class should be added here.
15 // $Id: functions.inc.php,v 1.3 2009/06/09 23:37:15 tylerl Exp $
19 * Emulate a Java-style import statement.
20 * Simply includes the associated PHP file (using require_once so multiple calls to include the same file have no effect).
21 * @param $class string the complete name of the class to be imported (e.g. "core.Core")
23 function import($class) {
24 require_once(str_replace('.', '/', $class) . '.inc.php');
28 * Check if request is for a page that requires the system to be installed.
29 * Any pages that can be accessed from an uninstalled system should be allowed here.
32 function pageRequiresInstall() {
33 $page = Request
::getRequestedPage();
34 return ($page != 'install' && $page != 'help');
38 * Perform basic system initialization.
39 * Initializes configuration variables, database connection, and user session.
41 function initSystem() {
42 $microTime = Core
::microtime();
44 Registry
::set('system.debug.startTime', $microTime);
46 Registry
::set('system.debug.notes', $notes);
48 if (Config
::getVar('general', 'installed')) {
49 // Initialize database connection
50 $conn =& DBConnection
::getInstance();
52 if (!$conn->isConnected()) {
53 if (Config
::getVar('database', 'debug')) {
54 $dbconn =& $conn->getDBConn();
55 fatalError('Database connection failed: ' . $dbconn->errorMsg());
58 fatalError('Database connection failed!');
64 if (!function_exists('file_get_contents')) {
66 function file_get_contents($file) {
67 return join('', file($file));
72 * Wrapper around die() to pretty-print an error message with an optional stack trace.
74 function fatalError($reason) {
75 // Because this method may be called when checking the value of the show_stacktrace
76 // configuration string, we need to ensure that we don't get stuck in an infinite loop.
77 static $isErrorCondition = null;
78 static $showStackTrace = false;
80 if ($isErrorCondition === null) {
81 $isErrorCondition = true;
82 $showStackTrace = Config
::getVar('debug', 'show_stacktrace');
83 $isErrorCondition = false;
86 echo "<h1>$reason</h1>";
88 if ($showStackTrace && checkPhpVersion('4.3.0')) {
89 echo "<h4>Stack Trace:</h4>\n";
90 $trace = debug_backtrace();
92 // Remove the call to fatalError from the call trace.
95 // Back-trace pretty-printer adapted from the following URL:
96 // http://ca3.php.net/manual/en/function.debug-backtrace.php
97 // Thanks to diz at ysagoon dot com
99 // FIXME: Is there any way to localize this when the localization
100 // functions may have caused the failure in the first place?
101 foreach ($trace as $bt) {
103 if (isset($bt['args'])) foreach ($bt['args'] as $a) {
107 switch (gettype($a)) {
113 $a = htmlspecialchars(substr($a, 0, 64)).((strlen($a) > 64) ?
'...' : '');
117 $args .= 'Array('.count($a).')';
120 $args .= 'Object('.get_class($a).')';
123 $args .= 'Resource('.strstr($a, '#').')';
126 $args .= $a ?
'True' : 'False';
135 $class = isset($bt['class'])?
$bt['class']:'';
136 $type = isset($bt['type'])?
$bt['type']:'';
137 $function = isset($bt['function'])?
$bt['function']:'';
138 $file = isset($bt['file'])?
$bt['file']:'(unknown)';
139 $line = isset($bt['line'])?
$bt['line']:'(unknown)';
141 echo "<strong>File:</strong> {$file} line {$line}<br />\n";
142 echo "<strong>Function:</strong> {$class}{$type}{$function}($args)<br />\n";
147 error_log("OMP: $reason");
152 * Check to see if the server meets a minimum version requirement for PHP.
153 * @param $version Name of version (see version_compare documentation)
156 function checkPhpVersion($version) {
157 return (version_compare(PHP_VERSION
, $version) !== -1);