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;
|
|
|
|
|
2018-11-05 14:08:50 +00:00
|
|
|
use Docopt\Response as Opts;
|
|
|
|
|
2017-07-21 21:15:43 +00:00
|
|
|
class CLI {
|
|
|
|
protected $args = [];
|
2018-10-26 18:58:04 +00:00
|
|
|
|
2017-07-21 21:15:43 +00:00
|
|
|
protected function usage(): string {
|
|
|
|
$prog = basename($_SERVER['argv'][0]);
|
|
|
|
return <<<USAGE_TEXT
|
|
|
|
Usage:
|
|
|
|
$prog daemon
|
|
|
|
$prog feed refresh <n>
|
2018-11-05 14:08:50 +00:00
|
|
|
$prog conf save-defaults [<file>]
|
|
|
|
$prog user [list]
|
2017-08-28 23:38:58 +00:00
|
|
|
$prog user add <username> [<password>]
|
2018-11-05 14:08:50 +00:00
|
|
|
$prog user remove <username>
|
|
|
|
$prog user set-pass [--oldpass=<pass>] <username> [<password>]
|
|
|
|
$prog user auth <username> <password>
|
2017-07-21 21:15:43 +00:00
|
|
|
$prog --version
|
|
|
|
$prog --help | -h
|
|
|
|
|
|
|
|
The Arsse command-line interface currently allows you to start the refresh
|
2018-11-05 14:08:50 +00:00
|
|
|
daemon, refresh a specific feed by numeric ID, manage users, or save default
|
2017-08-28 23:38:58 +00:00
|
|
|
configuration to a sample file.
|
2017-07-21 21:15:43 +00:00
|
|
|
USAGE_TEXT;
|
|
|
|
}
|
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function __construct(array $argv = null) {
|
2017-09-05 23:35:14 +00:00
|
|
|
$argv = $argv ?? array_slice($_SERVER['argv'], 1);
|
2017-07-21 21:15:43 +00:00
|
|
|
$this->args = \Docopt::handle($this->usage(), [
|
|
|
|
'argv' => $argv,
|
|
|
|
'help' => true,
|
2017-10-01 13:33:49 +00:00
|
|
|
'version' => Arsse::VERSION,
|
2017-07-21 21:15:43 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2018-11-05 14:08:50 +00:00
|
|
|
protected function command(array $options, $args): string {
|
|
|
|
foreach ($options as $cmd) {
|
|
|
|
foreach (explode(" ", $cmd) as $part) {
|
|
|
|
if (!$args[$part]) {
|
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $cmd;
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2017-08-20 03:56:32 +00:00
|
|
|
protected function loadConf(): bool {
|
2018-06-18 14:09:43 +00:00
|
|
|
$conf = file_exists(BASE."config.php") ? new Conf(BASE."config.php") : new Conf;
|
|
|
|
Arsse::load($conf);
|
2017-08-28 23:38:58 +00:00
|
|
|
// command-line operations will never respect authorization
|
|
|
|
Arsse::$user->authorizationEnabled(false);
|
2017-08-20 03:56:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function dispatch(array $args = null): int {
|
2017-07-21 21:15:43 +00:00
|
|
|
// act on command line
|
2017-09-05 23:35:14 +00:00
|
|
|
$args = $args ?? $this->args;
|
2018-11-05 14:08:50 +00:00
|
|
|
try {
|
|
|
|
switch ($this->command(["daemon", "feed refresh", "conf save-defaults", "user"], $args)) {
|
|
|
|
case "daemon":
|
|
|
|
$this->loadConf();
|
|
|
|
return $this->daemon();
|
|
|
|
case "feed refresh":
|
|
|
|
$this->loadConf();
|
|
|
|
return $this->feedRefresh((int) $args['<n>']);
|
|
|
|
case "conf save-defaults":
|
|
|
|
return $this->confSaveDefaults($args['<file>']);
|
|
|
|
case "user":
|
|
|
|
$this->loadConf();
|
|
|
|
return $this->userManage($args);
|
2017-08-28 23:38:58 +00:00
|
|
|
}
|
2018-11-05 14:08:50 +00:00
|
|
|
} catch (AbstractException $e) {
|
|
|
|
fwrite(STDERR, $e->getMessage().\PHP_EOL);
|
|
|
|
return $e->getCode();
|
2017-07-21 21:15:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function daemon(bool $loop = true): int {
|
2017-07-21 21:15:43 +00:00
|
|
|
(new Service)->watch($loop);
|
|
|
|
return 0; // FIXME: should return the exception code of thrown exceptions
|
|
|
|
}
|
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function feedRefresh(int $id): int {
|
2017-08-18 03:05:08 +00:00
|
|
|
return (int) !Arsse::$db->feedUpdate($id); // FIXME: exception error codes should be returned here
|
2017-07-21 21:15:43 +00:00
|
|
|
}
|
2017-08-20 03:56:32 +00:00
|
|
|
|
2018-11-05 14:08:50 +00:00
|
|
|
public function confSaveDefaults(string $file = null): int {
|
|
|
|
$file = ($file=="-" ? null : $file) ?? STDOUT;
|
2017-08-20 03:56:32 +00:00
|
|
|
return (int) !(new Conf)->exportFile($file, true);
|
|
|
|
}
|
2017-08-28 23:38:58 +00:00
|
|
|
|
2018-11-05 14:08:50 +00:00
|
|
|
public function userManage($args): int {
|
|
|
|
switch ($this->command(["add", "remove", "set-pass", "list", "auth"], $args)) {
|
|
|
|
case "add":
|
|
|
|
return $this->userAddOrSetPassword("add", $args["<username>"], $args["<password>"]);
|
|
|
|
case "set-pass":
|
|
|
|
return $this->userAddOrSetPassword("passwordSet", $args["<username>"], $args["<password>"], $args["<oldpass>"]);
|
|
|
|
case "remove":
|
|
|
|
return (int) !Arsse::$user->remove($args["<username>"]);
|
|
|
|
case "auth":
|
|
|
|
return $this->userAuthenticate($args["<username>"], $args["<password>"]);
|
|
|
|
case "list":
|
|
|
|
case "":
|
|
|
|
return $this->userList();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function userAddOrSetPassword(string $method, string $user, string $password = null, string $oldpass = null): int {
|
|
|
|
$args = \func_get_args();
|
|
|
|
array_shift($args);
|
|
|
|
$passwd = Arsse::$user->$method(...$args);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function userAuthenticate(string $user, string $password): int {
|
|
|
|
if (Arsse::$user->auth($user, $password)) {
|
|
|
|
echo Arsse::$lang->msg("CLI.Auth.Success").\PHP_EOL;
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
echo Arsse::$lang->msg("CLI.Auth.Failure").\PHP_EOL;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2017-08-29 14:50:31 +00:00
|
|
|
}
|