soselo/cli.php

118 lines
2.8 KiB
PHP

<?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();
__session_delete($args[0]);
$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 (count($args) !== 1) $help();
if (!isset($users[$args[0]]))
$msg = 'User "'.$args[0].'" does not exist.';
else {
unset($users[$args[0]]);
__session_delete($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__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':
filedb_delall('cache');
filedb_delall('cron/db');
$msg = 'Application cache has been succesfully purged';
break;
case 'help':
default:
$help();
}
if ($msg != nulL)
echo $msg."\n";
}
function cli__help($full=true) {
echo 'Soselo 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";
echo ' cache <help> Handle all the web application caches'."\n";
die;
}
if (count($argv) <= 1)
cli__help();
switch ($argv[1]) {
case 'users':
cli__users(array_slice($argv, 2));
break;
case 'cache':
cli__cache(array_slice($argv, 2));
break;
case 'help':
cli__help();
break;
}