<?php 
  include "settings.php";
  include "logger.php";
  
  $db = new mysqli($DB_HOST,$DB_USER,$DB_PASS,$DB_NAME);
  $db->query("SET CHARSET SET utf8");
  $db->query("SET NAMES 'utf8'");
  if (mysqli_connect_errno()) {
    echo "Error connecting to DB";
    exit;
  }
  writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> loginClient <> '.$_POST['data'] );
  
  $data = $_POST['data'];
  $data = stripslashes($data);
  $arraydata = (array)json_decode($data);

  if ($arraydata['func']=='check_login')
  {
    $login = $arraydata["login"];
    $password = $arraydata["password"];

    $i=0;
    $Model=array();
	$query = "SELECT COUNT(`id`),`id`,`instance_id`,id_role FROM ".$DB_PREFIX."users WHERE `name`='".$login."' AND `password`= '".$password."';";
    if ($stmt = $db->prepare($query)){
      $stmt->execute();
      $stmt->bind_result($count_id,$id,$instance_id,$id_role);


      while ($stmt->fetch()) {
        $Model[$i]=array(
			'count'=>$count_id,
			'id'=>$id,
			'instance_id'=>$instance_id,
			'id_role'=>$id_role
        );
        $i++;
      } 
      $stmt->close();
      $db->close();
    }
    echo json_encode($Model);
	writeToLogFile( ' -> '.date('Y-m-d H:i:s').' <> loginClient <> '.json_encode($Model) );
  }
?>