Example #1
0
function cs_ajaxwrap()
{
    global $cs_main, $account;
    $json = array();
    header('Content-Type:application/json');
    if (empty($cs_main['public']) and $account['access_clansphere'] < $cs_main['maintenance_access']) {
        return json_encode(array('location' => '', 'reload' => 1));
    }
    if (!isset($_REQUEST['xhr_nocontent'])) {
        $content = cs_contentload($cs_main['show']);
        $json['title'] = html_entity_decode(cs_title(), ENT_NOQUOTES, $cs_main['charset']);
        $pathPrefix = str_replace('\\', '/', $cs_main['php_self']['dirname'] . $cs_main['php_self']['filename']) . '/';
        $uri = preg_replace('/^(.*?)\\.php\\??(.*?)$/s', '\\2', $_SERVER['REQUEST_URI']);
        $uri = preg_replace('/[&\\?\\/]?(xhr_navlists[=\\/])[^&\\/]*/s', '', $uri);
        $uri = str_replace(array('&xhr=1', '/xhr/1', '&xhr_nocontent=1', 'params=/', '/params//', $pathPrefix), '', $uri);
        $uri = preg_replace('/' . str_replace('/', '\\/', $cs_main['php_self']['dirname']) . '/s', '', $uri, 0);
        $json['location'] = $uri;
        if (isset($cs_main['ajax_js'])) {
            $json['scripts'] = $cs_main['ajax_js'];
        }
        $json['content'] = $content;
    }
    if (isset($_REQUEST['xhr_navlists'])) {
        $navs = explode(',', $_REQUEST['xhr_navlists']);
        $navlists = array();
        foreach ($navs as $nav) {
            $navlist = explode('-', $nav);
            if ($navlist[1] != 'func') {
                $navlists[$nav] = cs_templatefile($navlist);
            }
        }
        $json['navlists'] = $navlists;
    }
    if (!empty($cs_main['debug'])) {
        global $cs_logs;
        $cs_logs['php_errors'] = nl2br($cs_logs['php_errors']);
        $cs_logs['errors'] = nl2br($cs_logs['errors']);
        $data = array('data');
        $data['data']['log_sql'] = cs_log_format('sql');
        $data['data']['php_errors'] = $cs_logs['php_errors'];
        $data['data']['csp_errors'] = $cs_logs['errors'];
        $json['debug'] = cs_subtemplate(__FILE__, $data, 'clansphere', 'debug');
    }
    return json_encode($json);
}
Example #2
0
function cs_template($cs_micro, $tpl_file = 'index.htm')
{
    global $account, $cs_logs, $cs_main;
    if ((empty($cs_main['public']) or $tpl_file == 'admin.htm' and $account['access_clansphere'] < 3) and $account['access_clansphere'] < $cs_main['maintenance_access']) {
        $cs_main['show'] = 'mods/users/login.php';
        $tpl_file = 'login.htm';
        $cs_main['ajax'] = 0;
    }
    if (!empty($account['users_tpl'])) {
        $cs_main['template'] = $account['users_tpl'];
    }
    if (!empty($_GET['template'])) {
        $cs_main['template'] = str_replace(array('.', '/'), '', $_GET['template']);
    }
    if (!empty($_SESSION['tpl_preview'])) {
        $cs_main['template'] = str_replace(array('.', '/'), '', $_SESSION['tpl_preview']);
    }
    if ($tpl_file == 'error.htm') {
        $cs_main['template'] = 'install';
    }
    if ($cs_main['template'] != $cs_main['def_tpl'] and !is_dir('templates/' . $cs_main['template'])) {
        $cs_main['template'] = $cs_main['def_tpl'];
    }
    $tpl_path = $cs_main['def_path'] . '/templates/' . $cs_main['template'] . '/' . $tpl_file;
    if (!file_exists($tpl_path)) {
        cs_error($tpl_path, 'cs_template - Template file not found');
        $msg = 'Template file not found: ' . $tpl_file;
        if ($tpl_file != 'error.htm') {
            die(cs_error_internal('tpl', $msg));
        } else {
            die($msg);
        }
    }
    # Initalize array of upcoming additions and get show content
    $replace = array('func:body_add' => '');
    $replace['func:show'] = '<div id="csp_content">' . cs_contentload($cs_main['show']) . '</div>';
    if (isset($cs_main['ajax']) and $cs_main['ajax'] == 2 or !empty($account['users_ajax']) and !empty($account['access_ajax'])) {
        $replace['func:body_add'] = ' onload="Clansphere.initialize(' . $cs_main['mod_rewrite'] . ',\'' . $_SERVER['SCRIPT_NAME'] . '\',' . $cs_main['ajax_reload'] * 1000 . ')"';
    }
    # Provide the def_title and a title with more information
    $replace['func:title_website'] = htmlentities($cs_main['def_title'], ENT_QUOTES, $cs_main['charset']);
    $replace['func:title'] = cs_title();
    # Fetch template file and parse exploded contents
    $template = cs_cache_template($tpl_file);
    $template = cs_tokenizer_parse($template);
    # Add scriptload to replaces
    global $cs_main;
    $replace['func:stylesheet'] = empty($cs_main['scriptload']['stylesheet']) ? '' : $cs_main['scriptload']['stylesheet'];
    $replace['func:javascript'] = empty($cs_main['scriptload']['javascript']) ? '' : $cs_main['scriptload']['javascript'];
    # Prepare debug and log data
    $debug = '';
    $logsql = '';
    if (!empty($cs_main['developer']) or $account['access_clansphere'] > 4) {
        $cs_logs['php_errors'] = nl2br($cs_logs['php_errors']);
        $cs_logs['errors'] = nl2br($cs_logs['errors']);
        $logsql = cs_log_format('sql');
    } else {
        $cs_logs['php_errors'] = '';
        $cs_logs['errors'] = 'Developer mode is turned off';
    }
    if (!empty($cs_main['debug'])) {
        $data = array('data');
        $data['data']['log_sql'] = $logsql;
        $data['data']['php_errors'] = $cs_logs['php_errors'];
        $data['data']['csp_errors'] = $cs_logs['errors'];
        $debug = cs_subtemplate(__FILE__, $data, 'clansphere', 'debug');
    }
    $replace['func:queries'] = $cs_logs['queries'];
    $replace['func:errors'] = $cs_logs['php_errors'] . $cs_logs['errors'];
    $replace['func:sql'] = $logsql;
    $replace['func:debug'] = $debug;
    $replace['func:parse'] = cs_parsetime($cs_micro);
    $replace['func:memory'] = function_exists('memory_get_usage') ? cs_filesize(memory_get_usage()) : '-';
    if (function_exists('memory_get_peak_usage')) {
        $replace['func:memory'] .= ' [peak ' . cs_filesize(memory_get_peak_usage()) . ']';
    }
    # Assemble content parts
    $result = '';
    foreach ($template as $num => $content) {
        if (array_key_exists($content, $replace)) {
            $result .= $replace[$content];
        } else {
            $result .= $content;
        }
    }
    # Enable zlib output compression if possible
    if (!empty($cs_main['zlib']) and extension_loaded('zlib')) {
        ob_start('ob_gzhandler');
    }
    # Send content type header with charset
    header('Content-type: text/html; charset=' . $cs_main['charset']);
    return $result;
}