summaryrefslogtreecommitdiff
path: root/localwebsite/utils.php
blob: 333ebfcd962d6eee5683eecbf521c710c0e68727 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env php
<?php

require_once __DIR__.'/init.php';

function read_stdin(?string $prompt = null, bool $multiline = true) {
    if (!is_null($prompt))
        echo $prompt;

    if (!$multiline)
        return trim(fgets(STDIN));

    $fp = fopen('php://stdin', 'r');
    $data = stream_get_contents($fp);
    fclose($fp);

    return $data;
}

function usage() {
    global $argv;
    echo <<<EOF
usage: {$argv[0]} COMMAND

Supported commands:
    add-user
    change-password

EOF;
    exit(1);
}

if (empty($argv[1]))
    usage();

switch ($argv[1]) {
    case 'add-user':
        $username = read_stdin('enter username: ', false);
        $password = read_stdin('enter password: ', false);

        if (users::exists($username)) {
            fwrite(STDERR, "user already exists\n");
            exit(1);
        }

        $id = users::add($username, $password);
        echo "added user, id = $id\n";

        break;

    case 'change-password':
        $id = (int)read_stdin('enter ID: ', false);
        if (!$id)
            die("invalid id\n");

        $password = read_stdin('enter new password: ', false);
        if (!$password)
            die("invalid password\n");

        users::setPassword($id, $password);
        break;

    default:
        fwrite(STDERR, "invalid command\n");
        exit(1);
}