-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconsole.php
executable file
·89 lines (77 loc) · 2.3 KB
/
console.php
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
#!/usr/bin/env php
<?php
if(php_sapi_name() != 'cli')
die('This script must be executed from command line.'."\n");
if(!isset($argv[1]))
die('Please specify an action as first parameter (createuser|listuser|changeuserpasswd|deleteuser).'."\n");
require_once('lib/loader.php');
try {
$null = null;
switch($argv[1]) {
case 'housekeeping':
if(!isset($argv[2]) || !isset($argv[3])) {
throw new Exception('missing arguments, need <inactive-since-days> <only-if-no-entries>');
}
$hk = new housekeeping($db->getDbHandle());
if($hk->cleanup(intval($argv[2]), boolval($argv[3]))) {
echo $argv[1].': OK'."\n";
} else {
throw new Exception('database operation error');
}
break;
case 'createuser':
if(!isset($argv[2]) || !isset($argv[3])) {
throw new Exception('missing arguments, need <username> <password>');
}
if($db->insertClient($argv[2], password_hash($argv[3], PASSWORD_DEFAULT), $null) > 0) {
echo $argv[1].': OK'."\n";
} else {
throw new Exception('database operation error');
}
break;
case 'listuser':
$clients = $db->getClients();
if($clients !== null) {
foreach($clients as $client) {
echo $client->id." ".$client->email." ".$client->last_login."\n";
}
echo $argv[1].': OK'."\n";
} else {
throw new Exception('database operation error');
}
break;
case 'changeuserpasswd':
if(!isset($argv[2])) {
throw new Exception('missing arguments, need <userid> <new-password>');
}
if($db->getClient(intval($argv[2])) !== null) {
if($db->setClientPassword(intval($argv[2]), password_hash($argv[3], PASSWORD_DEFAULT))) {
echo $argv[1].': OK'."\n";
} else {
throw new Exception('database operation error');
}
} else {
throw new Exception('userid not found');
}
break;
case 'deleteuser':
if(!isset($argv[2])) {
throw new Exception('missing arguments, need <userid>');
}
if($db->getClient(intval($argv[2])) !== null) {
if($db->deleteClient(intval($argv[2]))) {
echo $argv[1].': OK'."\n";
} else {
throw new Exception('database operation error');
}
} else {
throw new Exception('userid not found');
}
break;
default:
throw new Exception('unknown command');
}
} catch(Exception $e) {
echo $argv[1].' ERROR: '.$e->getMessage()."\n";
exit(1);
}