<?php

class Scraper 
{
    public function scrape($url) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, false);
        $ip = rand(0, 255) . '.' . rand(0, 255) . '.' . rand(0, 255) . '.' . rand(0, 255);
        curl_setopt($curl, CURLOPT_HTTPHEADER, ["REMOTE_ADDR: $ip", "HTTP_X_FORWARDED_FOR: $ip"]);
        curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36");
        curl_setopt($curl, CURLOPT_REFERER, "http://www.google.com");
        curl_setopt($curl, CURLOPT_AUTOREFERER, true);
        $data = curl_exec($curl);
        curl_close($curl);
        return $data;
    }

	public function scrape_site($url, $ua = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36', $referer = 'http://www.google.com/') {
	  if (function_exists('curl_exec')) {
		$header[0] 	= "Accept-Language: en-us,en;q=0.5";
		$agent 		= 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36';
		$agent2		= 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36';
		$agent3		= 'facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)';
		$agent4		= 'Mozilla/5.0 (iPad; CPU OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148';
		$agent5		= 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.83 Safari/537.36';
		$agent6		= 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36';
		$referer 	= 'http://www.google.com/';
		$ip 		= rand(0,255).'.'.rand(0,255).'.'.rand(0,255).'.'.rand(0,255); 
		$ips 		= '51.68.87.157:49320';
			
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');   
		curl_setopt($ch, CURLOPT_USERAGENT, $agent);
		curl_setopt($ch, CURLOPT_REFERER, $referer);
		curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
		//curl_setopt($ch, CURLOPT_TIMEOUT, 10);	
		curl_setopt($ch, CURLOPT_HEADER, false); 
		curl_setopt($ch, CURLOPT_HTTPHEADER, ["REMOTE_ADDR: $ip", "HTTP_X_FORWARDED_FOR: $ip"]);
		
		$html = curl_exec($ch);
		curl_close($ch);
	  } else { 
		//$html = file_get_contents($url);
		$html = @file_get_contents($url);
	  }
	  return $html;
	}
	
	public function file_get_contents_curl($url, $retries=5){
		$ua = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.82 Safari/537.36';

		if (extension_loaded('curl') === true) {
			$ch = curl_init();

			curl_setopt($ch, CURLOPT_URL, $url); // The URL to fetch. This can also be set when initializing a session with curl_init().
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
			curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); // The number of seconds to wait while trying to connect.
			curl_setopt($ch, CURLOPT_USERAGENT, $ua); // The contents of the "User-Agent: " header to be used in a HTTP request.
			curl_setopt($ch, CURLOPT_FAILONERROR, TRUE); // To fail silently if the HTTP code returned is greater than or equal to 400.
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); // To follow any "Location: " header that the server sends as part of the HTTP header.
			curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE); // To automatically set the Referer: field in requests where it follows a Location: redirect.
			curl_setopt($ch, CURLOPT_TIMEOUT, 10); // The maximum number of seconds to allow cURL functions to execute.
			curl_setopt($ch, CURLOPT_MAXREDIRS, 5); // The maximum number of redirects

			$result = curl_exec($ch);

			curl_close($ch);
		} else {
			$result = @file_get_contents($url);
		} if (empty($result) === true) {
			$result = false;

			if ($retries >= 1) {
				sleep(1);
				return file_get_contents_curl($url, --$retries);
			}
		}    

		return $result;
	}
	
	public function get_linkz($url){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
		$ip = rand(0,255).'.'.rand(0,255).'.'.rand(0,255).'.'.rand(0,255); 
		curl_setopt($ch, CURLOPT_HTTPHEADER, array("REMOTE_ADDR: $ip", "HTTP_X_FORWARDED_FOR: $ip"));
		//curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/".rand(3,5).".".rand(0,3)." (Windows NT ".rand(3,5).".".rand(0,2)."; rv:2.0.1) Gecko/20100101 Firefox/".rand(3,5).".0.1");
		curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36");
		//curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0");
		curl_setopt($ch, CURLOPT_REFERER, "http://www.google.com");
		curl_setopt($ch, CURLOPT_AUTOREFERER, true);
		$html = curl_exec($ch);
		curl_close($ch);
		return $html;
	}
	
    public function match_all_key_value($regex, $str, $keyIndex = 1, $valueIndex = 2) {
        $arr = array();
        preg_match_all($regex, $str, $matches, PREG_SET_ORDER);
        foreach ($matches as $m) {
            $arr[$m[$keyIndex]] = $m[$valueIndex];
        }
        return $arr;
    }

    public function match_all($regex, $str, $index = 0) {
        if (preg_match_all($regex, $str, $matches) === false) {
            return false;
        } else {
            return $matches[$index];
        }
    }

    public function match($regex, $str, $index = 0) {
        if (preg_match($regex, $str, $match) == 1) {
            return $match[$index];
        } else {
            return false;
        }
    }
}

class APKT_Scraper
{
    public function scrape($url) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, false);
        $ip = rand(0, 255) . '.' . rand(0, 255) . '.' . rand(0, 255) . '.' . rand(0, 255);
        curl_setopt($curl, CURLOPT_HTTPHEADER, ["REMOTE_ADDR: $ip", "HTTP_X_FORWARDED_FOR: $ip"]);
        curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36");
        curl_setopt($curl, CURLOPT_REFERER, "http://www.google.com");
        curl_setopt($curl, CURLOPT_AUTOREFERER, true);
        $data = curl_exec($curl);
        curl_close($curl);
        return $data;
    }

    public function match_all_key_value($regex, $str, $keyIndex = 1, $valueIndex = 2) {
        $arr = array();
        preg_match_all($regex, $str, $matches, PREG_SET_ORDER);
        foreach ($matches as $m) {
            $arr[$m[$keyIndex]] = $m[$valueIndex];
        }
        return $arr;
    }

    public function match_all($regex, $str, $index = 0) {
        if (preg_match_all($regex, $str, $matches) === false) {
            return false;
        } else {
            return $matches[$index];
        }
    }

    public function match($regex, $str, $index = 0) {
        if (preg_match($regex, $str, $match) == 1) {
            return $match[$index];
        } else {
            return false;
        }
    }
}