summaryrefslogtreecommitdiff
path: root/update.php
blob: fda9ecf45a927544bfdc3985b5e0f11bdbbbf77b (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
	if (php_sapi_name() != "cli") {
		header("Content-type: text/plain");
		print "Please run this script from the command line.\n";
		exit;
	}

	set_include_path(__DIR__ ."/include" . PATH_SEPARATOR .
		get_include_path());

	chdir(__DIR__);

	require_once "common.php";

	Config::sanity_check();

	$options = getopt("", [ "user-add:", "user-del:", "user-list", "update-schema:", "help" ]);

	if (count($options) == 0 || isset($options["help"])) {
		print "The Epube CLI management tool.\n";
		print "Options:\n";
		print "	--log-level\n";
		print "	--update-schema[=force-yes]\n";
		print "	--help\n";
		print "	--user-add USER[:PASSWORD]\n";
		print "	--user-del USER\n";
		print "	--user-list\n";
	}

	Debug::set_enabled(true);

	if (!isset($options['update-schema']) && Config::is_migration_needed()) {
		die("Schema version is wrong, please upgrade the database (--update-schema).\n");
	}

	if (isset($options["log-level"])) {
		Debug::set_loglevel((int)$options["log-level"]);
	}

	if (isset($options["update-schema"])) {
		if (Config::is_migration_needed()) {

			if ($options["update-schema"] != "force-yes") {
				Debug::log("Type 'yes' to continue.");

				if (read_stdin() != 'yes')
					exit;
			} else {
				Debug::log("Proceeding to update without confirmation.");
			}

			if (!isset($options["log-level"])) {
				Debug::set_loglevel(Debug::$LOG_VERBOSE);
			}

			$migrations = Config::get_migrations();
			$rc = $migrations->migrate();

			exit($rc ? 0 : 1);

		} else {
			Debug::log("Database schema is already at latest version.");
		}
	}

	if (isset($options["user-del"])) {

		$username = $options["user-del"];

		$user = ORM::for_table('epube_users')
			->where('username', $username)
			->find_one();

		if ($user) {
			Debug::log("Deleting user: $username");
			$user->delete();
		} else {
			Debug::log("User not found: $username");
		}
	}

	if (isset($options["user-list"])) {
		$users = ORM::for_table('epube_users')
			->find_many();

		foreach ($users as $user) {
			Debug::log("{$user->id}. {$user->user}");
		}
	}

	if (isset($options["user-add"])) {
		list ($username, $pass) = explode(":", $options["user-add"], 2);

		if (!$username) {
			Debug::log("Not enough arguments");
			exit;
		}

		if (empty($pass)) {
			print "Enter password for user $username: ";
			$pass = trim(read_stdin());
		}

		$username = mb_strtolower($username);

		$pass_hash = 'SHA256:' . hash('sha256', "$username:" . trim($pass));

		$user = ORM::for_table('epube_users')
			->where('username', $username)
			->find_one();

		if ($user) {
			Debug::log("User $username already exists, updating password.");
			$user->pass = $pass_hash;
			$user->save();
		} else {
			Debug::log("Adding user $username.");
			$user = ORM::for_table('epube_users')->create();
			$user->username = $username;
			$user->pass = $pass_hash;
			$user->save();
		}
	}