<?php
header("Content-Type:text/html; charset=utf-8");
$runtime_start = microtime(true);
define('APP_PATH', __DIR__ . '/wifi/app/');
define('APP_HTML', __DIR__ . '/pub/HTML/');
define('HTMLDIR', __DIR__. '/pub/HTML/');
define('DOHTML','/dohtml');
/*404页面*/
function html404($ty='')
{
    header("http//1.1 404 not found");
    header("status: 404 not found");           
    $htm='<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
            <html><head>
            <title>404 Not Found</title>
            </head><body>
            <h1>404 Not Found</h1>
            <p>The requested URL was not found on this server.</p>
            <!-- '.$ty.' -->
            </body></html>';
    echo $htm;
    exit;
}
/*检查字符串和数组 先过滤单引号 双引号*/
function checkstr($str)
{
    if (str_replace("'","",$str)!=$str){html404();}
    /*过滤的自己可以新增*/
    $nosql='eval|select|$_|fputs|vars[|delete|upfile|/think|function=|invokefunction|call_user_func_array|s=|<|(|@|)|script|"';
    $arr=explode('|',$nosql);
    foreach ($arr as $ky)
    {
        if (str_replace($ky,'##',$str)!=$str){html404($ky);}
    }
}
function checksql($arr)
{
    foreach ($arr as $key => $row)
    {
        if (is_array($row)){checksql($row);}else{checkstr($row);}
    }
}

$url=$_SERVER['REQUEST_URI'];
$url=strtolower($url);
checkstr($url);
if ($url!=str_replace('.php','',$url)){html404('php');}
/*url长度不能超过40个字符*/
if (strlen($url)>90){html404(80);}

$post=$_POST;$get=$_GET;
if ($post){checksql($post);}
if ($get){checksql($get);}

$url=$_SERVER['REQUEST_URI'];
$url=strtolower($url);
if ($url!=str_replace('.php','',$url) || $url!=str_replace('.php','',$url))
{
    header('HTTP/1.1 301 Moved Permanently');
    header('Location:/');
    exit();
}
//images.li
$host=$_SERVER['HTTP_HOST'];
$host=$_SERVER['HTTP_HOST'];
if($host=='qifengjia.com')
{
    header("HTTP/1.1 301 Moved Permanently");
    header('Location: https://www.'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
    exit;
}

if($host=='www.qifengjia.com' || $host=='qifengjia.com')
{
    //$str=file_get_contents('index2.html');echo $str;
    //exit;
}

//echo "<!-- $host -->";
if(str_replace('qifengjia.com','',$host)==$host)
{
    header("HTTP/1.1 301 Moved Permanently");
    header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
    exit;
}
if(($host=='qifengjia.com'))
{
    header("HTTP/1.1 301 Moved Permanently");
    header('Location: https://www.'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
    exit;
}
if($_SERVER['SERVER_PORT']=="808" && ($host=='www.qifengjia.com' || $host=='qifengjia.com'))
{
    //header("HTTP/1.1 301 Moved Permanently");
    //header('Location: https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
    //exit;
}
if($_SERVER['SERVER_PORT']!="80" && ($host!='www.qifengjia.com' && $host!='qifengjia.com'))
{
    //header("HTTP/1.1 301 Moved Permanently");
    //header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
    //exit;
}

function is_crawler() { 
  $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']); 
  $spiders = array( 
        'SemrushBot','yisouspider','YYSpider','YisouSpider','Semrush','GPTBot','Scrapy','Curl','HttpClient','Amazonbot','AhrefsBot','dataforseo','semrushbot','yisouspider','yyspider','yisouspider','semrush','gptbot','scrapy','curl','httpclient','amazonbot','ahrefsBot','dataforseo','claudebot','awariobot'
  ); 
  foreach ($spiders as $spider) { 
    $spider = strtolower($spider); 
    if (strpos($userAgent, $spider) !== false) { 
      header('HTTP/1.1 403 Unauthorized');
      exit;
    } 
  } 
  return false; 
}

is_crawler();


//获取IP
function GetUIP()
{
    if(!empty($_SERVER["HTTP_CLIENT_IP"]))
    {
        $cip = $_SERVER["HTTP_CLIENT_IP"];
    }
    else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"]))
    {
        $cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
    }
    else if(!empty($_SERVER["REMOTE_ADDR"]))
    {
        $cip = $_SERVER["REMOTE_ADDR"];
    }
    else
    {
        $cip = '';
    }
    preg_match("/[\d\.]{7,15}/", $cip, $cips);
    $cip = isset($cips[0]) ? $cips[0] : 'unknown';
    unset($cips);
    return $cip;
}
$ip=GetUIP();
$iparr=['1.15.','42.192.','36.99.136.','106.54.','175.16.234.','121.237.36.','40.77.167.'];
$iparr=['1.15.','42.192','36.99.136','106.54.','101.43.','101.56.','150.158.','122.51.','175.24.','175.16.234.','121.237.36.','40.77.167.','106.60.48','106.60.48'];
foreach ($iparr as $k=>$v){
    if (str_replace($v,'',$ip)!=$ip){
            $wdata=[];
            if (isset($_GET)){
                $wdata['get']=$_GET;
            }
            if (isset($_POST)){
                $wdata['post']=$_POST;
            }
            $xmlData = file_get_contents('php://input');
            if ($xmlData){
                $wdata['input']=$xmlData;
            }
            $str=json_encode($wdata,JSON_UNESCAPED_UNICODE);
            file_put_contents('wifi/runtime/403/'.date('Y-m-d-H-i-s').".txt",$str);
            
          header('HTTP/1.1 403 Unauthorized');
          exit;
    }
}

require __DIR__ . '/wifi/core/init.php';
require __DIR__ . '/wifi/core/start.php';