<?php
/**
 * URL 301 Redirect Checker
 * 
 * This script reads a CSV file containing URLs and checks if each URL returns a 301 redirect.
 * It creates a new CSV file with the results including the original URL and its redirect status.
 */

// Configuration
$inputFile = 'preprod_initial_urls.csv';
$outputFile = 'url_301_check_results.csv';
$baseUrl = 'https://preprod.autogedal.ro'; // Base URL with HTTP authentication
$username = 'agd'; // Replace with your HTTP Basic Auth username
$password = 'agd'; // Replace with your HTTP Basic Auth password
$concurrentRequests = 10; // Number of concurrent requests
$timeout = 10; // Connection timeout in seconds

// Initialize output file with headers
$outputHandle = fopen($outputFile, 'w');
fputcsv($outputHandle, ['URL', 'Status Code', 'Is 301', 'Redirect Location']);

// Function to check if a URL returns a 301 redirect
function checkUrl($url) {
    global $timeout;
    
    $ch = curl_init();
    global $username, $password;
    
    curl_setopt_array($ch, [
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FOLLOWLOCATION => false, // Don't follow redirects
        CURLOPT_TIMEOUT => $timeout,
        CURLOPT_CONNECTTIMEOUT => $timeout,
        CURLOPT_NOBODY => true, // HEAD request only
        CURLOPT_HEADER => true,
        CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
        CURLOPT_USERPWD => "$username:$password" // HTTP Basic Authentication
    ]);
    
    $response = curl_exec($ch);
    
    if (curl_errno($ch)) {
        $result = [
            'url' => $url,
            'status_code' => 0,
            'is_301' => false,
            'redirect_location' => 'Error: ' . curl_error($ch)
        ];
    } else {
        $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $redirectUrl = '';
        
        // Extract Location header for redirects
        if ($statusCode >= 300 && $statusCode < 400) {
            preg_match('/Location:(.*?)\n/i', $response, $matches);
            $redirectUrl = isset($matches[1]) ? trim($matches[1]) : '';
        }
        
        $result = [
            'url' => $url,
            'status_code' => $statusCode,
            'is_301' => ($statusCode == 301),
            'redirect_location' => $redirectUrl
        ];
    }
    
    curl_close($ch);
    return $result;
}

// Read the CSV file
if (($handle = fopen($inputFile, 'r')) !== false) {
    $urls = [];
    $header = true;
    
    while (($data = fgetcsv($handle)) !== false) {
        // Skip header row if exists
        if ($header) {
            $header = false;
            // If the file doesn't have a header, rewind back to the first line
            if (isset($data[0]) && strpos($data[0], 'initial_path') === false) {
                fseek($handle, 0);
            }
            continue;
        }
        
        if (!empty($data[0])) {
            // Prepare full URL (add base URL if path is relative)
            $path = trim($data[0]);
            if (strpos($path, 'http') !== 0) {
                // Remove leading slash if exists to avoid double slashes
                $path = ltrim($path, '/');
                $urls[] = rtrim($baseUrl, '/') . '/' . $path;
            } else {
                $urls[] = $path;
            }
        }
    }
    fclose($handle);
    
    echo "Found " . count($urls) . " URLs to check.\n";
    
    // Process URLs in batches for better performance
    $totalUrls = count($urls);
    $processed = 0;
    
    for ($i = 0; $i < $totalUrls; $i += $concurrentRequests) {
        $batch = array_slice($urls, $i, $concurrentRequests);
        $batchResults = array_map('checkUrl', $batch);
        
        foreach ($batchResults as $result) {
            fputcsv($outputHandle, [
                $result['url'],
                $result['status_code'],
                $result['is_301'] ? 'Yes' : 'No',
                $result['redirect_location']
            ]);
        }
        
        $processed += count($batch);
        echo "Processed $processed of $totalUrls URLs (" . round(($processed / $totalUrls) * 100, 2) . "%)...\n";
        
        // Free up memory
        unset($batch);
        unset($batchResults);
    }
    
    echo "URL check completed. Results saved to $outputFile\n";
} else {
    echo "Failed to open input file: $inputFile\n";
}

fclose($outputHandle);
