<?php 
set_time_limit(0);
require_once 'inc/mysqli.php';
include 'inc/functions.php';

if($_GET['hash'] != 'jFOFsfh934NFnpwq321j'){
	exit('Доступ ограничен!');
}

if(isset($_GET['update_baze'])){
	
	$db->query("TRUNCATE `sitemap_url`");
	
	$db->query("INSERT INTO `sitemap_url` SET `url` = ?s, `priority` = ?s, `changefreq` = ?s, `position` = ?s, `type` = 'mix'", '/', '1.0', 'hourly', '110');
	$db->query("INSERT INTO `sitemap_url` SET `url` = ?s, `priority` = ?s, `changefreq` = ?s, `position` = ?s, `type` = 'mix'", '/song-list/zarubezhnaya-muzyka/', '1.0', 'hourly', '108');
	$db->query("INSERT INTO `sitemap_url` SET `url` = ?s, `priority` = ?s, `changefreq` = ?s, `position` = ?s, `type` = 'mix'", '/song-list/russkaya-muzyka/', '1.0', 'hourly', '108');
	$db->query("INSERT INTO `sitemap_url` SET `url` = ?s, `priority` = ?s, `changefreq` = ?s, `position` = ?s, `type` = 'mix'", '/artist-all/', '1.0', 'hourly', '109');
	
	$data_files = $db->getAll("SELECT `id`,`title`,`artist` FROM `files`");
	
	foreach($data_files as $url_file){
		$db->query("INSERT INTO `sitemap_url` SET `url` = ?s, `priority` = ?s, `changefreq` = ?s, `position` = ?s, `type` = 'file'",
		'/song/'.$url_file['id'].'/'.rus2eng(str2url($url_file['artist'].' '.$url_file['title'])).'/', '1.0', 'hourly', '50');
		
	}
	
	$data_artist = $db->getAll("SELECT `id`,`artist` FROM `files` WHERE `id` = `canonical_artist`");
	
	foreach($data_artist as $url_artist){
		$db->query("INSERT INTO `sitemap_url` SET `url` = ?s, `priority` = ?s, `changefreq` = ?s, `position` = ?s, `type` = 'artist'",
		'/artist/'.$url_artist['id'].'/'.rus2eng(str2url($url_artist['artist'])).'/', '1.0', 'hourly', '100');
	}
	
	$arr_en = array('a','b','c','b','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9');
	$arr_ru = array('aRU','bRU','vRU','gRU','dRU','eRU','zhRU','zRu','iRU','kRU','lRU','mRu','nRU','oRU','pRU','rRU','sRU','tRU','uRU','fRU','hRY','cRU','chRU','shRU','schRU','eeRU','yuRU','yaRU');

	for($i=0;$i<count($arr_en);$i++){
		$db->query("INSERT INTO `sitemap_url` SET `url` = ?s, `priority` = ?s, `changefreq` = ?s, `position` = ?s, `type` = 'mix'",
		'/singer-song/'.$arr_en[$i].'/', '1.0', 'hourly', '100');
		
		$db->query("INSERT INTO `sitemap_url` SET `url` = ?s, `priority` = ?s, `changefreq` = ?s, `position` = ?s, `type` = 'mix'",
		'/singer-artist/'.$arr_en[$i].'/', '1.0', 'hourly', '100');
	}
	
	for($q=0;$q<count($arr_ru);$q++){
		$db->query("INSERT INTO `sitemap_url` SET `url` = ?s, `priority` = ?s, `changefreq` = ?s, `position` = ?s, `type` = 'mix'",
		'/singer-song/'.$arr_ru[$q].'/', '1.0', 'hourly', '100');
		
		$db->query("INSERT INTO `sitemap_url` SET `url` = ?s, `priority` = ?s, `changefreq` = ?s, `position` = ?s, `type` = 'mix'",
		'/singer-artist/'.$arr_ru[$q].'/', '1.0', 'hourly', '100');
	}
	
	exit;
}

if($handle = opendir('sitemap/'))
{
    while(false !== ($file = readdir($handle)))
    if($file != "." && $file != "..") unlink('sitemap/'.$file);
    closedir($handle);
}

$db->query("TRUNCATE `sitemap_files`");

$fc_mix = 0; 
$i_mix = 1;
$data_mix = $db->getAll("SELECT * FROM `sitemap_url` WHERE `type` = 'mix' ORDER BY `position` DESC");
foreach($data_mix as $url_mix){
	$fp_mix = fopen('sitemap/mix'.$fc_mix.'.xml', 'a');
	
	if($i_mix == 1){
		fwrite($fp_mix, '<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">'."\n");
		$db->query("INSERT INTO `sitemap_files` SET `file` = ?s, `type` = 'mix', `position` = ?i", 'mix'.$fc_mix.'.xml', $fc_mix);
	}
$url_mix['url'] = str_replace('&', '&amp;', $url_mix['url']);		
fwrite($fp_mix, '
<url>
	<loc>'.set('protocol', $db).$_SERVER['HTTP_HOST'].$url_mix['url'].'</loc>
	<changefreq>'.$url_mix['changefreq'].'</changefreq>
	<priority>'.$url_mix['priority'].'</priority>
</url>'."\n");
  
  if($i_mix == 3000){
	  fwrite($fp_mix, '</urlset>');
	  $fc_mix += 1;
	  $i_mix = 0;
  }
  
  $i_mix += 1;
}
fwrite($fp_mix, '</urlset>');
  
$fc_file = 0; 
$i_file = 1;
$data_file = $db->getAll("SELECT * FROM `sitemap_url` WHERE `type` = 'file' ORDER BY `position` DESC");
foreach($data_file as $url_file){
	$fp_file = fopen('sitemap/file'.$fc_file.'.xml', 'a');
	
	if($i_file == 1){
		fwrite($fp_file, '<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">'."\n");
		$db->query("INSERT INTO `sitemap_files` SET `file` = ?s, `type` = 'file', `position` = ?i", 'file'.$fc_file.'.xml', $fc_file);
	}	
$url_file['url'] = str_replace('&', '&amp;', $url_file['url']);	
fwrite($fp_file, '
<url>
	<loc>'.set('protocol', $db).$_SERVER['HTTP_HOST'].$url_file['url'].'</loc>
	<changefreq>'.$url_file['changefreq'].'</changefreq>
	<priority>'.$url_file['priority'].'</priority>
</url>'."\n");

  if($i_file == 3000){
	  fwrite($fp_file, '</urlset>');
	  $fc_file += 1;
	  $i_file = 0;
  }
  
  $i_file += 1;
}
fwrite($fp_file, '</urlset>');

$fc_artist = 0; 
$i_artist = 1;
$data_artist = $db->getAll("SELECT * FROM `sitemap_url` WHERE `type` = 'artist' ORDER BY `position` DESC");
foreach($data_artist as $url_artist){
	$fp_artist = fopen('sitemap/artist'.$fc_artist.'.xml', 'a');
	
	if($i_artist == 1){
		fwrite($fp_artist, '<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">'."\n");
		$db->query("INSERT INTO `sitemap_files` SET `file` = ?s, `type` = 'artist', `position` = ?i", 'artist'.$fc_artist.'.xml', $fc_artist);
	}
$url_artist['url'] = str_replace('&', '&amp;', $url_artist['url']);		
fwrite($fp_artist, '
<url>
	<loc>'.set('protocol', $db).$_SERVER['HTTP_HOST'].$url_artist['url'].'</loc>
	<changefreq>'.$url_artist['changefreq'].'</changefreq>
	<priority>'.$url_artist['priority'].'</priority>
</url>'."\n");
  
  if($i_artist == 3000){
	  fwrite($fp_artist, '</urlset>');
	  $fc_artist += 1;
	  $i_artist = 0;
  }
  
  $i_artist += 1;
}
fwrite($fp_artist, '</urlset>');

$sitemap = fopen('sitemap.xml', 'w');

fwrite($sitemap, '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'."\n");

$data_sitemap = $db->getAll("SELECT * FROM `sitemap_files`");
foreach($data_sitemap as $url_sitemap){

fwrite($sitemap, '
<sitemap>
	<loc>'.set('protocol', $db).$_SERVER['HTTP_HOST'].'/sitemap/'.$url_sitemap['file'].'</loc>
</sitemap>'."\n");	
}

fwrite($sitemap, '</sitemapindex>');

?>