/** * This would process error message sent from frontend */ public function action_index() { $input = request::input(); if (!empty($input['token'])) { $crypt = new crypt(); $token_data = $crypt->token_validate($input['token'], ['skip_time_validation' => true]); if (!($token_data === false || $token_data['id'] !== 'general')) { $input['data'] = json_decode($input['data'], true); error_base::error_handler('javascript', $input['data']['message'], $input['data']['file'], $input['data']['line']); } } // rendering layout::render_as(file_get_contents(__DIR__ . '/error.png'), 'image/png'); }
/** * Destroy everything */ public static function destroy() { $__run_only_bootstrap = application::get(['flag', 'global', '__run_only_bootstrap']); // we need to set working directory again chdir(application::get(['application', 'path_full'])); // error processing if (empty(error_base::$flag_error_already)) { $last_error = error_get_last(); $flag_render = false; if (in_array($last_error['type'], [E_COMPILE_ERROR, E_PARSE, E_ERROR])) { error_base::error_handler($last_error['type'], $last_error['message'], $last_error['file'], $last_error['line']); error_base::$flag_error_already = true; $flag_render = true; } if ($flag_render || error_base::$flag_exception) { error_base::$flag_error_already = true; if ($__run_only_bootstrap) { helper_ob::clean_all(); print_r(error_base::$errors); } else { // set mvc + process application::set_mvc('/error/_error/500'); application::process(); } } } // write sessions session_write_close(); // final benchmark if (debug::$debug) { debug::benchmark('application end'); } // debugging toolbar last if (debug::$toolbar && !$__run_only_bootstrap) { echo str_replace('<!-- [numbers: debug toolbar] -->', debug::render(), helper_ob::clean()); } // flush data to client flush(); // closing caches before db $cache = factory::get(['cache']); if (!empty($cache)) { foreach ($cache as $k => $v) { $object = $v['object']; if (!empty(cache::$reset_caches[$k])) { $object->gc(1, cache::$reset_caches[$k]); } $object->close(); } } // destroy i18n if (i18n::$initialized) { i18n::destroy(); } // close db connections $dbs = factory::get(['db']); if (!empty($dbs)) { foreach ($dbs as $k => $v) { $object = $v['object']; $object->close(); } } // emails with erros if (debug::$debug && !empty(debug::$email)) { debug::send_errors_to_admin(); } }
/** * Run application * * @param array $options * string application_name * string application_path * string ini_folder * boolean __run_only_bootstrap * @throws Exception */ public static function run($options = []) { // fixing location paths $application_path = isset($options['application_path']) ? rtrim($options['application_path'], '/') . '/' : '../application/'; $application_name = isset($options['application_name']) ? $options['application_name'] : 'default'; $ini_folder = isset($options['ini_folder']) ? rtrim($options['ini_folder'], '/') . '/' : $application_path . 'config/'; // working directory is location of the application chdir($application_path); $application_path_full = getcwd(); // setting include_path $paths = []; $paths[] = $application_path_full; $paths[] = __DIR__; $paths[] = str_replace('/numbers/framework', '', __DIR__); set_include_path(implode(PATH_SEPARATOR, $paths)); // support functions require "functions.php"; // load ini settings self::$settings = system_config::load($ini_folder); // special handling of media files for development, so there's no need to redeploy application if (self::$settings['environment'] == 'development' && isset($_SERVER['REQUEST_URI'])) { system_media::serve_media_if_exists($_SERVER['REQUEST_URI'], $application_path); } // we need to solve chicken and egg problem so we load cache first and then run application //cache::create('php', array('type'=>'php', 'dir'=>'../application/cache')); // setting variables if (!isset(self::$settings['application']) || !is_array(self::$settings['application'])) { self::$settings['application'] = []; } self::$settings['application']['name'] = $application_name; self::$settings['application']['path'] = $application_path; self::$settings['application']['path_full'] = $application_path_full . '/'; self::$settings['application']['loaded_classes'] = []; // class paths self::$settings['layout'] = []; // layout settings // flags self::$settings['flag'] = isset(self::$settings['flag']) && is_array(self::$settings['flag']) ? self::$settings['flag'] : []; self::$settings['flag']['global']['__run_only_bootstrap'] = !empty($options['__run_only_bootstrap']); // magic variables processed here self::$settings['flag']['global']['__content_type'] = 'text/html'; self::process_magic_variables(); // processing php settings if (isset(self::$settings['php'])) { foreach (self::$settings['php'] as $k => $v) { if (is_array($v)) { foreach ($v as $k2 => $v2) { if (is_numeric($v2)) { $v2 = $v2 * 1; } ini_set($k . '.' . $k2, $v2); } } else { if (is_numeric($v)) { $v = $v * 1; } ini_set($k, $v); } } } // Destructor register_shutdown_function(array('bootstrap', 'destroy')); // error handler first error_base::init(); // debug after error handler debug::init(self::get('debug')); // Bootstrap Class $bootstrap = new bootstrap(); $bootstrap_methods = get_class_methods($bootstrap); foreach ($bootstrap_methods as $method) { if (strpos($method, 'init') === 0) { call_user_func(array($bootstrap, $method), $options); } } // if we are calling application from the command line if (!empty($options['__run_only_bootstrap'])) { // dispatch before, in case if we open database connections in there if (!empty(self::$settings['application']['dispatch']['before_controller'])) { call_user_func(self::$settings['application']['dispatch']['before_controller']); } return; } // processing mvc settings self::set_mvc(); // check if controller exists if (!file_exists(self::$settings['mvc']['controller_file'])) { throw new Exception('Resource not found!', -1); } // initialize the controller $controller_class = self::$settings['mvc']['controller_class']; $controller = new $controller_class(); self::$settings['controller'] = get_object_vars($controller); // dispatch before, we need some settings from the controller if (!empty(self::$settings['application']['dispatch']['before_controller'])) { call_user_func(self::$settings['application']['dispatch']['before_controller']); } // singleton start if (!empty(self::$settings['controller']['singleton_flag'])) { $message = !empty(self::$settings['controller']['singleton_message']) ? self::$settings['controller']['singleton_message'] : 'This script is being run by another user!'; $lock_id = "singleton_" . $controller_class; if (lock::process($lock_id) === false) { throw new Exception($message); } } // process parameters and provide output self::process(); // release singleton lock if (!empty(self::$settings['controller']['singleton_flag'])) { lock::release($lock_id); } // dispatch after controller if (!empty(self::$settings['application']['dispatch']['after_controller'])) { call_user_func(self::$settings['application']['dispatch']['after_controller']); } // headers if (!empty(self::$settings['header']) && !headers_sent()) { foreach (self::$settings['header'] as $k => $v) { header($v); } } }
/** * Exception handler function * * @param Exception $e */ public static function exception_handler(Throwable $e) { self::$errors[] = ['errno' => $e->getCode(), 'error' => [$e->getMessage()], 'file' => $e->getFile(), 'line' => $e->getLine(), 'code' => self::get_code($e->getFile(), $e->getLine()), 'backtrace' => self::debug_backtrace_string($e->getTrace())]; self::$flag_exception = true; exit; }