diff options
author | Romain d'Alverny <rdalverny@gmail.com> | 2022-01-11 17:57:46 +0100 |
---|---|---|
committer | Romain d'Alverny <rdalverny@gmail.com> | 2022-01-11 17:57:46 +0100 |
commit | 1b7fbc4614b577e14052fea50487ee5d0a301e93 (patch) | |
tree | 30ac8bfd172b4dd1f3a5da9611b452475056eac9 | |
parent | 138865bbde25bb6193930c98e30c33913d19e367 (diff) | |
download | planet-1b7fbc4614b577e14052fea50487ee5d0a301e93.tar planet-1b7fbc4614b577e14052fea50487ee5d0a301e93.tar.gz planet-1b7fbc4614b577e14052fea50487ee5d0a301e93.tar.bz2 planet-1b7fbc4614b577e14052fea50487ee5d0a301e93.tar.xz planet-1b7fbc4614b577e14052fea50487ee5d0a301e93.zip |
Factor more config into PlanetConfig
This moves into PlanetConfig:
* people.opml, cache dir path definition,
* installation check,
* config upgrade when needed,
* responsibility for providing config values
instead of $conf global array).
-rwxr-xr-x | admin/administration.php | 2 | ||||
-rwxr-xr-x | admin/index.php | 2 | ||||
-rwxr-xr-x | admin/subscriptions.php | 10 | ||||
-rwxr-xr-x | app/app.php | 22 | ||||
-rw-r--r-- | app/classes/Planet.php | 2 | ||||
-rw-r--r-- | app/classes/PlanetConfig.php | 66 | ||||
-rw-r--r-- | app/helpers.php | 10 | ||||
-rwxr-xr-x | app/l10n/extract.php | 2 | ||||
-rw-r--r-- | atom.php | 2 | ||||
-rw-r--r-- | cron.php | 4 | ||||
-rwxr-xr-x | custom/views/archive/index.tpl.php | 2 | ||||
-rw-r--r-- | custom/views/default/index.tpl.php | 3 | ||||
-rwxr-xr-x | index.php | 8 | ||||
-rwxr-xr-x | install.php | 2 | ||||
-rw-r--r-- | postload.php | 4 |
15 files changed, 94 insertions, 47 deletions
diff --git a/admin/administration.php b/admin/administration.php index 369f65b..e6236fd 100755 --- a/admin/administration.php +++ b/admin/administration.php @@ -4,7 +4,7 @@ require_once __DIR__ . '/../app/app.php'; require_once __DIR__ . '/inc/auth.inc.php'; -$opml = OpmlManager::load(__DIR__ . '/../custom/people.opml'); +$opml = OpmlManager::load($PlanetConfig->getOpmlFile()); $opml_people = $opml->getPeople(); $page_id = 'admin-admin'; $header_extra = <<<"HTML" diff --git a/admin/index.php b/admin/index.php index 3a9e259..43cd5af 100755 --- a/admin/index.php +++ b/admin/index.php @@ -17,7 +17,7 @@ if (is_file($config_file)) { $Planet = new Planet($PlanetConfig); //Load -if (0 < $Planet->loadOpml(__DIR__ . '/../custom/people.opml')) { +if (0 < $Planet->loadOpml($PlanetConfig->getOpmlFile())) { $Planet->loadFeeds(); $items = $Planet->getItems(); } diff --git a/admin/subscriptions.php b/admin/subscriptions.php index 313e5bd..76980f4 100755 --- a/admin/subscriptions.php +++ b/admin/subscriptions.php @@ -12,9 +12,11 @@ if (!$csrf->verify($_POST['_csrf'], 'feedmanage')) { die('Invalid CSRF token!'); } +$opmlFile = $PlanetConfig->getOpmlFile(); + if (isset($_POST['opml']) || isset($_POST['add'])) { // Load old OPML - $oldOpml = OpmlManager::load(__DIR__.'/../custom/people.opml'); + $oldOpml = OpmlManager::load($opmlFile); if ($PlanetConfig->getName() === '') { $PlanetConfig->setName($oldOpml->getTitle()); } @@ -46,7 +48,7 @@ if (isset($_POST['opml']) || isset($_POST['add'])) { $feed = new SimplePie(); $feed->enable_cache(false); $feed->set_feed_url($_POST['url']); - if ($conf['checkcerts'] === false) { + if (!$PlanetConfig->checkCertificates()) { $feed->set_curl_options([ CURLOPT_SSL_VERIFYHOST => false, CURLOPT_SSL_VERIFYPEER => false @@ -65,10 +67,10 @@ if (isset($_POST['opml']) || isset($_POST['add'])) { } // Backup old OPML - OpmlManager::backup(__DIR__.'/../custom/people.opml'); + OpmlManager::backup($opmlFile); // Save new OPML - OpmlManager::save($newOpml, __DIR__.'/../custom/people.opml'); + OpmlManager::save($newOpml, $opmlFile); } header("Location: index.php"); die(); diff --git a/app/app.php b/app/app.php index 4ae1947..3d69292 100755 --- a/app/app.php +++ b/app/app.php @@ -4,28 +4,16 @@ error_reporting(0); require_once __DIR__.'/../vendor/autoload.php'; -$savedConfig = __DIR__.'/../custom/config.yml'; $moon_version = trim(file_get_contents(__DIR__.'/../VERSION')); session_start(); -if (is_installed()) { - $conf = Spyc::YAMLLoad($savedConfig); +$PlanetConfig = PlanetConfig::load(__DIR__); +$Planet = new Planet($PlanetConfig); - // this is a check to upgrade older config file without l10n - if (!isset($conf['locale'])) { - $resetPlanetConfig = new PlanetConfig($conf); - file_put_contents($savedConfig, $resetPlanetConfig->toYaml()); - $conf = Spyc::YAMLLoad($savedConfig); - } - - $PlanetConfig = new PlanetConfig($conf); - $Planet = new Planet($PlanetConfig); - - if ($conf['debug']) { - error_reporting(E_ALL); - } +if ($PlanetConfig->getDebug()) { + error_reporting(E_ALL); } -$l10n = new Simplel10n($conf['locale']); +$l10n = new Simplel10n($PlanetConfig->getLocale()); $csrf = new CSRF(); diff --git a/app/classes/Planet.php b/app/classes/Planet.php index 898126f..dc540bb 100644 --- a/app/classes/Planet.php +++ b/app/classes/Planet.php @@ -114,7 +114,7 @@ class Planet * @param string $file File to load the OPML from. * @return integer Number of people loaded. */ - public function loadOpml($file) + public function loadOpml(string $file) { if (!is_file($file)) { $this->errors[] = new PlanetError(3, $file.' is missing.'); diff --git a/app/classes/PlanetConfig.php b/app/classes/PlanetConfig.php index 257d1b9..179e25b 100644 --- a/app/classes/PlanetConfig.php +++ b/app/classes/PlanetConfig.php @@ -8,6 +8,9 @@ class PlanetConfig protected $conf = []; + private ?string $opmlFile = null; + private ?string $cacheDir = null; + public static $defaultConfig = [ 'url' => 'http://www.example.com/', 'name' => '', @@ -36,6 +39,43 @@ class PlanetConfig } /** + * @return static + */ + public static function load(string $dir) + { + $config = new PlanetConfig; + $configFile = realpath($dir . '/../custom/config.yml'); + + if (self::isInstalled()) { + $conf = Spyc::YAMLLoad($configFile); + + // this is a check to upgrade older config file without l10n + if (!isset($conf['locale'])) { + $resetPlanetConfig = new PlanetConfig($conf); + file_put_contents($configFile, $resetPlanetConfig->toYaml()); + $conf = Spyc::YAMLLoad($configFile); + + return $resetPlanetConfig; + } + + $config = new PlanetConfig($conf); + } + + return $config; + } + + /** + * Is moonmoon installed? + * + * @return bool + */ + public static function isInstalled() : bool + { + return file_exists(custom_path('config.yml')) && + file_exists(custom_path('people.opml')); + } + + /** * Merge the configuration of the user in the default one. * * @param array $default @@ -67,6 +107,22 @@ class PlanetConfig return $this->conf['refresh']; } + public function getCacheDir() + { + if (is_null($this->cacheDir)) { + $this->cacheDir = realpath(__DIR__ . '/../../'.$this->conf['cachedir']); + } + return $this->cacheDir; + } + + public function getOpmlFile() + { + if (is_null($this->opmlFile)) { + $this->opmlFile = realpath(__DIR__ . '/../../custom/people.opml'); + } + return $this->opmlFile; + } + public function getOutputTimeout() { return $this->conf['cache']; @@ -127,6 +183,16 @@ class PlanetConfig return $this->conf['debug']; } + public function checkCertificates() + { + return $this->conf['checkcerts']; + } + + public function getLocale() + { + return $this->conf['locale']; + } + /** * @return array */ diff --git a/app/helpers.php b/app/helpers.php index f3a9dfa..3bb0c1d 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -47,16 +47,6 @@ function admin_path($file = '') } /** - * Is moonmoon installed? - * - * @return bool - */ -function is_installed() -{ - return file_exists(custom_path('config.yml')) && file_exists(custom_path('people.opml')); -} - -/** * Shortcut to Simplel10n::getString(). * * @param string $str diff --git a/app/l10n/extract.php b/app/l10n/extract.php index ef44912..c6e3acf 100755 --- a/app/l10n/extract.php +++ b/app/l10n/extract.php @@ -17,7 +17,7 @@ $root = __DIR__ . '/../../'; require_once __DIR__ . '/../app.php'; -if ($conf['debug'] !== true) { +if (!$PlanetConfig->getDebug()) { die('Please enable the debug mode to use extract.php.'); } @@ -2,7 +2,7 @@ include_once(__DIR__.'/app/app.php'); include_once(__DIR__.'/app/classes/Cache.php'); -if ($Planet->loadOpml(__DIR__.'/custom/people.opml') == 0) { +if ($Planet->loadOpml($PlanetConfig->getOpmlFile()) == 0) { exit; } @@ -2,11 +2,11 @@ include_once(__DIR__.'/app/app.php'); //Load OPML -if (0 < $Planet->loadOpml(__DIR__.'/custom/people.opml')) { +if (0 < $Planet->loadOpml($PlanetConfig->getOpmlFile())) { $Planet->download(1.0); } -if ($conf['debug'] === true) { +if ($PlanetConfig->getDebug()) { foreach ($Planet->errors as $error) { echo $error->toString() . "\n"; } diff --git a/custom/views/archive/index.tpl.php b/custom/views/archive/index.tpl.php index 7dd2737..679ef71 100755 --- a/custom/views/archive/index.tpl.php +++ b/custom/views/archive/index.tpl.php @@ -22,7 +22,7 @@ foreach ($items as $item) { header('Content-type: text/html; charset=UTF-8'); ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> -<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?=$conf['locale']?>" lang="<?=$conf['locale']?>"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?=$PlanetConfig->getLocale()?>" lang="<?=$PlanetConfig->getLocale()?>"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> diff --git a/custom/views/default/index.tpl.php b/custom/views/default/index.tpl.php index b3fa1e0..5e68161 100644 --- a/custom/views/default/index.tpl.php +++ b/custom/views/default/index.tpl.php @@ -5,8 +5,9 @@ $count = 0; header('Content-type: text/html; charset=UTF-8'); ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> -<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?=$conf['locale']?>" lang="<?=$conf['locale']?>"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?=$PlanetConfig->getLocale()?>" lang="<?=$PlanetConfig->getLocale()?>"> <head> + <meta charset="utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <meta http-equiv="Content-Style-Type" content="text/css" /> @@ -3,14 +3,14 @@ include_once(__DIR__.'/app/app.php'); include_once(__DIR__.'/app/classes/Cache.php'); //Installed ? -if (!isset($Planet)) { +if (!$PlanetConfig::isInstalled()) { echo '<p>' . _g('You might want to <a href="install.php">install moonmoon</a>.') . '</p>'; exit; } //Load from cache $items = array(); -if (0 < $Planet->loadOpml(__DIR__.'/custom/people.opml')) { +if (0 < $Planet->loadOpml($PlanetConfig->getOpmlFile())) { $Planet->loadFeeds(); $items = $Planet->getItems(); } @@ -21,7 +21,7 @@ $cache_key = (count($items)) ? $items[0]->get_id() : ''; $last_modified = (count($items)) ? $items[0]->get_date() : ''; $cache_duration = $PlanetConfig->getOutputTimeout()*60; -Cache::setStore(__DIR__ . '/' . $conf['cachedir'] . '/'); +Cache::setStore($PlanetConfig->getCacheDir()); if (isset($_GET['type']) && $_GET['type'] == 'atom10') { /* XXX: Redirect old ATOM feeds to new url to make sure our users don't @@ -45,7 +45,7 @@ if (!OutputCache::Start($_GET['type'], $cache_key, $cache_duration)) { OutputCache::End(); } -if ($conf['debug'] === true) { +if ($PlanetConfig->getDebug()) { echo "<!-- \$Planet->errors:\n"; var_dump($Planet->errors); echo "-->"; diff --git a/install.php b/install.php index 5dc20bc..4294beb 100755 --- a/install.php +++ b/install.php @@ -10,7 +10,7 @@ function installStatus($str, $msg, $result) } // If the config file exists and the auth variables are set, moonmoon is already installed -if (is_installed()) { +if ($PlanetConfig::isInstalled()) { $status = 'installed'; } elseif (isset($_POST['url'])) { // Do no try to use the file of an invalid locale diff --git a/postload.php b/postload.php index f0f10fd..0dff886 100644 --- a/postload.php +++ b/postload.php @@ -2,11 +2,11 @@ require_once __DIR__.'/app/app.php'; -if (!is_installed()) { +if (!$PlanetConfig::isInstalled()) { die(); } -$xml = new SimpleXMLElement(file_get_contents(custom_path('people.opml'))); +$xml = new SimpleXMLElement(file_get_contents($PlanetConfig->getOpmlFile())); foreach ($xml->xpath('/opml/body/outline[@xmlUrl]') as $element) { if ($element->attributes()->xmlUrl == $_GET['url']) { |