diff options
author | Andrew Dolgov <[email protected]> | 2017-06-28 12:32:48 +0300 |
---|---|---|
committer | Andrew Dolgov <[email protected]> | 2017-06-28 12:32:48 +0300 |
commit | 9f4927825bb5efeefdff9a2aac05c5b3200f5ef6 (patch) | |
tree | f7782cb57127c68bfd5c67fb0d90c725eb8f0e68 /useradm.php | |
parent | 4496d4a5e1f3ddb5fd0b3a0315f12c207e7c9041 (diff) |
move to internal user management because it's impossible to implement
proper transparent offline mode with http auth (worker is incapable of
authenticating properly)
MIGRATION:
1. disable HTTP authentication (this is important!)
2. add two new tables to db/scratch.db (sessions & users)
3. create users via useradm.php (same names and passwords, previous data
is kept)
Diffstat (limited to 'useradm.php')
-rw-r--r-- | useradm.php | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/useradm.php b/useradm.php new file mode 100644 index 0000000..2af25d2 --- /dev/null +++ b/useradm.php @@ -0,0 +1,59 @@ +#!/usr/bin/php +<?php + require_once "config.php"; + require_once "db.php"; + + $dbh = Db::get(); + + $longopts = [ "add:", "del:", "list", "help" ]; + + $options = getopt("", $longopts); + + if (count($options) == 0 || isset($options["help"])) { + print "Manage Epube user database. Usage: + --add USER:PASSWORD + --del USER + --list\n"; + } + + if (isset($options["del"])) { + $user = SQLite3::escapeString($options["del"]); + + print "Deleting user $user...\n"; + $dbh->query("DELETE FROM epube_users WHERE user = '$user'"); + } + + if (isset($options["list"])) { + $res = $dbh->query("SELECT id, user FROM epube_users ORDER BY user"); + + while ($line = $res->fetchArray(SQLITE3_ASSOC)) { + printf("%d. %s\n", $line["id"], $line["user"]); + } + + } + + if (isset($options["add"])) { + @list($user, $pass) = explode(":", $options["add"]); + + if (!$user || !$pass) { + print "Not enough arguments.\n"; + exit; + } + + $user = SQLite3::escapeString($user); + $pass_hash = SQLite3::escapeString('SHA256:' . hash('sha256', "$user:$pass")); + + print "Adding user $user with password $pass...\n"; + + $res = $dbh->query("SELECT user FROM epube_users WHERE user = '$user'"); + + if ($line = $res->fetchArray(SQLITE3_ASSOC)) { + print "User already exists.\n"; + } else { + $dbh->query("INSERT INTO epube_users (user, pass) + VALUES ('$user', '$pass_hash')"); + } + + } + +?> |