2013-12-23 02:55:53 +01:00
|
|
|
<?php
|
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
namespace Miniflux\Controller;
|
|
|
|
|
|
|
|
use Miniflux\Session\SessionStorage;
|
2016-08-24 04:03:45 +02:00
|
|
|
use Miniflux\Validator;
|
2016-08-25 03:17:58 +02:00
|
|
|
use Miniflux\Router;
|
|
|
|
use Miniflux\Response;
|
|
|
|
use Miniflux\Request;
|
|
|
|
use Miniflux\Template;
|
|
|
|
use Miniflux\Helper;
|
|
|
|
use Miniflux\Handler;
|
|
|
|
use Miniflux\Model;
|
2016-04-18 01:34:54 +02:00
|
|
|
|
2013-12-23 02:55:53 +01:00
|
|
|
// Refresh all feeds, used when Javascript is disabled
|
2016-04-18 01:44:45 +02:00
|
|
|
Router\get_action('refresh-all', function () {
|
2016-12-26 15:44:53 +01:00
|
|
|
$user_id = SessionStorage::getInstance()->getUserId();
|
|
|
|
Handler\Feed\update_feeds($user_id);
|
|
|
|
SessionStorage::getInstance()->setFlashErrorMessage(t('Your subscriptions are updated'));
|
2013-12-23 02:55:53 +01:00
|
|
|
Response\redirect('?action=unread');
|
|
|
|
});
|
|
|
|
|
|
|
|
// Edit feed form
|
2016-04-18 01:44:45 +02:00
|
|
|
Router\get_action('edit-feed', function () {
|
2016-12-26 15:44:53 +01:00
|
|
|
$user_id = SessionStorage::getInstance()->getUserId();
|
|
|
|
$feed_id = Request\int_param('feed_id');
|
2013-12-23 02:55:53 +01:00
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
$values = Model\Feed\get_feed($user_id, $feed_id);
|
2015-08-05 01:01:21 +02:00
|
|
|
$values += array(
|
2016-12-26 15:44:53 +01:00
|
|
|
'feed_group_ids' => Model\Group\get_feed_group_ids($feed_id)
|
2015-08-05 01:01:21 +02:00
|
|
|
);
|
|
|
|
|
2017-01-21 20:40:24 +01:00
|
|
|
Response\html(Template\layout('feeds/edit', array(
|
2015-08-05 01:01:21 +02:00
|
|
|
'values' => $values,
|
2013-12-23 02:55:53 +01:00
|
|
|
'errors' => array(),
|
2016-12-26 15:44:53 +01:00
|
|
|
'groups' => Model\Group\get_all($user_id),
|
2013-12-23 02:55:53 +01:00
|
|
|
'menu' => 'feeds',
|
|
|
|
'title' => t('Edit subscription')
|
|
|
|
)));
|
|
|
|
});
|
|
|
|
|
|
|
|
// Submit edit feed form
|
2016-04-18 01:44:45 +02:00
|
|
|
Router\post_action('edit-feed', function () {
|
2016-12-26 15:44:53 +01:00
|
|
|
$user_id = SessionStorage::getInstance()->getUserId();
|
2013-12-23 02:55:53 +01:00
|
|
|
$values = Request\values();
|
2015-08-05 01:01:21 +02:00
|
|
|
$values += array(
|
2016-12-28 03:59:09 +01:00
|
|
|
'enabled' => 0,
|
|
|
|
'download_content' => 0,
|
|
|
|
'rtl' => 0,
|
|
|
|
'cloak_referrer' => 0,
|
|
|
|
'parsing_error' => 0,
|
|
|
|
'parsing_error_message' => '',
|
|
|
|
'feed_group_ids' => array(),
|
2015-08-05 01:01:21 +02:00
|
|
|
);
|
2015-04-11 23:05:57 +02:00
|
|
|
|
2016-08-24 04:03:45 +02:00
|
|
|
list($valid, $errors) = Validator\Feed\validate_modification($values);
|
2013-12-23 02:55:53 +01:00
|
|
|
|
|
|
|
if ($valid) {
|
2016-12-26 15:44:53 +01:00
|
|
|
if (Model\Feed\update_feed($user_id, $values['id'], $values)) {
|
|
|
|
SessionStorage::getInstance()->setFlashMessage(t('Your subscription has been updated.'));
|
2015-04-11 23:05:57 +02:00
|
|
|
Response\redirect('?action=feeds');
|
2016-04-18 01:44:45 +02:00
|
|
|
} else {
|
2016-12-26 15:44:53 +01:00
|
|
|
SessionStorage::getInstance()->setFlashErrorMessage(t('Unable to edit your subscription.'));
|
2013-12-23 02:55:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-21 20:40:24 +01:00
|
|
|
Response\html(Template\layout('feeds/edit', array(
|
2013-12-23 02:55:53 +01:00
|
|
|
'values' => $values,
|
|
|
|
'errors' => $errors,
|
2016-12-26 15:44:53 +01:00
|
|
|
'groups' => Model\Group\get_all($user_id),
|
2013-12-23 02:55:53 +01:00
|
|
|
'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 () {
|
2016-12-26 15:44:53 +01:00
|
|
|
$user_id = SessionStorage::getInstance()->getUserId();
|
|
|
|
$feed_id = Request\int_param('feed_id');
|
2013-12-23 02:55:53 +01:00
|
|
|
|
2017-01-21 20:40:24 +01:00
|
|
|
Response\html(Template\layout('feeds/remove', array(
|
2016-12-26 15:44:53 +01:00
|
|
|
'feed' => Model\Feed\get_feed($user_id, $feed_id),
|
2013-12-23 02:55:53 +01:00
|
|
|
'menu' => 'feeds',
|
|
|
|
'title' => t('Confirmation')
|
|
|
|
)));
|
|
|
|
});
|
|
|
|
|
|
|
|
// Remove a feed
|
2016-04-18 01:44:45 +02:00
|
|
|
Router\get_action('remove-feed', function () {
|
2016-12-26 15:44:53 +01:00
|
|
|
$user_id = SessionStorage::getInstance()->getUserId();
|
|
|
|
$feed_id = Request\int_param('feed_id');
|
2013-12-23 02:55:53 +01:00
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
if (Model\Feed\remove_feed($user_id, $feed_id)) {
|
|
|
|
SessionStorage::getInstance()->setFlashMessage(t('This subscription has been removed successfully.'));
|
2016-04-18 01:44:45 +02:00
|
|
|
} else {
|
2016-12-26 15:44:53 +01:00
|
|
|
SessionStorage::getInstance()->setFlashErrorMessage(t('Unable to remove this subscription.'));
|
2013-12-23 02:55:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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 () {
|
2016-12-26 15:44:53 +01:00
|
|
|
$user_id = SessionStorage::getInstance()->getUserId();
|
2014-12-11 03:26:07 +01:00
|
|
|
$feed_id = Request\int_param('feed_id');
|
|
|
|
$redirect = Request\param('redirect', 'unread');
|
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
Handler\Feed\update_feed($user_id, $feed_id);
|
2014-12-11 03:26:07 +01:00
|
|
|
Response\redirect('?action='.$redirect.'&feed_id='.$feed_id);
|
2013-12-23 02:55:53 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// Ajax call to refresh one feed
|
2016-04-18 01:44:45 +02:00
|
|
|
Router\post_action('refresh-feed', function () {
|
2016-12-26 15:44:53 +01:00
|
|
|
$user_id = SessionStorage::getInstance()->getUserId();
|
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(
|
2017-01-04 02:31:12 +01:00
|
|
|
'feed_id' => $feed_id,
|
|
|
|
'result' => Handler\Feed\update_feed($user_id, $feed_id),
|
|
|
|
'feed' => Model\Feed\get_feed($user_id, $feed_id),
|
2016-12-26 15:44:53 +01:00
|
|
|
'items_count' => Model\ItemFeed\count_items_by_status($user_id, $feed_id),
|
2014-02-23 01:45:02 +01:00
|
|
|
));
|
2013-12-23 02:55:53 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// Display all feeds
|
2016-04-18 01:44:45 +02:00
|
|
|
Router\get_action('feeds', function () {
|
2016-12-26 15:44:53 +01:00
|
|
|
$user_id = SessionStorage::getInstance()->getUserId();
|
2014-12-19 21:58:46 +01:00
|
|
|
$nothing_to_read = Request\int_param('nothing_to_read');
|
2016-12-26 15:44:53 +01:00
|
|
|
$nb_unread_items = Model\Item\count_by_status($user_id, 'unread');
|
2017-01-15 20:51:55 +01:00
|
|
|
$feeds = Model\Feed\get_feeds_with_items_count_and_groups($user_id);
|
2014-12-19 21:58:46 +01:00
|
|
|
|
|
|
|
if ($nothing_to_read === 1 && $nb_unread_items > 0) {
|
|
|
|
Response\redirect('?action=unread');
|
|
|
|
}
|
2013-12-23 02:55:53 +01:00
|
|
|
|
2017-01-21 20:40:24 +01:00
|
|
|
Response\html(Template\layout('feeds/list', array(
|
2017-01-15 20:51:55 +01:00
|
|
|
'favicons' => Model\Favicon\get_feeds_favicons($feeds),
|
|
|
|
'feeds' => $feeds,
|
2014-12-19 21:58:46 +01:00
|
|
|
'nothing_to_read' => $nothing_to_read,
|
|
|
|
'nb_unread_items' => $nb_unread_items,
|
2016-12-26 15:44:53 +01:00
|
|
|
'nb_failed_feeds' => Model\Feed\count_failed_feeds($user_id),
|
2017-01-15 20:51:55 +01:00
|
|
|
'menu' => 'feeds',
|
|
|
|
'title' => t('Subscriptions'),
|
2013-12-23 02:55:53 +01:00
|
|
|
)));
|
|
|
|
});
|
|
|
|
|
|
|
|
// Display form to add one feed
|
2016-04-18 01:44:45 +02:00
|
|
|
Router\get_action('add', function () {
|
2016-12-26 15:44:53 +01:00
|
|
|
$user_id = SessionStorage::getInstance()->getUserId();
|
2015-08-05 01:01:21 +02:00
|
|
|
$values = array(
|
|
|
|
'download_content' => 0,
|
2016-12-26 15:44:53 +01:00
|
|
|
'rtl' => 0,
|
|
|
|
'cloak_referrer' => 0,
|
|
|
|
'create_group' => '',
|
|
|
|
'feed_group_ids' => array(),
|
2015-08-05 01:01:21 +02:00
|
|
|
);
|
2015-04-10 20:08:33 +02:00
|
|
|
|
2017-01-21 20:40:24 +01:00
|
|
|
Response\html(Template\layout('feeds/create', array(
|
2016-08-18 04:05:11 +02:00
|
|
|
'values' => $values + array('csrf' => Helper\generate_csrf()),
|
2013-12-23 02:55:53 +01:00
|
|
|
'errors' => array(),
|
2016-12-26 15:44:53 +01:00
|
|
|
'groups' => Model\Group\get_all($user_id),
|
|
|
|
'menu' => 'feeds',
|
|
|
|
'title' => t('New subscription'),
|
2013-12-23 02:55:53 +01:00
|
|
|
)));
|
|
|
|
});
|
|
|
|
|
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()) {
|
2013-12-23 02:55:53 +01:00
|
|
|
$values = Request\values();
|
2016-08-18 04:05:11 +02:00
|
|
|
Helper\check_csrf_values($values);
|
2013-12-23 02:55:53 +01:00
|
|
|
$url = isset($values['url']) ? $values['url'] : '';
|
2016-12-26 15:44:53 +01:00
|
|
|
$user_id = SessionStorage::getInstance()->getUserId();
|
2016-04-18 01:34:54 +02:00
|
|
|
} else {
|
2014-05-28 22:44:25 +02:00
|
|
|
$url = Request\param('url');
|
|
|
|
$token = Request\param('token');
|
2016-12-26 15:44:53 +01:00
|
|
|
$user = Model\User\get_user_by_token('bookmarklet_token', $token);
|
|
|
|
$values = array();
|
2014-05-28 22:44:25 +02:00
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
if (empty($user)) {
|
|
|
|
Response\text('Unauthorized', 401);
|
2014-05-28 22:44:25 +02:00
|
|
|
}
|
2016-12-26 15:44:53 +01:00
|
|
|
|
|
|
|
$user_id = $user['id'];
|
2014-05-28 22:44:25 +02:00
|
|
|
}
|
2013-12-23 02:55:53 +01:00
|
|
|
|
2015-08-05 01:01:21 +02:00
|
|
|
$values += array(
|
2016-12-26 15:44:53 +01:00
|
|
|
'url' => trim($url),
|
2015-08-05 01:01:21 +02:00
|
|
|
'download_content' => 0,
|
2016-12-26 15:44:53 +01:00
|
|
|
'rtl' => 0,
|
|
|
|
'cloak_referrer' => 0,
|
|
|
|
'feed_group_ids' => array(),
|
2017-02-02 01:30:04 +01:00
|
|
|
'group_name' => '',
|
2015-08-05 01:01:21 +02:00
|
|
|
);
|
2013-12-23 02:55:53 +01:00
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
list($feed_id, $error_message) = Handler\Feed\create_feed(
|
|
|
|
$user_id,
|
|
|
|
$values['url'],
|
|
|
|
$values['download_content'],
|
|
|
|
$values['rtl'],
|
|
|
|
$values['cloak_referrer'],
|
|
|
|
$values['feed_group_ids'],
|
2016-12-26 21:07:18 +01:00
|
|
|
$values['group_name']
|
2016-12-26 15:44:53 +01:00
|
|
|
);
|
2015-04-18 16:01:18 +02:00
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
if ($feed_id >= 1) {
|
|
|
|
SessionStorage::getInstance()->setFlashMessage(t('Subscription added successfully.'));
|
2014-10-19 15:52:59 +02:00
|
|
|
Response\redirect('?action=feed-items&feed_id='.$feed_id);
|
2016-04-18 01:44:45 +02:00
|
|
|
} else {
|
2016-12-26 15:44:53 +01:00
|
|
|
SessionStorage::getInstance()->setFlashErrorMessage($error_message);
|
2013-12-23 02:55:53 +01:00
|
|
|
}
|
|
|
|
|
2017-01-21 20:40:24 +01:00
|
|
|
Response\html(Template\layout('feeds/create', array(
|
2016-08-18 04:05:11 +02:00
|
|
|
'values' => $values + array('csrf' => Helper\generate_csrf()),
|
2016-12-26 15:44:53 +01:00
|
|
|
'groups' => Model\Group\get_all($user_id),
|
|
|
|
'menu' => 'feeds',
|
|
|
|
'title' => t('Subscriptions'),
|
2013-12-23 02:55:53 +01:00
|
|
|
)));
|
|
|
|
});
|