function db_query($query, $mysql_connection_id = null)
{
    global $__mysql_connection_id, $app_config_file;
    global $__sql_counter;
    if (!isset($mysql_connection_id)) {
        $mysql_connection_id = $__mysql_connection_id;
    }
    $start = microtime(true);
    $id = cw_bench_open_tag('', 'sql', $query);
    $result = mysql_query($query, $mysql_connection_id);
    cw_bench_close_tag($id);
    if ($app_config_file['debug']['development_mode'] && !defined('IS_AJAX')) {
        $ll = microtime(true) - $start;
        /*		
        // Collect all table names mentioned in query
        		preg_match_all('/\bcw_\w+\s/',$query,$matches);
        		$_cnt = count($matches[0]);
        		for ($i=0; $i<$_cnt; $i++) 
        			$__sql_counter[strtolower(trim($matches[0][$i]))]++;   // +=$ll;
        */
        if ($ll > 1 && function_exists('cw_log_add')) {
            cw_log_add("log_slow_queries", "{$ll} msec: " . $query, true, 0, 0, 0);
        }
    }
    # Auto repair
    if (!$result && MYSQL_AUTOREPAIR && preg_match("/'(\\S+)\\.(MYI|MYD)/", mysql_error(), $m)) {
        $stm = "REPAIR TABLE {$m['1']} EXTENDED";
        error_log("Repairing table {$m['1']}", 0);
        if (DEBUG_MODE == 1 || DEBUG_MODE == 3) {
            $mysql_error = mysql_errno($mysql_connection_id) . " : " . mysql_error($mysql_connection_id);
            echo "<b><font COLOR=DARKRED>Repairing table {$m['1']}...</font></b>{$mysql_error}<br />";
            flush();
        }
        $result = mysql_query($stm, $mysql_connection_id);
        if (!$result) {
            error_log("Repaire table {$m['1']} is failed: " . mysql_errno() . " : " . mysql_error(), 0);
        } else {
            $result = mysql_query($query, $mysql_connection_id);
        }
        # try repeat query...
    }
    if (db_error($result, $query, $mysql_connection_id) && DEBUG_MODE == 1) {
        exit;
    }
    return $result;
}
 /**
  * Trigger specific event with a given parameters
  */
 function trigger($_event_, $include_mode = INCLUDE_RW_GLOBALS, $result = null)
 {
     $trigger_bench_id = cw_bench_open_tag($_event_, 'include', '');
     // Restore global scope in read only or read/write mode
     if ($include_mode == INCLUDE_RW_GLOBALS) {
         foreach ($GLOBALS as $gkey => $gval) {
             global ${$gkey};
         }
     } elseif ($include_mode == INCLUDE_R_GLOBALS) {
         /* TODO: useless mode, remove it */
         extract($GLOBALS);
     }
     $event = $_event_;
     // restore $event var in case it was in global scope
     if (isset($this->events[$event])) {
         if (is_array($this->events[$event])) {
             if (!$this->sorted) {
                 $this->sortEvents();
             }
             foreach ($this->events[$event] as $callback) {
                 // Go baby go...
                 if (isset($this->events[$callback]) && $callback != $event) {
                     $this->trigger($callback, $include_mode);
                 } else {
                     $callback_id = cw_bench_open_tag($callback, 'include', '');
                     if (file_exists($callback)) {
                         $callback_result = (include $callback);
                     } else {
                         $callback_result = null;
                     }
                     cw_bench_close_tag($callback_id);
                 }
             }
             // foreach
         }
         // if
     } else {
         if (file_exists($event)) {
             $callback_result = (include $event);
         } else {
             $callback_result = null;
         }
     }
     cw_bench_close_tag($trigger_bench_id);
     return $callback_result;
 }
 /**
  * Trigger specific event with a given parameters
  *
  * $result is start value of result. It determines how data returned from
  * callback functions will be handled. If $result is:
  *
  * - array - values will be added as new elements
  * - integer or float - values will be added to the $result
  * - string - values will be appended to current value
  * - null - last value returned from callback function
  *
  *
  * @param string $event
  * @param array $params
  * @param mixed $result
  * @return mixed
  */
 function trigger($event, $params, $result = null)
 {
     $trigger_bench_id = cw_bench_open_tag($event, 'call', '');
     $this->last_return = null;
     $callback_result = null;
     // if not exist - init event "on the fly"
     if (!isset($this->events[$event]) || !is_array($this->events[$event])) {
         $this->events[$event] = array(0 => $event);
     }
     if (!$this->sorted) {
         $this->sortEvents();
     }
     foreach ($this->events[$event] as $callback) {
         if (isset($this->events[$callback]) && $callback != $event) {
             // Hook also has own hooks
             $callback_return = $this->trigger($callback, $params, $result);
         } elseif (function_exists($callback)) {
             $callback_return = call_user_func_array($callback, $params);
         } elseif (strpos($event, 'on_') !== 0) {
             error_log("Expected function '{$event}' was not found. Check if it is defined and spelled correctly");
         } else {
             continue;
         }
         // Special handler of returned class EventReturn, which allows to return value and change inpput params
         if (instance_of($callback_return, 'EventReturn')) {
             if (!is_null($callback_return->params)) {
                 $params = $callback_return->params;
             }
             $callback_return = $callback_return->getReturn();
         }
         // null returned by hook means to ignore this hook in chain of results
         if (!is_null($callback_return)) {
             $callback_result = $callback_return;
             $this->last_return = $callback_result;
             if (is_array($result) && !is_null($callback_result)) {
                 $result[$callback] = $callback_result;
             } elseif (is_string($result)) {
                 $result .= $callback_result;
             } elseif (is_int($result) || is_float($result)) {
                 $result += $callback_result;
             }
         }
     }
     // foreach
     $this->last_return = null;
     cw_bench_close_tag($trigger_bench_id);
     return is_null($result) ? $callback_result : $result;
 }
$area = isset($_REQUEST['area']) ? $_REQUEST['area'] : 'customer';
$target = isset($_REQUEST['target']) ? $_REQUEST['target'] : 'index';
define('APP_START', 1);
$request_prepared = array();
define('APP_AREA', $area);
include_once $app_main_dir . '/init.php';
cw_include($area . '/auth.php');
cw_event('on_before_' . $target);
cw_event('on_before_' . $target . '_' . $action);
cw_include($area . '/' . $target . '.php');
if (defined('IS_AJAX') && !defined('PREVENT_XML_OUT')) {
    cw_include($area . '/ajax.php');
    exit(0);
}
$__script_microtime = microtime(true) - $__start_mictotime;
$__bech_display_id = cw_bench_open_tag('DISPLAY', 'POINT', '');
cw_display($area . '/index.tpl', $smarty, true);
cw_bench_close_tag($__bech_display_id);
$__smarty_microtime = microtime(true) - $__start_mictotime - $__script_microtime;
// Time end
if (!defined('IS_AJAX')) {
    // Time end
    $__output_microtime = 'Runtime: ' . sprintf("%.4f", $__smarty_microtime + $__script_microtime) . ' (SCRIPT: ' . sprintf("%.4f", $__script_microtime) . '; SMARTY: ' . sprintf("%.4f", $__smarty_microtime) . ')';
    if (!$app_config_file['debug']['development_mode']) {
        $__output_microtime = '<!-- ' . $__output_microtime . ' -->';
    } else {
        //    echo '<!-- SQL queries: ';asort($__sql_counter);var_dump($__sql_counter);
        //    echo "<br/>\n";
    }
    echo $__output_microtime;
}
 function _smarty_include($params)
 {
     global $ars_hooks;
     $pre = $post = $replace = array();
     $orig_file = $params['smarty_include_tpl_file'];
     $bench_id = cw_bench_open_tag($orig_file, 'tpl');
     if (is_array($ars_hooks['tpl'][$orig_file]) && !$params['smarty_include_vars']['disable_hooks']) {
         foreach ($ars_hooks['tpl'][$orig_file] as $type => $files) {
             foreach ($files as $file) {
                 if ($orig_file == $file[0]) {
                     continue;
                 }
                 // skip loop in hooks
                 $file[2] = $type;
                 $file[0] = $file[0];
                 if (!isset($file[1]) || $this->_tpl_vars[$file[1]] || function_exists($file[1]) && cw_call($file[1], array(&$params['smarty_include_vars'])) || strpos($file[1], '.tpl') !== false && $this->_tpl_vars['_current_compiled_file'] == $file[1]) {
                     switch ($type) {
                         case 'replace':
                             $replace[] = $file;
                             break;
                         case 'pre':
                             $pre[] = $file;
                             break;
                         case 'post':
                             $post[] = $file;
                             break;
                     }
                 }
             }
         }
     }
     if (empty($replace)) {
         $replace[] = array($orig_file, '', 'original');
     }
     $include_files = array_merge($pre, $replace, $post);
     foreach ($include_files as $file) {
         if (in_array($file[2], array('pre', 'post', 'replace'))) {
             $params['smarty_include_tpl_file'] = $file[0];
             $this->_smarty_include($params);
         } elseif ($file[2] == 'original') {
             $params['smarty_include_tpl_file'] = $file[0];
             if (!$this->template_exists($params['smarty_include_tpl_file'])) {
                 continue;
             }
             // skip non-existent tpl if it has hooks
             parent::_smarty_include($params);
         }
     }
     cw_bench_close_tag($bench_id);
 }
<?php

/*
 * Required includes
 */
include_once $app_main_dir . '/init/constants.php';
include_once $app_main_dir . '/include/lib/events/init.php';
include_once $app_main_dir . '/include/functions/cw.hook.php';
include_once $app_main_dir . '/include/functions/cw.core.php';
include_once $app_main_dir . '/init/globals.php';
$__bech_main_id = cw_bench_open_tag('MAIN', 'POINT', '');
include_once $app_main_dir . '/include/lib/error/init.php';
include_once $app_main_dir . '/include/lib/cache/init.php';
cw_load('db', 'files', 'session', 'prepare', 'user', 'auth', 'system_messages');
/*
 * AJAX detection
 */
if (cw_is_ajax_request()) {
    cw_load('ajax');
    define('IS_AJAX', true);
}
if (function_exists('date_default_timezone_set')) {
    if (!empty($app_config_file['time_zone']['default_zone'])) {
        date_default_timezone_set($app_config_file['time_zone']['default_zone']);
    }
}
cw_include('init/prepare.php');
include_once $app_main_dir . '/config.php';
// Redefine error_reporting option (see config.php)
error_reporting($app_error_reporting);
// Connect to database