Example #1
0
Router\action('subscribe', function () {
    if (Request\is_post()) {
        $values = Request\values();
        Model\Config\check_csrf_values($values);
        $url = isset($values['url']) ? $values['url'] : '';
    } else {
        $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']);
    } 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.');
    }
    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);
    } 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' => Model\Config\generate_csrf()), 'nb_unread_items' => Model\Item\count_by_status('unread'), 'groups' => Model\Group\get_all(), 'menu' => 'feeds', 'title' => t('Subscriptions'))));
});
Example #2
0
Router\get_action('add', function () {
    Response\html(Template\layout('add', array('values' => array('csrf' => Model\Config\generate_csrf()), 'errors' => array(), 'nb_unread_items' => Model\Item\count_by_status('unread'), '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 () {
    if (Request\is_post()) {
        $values = Request\values();
        Model\Config\check_csrf_values($values);
        $url = isset($values['url']) ? $values['url'] : '';
    } else {
        $values = array();
        $url = Request\param('url');
        $token = Request\param('token');
        if ($token !== Model\Config\get('bookmarklet_token')) {
            Response\text('Access Forbidden', 403);
        }
    }
    $values += array('download_content' => 0, 'rtl' => 0, 'cloak_referrer' => 0);
    $url = trim($url);
    $feed_id = Model\Feed\create($url, $values['download_content'], $values['rtl'], $values['cloak_referrer']);
    if ($feed_id) {
        Session\flash(t('Subscription added successfully.'));
        Response\redirect('?action=feed-items&feed_id=' . $feed_id);
    } else {
        Session\flash_error(t('Unable to find a subscription.'));
    }
    Response\html(Template\layout('add', array('values' => array('url' => $url, 'csrf' => Model\Config\generate_csrf()), 'nb_unread_items' => Model\Item\count_by_status('unread'), 'menu' => 'feeds', 'title' => t('Subscriptions'))));
});
// OPML export
Router\get_action('export', function () {
    Response\force_download('feeds.opml');
    Response\xml(Model\Feed\export_opml());