����JFIF��`�`�����Viewing File: /home/u820193700/domains/spiti2026im.in/public_html/api/qa_public.php
<?php
// api/qa_public.php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');

require_once 'db_connect.php';

if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    try {
        $rows = $pdo->query("SELECT uid, question, answer, asked_by, created_at FROM qa WHERE published = 1 ORDER BY created_at ASC")->fetchAll(PDO::FETCH_ASSOC);
        echo json_encode(['qa' => $rows]);
    } catch (PDOException $e) {
        http_response_code(500);
        echo json_encode(['error' => $e->getMessage()]);
    }
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $ip = $_SERVER['REMOTE_ADDR'] ?? 'unknown';
    $rate_file = sys_get_temp_dir() . '/spiti_qa_rate_' . md5($ip) . '.json';
    $count = 0;
    $time_window = time();

    if (file_exists($rate_file)) {
        $data = json_decode(file_get_contents($rate_file), true);
        if ($data) {
            $count = $data['count'] ?? 0;
            $time_window = $data['time'] ?? time();
        }
    }

    $now = time();
    if (($now - $time_window) > 3600) { $count = 0; $time_window = $now; }
    $count++;

    if ($count > 3) {
        http_response_code(429);
        echo json_encode(['error' => 'Too many questions submitted.']);
        exit;
    }
    
    @file_put_contents($rate_file, json_encode(['count' => $count, 'time' => $time_window]));

    $input = json_decode(file_get_contents('php://input'), true);
    $question = trim($input['question'] ?? '');
    $asked_by = trim($input['asked_by'] ?? 'Anonymous');

    if (strlen($question) > 500) {
        http_response_code(400); echo json_encode(['error' => 'Question too long.']); exit;
    }
    $asked_by = substr(strip_tags($asked_by), 0, 100);

    $stmt = $pdo->prepare("INSERT INTO qa (uid, question, asked_by) VALUES (?, ?, ?)");
    $stmt->execute([0, $question, $asked_by]);
    if ($stmt->rowCount() > 0) {
        $newId = $pdo->lastInsertId();
        $pdo->query("UPDATE qa SET uid = $newId WHERE id = $newId");
    }
    echo json_encode(['success' => true]);
}
Back to Directory �������}�!1AQa"q2���#B��R��$3br� %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz�������������������������������������������������������������������������������� ������w�!1AQaq"2�B���� #3R�br� $4�%�&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz������������������������������������������������������������������������ ��?��_��+��?��(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(�����