<?php
// member/api/test-scan.php - Test website scanning
require_once '../../config.php';
requireLogin();

header('Content-Type: application/json');

$url = 'https://utalii.ac.ke';

// Test if we can reach the website
$ch = curl_init();
curl_setopt_array($ch, [
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_TIMEOUT => 15,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_SSL_VERIFYHOST => false,
    CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
    CURLOPT_HEADER => true,
]);

$response = curl_exec($ch);
$error = curl_error($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$effectiveUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
curl_close($ch);

echo json_encode([
    'success' => empty($error),
    'url_tested' => $url,
    'effective_url' => $effectiveUrl,
    'http_code' => $httpCode,
    'curl_error' => $error ?: 'None',
    'response_length' => strlen($response),
    'response_preview' => substr(strip_tags($response), 0, 500)
]);