function cw_timer($label)
{
    static $timer;
    $now = cw_core_microtime();
    if (empty($timer)) {
        $timer = $now;
    }
    if (!defined('IS_AJAX')) {
        echo $label . ': ' . ($now - $timer) . "<br/>\n";
    }
    $timer = $now;
}
function smarty_function_edit_on_place($p, $smarty)
{
    global $REQUEST_URI;
    static $start;
    if (empty($p['table']) || empty($p['field']) || $p['pk'] . $p['where'] == '') {
        return '';
    }
    $cw_tokens =& cw_session_register('cw_tokens', array());
    $group = md5($REQUEST_URI);
    if (is_null($start) && !defined('IS_AJAX')) {
        cw_unset_tokens_group($group);
        $start = true;
    }
    $t = md5(cw_core_microtime() . serialize($p) . rand(1, 100));
    $cw_tokens[$group][$t] = $t;
    $cw_tokens[$group]['time'] = CURRENT_TIME;
    $cw_tokens[$t] = array('group' => $group, 'table' => $p['table'], 'field' => $p['field'], 'pk' => $p['pk'], 'where' => $p['where'], 'value' => $p['value'], 'handler' => $p['handler'], 'time' => CURRENT_TIME);
    if ($p['token_only']) {
        return $t;
    } else {
        return 'class="edit_on_place" token="' . $t . '"';
    }
}
function cw_bench_close_tag($id)
{
    global $__bench, $__bench_depth, $__bench_max_memory;
    if (!constant('BENCH') || !isset($__bench[$id])) {
        return false;
    }
    $__bench[$id]['end_time'] = cw_core_microtime();
    $__bench[$id]['end_mem'] = memory_get_usage(true);
    $__bench_depth--;
}
function cw_send_survey_invitation($survey_id, $email, $customer_id, $force = false)
{
    global $smarty, $config, $tables, $http_location;
    global $app_main_dir, $admin_safe_mode, $app_catalogs;
    static $store_surveys = array();
    include $app_main_dir . "/include/safe_mode.php";
    cw_load("mail", "user");
    # Check respondent status
    $is_valid = cw_query_first_cell("SELECT COUNT(*) FROM {$tables['survey_maillist']} WHERE survey_id = '{$survey_id}' AND email = '" . addslashes($email) . "' AND sent_date = 0") > 0;
    if (!$is_valid && !$force) {
        return -1;
    }
    if (!isset($store_surveys[$survey_id])) {
        $store_surveys[$survey_id] = cw_get_survey($survey_id);
    }
    $survey = $store_surveys[$survey_id];
    $survey['complete'] = cw_eol2br($survey['complete']);
    $survey['header'] = cw_eol2br($survey['header']);
    $survey['footer'] = cw_eol2br($survey['footer']);
    $smarty->assign('survey', $survey);
    # Geet access key
    $access_key = cw_query_first_cell("SELECT access_key FROM {$tables['survey_maillist']} WHERE survey_id = '{$survey_id}' AND email = '" . addslashes($email) . "'");
    if (empty($access_key)) {
        # Generate access key
        srand(cw_core_microtime());
        $access_key = md5(rand(0, time()));
        cw_array2update("survey_maillist", array("access_key" => $access_key), "survey_id = '{$survey_id}' AND email = '" . addslashes($email) . "'");
    }
    $smarty->assign('link', $http_location . "/survey.php?survey_key=" . $access_key);
    # Get repondents info (if respondent - customer)
    $userinfo = array();
    if (!empty($customer_id)) {
        $userinfo = cw_user_get_info($customer_id);
        $smarty->assign('userinfo', $userinfo);
    } else {
        $smarty->assign('userinfo', false);
    }
    # Send invitation
    $to_customer = $userinfo['language'] ? $userinfo['language'] : $config['default_customer_language'];
    cw_call('cw_send_mail', array($config['Company']['support_department'], $email, "mail/survey_invitation_subj.tpl", "mail/survey_invitation.tpl"));
    cw_array2update("survey_maillist", array("sent_date" => time(), "delay_date" => 0), "survey_id = '{$survey_id}' AND email = '" . addslashes($email) . "'");
    return true;
}
} elseif (!empty($HTTP_FORWARDED)) {
    $PROXY_IP = $HTTP_FORWARDED;
} elseif (!empty($HTTP_CLIENT_IP)) {
    $PROXY_IP = $HTTP_CLIENT_IP;
} elseif (!empty($HTTP_X_COMING_FROM)) {
    $PROXY_IP = $HTTP_X_COMING_FROM;
} elseif (!empty($HTTP_COMING_FROM)) {
    $PROXY_IP = $HTTP_COMING_FROM;
}
if (!empty($PROXY_IP)) {
    $CLIENT_IP = $PROXY_IP;
    $PROXY_IP = $REMOTE_ADDR;
} elseif (isset($REMOTE_ADDR)) {
    $CLIENT_IP = $REMOTE_ADDR;
}
if (isset($_GET['benchmark']) || isset($_POST['benchmark'])) {
    define("START_TIME", cw_core_microtime());
}
/**
 * Service server for updates, news, adv
 */
define('SERVICE_SERVER', base64_decode('d3d3LmNhcnR3b3Jrcy1wbGF0Zm9ybS5jb20='));
define('SERVICE_SERVER_SCRIPT', '/index.php?');
#
# Allow displaying content in functions, registered in register_shutdown_function()
#
$zlib_oc = ini_get("zlib.output_compression");
if (!empty($zlib_oc)) {
    define("NO_RSFUNCTION", true);
}
unset($zlib_oc);