This repository has been archived on 2024-06-26. You can view files and clone it, but cannot push or open issues or pull requests.
sakura/main/news.php

152 lines
5.1 KiB
PHP
Raw Normal View History

2015-04-02 13:41:05 +00:00
<?php
/*
* Sakura News Page
*/
// Declare Namespace
namespace Sakura;
2015-05-23 03:31:42 +00:00
// Use DOMDocument
use DOMDocument;
2015-04-02 13:41:05 +00:00
// Include components
2015-04-06 16:15:20 +00:00
require_once str_replace(basename(__DIR__), '', dirname(__FILE__)) .'_sakura/sakura.php';
2015-04-02 13:41:05 +00:00
2015-07-19 20:10:17 +00:00
// Get user data
2015-08-21 22:07:45 +00:00
$disqus_user = new User(Session::$userId);
2015-07-19 20:10:17 +00:00
// Set disqus data
$disqus_data = [
2015-08-21 22:07:45 +00:00
$disqus_user->data['id'],
$disqus_user->data['username'],
$disqus_user->data['email'],
'http://'. Configuration::getConfig('url_main') .'/a/'. $disqus_user->data['id'],
'http://'. Configuration::getConfig('url_main') .'/u/'. $disqus_user->data['id']
2015-07-19 20:10:17 +00:00
];
2015-04-02 13:41:05 +00:00
// Add page specific things
$renderData['newsPosts'] = Main::getNewsPosts((isset($_GET['id']) && !isset($_GET['xml']) && is_numeric($_GET['id'])) ? $_GET['id'] : null, (isset($_GET['id']) && !isset($_GET['xml']) && is_numeric($_GET['id'])));
2015-08-21 22:07:45 +00:00
2015-04-02 13:41:05 +00:00
$renderData['page'] = [
2015-08-21 22:07:45 +00:00
2015-07-31 21:18:14 +00:00
'title' => (isset($_GET['id']) ? (count($renderData['newsPosts']) ? $renderData['newsPosts'][0]['title'] : 'Post does not exist!') : 'News'),
'disqus_sso' => (($disqus_message = base64_encode(json_encode($disqus_data))) .' '. Main::dsqHmacSha1($disqus_message .' '. time(), Configuration::getConfig('disqus_api_secret')) .' '. time()),
'view_post' => isset($_GET['id']) && count($renderData['newsPosts']),
'currentPage' => 0
2015-08-21 22:07:45 +00:00
2015-04-02 13:41:05 +00:00
];
2015-04-02 14:25:35 +00:00
2015-05-11 22:20:19 +00:00
// News XML feed
2015-04-02 14:25:35 +00:00
if(isset($_GET['xml'])) {
2015-08-21 22:07:45 +00:00
2015-05-23 03:31:42 +00:00
// Meta data attributes
$metaData = [
'title' => ($_FEED_TITLE = Configuration::getConfig('sitename')) .' News',
2015-08-09 20:50:03 +00:00
'link' => ($_FEED_URL = 'http://'. Configuration::getConfig('url_main')),
2015-05-23 03:31:42 +00:00
'description' => 'News about '. $_FEED_TITLE,
'language' => 'en-gb',
2015-08-21 22:07:45 +00:00
'webMaster' => (new User(1))->data['email'] .' ('. $_FEED_TITLE .' Webmaster)',
2015-05-23 03:31:42 +00:00
'pubDate' => ($_FEED_DATE = date('r', $renderData['newsPosts'][0]['date'])),
'lastBuildDate' => $_FEED_DATE,
];
// Item attributes
$itemData = [
'title' => ['text' => '{EVAL}', 'eval' => '$newsPost["title"]'],
'link' => ['text' => $_FEED_URL .'/news/{EVAL}', 'eval' => '$newsPost["id"]'],
'guid' => ['text' => $_FEED_URL .'/news/{EVAL}', 'eval' => '$newsPost["id"]'],
'pubDate' => ['text' => '{EVAL}', 'eval' => 'date("D, d M Y G:i:s O", $newsPost["date"])'],
'dc:publisher' => ['text' => '{EVAL}', 'eval' => '$newsPost["udata"]["username"]'],
'description' => ['cdata' => '{EVAL}', 'eval' => '$newsPost["parsed"]'],
];
// Create a new DOM document
$feed = new DOMDocument('1.0', 'utf-8');
// Create the RSS element
$fRss = $feed->createElement('rss');
// Set attributes
$fRss->setAttribute('version', '2.0');
$fRss->setAttribute('xmlns:atom', 'http://www.w3.org/2005/Atom');
$fRss->setAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1');
// Create the channel element
$fChannel = $feed->createElement('channel');
// Build meta elements
foreach($metaData as $name => $value) {
// Create the elements
$mElem = $feed->createElement($name);
$mElemText = $feed->createTextNode($value);
// Append them
$mElem ->appendChild($mElemText);
$fChannel ->appendChild($mElem);
// Unset the working vars
unset($mElem);
unset($mElemText);
2015-04-02 14:25:35 +00:00
2015-05-23 03:31:42 +00:00
}
2015-04-02 14:25:35 +00:00
2015-05-23 03:31:42 +00:00
// Add all the posts
foreach($renderData['newsPosts'] as $newsPost) {
2015-04-02 14:25:35 +00:00
2015-05-23 03:31:42 +00:00
// Create item element
$fPost = $feed->createElement('item');
2015-04-02 14:25:35 +00:00
2015-05-23 03:31:42 +00:00
// Create post attributes
foreach($itemData as $tag => $valueData) {
// Create the element
$pElem = $feed->createElement($tag);
2015-04-02 14:25:35 +00:00
2015-05-23 03:31:42 +00:00
// Create value
eval('$value = '. $valueData['eval'] .';');
$value = str_replace('{EVAL}', $value, $valueData[(array_key_exists('cdata', $valueData) ? 'cdata' : 'text')]);
2015-04-02 14:25:35 +00:00
2015-05-23 03:31:42 +00:00
// Create text node or cdata container
$pElemText = (array_key_exists('cdata', $valueData)) ? $feed->createCDATASection($value) : $feed->createTextNode($value);
2015-04-02 14:25:35 +00:00
2015-05-23 03:31:42 +00:00
// Append them
$pElem ->appendChild($pElemText);
$fPost ->appendChild($pElem);
// Unset the working vars
unset($pElem);
unset($pElemText);
unset($value);
}
// Append the item to the channel
$fChannel->appendChild($fPost);
2015-04-02 14:25:35 +00:00
}
2015-05-23 03:31:42 +00:00
// Append the channel element to RSS
$fRss->appendChild($fChannel);
// Append the RSS element to the DOM
$feed->appendChild($fRss);
2015-04-02 14:25:35 +00:00
2015-05-23 03:31:42 +00:00
// Return the feed
print $feed->saveXML();
2015-04-02 14:25:35 +00:00
exit;
}
2015-04-02 13:41:05 +00:00
2015-07-31 21:18:14 +00:00
// If we're not using the XML feed and we're not viewing a single post create pages
if(!isset($_GET['id'])) {
// Create the current page
$renderData['newsPosts'] = array_chunk($renderData['newsPosts'], Configuration::getConfig('news_posts_per_page'), true);
$renderData['page']['currentPage'] = isset($_GET['page']) && ($_GET['page'] - 1) >= 0 ? $_GET['page'] - 1 : 0;
}
2015-04-02 13:41:05 +00:00
// Print page contents
2015-04-06 21:57:17 +00:00
print Templates::render('main/news.tpl', $renderData);