src/Controller/sitemap.php line 43

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Service\dbincl;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Symfony\Component\HttpFoundation\Request;
  8. /**
  9.  * @Route("/", name="sitemap_")
  10.  */
  11. class sitemap extends AbstractController
  12. {
  13.   /**
  14.    * @Route("/sitemap.xml", name="main")
  15.    */
  16.   public function main(dbincl $db_i): Response
  17.   {
  18.     
  19.     $db $db_i->set();
  20.     $responsec '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
  21.     $url sprintf("%s://%s", isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' 'https' 'http'$_SERVER['SERVER_NAME']);
  22.     $dt = new \DateTime();
  23.     $sql mysqli_query($db[2], "SELECT update_time, name FROM global");
  24.     while ($row mysqli_fetch_assoc($sql)) {
  25.       $dt->setTimestamp($row['update_time']);
  26.       $responsec .= '<sitemap><loc>' $url $this->generateUrl('sitemap_groups', ['groid' => $row['name']]) . '</loc><lastmod>' $dt->format('Y-m-d\TH:i:sP') . '</lastmod></sitemap>';
  27.     }
  28.     
  29.     $responsec .= '</sitemapindex>';
  30.     $response = new Response($responsec);
  31.     $response->headers->set('Content-Type''text/xml; charset=utf-8');
  32.     return $response;
  33.   }
  34.   /**
  35.    * @Route("/@sitemaps/{groid}_s.xml", name="groups", requirements={"groid"="[a-zA-Z0-9-_]+"})
  36.    */
  37.   public function groups(dbincl $db_i$groid): Response
  38.   {
  39.     
  40.     $db $db_i->set();
  41.     $responsec "";
  42.     $url sprintf("%s://%s", isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' 'https' 'http'$_SERVER['SERVER_NAME']);
  43.     $dt = new \DateTime();
  44.     
  45.     $return_error true;
  46.     $structure_original explode("_"$groid);
  47.     $structure array_reverse($structure_original);
  48.     if(!in_array(""$structure)){
  49.       #MAP_CHECK
  50.       $continu false;
  51.       $global $structure[0];
  52.       $parent = ['global'$global];
  53.       if(mysqli_num_rows(mysqli_query($db[2], "SELECT id FROM global WHERE name='$global' LIMIT 1")) > 0){
  54.         $che_arr $structure;
  55.         unset($che_arr[0]);
  56.         $continu_for true;
  57.         foreach ($che_arr as $str_val) {
  58.           list($parent_type$parent_name) = $parent;
  59.           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){
  60.             $parent[0] = 'group'$parent[1] = $str_val;
  61.           }else{
  62.             $continu_for false; break;
  63.           }
  64.         }
  65.         $continu $continu_for;
  66.       }
  67.       if($continu){
  68.         $generated_name $structure_original[0];
  69.         $generated_parent_type $parent[0];
  70.         $sql mysqli_query($db[2], "SELECT update_time, name FROM groups WHERE parent_name='$generated_name' AND parent_type='$generated_parent_type'");
  71.         if(mysqli_num_rows($sql) > 0){
  72.           $responsec .= '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
  73.           while ($row mysqli_fetch_assoc($sql)) {
  74.             $dt->setTimestamp($row['update_time']);
  75.             $tem_groid $row['name'] . "_" implode("_"$structure_original);
  76.             $responsec .= '<sitemap><loc>' $url $this->generateUrl('sitemap_groups', ['groid' => $tem_groid]) . '</loc><lastmod>' $dt->format('Y-m-d\TH:i:sP') . '</lastmod></sitemap>';
  77.           }
  78.           $responsec .= '</sitemapindex>';
  79.         }else{
  80.           $responsec .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
  81.           $allow_freq = ["always","hourly","daily","weekly","monthly","yearly","never"];
  82.           $loc_pat = ['/\{main_url\}/']; $loc_rec = [$url];
  83.           $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");
  84.           while ($row_i mysqli_fetch_assoc($sql)) {
  85.             $dt->setTimestamp($row_i['update_time']);
  86.             $freq ""$freq_db $row_i['changefreq'];
  87.             if(in_array($freq_db$allow_freq)){$freq '<changefreq>' $freq_db '</changefreq>';}
  88.             $loc preg_replace($loc_pat$loc_rec$row_i['loc']);
  89.             $responsec .= '<url><loc>' $loc '</loc><lastmod>' $dt->format('Y-m-d\TH:i:sP') . '</lastmod>' $freq '</url>';
  90.           }
  91.           $responsec .= '</urlset>';
  92.         }
  93.         $return_error false;
  94.       }
  95.     }
  96.     if($return_error === true){
  97.       return (new Response)->setStatusCode(404);
  98.     }else{
  99.       $response = new Response($responsec);
  100.       $response->headers->set('Content-Type''text/xml; charset=utf-8');
  101.       return $response;
  102.     }
  103.   }
  104. }
  105. ?>