<?php
/**
 * Front controller for the blog REST API.
 *
 * Point the blog admin's "API URL" setting at this file, e.g.:
 *   https://yoursite.com/backend-php/api/index.php
 *
 * assets/blog-client.js then requests "<that URL>/posts", "<that URL>/posts/my-slug",
 * etc. — PHP receives everything after index.php as $_SERVER['PATH_INFO'], which is
 * how a single file can serve both "/posts" and "/posts/:slug" without needing any
 * .htaccess rewrite rules (this works out of the box on standard Apache+PHP hosting).
 *
 * Nginx note: PATH_INFO isn't parsed by default under Nginx's usual PHP-FPM config.
 * If you're on Nginx, add a fastcgi_split_path_info rule (search "nginx PATH_INFO
 * php-fpm"), or ask your host's support for the one-line config change.
 */
require_once __DIR__ . '/../includes/auth.php';
send_cors_headers();

$pathInfo = $_SERVER['PATH_INFO'] ?? '';
$parts = array_values(array_filter(explode('/', $pathInfo)));

if (($parts[0] ?? '') === 'posts') {
    require __DIR__ . '/posts.php';
    handle_posts_route($parts);
    exit;
}

json_response(['error' => 'Not found'], 404);
