<?php
use Stichoza\GoogleTranslate\GoogleTranslate;

class scrapes{	 
	
	private $Go_Curl;

    public function __construct()
    {
        $this->Go_Curl = new Go_Curl();
    }

	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)');

		 
		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		= get_url($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);	
		}
		
		// Asumsikan $html sudah berisi konten halaman dari Go_Curl

		// 1. Ekstrak isi <title> (full title)
		$raw_titles = $this->Go_Curl->match('/<title>(.*?)<\/title>/msi', $html, 1);

		if (empty($raw_titles)) {
			// fallback jika regex gagal
			$raw_titles = '';
		}

		// 2. Cleaning agresif tapi aman
		$raw_titles = str_replace(
			['download', 'Download', 'DOWNLOAD', 'Downloads', 'downloads', 
			 'apk', 'Apk', 'APK', 'Apks', 'APKS'],
			'',
			$raw_titles
		);

		$clean_titles = $raw_titles;

		// Hapus prefix "Download " (case insensitive)
		$clean_titles = preg_replace('/^download\s+/i', '', $clean_titles);

		// Hapus suffix " for Android" (case insensitive, termasuk variasi spasi)
		$clean_titles = preg_replace('/\s+for\s+Android$/i', '', $clean_titles);

		// Hapus kata-kata tidak perlu: download, apk, mod, dll.
		
		//$clean_titles = preg_replace('/\b(download|downloads|apk|apks|mod|hack|premium|pro|full|unlimited)\b\s*/i', ' ', $clean_titles);

		// Hapus semua isi dalam kurung beserta kurungnya ((), [], {}, <>)
		$clean_titles = preg_replace('/\s*[\(\[\{<].*?[\)\]\}>]\s*/i', ' ', $clean_titles);

		// Hapus versi angka (1.9.12, v1.0, 2.0.1, dll.)
		$clean_titles = preg_replace('/\b(v?[\d\.\_]+)\b/i', ' ', $clean_titles);

		// Hapus tanda hubung akhir atau garbage lain (misal: " - 5play.org")
		$clean_titles = preg_replace('/\s*[-–—].*$/', '', $clean_titles);

		// Bersihkan spasi berlebih dan trim
		$clean_titles = trim(preg_replace('/\s+/', ' ', $clean_titles));

		// Hasil akhir: $clean_titles siap dipakai untuk search Play Store ID
		 

		$CariJudul 			= $clean_titles; 		 
		
		$h1_raw2			= str_replace(array('gamedva.com', '&#8211;', '-', 'free', 'for Android', '+', 'Download'), '', trim(strip_tags($this->Go_Curl->match('/<h1 class="entry-title">(.*?)<\/h1>/ms', $links, 1))));
		
		
        // === Google Play Package ID ===
		$arr['id_ps']		= $this->Go_Curl->match('/<a.*?href=".*?\?id=(.*?)">Google Play<\/a>/ms', $html, 1);		
		$arr['id_ps']		= $this->Go_Curl->validate_gplay_id($arr['id_ps']);

		$arr['title_id']	= $arr['id_ps'];
		$arr['title_id_2']	= $arr['id_ps'];
 		 
		
        $gp_id 				= !empty($arr['id_ps']) ? $arr['id_ps'] : ($arr['title_id'] ?? $arr['title_id_2'] ?? '');		
		
		/* 
		$arr['title_id']	= $this->Go_Curl->match('/<a.*?href=".*?\?id=(.*?)\&.*?">Google Play<\/a>/ms', $links, 1);		
		 
		
		$arr['title_id_2']	= $this->Go_Curl->match('/<a.*?href=".*?\?id=(.*?)">Google Play<\/a>/ms', $links, 1); 
		
		$arr['GP_ID']		= $arr['title_id'];
		$arr['GP_ID_2']		= $arr['title_id_2'];
		$arr['GP_IDX']		= $arr['title_id'];
		$arr['GP_IDX_2']	= $arr['title_id_2'];
		
        if ($arr['title_id'] === FALSE or $arr['title_id'] == '') $arr['title_id'] = $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 ( !empty($arr['title_id']) ) { 
		$title_id			= $arr['title_id'];
		} else {
		$title_id			= $arr['title_id_2'];
		}				 
		$titleId			= $title_id;
		 */
		
		if ( !empty($gp_id) ) {			
			$titleId		= $gp_id; 
		} else {			  
			$titleId 		= $this->Go_Curl->get_playstore_id_by_title_google($h1_raw2);
		}
		 
		
		//$titleId 			= $gp_id; 
		
		$arr['GP_ID'] 		= $titleId; 
		
			
		if ( !empty($titleId) ) {
			require_once get_template_directory().'/libs/addons/includes/playstore.php';
		}
		
		$arr['title']		= str_replace(array('gamedva.com', '&#8211;', '-', 'free', 'for Android', '+', 'Download'), '', $this->Go_Curl->match('/<title>(.*?)<\/title>/ms', $links, 1));
		$arr['title']		= preg_replace('/\s+/', ' ', $arr['title']);

		$arr['title_sc']	= str_replace(array('gamedva.com', '&#8211;', '-', 'free', 'for Android', '+', 'Download'), '', trim(strip_tags($this->Go_Curl->match('/<h1 class="entry-title">(.*?)<\/h1>/ms', $links, 1))));
		
		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']);
		
		$arr['poster_sc']	= $this->Go_Curl->match('/<meta property="og:image" content="(.*?)" \/>/', $links, 1);
		
		$arr['app_name']	= wp_strip_all_tags($this->Go_Curl->match('/Name<\/td><td>(.*?)<\/td>/ms', $links, 1));
		$arr['app_name']	= str_replace('APK', '', $arr['app_name']);
		$arr['app_name']	= str_replace(array('APK', 'Apk', 'apk' ), '', $arr['app_name'] );
	
		$arr['version_sc']	= $this->Go_Curl->match('/Version<\/td><td>(.*?)<\/td>/ms', $links, 1);	
		$arr['version_sc']	= str_replace('v', '', $arr['version_sc']);	
		$arr['size_sc']		= $this->Go_Curl->match('/Size<\/td><td>(.*?)<\/td>/ms', $links, 1);	 
		$arr['genre_sc']	= $this->Go_Curl->match('/Category<\/td><td><a.*?>(.*?)<\/a><\/td>/ms', $links, 1);
		$arr['developer_sc']	= $this->Go_Curl->match('/Publisher<\/td><td>(.*?)<\/td>/ms', $links, 1);
 
		
		$arr['updates']		= $this->Go_Curl->match('/<meta property="article:modified_time" content="(.*?)"/ms', $links, 1);
		$arr['updated_sc'] 	= date("M d, Y", strtotime($arr['updates']));
		
		
        $arr['carimod']		= preg_match('/\[([^\]]*)\]/', $arr['title_sc'], $arr['hasilmod']);
		$arr['mods']		= $arr['hasilmod'][1];
        //$arr['mods_2']	= str_replace(array('getmodsapk.com', 'MOD ', 'Download'), '', $this->Go_Curl->match('/<span class="sm:text-left text-center block w-full">.*?\((.*?)\).*?<\/span>/ms', $links, 1));

        //if ($arr['mods_2'] === FALSE or $arr['mods_2'] == '') $arr['mods_2'] = $arr['mods'];	

		$arr['full_mod_title']		= ''; 
		$arr['full_mod']	= $this->Go_Curl->match('/<div id="mod-description".*?>(.*?)<\/div>/ms', $links, 1);	
		$arr['full_mod']	= str_replace('&nbsp;', '', $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']);
		$arr['full_mod']	= preg_replace('/<p[^>]*><\\/p[^>]*>/', '', $arr['full_mod']);
		$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']	= $this->Go_Curl->match('/<div class="ad-slot">.*?<\/h.*?>(.*?)<center><h2>How to Download/ms', $links, 1);
		
		$arr['article_sc']	= preg_replace('/<div id="mod-description" class="toggle-body">.*?<\/div>/', '', $arr['article_sc']);
		$arr['article_sc']	= preg_replace('/<span id="mod-title" class="toggle-title">.*?<\/span>/', '', $arr['article_sc']);
		$arr['article_sc']	= preg_replace('/<div class="toggle">.*?<\/div>/', '', $arr['article_sc']);
		$arr['article_sc']	= preg_replace('/<div id="toc_container".*?>.*?<\/div>/', '', $arr['article_sc']);
		$arr['article_sc']	= preg_replace('/<div class="wp-block-image">.*?<\/div>/', '', $arr['article_sc']);
		$arr['article_sc']	= preg_replace('/<img.*?>/', '', $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']);  
		$arr['article_sc']	= preg_replace('/<p[^>]*><\\/p[^>]*>/', '', $arr['article_sc']);  
		$arr['article_sc']	= preg_replace('/<!--.*?-->/', '', $arr['article_sc']); 
		$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['summary_sc']	= wp_strip_all_tags(mb_substr( $arr['article_content'], 0, 100, 'utf8' ));
		
		$arr['canonical']	= $this->Go_Curl->match('/<link rel="canonical" href="(.*?)" \/>/ms', $links, 1);  
		
		$arr['download_links_page']	= $arr['canonical'].'?download';  
		
		$download_links_pages_html	= $arr['download_links_page'];	
		  
		if ( !empty($arr['download_links_page']) ) { 
		
		if($errorcode === 403) {
			$download_info	= scrapedo($download_links_pages_html); 
		} elseif($errorcode === 302) {
			$download_info	= scrapedo($download_links_pages_html);	
		} elseif($errorcode === 200) {
			$download_info	= file_get_html($download_links_pages_html);	
		}
		//$download_info	= file_get_html($download_links_pages_html);	
		
		}
		/*
		<a class="app" href="https://gamedva.com/vegas-crime-simulator-mod?download&file=0" rel="nofollow" class="wp-block-button__link">
		*/
		
		$arr['downloadlinks_gma']	= $this->Go_Curl->match_all('/<p style="margin-top: 10px">(.*?)<\/p>/ms', $this->Go_Curl->match('/<main id="main" class="main-box main-box-single">(.*?)<div class="ad-slot">/ms', $download_info, 1), 1);  
		
		$arr['titlex2']		= $this->Go_Curl->match('/<title>(.*?)<\/title>/ms', $download_info, 1);
		
		echo '<pre>';
			print_r($arr['downloadlinks_gma']);
		echo '</pre>';
			
		$arr['name_downloadlinks_gma']	= $this->Go_Curl->match_all('/<div class="app-text">(.*?)\<span.*?<\/div>/ms', $this->Go_Curl->match('/<div class="wp-block-buttons download is-small-text">(.*?)\<span style="color:.*?/ms', $download_info, 1), 1);

		$arr['name_downloadlinks_gma']	= str_replace(array('<br>', '<span class="size">', '</span>', '·'), ' ',  $arr['name_downloadlinks_gma']);
		$arr['name_downloadlinks_gma']	= preg_replace('/\s+\s+/', ' ', $arr['name_downloadlinks_gma']);

		$arr['size_downloadlinks_gma']	= str_replace(array('APK ', 'OBB ', 'XAPK ', 'X', 'ZIP '), '', $this->Go_Curl->match_all('/<span class="size">(.*?)\ ·.*?/ms', $this->Go_Curl->match('/<div class="wp-block-buttons download is-small-text">(.*?)\<span style="color:.*?/ms', $download_info, 1), 1));
		 
		$arr['types_downloadlinks_gma']	= '';
				 
		$arr['types_downloadlinks_gma']	= preg_replace('/\d+/', '',  $arr['types_downloadlinks_gma']);  
		$arr['types_downloadlinks_gma']	= str_replace(array('MB', 'GB', 'gb', 'mb', '.'), '',  $arr['types_downloadlinks_gma']); 
		 
		$arr['notes_downloadlinks_gma']	= $this->Go_Curl->match_all('/<span class="size">.*?\·(.*?)<\/span>/ms', $this->Go_Curl->match('/<div class="wp-block-buttons download is-small-text">(.*?)\<span style="color.*?/ms', $download_info, 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]);
		$arr['downloadlink_gma_'[$i]]	= $this->Go_Curl->match('/<a id="download-now".*?href.*?\="(.*?)\".*?>/ms', $apk_link_[$i], 1);
		
		print_r($arr['downloadlink_gma_'[$i]]);
		
		}
		 
		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']); 

		}
		
		/* 
		https://gamedva.com/world-war-2?download
		https://gamedva.com/world-war-2/?download
		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();
		$randoms	= array("https://google.com", "https://google.com", "https://bing.com", "https://ask.com", "https://yahoo.com", "https://getmodsapk.com"); 
		$referall2	= $randoms[array_rand($randoms, 1)];
		$referall	= 'https://google.com';
		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 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		= trim(curl_exec($ch));
		curl_close($ch); 
		//print_r($url);
		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;
	}
}