<?php
if (!defined('DB_HOST')) define('DB_HOST', '127.0.0.1');
if (!defined('DB_NAME')) define('DB_NAME', 'gabinete');
if (!defined('DB_USER')) define('DB_USER', 'root');
if (!defined('DB_PASS')) define('DB_PASS', 'root'); // MAMP usual

function pdo_conn(){
  static $pdo=null;
  if ($pdo instanceof PDO) return $pdo;
  $dsn='mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset=utf8mb4';
  $opts=array(PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION,PDO::ATTR_DEFAULT_FETCH_MODE=>PDO::FETCH_ASSOC,PDO::ATTR_EMULATE_PREPARES=>false);
  $pdo=new PDO($dsn,DB_USER,DB_PASS,$opts);
  return $pdo;
}
function db_get_user_by_email($email){
  $pdo=pdo_conn();
  $st=$pdo->prepare("SELECT id,nome,email,senha,nivel_acesso,ativo FROM usuarios WHERE email=? AND ativo=1 LIMIT 1");
  $st->execute(array($email));
  $row=$st->fetch();
  return $row?$row:null;
}
