<?php

use Stichoza\GoogleTranslate\GoogleTranslate;

class scrapes{
	public function scrape_web_info($sources)	{

		require_once get_template_directory() . '/libs/addons/inc/ssl.php';
		@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'); 
		global $opt_themes;

		if ($opt_themes['bahasa_on']) {

			$tr				= new GoogleTranslate();
			$bahasa			= $opt_themes['bahasa'];
		}

		$url			= $_POST['wp_url'];

		$arr			= array();

		//$links		= scrape_site($url);	
		//$links			= file_get_html($url);
		//$links		= file_get_contents($url, false, stream_context_create($ssl));
		//$links		= $this->geturl($url);	 

		$errorcode = http_response($url); 
 
		if($errorcode === 403) {
			$links	= scrapedo($url); 
		} elseif($errorcode === 200) {
			$links	= get_url($url);	
		}

		/*
		id info
		*/
		 
		$arr['id_psa']		= $this->match('/Package.*?<\/span>.*?<a.*?>(.*?)<\/a>/ms', $links, 1);
		$arr['id_ps']		= trim($arr['id_psa']);

		$arr['title_ids']	= $this->match('/Package.*?<\/span>.*?<a.*?>(.*?)<\/a>/ms', $links, 1);
		$arr['title_id']		= trim($arr['title_ids']);

		$arr['title_id_2a']	= $this->match('/Package.*?<\/span>.*?<a.*?>(.*?)<\/a>/ms', $links, 1);
		$arr['title_id_2']		= trim($arr['title_id_2a']);
		
		$arr['GP_ID']		= trim($arr['title_id']);
		$arr['GP_ID_2']		= trim($arr['title_id_2']);
		$arr['GP_IDX']		= trim($arr['title_id']);
		$arr['GP_IDX_2']	= trim($arr['title_id_2']);
		

		if ($arr['title_id'] === FALSE or $arr['title_id'] == '') $arr['title_id'] = $arr['title_id_2'];
		if ($arr['id_ps'] === FALSE or $arr['id_ps'] == '') $arr['id_ps'] = $arr['title_id_2'];
		if ($arr['GP_ID'] === FALSE or $arr['GP_ID'] == '') $arr['GP_ID'] = $arr['GP_ID_2'];
		if ($arr['GP_IDX'] === FALSE or $arr['GP_IDX'] == '') $arr['GP_IDX'] = $arr['GP_IDX_2'];

		if ($arr['title_id']) {
			$title_id			= $arr['title_id'];
		} else {
			$title_id			= $arr['title_id_2'];
		}
		$titleId			= $title_id;

		
		/*
		data info
		*/
		
		/*
		<div class="info-block scora">
                                    <span class="body-2 gray">
                                        App name.*?<span class="body-2">(.*?)<\/span>.*?<\/div>
		*/
		$arr['title']		= str_replace(array('mod apk', 'mod', 'apk', '-', 'for Android', '+', 'Download'), '', $this->match('/<div class="ml-3 text d-block">.*?<h1>(.*?)<\/h1>.*?<\/div>/ms', $links, 1));
		$arr['title']		= preg_replace('/\s+/', ' ', $arr['title']);
		$arr['title_sc']	= $arr['title'];
		if (!$arr['title']) $arr['title'] = $arr['title_sc'];

		if ($arr['title'] === FALSE or $arr['title'] == '') $arr['title'] = $arr['title_sc'];
		$arr['title']		= str_replace('[', '(', $arr['title']);
		$arr['title']		= str_replace(']', ')', $arr['title']);
		$arr['title']		= str_replace('(', '[', $arr['title']);
		$arr['title']		= str_replace(')', ']', $arr['title']);
		$arr['title']		= str_replace('download', '', $arr['title']);
		$arr['title_sc']	= str_replace('[', '(', $arr['title_sc']);
		$arr['title_sc']	= str_replace(']', ')', $arr['title_sc']);
		$arr['title_sc']	= str_replace('(', '[', $arr['title_sc']);
		$arr['title_sc']	= str_replace(')', ']', $arr['title_sc']);
		$arr['title_sc']	= str_replace('download', '', $arr['title_sc']);
		
		/*
		<div class="icon">.*?<img.*?src="(.*?)\".*?\/>.*?<div class="ml-3 text d-block">
					<div class="info-block scora">.*?<span class="body-2 gray">
		*/
		$arr['poster_sc']	= $this->match('/<div class="icon">.*?<img.*?src="(.*?)\".*?\/>.*?<div class="ml-3 text d-block">/ms', $links, 1);
		$arr['app_name']	= $this->match('/<div class="info-block scora">.*?<span class="body-2 gray">.*App name.*?<span class="body-2">(.*?)<\/span>.*?<\/div>/ms', $links, 1);
		$arr['app_name']	= trim($arr['app_name']);
		
		$arr['version_sc']	= $this->match('/<div class="info-block scora">.*?<span class="body-2 gray">.*?Version.*?<span class="body-2">(.*?)<\/span>.*?<\/div>/ms', $links, 1);
		$arr['version_sc']	= trim($arr['version_sc']);
		
		$arr['size_sc']		= $this->match('/<div class="info-block scora">.*?<span class="body-2 gray">.*?Size.*?<span class="body-2">(.*?)<\/span>.*?<\/div>/ms', $links, 1);
		$arr['size_sc']	= trim($arr['size_sc']); 
		
		$arr['genre_sc']	= $this->match('/<a.*?class="badge badge-pill badge-success ml-2 px-2">(.*?)<\/a>/ms', $links, 1);
		
		$arr['developer_sc']	= $this->match('/<div class="info-block scora">.*?Publisher.*?<span class="body-2">.*?<a.*?>(.*?)<\/a>/ms', $links, 1);
		
		$arr['updated']		= $this->match('/<meta property="article:published_time" content="(.*?)" \/>/ms', $links, 1);
		$arr['updated_sc'] 	= date("M d, Y", strtotime($arr['updated']));
		
		$arr['mods']		= $this->match('/<div class="info-block scora">.*?<span class="body-2 gray">.*?MOD info.*?<span class="body-2">(.*?)<\/span>.*?<\/div>/ms', $links, 1);
		$arr['mods']	= strip_tags(trim($arr['mods'])); 
		
		$arr['mods2']		= $this->match('/<title>.*?\((.*?)\).*?<\/title>/ms', $links, 1);
		if (!$arr['mods']) $arr['mods'] = $arr['mods2'];

		$arr['mods2']		= preg_replace('/\shref=[\'|"][^\'"]+[\'|"]/', '', $arr['mods2']);
		$arr['mods2']		= preg_replace('/\starget=[\'|"][^\'"]+[\'|"]/', '', $arr['mods2']);
		$arr['mods2']		= preg_replace('/\sclass=[\'|"][^\'"]+[\'|"]/', '', $arr['mods2']);
		$arr['mods2']		= preg_replace('/\sid=[\'|"][^\'"]+[\'|"]/', '', $arr['mods2']);
		$arr['mods2']		= preg_replace('/\sstyle=[\'|"][^\'"]+[\'|"]/', '', $arr['mods2']);
		$arr['full_mod_title']		= '';
 
		$arr['full_mod']	= trim($this->match('/<div id="tabs-1" class="tabs-container ui-tabs-panel ui-corner-bottom ui-widget-content".*?>.*?<div class="body-2 blockreadmore body-content readmore-section">(.*?)<\/div>.*?<\/div>/ms', $links, 1));
 
		$arr['full_mod']	= preg_replace('/<br>/is', '',  $arr['full_mod']);
		$arr['full_mod']	= preg_replace('/\shref=[\'|"][^\'"]+[\'|"]/', '', $arr['full_mod']);
		$arr['full_mod']	= preg_replace('/\starget=[\'|"][^\'"]+[\'|"]/', '', $arr['full_mod']);
		$arr['full_mod']	= preg_replace('/\sclass=[\'|"][^\'"]+[\'|"]/', '', $arr['full_mod']);
		$arr['full_mod']	= preg_replace('/\sid=[\'|"][^\'"]+[\'|"]/', '', $arr['full_mod']);
		$arr['full_mod']	= preg_replace('/\sstyle=[\'|"][^\'"]+[\'|"]/', '', $arr['full_mod']);
		$arr['full_mod']	= preg_replace('/<script.*?\/script>/', '', $arr['full_mod']);
		$arr['full_mod']	= preg_replace('/<[^\/>]*>([\s]?)*<\/[^>]*>/', '', $arr['full_mod']); //hilangkan <p> kosong
		$arr['full_mod']	= preg_replace('/<p[^>]*><\\/p[^>]*>/', '', $arr['full_mod']); //hilangkan <p> kosong
 
		/*
		<div id="tabs-description" class="tabs-container ui-tabs-panel ui-corner-bottom ui-widget-content".*?>.*?<div class="body-2 blockreadmore body-content readmore-section">(.*?)<\/div>.*?<\/div>

		*/
		
		$arr['article_sc']	= $this->match('/<div id="tabs-description" class="tabs-container ui-tabs-panel ui-corner-bottom ui-widget-content".*?>.*?<div class="body-2 blockreadmore body-content readmore-section">(.*?)<\/div>.*?<\/div>/ms', $links, 1);
		$arr['article_sc']	= preg_replace('/<figure.*?figure>.*?<p>/is', '<p>', $arr['article_sc']);
		$arr['article_sc']	= preg_replace('/<div class="ads123".*?<\/div>.*?<p>/is', '<p>', $arr['article_sc']);
		$arr['article_sc']	= preg_replace('/<div><p class="txt-ads".*?<\/p><\/div>/is', '', $arr['article_sc']);
		$arr['article_sc']	= preg_replace('/<\/p><\/div>.*?<\/div>/is', '</p>', $arr['article_sc']);
		$arr['article_sc']	= preg_replace('/<div id="contentMakeToc" class="wrapcontent"><p>/is', '<p>',  $arr['article_sc']);
		$arr['article_sc']	= preg_replace('/<img.*?>/is', '', $arr['article_sc']);
		$arr['article_sc']	= preg_replace('/\s+\s+/', ' ', $arr['article_sc']);
		$arr['article_sc']	= preg_replace('/\shref=[\'|"][^\'"]+[\'|"]/', '', $arr['article_sc']);
		$arr['article_sc']	= preg_replace('/\starget=[\'|"][^\'"]+[\'|"]/', '', $arr['article_sc']);
		$arr['article_sc']	= preg_replace('/\sclass=[\'|"][^\'"]+[\'|"]/', '', $arr['article_sc']);
		$arr['article_sc']	= preg_replace('/\sid=[\'|"][^\'"]+[\'|"]/', '', $arr['article_sc']);
		$arr['article_sc']	= preg_replace('/\sstyle=[\'|"][^\'"]+[\'|"]/', '', $arr['article_sc']);
		$arr['article_sc']	= preg_replace('/<script.*?\/script>/', '', $arr['article_sc']);
		$arr['article_sc']	= preg_replace('/<[^\/>]*>([\s]?)*<\/[^>]*>/', '', $arr['article_sc']); //hilangkan <p> kosong
		$arr['article_sc']	= preg_replace('/<p[^>]*><\\/p[^>]*>/', '', $arr['article_sc']); //hilangkan <p> kosong
		$arr['article_sc']	= preg_replace('/<!--.*?-->/', '', $arr['article_sc']);
		$arr['article_sc']	= preg_replace('/\s+/', ' ', $arr['article_sc']);
		$arr['article_sc']	= trim($arr['article_sc']);

		
		$arr['mods2']		= str_replace(array('<div>', '</div>', '<center>', '</center>', '<span>', '</span>'), '', $arr['mods2']);
		$arr['mods2']		= str_replace('<strong>', '<b>', $arr['mods2']);
		$arr['mods2']		= str_replace('</strong>', '</b>', $arr['mods2']);

		$arr['full_mod']	= str_replace(array('<div>', '</div>', '<center>', '</center>', '<span>', '</span>'), '', $arr['full_mod']);
		$arr['full_mod']	= str_replace('<strong>', '<b>', $arr['full_mod']);
		$arr['full_mod']	= str_replace('</strong>', '</b>', $arr['full_mod']);

		$arr['article_sc']	= str_replace(array('<div>', '</div>', '<center>', '</center>', '<span>', '</span>'), '', $arr['article_sc']);
		$arr['article_sc']	= str_replace('<strong>', '<b>', $arr['article_sc']);
		$arr['article_sc']	= str_replace('</strong>', '</b>', $arr['article_sc']);
		$arr['article_content']	= $arr['article_sc'];
		$arr['article_content']	= $arr['article_sc'];
		$arr['summary_sc']	= wp_strip_all_tags(mb_substr($arr['article_content'], 0, 100, 'utf8'));


		$arr['download_links_page'] = $this->match('/<a class="btn w-100 p-3 green download-button" href="#" data-href="(.*?)".*?>/ms', $links, 1);
		$arr['download_links_page']	= base64_decode($arr['download_links_page']);

		$download_links_pages_html	= $arr['download_links_page'];
		

		//$download_links_pages		= file_get_contents($download_links_pages_html, false, stream_context_create($ssl)); 
		
		if($errorcode === 403) {
			$download_links_pages	= scrapedo($download_links_pages_html); 
		} elseif($errorcode === 200) {
			$download_links_pages	= get_url($download_links_pages_html);	
		}

		//$download_links_pages		= file_get_contents_curl($download_links_pages_html);
		
		/* 
		$arr['mods_alt_title']		= $this->match('/<h3>.*?<span.*?>(.*?)<\/span>.*?<\/h3>/ms', $download_links_pages, 1);
		$arr['mods_alt_desc']		= $this->match('/<div class="wrapcontent">.*?<h3>.*?<\/h3>(.*?)<\/div>/ms', $download_links_pages, 1);
		*/

		/*
		<a class="variant d-flex align-items-center justify-content-between text-decoration-none p-2 mb-3 rounded" href="(.*?)" >
		*/
 
		/*
		<a class="variant d-flex align-items-center justify-content-between text-decoration-none p-2 mb-3 rounded" href="https://9mod.com/download/frostborn-action-rpg-147475/1" >
		*/
		
		$arr['download_linkz']	= $this->match_all('/<a class="variant\ d-flex.*?" href="(.*?)\".?/ms', $this->match('/<div id="versions-accordion">(.*?)<div class="help-block">/ms', $download_links_pages, 1), 1);
		
		$arr['download_namez']	= $this->match_all('/<span class="font-weight-bold">(.*?)<\/span>/ms', $this->match('/<div id="versions-accordion">(.*?)<div class="help-block">/ms', $download_links_pages, 1), 1);
		
		$arr['download_sizez']	= $this->match_all('/<div class="foot small">.*?<span class="badge badge-pill badge-warning px-2 text-uppercase">(.*?)<\/span>.*?<span.*?/ms', $this->match('/<div id="versions-accordion">(.*?)<div class="help-block">/ms', $download_links_pages, 1), 1);
		 
		 
		$arr['downloadlinks_gma']	= $this->match_all('/<a class="variant\ d-flex.*?" href="(.*?)\".?/ms', $this->match('/<div id="versions-accordion">(.*?)<div class="help-block">/ms', $download_links_pages, 1), 1);
		
		echo '<pre>';
		print_r($arr['downloadlinks_gma']); 
		echo '</pre>';
		

		$arr['name_downloadlinks_gma']	= $this->match_all('/<span class="font-weight-bold">(.*?)<\/span>/ms', $this->match('/<div id="versions-accordion">(.*?)<div class="help-block">/ms', $download_links_pages, 1), 1);
		
		$arr['size_downloadlinks_gma']	= $this->match_all('/<div class="foot small">.*?<span class="badge badge-pill badge-warning px-2 text-uppercase">(.*?)<\/span>.*?<span.*?/ms', $this->match('/<div id="versions-accordion">(.*?)<div class="help-block">/ms', $download_links_pages, 1), 1);
		
		
		$count							= count($arr['downloadlinks_gma']);
		for ($i = 0; $i < $count; $i++) {
			$get_link_[$i]					= $arr['downloadlinks_gma'][$i];
			
			if($errorcode === 403) {
				$apk_link_[$i]	= scrapedo($get_link_[$i]); 
			} elseif($errorcode === 200) {
				$apk_link_[$i]	= get_url($get_link_[$i]);	
			}			
			
			//$apk_link_[$i]					= scrape_site($get_link_[$i]);
			//$apk_link_[$i]					= file_get_contents($get_link_[$i], false, stream_context_create($ssl));
			$arr['downloadlink_gma_'[$i]]	= $this->match('/<div id="download-section".*?>(.*?)<p><script/ms', $apk_link_[$i], 1);
			//$arr['type_downloadlink_gma_'[$i]]	= $this->match('/<script>.*?href = ".*?\.Com.(.*?)".*?<\/script>/ms', $apk_link_[$i], 1);
			$arr['link_apk']				= $arr['downloadlink_gma_'[$i]];
			
			
		echo '<pre>';
		print_r($arr['link_apk']); 
		echo '</pre>';

		}

		if ($opt_themes['bahasa_on']) {
			if (!empty($arr['mods'])) {
				$arr['mods']			= $tr->setSource()->setTarget($bahasa)->translate($arr['mods']);
				$arr['mods']			= str_replace('< ', '<', $arr['mods']);
				$arr['mods']			= str_replace('< /', '</', $arr['mods']);
				$arr['mods']			= str_replace('</ ', '</', $arr['mods']);
				$arr['mods']			= str_replace(' >', '>', $arr['mods']);
			}

			if (!empty($arr['full_mod'])) {
				$arr['full_mod']		= $tr->setSource()->setTarget($bahasa)->translate($arr['full_mod']);
				$arr['full_mod']		= str_replace('< ', '<', $arr['full_mod']);
				$arr['full_mod']		= str_replace('< /', '</', $arr['full_mod']);
				$arr['full_mod']		= str_replace('</ ', '</', $arr['full_mod']);
				$arr['full_mod']		= str_replace(' >', '>', $arr['full_mod']);
			}

			if (!empty($arr['article_content'])) {
				$arr['article_content']	= $tr->setSource()->setTarget($bahasa)->translate($arr['article_content']);
				$arr['article_content']	= str_replace('< ', '<', $arr['article_content']);
				$arr['article_content']	= str_replace('< /', '</', $arr['article_content']);
				$arr['article_content']	= str_replace('</ ', '</', $arr['article_content']);
				$arr['article_content']	= str_replace(' >', '>', $arr['article_content']);
			}
			$arr['summary_sc']		= $tr->setSource()->setTarget($bahasa)->translate($arr['summary_sc']);
		}


		if (empty($arr['app_name'])) {
			$arr['error']			= NO_ID;
			echo $arr['error'];
		}

		if (!empty($titleId)) {
			require_once get_template_directory().'/libs/addons/includes/playstore.php';
		}

		return $arr;
	}


	private function geturl($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);
		//$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 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)");
		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;
	}
}
