<?PHP

set_time_limit(380);

$this->callSqlClass(array('redirect_link'), $this);
$this->callClass(array('File'));


echo '<a href="?action=xml_to_database">xml to database</a><br>';
echo '<a href="?action=database_to_xml">database to xml</a>';


if($_GET['action'] == 'xml_to_database'){

	$this->File->GetAllFiles(_RootFolder, 'xml', '', 'both', false);
	foreach($this->File->Files as $v){

		$file = $v['file'];
		$content = file_get_contents($file);
		preg_match_all('/<loc>(.*?)<\/loc>/se', $content, $matches);
		foreach($matches[1] as $v2){

			$redirect_url = str_replace(_RootUrl.'/', '', $v2);
			$this->redirect_link->Select("WHERE `redirect_url` = '".addslashes($redirect_url)."'").'<br>';
			if($this->redirect_link->Numrows == 0){

				echo utf8_decode($redirect_url).'<br>';
			}
		}
	}
}
if($_GET['action'] == 'database_to_xml'){

	echo '<br><br>';
	$i = 1;

	$this->File->GetAllFiles(_RootFolder, 'xml', '', 'both', false);

	$this->redirect_link->Select("WHERE is_indexed = 1 AND is_deleted = 0 GROUP BY redirect_url ORDER BY id");
	while($row = $this->redirect_link->Assoc()){

		$link_exists = false;
		foreach($this->File->Files as $v){

			$file = $v['file'];
			$content = file_get_contents($file);
			$url = _RootUrl.'/'.$row['redirect_url'];
			$url = str_replace('/', '\/', $url);
			$url = str_replace('+', '\+', $url);
			$url = str_replace('-', '\-', $url);
			$url = str_replace('(', '\(', $url);
			$url = str_replace(')', '\)', $url);
			$url = str_replace('.', '\.', $url);
			$url = str_replace(':', '\:', $url);
			if(preg_match('/'.$url.'/i', $content)){

				$link_exists = true;
				break;
			}
		}
		if($link_exists == false){

			echo utf8_decode($row['redirect_url']).' <b>urlcheck:</b> '.$url.'<br>';
		}
		$i++;
	}
}


?>