From 20513e7f23dc3a626596b883f5c1ebed5b9c07c7 Mon Sep 17 00:00:00 2001 From: Bofh Date: Sat, 10 Dec 2022 16:45:39 +0100 Subject: [PATCH] Created cli.php for managing configuration from cli --- cli.php | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ cron.php | 2 ++ 2 files changed, 86 insertions(+) create mode 100644 cli.php diff --git a/cli.php b/cli.php new file mode 100644 index 0000000..72d7ae8 --- /dev/null +++ b/cli.php @@ -0,0 +1,84 @@ + + + Add a new user'."\n"; + echo ' users edit|update Update a user'."\n"; + echo ' users del|delete|remove Delete a user'."\n"; + die; + }; + + $msg = null; + $users = $GLOBALS['appconf']['users']; + switch ($args[0]) { + case 'create': + case 'update': + case 'edit': + case 'new': + case 'add': + $args = array_slice($args, 1); + if (count($args) !== 2) $help(); + $args[1] = hash_hmac('sha256', $args[1], $GLOBALS['appconf']['users_hash_secret']); + $users[$args[0]] = $args[1]; + $msg = 'User "'.$args[0].'" has been succesfully saved.'; + break; + case 'remove': + case 'delete': + case 'del': + $args = array_slice($args, 1); + if (!isset($users[$args[0]])) + $msg = 'User "'.$args[0].'" does not exist.'; + else { + unset($users[$args[0]]); + $msg = 'User "'.$args[0].'" has been succesfully deleted.'; + } + break; + case 'help': + default: + $help(); + } + $booladd = true; + $newlines = []; + $lines = explode("\n", trim(file_get_contents('config/users.php'))); + foreach ($lines as $line) { + if ($booladd) + $newlines []= $line; + if (trim($line) === '#start_users') { + $booladd = false; + continue; + } + if (trim($line) === '#end_users') { + foreach ($users as $uname => $pwd) + $newlines []= "$uname $pwd"; + $booladd = true; + $newlines []= $line; + continue; + } + } + file_put_contents('config/users.php', implode("\n", $newlines)); + if ($msg !== null) + echo $msg."\n"; +} + +function cli__help($full=true) { + echo 'APub Control cli.php 1.0 author: bofh@nogafam.es'."\n"; + echo "\n"; + echo ' Parameters:'."\n"; + if ($full === false) return; + echo ' users Add/delete/modify web application users'."\n"; + die; +} + +switch ($argv[1]) { + case 'users': + cli__users(array_slice($argv, 2)); + break; + case 'help': + cli__help(); + break; +} diff --git a/cron.php b/cron.php index 18598f2..2fb2db0 100644 --- a/cron.php +++ b/cron.php @@ -2,6 +2,8 @@