<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class core extends MX_Controller {

    function __construct() {
        parent::__construct();
        
        // Enable profiler
        if ($this->config->item('enable_profiler')) {
            $this->output->enable_profiler(TRUE);
        }

        // XSS filter $_POST, $_GET, $_COOKIE
        $global_xss_filtering = $this->config->item('global_xss_filtering');
        if ($global_xss_filtering === FALSE) {
            $_GET = $this->input->get(NULL, TRUE);
            $_POST = $this->input->post(NULL, TRUE);
            $_COOKIE = $this->input->cookie(NULL, TRUE);
        }
    }

    public function index() {

        // Browser check
        if ($this->config->item('check_browser') == TRUE) {
            $this->load->library('ciext/browser');
            $this->browser->check_browser();
        }

        // Check for database class loaded
        if (!$this->load->is_loaded('session')) {

            redirect(base_url() . 'admin/install');
        }

        $this->load->model('ciext/core_model');
        $this->lang->load('ciext/core');

        // Get page
        $page = $this->core_model->get_page();

        // If not found
        if (empty($page)) {

            show_404();
        }

        // Render page
        $this->parser->parse_string($page, array());
    }

    public function get_assets($file = '') {
        // Loading from cache
        $this->load->driver('cache', $this->config->item('cache_init_config'));
        $data = $this->cache->get($file);
        // Direct file loading
        if ($file == 'direct') {
            $direct_file = $this->config->item('assets_folder') . '/' . str_replace(array('../', './'), '', $this->input->get('file'));
            if (file_exists($direct_file)) {
                $file = $direct_file;
                $data = file_get_contents($direct_file);
            }
        }

        $ext = pathinfo($file, PATHINFO_EXTENSION);
        // Enable GZip encoding.
        ob_start("ob_gzhandler");
        // Enable caching
        header('Cache-Control: public');
        // Expire in 2 weeks
        header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 1209600) . ' GMT');

        switch ($ext) {
            case 'css' :
                header("Content-Type: text/css");
                // Minimize
                if (ENVIRONMENT == 'production') {
                    $this->load->library('cssmin', $data);
                    $data = $this->cssmin->min();
                }
                break;

            case 'js':
                header('Content-Type: application/javascript');
                // Minimize
                if (ENVIRONMENT == 'production') {
                    $this->load->library('jsmin', '');
                    $data = JSMin::minify($data);
                }
                break;

            default :
                // Deny file
                show_404();
                break;
        }

        die($data);
    }

}

/* End of file core.php */
