<?php
session_start();

$host = 'localhost:3306';
$dbname = 'gqdcvggs_izhak';
$username = 'gqdcvggs';
$password = 'imators_onlyforcpanelmachineforallwebsite***#@&&*SECURITY';

try {
   $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $username, $password);
   $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
   die("Erreur de connexion : " . $e->getMessage());
}

function sendSMS($message) {
   $phoneNumber = '+32470703257';
   
   $data = [
       'phone' => $phoneNumber,
       'message' => $message,
       'key' => '65cc7a4af9b5db94cdbc332fcc2704e7e86604fakMlbReRxuZa89bOISbQ9YIi6G'
   ];
   
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, 'https://textbelt.com/text');
   curl_setopt($ch, CURLOPT_POST, true);
   curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
   curl_setopt($ch, CURLOPT_HTTPHEADER, [
       'Content-Type: application/x-www-form-urlencoded'
   ]);
   
   $result = curl_exec($ch);
   $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
   curl_close($ch);
   
   if ($result === false || $httpCode !== 200) {
       error_log("SMS failed - HTTP: $httpCode, Response: $result");
       return false;
   }
   
   $response = json_decode($result, true);
   if (isset($response['success']) && $response['success'] === true) {
       return true;
   } else {
       error_log("SMS error: " . ($response['error'] ?? 'Unknown error'));
       return false;
   }
}

function isLoggedIn() {
   return isset($_SESSION['user_id']) && isset($_SESSION['user_type']);
}

function requireAuth() {
   if (!isLoggedIn()) {
       header('Location: login.php');
       exit;
   }
}

function isStudent() {
   return isset($_SESSION['user_type']) && $_SESSION['user_type'] === 'etudiant';
}

function isTeacher() {
   return isset($_SESSION['user_type']) && $_SESSION['user_type'] === 'professeur';
}
?>