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

if(!defined('DB_INCLUDED')) {
    define('DB_INCLUDED', true);
    
try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->exec("SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci");
} catch(PDOException $e) {
    $pdo = null;
}
    
    if(isset($_SERVER['HTTP_ACCEPT']) && strpos($_SERVER['HTTP_ACCEPT'], 'application/json') !== false) {
        header('Content-Type: application/json');
        header('Access-Control-Allow-Origin: *');
        
        if($pdo) {
            try {
                $stmt = $pdo->query("SELECT id, title, description, content, date_created as date FROM articles ORDER BY date_created DESC");
                $articles = $stmt->fetchAll(PDO::FETCH_ASSOC);
                
                echo json_encode($articles);
                
            } catch(PDOException $e) {
                echo json_encode(['error' => 'Database connection failed: ' . $e->getMessage()]);
            }
        } else {
            echo json_encode(['error' => 'Database connection failed']);
        }
    }
}
?>