<?php
/**
* rss.php
* Генерация RSS-ленты
* MSE-Script
* https://mse-script.ru/
* This code is protected by copyright
*/
namespace liw\core;
class rss extends controller
{
protected static $instance;
public $MSE = null;
public $dom = null;
public $channel = null;
public $siteUrl = null;
public function __construct ($MSE)
{
$this->MSE = $MSE;
$this->dom = new \DOMDocument('1.0', 'utf-8');
$this->dom->formatOutput = true;
$this->siteUrl = $this->MSE->config['allow_https'] ? 'https://musicat.net' : 'http://' . $this->MSE->config['domain'] . '/';
$root = $this->dom->appendChild($this->dom->createElement('rss'));
$root->setAttribute('version', '2.0');
$channel = $root->appendChild($this->dom->createElement('channel'));
$channel->appendChild($this->dom->createElement('title', $this->MSE->config['main_title']));
$channel->appendChild($this->dom->createElement('link', $this->siteUrl));
$channel->appendChild($this->dom->createElement('description', $this->MSE->config['main_description']));
$channel->appendChild($this->dom->createElement('poster', $this->MSE->config['main_poster']));
$this->channel = $channel;
}
# Singleton контроллера
public static function getInstance($data = NULL, $controllerName)
{
if (!is_object(self::$instance)) self::$instance = new rss($data);
return self::$instance;
}
# Действие по-умолчанию
# -----------------------------
public function index ()
{
# Выборка треков из базы для отправки в ленту
$stmt = $this->MSE->conn->prepare("SELECT
name,
url,
poster,
artist,
description
FROM
tracks
ORDER BY
`id` DESC
LIMIT
10");
$stmt->execute();
$results = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
foreach ($results as $track) {
$this->addItem($track['artist'].' - '.$track['name'], '/track/'.$track['url'], $track['description'], $track['poster']);
}
header("content-type: application/xml; charset=utf-8");
print($this->dom->saveXML());
}
# Добавление нового элемента в ленту
# -----------------------------
public function addItem($title, $link, $description, $poster)
{
$item = $this->dom->createElement('item');
$item->appendChild($this->dom->createElement('guid', md5($link)));
$item->appendChild($this->dom->createElement('title', $title));
$item->appendChild($this->dom->createElement('link', $this->siteUrl . $link));
$item->appendChild($this->dom->createElement('description', $description));
$item->appendChild($this->dom->createElement('content', $poster));
$this->channel->appendChild($item);
}
}