<?php
class MessageAndLogs 
{
    var $DB;//Переменная базы данных
    var $StartTime;
    
    function MessageAndLogs()
    {
//<<<<<<< .mine
        //require_once("../settings.php");
        $this->DB = new mysqli ('localhost','root','','monitor');
//=======
        //require_once("settings.php");
        //$this->DB = new mysqli ('localhost','root','1111','tmslogistic');
//>>>>>>> .r70
        $this->DB->query("SET CHARSET SET utf8");
        $this->DB->query("SET NAMES 'utf8'");  
        //echo $DB_HOST.','.$DB_USER.','.$DB_PASS.','.$DB_NAME; 
    }
    
    
    function WriteLogs($Message,$Name,$Path)
    {
        $Date = date("Y_m_d");
        $NameFile = $Date."_".$Name.".txt";
        $FullPath=$Path . '/' . $NameFile;
        if  (!$Handle = @fopen($FullPath, "a")) 
        {
            exit;
        }
        else 
        {
            if (@fwrite($Handle,"$Message\r\n") === FALSE) 
            {
                exit;
            }
            @fclose($Handle);
        }
    }



    function WriteLogAttendance($Message,$Name,$Path)
    {
        $IP=$this->GetRealIP();
        $Date = date("Y_m_d");
        $TimeNow=date( 'Y-m-d H:i:s', time());
        $NameFile = $Date."_".$Name.".txt";
        $FullPath=$Path . '/' . $NameFile;
        $Message="IP:".$IP."; TIME:".$TimeNow."; ".$Message;
        if  (!$Handle = @fopen($FullPath, "a")) 
        {
            exit;
        }
        else 
        {
            if (@fwrite($Handle,"$Message\r\n") === FALSE) 
            {
                exit;
            }
            @fclose($Handle);
        }   
    }
    
     //$Type=1 : начало отчета
     //$Type=2 : конец отчета
    function WriteLogsToDB($Type,$Notice,$Type,$Login)
    {
        switch ($Type)
        {
            case 1:
                    $TimeNow=date( 'Y-m-d H:i:s', time());
                    $this->StartTime=time();
                    $IP=$this->GetRealIP();
                    $Query = "INSERT INTO wm_logs_attendance (`date`,`ip`,`type`,`login`,`notice`) VALUES ('".$TimeNow."','".$IP."',".$Type.",'".$Login."','".$Notice."');";
                    $Stmt = $this->DB->prepare($Query);
                    $Stmt->execute();
                    $Stmt->close();
            break;
            case 2:
                    $TimeNow=date( 'Y-m-d H:i:s', time());
                    $Duration = time()-$this->StartTime;
                    $IP=$this->GetRealIP();
                    $Query = "INSERT INTO wm_logs_attendance (`date`,`ip`,`type`,`duration`,`login`,`notice`) VALUES ('".$TimeNow."','".$IP."',".$Type.",".$Duration.",'".$Login."','".$Notice."');";
                    $Stmt = $this->DB->prepare($Query);
                    $Stmt->execute();
                    $Stmt->close();
            break;     
        }
            
    }
    
    
    function GetRealIP()
    {
        /*if (!empty($_SERVER['HTTP_CLIENT_IP'])) 
        {
            $IP=$_SERVER['HTTP_CLIENT_IP'];
        }      
        elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
        {
            $IP=$_SERVER['HTTP_X_FORWARDED_FOR'];
        }
        else
        {
            $IP=$_SERVER['REMOTE_ADDR'];
        }   */
        $IP=$_SERVER['HTTP_CLIENT_IP'].' - '.$_SERVER['HTTP_X_FORWARDED_FOR'].' - '.$_SERVER['REMOTE_ADDR'];
        return $IP;
    }

}
?>
