miniflux-legacy/app/models/config.php

280 lines
6.3 KiB
PHP
Raw Normal View History

<?php
namespace Model\Config;
use Helper;
2015-01-18 00:53:40 +01:00
use Translator;
2014-10-30 02:28:23 +01:00
use DirectoryIterator;
2014-02-08 20:13:14 +01:00
use PicoDb\Database;
use PicoFeed\Config\Config as ReaderConfig;
use PicoFeed\Logging\Logger;
2014-05-20 20:20:27 +02:00
2015-09-15 00:47:59 +02:00
const HTTP_USER_AGENT = 'Miniflux (https://miniflux.net)';
2014-05-20 20:20:27 +02:00
// Get PicoFeed config
function get_reader_config()
{
$config = new ReaderConfig;
$config->setTimezone(get('timezone'));
// Client
2014-05-20 20:20:27 +02:00
$config->setClientTimeout(HTTP_TIMEOUT);
$config->setClientUserAgent(HTTP_USER_AGENT);
$config->setMaxBodySize(HTTP_MAX_RESPONSE_SIZE);
2015-04-11 02:34:48 +02:00
// Grabber
$config->setGrabberRulesFolder(RULES_DIRECTORY);
// Proxy
2014-05-20 20:20:27 +02:00
$config->setProxyHostname(PROXY_HOSTNAME);
$config->setProxyPort(PROXY_PORT);
$config->setProxyUsername(PROXY_USERNAME);
$config->setProxyPassword(PROXY_PASSWORD);
// Filter
2014-05-20 20:20:27 +02:00
$config->setFilterIframeWhitelist(get_iframe_whitelist());
if ((bool) get('debug_mode')) {
Logger::enable();
}
// Parser
$config->setParserHashAlgo('crc32b');
2014-05-20 20:20:27 +02:00
return $config;
}
function get_iframe_whitelist()
{
return array(
'http://www.youtube.com',
'https://www.youtube.com',
'http://player.vimeo.com',
'https://player.vimeo.com',
'http://www.dailymotion.com',
'https://www.dailymotion.com',
);
}
2014-03-30 21:59:26 +02:00
// Send a debug message to the console
function debug($line)
{
Logger::setMessage($line);
2014-03-30 21:59:26 +02:00
write_debug();
}
// Write PicoFeed debug output to a file
function write_debug()
{
if ((bool) get('debug_mode')) {
file_put_contents(DEBUG_FILENAME, implode(PHP_EOL, Logger::getMessages()));
}
}
2014-02-26 01:03:46 +01:00
// Get available timezone
function get_timezones()
{
2014-10-30 02:28:23 +01:00
$timezones = timezone_identifiers_list();
2014-02-26 01:03:46 +01:00
return array_combine(array_values($timezones), $timezones);
}
// Returns true if the language is RTL
function is_language_rtl()
{
$languages = array(
'ar_AR'
);
return in_array(get('language'), $languages);
}
// Get all supported languages
function get_languages()
{
2014-10-30 02:28:23 +01:00
return array(
'ar_AR' => 'عربي',
2014-10-30 02:28:23 +01:00
'cs_CZ' => 'Čeština',
'de_DE' => 'Deutsch',
'en_US' => 'English',
'es_ES' => 'Español',
'fr_FR' => 'Français',
'it_IT' => 'Italiano',
2016-03-09 02:00:14 +01:00
'ja_JP' => '日本語',
2014-10-30 02:28:23 +01:00
'pt_BR' => 'Português',
'zh_CN' => '简体中国',
'sr_RS' => 'српски',
'sr_RS@latin' => 'srpski',
2015-06-11 15:35:25 +02:00
'ru_RU' => 'Русский',
'tr_TR' => 'Türkçe',
);
}
// Get all skins
function get_themes()
{
$themes = array(
'original' => t('Default')
);
if (file_exists(THEME_DIRECTORY)) {
2014-10-30 02:28:23 +01:00
$dir = new DirectoryIterator(THEME_DIRECTORY);
foreach ($dir as $fileinfo) {
if (! $fileinfo->isDot() && $fileinfo->isDir()) {
$themes[$dir->getFilename()] = ucfirst($dir->getFilename());
}
}
}
return $themes;
}
// Sorting direction choices for items
function get_sorting_directions()
{
return array(
'asc' => t('Older items first'),
'desc' => t('Most recent first'),
);
}
// Display summaries or full contents on lists
function get_display_mode()
{
2016-04-18 01:44:45 +02:00
return array(
'summaries' => t('Summaries'),
'full' => t('Full contents')
);
}
2016-02-27 20:21:18 +01:00
// Item title links to original or full contents
function get_item_title_link()
{
2016-04-18 01:44:45 +02:00
return array(
'original' => t('Original'),
'full' => t('Full contents')
);
2016-02-27 20:21:18 +01:00
}
// Autoflush choices for read items
function get_autoflush_read_options()
{
return array(
'0' => t('Never'),
'-1' => t('Immediately'),
'1' => t('After %d day', 1),
'5' => t('After %d day', 5),
'15' => t('After %d day', 15),
'30' => t('After %d day', 30)
);
}
// Autoflush choices for unread items
function get_autoflush_unread_options()
{
return array(
'0' => t('Never'),
'15' => t('After %d day', 15),
'30' => t('After %d day', 30),
'45' => t('After %d day', 45),
'60' => t('After %d day', 60),
);
}
// Number of items per pages
function get_paging_options()
{
return array(
2015-12-13 03:35:09 +01:00
10 => 10,
20 => 20,
30 => 30,
50 => 50,
100 => 100,
150 => 150,
200 => 200,
250 => 250,
);
}
// Get redirect options when there is nothing to read
function get_nothing_to_read_redirections()
{
return array(
'feeds' => t('Subscriptions'),
'history' => t('History'),
'bookmarks' => t('Bookmarks'),
);
}
// Regenerate tokens for the API and bookmark feed
function new_tokens()
{
$values = array(
'api_token' => Helper\generate_token(),
'feed_token' => Helper\generate_token(),
'bookmarklet_token' => Helper\generate_token(),
'fever_token' => substr(Helper\generate_token(), 0, 8),
);
2015-08-15 03:33:39 +02:00
return Database::getInstance('db')->hashtable('settings')->put($values);
}
// Get a config value from the DB or from the session
function get($name)
{
if (! isset($_SESSION)) {
2015-08-15 03:33:39 +02:00
return current(Database::getInstance('db')->hashtable('settings')->get($name));
2016-04-18 01:44:45 +02:00
} else {
2014-02-26 01:03:46 +01:00
if (! isset($_SESSION['config'][$name])) {
$_SESSION['config'] = get_all();
}
if (isset($_SESSION['config'][$name])) {
return $_SESSION['config'][$name];
}
}
return null;
}
// Get all config parameters
function get_all()
{
2015-08-15 03:33:39 +02:00
$config = Database::getInstance('db')->hashtable('settings')->get();
2014-12-24 23:54:27 +01:00
unset($config['password']);
return $config;
}
// Save config into the database and update the session
function save(array $values)
{
// Update the password if needed
if (! empty($values['password'])) {
2015-01-18 00:53:40 +01:00
$values['password'] = password_hash($values['password'], PASSWORD_BCRYPT);
} else {
unset($values['password']);
}
unset($values['confirmation']);
// If the user does not want content of feeds, remove it in previous ones
if (isset($values['nocontent']) && (bool) $values['nocontent']) {
2015-08-15 03:33:39 +02:00
Database::getInstance('db')->table('items')->update(array('content' => ''));
}
2015-08-15 03:33:39 +02:00
if (Database::getInstance('db')->hashtable('settings')->put($values)) {
2014-12-24 16:47:24 +01:00
reload();
return true;
}
return false;
}
// Reload the cache in session
function reload()
{
$_SESSION['config'] = get_all();
2015-01-18 00:53:40 +01:00
Translator\load(get('language'));
}