baseline
[omp.pkp.sfu.ca.git] / includes / functions.inc.php
blobb683f08edc8c8c4b0ae06e3224a1c7ecdedc6c77
1 <?php
3 /**
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.
9 * @ingroup index
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 $
18 /**
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');
27 /**
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.
30 * @return boolean
32 function pageRequiresInstall() {
33 $page = Request::getRequestedPage();
34 return ($page != 'install' && $page != 'help');
37 /**
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);
45 $notes = array();
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());
57 } else {
58 fatalError('Database connection failed!');
64 if (!function_exists('file_get_contents')) {
65 // For PHP < 4.3.0
66 function file_get_contents($file) {
67 return join('', file($file));
71 /**
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.
93 array_shift($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) {
102 $args = '';
103 if (isset($bt['args'])) foreach ($bt['args'] as $a) {
104 if (!empty($args)) {
105 $args .= ', ';
107 switch (gettype($a)) {
108 case 'integer':
109 case 'double':
110 $args .= $a;
111 break;
112 case 'string':
113 $a = htmlspecialchars(substr($a, 0, 64)).((strlen($a) > 64) ? '...' : '');
114 $args .= "\"$a\"";
115 break;
116 case 'array':
117 $args .= 'Array('.count($a).')';
118 break;
119 case 'object':
120 $args .= 'Object('.get_class($a).')';
121 break;
122 case 'resource':
123 $args .= 'Resource('.strstr($a, '#').')';
124 break;
125 case 'boolean':
126 $args .= $a ? 'True' : 'False';
127 break;
128 case 'NULL':
129 $args .= 'Null';
130 break;
131 default:
132 $args .= 'Unknown';
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";
143 echo "<br/>\n";
147 error_log("OMP: $reason");
148 die();
152 * Check to see if the server meets a minimum version requirement for PHP.
153 * @param $version Name of version (see version_compare documentation)
154 * @return boolean
156 function checkPhpVersion($version) {
157 return (version_compare(PHP_VERSION, $version) !== -1);