<?

if ($set['cron_parser'] == 1) {
	$hfile = H.'files/cache/cron/newsParser.json';
	if (@filectime($hfile) < time() - 59) {
		@unlink($hfile);
	}
	if (!file_exists($hfile)) {
	include H.'core/includes/simple_html_dom.php';

	$RazdData = newsfrontConfig();

	$RandRazd = array_rand($RazdData, 1);
	$explode = explode('::', $RazdData[$RandRazd]);
	$razd = $explode[0];
	$url = $explode[2];

	$newsfrontlist = newsfrontlist($url);
	foreach($newsfrontlist AS $p) {
		
		$key = md5($p);
		
		$cc = $sql->rowCount("SELECT * FROM `news` where `key` = ? ",[$key]);
		if ($cc == 1) {
			continue;
		}
		$exp = newsfront($p);
		$news = explode('::', $exp);
		$text = $news[0];
		$img = $news[1];	
		$title = $news[2];	
		$TranslitLink = Translit($title , 1, 1, 1);
		$text = trim($text);
		$title = trim($title);
		$TranslitLink = trim($TranslitLink);
		$TranslitLink = str_replace('  ','', $TranslitLink);
		
		if (mb_strlen($img, 'UTF-8') > 10 AND mb_strlen($text, 'UTF-8') > 10 AND mb_strlen($title, 'UTF-8') > 5) {
			$time = time();
			$newimg = md5(md5($key));
			$tmpfile = H.'files/tmp/'. $newimg .'.png';
			
			downloadFile($img, $tmpfile);
			imgResize($tmpfile, H.'files/news-images/big/'. $newimg .'.jpg', 900);
			
			$imgs = new acResizeImage($tmpfile);
			$path = $imgs->
			cropSquare(25, 50, 1500)->
			resize(300, 500)->
			save(H.'files/news-images/mid/', $newimg, 'jpg', true, 75);		
			
			$imgs = new acResizeImage($tmpfile);
			$path = $imgs->
			cropSquare(25, 50, 1500)->
			resize(128, 300)->
			save(H.'files/news-images/large/', $newimg, 'jpg', true, 75);			
			
			$imgs = new acResizeImage($tmpfile);
			$path = $imgs->
			cropSquare(25, 50, 1500)->
			resize(50, 300)->
			save(H.'files/news-images/min/', $newimg, 'jpg', true, 75);			
			unlink($tmpfile);
			
			$text = trim($text);
			$title = trim($title);
			
			$sql->query("INSERT INTO `news` (`id_user`, `date_create`, `link`, `token`, `key`, `title`, `text`, `img`, `razd`) VALUES 	(?,?,?,?,?,?,?,?,?);",
				[
					2 ,
					$time ,
					$TranslitLink,
					md5($time.$time),
					$key,	
					$title,	
					$text,	
					$newimg,
					$razd,
				]
			);
		}
	}

		file_put_contents(@$hfile, json_encode(['time' => time()], true));
	}
}