2017-07-21 21:15:43 +00:00
|
|
|
<?php
|
2017-11-17 01:23:18 +00:00
|
|
|
/** @license MIT
|
|
|
|
* Copyright 2017 J. King, Dustin Wilson et al.
|
|
|
|
* See LICENSE and AUTHORS files for details */
|
|
|
|
|
2017-07-21 21:15:43 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
namespace JKingWeb\Arsse;
|
|
|
|
|
2019-03-25 14:45:05 +00:00
|
|
|
use JKingWeb\Arsse\REST\Fever\User as Fever;
|
2019-04-01 21:24:19 +00:00
|
|
|
use JKingWeb\Arsse\ImportExport\OPML;
|
2021-02-11 02:40:51 +00:00
|
|
|
use JKingWeb\Arsse\REST\Miniflux\Token as Miniflux;
|
2021-06-15 20:58:54 +00:00
|
|
|
use JKingWeb\Arsse\Service\Daemon;
|
2018-11-05 14:08:50 +00:00
|
|
|
|
2017-07-21 21:15:43 +00:00
|
|
|
class CLI {
|
2020-03-01 23:32:01 +00:00
|
|
|
public const USAGE = <<<USAGE_TEXT
|
2017-07-21 21:15:43 +00:00
|
|
|
Usage:
|
2018-11-06 17:32:28 +00:00
|
|
|
arsse.php user [list]
|
2021-02-10 16:24:01 +00:00
|
|
|
arsse.php user add <username> [<password>] [--admin]
|
2018-11-06 17:32:28 +00:00
|
|
|
arsse.php user remove <username>
|
2021-02-10 16:24:01 +00:00
|
|
|
arsse.php user show <username>
|
|
|
|
arsse.php user set <username> <property> <value>
|
|
|
|
arsse.php user unset <username> <property>
|
2021-05-30 01:48:02 +00:00
|
|
|
arsse.php user set-pass <username> [<password>] [--fever]
|
|
|
|
arsse.php user unset-pass <username> [--fever]
|
2019-03-25 14:45:05 +00:00
|
|
|
arsse.php user auth <username> <password> [--fever]
|
2021-02-11 02:40:51 +00:00
|
|
|
arsse.php token list <username>
|
|
|
|
arsse.php token create <username> [<label>]
|
|
|
|
arsse.php token revoke <username> [<token>]
|
2021-05-30 01:48:02 +00:00
|
|
|
arsse.php import <username> [<file>] [-f|--flat] [-r|--replace]
|
|
|
|
arsse.php export <username> [<file>] [-f|--flat]
|
2021-06-06 19:20:53 +00:00
|
|
|
arsse.php daemon [--fork=PIDFILE]
|
2021-05-30 01:48:02 +00:00
|
|
|
arsse.php feed refresh-all
|
|
|
|
arsse.php feed refresh <n>
|
|
|
|
arsse.php conf save-defaults [<file>]
|
2018-11-06 17:32:28 +00:00
|
|
|
arsse.php --version
|
2021-05-30 01:48:02 +00:00
|
|
|
arsse.php -h|--help
|
2019-05-06 04:02:59 +00:00
|
|
|
|
|
|
|
The Arsse command-line interface can be used to perform various administrative
|
|
|
|
tasks such as starting the newsfeed refresh service, managing users, and
|
|
|
|
importing or exporting data.
|
|
|
|
|
2021-05-30 01:48:02 +00:00
|
|
|
See the manual page for more details:
|
2021-02-10 16:24:01 +00:00
|
|
|
|
2021-06-15 20:58:54 +00:00
|
|
|
man arsse
|
2017-07-21 21:15:43 +00:00
|
|
|
USAGE_TEXT;
|
|
|
|
|
2018-11-06 17:32:28 +00:00
|
|
|
protected function usage($prog): string {
|
|
|
|
$prog = basename($prog);
|
|
|
|
return str_replace("arsse.php", $prog, self::USAGE);
|
2017-07-21 21:15:43 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 17:46:28 +00:00
|
|
|
protected function command($args): string {
|
|
|
|
$out = [];
|
|
|
|
foreach ($args as $k => $v) {
|
|
|
|
if (preg_match("/^[a-z]/", $k) && $v === true) {
|
|
|
|
$out[] = $k;
|
2018-11-05 14:08:50 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-10 17:46:28 +00:00
|
|
|
return implode(" ", $out);
|
2018-11-05 14:08:50 +00:00
|
|
|
}
|
|
|
|
|
2019-12-27 14:00:22 +00:00
|
|
|
/** @codeCoverageIgnore */
|
2017-08-20 03:56:32 +00:00
|
|
|
protected function loadConf(): bool {
|
2021-07-05 02:04:05 +00:00
|
|
|
Arsse::bootstrap();
|
2017-08-20 03:56:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-01 21:24:19 +00:00
|
|
|
protected function resolveFile($file, string $mode): string {
|
|
|
|
// TODO: checking read/write permissions on the provided path may be useful
|
|
|
|
$stdinOrStdout = in_array($mode, ["r", "r+"]) ? "php://input" : "php://output";
|
|
|
|
return ($file === "-" ? null : $file) ?? $stdinOrStdout;
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:34:03 +00:00
|
|
|
public function dispatch(array $argv = null): int {
|
2018-11-06 17:32:28 +00:00
|
|
|
$argv = $argv ?? $_SERVER['argv'];
|
|
|
|
$argv0 = array_shift($argv);
|
|
|
|
$args = \Docopt::handle($this->usage($argv0), [
|
|
|
|
'argv' => $argv,
|
|
|
|
'help' => false,
|
|
|
|
]);
|
2018-11-05 14:08:50 +00:00
|
|
|
try {
|
2021-07-03 16:38:48 +00:00
|
|
|
// ensure the require extensions are loaded
|
|
|
|
Arsse::checkExtensions(...Arsse::REQUIRED_EXTENSIONS);
|
|
|
|
// reconstitute multi-token commands (e.g. user add) into a single string
|
2021-02-10 17:46:28 +00:00
|
|
|
$cmd = $this->command($args);
|
2021-06-06 19:20:53 +00:00
|
|
|
if ($cmd && !in_array($cmd, ["", "conf save-defaults", "daemon"])) {
|
2021-06-06 20:38:11 +00:00
|
|
|
// only certain commands don't require configuration to be loaded; daemon loads configuration after forking (if applicable)
|
2019-04-01 21:24:19 +00:00
|
|
|
$this->loadConf();
|
|
|
|
}
|
2021-07-03 16:38:48 +00:00
|
|
|
// run the requested command
|
2019-04-01 21:24:19 +00:00
|
|
|
switch ($cmd) {
|
2021-02-10 17:46:28 +00:00
|
|
|
case "":
|
|
|
|
if ($args['--version']) {
|
|
|
|
echo Arsse::VERSION.\PHP_EOL;
|
|
|
|
} elseif ($args['--help'] || $args['-h']) {
|
|
|
|
echo $this->usage($argv0).\PHP_EOL;
|
|
|
|
}
|
2018-11-06 17:32:28 +00:00
|
|
|
return 0;
|
2018-11-05 14:08:50 +00:00
|
|
|
case "daemon":
|
2021-06-06 19:20:53 +00:00
|
|
|
if ($args['--fork'] !== null) {
|
2021-06-15 20:58:54 +00:00
|
|
|
return $this->serviceFork($args['--fork']);
|
|
|
|
} else {
|
|
|
|
$this->loadConf();
|
|
|
|
Arsse::$obj->get(Service::class)->watch(true);
|
2021-06-06 22:54:24 +00:00
|
|
|
}
|
2018-11-06 17:32:28 +00:00
|
|
|
return 0;
|
2018-11-05 14:08:50 +00:00
|
|
|
case "feed refresh":
|
2022-12-30 17:41:12 +00:00
|
|
|
return (int) !Arsse::$db->subscriptionUpdate(null, (int) $args['<n>'], true);
|
2019-03-02 19:59:44 +00:00
|
|
|
case "feed refresh-all":
|
2021-02-07 04:51:23 +00:00
|
|
|
Arsse::$obj->get(Service::class)->watch(false);
|
2019-03-02 19:59:44 +00:00
|
|
|
return 0;
|
2018-11-05 14:08:50 +00:00
|
|
|
case "conf save-defaults":
|
2019-04-01 21:24:19 +00:00
|
|
|
$file = $this->resolveFile($args['<file>'], "w");
|
2021-02-07 04:51:23 +00:00
|
|
|
return (int) !Arsse::$obj->get(Conf::class)->exportFile($file, true);
|
2019-04-01 21:24:19 +00:00
|
|
|
case "export":
|
|
|
|
$u = $args['<username>'];
|
|
|
|
$file = $this->resolveFile($args['<file>'], "w");
|
2021-02-07 04:51:23 +00:00
|
|
|
return (int) !Arsse::$obj->get(OPML::class)->exportFile($file, $u, ($args['--flat'] || $args['-f']));
|
2019-05-01 14:46:44 +00:00
|
|
|
case "import":
|
|
|
|
$u = $args['<username>'];
|
2019-07-25 19:45:18 +00:00
|
|
|
$file = $this->resolveFile($args['<file>'], "r");
|
2021-02-07 04:51:23 +00:00
|
|
|
return (int) !Arsse::$obj->get(OPML::class)->importFile($file, $u, ($args['--flat'] || $args['-f']), ($args['--replace'] || $args['-r']));
|
2021-02-11 02:40:51 +00:00
|
|
|
case "token list":
|
|
|
|
case "list token": // command reconstruction yields this order for "token list" command
|
|
|
|
return $this->tokenList($args['<username>']);
|
|
|
|
case "token create":
|
|
|
|
echo Arsse::$obj->get(Miniflux::class)->tokenGenerate($args['<username>'], $args['<label>']).\PHP_EOL;
|
|
|
|
return 0;
|
|
|
|
case "token revoke":
|
|
|
|
Arsse::$db->tokenRevoke($args['<username>'], "miniflux.login", $args['<token>']);
|
|
|
|
return 0;
|
2021-02-10 17:46:28 +00:00
|
|
|
case "user add":
|
|
|
|
$out = $this->userAddOrSetPassword("add", $args["<username>"], $args["<password>"]);
|
|
|
|
if ($args['--admin']) {
|
|
|
|
Arsse::$user->propertiesSet($args["<username>"], ['admin' => true]);
|
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
case "user set-pass":
|
|
|
|
if ($args['--fever']) {
|
|
|
|
$passwd = Arsse::$obj->get(Fever::class)->register($args["<username>"], $args["<password>"]);
|
|
|
|
if (is_null($args["<password>"])) {
|
|
|
|
echo $passwd.\PHP_EOL;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
} else {
|
2021-06-06 23:27:46 +00:00
|
|
|
return $this->userAddOrSetPassword("passwordSet", $args["<username>"], $args["<password>"]);
|
2021-02-10 17:46:28 +00:00
|
|
|
}
|
|
|
|
// no break
|
|
|
|
case "user unset-pass":
|
|
|
|
if ($args['--fever']) {
|
|
|
|
Arsse::$obj->get(Fever::class)->unregister($args["<username>"]);
|
|
|
|
} else {
|
2021-06-06 23:27:46 +00:00
|
|
|
Arsse::$user->passwordUnset($args["<username>"]);
|
2021-02-10 17:46:28 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
case "user remove":
|
|
|
|
return (int) !Arsse::$user->remove($args["<username>"]);
|
|
|
|
case "user show":
|
|
|
|
return $this->userShowProperties($args["<username>"]);
|
|
|
|
case "user set":
|
|
|
|
return (int) !Arsse::$user->propertiesSet($args["<username>"], [$args["<property>"] => $args["<value>"]]);
|
|
|
|
case "user unset":
|
|
|
|
return (int) !Arsse::$user->propertiesSet($args["<username>"], [$args["<property>"] => null]);
|
|
|
|
case "user auth":
|
|
|
|
return $this->userAuthenticate($args["<username>"], $args["<password>"], $args["--fever"]);
|
|
|
|
case "user list":
|
|
|
|
case "user":
|
|
|
|
return $this->userList();
|
2021-02-11 02:40:51 +00:00
|
|
|
default:
|
|
|
|
throw new Exception("constantUnknown", $cmd); // @codeCoverageIgnore
|
2017-08-28 23:38:58 +00:00
|
|
|
}
|
2018-11-05 14:08:50 +00:00
|
|
|
} catch (AbstractException $e) {
|
2018-12-08 01:03:04 +00:00
|
|
|
$this->logError($e->getMessage());
|
2018-11-05 14:08:50 +00:00
|
|
|
return $e->getCode();
|
2017-07-21 21:15:43 +00:00
|
|
|
}
|
2019-10-17 17:00:56 +00:00
|
|
|
} // @codeCoverageIgnore
|
2017-07-21 21:15:43 +00:00
|
|
|
|
2018-12-08 01:03:04 +00:00
|
|
|
/** @codeCoverageIgnore */
|
2020-01-20 18:34:03 +00:00
|
|
|
protected function logError(string $msg): void {
|
2019-01-23 21:34:54 +00:00
|
|
|
fwrite(STDERR, $msg.\PHP_EOL);
|
2018-12-08 01:03:04 +00:00
|
|
|
}
|
|
|
|
|
2021-06-15 20:58:54 +00:00
|
|
|
protected function serviceFork(string $pidfile): int {
|
|
|
|
// initialize the object factory
|
|
|
|
Arsse::$obj = Arsse::$obj ?? new Factory;
|
|
|
|
// create a Daemon object which contains various helper functions
|
|
|
|
$daemon = Arsse::$obj->get(Daemon::class);
|
|
|
|
// resolve the PID file to its absolute path; this also checks its readability and writability
|
2021-06-16 16:48:09 +00:00
|
|
|
$pidfile = $daemon->checkPIDFilePath($pidfile);
|
2021-06-15 20:58:54 +00:00
|
|
|
// daemonize
|
|
|
|
$daemon->fork($pidfile);
|
|
|
|
// start the fetching service as normal
|
|
|
|
$this->loadConf();
|
|
|
|
Arsse::$obj->get(Service::class)->watch(true);
|
|
|
|
// after the service has been shut down, delete the PID file and exit cleanly
|
|
|
|
unlink($pidfile);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-05 14:08:50 +00:00
|
|
|
protected function userAddOrSetPassword(string $method, string $user, string $password = null, string $oldpass = null): int {
|
2018-11-06 17:32:28 +00:00
|
|
|
$passwd = Arsse::$user->$method(...array_slice(func_get_args(), 1));
|
2017-08-29 14:50:31 +00:00
|
|
|
if (is_null($password)) {
|
2017-09-28 23:25:31 +00:00
|
|
|
echo $passwd.\PHP_EOL;
|
2017-08-28 23:38:58 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2018-11-05 14:08:50 +00:00
|
|
|
|
|
|
|
protected function userList(): int {
|
|
|
|
$list = Arsse::$user->list();
|
|
|
|
if ($list) {
|
|
|
|
echo implode(\PHP_EOL, $list).\PHP_EOL;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-03-25 14:45:05 +00:00
|
|
|
protected function userAuthenticate(string $user, string $password, bool $fever = false): int {
|
2021-02-07 04:51:23 +00:00
|
|
|
$result = $fever ? Arsse::$obj->get(Fever::class)->authenticate($user, $password) : Arsse::$user->auth($user, $password);
|
2019-03-25 14:45:05 +00:00
|
|
|
if ($result) {
|
2018-11-05 14:08:50 +00:00
|
|
|
echo Arsse::$lang->msg("CLI.Auth.Success").\PHP_EOL;
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
echo Arsse::$lang->msg("CLI.Auth.Failure").\PHP_EOL;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2021-02-10 16:24:01 +00:00
|
|
|
|
|
|
|
protected function userShowProperties(string $user): int {
|
|
|
|
$data = Arsse::$user->propertiesGet($user);
|
|
|
|
$len = array_reduce(array_keys($data), function($carry, $item) {
|
|
|
|
return max($carry, strlen($item));
|
|
|
|
}, 0) + 2;
|
|
|
|
foreach ($data as $k => $v) {
|
|
|
|
echo str_pad($k, $len, " ");
|
|
|
|
echo var_export($v, true).\PHP_EOL;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2021-02-11 02:40:51 +00:00
|
|
|
|
|
|
|
protected function tokenList(string $user): int {
|
|
|
|
$list = Arsse::$obj->get(Miniflux::class)->tokenList($user);
|
|
|
|
usort($list, function($v1, $v2) {
|
|
|
|
return $v1['label'] <=> $v2['label'];
|
|
|
|
});
|
|
|
|
foreach ($list as $t) {
|
|
|
|
echo $t['id']." ".$t['label'].\PHP_EOL;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2017-08-29 14:50:31 +00:00
|
|
|
}
|