+// Non-database functions
+LOAD_INC_ONCE("inc/functions.php");
+
+// Filter functions
+LOAD_INC_ONCE("inc/filters.php");
+
+// Functions which interact with the database
+LOAD_INC_ONCE("inc/mysql-manager.php");
+
+// Load extensions and language
+LOAD_INC_ONCE("inc/extensions.php");
+
+// Load database library
+LOAD_INC_ONCE("inc/db/lib.php");
+
+// Error handler function
+function __errorHandler ($errno, $errstr, $errfile, $errline) {
+ // Construct message
+ $msg = sprintf("errno=%s,errstr=%s,errfile=%s,errline=%s",
+ $errno,
+ $errstr,
+ basename($errfile),
+ $errline
+ );
+
+ // Write debug log message
+ DEBUG_LOG(__FUNCTION__, __LINE__, "".$msg, true);
+
+ // Output message to user and die
+ if (EXT_IS_ACTIVE("debug")) {
+ // Debug extension found! So Output a small message
+ mxchange_die("Error message written to debug.log. Please try to call <a href=\"".URL."\">the main page</a> to continue.");
+ } else {
+ // No debug extension found
+ // @TODO Find a way to rewrite debug_print_backtrace() so we can maybe
+ // @TODO (?) rewrite this to mxchange_die()
+ print("Please report this error at <a href=\"http://bugs.mxchange.org\" rel=\"external\" target=\"_blank\">bugs.mxchange.org</a>:<pre>");
+ debug_print_backtrace();
+ die("</pre>Thank you for your help finding bugs.");
+ }
+}
+
+// Set error handler
+set_error_handler('__errorHandler');
+
+// Call-back function for running shutdown functions and close database connection
+function __SHUTDOWN_HOOK () {
+ global $link;
+
+ // Call the filter chain 'shutdown'
+ RUN_FILTER('shutdown', null, false);
+
+ if (is_resource($link)) {
+ // Close link
+ SQL_CLOSE($link, __FILE__, __LINE__);
+ } else {
+ // No database link
+ addFatalMessage(NO_DB_LINK);
+ }
+}
+
+// Register shutdown hook
+register_shutdown_function('__SHUTDOWN_HOOK');