/** * 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(); } }
/** * Render debug toolbar * * @return string */ public static function render() { $loaded_classes = application::get(['application', 'loaded_classes']); self::$data['session'] = []; if (!empty($_SESSION)) { self::$data['session'] = [$_SESSION]; } $application = application::get(); $result = '<div class="container" dir="ltr">'; $result .= '<table cellpadding="2" cellspacing="2" width="100%">'; $result .= '<tr>'; $result .= '<td>'; $result .= '<table width="100%">'; $result .= '<tr>'; $result .= '<td nowrap> ' . html::a(['value' => 'Hide All', 'href' => 'javascript:void(0);', 'onclick' => "\$('.debuging_toolbar_class').hide();"]) . ' </td>'; foreach (self::$data as $k => $v) { if ($k == 'errors') { $count = count(error_base::$errors); } else { if ($k == 'classes') { $count = count($loaded_classes); } else { if ($k == 'application') { $count = count($application); } else { if ($k == 'phpinfo') { $count = 1; } else { $count = count($v); } } } } $result .= '<td nowrap> ' . html::a(['value' => ucwords($k) . ' (' . $count . ')', 'id' => "debuging_toolbar_{$k}_a", 'href' => 'javascript:void(0);', 'onclick' => "\$('#debuging_toolbar_{$k}').toggle();"]) . ' </td>'; } $result .= '<td width="50%" align="right">' . html::a(['href' => '/numbers/frontend/system/controller/dev', 'value' => 'Dev. Portal']) . '</td>'; $result .= '</tr>'; $result .= '</table>'; $result .= '</td>'; $result .= '</tr>'; // errors $result .= '<tr id="debuging_toolbar_errors" class="debuging_toolbar_class" style="display: none;">'; $result .= '<td>'; $result .= '<h3>Errors (' . count(error_base::$errors) . ')</h3>'; $result .= '<table border="1" cellpadding="2" cellspacing="2" width="100%">'; foreach (error_base::$errors as $k => $v) { $result .= '<tr>'; $result .= '<td><b>' . error_base::$error_codes[$v['errno']] . ' (' . $v['errno'] . ') - ' . implode('<br/>', $v['error']) . '</b></td>'; $result .= '</tr>'; $result .= '<tr>'; $result .= '<td>File: ' . $v['file'] . ', Line: ' . $v['line'] . '</td>'; $result .= '</tr>'; $result .= '<tr>'; $result .= '<td><pre>' . $v['code'] . '</pre></td>'; $result .= '</tr>'; $result .= '<tr>'; $result .= '<td><pre>' . implode("\n", $v['backtrace']) . '</pre></td>'; $result .= '</tr>'; } $result .= '</table>'; $result .= '</td>'; $result .= '</tr>'; // suppressed $result .= '<tr id="debuging_toolbar_suppressed" class="debuging_toolbar_class" style="display: none;">'; $result .= '<td>'; $result .= '<h3>Suppressed (' . count(self::$data['suppressed']) . ')</h3>'; $result .= '<table border="1" cellpadding="2" cellspacing="2" width="100%">'; foreach (self::$data['suppressed'] as $k => $v) { $result .= '<tr>'; $result .= '<td><b>' . error_base::$error_codes[$v['errno']] . ' (' . $v['errno'] . ') - ' . implode('<br/>', $v['error']) . '</b></td>'; $result .= '</tr>'; $result .= '<tr>'; $result .= '<td>File: ' . $v['file'] . ', Line: ' . $v['line'] . '</td>'; $result .= '</tr>'; $result .= '<tr>'; $result .= '<td><pre>' . $v['code'] . '</pre></td>'; $result .= '</tr>'; } $result .= '</table>'; $result .= '</td>'; $result .= '</tr>'; // javascript $result .= '<tr id="debuging_toolbar_js" class="debuging_toolbar_class" style="display: none;">'; $result .= '<td>'; $result .= '<h3>Javascript Errors (' . count(self::$data['js']) . ')</h3>'; $result .= '<table border="1" cellpadding="2" cellspacing="2" width="100%">'; foreach (self::$data['js'] as $k => $v) { $result .= '<tr>'; $result .= '<td><b>' . implode('<br/>', $v['error']) . '</b></td>'; $result .= '</tr>'; $result .= '<tr>'; $result .= '<td>File: ' . $v['file'] . ', Line: ' . $v['line'] . '</td>'; $result .= '</tr>'; } $result .= '</table>'; $result .= '<div id="debuging_toolbar_js_data">'; $result .= ' '; $result .= '</div>'; $result .= '</td>'; $result .= '</tr>'; // benchmark first $result .= '<tr id="debuging_toolbar_benchmark" class="debuging_toolbar_class" style="display: none;">'; $result .= '<td>'; $result .= '<h3>Benchmark (' . count(self::$data['benchmark']) . ')' . '</h3>'; $result .= '<table border="1" cellpadding="2" cellspacing="2" width="100%">'; $result .= '<tr>'; $result .= '<th>Name</th>'; $result .= '<th>Time</th>'; $result .= '<th>Start</th>'; $result .= '<th>Total</th>'; $result .= '<th>Memory</th>'; $result .= '</tr>'; foreach (self::$data['benchmark'] as $k => $v) { $result .= '<tr>'; $result .= '<td>' . $v['name'] . '</td>'; $result .= '<td align="right">' . $v['time'] . '</td>'; $result .= '<td align="right">' . $v['start'] . '</td>'; $result .= '<td align="right">' . $v['total'] . '</td>'; $result .= '<td align="right">' . format::memory($v['memory'], 'm') . '</td>'; $result .= '</tr>'; } $result .= '</table>'; $result .= '</td>'; $result .= '</tr>'; // sql $result .= '<tr id="debuging_toolbar_sql" class="debuging_toolbar_class" style="display: none;">'; $result .= '<td>'; $result .= '<h3>Sql (' . count(self::$data['sql']) . ')' . '</h3>'; $result .= '<table border="1" cellpadding="2" cellspacing="2" width="100%">'; $result .= '<tr>'; $result .= '<th>Sql</th>'; $result .= '<th>Error</th>'; $result .= '<th>Errno</th>'; $result .= '<th>Num Rows</th>'; $result .= '<th>Affected Rows</th>'; //$result.= '<th>Rows</th>'; $result .= '<th>Key</th>'; $result .= '<th>Structure</th>'; $result .= '<th>Time</th>'; $result .= '</tr>'; foreach (self::$data['sql'] as $k => $v) { $temp = is_array($v['key']) ? implode('<br/>', $v['key']) : $v['key']; // header first $result .= '<tr>'; $result .= '<td valign="top"><pre style="width: 500px;">' . nl2br($v['sql']) . '</pre></td>'; $result .= '<td valign="top">' . implode('<br/>', $v['error']) . ' - ' . implode('<br/>', $v['error_original'] ?? []) . '</td>'; $result .= '<td valign="top">' . $v['errno'] . '</td>'; $result .= '<td valign="top">' . $v['num_rows'] . '</td>'; $result .= '<td valign="top">' . $v['affected_rows'] . '</td>'; //$result.= '<td valign="top">' . html::table(['options' => $v['rows']]) . '</td>'; $result .= '<td valign="top">' . $temp . '</td>'; $result .= '<td valign="top">' . html::table(['options' => $v['structure']]) . '</td>'; $result .= '<td valign="top">' . $v['time'] . '</td>'; $result .= '</tr>'; // results second if (!empty($v['rows'])) { $temp = array_keys(current($v['rows'])); $header = array_combine($temp, $temp); if (!empty($header)) { $result .= '<tr>'; $result .= '<td valign="top" colspan="8" style="max-width: 1000px; overflow: scroll;">' . html::table(['header' => $header, 'options' => $v['rows']]) . '</td>'; $result .= '</tr>'; } } } $result .= '</table>'; $result .= '</td>'; $result .= '</tr>'; // cache $result .= '<tr id="debuging_toolbar_cache" class="debuging_toolbar_class" style="display: none;">'; $result .= '<td>'; $result .= '<h3>Cache (' . count(self::$data['cache']) . ')' . '</h3>'; $result .= '<table border="1" cellpadding="2" cellspacing="2" width="100%">'; $result .= '<tr>'; $result .= '<th>Type</th>'; $result .= '<th>Link</th>'; $result .= '<th>Cache #</th>'; $result .= '<th>Has Data</th>'; $result .= '</tr>'; foreach (self::$data['cache'] as $k => $v) { $result .= '<tr>'; $result .= '<td valign="top">' . $v['type'] . '</td>'; $result .= '<td valign="top">' . $v['link'] . '</td>'; $result .= '<td valign="top">' . $v['cache_id'] . '</td>'; $result .= '<td valign="top">' . ($v['have_data'] ? 'Yes' : 'No') . '</td>'; $result .= '</tr>'; } $result .= '</table>'; $result .= '</td>'; $result .= '</tr>'; // dump $result .= '<tr id="debuging_toolbar_dump" class="debuging_toolbar_class" style="display: none;">'; $result .= '<td>'; $result .= '<h3>Dump (' . count(self::$data['dump']) . ')' . '</h3>'; $result .= '<table border="1" cellpadding="2" cellspacing="2" width="100%">'; $result .= '<tr>'; $result .= '<th>Name</th>'; $result .= '<th>Dump</th>'; $result .= '</tr>'; foreach (self::$data['dump'] as $k => $v) { $result .= '<tr>'; $result .= '<td valign="top">' . $v['name'] . '</td>'; $result .= '<td valign="top"><pre>' . print_r($v['value'], true) . '</pre></td>'; $result .= '</tr>'; } $result .= '</table>'; $result .= '</td>'; $result .= '</tr>'; // input $result .= '<tr id="debuging_toolbar_input" class="debuging_toolbar_class" style="display: none;">'; $result .= '<td>'; $result .= '<h3>Input (' . count(self::$data['input']) . ')' . '</h3>'; $result .= '<table border="1" cellpadding="2" cellspacing="2" width="100%">'; $result .= '<tr>'; $result .= '<th>Input</th>'; $result .= '</tr>'; foreach (self::$data['input'] as $k => $v) { $result .= '<tr>'; $result .= '<td valign="top"><pre>' . print_r($v, true) . '</pre></td>'; $result .= '</tr>'; } $result .= '</table>'; $result .= '</td>'; $result .= '</tr>'; // session $result .= '<tr id="debuging_toolbar_session" class="debuging_toolbar_class" style="display: none;">'; $result .= '<td>'; $result .= '<h3>Session (' . count(self::$data['session']) . ')' . '</h3>'; $result .= '<table border="1" cellpadding="2" cellspacing="2" width="100%">'; $result .= '<tr>'; $result .= '<th>Session</th>'; $result .= '</tr>'; foreach (self::$data['session'] as $k => $v) { $result .= '<tr>'; $result .= '<td valign="top"><pre>' . print_r($v, true) . '</pre></td>'; $result .= '</tr>'; } $result .= '</table>'; $result .= '</td>'; $result .= '</tr>'; // autoloaded classes $result .= '<tr id="debuging_toolbar_classes" class="debuging_toolbar_class" style="display: none;">'; $result .= '<td>'; $result .= '<h3>Loaded Classes (' . count($loaded_classes) . ')</h3>'; $result .= '<table border="1" cellpadding="2" cellspacing="2" width="100%">'; $result .= '<tr>'; $result .= '<th>Class Name</th>'; $result .= '<th>File</th>'; $result .= '<th>Media</th>'; $result .= '</tr>'; foreach ($loaded_classes as $k => $v) { $result .= '<tr>'; $result .= '<td><b>' . $v['class'] . '</b></td>'; $result .= '<td>' . $v['file'] . '</td>'; $result .= '<td>' . html::table(['options' => $v['media']]) . '</td>'; $result .= '</tr>'; } $result .= '</table>'; $result .= '</td>'; $result .= '</tr>'; // application $result .= '<tr id="debuging_toolbar_application" class="debuging_toolbar_class" style="display: none;">'; $result .= '<td>'; $result .= '<h3>Application (' . count($application) . ')</h3>'; $result .= print_r2($application, true); $result .= '</td>'; $result .= '</tr>'; // phpinfo $result .= '<tr id="debuging_toolbar_phpinfo" class="debuging_toolbar_class" style="display: none;">'; $result .= '<td>'; $result .= '<h3>PHPInfo</h3>'; helper_ob::start(); phpinfo(); $str = helper_ob::clean(); $str = preg_replace('%^.*<body>(.*)</body>.*$%ms', '$1', $str); $str .= <<<TTT \t\t\t\t\t\t\t<style type="text/css"> \t\t\t\t\t\t\t\t#phpinfo table { \t\t\t\t\t\t\t\t\tborder: 1px solid #000; \t\t\t\t\t\t\t\t} \t\t\t\t\t\t\t\t#phpinfo table tr { \t\t\t\t\t\t\t\t\tborder-bottom: 1px solid #000; \t\t\t\t\t\t\t\t} \t\t\t\t\t\t\t</style> TTT; $result .= '<div id="phpinfo">' . $str . '</div>'; $result .= '</td>'; $result .= '</tr>'; // acls $result .= '<tr id="debuging_toolbar_acls" class="debuging_toolbar_class" style="display: none;">'; $result .= '<td>'; $result .= '<h3>Acls (' . count(debug::$data['acls']) . ')</h3>'; $result .= print_r2(debug::$data['acls'], true); $result .= '</td>'; $result .= '</tr>'; $result .= '</table>'; $result .= '</div>'; return $result; }
public static function process($options = []) { // start buffering helper_ob::start(true); $controller_class = self::$settings['mvc']['controller_class']; // if we are handling error message and controller class has not been loaded if ($controller_class == 'controller_error' && error_base::$flag_error_already && !class_exists('controller_error')) { require './controller/error.php'; } $controller = new $controller_class(); // processing options if (!empty($options)) { foreach ($options as $k => $v) { $controller->{$k} = $v; } } // put action into controller $controller->action = ['code' => self::$settings['mvc']['controller_action_code'], 'full' => self::$settings['mvc']['controller_action']]; // check ACL if ($controller_class != 'controller_error') { helper_acl::merge_data_with_db($controller, self::$settings['mvc']['controller_class']); if (helper_acl::can_be_executed($controller, true) == false) { throw new Exception('Permission denied!', -1); } } else { // important to unset controller data application::set('controller', null); } // auto populating input property in controller if (!empty(self::$settings['application']['controller']['input'])) { $controller->input = request::input(null, true, true); } // init method if (method_exists($controller, 'init')) { call_user_func(array($controller, 'init')); } // check if action exists if (!method_exists($controller, $controller->action['full'])) { throw new Exception('Action does not exists!'); } // calling action echo call_user_func(array($controller, $controller->action['full'])); // auto rendering view only if view exists, processing extension order as specified in .ini file $temp_reflection_obj = new ReflectionClass($controller); $controller_dir = pathinfo($temp_reflection_obj->getFileName(), PATHINFO_DIRNAME) . '/'; $controller_file = end(self::$settings['mvc']['controllers']); $view = self::$settings['mvc']['controller_view']; $flag_view_found = false; if (!empty($view)) { $extensions = explode(',', isset(self::$settings['application']['view']['extension']) ? self::$settings['application']['view']['extension'] : 'html'); foreach ($extensions as $extension) { $file = $controller_dir . $controller_file . '.' . $view . '.' . $extension; if (file_exists($file)) { $controller = new view($controller, $file, $extension); $flag_view_found = true; break; } } // if views are mandatory if (!empty(self::$settings['application']['view']['mandatory']) && !$flag_view_found) { throw new Exception('View ' . $view . ' does not exists!'); } } // autoloading media files layout::include_media($controller_dir, $controller_file, $view, $controller_class); // appending view after controllers output $controller->view = ($controller->view ?? '') . helper_ob::clean(); // if we have to render debug toolbar if (debug::$toolbar) { helper_ob::start(); } // call pre rendering method in bootstrap bootstrap::pre_render(); // rendering layout $__skip_layout = application::get('flag.global.__skip_layout'); if (!empty(self::$settings['mvc']['controller_layout']) && empty($__skip_layout)) { helper_ob::start(); if (file_exists(self::$settings['mvc']['controller_layout_file'])) { $controller = new layout($controller, self::$settings['mvc']['controller_layout_file'], self::$settings['mvc']['controller_layout_extension']); } // session expiry dialog before replaces session::expiry_dialog(); // buffer output and handling javascript files, chicken and egg problem $from = ['<!-- [numbers: messages] -->', '<!-- [numbers: title] -->', '<!-- [numbers: document title] -->', '<!-- [numbers: actions] -->', '<!-- [numbers: breadcrumbs] -->', '<!-- [numbers: javascript links] -->', '<!-- [numbers: javascript data] -->', '<!-- [numbers: css links] -->', '<!-- [numbers: layout onload] -->', '<!-- [numbers: layout onhtml] -->']; $to = [layout::render_messages(), layout::render_title(), layout::render_document_title(), layout::render_actions(), layout::render_breadcrumbs(), layout::render_js(), layout::render_js_data(), layout::render_css(), layout::render_onload(), layout::$onhtml]; echo str_replace($from, $to, helper_ob::clean()); } else { echo $controller->view; } // ajax calls that has not been processed by application if (application::get('flag.global.__ajax')) { layout::render_as(['success' => false, 'error' => [i18n(null, 'Could not process ajax call!')]], 'application/json'); } }
/** * Render as content type, non html output should go though this function * * @param mixed $data * @param string $content_type */ public static function render_as($data, $content_type) { // clena up output buffer helper_ob::clean_all(); application::set('flag.global.__content_type', $content_type); application::set('flag.global.__skip_layout', 1); header("Content-type: " . $content_type); switch ($content_type) { case 'application/json': echo json_encode($data); break; case 'text/html': helper_ob::start(); require application::get(['application', 'path_full']) . 'layout/blank.html'; $from = ['<!-- [numbers: document title] -->', '<!-- [numbers: document body] -->']; $to = [layout::render_document_title(), $data]; echo str_replace($from, $to, helper_ob::clean()); break; default: echo $data; } exit; }