

<?php
// ============ Configuration ============
$websites = [
   [
        'url' => 'https://indiamatka.org/main_index',
        'save_path' => '/home2/public_html/index.php',
        'label' => 'sattamatkano1'
    ],
    // [
    //     'url' => 'https://vipsattamatka.mobi/',
    //     'save_path' => '/home/sattamatkanew/public_html/vipsattamatka.html',
    //     'label' => 'vipsattamatka'
    // ],
    // [
    //     'url' => 'https://sattamatkanew.mobi/rss.php',
    //     'save_path' => '/home/sattamatkanew/public_html/result.html',
    //     'label' => 'rss',
    //     'minify' => true
    // ],
    
    //  [
    //     'url' => 'https://sattamatkanew.mobi/main_index.php',
    //     'save_path' => '/home/sattamatkanew/public_html/index.php',
    //     'label' => 'rss',
    //     'minify' => true
    // ],
    
    
];

// SSL Context for HTTPS requests
$contextOptions = [
    'ssl' => [
        'verify_peer' => false,
        'verify_peer_name' => false
    ]
];

// ============ Logger Function ============
function logStatus($label, $message) {
    $time = date('Y-m-d H:i:s');
    echo "[{$time}] [{$label}] {$message}\n";
}

// ============ Minify Function ============
function minifyHtmlCss($content) {
    return preg_replace([
        '/\s{2,}/',                           // Remove extra whitespace
        '/<!--.*?-->|\t|(?:\r?\n|\r)/s',      // Remove comments and newlines
        // '/\s*([{}|:;,])\s*/',                 // Remove spaces around specific characters
        '/;}/'                                // Remove last semicolon before closing brace
    ], [
        ' ', '', '$1', '}'
    ], $content);
}

// ============ Process Each Website ============
foreach ($websites as $site) {
    $url = $site['url'];
    $savePath = $site['save_path'];
    $label = $site['label'];
    $minify = $site['minify'] ?? false;

    logStatus($label, "Fetching data from: $url");

    $data = @file_get_contents($url, false, stream_context_create($contextOptions));

    if ($data === false) {
        logStatus($label, "❌ Failed to fetch content.  <br/>");
        continue;
    }

    // If enabled, minify the content
    if ($minify) {
        $data = minifyHtmlCss($data);
        logStatus($label, "✅ Content minified.  <br/>"  );
    }

    // Save to file
    if (file_put_contents($savePath, $data) !== false) {
        logStatus($label, "✅ File saved to: $savePath  <br/>" );
    } else {
        logStatus($label, "❌ Failed to write file to: $savePath  <br/>" ) ;
    }
}

?>