UserManager.php
3.99 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
require_once(dirname(__FILE__) . '/UserManagerClass.php');
putenv("LD_LIBRARY_PATH=".getenv("LD_LIBRARY_PATH"));
putenv("PATH=./:".getenv("DDBASEBIN").":/bin:/usr/bin");
$stderr = fopen("php://stderr","w");
$shortopts = "j:";
$longopts = array("json:");
$options = getopt($shortopts, $longopts);
$json_file = "";
if (array_key_exists("json",$options)) {
$json_file = $options["json"];
}
else if (array_key_exists("j",$options)) {
$json_file = $options["j"];
}
if (!empty($json_file)) {
if (!file_exists($json_file)) {
echo "[ERROR] Cannot find json file: ".$json_file.PHP_EOL;
exit(1);
}
$json_string = file_get_contents($json_file);
$args = json_decode($json_string);
if (empty($args)) {
echo "[ERROR] Cannot decode json data from: ".$json_file.PHP_EOL;
exit(1);
}
}
else {
$args = new stdClass();
$args->action = $argv[1];
switch ($args->action)
{
case 'delete' :
$args->login = $argv[2];
break;
case 'groups' :
$args->login = $argv[2];
$args->groups = $argv[3];
break;
case 'add' :
$args->login = $argv[6];
$args->pwd = $argv[7];
$args->first_name= $argv[8];
$args->last_name= $argv[9];
$args->email= $argv[10];
$args->pwd_hashed = $argv[2];
$args->news= $argv[3];
$args->groups= $argv[4];
$args->sendEmail= $argv[5];
break;
default :
fprintf($stderr,"Not implemented action ".$args->action."\n");
exit;
}
}
if (!isset($args->action))
{
fprintf($stderr,"Missing action argument\n");
exit;
}
$userManager = new UserManagerClass($stderr);
switch ($args->action)
{
case 'add' :
if (!isset($args->login) || !isset($args->pwd) || !isset($args->first_name) || !isset($args->last_name) ||
!isset($args->email))
{
fprintf($stderr,"Missing argument(s) to add a user\n");
exit;
}
if (isset($args->pwd_hashed) && !($args->pwd_hashed))
$pwd_hash = crypt($args->pwd,chr(rand(97,122)).chr(rand(97,122)));
else
$pwd_hash = $args->pwd;
if ($userManager->AddUser($args->login,$pwd_hash,$args->first_name,$args->last_name,$args->email,
isset($args->news) ? $args->news : 1,
isset($args->groups) ? $args->groups : ''))
{
$userManager->GenerateGroupsXmlFile();
if (isset($args->sendEmail) && $args->sendEmail)
$userManager->SendRegistrationMail($args->login,$args->pwd,$args->first_name,$args->last_name,$args->email);
fprintf($stderr,"User ".$args->login." added\n");
}
break;
case 'modify' :
if (!isset($args->login) || !isset($args->pwd) || !isset($args->pwd_new))
{
fprintf($stderr,"Missing argument(s) to modify the user password\n");
exit;
}
if (isset($args->pwd_hashed) && !($args->pwd_hashed))
$pwd_hash = $userManager->cryptPwd($args->login,$args->pwd);
else
$pwd_hash = $args->pwd;
if (isset($args->pwd_new_hashed) && !($args->pwd_new_hashed))
$pwd_new_hash = crypt($args->pwd_new,chr(rand(97,122)).chr(rand(97,122)));
else
$pwd_new_hash = $args->pwd_new;
if ($userManager->ModifyUserPwd($args->login,$pwd_hash,$pwd_new_hash))
fprintf($stderr,"Password of".$args->login." modified\n");
break;
case 'groups' :
if (!isset($args->login) || !isset($args->groups))
{
fprintf($stderr,"Missing argument(s) to modify the user groups\n");
exit;
}
if ($userManager->ModifyUserGroup($args->login,$args->groups))
{
$userManager->GenerateGroupsXmlFile();
fprintf($stderr,"Groups of ".$args->login." modified\n");
}
break;
case 'reset' :
if (!isset($args->login))
{
fprintf($stderr,"Missing argument(s) to reset the user password\n");
exit;
}
$userManager->ResetUserPwd($args->login);
break;
case 'delete' :
if (!isset($args->login))
{
fprintf($stderr,"Missing argument(s) to delete user\n");
exit;
}
if ($userManager->DeleteUser($args->login))
{
$userManager->GenerateGroupsXmlFile();
fprintf($stderr,"User ".$args->login." deleted\n");
}
break;
case 'check' :
$userManager->Check();
break;
}
?>