miniflux-legacy/app/controllers/feed.php

254 lines
8.0 KiB
PHP
Raw Normal View History

<?php
use PicoFeed\Parser\MalformedXmlException;
2016-08-24 04:03:45 +02:00
use Miniflux\Validator;
// Refresh all feeds, used when Javascript is disabled
2016-04-18 01:44:45 +02:00
Router\get_action('refresh-all', function () {
Model\Feed\refresh_all();
Session\flash(t('Your subscriptions are updated'));
Response\redirect('?action=unread');
});
// Edit feed form
2016-04-18 01:44:45 +02:00
Router\get_action('edit-feed', function () {
$id = Request\int_param('feed_id');
$values = Model\Feed\get($id);
$values += array(
'feed_group_ids' => Model\Group\get_feed_group_ids($id)
);
Response\html(Template\layout('edit_feed', array(
'values' => $values,
'errors' => array(),
'nb_unread_items' => Model\Item\count_by_status('unread'),
'groups' => Model\Group\get_all(),
'menu' => 'feeds',
'title' => t('Edit subscription')
)));
});
// Submit edit feed form
2016-04-18 01:44:45 +02:00
Router\post_action('edit-feed', function () {
$values = Request\values();
$values += array(
'enabled' => 0,
'download_content' => 0,
'rtl' => 0,
'cloak_referrer' => 0,
'feed_group_ids' => array(),
'create_group' => ''
);
2016-08-24 04:03:45 +02:00
list($valid, $errors) = Validator\Feed\validate_modification($values);
if ($valid) {
if (Model\Feed\update($values)) {
Session\flash(t('Your subscription has been updated.'));
Response\redirect('?action=feeds');
2016-04-18 01:44:45 +02:00
} else {
Session\flash_error(t('Unable to edit your subscription.'));
}
}
Response\html(Template\layout('edit_feed', array(
'values' => $values,
'errors' => $errors,
'nb_unread_items' => Model\Item\count_by_status('unread'),
'groups' => Model\Group\get_all(),
'menu' => 'feeds',
'title' => t('Edit subscription')
)));
});
// Confirmation box to remove a feed
2016-04-18 01:44:45 +02:00
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),
'nb_unread_items' => Model\Item\count_by_status('unread'),
'menu' => 'feeds',
'title' => t('Confirmation')
)));
});
// Remove a feed
2016-04-18 01:44:45 +02:00
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.'));
2016-04-18 01:44:45 +02:00
} else {
Session\flash_error(t('Unable to remove this subscription.'));
}
Response\redirect('?action=feeds');
});
// Refresh one feed and redirect to unread items
2016-04-18 01:44:45 +02:00
Router\get_action('refresh-feed', function () {
$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);
});
// Ajax call to refresh one feed
2016-04-18 01:44:45 +02:00
Router\post_action('refresh-feed', function () {
$feed_id = Request\int_param('feed_id', 0);
Response\json(array(
'feed_id' => $feed_id,
'result' => Model\Feed\refresh($feed_id),
'items_count' => Model\Feed\count_items($feed_id),
));
});
// Display all feeds
2016-04-18 01:44:45 +02:00
Router\get_action('feeds', function () {
$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');
}
Response\html(Template\layout('feeds', array(
2016-01-10 01:04:42 +01:00
'favicons' => Model\Favicon\get_all_favicons(),
'feeds' => Model\Feed\get_all_item_counts(),
'nothing_to_read' => $nothing_to_read,
'nb_unread_items' => $nb_unread_items,
'nb_failed_feeds' => Model\Feed\count_failed_feeds(),
'menu' => 'feeds',
'title' => t('Subscriptions')
)));
});
// Display form to add one feed
2016-04-18 01:44:45 +02:00
Router\get_action('add', function () {
$values = array(
'download_content' => 0,
'rtl' => 0,
'cloak_referrer' => 0,
'create_group' => '',
'feed_group_ids' => array()
);
Response\html(Template\layout('add', array(
'values' => $values + array('csrf' => Helper\generate_csrf()),
'errors' => array(),
'nb_unread_items' => Model\Item\count_by_status('unread'),
'groups' => Model\Group\get_all(),
'menu' => 'feeds',
'title' => t('New subscription')
)));
});
2016-08-21 18:55:27 +02:00
// Add a feed with the form or directly from the url, it can be used by a bookmarklet for example
2016-04-18 01:44:45 +02:00
Router\action('subscribe', function () {
2014-05-28 22:44:25 +02:00
if (Request\is_post()) {
$values = Request\values();
Helper\check_csrf_values($values);
$url = isset($values['url']) ? $values['url'] : '';
} 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);
}
}
$values += array(
'url' => trim($url),
'download_content' => 0,
'rtl' => 0,
'cloak_referrer' => 0,
'create_group' => '',
'feed_group_ids' => array()
);
try {
$feed_id = Model\Feed\create(
$values['url'],
$values['download_content'],
$values['rtl'],
$values['cloak_referrer'],
$values['feed_group_ids'],
$values['create_group']
);
2016-04-18 01:44:45 +02:00
} catch (UnexpectedValueException $e) {
$error_message = t('This subscription already exists.');
2016-04-18 01:44:45 +02:00
} catch (PicoFeed\Client\InvalidCertificateException $e) {
$error_message = t('Invalid SSL certificate.');
2016-04-18 01:44:45 +02:00
} catch (PicoFeed\Client\InvalidUrlException $e) {
$error_message = $e->getMessage();
2016-04-18 01:44:45 +02:00
} catch (PicoFeed\Client\MaxRedirectException $e) {
$error_message = t('Maximum number of HTTP redirections exceeded.');
2016-04-18 01:44:45 +02:00
} catch (PicoFeed\Client\MaxSizeException $e) {
$error_message = t('The content size exceeds to maximum allowed size.');
2016-04-18 01:44:45 +02:00
} catch (PicoFeed\Client\TimeoutException $e) {
$error_message = t('Connection timeout.');
2016-04-18 01:44:45 +02:00
} catch (PicoFeed\Parser\MalformedXmlException $e) {
$error_message = t('Feed is malformed.');
2016-04-18 01:44:45 +02:00
} catch (PicoFeed\Reader\SubscriptionNotFoundException $e) {
$error_message = t('Unable to find a subscription.');
2016-04-18 01:44:45 +02:00
} catch (PicoFeed\Reader\UnsupportedFeedFormatException $e) {
$error_message = t('Unable to detect the feed format.');
}
Model\Config\write_debug();
if (isset($feed_id) && $feed_id !== false) {
Session\flash(t('Subscription added successfully.'));
Response\redirect('?action=feed-items&feed_id='.$feed_id);
2016-04-18 01:44:45 +02:00
} else {
if (! isset($error_message)) {
$error_message = t('Error occured.');
}
Session\flash_error($error_message);
}
Response\html(Template\layout('add', array(
'values' => $values + array('csrf' => Helper\generate_csrf()),
'nb_unread_items' => Model\Item\count_by_status('unread'),
'groups' => Model\Group\get_all(),
'menu' => 'feeds',
'title' => t('Subscriptions')
)));
});
// OPML export
2016-04-18 01:44:45 +02:00
Router\get_action('export', function () {
Response\force_download('feeds.opml');
2016-08-24 03:38:09 +02:00
Response\xml(Handler\Opml\export_all_feeds());
});
// OPML import form
2016-04-18 01:44:45 +02:00
Router\get_action('import', function () {
Response\html(Template\layout('import', array(
'errors' => array(),
'nb_unread_items' => Model\Item\count_by_status('unread'),
'menu' => 'feeds',
'title' => t('OPML Import')
)));
});
// OPML importation
2016-04-18 01:44:45 +02:00
Router\post_action('import', function () {
try {
Model\Feed\import_opml(Request\file_content('file'));
Session\flash(t('Your feeds have been imported.'));
Response\redirect('?action=feeds');
} catch (MalformedXmlException $e) {
Session\flash_error(t('Unable to import your OPML file.').' ('.$e->getMessage().')');
Response\redirect('?action=import');
}
});