<?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') == 1) {
            $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); 
        }    
    }
	
    /**
     * Handles database data transactions 
     *
     * @param type: Type of method (Query, Prepare, EscapeString, Close)
     * @param param: Data variable
     */
    public function MySQL($type, $param = null) {
        if (!isset($this->DB)) {
            $this->DB = new mysqli
            (Settings::getProtected('MySQLHost'), 
             Settings::getProtected('MySQLUser'), 
             Settings::getProtected('MySQLPass'), 
             Settings::getProtected('MySQLData'), 
             Settings::getProtected('MySQLPort'));
        }
        
        switch (strtolower($type)) {
            case 'query':
                if ($Query = $this->DB->query($param)) {
                    $this->TotalQueries++;
                    return $Query;
                } else $this->SystemExit('MySQLi failed to query: ' . $param, __LINE__, __FILE__);                
                break;
            case 'prepare':
                if ($Query = $this->DB->prepare($param)) {
                    $this->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':
                $this->DB->close();
                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);
    }
    
    /**
     * Terminates the script and print out error details
     *
     * @param text: Information regarding the error
     * @param line: Line where the error occurs in the script
     * @param file: File location of the error
     */
    public function SystemExit($text, $line, $file = null) {
        if (ob_get_level()) ob_end_clean();
        $this->MySQL('Close');
        header('Content-Type: text/plain');
        print ("$text - " . date("F j, Y, g:i a"));
        print ("\nLocation: $file ($line)");
        exit(1);
    }
}
?>