UserManagerClass.php
7.04 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php
class UserManagerClass
{
protected $infoXml;
protected $stderr;
protected $rootElement;
function __construct($stderr)
{
$this->stderr = $stderr;
$this->infoXml = new DomDocument("1.0","UTF-8");
$this->infoXml->preserveWhiteSpace = false;
$this->infoXml->formatOutput = true;
}
function GetInfoXmlFilePath()
{
return getenv("DDINFO")."/".getenv("AMDA_USERS_INFO");
}
function GetGroupsXmlFilePath()
{
return getenv("DDINFO")."/".getenv("AMDA_GROUPS_INFO");
}
function LoadInfoXmlFile()
{
if (file_exists($this->GetInfoXmlFilePath()))
{
$this->infoXml->load($this->GetInfoXmlFilePath());
$this->rootElement = $this->infoXml->documentElement;
}
else
{
$this->rootElement = $this->infoXml->createElement("users");
$this->infoXml->appendChild($this->rootElement);
}
return 1;
}
function AddUser($login, $pwd_hash, $first_name, $last_name, $email, $news, $groups)
{
if (!$this->LoadInfoXmlFile())
return 0;
$users = $this->infoXml->getElementsByTagName("user");
//test if login is already used
for ($i = 0; $i < $users->length; $i++)
{
$crtLogin = $users->item($i)->getAttribute("login");
if ($crtLogin == $login)
{
fprintf($this->stderr,"Login already exist on users info file\n");
return 0;
}
}
date_default_timezone_set('UTC');
$user = $this->infoXml->createElement("user");
$user->setAttribute("login",$login);
$user->setAttribute("name",$last_name);
$user->setAttribute("first_name",$first_name);
$user->setAttribute("group",$groups);
$user->setAttribute("email",$email);
$user->setAttribute("date",date('j/m/y'));
$user->setAttribute("news",$news);
$this->rootElement->appendChild($user);
exec('DDadmin -a '.$login.' '.$pwd_hash,$output,$return);
if ($return != 0)
{
fprintf($this->stderr,"DDadmin error : ".$output[0]."\n");
return 0;
}
else
$this->infoXml->save($this->GetInfoXmlFilePath());
return 1;
}
function ModifyUserPwd($login, $pwd_hash, $pwd_hash_new)
{
exec('DDadmin -m '.$login.' '.$pwd_hash.' '.$pwd_hash_new,$output,$return);
if ($return != 0)
{
fprintf($this->stderr,"DDadmin error : ".$output[0]."\n");
return 0;
}
return 1;
}
function ModifyUserGroup($login,$groups)
{
if (!$this->LoadInfoXmlFile())
return 0;
$users = $this->infoXml->getElementsByTagName("user");
for ($i = 0; $i < $users->length; $i++)
{
$crtLogin = $users->item($i)->getAttribute("login");
if ($crtLogin == $login)
{
$users->item($i)->setAttribute("group",$groups);
$this->infoXml->save($this->GetInfoXmlFilePath());
return 1;
}
}
fprintf($this->stderr,"Cannot found user\n");
return 0;
}
function ResetUserPwd($login)
{
exec('DDadmin -r '.$login,$output,$return);
if ($return != 0)
{
fprintf($this->stderr,"DDadmin error : ".$output."\n");
return 0;
}
else
fprintf($this->stderr,"New password is : ".$output[0]."\n");
return 1;
}
function DeleteUser($login)
{
if (!$this->LoadInfoXmlFile())
return 0;
$users = $this->infoXml->getElementsByTagName("user");
for ($i = 0; $i < $users->length; $i++)
{
$crtLogin = $users->item($i)->getAttribute("login");
if ($crtLogin == $login)
{
$this->rootElement->removeChild($users->item($i));
break;
}
}
exec('DDadmin -d '.$login,$output,$return);
if ($return != 0)
{
fprintf($this->stderr,"DDadmin error : ".$output[0]."\n");
return 0;
}
$this->infoXml->save($this->GetInfoXmlFilePath());
return 1;
}
//This function will be deprecated for AMDA-NG
function GenerateGroupsXmlFile()
{
if (!$this->LoadInfoXmlFile())
return 0;
$users = $this->infoXml->getElementsByTagName("user");
$groupsXml = new DomDocument("1.0","UTF-8");
$groupsXml->preserveWhiteSpace = false;
$groupsXml->formatOutput = true;
$rootGroupsElement = $groupsXml->createElement("AMDA_USERS");
$groupsXml->appendChild($rootGroupsElement);
for ($i = 0; $i < $users->length; $i++)
{
if (strcmp($users->item($i)->getAttribute("group"),"") == 0)
continue;
$u = $groupsXml->createElement("user",$users->item($i)->getAttribute("login"));
$u->setAttribute("group",$users->item($i)->getAttribute("group"));
$rootGroupsElement->appendChild($u);
}
$groupsXml->save($this->GetGroupsXmlFilePath());
return 1;
}
function SendRegistrationMail($login,$pwd,$first_name, $last_name, $email)
{
$subject = 'AMDA registration';
$msg = "Dear $first_name $last_name, \r\n\r\n";
$msg .= "Thanks for your interest in our interactive space physics data manipulation tool and database AMDA\r\n\r\n";
$msg .= "Your login: $login \r\n";
$msg .= " password: $pwd \r\n\r\n";
$msg .= "at http://amda.cdpp.eu \r\n\r\n";
$msg .= "Please contact us in case of any problems or questions.\r\n\r\n";
$msg .= "For acknowledgment and data use policy please follow the rules of the road available at http://amda.cdpp.eu/help/policy.html.\r\n\r\n";
$msg .= "We also invite you to connect to our data archive (https://cdpp-archive.cnes.fr) and use our other tools for 3D visualization of spacecraft and science data (3DView, http://3dview.cdpp.eu), for solar wind propagation (Propagation Tool, http://propagationtool.cdpp.eu and Heliopropa, http://heliopropa.cdpp.eu), or for coordinate and time transformation (TREPS, http://treps.cdpp.eu).\r\n\r\n";
$msg .= "Feedback on CDPP tools is always welcome and greatly appreciated (amda at irap.omp.eu)!\r\n\r\n";
$msg .= "Best regards,\r\n\r\n";
$msg .= "CDPP-AMDA Team";
$headers = "From: amda@irap.omp.eu " . "\r\n".
"Reply-To: amda@irap.omp.eu " . "\r\n".
"Cc: amda@irap.omp.eu" . "\r\n".
"Content-type: text/plain; charset=utf-8\r\n";
mail($email, $subject, $msg, $headers);
}
function Check()
{
if (!$this->LoadInfoXmlFile())
return 0;
$users = $this->infoXml->getElementsByTagName("user");
exec('DDadmin -l',$output,$return);
if ($return != 0)
{
fprintf($this->stderr,"DDadmin error : ".$output[0]."\n");
return 0;
}
$ddAdminUsers = explode(',',$output[0]);
for ($i = 0; $i < $users->length; $i++)
{
$found = false;
for ($j = 0; $j < count($ddAdminUsers); $j++)
if (strcmp($users->item($i)->getAttribute("login"),$ddAdminUsers[$j]) == 0)
{
$found = true;
break;
}
if (!$found)
fprintf($this->stderr,"User ".$users->item($i)->getAttribute("login")." not found on nc file\n");
}
for ($i = 0; $i < count($ddAdminUsers); $i++)
{
$found = false;
for ($j = 0; $j < $users->length; $j++)
if (strcmp($users->item($j)->getAttribute("login"),$ddAdminUsers[$i]) == 0)
{
$found = true;
break;
}
if (!$found)
fprintf($this->stderr,"User ".$ddAdminUsers[$i]." not found on info file\n");
}
return 1;
}
//crypt the password with the salt corresponding to the login
function cryptPwd($login,$pwd)
{
exec('DDadmin -s '.$login,$output,$return);
if ($return != 0)
{
fprintf($this->stderr,"DDadmin error : ".$output."\n");
return '';
}
$salt = $output[0];
return crypt($pwd,$salt);
}
}
?>