<?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;
		$Go_Curl		= new Go_Curl();	

		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		= $Go_Curl->geturl($url);	 

		$status 		= http_response($url); 
		 
		if($status === 403) {
		$links		= scrapedo($url);	
		} else {
		$links		= file_get_html($url);	
		}

		/*
		id info
		*/
		$arr['id_ps']		= $Go_Curl->match('/Google Play Link.*?<a href=".*?\?id=(.*?)\&.*?".*?<span class="ch-play">/ms', $links, 1);

		$arr['title_id']	= $Go_Curl->match('/Google Play Link.*?<a href=".*?\?id=(.*?)\&.*?".*?<span class="ch-play">/ms', $links, 1);

		$arr['title_id_2']	= $Go_Curl->match('/Google Play Link.*?<a href=".*?\?id=(.*?)".*?<span class="ch-play">/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['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
		*/
		$arr['title']		= str_replace(array('mod apk', 'mod', 'apk', '-', 'for Android', '+', 'Download'), '', $Go_Curl->match('/<h1 class="page-title">(.*?)<\/h1>/ms', $links, 1));
		$arr['title']		= preg_replace('/\s+/', ' ', $arr['title']);
		$arr['title_sc']	= trim(strip_tags($Go_Curl->match('/<figure class="thumb">.*?<img.*?alt="Icon(.*?)">.*?<\/figure>/ms', $links, 1)));
		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']);

		$arr['poster_sc']	= $Go_Curl->match('/<figure class="thumb">.*?<img src="(.*?)".*?>.*?<\/figure>/ms', $links, 1);
		$arr['app_name']	= $Go_Curl->match('/<th>Name<\/th>.*?<td>(.*?)<\/td>/ms', $links, 1);
		$arr['version_sc']	= $Go_Curl->match('/<th>Last version<\/th>.*?<td>(.*?)<\/td>/ms', $links, 1);
		$arr['size_sc']		= $Go_Curl->match('/<tr>.*?<th>Size<\/th>.*?<td>(.*?)<\/td>.*?<\/tr>/ms', $links, 1);
		$arr['genre_sc']	= $Go_Curl->match('/<th>Category<\/th>.*?<td><a.*?>(.*?)<\/a><\/td>/ms', $links, 1);
		$arr['developer_sc']	= $Go_Curl->match('/<th>Developer<\/th>.*?<td><a.*?>(.*?)<\/a><\/td>/ms', $links, 1);
		$arr['updated']		= $Go_Curl->match('/<th>Updated<\/th>.*?<td><time.*?>(.*?)<\/time><\/td>/ms', $links, 1);

		$arr['updated_sc'] 	= date("M d, Y", strtotime($arr['updated']));
		$arr['mods']		= str_replace(array('modcombo.com', 'modcombo', 'mod apk', 'mod', 'apk', '-', 'for Android', '+', 'Download'), '', $Go_Curl->match('/<th>MOD<\/th>.*?<td>(.*?)<\/td>/ms', $links, 1));
		$arr['mods2']		= $Go_Curl->match('/<h1 class="page-title">.*?\((.*?)\).*?<\/h1>/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(strip_tags($Go_Curl->match('/<div class="entry-mod mb-20">.*?<div class="accordion.*?">.*?<div class="abody wrapcontent">(.*?)<\/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

		$arr['article_sc']	= $Go_Curl->match('/<div.*?id="content-desc">(.*?)<div class="entry-download text-center mb-20" id="content-download">/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'] = $Go_Curl->match('/<div.*?id="btn-download-1">.*?<a href="(.*?)".*?<\/a>/ms', $links, 1);
		$download_links_pages_html	= $arr['download_links_page'];
		//$download_links_pages		= file_get_contents($download_links_pages_html, false, stream_context_create($ssl)); 
		$download_links_pages		= scrape_site($download_links_pages_html);
		$arr['mods_alt_title']		= $Go_Curl->match('/<h3>.*?<span.*?>(.*?)<\/span>.*?<\/h3>/ms', $download_links_pages, 1);
		$arr['mods_alt_desc']		= $Go_Curl->match('/<div class="wrapcontent">.*?<h3>.*?<\/h3>(.*?)<\/div>/ms', $download_links_pages, 1);

		$arr['downloadlinks_gma']	= $Go_Curl->match_all('/<div class="item item-apk">.*?<a href="(.*?)".*?<\/a>/ms', $Go_Curl->match('/<article.*?<div class="links">(.*?)<\/article>/ms', $download_links_pages, 1), 1);

		//$arr['downloadlinks_gma_alt']	= $Go_Curl->match_all('/<a href="(.*?)".*?<\/a>/ms', $Go_Curl->match('/<div id="bh-original" class="sbhTitle mb-20">(.*?)<div class="ads12 box-space">/ms', $download_links_pages, 1), 1);
		//if (!$arr['downloadlinks_gma']) $arr['downloadlinks_gma'] = $arr['downloadlinks_gma_alt'];

		$arr['name_downloadlinks_gma']	= $Go_Curl->match_all('/<a.*?title="(.*?)\[.*?".*?<\/a>/ms', $Go_Curl->match('/<article.*?<div class="links">(.*?)<\/article>/ms', $download_links_pages, 1), 1);
		
		//$arr['name_downloadlinks_gma_alt']	= $Go_Curl->match_all('/<a.*?title="(.*?)\[.*?".*?<\/a>/ms', $Go_Curl->match('/<article.*?<div class="sbhTitle mb-20">.*?<div class="links">(.*?)<\/article>/ms', $download_links_pages, 1), 1);
		//if (!$arr['name_downloadlinks_gma']) $arr['name_downloadlinks_gma'] = $arr['name_downloadlinks_gma_alt'];

		$arr['size_downloadlinks_gma']	= $Go_Curl->match_all('/<a.*?title=".*?\[(.*?)\].*?">/ms', $Go_Curl->match('/<div class="links">(.*?)<\/article>/ms', $download_links_pages, 1), 1);
		
		//$arr['size_downloadlinks_gma_alt']	= $Go_Curl->match_all('/<a.*?title=".*?\[(.*?)\].*?">/ms', $Go_Curl->match('/<div id="bh-original" class="sbhTitle mb-20">(.*?)<div class="ads12 box-space">/ms', $download_links_pages, 1), 1);
		//if (!$arr['size_downloadlinks_gma']) $arr['size_downloadlinks_gma'] = $arr['size_downloadlinks_gma_alt'];

		$count							= count($arr['downloadlinks_gma']);
		for ($i = 0; $i < $count; $i++) {
			$get_link_[$i]		= $arr['downloadlinks_gma'][$i];
			$get_linkzz			= $arr['downloadlinks_gma'][$i];
			$apk_link_[$i]		= file_get_contents($get_linkzz);
			
			$arr['apklink'][$i]	= $Go_Curl->match('/<\/main>.*?<\/script><script>.*?var counter =.*?href = "(.*?)".*?, 1000.*?<\/script>/ms', $apk_link_[$i], 1);
			
			$arr['type_downloadlink_gma_'[$i]]	= $Go_Curl->match('/<script>.*?href = ".*?\.Com.(.*?)".*?<\/script>/ms', $apk_link_[$i], 1);
			
			$arr['link_apk']	= $arr['apklink'][$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;
	}

 
}
