<?php 

//$getslinks			= new get_data;
//$gets_data			= $getslinks->scrape_web($url, false);

class get_data{	 

	public function scrape_web($pkgid) {	
		require_once get_template_directory().'/libs/addons/inc/ssl.php';
		
		global $opt_themes;
		@ini_set('user_agent', 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)');   
		@ini_set('user_agent', 'My-Application/2.5'); 
					
		$language		= 'en-US';  
		$pkgid 			= $_GET['pkgid']; 
		$gp				= "https://play.google.com/store/apps/details?id=".$pkgid."&hl=".$language;
		$gp_en_usa		= "https://play.google.com/store/apps/details?id=".$pkgid."&hl=en-US"; 
		$url 			= (empty($_SERVER['HTTPS']) ? 'http' : 'https').'://'.$_SERVER['HTTP_HOST'].'/gps/?pkgid='.$pkgid; 
		$url2 			= 'https://apks.exthem.es/gps/?pkgid='.$pkgid; 
		$url3 			= 'https://apks.exthem.es/?pkgid='.$pkgid; 
		$url4			= (empty($_SERVER['HTTPS']) ? 'http' : 'https').'//apks.exthem.es/?pkgid='.$pkgid; 

		$get_infos		= @get_linkz1($gp);  
		$g_data			= $this->geturl($url3);
		//$g_data		= @file_get_contents($url3);
		//$g_data2		= @file_get_contents($url);
		//$g_data3		= @file_get_contents_curl1($url2);
		 
		$apk_name		= $this->match('/<h1><span.*?itemprop="name">(.*?)<\/span><\/h1>/ms', $get_infos, 1);  
		$new_youtube2	= $this->match('/<video.*?poster="https:\/\/i.ytimg.com\/vi\/(.*?)\/hqdefault.jpg".*?>/', $get_infos, 1); 
		$appname		= $this->match('/<div class="appname">(.*?)<\/div>/ms', $g_data, 1);  
		$new_version	= $this->match('/<div class="version">(.*?)<\/div>/ms', $g_data, 1);  
		$new_size		= $this->match('/<div class="size">(.*?)<\/div>/ms', $g_data, 1);  
		$new_youtube	= $this->match('/<div class="youtube">(.*?)<\/div>/ms', $g_data, 1);  
		$new_desck_GP	= $this->match('/<div class="description">(.*?)<\/div>/ms', $g_data, 1);  
		$idgps			= $this->match('/<div class="pkgids">(.*?)<\/div>/ms', $g_data, 1);  
		$linkgps		= $this->match('/<div class="url_ps">(.*?)<\/div>/ms', $g_data, 1);  
		$developer		= $this->match('/<div class="developer">(.*?)<\/div>/ms', $g_data, 1);  
		$content		= $this->match('/<div class="bARER".*?>(.*?)<\/div>.*?<div class="TKjAsc">/ms', $get_infos, 1);
		$content2		= $this->match('/<div class="content">(.*?)<\/div>/ms', $g_data, 1);   
		$description	= $this->match('/<div class="description">(.*?)<\/div>/ms', $g_data, 1);  
		$whatsnew		= $this->match('/<div class="whatsnew">(.*?)<\/div>/ms', $g_data, 1);  
		$os				= $this->match('/<div class="os">(.*?)<\/div>/ms', $g_data, 1);  
		$update			= $this->match('/<div class="update">(.*?)<\/div>/ms', $g_data, 1);  
		$rate			= $this->match('/<div class="rate">(.*?)<\/div>/ms', $g_data, 1);  
		$vote			= $this->match('/<div class="vote">(.*?)<\/div>/ms', $g_data, 1);  
		$poster			= $this->match('/<div class="poster">(.*?)<\/div>/ms', $g_data, 1);  
		$background		= $this->match('/<div class="background">(.*?)<\/div>/ms', $g_data, 1);  
		$category		= $this->match('/<div class="category">(.*?)<\/div>/ms', $g_data, 1);  
		$developer		= $this->match('/<div class="developer">(.*?)<\/div>/ms', $g_data, 1);  
		$images_GP		= $this->match_all('/<img.*?src="(.*?)".*?>/ms', $this->match('/<div id="gallery">(.*?)<\/div>/ms', $g_data, 1), 1);
		$images_GP2		= $this->match_all('/<img.*?src=(.*?)width.*?>/ms', $this->match('/<div id="gallery">(.*?)<\/div>/ms', $g_data, 1), 1); 
		$tag			= $this->match('/<div class="tag">(.*?)<\/div>/ms', $g_data, 1);  
	
		$json			= array();
		$json['pkgid'] = $idgps;
		$json['linkgps'] = $linkgps;
		$json['name'] = $appname;
		$json['versi'] = $new_version;
		$json['size'] = $new_size;
		$json['developer'] = $developer;
		$json['content'] = $content;
		$json['description'] = $description;
		$json['whatsnew'] = $whatsnew;
		$json['os'] = $os;
		$json['category'] = $category; 
		$json['update'] = $update;
		$json['youtube'] = $new_youtube;
		$json['rate'] = $rate;
		$json['vote'] = $vote;
		$json['poster'] = $poster;
		$json['background'] = $background;
		$json['images_GP'] = $images_GP;
		$json['images_GP2'] = $images_GP2; 
		$json['tag'] = $appname.', '.$tag;
		 
        return $json;
		//echo json_encode($json);
		
	}
		
	private function geturl($url){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
		curl_setopt($ch, CURLOPT_HEADER, false); 
		$ip=rand(0,255).'.'.rand(0,255).'.'.rand(0,255).'.'.rand(0,255);
		//$ip=172.69.70.6;
		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 6.1; WOW64; rv:56.0) Gecko/20100101 Firefox/56.0");
		curl_setopt($ch, CURLOPT_REFERER, 'https://google.com');
		curl_setopt($ch, CURLOPT_AUTOREFERER, true);
		$html = curl_exec($ch);
		curl_close($ch);
		return $html;
	}
	
	private 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;
	}
	
	private function match_all($regex, $str, $i = 0){
		if(preg_match_all($regex, $str, $matches) === false)
			return false;
		else
			return $matches[$i];
	}
	
	private function match($regex, $str, $i = 0){
		if(preg_match($regex, $str, $match) == 1)
			return $match[$i];
		else
			return false;
	}
	

}