diff --git a/lib/CLI.php b/lib/CLI.php index 7422c85..895fc36 100644 --- a/lib/CLI.php +++ b/lib/CLI.php @@ -12,12 +12,13 @@ Usage: $prog daemon $prog feed refresh $prog conf save-defaults + $prog user add [] $prog --version $prog --help | -h The Arsse command-line interface currently allows you to start the refresh -daemon, refresh a specific feed by numeric ID, or save default configuration -to a sample file. +daemon, refresh a specific feed by numeric ID, add a user, or save default +configuration to a sample file. USAGE_TEXT; } @@ -38,6 +39,8 @@ USAGE_TEXT; if(file_exists(BASE."config.php")) { Arsse::$conf->importFile(BASE."config.php"); } + // command-line operations will never respect authorization + Arsse::$user->authorizationEnabled(false); return true; } @@ -46,27 +49,47 @@ USAGE_TEXT; if(is_null($args)) { $args = $this->args; } - if($args['daemon']) { + if($this->command("daemon", $args)) { $this->loadConf(); return $this->daemon(); - } else if($args['feed'] && $args['refresh']) { + } else if($this->command("feed refresh", $args)) { $this->loadConf(); return $this->feedRefresh((int) $args['']); - } else if($args['conf'] && $args['save-defaults']) { + } else if($this->command("conf save-defaults", $args)) { return $this->confSaveDefaults($args['']); + } else if($this->command("user add", $args)) { + $this->loadConf(); + return $this->userAdd($args[''], $args['']); + } + } + + protected function command($cmd, $args): bool { + foreach(explode(" ", $cmd) as $part) { + if(!$args[$part]) { + return false; + } } + return true; } - protected function daemon(bool $loop = true): int { + function daemon(bool $loop = true): int { (new Service)->watch($loop); return 0; // FIXME: should return the exception code of thrown exceptions } - protected function feedRefresh(int $id): int { + function feedRefresh(int $id): int { return (int) !Arsse::$db->feedUpdate($id); // FIXME: exception error codes should be returned here } - protected function confSaveDefaults($file): int { + function confSaveDefaults(string $file): int { return (int) !(new Conf)->exportFile($file, true); } + + function userAdd(string $user, string $password = null): int { + $passwd = Arsse::$user->add($user, $password); + if(is_null($password)) { + echo $passwd; + } + return 0; + } } \ No newline at end of file diff --git a/lib/Conf.php b/lib/Conf.php index ff176b0..a71d49c 100644 --- a/lib/Conf.php +++ b/lib/Conf.php @@ -15,8 +15,8 @@ class Conf { public $dbDriver = Db\SQLite3\Driver::class; /** @var boolean Whether to attempt to automatically update the database when updated to a new version with schema changes */ public $dbAutoUpdate = true; - /** @var string Full path and file name of SQLite database (if using SQLite) */ - public $dbSQLite3File = BASE."arsse.db"; + /** @var string|null Full path and file name of SQLite database (if using SQLite) */ + public $dbSQLite3File = null; /** @var string Encryption key to use for SQLite database (if using a version of SQLite with SEE) */ public $dbSQLite3Key = ""; /** @var integer Number of seconds for SQLite to wait before returning a timeout error when writing to the database */ diff --git a/lib/Db/SQLite3/Driver.php b/lib/Db/SQLite3/Driver.php index b6fb02d..a4f2642 100644 --- a/lib/Db/SQLite3/Driver.php +++ b/lib/Db/SQLite3/Driver.php @@ -22,6 +22,10 @@ class Driver extends \JKingWeb\Arsse\Db\AbstractDriver { throw new Exception("extMissing", self::driverName()); // @codeCoverageIgnore } $dbFile = Arsse::$conf->dbSQLite3File; + if(is_null($dbFile)) { + // if no database file is specified in the configuration, use a suitable default + $dbFile = \JKingWeb\Arsse\BASE."arsse.db"; + } $mode = \SQLITE3_OPEN_READWRITE | \SQLITE3_OPEN_CREATE; $timeout = Arsse::$conf->dbSQLite3Timeout * 1000; try {