Projekt

Obecné

Profil

Stáhnout (14.2 KB) Statistiky
| Větev: | Tag: | Revize:
1
<?php
2

    
3
	/**
4
	 * Manage users in a database cluster
5
	 *
6
	 * $Id: users.php,v 1.31 2005/10/18 03:45:16 chriskl Exp $
7
	 */
8

    
9
	// Include application functions
10
	include_once('./libraries/lib.inc.php');
11

    
12
	$action = (isset($_REQUEST['action'])) ? $_REQUEST['action'] : '';
13
	if (!isset($msg)) $msg = '';
14
	$PHP_SELF = $_SERVER['PHP_SELF'];
15
		
16
	/**
17
	 * If a user is not a superuser, then we have an 'account management' page
18
	 * where they can change their password, etc.  We don't prevent them from
19
	 * messing with the URL to gain access to other user admin stuff, because
20
	 * the PostgreSQL permissions will prevent them changing anything anyway.
21
	 */
22
	function doAccount($msg = '') {
23
		global $data, $misc;
24
		global $PHP_SELF, $lang;
25
		
26
		$server_info = $misc->getServerInfo();
27
		
28
		$userdata = $data->getUser($server_info['username']);
29
		$_REQUEST['user'] = $server_info['username'];
30
		
31
		$misc->printTrail('user');
32
		$misc->printTabs('server','account');
33
		$misc->printMsg($msg);
34

    
35
		if ($userdata->recordCount() > 0) {
36
			$userdata->f['usesuper'] = $data->phpBool($userdata->f['usesuper']);
37
			$userdata->f['usecreatedb'] = $data->phpBool($userdata->f['usecreatedb']);
38
			echo "<table>\n";
39
			echo "<tr><th class=\"data\">{$lang['strusername']}</th><th class=\"data\">{$lang['strsuper']}</th><th class=\"data\">{$lang['strcreatedb']}</th><th class=\"data\">{$lang['strexpires']}</th>";
40
			if ($data->hasUserSessionDefaults()) echo "<th class=\"data\">{$lang['strsessiondefaults']}</th>";
41
			echo "</tr>\n";
42
			echo "<tr>\n\t<td class=\"data1\">", $misc->printVal($userdata->f['usename']), "</td>\n";
43
			echo "\t<td class=\"data1\">", (($userdata->f['usesuper']) ? $lang['stryes'] : $lang['strno']), "</td>\n";
44
			echo "\t<td class=\"data1\">", (($userdata->f['usecreatedb']) ? $lang['stryes'] : $lang['strno']), "</td>\n";
45
			echo "\t<td class=\"data1\">", ($userdata->f['useexpires'] == 'infinity' ? '' : $misc->printVal($userdata->f['useexpires'])), "</td>\n";
46
			if ($data->hasUserSessionDefaults()) echo "\t<td class=\"data1\">", $misc->printVal($userdata->f['useconfig']), "</td>\n";
47
			echo "</tr>\n</table>\n";
48
		}
49
		else echo "<p>{$lang['strnodata']}</p>\n";
50
		
51
		echo "<p><a class=\"navlink\" href=\"{$PHP_SELF}?action=confchangepassword&amp;{$misc->href}\">{$lang['strchangepassword']}</a></p>\n";
52
	}
53
	
54
	/**
55
	 * Show confirmation of change password and actually change password
56
	 */
57
	function doChangePassword($confirm, $msg = '') {
58
		global $data, $misc;
59
		global $PHP_SELF, $lang, $conf;
60
		
61
		$server_info = $misc->getServerInfo();
62
		
63
		if ($confirm) {
64
			$_REQUEST['user'] = $server_info['username'];
65
			$misc->printTrail('user');
66
			$misc->printTitle($lang['strchangepassword'],'pg.user.alter');
67
			$misc->printMsg($msg);
68
			
69
			if (!isset($_POST['password'])) $_POST['password'] = '';
70
			if (!isset($_POST['confirm'])) $_POST['confirm'] = '';
71
			
72
			echo "<form action=\"$PHP_SELF\" method=\"post\">\n";
73
			echo $misc->form;
74
			echo "<table>\n";
75
			echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strpassword']}</th>\n";
76
			echo "\t\t<td><input type=\"password\" name=\"password\" size=\"32\" value=\"", 
77
				htmlspecialchars($_POST['password']), "\" /></td>\n\t</tr>\n";
78
			echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strconfirm']}</th>\n";
79
			echo "\t\t<td><input type=\"password\" name=\"confirm\" size=\"32\" value=\"\" /></td>\n\t</tr>\n";
80
			echo "<table>\n";
81
			echo "<p><input type=\"hidden\" name=\"action\" value=\"changepassword\" />\n";
82
			echo "<input type=\"submit\" name=\"ok\" value=\"{$lang['strok']}\" />\n";
83
			echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
84
			echo "</p></form>\n";
85
		}
86
		else {
87
			// Check that password is minimum length
88
			if (strlen($_POST['password']) < $conf['min_password_length'])
89
				doChangePassword(true, $lang['strpasswordshort']);
90
			// Check that password matches confirmation password
91
			elseif ($_POST['password'] != $_POST['confirm'])
92
				doChangePassword(true, $lang['strpasswordconfirm']);
93
			else {
94
				$status = $data->changePassword($server_info['username'], 
95
					$_POST['password']);
96
				if ($status == 0)
97
					doAccount($lang['strpasswordchanged']);
98
				else
99
					doAccount($lang['strpasswordchangedbad']);
100
			}
101
		}		
102
	}
103

    
104
	/**
105
	 * Function to allow editing of a user
106
	 */
107
	function doEdit($msg = '') {
108
		global $data, $misc;
109
		global $PHP_SELF, $lang;
110
	
111
		$misc->printTrail('user');
112
		$misc->printTitle($lang['stralter'],'pg.user.alter');
113
		$misc->printMsg($msg);
114
		
115
		$userdata = $data->getUser($_REQUEST['username']);
116
		
117
		if ($userdata->recordCount() > 0) {
118
			$server_info = $misc->getServerInfo();
119
			$canRename = $data->hasUserRename() && ($_REQUEST['username'] != $server_info['username']);
120
			$userdata->f['usesuper'] = $data->phpBool($userdata->f['usesuper']);
121
			$userdata->f['usecreatedb'] = $data->phpBool($userdata->f['usecreatedb']);
122

    
123
			if (!isset($_POST['formExpires'])){
124
				if ($canRename) $_POST['newname'] = $userdata->f['usename'];
125
				if ($userdata->f['usesuper']) $_POST['formSuper'] = '';
126
				if ($userdata->f['usecreatedb']) $_POST['formCreateDB'] = '';
127
				$_POST['formExpires'] = $userdata->f['useexpires'] == 'infinity' ? '' : $userdata->f['useexpires'];
128
				$_POST['formPassword'] = '';
129
			}
130
		
131
			echo "<form action=\"$PHP_SELF\" method=\"post\">\n";
132
			echo $misc->form;
133
			echo "<table>\n";
134
			echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strusername']}</th>\n";
135
			echo "\t\t<td class=\"data1\">", ($canRename ? "<input name=\"newname\" size=\"15\" value=\"" . htmlspecialchars($_POST['newname']) . "\" />" : $misc->printVal($userdata->f['usename'])), "</td>\n\t</tr>\n";
136
			echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strsuper']}</th>\n";
137
			echo "\t\t<td class=\"data1\"><input type=\"checkbox\" name=\"formSuper\"", 
138
				(isset($_POST['formSuper'])) ? ' checked="checked"' : '', " /></td>\n\t</tr>\n";
139
			echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strcreatedb']}</th>\n";
140
			echo "\t\t<td class=\"data1\"><input type=\"checkbox\" name=\"formCreateDB\"", 
141
				(isset($_POST['formCreateDB'])) ? ' checked="checked"' : '', " /></td>\n\t</tr>\n";
142
			echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strexpires']}</th>\n";
143
			echo "\t\t<td class=\"data1\"><input size=\"16\" name=\"formExpires\" value=\"", htmlspecialchars($_POST['formExpires']), "\" /></td>\n\t</tr>\n";
144
			echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strpassword']}</th>\n";
145
			echo "\t\t<td class=\"data1\"><input type=\"password\" size=\"16\" name=\"formPassword\" value=\"", htmlspecialchars($_POST['formPassword']), "\" /></td>\n\t</tr>\n";
146
			echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strconfirm']}</th>\n";
147
			echo "\t\t<td class=\"data1\"><input type=\"password\" size=\"16\" name=\"formConfirm\" value=\"\" /></td>\n\t</tr>\n";
148
			echo "</table>\n";
149
			echo "<p><input type=\"hidden\" name=\"action\" value=\"save_edit\" />\n";
150
			echo "<input type=\"hidden\" name=\"username\" value=\"", htmlspecialchars($_REQUEST['username']), "\" />\n";
151
			echo "<input type=\"submit\" value=\"{$lang['stralter']}\" />\n";
152
			echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
153
			echo "</form>\n";
154
		}
155
		else echo "<p>{$lang['strnodata']}</p>\n";
156
	}
157
	
158
	/** 
159
	 * Function to save after editing a user
160
	 */
161
	function doSaveEdit() {
162
		global $data, $lang;
163
		
164
		// Check name and password
165
		if (isset($_POST['newname']) && $_POST['newname'] == '')
166
			doEdit($lang['struserneedsname']);
167
		else if ($_POST['formPassword'] != $_POST['formConfirm'])
168
			doEdit($lang['strpasswordconfirm']);
169
		else {
170
			if (isset($_POST['newname'])) $status = $data->setRenameUser($_POST['username'], $_POST['formPassword'], isset($_POST['formCreateDB']), isset($_POST['formSuper']), $_POST['formExpires'], $_POST['newname']);
171
			else $status = $data->setUser($_POST['username'], $_POST['formPassword'], isset($_POST['formCreateDB']), isset($_POST['formSuper']), $_POST['formExpires']);
172
			if ($status == 0)
173
				doDefault($lang['struserupdated']);
174
			else
175
				doEdit($lang['struserupdatedbad']);
176
		}
177
	}
178

    
179
	/**
180
	 * Show confirmation of drop and perform actual drop
181
	 */
182
	function doDrop($confirm) {
183
		global $data, $misc;
184
		global $PHP_SELF, $lang;
185

    
186
		if ($confirm) {
187
			$misc->printTrail('user');
188
			$misc->printTitle($lang['strdrop'],'pg.user.drop');
189
			
190
			echo "<p>", sprintf($lang['strconfdropuser'], $misc->printVal($_REQUEST['username'])), "</p>\n";	
191
			
192
			echo "<form action=\"$PHP_SELF\" method=\"post\">\n";
193
			echo $misc->form;
194
			echo "<input type=\"hidden\" name=\"action\" value=\"drop\" />\n";
195
			echo "<input type=\"hidden\" name=\"username\" value=\"", htmlspecialchars($_REQUEST['username']), "\" />\n";
196
			echo "<input type=\"submit\" name=\"drop\" value=\"{$lang['strdrop']}\" />\n";
197
			echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
198
			echo "</form>\n";
199
		}
200
		else {
201
			$status = $data->dropUser($_REQUEST['username']);
202
			if ($status == 0)
203
				doDefault($lang['struserdropped']);
204
			else
205
				doDefault($lang['struserdroppedbad']);
206
		}		
207
	}
208
	
209
	/**
210
	 * Displays a screen where they can enter a new user
211
	 */
212
	function doCreate($msg = '') {
213
		global $data, $misc, $username;
214
		global $PHP_SELF, $lang;
215
		
216
		if (!isset($_POST['formUsername'])) $_POST['formUsername'] = '';
217
		if (!isset($_POST['formPassword'])) $_POST['formPassword'] = '';
218
		if (!isset($_POST['formConfirm'])) $_POST['formConfirm'] = '';
219
		if (!isset($_POST['formExpires'])) $_POST['formExpires'] = '';
220
		
221
		$misc->printTrail('server');
222
		$misc->printTitle($lang['strcreateuser'],'pg.user.create');
223
		$misc->printMsg($msg);
224

    
225
		echo "<form action=\"$PHP_SELF\" method=\"post\">\n";
226
		echo $misc->form;
227
		echo "<table>\n";
228
		echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strusername']}</th>\n";
229
		echo "\t\t<td class=\"data1\"><input size=\"15\" name=\"formUsername\" value=\"", htmlspecialchars($_POST['formUsername']), "\" /></td>\n\t</tr>\n";
230
		echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strpassword']}</th>\n";
231
		echo "\t\t<td class=\"data1\"><input size=\"15\" type=\"password\" name=\"formPassword\" value=\"", htmlspecialchars($_POST['formPassword']), "\" /></td>\n\t</tr>\n";
232
		echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strconfirm']}</th>\n";
233
		echo "\t\t<td class=\"data1\"><input size=\"15\" type=\"password\" name=\"formConfirm\" value=\"", htmlspecialchars($_POST['formConfirm']), "\" /></td>\n\t</tr>\n";
234
		echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strsuper']}</th>\n";
235
		echo "\t\t<td class=\"data1\"><input type=\"checkbox\" name=\"formSuper\"", 
236
			(isset($_POST['formSuper'])) ? ' checked="checked"' : '', " /></td>\n\t</tr>\n";
237
		echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strcreatedb']}</th>\n";
238
		echo "\t\t<td class=\"data1\"><input type=\"checkbox\" name=\"formCreateDB\"", 
239
			(isset($_POST['formCreateDB'])) ? ' checked="checked"' : '', " /></td>\n\t</tr>\n";
240
		echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strexpires']}</th>\n";
241
		echo "\t\t<td class=\"data1\"><input size=\"30\" name=\"formExpires\" value=\"", htmlspecialchars($_POST['formExpires']), "\" /></td>\n\t</tr>\n";
242
		echo "</table>\n";
243
		echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create\" />\n";
244
		echo "<input type=\"submit\" value=\"{$lang['strcreate']}\" />\n";
245
		echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
246
		echo "</form>\n";
247
	}
248
	
249
	/**
250
	 * Actually creates the new user in the database
251
	 */
252
	function doSaveCreate() {
253
		global $data;
254
		global $lang;
255

    
256
		// Check data
257
		if ($_POST['formUsername'] == '')
258
			doCreate($lang['struserneedsname']);
259
		else if ($_POST['formPassword'] != $_POST['formConfirm'])
260
			doCreate($lang['strpasswordconfirm']);
261
		else {		
262
			$status = $data->createUser($_POST['formUsername'], $_POST['formPassword'], 
263
				isset($_POST['formCreateDB']), isset($_POST['formSuper']), $_POST['formExpires'], array());
264
			if ($status == 0)
265
				doDefault($lang['strusercreated']);
266
			else
267
				doCreate($lang['strusercreatedbad']);
268
		}
269
	}	
270

    
271
	/**
272
	 * Show default list of users in the database
273
	 */
274
	function doDefault($msg = '') {
275
		global $data, $misc;
276
		global $PHP_SELF, $lang;
277
		
278
		function renderUseExpires($val) {
279
			return $val == 'infinity' ? '' : htmlspecialchars($val);
280
 		}
281
		
282
		$misc->printTrail('server');
283
		$misc->printTabs('server','users');
284
		$misc->printMsg($msg);
285
		
286
		$users = $data->getUsers();
287
		
288
		$columns = array(
289
			'user' => array(
290
				'title' => $lang['strusername'],
291
				'field' => 'usename',
292
			),
293
			'superuser' => array(
294
				'title' => $lang['strsuper'],
295
				'field' => 'usesuper',
296
				'type'  => 'yesno',
297
			),
298
			'createdb' => array(
299
				'title' => $lang['strcreatedb'],
300
				'field' => 'usecreatedb',
301
				'type'  => 'yesno',
302
			),
303
			'expires' => array(
304
				'title' => $lang['strexpires'],
305
				'field' => 'useexpires',
306
				'type'  => 'callback',
307
				'params'=> array('function' => 'renderUseExpires'),
308
			),
309
			'defaults' => array(
310
				'title' => $lang['strsessiondefaults'],
311
				'field' => 'useconfig',
312
			),
313
			'actions' => array(
314
				'title' => $lang['stractions'],
315
			),
316
		);
317
		
318
		$actions = array(
319
			'alter' => array(
320
				'title' => $lang['stralter'],
321
				'url'   => "{$PHP_SELF}?action=edit&amp;{$misc->href}&amp;",
322
				'vars'  => array('username' => 'usename'),
323
			),
324
			'drop' => array(
325
				'title' => $lang['strdrop'],
326
				'url'   => "{$PHP_SELF}?action=confirm_drop&amp;{$misc->href}&amp;",
327
				'vars'  => array('username' => 'usename'),
328
			),
329
		);
330
		
331
		if (!$data->hasUserSessionDefaults()) unset($columns['defaults']);
332
		
333
		$misc->printTable($users, $columns, $actions, $lang['strnousers']);
334

    
335
		echo "<p><a class=\"navlink\" href=\"{$PHP_SELF}?action=create&amp;{$misc->href}\">{$lang['strcreateuser']}</a></p>\n";
336

    
337
	}
338

    
339
	$misc->printHeader($lang['strusers']);
340
	$misc->printBody();
341

    
342
	switch ($action) {
343
		case 'changepassword':
344
			if (isset($_REQUEST['ok'])) doChangePassword(false);
345
			else doAccount();
346
			break;
347
		case 'confchangepassword':
348
			doChangePassword(true);
349
			break;			
350
		case 'account':
351
			doAccount();
352
			break;
353
		case 'save_create':
354
			if (isset($_REQUEST['cancel'])) doDefault();
355
			else doSaveCreate();
356
			break;
357
		case 'create':			
358
			doCreate();
359
			break;
360
		case 'drop':
361
			if (isset($_REQUEST['cancel'])) doDefault();
362
			else doDrop(false);
363
			break;
364
		case 'confirm_drop':
365
			doDrop(true);
366
			break;
367
		case 'save_edit':
368
			if (isset($_REQUEST['cancel'])) doDefault();
369
			else doSaveEdit();
370
			break;
371
		case 'edit':
372
			doEdit();
373
			break;
374
		default:
375
			doDefault();
376
			break;
377
	}	
378

    
379
	$misc->printFooter();
380

    
381
?>
(51-51/53)