1
0
Fork 0
feedizer-php/htdocs/includes/views.inc.php
2015-11-13 23:51:46 +01:00

196 lines
9 KiB
PHP

<?php
#require_once(BASE_PATH . 'classes/Nibble-Forms/Nibble/NibbleForms/NibbleForm.php');
require(BASE_PATH . 'libraries/password_compat/lib/password.php');
require(BASE_PATH . 'libraries/FeedWriter/FeedTypes.php');
class views {
public static function home($params) {
global $tpl;
/* #FIXME
$tpl->assign('announcement', array(
'id' => 1234,
'date' => time(),
'title' => 'Foo Bar',
'abstract' => 'baz',
'content' => 'It\'s the text!',
'is_important' => false
));
*/
$tpl->display('home.html');
}
public static function create($params) {
global $tpl;
$formdata = array(
'name' => array('value' => $_REQUEST['slug']),
'uri' => array('value' => $_REQUEST['uri']),
'refresh_on_request' => array('checked' => $_REQUEST['refresh'] == 'interval' ? false : true),
'interval' => array('value' => $_REQUEST['interval']),
'expire' => array('checked' => isset($_REQUEST['expire']) ? true : false,
'value' => $_REQUEST['expire_value'],
'unit' => $_REQUEST['expire_unit']),
'description' => array('value' => $_REQUEST['description']),
);
// Check name (slug)
if (isset($_REQUEST['slug'])
and preg_match('#^[a-zA-Z0-9_\.+-]+$#', $_REQUEST['slug'])
) {
// Check URI
if (isset($_REQUEST['uri'])
and preg_match('#^https?://[a-zA-Z0-9_\.-]+\.[a-zA-Z0-9]{2,}(/.*)?$#', $_REQUEST['uri'])
) {
// Check refresh
if ($_REQUEST['refresh'] == 'request'
or ($_REQUEST['refresh'] == 'interval'
and $_REQUEST['interval'] >= 900
)) {
// Check expire
if ($_REQUEST['expire'] == false
or ($_REQUEST['expire'] == true
and intval($_REQUEST['expire_value']) > 0
and in_array($_REQUEST['expire_unit'], ['h', 'd', 'w', 'm'])
)) {
// Check description
if (strlen($_REQUEST['description']) < 1000) {
// Calculate expiration date, if needed
if ($_REQUEST['expire'] == true) {
$expireDate = new DateTime();
switch ($_REQUEST['expire_unit']) {
case 'h':
#$expireDate = strtotime('+' . $_REQUEST['expire_value'] . ' hours');
$expireString = $expireDate->add(new DateInterval("PT$_REQUEST[expire_value]H"))->format('Y-m-d H:i:s');
break;
case 'd':
#$expireDate = strtotime('+' . $_REQUEST['expire_value'] . ' days');
$expireString = $expireDate->add(new DateInterval("P$_REQUEST[expire_value]D"))->format('Y-m-d H:i:s');
break;
case 'w':
#$expireDate = strtotime('+' . $_REQUEST['expire_value'] . ' weeks');
$expireString = $expireDate->add(new DateInterval("P$_REQUEST[expire_value]W"))->format('Y-m-d H:i:s');
break;
case 'm':
#$expireDate = strtotime('+' . $_REQUEST['expire_value'] . ' months');
$expireString = $expireDate->add(new DateInterval("P$_REQUEST[expire_value]M"))->format('Y-m-d H:i:s');
break;
}
} else $expireString = null;
if (feeds::create($_REQUEST['slug'],
$_REQUEST['uri'],
$_REQUEST['refresh'] == 'interval' ? 'true' : 'false',
$_REQUEST['interval'],
'now',
isset($_REQUEST['expire']) ? 'true' : 'false',
$expireString,
$_REQUEST['password'],
$_SERVER['REMOTE_ADDR'],
'now'
)) {
$tpl->assign('title', 'Feedized!');
$tpl->assign('feed', $_REQUEST);
$tpl->assign('interval_hms', sec2hms($_REQUEST['interval']));
if ($_REQUEST['expire'])
$tpl->assign('expire_hms', sec2hms($_REQUEST['expire_value']));
$tpl->display('created.html');
exit();
}
} else { // erroneous description?
$formdata['description']['has_error'] = true;
$formdata['description']['error_message'] = 'You description is too long.';
}
} else { // erroneous expire?
$formdata['expire']['has_error'] = true;
$formdata['expire']['error_message'] = 'Please enter a valid timespan.';
}
} else { // erroneous interval?
$formdata['interval']['has_error'] = true;
$formdata['interval']['error_message'] = 'The request interval must be at least 15 minutes (900 seconds). Why did you even try?';
}
} else { // erroneous uri?
$formdata['uri']['has_error'] = true;
$formdata['uri']['error_message'] = 'Please enter a valid URI, like "http://duckduckgo.com/"';
}
} else { // erroneous name?
$formdata['name']['has_error'] = true;
$formdata['name']['error_message'] = 'Please enter a valid name for your feed. Allowed characters are: a-z A-Z 0-9 _ . -';
}
$tpl->assign('form', $formdata);
views::home($params);
}
public static function feed($params) {
$feedData = feeds::getBySlug($params['slug']);
if (!$feedData)
return [404, 'feed'];
$itemData = feedItems::getAll($feedData['id']);
$feed = new ATOMFeedWriter();
$feed->setTitle($feedData['slug']);
$feed->setLink('https://feedizer.tigris.fanir.de/feed/' . $feedData['slug']);
$feed->setChannelElement('updated', (new DateTime($itemData[0]['timestamp']))->format(DATE_ATOM));
foreach ($itemData as $item) {
$feedItem = $feed->createNewItem();
$feedItem->setTitle($feedData['slug']);
$feedItem->setLink($feedData['uri']);
$feedItem->setDate($item['timestamp']);
switch ($params['contentType']) {
case 'page':
$feedItem->setDescription($item['html']);
break;
case 'diff':
$feedItem->setDescription(nl2br(htmlspecialchars($item['diff'], ENT_XML1 | ENT_SUBSTITUTE | ENT_COMPAT)));
break;
default: return [404];
}
$feed->addItem($feedItem);
}
$feed->generateFeed();
return 0;
}
public static function feedInfo($params) {
global $tpl, $md;
$feed = feeds::getBySlug($params['slug']);
if (!$feed)
return [404, 'feed'];
#$feed['description'] = $md->transform(htmlspecialchars($feed['description']));
$tpl->assign('feed', $feed);
$tpl->assign('interval_hms', sec2hms($feed['refresh_interval']));
$tpl->display('feedInfo.html');
}
public static function flatpage($params) {
$file = $params['slug'] . '.html';
if (in_array($file, scandir(BASE_PATH . 'templates/flatpages'))) {
global $tpl;
$tpl->display('flatpages/' . $file);
} else {
return [404];
}
}
public static function announcement($params) {
$ann = announcements::getById($params['id']);
if ($ann != false) {
global $tpl;
$tpl->assign('announcement', $ann);
$tpl->display('announcement.html');
} else {
return [404, 'announcement'];
}
}
}