<?php
ini_set('display_errors', 0);

//main db
$db = new SQLite3('.db.db');

//dns
$rows3 = $db->query("SELECT * FROM dns");
$rows = array();
while ($row = $rows3->fetchArray(SQLITE3_ASSOC)) {
	$rows[] = $row['url'];
}

function getresult($url,$port){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_PORT, $port);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_2; en-US) AppleWebKit/532.8 (KHTML, like Gecko) Chrome/4.0.302.2 Safari/532.8');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , 10);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
    $html = curl_exec($ch);
    curl_close($ch);
    return $html;
}



foreach($rows as $portal){
    # $url = $portal."/live".http_build_query($_POST).str_replace("%40","@",$_SERVER['QUERY_STRING']);
    $url = $portal."/series/".$_GET["path"];
     file_put_contents("filess.txt",$url);
    parse_str($_SERVER['QUERY_STRING'], $result_array);
    $_SERVER['QUERY_STRING'] = http_build_query($result_array);
    // if( isset($_SERVER['HTTPS'] ) )
    $portalseg = explode(":",$portal);
    $res = getresult($url,isset($portalseg[2])?$portalseg[2]:'80');
    $res = trim($res);
    if($res === false || $res == '{"user_info":{"auth":0}}' || $res == header('404 (Not Found)') || stripos($res,"HTTP/1.1 401 Unauthorized") || $res=='{"success": true}') {
        // SO for http://system52.xyz/ it fails when you get {"success": true}
        continue;
    }
    else{
        header("HTTP/1.1 301 Moved Permanently");
        header("Location: ".$url."",TRUE,301);
        die();
    }
}
