2013-12-23 02:55:53 +01:00
|
|
|
<?php
|
|
|
|
|
2016-04-18 01:34:54 +02:00
|
|
|
use PicoFeed\Parser\MalformedXmlException;
|
|
|
|
|
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');
|
|
|
|
|
2015-08-05 01:01:21 +02:00
|
|
|
$values = Model\Feed\get($id);
|
|
|
|
$values += array(
|
|
|
|
'feed_group_ids' => Model\Group\get_feed_group_ids($id)
|
|
|
|
);
|
|
|
|
|
2013-12-23 02:55:53 +01:00
|
|
|
Response\html(Template\layout('edit_feed', array(
|
2015-08-05 01:01:21 +02:00
|
|
|
'values' => $values,
|
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'),
|
2015-08-05 01:01:21 +02:00
|
|
|
'groups' => Model\Group\get_all(),
|
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();
|
2015-08-05 01:01:21 +02:00
|
|
|
$values += array(
|
|
|
|
'enabled' => 0,
|
|
|
|
'download_content' => 0,
|
|
|
|
'rtl' => 0,
|
|
|
|
'cloak_referrer' => 0,
|
|
|
|
'feed_group_ids' => array(),
|
|
|
|
'create_group' => ''
|
|
|
|
);
|
2015-04-11 23:05:57 +02:00
|
|
|
|
2013-12-23 02:55:53 +01:00
|
|
|
list($valid, $errors) = Model\Feed\validate_modification($values);
|
|
|
|
|
|
|
|
if ($valid) {
|
|
|
|
if (Model\Feed\update($values)) {
|
|
|
|
Session\flash(t('Your subscription has been updated.'));
|
2015-04-11 23:05:57 +02:00
|
|
|
Response\redirect('?action=feeds');
|
2013-12-23 02:55:53 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
Session\flash_error(t('Unable to edit your subscription.'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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'),
|
2015-08-05 01:01:21 +02:00
|
|
|
'groups' => Model\Group\get_all(),
|
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() {
|
2014-12-19 21:58:46 +01:00
|
|
|
$nothing_to_read = Request\int_param('nothing_to_read');
|
|
|
|
$nb_unread_items = Model\Item\count_by_status('unread');
|
|
|
|
|
|
|
|
// possible with remember me function
|
|
|
|
if ($nothing_to_read === 1 && $nb_unread_items > 0) {
|
|
|
|
Response\redirect('?action=unread');
|
|
|
|
}
|
2013-12-23 02:55:53 +01:00
|
|
|
|
|
|
|
Response\html(Template\layout('feeds', array(
|
2016-01-10 01:04:42 +01:00
|
|
|
'favicons' => Model\Favicon\get_all_favicons(),
|
2014-02-23 01:45:02 +01:00
|
|
|
'feeds' => Model\Feed\get_all_item_counts(),
|
2014-12-19 21:58:46 +01:00
|
|
|
'nothing_to_read' => $nothing_to_read,
|
|
|
|
'nb_unread_items' => $nb_unread_items,
|
2015-01-14 19:44:41 +01:00
|
|
|
'nb_failed_feeds' => Model\Feed\count_failed_feeds(),
|
2013-12-23 02:55:53 +01:00
|
|
|
'menu' => 'feeds',
|
2015-08-02 18:20:38 +02:00
|
|
|
'title' => t('Subscriptions')
|
2013-12-23 02:55:53 +01:00
|
|
|
)));
|
|
|
|
});
|
|
|
|
|
|
|
|
// Display form to add one feed
|
|
|
|
Router\get_action('add', function() {
|
2015-08-05 01:01:21 +02:00
|
|
|
$values = array(
|
|
|
|
'download_content' => 0,
|
|
|
|
'rtl' => 0,
|
|
|
|
'cloak_referrer' => 0,
|
|
|
|
'create_group' => '',
|
|
|
|
'feed_group_ids' => array()
|
|
|
|
);
|
2015-04-10 20:08:33 +02:00
|
|
|
|
2013-12-23 02:55:53 +01:00
|
|
|
Response\html(Template\layout('add', array(
|
2015-04-10 20:08:33 +02:00
|
|
|
'values' => $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'),
|
2015-08-05 01:01:21 +02:00
|
|
|
'groups' => Model\Group\get_all(),
|
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'] : '';
|
2016-04-18 01:34:54 +02:00
|
|
|
} else {
|
2014-05-28 22:44:25 +02:00
|
|
|
$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
|
|
|
|
2015-08-05 01:01:21 +02:00
|
|
|
$values += array(
|
|
|
|
'url' => trim($url),
|
|
|
|
'download_content' => 0,
|
|
|
|
'rtl' => 0,
|
|
|
|
'cloak_referrer' => 0,
|
|
|
|
'create_group' => '',
|
|
|
|
'feed_group_ids' => array()
|
|
|
|
);
|
2013-12-23 02:55:53 +01:00
|
|
|
|
2015-04-10 23:34:07 +02:00
|
|
|
try {
|
2015-08-05 01:01:21 +02:00
|
|
|
$feed_id = Model\Feed\create(
|
|
|
|
$values['url'],
|
|
|
|
$values['download_content'],
|
|
|
|
$values['rtl'],
|
|
|
|
$values['cloak_referrer'],
|
|
|
|
$values['feed_group_ids'],
|
|
|
|
$values['create_group']
|
|
|
|
);
|
2015-04-10 23:34:07 +02:00
|
|
|
}
|
|
|
|
catch (UnexpectedValueException $e) {
|
|
|
|
$error_message = t('This subscription already exists.');
|
|
|
|
}
|
|
|
|
catch (PicoFeed\Client\InvalidCertificateException $e) {
|
|
|
|
$error_message = t('Invalid SSL certificate.');
|
|
|
|
}
|
|
|
|
catch (PicoFeed\Client\InvalidUrlException $e) {
|
|
|
|
// picoFeed uses this exception for multiple reasons, but doesn't
|
|
|
|
// provide an exception code to distinguish what exactly happend here
|
|
|
|
$error_message = $e->getMessage();
|
|
|
|
}
|
|
|
|
catch (PicoFeed\Client\MaxRedirectException $e) {
|
|
|
|
$error_message = t('Maximum number of HTTP redirections exceeded.');
|
|
|
|
}
|
|
|
|
catch (PicoFeed\Client\MaxSizeException $e) {
|
|
|
|
$error_message = t('The content size exceeds to maximum allowed size.');
|
|
|
|
}
|
|
|
|
catch (PicoFeed\Client\TimeoutException $e) {
|
|
|
|
$error_message = t('Connection timeout.');
|
|
|
|
}
|
|
|
|
catch (PicoFeed\Parser\MalformedXmlException $e) {
|
|
|
|
$error_message = t('Feed is malformed.');
|
|
|
|
}
|
|
|
|
catch (PicoFeed\Reader\SubscriptionNotFoundException $e) {
|
|
|
|
$error_message = t('Unable to find a subscription.');
|
|
|
|
}
|
|
|
|
catch (PicoFeed\Reader\UnsupportedFeedFormatException $e) {
|
|
|
|
$error_message = t('Unable to detect the feed format.');
|
|
|
|
}
|
|
|
|
|
2015-04-18 16:01:18 +02:00
|
|
|
Model\Config\write_debug();
|
|
|
|
|
2015-04-10 23:34:07 +02:00
|
|
|
if (isset($feed_id) && $feed_id !== false) {
|
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 {
|
2015-04-10 23:34:07 +02:00
|
|
|
if (! isset($error_message)) {
|
|
|
|
$error_message = t('Error occured.');
|
|
|
|
}
|
|
|
|
|
|
|
|
Session\flash_error($error_message);
|
2013-12-23 02:55:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Response\html(Template\layout('add', array(
|
2015-04-10 20:08:33 +02:00
|
|
|
'values' => $values + array('csrf' => Model\Config\generate_csrf()),
|
2014-11-15 14:32:31 +01:00
|
|
|
'nb_unread_items' => Model\Item\count_by_status('unread'),
|
2015-08-05 01:01:21 +02:00
|
|
|
'groups' => Model\Group\get_all(),
|
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() {
|
2016-04-18 01:34:54 +02:00
|
|
|
try {
|
|
|
|
Model\Feed\import_opml(Request\file_content('file'));
|
2013-12-23 02:55:53 +01:00
|
|
|
Session\flash(t('Your feeds have been imported.'));
|
2015-01-14 19:44:41 +01:00
|
|
|
Response\redirect('?action=feeds');
|
2016-04-18 01:34:54 +02:00
|
|
|
} catch (MalformedXmlException $e) {
|
|
|
|
Session\flash_error(t('Unable to import your OPML file.').' ('.$e->getMessage().')');
|
2013-12-23 02:55:53 +01:00
|
|
|
Response\redirect('?action=import');
|
|
|
|
}
|
|
|
|
});
|