<?php
    //require_once ('settings.php');
    $login= trim($_POST['login']);
    $password = trim($_POST['password']);
    require_once("scripts/ClassLibs/ClassMessageAndLogs.php");
    $ObjLogs=new MessageAndLogs();
    if ($_POST['inp_language'])
    {
        $lng=$_POST['inp_language'];
    }
    else
    {
        $lng=0;
    }
    $is_bloked = "";
    if ($login!='')
    if (($login || $login!='') && ($password || $password!='')) {
    //connecting to db
        if (!get_magic_quotes_gpc()){
            $login = addslashes($login);
            $password = addslashes ($password);
        }
        
        $db = new mysqli ($DB_HOST,$DB_USER,$DB_PASS,$DB_NAME,$DB_PORT);
        $db->query("SET CHARSET SET utf8");
        $db->query("SET NAMES 'utf8'");
        if (mysqli_connect_errno()) {
            //echo "Error connecting to DB";
            exit;
        }
       
        $query = "SELECT COUNT(*),u.role_id, u.client_id, u.id, u.frequency, u.locale, s.milestone,s.MapCenter_longitude,s.MapCenter_latitude,".        "s.defaultMap_scale,s.gmt,s.use_fixed_data,c.is_blocked,b.balance,s.time_track,s.dist_track, s.car_name_format, c.`client_type`, s.`lost_signal`, u.`theme`, s.monit_from, s.groupmarker FROM wm_users AS u LEFT JOIN wm_settings AS s ON u.client_id = s.client_id LEFT JOIN wm_clients c on u.client_id=c.id LEFT JOIN wm_balances b ON b.client_id=c.id WHERE (u.login=? AND u.password=?)";
        //$query = "SELECT COUNT(*),u.role_id, u.client_id, u.id, u.frequency, u.locale, s.milestone,s.MapCenter_longitude,s.MapCenter_latitude,".        "s.defaultMap_scale,s.gmt,s.use_fixed_data,c.is_blocked,b.balance,s.time_track,s.dist_track, s.car_name_format, c.`client_type`, s.`lost_signal`, u.`theme` FROM wm_users AS u LEFT JOIN wm_settings AS s ON u.client_id = s.client_id LEFT JOIN wm_clients c on u.client_id=c.id LEFT JOIN wm_balances b ON b.client_id=c.id WHERE (u.login=".$login." AND u.password=".md5($password).")";
		//echo  $query;
        $stmt = $db->prepare($query);
        $stmt->bind_param('ss',$login,md5($password));
        $stmt->execute();
        $stmt->bind_result($result,$role_id,$client_id, $id, $frequency, $locale, $milestone, $MapCenter_longitude, $MapCenter_latitude,$defaultMap_scale,$gmt,$fixed_data,$bloked,$balance,$time_track,$dist_track,$car_name_format,$client_type,$lost_signal,$theme, $monit_from, $murkercluster);
        $stmt->fetch(); // Get a result row as an enumerated array
        $is_bloked = $bloked;
        if ($result > 0 && $is_bloked != 1) {
            $ObjLogs->WriteLogAttendance("Вход в систему; client_id:".$client_id,'AttendanceLog','logs');
            $ObjLogs->WriteLogsToDB(1,"client_id:".$client_id,1,$login);
            $_SESSION['id'] = $id;
            $_SESSION['role_id'] = $role_id;
            $_SESSION['client_id'] = $client_id;
            $_SESSION['frequency'] = $frequency;
            $_SESSION['locale'] = $locale;
			$_SESSION['milestone'] = $milestone;
            $_SESSION['click_zone_monitoring'] = 0;
            $_SESSION['click_favor_zone_monitoring'] = 0;
            $_SESSION['lng']=$lng;
            $_SESSION['login']=$login;
            $_SESSION['password']=$password;
            $_SESSION['MapCenter_latitude'] = $MapCenter_latitude;
            $_SESSION['MapCenter_longitude'] = $MapCenter_longitude;
            $_SESSION['defaultMap_scale'] = $defaultMap_scale;
            $_SESSION['gmt'] = $gmt;
            $_SESSION['fixed_data'] = $fixed_data;
            $_SESSION['balance'] = $balance;
            $_SESSION['show_bal_mess'] = '0';
            $_SESSION['time_track'] = $time_track;
            $_SESSION['dist_track'] = $dist_track;
            $_SESSION['car_name_format'] =$car_name_format;
			
			$_SESSION['client_type'] = $client_type;
			$_SESSION['lost_signal'] = $lost_signal;			
			$_SESSION['theme'] = $theme;
			
			$_SESSION['startTime'] = date('Y-m-d', time()).' '.$monit_from.':00';
			$_SESSION['murkercluster']=$murkercluster;
            //logging
       
           /* $a = new Action_log();
            $a->dir_name = $dirname;
            $query = "INSERT INTO tms_log (login,action,time) VALUES ('".$_SESSION['id']."','login',NOW())";
            $a->Save($query);*/
            
            $URL = 'index.php';
            uf_redirect($URL);
            exit;
        }
        $stmt->close();
    }
    
?>
