<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

$config = require(__DIR__ . '/config.php');

//--- mysql ---
function connect_db() {
    global $config;
    $link = mysqli_connect($config['db_host'], $config['db_user'], $config['db_pass'], $config['db_name'], $config['db_port']);
    if (!$link) {
        die('Connexion impossible : ' . mysqli_error($link));
    }
    return $link;
}
function close_db($link){
    mysqli_close($link);
}
function do_query($sql){
    $link = connect_db();
    $result = mysqli_query($link, $sql) or die(mysqli_error($link));
    close_db($link);
    return $result;
}

function configure_session_lifetime($hours = 12) {
    if (session_status() === PHP_SESSION_ACTIVE) {
        return;
    }

    $session_lifetime = max(1, intval($hours)) * 60 * 60;

    ini_set('session.gc_maxlifetime', (string)$session_lifetime);
    ini_set('session.cookie_lifetime', (string)$session_lifetime);

    if (PHP_VERSION_ID >= 70300) {
        session_set_cookie_params([
            'lifetime' => $session_lifetime,
            'path' => '/',
            'secure' => !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off',
            'httponly' => true,
            'samesite' => 'Lax'
        ]);
    } else {
        session_set_cookie_params($session_lifetime, '/');
    }
}

// bridge_st_session_to_php() was here — removed after the full PHP→Python
// migration. All sessions are now st_session (Python), no PHPSESSID bridge
// needed. Function was introduced in Phase C, removed in the PHPSESSID
// bridge drop. The only remaining caller (retrieve_mails.php) doesn't use
// it — it has its own DB connection logic.

/**
 * Check if the current user has access to a database.
 */
function user_has_db_access($db_name) {
    $allowed = $_SESSION['allowed_dbs'] ?? '';
    if (empty($allowed)) return false;
    $list = array_map('trim', explode(',', $allowed));
    return in_array($db_name, $list);
}

/**
 * Get list of databases the current user can access.
 */
function user_allowed_dbs() {
    $allowed = $_SESSION['allowed_dbs'] ?? '';
    if (empty($allowed)) return [];
    return array_map('trim', explode(',', $allowed));
}

function isValInArray($field, $val, $array) {
   foreach ($array as $key => $row) {
       if ($row[$field] === $val) {
           return true;
       }
   }
   return false;
}

function make_discount_array($discount){
    $discount_tmp = array();

    $array_tmp = explode("|", $discount);
    $tmp = array();

    for($j = 0; $j < count($array_tmp); $j++) {
        $tmp = explode(':', $array_tmp[$j]);
        $discount_tmp[$j] = array(2);
        $discount_tmp[$j][0] = $tmp[0];
        $discount_tmp[$j][1] = $tmp[1];
    }

    return $discount_tmp;
}

function send_mail($subject, $body, $email){
  global $config;

  require 'vendor/autoload.php';

  $mail = new PHPMailer(true);
  $mail->isSMTP();
  $mail->Host = 'smtp.gmail.com';
  $mail->SMTPAuth = true;
  $mail->SMTPSecure = 'tls';
  $mail->Port = 587;
  $mail->SMTPDebug = 0;
  $mail->Username = $config['smtp_user'];
  $mail->From = $config['smtp_user'];
  $mail->Password = $config['smtp_pass'];
  $mail->FromName = 'Stealth';
  $mail->addAddress($email);
  $mail->addBCC($config['smtp_user']);

  $mail->WordWrap = 50;
  $mail->isHTML(true);

  $mail->Subject = iconv("UTF-8", "CP1252//TRANSLIT", $subject);
  $mail->Body    = iconv("UTF-8", "CP1252//TRANSLIT", $body);

  if(!$mail->send()) {
      // mail send failure is silently ignored
  }
}
?>
