miniflux-legacy/index.php

483 lines
10 KiB
PHP
Raw Normal View History

2013-02-18 03:48:21 +01:00
<?php
require 'common.php';
require 'vendor/PicoTools/Template.php';
require 'vendor/PicoTools/Helper.php';
require 'vendor/PicoFarad/Response.php';
require 'vendor/PicoFarad/Request.php';
require 'vendor/PicoFarad/Session.php';
require 'vendor/PicoFarad/Router.php';
use PicoFarad\Router;
use PicoFarad\Response;
use PicoFarad\Request;
use PicoFarad\Session;
use PicoTools\Template;
Session\open(dirname($_SERVER['PHP_SELF']));
Router\before(function($action) {
if ($action !== 'login' && ! isset($_SESSION['user'])) {
2013-04-12 21:57:54 +02:00
Response\redirect('?action=login');
2013-02-18 03:48:21 +01:00
}
2013-04-13 03:08:55 +02:00
$language = 'en_US';
if (isset($_SESSION['user']['language'])) {
$language = $_SESSION['user']['language'];
}
else if (isset($_COOKIE['language'])) {
$language = $_COOKIE['language'];
}
if ($language !== 'en_US') {
PicoTools\Translator\load($language);
}
setcookie('language', $language, time()+365*24*3600, dirname($_SERVER['PHP_SELF']));
2013-02-18 03:48:21 +01:00
Response\csp(array(
'img-src' => '*',
'frame-src' => 'http://www.youtube.com https://www.youtube.com http://player.vimeo.com https://player.vimeo.com'
2013-02-18 03:48:21 +01:00
));
Response\xframe();
Response\xss();
Response\nosniff();
});
Router\get_action('logout', function() {
Session\close();
Response\redirect('?action=login');
});
Router\get_action('login', function() {
if (isset($_SESSION['user'])) {
Response\redirect('./index.php');
}
Response\html(Template\load('login', array(
'errors' => array(),
'values' => array()
)));
});
Router\post_action('login', function() {
$values = Request\values();
list($valid, $errors) = Model\validate_login($values);
if ($valid) {
Response\redirect('?action=default');
}
Response\html(Template\load('login', array(
'errors' => $errors,
'values' => $values
)));
});
2013-03-27 02:57:12 +01:00
Router\get_action('show', function() {
2013-05-26 19:09:34 +02:00
$id = Model\decode_item_id(Request\param('id'));
2013-03-27 02:57:12 +01:00
Response\html(Template\layout('read_item', array(
'item' => Model\get_item($id)
)));
});
Router\get_action('show_starred_item', function() {
$id = Model\decode_item_id(Request\param('id'));
Response\html(Template\layout('starred_item', array(
'item' => Model\get_item($id)
)));
});
2013-02-18 03:48:21 +01:00
Router\get_action('read', function() {
$id = Model\decode_item_id(Request\param('id'));
2013-03-27 02:57:12 +01:00
$item = Model\get_item($id);
$nav = Model\get_nav_item($item); // must be placed before set_item_read()
2013-02-18 03:48:21 +01:00
Model\set_item_read($id);
Response\html(Template\layout('read_item', array(
2013-03-27 02:57:12 +01:00
'item' => $item,
'item_nav' => $nav
2013-02-18 03:48:21 +01:00
)));
});
Router\get_action('read_starred', function() {
$id = Model\decode_item_id(Request\param('id'));
$item = Model\get_item($id);
$nav = Model\get_nav_starred_item($item); // must be placed before set_item_read()
Model\set_item_read($id);
Response\html(Template\layout('starred_item', array(
'item' => $item,
'item_nav' => $nav
)));
});
Router\get_action('mark-item-read', function() {
$id = Model\decode_item_id(Request\param('id'));
Model\set_item_read($id);
Response\Redirect('?action=default');
});
Router\get_action('mark-item-unread', function() {
$id = Model\decode_item_id(Request\param('id'));
Model\set_item_unread($id);
Response\Redirect('?action=history');
});
Router\get_action('mark-item-removed', function() {
$id = Model\decode_item_id(Request\param('id'));
Model\set_item_removed($id);
Response\Redirect('?action=history');
});
Router\get_action('mark-starred-item-removed', function() {
$id = Model\decode_item_id(Request\param('id'));
Model\set_item_removed($id);
Response\Redirect('?action=starred');
});
Router\post_action('mark-item-read', function() {
2013-04-03 04:49:14 +02:00
$id = Model\decode_item_id(Request\param('id'));
Model\set_item_read($id);
Response\json(array('Ok'));
});
2013-04-03 04:49:14 +02:00
Router\post_action('mark-item-unread', function() {
$id = Model\decode_item_id(Request\param('id'));
Model\set_item_unread($id);
2013-04-03 04:49:14 +02:00
Response\json(array('Ok'));
});
Router\get_action('mark-item-starred', function() {
$id = Model\decode_item_id(Request\param('id'));
Model\set_item_starred($id);
Response\Redirect('?action=default');
});
Router\get_action('mark-item-unstarred', function() {
$id = Model\decode_item_id(Request\param('id'));
Model\set_item_unstarred($id);
Response\Redirect('?action=starred');
});
Router\post_action('mark-item-starred', function() {
$id = Model\decode_item_id(Request\param('id'));
Model\set_item_starred($id);
Response\json(array('Ok'));
});
Router\post_action('mark-item-unstarred', function() {
$id = Model\decode_item_id(Request\param('id'));
Model\set_item_unstarred($id);
Response\json(array('Ok'));
});
Router\post_action('change-item-status', function() {
2013-04-03 04:49:14 +02:00
$id = Model\decode_item_id(Request\param('id'));
2013-04-03 04:49:14 +02:00
Response\json(array(
'item_id' => urlencode($id),
'status' => Model\switch_item_status($id)
));
});
2013-02-18 03:48:21 +01:00
Router\get_action('history', function() {
2013-05-26 19:09:34 +02:00
Response\html(Template\layout('history', array(
2013-02-18 03:48:21 +01:00
'items' => Model\get_read_items(),
'menu' => 'history'
)));
});
Router\get_action('starred', function() {
Response\html(Template\layout('starred', array(
'items' => Model\get_starred_items(),
'menu' => 'starred'
)));
});
2013-03-17 23:16:25 +01:00
Router\get_action('confirm-remove', function() {
$id = Request\int_param('feed_id');
Response\html(Template\layout('confirm_remove_feed', array(
2013-03-17 23:16:25 +01:00
'feed' => Model\get_feed($id),
'menu' => 'feeds'
)));
});
2013-02-18 03:48:21 +01:00
Router\get_action('remove', function() {
$id = Request\int_param('feed_id');
2013-03-17 23:16:25 +01:00
if ($id && Model\remove_feed($id)) {
2013-04-13 03:08:55 +02:00
Session\flash(t('This subscription has been removed successfully.'));
2013-03-17 23:16:25 +01:00
}
else {
2013-02-18 03:48:21 +01:00
2013-04-13 03:08:55 +02:00
Session\flash_error(t('Unable to remove this subscription.'));
2013-02-18 03:48:21 +01:00
}
Response\redirect('?action=feeds');
});
2013-02-24 20:04:56 +01:00
Router\get_action('refresh-feed', function() {
2013-02-18 03:48:21 +01:00
$id = Request\int_param('feed_id');
if ($id) {
Model\update_feed($id);
}
Response\redirect('?action=unread');
});
Router\post_action('refresh-feed', function() {
2013-02-24 20:04:56 +01:00
$id = Request\int_param('feed_id');
if ($id) {
Response\json(array('feed_id' => $id, 'result' => Model\update_feed($id)));
}
Response\json(array('feed_id' => 0, 'result' => false));
});
2013-04-03 04:49:14 +02:00
Router\get_action('mark-as-read', function() {
2013-02-18 03:48:21 +01:00
2013-04-03 04:49:14 +02:00
Model\mark_as_read();
2013-02-18 03:48:21 +01:00
Response\redirect('?action=unread');
});
2013-04-07 04:21:16 +02:00
Router\get_action('confirm-flush-history', function() {
Response\html(Template\layout('confirm_flush_items', array(
2013-04-07 04:21:16 +02:00
'menu' => 'history'
)));
});
2013-02-18 03:48:21 +01:00
Router\get_action('flush-history', function() {
2013-05-26 19:09:34 +02:00
Model\mark_as_removed();
2013-02-18 03:48:21 +01:00
Response\redirect('?action=history');
});
Router\get_action('refresh-all', function() {
Model\update_feeds();
2013-04-13 03:08:55 +02:00
Session\flash(t('Your subscriptions are updated'));
2013-02-18 03:48:21 +01:00
Response\redirect('?action=unread');
});
Router\get_action('feeds', function() {
Response\html(Template\layout('feeds', array(
'feeds' => Model\get_feeds(),
2013-03-17 23:16:25 +01:00
'nothing_to_read' => Request\int_param('nothing_to_read'),
2013-02-18 03:48:21 +01:00
'menu' => 'feeds'
)));
});
Router\get_action('add', function() {
Response\html(Template\layout('add', array(
'values' => array(),
'errors' => array(),
'menu' => 'feeds'
)));
});
Router\post_action('add', function() {
if (Model\import_feed($_POST['url'])) {
2013-04-13 03:08:55 +02:00
Session\flash(t('Subscription added successfully.'));
2013-02-18 03:48:21 +01:00
Response\redirect('?action=feeds');
}
else {
2013-04-13 03:08:55 +02:00
Session\flash_error(t('Unable to find a subscription.'));
2013-02-18 03:48:21 +01:00
}
Response\html(Template\layout('add', array(
'values' => array('url' => $_POST['url']),
'menu' => 'feeds'
)));
});
2013-03-27 02:57:12 +01:00
Router\get_action('optimize-db', function() {
\PicoTools\singleton('db')->getConnection()->exec('VACUUM');
Response\redirect('?action=config');
});
Router\get_action('download-db', function() {
Response\force_download('db.sqlite.gz');
2013-03-27 02:57:12 +01:00
Response\binary(gzencode(file_get_contents(get_db_filename())));
});
2013-02-18 03:48:21 +01:00
Router\get_action('export', function() {
Response\force_download('feeds.opml');
Response\xml(Model\export_feeds());
});
Router\get_action('import', function() {
Response\html(Template\layout('import', array(
'errors' => array(),
'menu' => 'feeds'
)));
});
Router\post_action('import', function() {
if (Model\import_feeds(Request\file_content('file'))) {
2013-04-13 03:08:55 +02:00
Session\flash(t('Your feeds have been imported.'));
2013-06-01 20:56:20 +02:00
Response\redirect('?action=feeds');
2013-02-18 03:48:21 +01:00
}
else {
2013-04-13 03:08:55 +02:00
Session\flash_error(t('Unable to import your OPML file.'));
2013-06-01 20:56:20 +02:00
Response\redirect('?action=import');
2013-02-18 03:48:21 +01:00
}
});
Router\get_action('config', function() {
Response\html(Template\layout('config', array(
'errors' => array(),
'values' => Model\get_config(),
2013-03-27 02:57:12 +01:00
'db_size' => filesize(get_db_filename()),
2013-04-13 03:08:55 +02:00
'languages' => Model\get_languages(),
2013-05-26 19:09:34 +02:00
'autoflush_options' => Model\get_autoflush_options(),
2013-02-18 03:48:21 +01:00
'menu' => 'config'
)));
});
Router\post_action('config', function() {
$values = Request\values();
list($valid, $errors) = Model\validate_config_update($values);
if ($valid) {
if (Model\save_config($values)) {
2013-04-13 03:08:55 +02:00
Session\flash(t('Your preferences are updated.'));
2013-02-18 03:48:21 +01:00
}
else {
2013-04-13 03:08:55 +02:00
Session\flash_error(t('Unable to update your preferences.'));
2013-02-18 03:48:21 +01:00
}
Response\redirect('?action=config');
}
Response\html(Template\layout('config', array(
'errors' => $errors,
'values' => $values,
2013-04-13 03:08:55 +02:00
'db_size' => filesize(get_db_filename()),
'languages' => Model\get_languages(),
2013-05-26 19:09:34 +02:00
'autoflush_options' => Model\get_autoflush_options(),
2013-02-18 03:48:21 +01:00
'menu' => 'config'
)));
});
Router\notfound(function() {
2013-05-26 19:09:34 +02:00
Model\autoflush();
$items = Model\get_unread_items();
2013-03-17 23:16:25 +01:00
if (empty($items)) {
Response\redirect('?action=feeds&nothing_to_read=1');
}
2013-02-18 03:48:21 +01:00
Response\html(Template\layout('unread_items', array(
2013-03-17 23:16:25 +01:00
'items' => $items,
2013-02-18 03:48:21 +01:00
'menu' => 'unread'
)));
});