soselo/cli.php

86 lines
2.1 KiB
PHP
Raw Normal View History

<?php chdir(__DIR__) ?>
<?php require 'base.php' ?>
<?php
if (!isset($argv) || $argv === NULL) die;
function cli__users($args) {
$help = function() {
cli__help(false);
echo ' users add|new|create <username> <password> Add a new user'."\n";
echo ' users edit|update <username> <password> Update a user'."\n";
echo ' users del|delete|remove <username> 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);
2022-12-10 15:47:50 +00:00
if (count($args) !== 1) $help();
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 <help> 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;
}