useradm.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. if (!defined('STDIN')) {
  3. print "Please run this script via PHP CLI interpreter (php ./useradm.php).";
  4. exit;
  5. }
  6. require_once "config.php";
  7. require_once "db.php";
  8. $dbh = Db::get();
  9. $longopts = [ "add:", "del:", "list", "help" ];
  10. $options = getopt("", $longopts);
  11. if (count($options) == 0 || isset($options["help"])) {
  12. print "Manage Epube user database. Usage:
  13. --add USER:PASSWORD
  14. --del USER
  15. --list\n";
  16. }
  17. if (isset($options["del"])) {
  18. $user = SQLite3::escapeString($options["del"]);
  19. print "Deleting user $user...\n";
  20. $dbh->query("DELETE FROM epube_users WHERE user = '$user'");
  21. }
  22. if (isset($options["list"])) {
  23. $res = $dbh->query("SELECT id, user FROM epube_users ORDER BY user");
  24. while ($line = $res->fetchArray(SQLITE3_ASSOC)) {
  25. printf("%d. %s\n", $line["id"], $line["user"]);
  26. }
  27. }
  28. if (isset($options["add"])) {
  29. @list($user, $pass) = explode(":", $options["add"]);
  30. if (!$user || !$pass) {
  31. print "Not enough arguments.\n";
  32. exit;
  33. }
  34. $user = SQLite3::escapeString(trim(mb_strtolower($user)));
  35. $pass_hash = SQLite3::escapeString('SHA256:' . hash('sha256', "$user:" . trim($pass)));
  36. print "Adding user $user with password $pass...\n";
  37. $res = $dbh->query("SELECT user FROM epube_users WHERE user = '$user'");
  38. if ($line = $res->fetchArray(SQLITE3_ASSOC)) {
  39. print "User already exists.\n";
  40. } else {
  41. $dbh->query("INSERT INTO epube_users (user, pass)
  42. VALUES ('$user', '$pass_hash')");
  43. }
  44. }
  45. ?>