From 004fcbbcc7609ca83807f2e38967ef54f469bf72 Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Sat, 23 Jul 2016 21:24:54 +0200 Subject: Move to new directory structure --- lib/Config/AppConfig.php | 137 ++++++++++++++++++++++++++++ lib/Config/Config.php | 181 +++++++++++++++++++++++++++++++++++++ lib/Config/DependencyException.php | 28 ++++++ 3 files changed, 346 insertions(+) create mode 100644 lib/Config/AppConfig.php create mode 100644 lib/Config/Config.php create mode 100644 lib/Config/DependencyException.php (limited to 'lib/Config') diff --git a/lib/Config/AppConfig.php b/lib/Config/AppConfig.php new file mode 100644 index 000000000..55dcd6d1a --- /dev/null +++ b/lib/Config/AppConfig.php @@ -0,0 +1,137 @@ + + * @author Bernhard Posselt + * @copyright Alessandro Cosentino 2012 + * @copyright Bernhard Posselt 2012, 2014 + */ + +namespace OCA\News\Config; + +use SimpleXMLElement; + +use OCP\INavigationManager; +use OCP\IURLGenerator; +use OCP\Util; +use OCP\App; + +// Used to parse app.json file, should be in core at some point +class AppConfig { + + private $config; + private $navigationManager; + private $urlGenerator; + + /** + * TODO: External deps that are needed: + * - add jobs + * - connect to hooks + */ + public function __construct(INavigationManager $navigationManager, + IURLGenerator $urlGenerator) { + $this->navigationManager = $navigationManager; + $this->urlGenerator = $urlGenerator; + $this->config = []; + } + + + /** + * Parse an xml config + */ + private function parseConfig($string) { + // no need to worry about XXE since local file + $xml = simplexml_load_string($string, 'SimpleXMLElement'); + return json_decode(json_encode((array)$xml), true); + } + + + /** + * @param string|array $data path to the config file or an array with the + * config + */ + public function loadConfig($data) { + if(is_array($data)) { + $this->config = $data; + } else { + $this->config = $this->parseConfig($data); + } + } + + + /** + * @param string $key if given returns the value of the config at index $key + * @return array|mixed the config + */ + public function getConfig($key=null) { + // FIXME: is this function interface a good idea? + if($key !== null) { + return $this->config[$key]; + } else { + return $this->config; + } + } + + + /** + * Registers all config options + */ + public function registerAll() { + $this->registerNavigation(); + $this->registerHooks(); + // IJob API is fucked up, so silence the code checker + $class = '\OCP\BackgroundJob'; + $class::addRegularTask($this->config['cron']['job'], 'run'); + App::registerAdmin($this->config['id'], $this->config['admin']); + } + + + /** + * Parses the navigation and creates a navigation entry if needed + */ + public function registerNavigation() { + if (array_key_exists('navigation', $this->config)) { + $this->navigationManager->add(function () { + $nav =& $this->config['navigation']; + + $navConfig = [ + 'id' => $this->config['id'], + 'order' => $nav['order'], + 'name' => $nav['name'] + ]; + + $navConfig['href'] = $this->urlGenerator->linkToRoute( + $nav['route'] + ); + $navConfig['icon'] = $this->urlGenerator->imagePath( + $this->config['id'], $nav['icon'] + ); + + return $navConfig; + }); + } + } + + + /** + * Registers all hooks in the config + */ + public function registerHooks() { + // FIXME: this is temporarily static because core emitters are not + // future proof, therefore legacy code in here + foreach ($this->config['hooks'] as $hook) { + $listener = explode('::', $hook['channel']); + $reaction = explode('::', $hook['subscriber']); + + // config is written like HookNamespace::method => Class::method + Util::connectHook($listener[0], $listener[1], $reaction[0], + $reaction[1]); + } + } + + +} diff --git a/lib/Config/Config.php b/lib/Config/Config.php new file mode 100644 index 000000000..a91c5053f --- /dev/null +++ b/lib/Config/Config.php @@ -0,0 +1,181 @@ + + * @author Bernhard Posselt + * @copyright Alessandro Cosentino 2012 + * @copyright Bernhard Posselt 2012, 2014 + */ + +namespace OCA\News\Config; + +use OCP\ILogger; +use OCP\Files\Folder; + + +class Config { + + private $fileSystem; + private $autoPurgeMinimumInterval; // seconds, used to define how + // long deleted folders and feeds + // should still be kept for an + // undo actions + private $autoPurgeCount; // number of allowed unread articles per feed + private $maxRedirects; // seconds + private $feedFetcherTimeout; // seconds + private $useCronUpdates; // turn off updates run by owncloud cronjob + private $logger; + private $loggerParams; + private $maxSize; + private $exploreUrl; + + public function __construct(Folder $fileSystem, + ILogger $logger, + $LoggerParameters) { + $this->fileSystem = $fileSystem; + $this->autoPurgeMinimumInterval = 60; + $this->autoPurgeCount = 200; + $this->maxRedirects = 10; + $this->maxSize = 100*1024*1024; // 100Mb + $this->feedFetcherTimeout = 60; + $this->useCronUpdates = true; + $this->logger = $logger; + $this->exploreUrl = ''; + $this->loggerParams = $LoggerParameters; + } + + public function getAutoPurgeMinimumInterval() { + if ($this->autoPurgeMinimumInterval > 60) { + return $this->autoPurgeMinimumInterval; + } else { + return 60; + } + } + + public function getAutoPurgeCount() { + return $this->autoPurgeCount; + } + + + public function getMaxRedirects() { + return $this->maxRedirects; + } + + + public function getFeedFetcherTimeout() { + return $this->feedFetcherTimeout; + } + + + public function getUseCronUpdates() { + return $this->useCronUpdates; + } + + + public function getMaxSize() { + return $this->maxSize; + } + + + public function getExploreUrl() { + return $this->exploreUrl; + } + + + public function setAutoPurgeMinimumInterval($value) { + $this->autoPurgeMinimumInterval = $value; + } + + + public function setAutoPurgeCount($value) { + $this->autoPurgeCount = $value; + } + + + public function setMaxRedirects($value) { + $this->maxRedirects = $value; + } + + + public function setFeedFetcherTimeout($value) { + $this->feedFetcherTimeout = $value; + } + + + public function setUseCronUpdates($value) { + $this->useCronUpdates = $value; + } + + public function setMaxSize($value) { + $this->maxSize = $value; + } + + + public function setExploreUrl($value) { + $this->exploreUrl = $value; + } + + + public function read($configPath, $createIfNotExists=false) { + if($createIfNotExists && !$this->fileSystem->nodeExists($configPath)) { + $this->fileSystem->newFile($configPath); + $this->write($configPath); + + } else { + + $content = $this->fileSystem->get($configPath)->getContent(); + $configValues = parse_ini_string($content); + + if($configValues === false || count($configValues) === 0) { + $this->logger->warning( + 'Configuration invalid. Ignoring values.', + $this->loggerParams + ); + } else { + + foreach($configValues as $key => $value) { + if(property_exists($this, $key)) { + $type = gettype($this->$key); + settype($value, $type); + $this->$key = $value; + } else { + $this->logger->warning( + 'Configuration value "' . $key . + '" does not exist. Ignored value.' , + $this->loggerParams + ); + } + } + + } + } + } + + + public function write($configPath) { + $ini = + 'autoPurgeMinimumInterval = ' . + $this->autoPurgeMinimumInterval . "\n" . + 'autoPurgeCount = ' . + $this->autoPurgeCount . "\n" . + 'maxRedirects = ' . + $this->maxRedirects . "\n" . + 'maxSize = ' . + $this->maxSize . "\n" . + 'exploreUrl = ' . + $this->exploreUrl . "\n" . + 'feedFetcherTimeout = ' . + $this->feedFetcherTimeout . "\n" . + 'useCronUpdates = ' . + var_export($this->useCronUpdates, true); + ; + + $this->fileSystem->get($configPath)->putContent($ini); + } + + +} diff --git a/lib/Config/DependencyException.php b/lib/Config/DependencyException.php new file mode 100644 index 000000000..690d187c3 --- /dev/null +++ b/lib/Config/DependencyException.php @@ -0,0 +1,28 @@ + + * @author Bernhard Posselt + * @copyright Alessandro Cosentino 2012 + * @copyright Bernhard Posselt 2012, 2014 + */ + +namespace OCA\News\Config; + +class DependencyException extends \Exception { + + + /** + * Constructor + * @param string $msg the error message + */ + public function __construct($msg){ + parent::__construct($msg); + } + + +} \ No newline at end of file -- cgit v1.2.3