2013-12-23 02:55:53 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use PicoFarad\Router;
|
|
|
|
use PicoFarad\Response;
|
|
|
|
use PicoFarad\Request;
|
|
|
|
use PicoFarad\Session;
|
2014-03-17 02:56:43 +01:00
|
|
|
use PicoFarad\Template;
|
2013-12-23 02:55:53 +01:00
|
|
|
|
|
|
|
// Refresh all feeds, used when Javascript is disabled
|
|
|
|
Router\get_action('refresh-all', function() {
|
|
|
|
|
|
|
|
Model\Feed\refresh_all();
|
|
|
|
Session\flash(t('Your subscriptions are updated'));
|
|
|
|
Response\redirect('?action=unread');
|
|
|
|
});
|
|
|
|
|
|
|
|
// Edit feed form
|
|
|
|
Router\get_action('edit-feed', function() {
|
|
|
|
|
|
|
|
$id = Request\int_param('feed_id');
|
|
|
|
|
|
|
|
Response\html(Template\layout('edit_feed', array(
|
|
|
|
'values' => Model\Feed\get($id),
|
|
|
|
'errors' => array(),
|
2014-11-15 14:32:31 +01:00
|
|
|
'nb_unread_items' => Model\Item\count_by_status('unread'),
|
2013-12-23 02:55:53 +01:00
|
|
|
'menu' => 'feeds',
|
|
|
|
'title' => t('Edit subscription')
|
|
|
|
)));
|
|
|
|
});
|
|
|
|
|
|
|
|
// Submit edit feed form
|
|
|
|
Router\post_action('edit-feed', function() {
|
|
|
|
|
|
|
|
$values = Request\values();
|
|
|
|
list($valid, $errors) = Model\Feed\validate_modification($values);
|
|
|
|
|
|
|
|
if ($valid) {
|
|
|
|
|
|
|
|
if (Model\Feed\update($values)) {
|
|
|
|
Session\flash(t('Your subscription has been updated.'));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Session\flash_error(t('Unable to edit your subscription.'));
|
|
|
|
}
|
|
|
|
|
|
|
|
Response\redirect('?action=feeds');
|
|
|
|
}
|
|
|
|
|
|
|
|
Response\html(Template\layout('edit_feed', array(
|
|
|
|
'values' => $values,
|
|
|
|
'errors' => $errors,
|
2014-11-15 14:32:31 +01:00
|
|
|
'nb_unread_items' => Model\Item\count_by_status('unread'),
|
2013-12-23 02:55:53 +01:00
|
|
|
'menu' => 'feeds',
|
|
|
|
'title' => t('Edit subscription')
|
|
|
|
)));
|
|
|
|
});
|
|
|
|
|
|
|
|
// Confirmation box to remove a feed
|
|
|
|
Router\get_action('confirm-remove-feed', function() {
|
|
|
|
|
|
|
|
$id = Request\int_param('feed_id');
|
|
|
|
|
|
|
|
Response\html(Template\layout('confirm_remove_feed', array(
|
|
|
|
'feed' => Model\Feed\get($id),
|
2014-11-15 14:32:31 +01:00
|
|
|
'nb_unread_items' => Model\Item\count_by_status('unread'),
|
2013-12-23 02:55:53 +01:00
|
|
|
'menu' => 'feeds',
|
|
|
|
'title' => t('Confirmation')
|
|
|
|
)));
|
|
|
|
});
|
|
|
|
|
|
|
|
// Remove a feed
|
|
|
|
Router\get_action('remove-feed', function() {
|
|
|
|
|
|
|
|
$id = Request\int_param('feed_id');
|
|
|
|
|
|
|
|
if ($id && Model\Feed\remove($id)) {
|
|
|
|
Session\flash(t('This subscription has been removed successfully.'));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Session\flash_error(t('Unable to remove this subscription.'));
|
|
|
|
}
|
|
|
|
|
|
|
|
Response\redirect('?action=feeds');
|
|
|
|
});
|
|
|
|
|
|
|
|
// Refresh one feed and redirect to unread items
|
|
|
|
Router\get_action('refresh-feed', function() {
|
|
|
|
|
2014-12-11 03:26:07 +01:00
|
|
|
$feed_id = Request\int_param('feed_id');
|
|
|
|
$redirect = Request\param('redirect', 'unread');
|
|
|
|
|
|
|
|
Model\Feed\refresh($feed_id);
|
|
|
|
Response\redirect('?action='.$redirect.'&feed_id='.$feed_id);
|
2013-12-23 02:55:53 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// Ajax call to refresh one feed
|
|
|
|
Router\post_action('refresh-feed', function() {
|
|
|
|
|
2014-02-23 01:45:02 +01:00
|
|
|
$feed_id = Request\int_param('feed_id', 0);
|
2013-12-23 02:55:53 +01:00
|
|
|
|
2014-02-23 01:45:02 +01:00
|
|
|
Response\json(array(
|
|
|
|
'feed_id' => $feed_id,
|
|
|
|
'result' => Model\Feed\refresh($feed_id),
|
|
|
|
'items_count' => Model\Feed\count_items($feed_id),
|
|
|
|
));
|
2013-12-23 02:55:53 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// Display all feeds
|
|
|
|
Router\get_action('feeds', function() {
|
|
|
|
|
|
|
|
if (! Request\int_param('disable_empty_feeds_check')) {
|
|
|
|
|
|
|
|
$empty_feeds = Model\Feed\get_all_empty();
|
|
|
|
|
|
|
|
if (! empty($empty_feeds)) {
|
|
|
|
|
|
|
|
$listing = array();
|
|
|
|
|
|
|
|
foreach ($empty_feeds as &$feed) {
|
|
|
|
$listing[] = '"'.$feed['title'].'"';
|
|
|
|
}
|
|
|
|
|
|
|
|
$message = t(
|
2015-01-13 00:07:09 +01:00
|
|
|
'There are %d empty feeds, there is maybe an error: %s...',
|
2013-12-23 02:55:53 +01:00
|
|
|
count($empty_feeds),
|
|
|
|
implode(', ', array_slice($listing, 0, 5))
|
|
|
|
);
|
|
|
|
|
|
|
|
Session\flash_error($message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Response\html(Template\layout('feeds', array(
|
2014-12-25 00:12:41 +01:00
|
|
|
'favicons' => Model\Feed\get_all_favicons(),
|
2014-02-23 01:45:02 +01:00
|
|
|
'feeds' => Model\Feed\get_all_item_counts(),
|
2013-12-23 02:55:53 +01:00
|
|
|
'nothing_to_read' => Request\int_param('nothing_to_read'),
|
2014-11-15 14:32:31 +01:00
|
|
|
'nb_unread_items' => Model\Item\count_by_status('unread'),
|
2013-12-23 02:55:53 +01:00
|
|
|
'menu' => 'feeds',
|
|
|
|
'title' => t('Subscriptions')
|
|
|
|
)));
|
|
|
|
});
|
|
|
|
|
|
|
|
// Display form to add one feed
|
|
|
|
Router\get_action('add', function() {
|
|
|
|
|
|
|
|
Response\html(Template\layout('add', array(
|
2014-11-08 03:44:20 +01:00
|
|
|
'values' => array(
|
|
|
|
'csrf' => Model\Config\generate_csrf(),
|
|
|
|
),
|
2013-12-23 02:55:53 +01:00
|
|
|
'errors' => array(),
|
2014-11-15 14:32:31 +01:00
|
|
|
'nb_unread_items' => Model\Item\count_by_status('unread'),
|
2013-12-23 02:55:53 +01:00
|
|
|
'menu' => 'feeds',
|
|
|
|
'title' => t('New subscription')
|
|
|
|
)));
|
|
|
|
});
|
|
|
|
|
|
|
|
// Add a feed with the form or directly from the url, it can be used by a bookmarklet by example
|
|
|
|
Router\action('subscribe', function() {
|
|
|
|
|
2014-05-28 22:44:25 +02:00
|
|
|
if (Request\is_post()) {
|
2013-12-23 02:55:53 +01:00
|
|
|
$values = Request\values();
|
2014-11-08 03:44:20 +01:00
|
|
|
Model\Config\check_csrf_values($values);
|
2013-12-23 02:55:53 +01:00
|
|
|
$url = isset($values['url']) ? $values['url'] : '';
|
|
|
|
}
|
2014-05-28 22:44:25 +02:00
|
|
|
else {
|
|
|
|
$values = array();
|
|
|
|
$url = Request\param('url');
|
|
|
|
$token = Request\param('token');
|
|
|
|
|
|
|
|
if ($token !== Model\Config\get('bookmarklet_token')) {
|
|
|
|
Response\text('Access Forbidden', 403);
|
|
|
|
}
|
|
|
|
}
|
2013-12-23 02:55:53 +01:00
|
|
|
|
2014-10-20 01:14:33 +02:00
|
|
|
$values += array('download_content' => 0, 'rtl' => 0);
|
2013-12-23 02:55:53 +01:00
|
|
|
$url = trim($url);
|
2014-10-20 01:14:33 +02:00
|
|
|
$feed_id = Model\Feed\create($url, $values['download_content'], $values['rtl']);
|
2013-12-23 02:55:53 +01:00
|
|
|
|
2014-10-19 15:52:59 +02:00
|
|
|
if ($feed_id) {
|
2013-12-23 02:55:53 +01:00
|
|
|
Session\flash(t('Subscription added successfully.'));
|
2014-10-19 15:52:59 +02:00
|
|
|
Response\redirect('?action=feed-items&feed_id='.$feed_id);
|
2013-12-23 02:55:53 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
Session\flash_error(t('Unable to find a subscription.'));
|
|
|
|
}
|
|
|
|
|
|
|
|
Response\html(Template\layout('add', array(
|
2014-11-08 03:44:20 +01:00
|
|
|
'values' => array(
|
|
|
|
'url' => $url,
|
|
|
|
'csrf' => Model\Config\generate_csrf(),
|
|
|
|
),
|
2014-11-15 14:32:31 +01:00
|
|
|
'nb_unread_items' => Model\Item\count_by_status('unread'),
|
2013-12-23 02:55:53 +01:00
|
|
|
'menu' => 'feeds',
|
|
|
|
'title' => t('Subscriptions')
|
|
|
|
)));
|
|
|
|
});
|
|
|
|
|
|
|
|
// OPML export
|
|
|
|
Router\get_action('export', function() {
|
|
|
|
|
|
|
|
Response\force_download('feeds.opml');
|
|
|
|
Response\xml(Model\Feed\export_opml());
|
|
|
|
});
|
|
|
|
|
|
|
|
// OPML import form
|
|
|
|
Router\get_action('import', function() {
|
|
|
|
|
|
|
|
Response\html(Template\layout('import', array(
|
|
|
|
'errors' => array(),
|
2014-11-15 14:32:31 +01:00
|
|
|
'nb_unread_items' => Model\Item\count_by_status('unread'),
|
2013-12-23 02:55:53 +01:00
|
|
|
'menu' => 'feeds',
|
|
|
|
'title' => t('OPML Import')
|
|
|
|
)));
|
|
|
|
});
|
|
|
|
|
|
|
|
// OPML importation
|
|
|
|
Router\post_action('import', function() {
|
|
|
|
|
|
|
|
if (Model\Feed\import_opml(Request\file_content('file'))) {
|
|
|
|
|
|
|
|
Session\flash(t('Your feeds have been imported.'));
|
|
|
|
Response\redirect('?action=feeds&disable_empty_feeds_check=1');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
Session\flash_error(t('Unable to import your OPML file.'));
|
|
|
|
Response\redirect('?action=import');
|
|
|
|
}
|
|
|
|
});
|