| @copyright 2007-2026 (c) AVE.cms
| @link https://ave-cms.ru
| @version 3.3
*/
namespace App\Frontend\Sitemap;
defined('BASEPATH') || die('Direct access to this location is not allowed.');
class Controller
{
const PAGE_SIZE = 2000;
public function sitemap(array $params = array())
{
return self::handle(null);
}
public function page(array $params = array())
{
return self::handle(isset($params['part']) ? $params['part'] : null);
}
public static function handle($part = null)
{
header('Content-Type: application/xml; charset=UTF-8');
if ($part === null || $part === '') {
return self::index();
}
$part = (int) $part;
if ($part < 1) {
http_response_code(404);
return self::emptySet();
}
$rows = Repository::page(($part - 1) * self::PAGE_SIZE, self::PAGE_SIZE);
if (!$rows && $part !== 1) {
http_response_code(404);
return self::emptySet();
}
return self::urlSet($rows, $part === 1);
}
protected static function index()
{
$parts = max(1, (int) ceil(Repository::count() / self::PAGE_SIZE));
$xml = self::declaration() . "\n\n";
for ($part = 1; $part <= $parts; $part++) {
$xml .= " " . self::escape(rtrim(HOST, '/') . '/sitemap-' . $part . '.xml')
. '' . date('c') . "\n";
}
return $xml . '';
}
protected static function urlSet(array $rows, $includeHome)
{
$frequencies = array('always', 'hourly', 'daily', 'weekly', 'monthly', 'yearly', 'never');
$xml = self::declaration() . "\n\n";
if ($includeHome) {
$xml .= self::url(rtrim(HOST, '/') . '/', time(), 'weekly', '0.8');
}
foreach ($rows as $row) {
$row = (array) $row;
$frequency = isset($frequencies[(int) $row['document_sitemap_freq']])
? $frequencies[(int) $row['document_sitemap_freq']]
: 'weekly';
$changed = !empty($row['document_changed']) ? (int) $row['document_changed'] : (int) $row['document_published'];
$xml .= self::url(
rtrim(HOST, '/') . '/' . ltrim((string) $row['document_alias'], '/') . URL_SUFF,
$changed > 0 ? $changed : time(),
$frequency,
(string) $row['document_sitemap_pr']
);
}
return $xml . '';
}
protected static function url($location, $timestamp, $frequency, $priority)
{
return ' ' . self::escape($location) . '' . date('c', $timestamp)
. '' . self::escape($frequency) . ''
. self::escape($priority) . "\n";
}
protected static function emptySet()
{
return self::declaration() . "\n";
}
protected static function declaration()
{
return '';
}
protected static function escape($value)
{
return htmlspecialchars((string) $value, ENT_QUOTES | ENT_XML1, 'UTF-8');
}
}