app->filter('seo.robots.macro', [ # подменяем макрос {sitemap} '{sitemap}' => $this->urlSEO($filter) . 'sitemap.xml', ])); return Response::text($template); } /** * Контент файла Sitemap.xml * @return \bff\http\Response */ public function sitemap_xml() { $path = $this->pathSEO(); # путь для хранения файлов $geo = ['id' => 0, 'city' => '', 'region1' => '', 'keyword' => '']; if (Geo::urlType() == Geo::URL_SUBDOMAIN) { $geo = Geo::filterUrl(); } $url = $this->urlSEO($geo['keyword']); $file = 'sitemap'; if ($geo['keyword']) { $file = $geo['keyword'] . '_' . $file; } $ext = '.xml'; $modified = 0; if (file_exists($path . $file . $ext)) { $modified = filemtime($path . $file . $ext); if ($this->input->get('check', TYPE_UINT)) { # проверка скачивания, не пересоздавать $modified = time(); } } if ($modified + $this->sitemapGenerateTimeout() < time()) { # создадим каталог, если не существует if (! file_exists($path)) { @mkdir($path, 0755, true); } if (! file_exists($path)) { return $this->errors->error404(); } if (Geo::urlType() == Geo::URL_SUBDOMAIN) { $this->generateSitemapXMLSubdomains($geo, $file, $path, $url); } else { $this->generateSitemapXMLSingleDomain($file, $path, $url); } } if (! file_exists($path . $file . $ext)) { return $this->errors->error404(); } # response: return Response::file($path . $file . $ext, 200, [ 'Content-Type' => 'text/xml', ]); } /** * Контент файла {region_}sitemap{number}.xml - если Sitemap.xml длинный и разбивается на части * @param string $region название региона * @param int $number номер файла * @param string $gzip расширение сжатого файла * @return \bff\http\Response */ public function sitemap_xml_part($region, $number, string $gzip = '') { $region = $this->input->clean($region, TYPE_NOTAGS); $number = $this->input->clean($number, TYPE_UINT); $path = $this->pathSEO() . $region . 'sitemap' . $number . '.xml' . $gzip; if (! file_exists($path)) { return $this->errors->error404(); } if ($region) { $filter = Geo::filterUrl('keyword'); if ($filter != trim($region, '_')) { return $this->errors->error404(); } } $headers = []; if ($gzip) { $headers['Content-Type'] = 'application/octet-stream'; $headers['Content-Encoding'] = 'gzip'; } else { $headers['Content-Type'] = 'text/xml'; } return Response::file($path, 200, $headers); } /** * Контент файла {region_}sitemap{number}.xml.gz - если sitemap.xml длинный, разбивается на части и архивируется * @param string $region название региона * @param int $number номер файла * @return \bff\http\Response */ public function sitemap_xml_part_gz($region, $number) { return $this->sitemap_xml_part($region, $number, '.gz'); } }