<?php
class ContentMS extends Settings {
    public $DB, $TotalQueries, $Variables, $Output, $Smarty;
    
    function ContentMS() {        
        date_default_timezone_set($this->Configuration('TimeZone'));
        require $this->Configuration('RootPath').'classes/class.smarty/Smarty.class.php';
        $this->Smarty = new Smarty;
        
        if ($this->Configuration('CacheMode') == 2) {
            $this->Smarty->caching = true;
            $this->Smarty->cache_lifetime = 120;        
        }
        
        switch($this->Configuration('ErrorReporting')) {
            case 0: error_reporting(0); break; 
            case 1: error_reporting(E_ERROR | E_WARNING | E_PARSE); break; 
            case 2: error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE); break; 
            case 3: error_reporting(E_ALL ^ (E_NOTICE | E_WARNING)); break; 
            case 4: error_reporting(E_ALL ^ E_NOTICE); break; 
            case 5: error_reporting(E_ALL); break; 
            default: error_reporting(E_ALL); 
        }    
    }
    
    public function MySQL($type, $param = null) {
        if (isset($this->DB)) {
            $DB = new mysqli
            ($this->Configuration('MySQLHost'), 
              $this->Configuration('MySQLUser'), 
              $this->Configuration('MySQLPass'), 
              $this->Configuration('MySQLData'), 
              $this->Configuration('MySQLPort'));
        }
        
        switch (strtolower($type)) {
            case 'query':
                if ($Query = $this->DB->query($param)) {
                    $TotalQueries++;
                    return $Query;
                } else $this->SystemExit('MySQLi failed to query: ' . $param, __LINE__, __FILE__);                
                break;
            case 'prepare':
                if ($Query = $this->DB->prepare($param)) {
                    $TotalQueries++;
                    return $Query;
                } else $this->SystemExit('MySQLi failed to prepare: ' . $param, __LINE__, __FILE__);
                
                break;
            case 'escapestring':                
                if ($Escape = $this->DB->real_escape_string($param)) {
                    return $Escape;
                } else $this->SystemExit('MySQLi failed to escape: ' . $param, __LINE__, __FILE__);          
                break;
            case 'close':
                if ($this->DB->close()) $this->MySQL->Connection = false;
                break;
        }
    }
    
    public function ApplyVariables($repeat = true) {
        foreach($this->Variables as $key => $one) {
            if (is_object($one) || strpos($key, '%') !== false) unset($this->Variables[$key]); 
        }
        
        $this->Output = empty($this->Variables) ? $this->Output : str_replace(array_keys($this->Variables), array_values($this->Variables), $this->Output);    
        $this->Variables = array_reverse($this->Variables, true);
        if ($repeat) $this->ApplyVariables(false);
    }
}
?>