2022-12-10 15:45:39 +00:00
|
|
|
<?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();
|
2022-12-10 16:19:51 +00:00
|
|
|
__session_delete($args[0]);
|
2022-12-10 15:45:39 +00:00
|
|
|
$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();
|
2022-12-10 15:45:39 +00:00
|
|
|
if (!isset($users[$args[0]]))
|
|
|
|
$msg = 'User "'.$args[0].'" does not exist.';
|
|
|
|
else {
|
|
|
|
unset($users[$args[0]]);
|
2022-12-10 16:19:51 +00:00
|
|
|
__session_delete($args[0]);
|
2022-12-10 15:45:39 +00:00
|
|
|
$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";
|
|
|
|
}
|
|
|
|
|
2022-12-13 11:06:30 +00:00
|
|
|
function cli__cache($args) {
|
|
|
|
$help = function() {
|
|
|
|
cli__help(false);
|
|
|
|
echo ' cache prune|purge|delete Delete all application caches'."\n";
|
|
|
|
die;
|
|
|
|
};
|
|
|
|
$msg = null;
|
|
|
|
switch ($args[0]) {
|
|
|
|
case 'prune':
|
|
|
|
case 'purge':
|
|
|
|
case 'delete':
|
|
|
|
foreach (scandir(filedb_getdir('cache')) as $fil) {
|
|
|
|
if (in_array($fil, ['.','..']))
|
|
|
|
continue;
|
|
|
|
filedb_del('cache', $fil);
|
|
|
|
}
|
|
|
|
$msg = 'Application cache has been succesfully purged';
|
|
|
|
break;
|
|
|
|
case 'help':
|
|
|
|
default:
|
|
|
|
$help();
|
|
|
|
}
|
|
|
|
if ($msg != nulL)
|
|
|
|
echo $msg."\n";
|
|
|
|
}
|
|
|
|
|
2022-12-10 15:45:39 +00:00
|
|
|
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";
|
2022-12-13 11:06:30 +00:00
|
|
|
echo ' cache <help> Handle all the web application caches'."\n";
|
2022-12-10 15:45:39 +00:00
|
|
|
die;
|
|
|
|
}
|
|
|
|
|
2022-12-13 11:06:30 +00:00
|
|
|
if (count($argv) <= 1)
|
|
|
|
cli__help();
|
|
|
|
|
2022-12-10 15:45:39 +00:00
|
|
|
switch ($argv[1]) {
|
|
|
|
case 'users':
|
|
|
|
cli__users(array_slice($argv, 2));
|
|
|
|
break;
|
2022-12-13 11:06:30 +00:00
|
|
|
case 'cache':
|
|
|
|
cli__cache(array_slice($argv, 2));
|
|
|
|
break;
|
2022-12-10 15:45:39 +00:00
|
|
|
case 'help':
|
|
|
|
cli__help();
|
|
|
|
break;
|
|
|
|
}
|