<?php
// === DB CONFIG (do NOT expose publicly) ===
define('DB_HOST', 'localhost');
define('DB_NAME', 'slmbredirects_guaranti');
define('DB_USER', 'slmbredirects_guaranti');
define('DB_PASS', 'Reza1372!@');
define('DB_CHARSET', 'utf8mb4');

// === TELEGRAM ===
define('TG_BOT_TOKEN', '490048283:AAGEC4Cj0c8SkIicNrMXl7QNDLC2rKK5LAs');
define('TG_CHAT_REQUESTS', '-1003122605108'); // گروه اول: درخواست نصب
define('TG_CHAT_INSTALLS', '-1003058637754'); // گروه دوم: نصب انجام‌شده

function db(){
  static $mysqli=null;
  if($mysqli===null){
    $mysqli=new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    if($mysqli->connect_errno){
      http_response_code(500);
      die(json_encode(['success'=>false,'error'=>'DB connect failed: '.$mysqli->connect_error], JSON_UNESCAPED_UNICODE));
    }
    $mysqli->set_charset(DB_CHARSET);
  }
  return $mysqli;
}

function esc_html($s){ return htmlspecialchars(trim($s??''), ENT_QUOTES|ENT_SUBSTITUTE, 'UTF-8'); }

function gen_tracking($len=6){
  $chars='ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
  $t=''; for($i=0;$i<$len;$i++) $t.=$chars[random_int(0,strlen($chars)-1)];
  return 'SBG'.$t;
}

function tg_send_message($chat_id,$html){
  $url="https://api.telegram.org/bot".TG_BOT_TOKEN."/sendMessage";
  $payload=['chat_id'=>$chat_id,'text'=>$html,'parse_mode'=>'HTML','disable_web_page_preview'=>false];
  $ch=curl_init($url);
  curl_setopt_array($ch,[CURLOPT_POST=>true,CURLOPT_RETURNTRANSFER=>true,CURLOPT_POSTFIELDS=>$payload,CURLOPT_TIMEOUT=>20,CURLOPT_SSL_VERIFYPEER=>false,CURLOPT_SSL_VERIFYHOST=>false]);
  $res=curl_exec($ch); curl_close($ch);
  return $res;
}
?>