<?php
namespace App\Controller;
use App\Service\dbincl;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
/**
* @Route("/", name="sitemap_")
*/
class sitemap extends AbstractController
{
/**
* @Route("/sitemap.xml", name="main")
*/
public function main(dbincl $db_i): Response
{
$db = $db_i->set();
$responsec = '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
$url = sprintf("%s://%s", isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http', $_SERVER['SERVER_NAME']);
$dt = new \DateTime();
$sql = mysqli_query($db[2], "SELECT update_time, name FROM global");
while ($row = mysqli_fetch_assoc($sql)) {
$dt->setTimestamp($row['update_time']);
$responsec .= '<sitemap><loc>' . $url . $this->generateUrl('sitemap_groups', ['groid' => $row['name']]) . '</loc><lastmod>' . $dt->format('Y-m-d\TH:i:sP') . '</lastmod></sitemap>';
}
$responsec .= '</sitemapindex>';
$response = new Response($responsec);
$response->headers->set('Content-Type', 'text/xml; charset=utf-8');
return $response;
}
/**
* @Route("/@sitemaps/{groid}_s.xml", name="groups", requirements={"groid"="[a-zA-Z0-9-_]+"})
*/
public function groups(dbincl $db_i, $groid): Response
{
$db = $db_i->set();
$responsec = "";
$url = sprintf("%s://%s", isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http', $_SERVER['SERVER_NAME']);
$dt = new \DateTime();
$return_error = true;
$structure_original = explode("_", $groid);
$structure = array_reverse($structure_original);
if(!in_array("", $structure)){
#MAP_CHECK
$continu = false;
$global = $structure[0];
$parent = ['global', $global];
if(mysqli_num_rows(mysqli_query($db[2], "SELECT id FROM global WHERE name='$global' LIMIT 1")) > 0){
$che_arr = $structure;
unset($che_arr[0]);
$continu_for = true;
foreach ($che_arr as $str_val) {
list($parent_type, $parent_name) = $parent;
if(mysqli_num_rows(mysqli_query($db[2], "SELECT id FROM groups WHERE name='$str_val' AND parent_type='$parent_type' AND parent_name='$parent_name' LIMIT 1")) > 0){
$parent[0] = 'group'; $parent[1] = $str_val;
}else{
$continu_for = false; break;
}
}
$continu = $continu_for;
}
if($continu){
$generated_name = $structure_original[0];
$generated_parent_type = $parent[0];
$sql = mysqli_query($db[2], "SELECT update_time, name FROM groups WHERE parent_name='$generated_name' AND parent_type='$generated_parent_type'");
if(mysqli_num_rows($sql) > 0){
$responsec .= '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
while ($row = mysqli_fetch_assoc($sql)) {
$dt->setTimestamp($row['update_time']);
$tem_groid = $row['name'] . "_" . implode("_", $structure_original);
$responsec .= '<sitemap><loc>' . $url . $this->generateUrl('sitemap_groups', ['groid' => $tem_groid]) . '</loc><lastmod>' . $dt->format('Y-m-d\TH:i:sP') . '</lastmod></sitemap>';
}
$responsec .= '</sitemapindex>';
}else{
$responsec .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
$allow_freq = ["always","hourly","daily","weekly","monthly","yearly","never"];
$loc_pat = ['/\{main_url\}/']; $loc_rec = [$url];
$sql = mysqli_query($db[2], "SELECT update_time, loc, changefreq FROM elements WHERE parent_name='$generated_name' AND parent_type='$generated_parent_type' AND if_valid=1");
while ($row_i = mysqli_fetch_assoc($sql)) {
$dt->setTimestamp($row_i['update_time']);
$freq = ""; $freq_db = $row_i['changefreq'];
if(in_array($freq_db, $allow_freq)){$freq = '<changefreq>' . $freq_db . '</changefreq>';}
$loc = preg_replace($loc_pat, $loc_rec, $row_i['loc']);
$responsec .= '<url><loc>' . $loc . '</loc><lastmod>' . $dt->format('Y-m-d\TH:i:sP') . '</lastmod>' . $freq . '</url>';
}
$responsec .= '</urlset>';
}
$return_error = false;
}
}
if($return_error === true){
return (new Response)->setStatusCode(404);
}else{
$response = new Response($responsec);
$response->headers->set('Content-Type', 'text/xml; charset=utf-8');
return $response;
}
}
}
?>