Project

General

Profile

« Previous | Next » 

Revision 1815

Added by Dietmar almost 12 years ago

! Complex code refactoring users management

View differences:

index.php
15 15
 *
16 16
 */
17 17

  
18
$config_file = realpath('../../config.php');
19
if(file_exists($config_file) && !defined('WB_URL'))
20
{
21
	require_once($config_file);
22
}
18
    /**
19
     * checks if a given string is part of a line in a defined file
20
     * @param string $sString
21
     * @param string $sListFile
22
     * @return bool TRUE if at least one match is found, otherwise FALSE
23
     */
24
    function findStringInFileList( $sString, $sListFile)
25
    {
26
     $aMatch = array();
27
     if(is_readable($sListFile)) {
28
      $aList = file($sListFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
29
      $aMatch = preg_grep('/'.preg_quote($sString, '/').'/i',$aList);
30
     }
31
     return (sizeof($aMatch)>0);
32
    }
23 33

  
24
if(!class_exists('admin', false)){ include(WB_PATH.'/framework/class.admin.php'); }
34
	function admin_users_index($aActionRequest)
35
	{
36
		global $MESSAGE;
37
		$database = WbDatabase::getInstance();
25 38

  
26
$admin = new admin('Access', 'users');
39
        $sAdminPath = dirname(str_replace('\\', '/', __FILE__));
40
        $sAdminName = basename($sAdminPath);
41
        $output = '';
42
        $aActionRequest['requestMethod'] = '_'.strtoupper($_SERVER['REQUEST_METHOD']);
43
        $action = 'show';
44
        // Set parameter 'action' as alternative to javascript mechanism
45
        $action = (isset($aActionRequest['add'])    ? 'add'    : $action );
46
        $action = (isset($aActionRequest['save'])   ? 'save'   : $action );
47
        $action = (isset($aActionRequest['modify']) ? 'modify' : $action );
48
        $action = (isset($aActionRequest['delete']) ? 'delete' : $action );
49
        $action = (isset($aActionRequest['delete_outdated']) ? 'delete_outdated' : $action );
27 50

  
28
$iUserStatus = 1;
29
$iUserStatus = ( ( $admin->get_get('status')==1 ) ? 0 : $iUserStatus );
30
unset($_GET);
51
		switch($action) :
52
			case 'delete': // delete the user
53
    			$admin = new admin('Access', 'users_delete',false);
31 54

  
32
// Setup template object, parse vars to it, then parse it
33
// Create new template object
34
$template = new Template(dirname($admin->correct_theme_source('users.htt')),'keep');
35
// $template->debug = true;
55
				include($sAdminPath.'/delete.php');
56
    			$output = delete_user($admin,$aActionRequest);
36 57

  
37
$template->set_file('page', 'users.htt');
38
$template->set_block('page', 'main_block', 'main');
39
$template->set_block("main_block", "manage_groups_block", "groups");
40
$template->set_var('ADMIN_URL', ADMIN_URL);
41
$template->set_var('FTAN', $admin->getFTAN());
42
$template->set_var('USER_STATUS', $iUserStatus );
43
$template->set_var('DISPLAY_ADD', '');
44
$template->set_var('DISPLAY_MODIFY', '');
45
$template->set_var('DISABLED_CHECKED', '');
46
$template->set_var('HEADING_MODIFY_USER', '');
47
$template->set_var('DISPLAY_HOME_FOLDERS', '');
58
        		if( ($msg = msgQueue::getError()) != '')
59
        		{
60
        		}
48 61

  
49
$UserStatusActive = 'url('.THEME_URL.'/images/user.png)';
50
$UserStatusInactive = 'url('.THEME_URL.'/images/user_red.png)';
62
                $aActionRequest['cancel_url'] = ADMIN_URL.'/access/index.php';
63
				$admin = new admin('Access', 'users');
64
				include($sAdminPath.'/user_list.php');
65
				$output .= show_userlist($admin, $aActionRequest);
66
				break;
67
			case 'add': // insert/update user
68
                $admin = new admin('Access', 'users_add',false);
69
				include($sAdminPath.'/add.php');
70
    			$output = add_user($admin,$aActionRequest);
71
                $aActionRequest['cancel_url'] = ADMIN_URL.'/access/index.php';
72
				$admin = new admin('Access', 'users');
73
				include($sAdminPath.'/user_list.php');
74
				$output .= show_userlist($admin, $aActionRequest);
75
				break;
76
			case 'save': // insert/update user
77
    			$admin = new admin('Access', 'users_modify',false);
78
// hold the cancel_url if request comes outside from users
79
                if(isset($aActionRequest['BackLink'])) {
80
                    $sBackLink = $aActionRequest['BackLink'];
81
                    $aActionRequest['cancel_url'] = $sBackLink;
82
                    $aActionRequest['BackLink'] = $sBackLink;
83
                }
84
     			include($sAdminPath.'/save.php');
85
                $user_id = save_user($admin, $aActionRequest);
86
    			$admin = new admin('Access', 'users_modify');
87
     			include($sAdminPath.'/user_form.php');
88
                $aActionRequest['user_id'] = $user_id;
89
    			$output = show_usermask($admin,$aActionRequest);
90
				break;
91
			case 'modify': // insert/update user
92
// first check acess to auth users can change his own preferences
93
    			$admin = new admin('Preferences', 'preferences_view',false);
94
    			$user_id = intval($admin->checkIDKEY('user_id', 0, $_SERVER['REQUEST_METHOD']));
95
// Check if user id is a valid number and doesnt equal 1
96
                $aActionRequest['user_id'] = $user_id;
97
    			if($user_id == 0){
98
        			msgQueue::add($MESSAGE['GENERIC_FORGOT_OPTIONS'] );
99
                }
51 100

  
52
$sUserTitle = ($iUserStatus == 0) ? $MENU['USERS'].' '.strtolower($TEXT['ACTIVE']) : $MENU['USERS'].' '.strtolower($TEXT['DELETED']) ;
101
    			if( ($user_id == $admin->get_user_id() ) )
102
    			{
103
                    $sQueryString = (isset($_SERVER['QUERY_STRING'])&& ($_SERVER['QUERY_STRING']!='')) ? $_SERVER['QUERY_STRING'] :  'tool=uaerat';
104
                    $admin->send_header(ADMIN_URL.'/preferences/index.php?'.$sQueryString);
105
    			}
53 106

  
54
$template->set_var('TEXT_USERS', $sUserTitle.' '.$TEXT['SHOW'] );
55
$template->set_var('STATUS_ICON', ( ($iUserStatus==0) ? $UserStatusActive : $UserStatusInactive) );
107
    			$admin = new admin('Access', 'users_modify');
56 108

  
57
// Get existing value from database
58
$sql  = 'SELECT `user_id`, `username`, `display_name`, `active` FROM `'.TABLE_PREFIX.'users` ' ;
59
$sql .= 'WHERE user_id != 1 ';
60
$sql .=     'AND active = '.$iUserStatus.' ';
61
$sql .= 'ORDER BY `display_name`,`username`';
109
    			if( ($user_id < 2 ) )
110
    			{
111
    				// if($admin_header) { $admin->print_header(); }
112
    				msgQueue::add($MESSAGE['GENERIC_SECURITY_ACCESS'] );
113
    			}
114
                $admin_header = false;
115
                if(isset($aActionRequest['BackLink'])) {
116
                    $sBackLink = $aActionRequest['BackLink'];
117
                    $aActionRequest['cancel_url'] = $sBackLink;
118
                    $aActionRequest['BackLink']   = $sBackLink;
119
                } else {
120
                    $sBackLink = (isset($_SERVER['QUERY_STRING'])&& ($_SERVER['QUERY_STRING']!='')) ? $_SERVER['HTTP_REFERER'].'?'.$_SERVER['QUERY_STRING'] :  $_SERVER['HTTP_REFERER'];
121
                    $aActionRequest['cancel_url'] = $sBackLink;
122
                    $aActionRequest['BackLink']   = $sBackLink;
123
                }
124
     			include($sAdminPath.'/user_form.php');
125
    			$output = show_usermask($admin,$aActionRequest);
126
				break;
127
			default: // show userlist with empty modify mask
128
				$admin = new admin('Access', 'users');
129
				msgQueue::clear();
130
    			$user_id = intval($admin->checkIDKEY('user_id', 0, $_SERVER['REQUEST_METHOD']));
131
    			// Check if user id is a valid number and doesnt equal 1
132
                $aActionRequest['user_id'] = $user_id;
133
                $aActionRequest['cancel_url'] = ADMIN_URL.'/access/index.php';
62 134

  
63
$query = "SELECT user_id, username, display_name, active FROM ".TABLE_PREFIX."users WHERE user_id != '1' ORDER BY display_name,username";
64
$results = $database->query($sql);
65
if($database->is_error()) {
66
	$admin->print_error($database->get_error(), 'index.php');
67
}
135
				if($user_id > 1) // prevent 'admin' [ID 1] from modify
136
				{
137
					include($sAdminPath.'/user_form.php');
138
					$output .= show_usermask($admin, $aActionRequest);
139
				} elseif($user_id == 0) { // if invalid UserID is called, fall back to 'show-mode'
140
					include($sAdminPath.'/user_list.php');
141
					$output  = show_userlist($admin, $aActionRequest);
142
				}
143
		endswitch; // end of switch
144
		if( ($msg = msgQueue::getSuccess()) != '')
145
		{
146
			$output = $admin->format_message($msg, 'ok').$output;
147
		}
148
		if( ($msg = msgQueue::getError()) != '')
149
		{
150
			$output = $admin->format_message($msg, 'error').$output;
151
		}
152
		print $output;
153
		$admin->print_footer();
154
    }
68 155

  
69
$sUserList  = $TEXT['LIST_OPTIONS'].' ';
70
$sUserList .= ($iUserStatus == 1) ? $MENU['USERS'].' '.strtolower($TEXT['ACTIVE']) : $MENU['USERS'].' '.strtolower($TEXT['DELETED']) ;
71
// Insert values into the modify/remove menu
72
$template->set_block('main_block', 'list_block', 'list');
73
if($results->numRows() > 0) {
74
	// Insert first value to say please select
75
	$template->set_var('VALUE', '');
76
	$template->set_var('NAME', $sUserList);
77
	$template->set_var('STATUS', 'class="user-active"' );
78
	$template->parse('list', 'list_block', true);
79
	// Loop through users
80
	while($user = $results->fetchRow(MYSQL_ASSOC)) {
81
		$template->set_var('VALUE',$admin->getIDKEY($user['user_id']));
82
		$template->set_var('STATUS', ($user['active']==false ? 'class="user-inactive"' : 'class="user-active"') );
83
		$template->set_var('NAME', $user['display_name'].' ('.$user['username'].')');
84
		$template->parse('list', 'list_block', true);
85
	}
86
} else {
87
	// Insert single value to say no users were found
88
	$template->set_var('NAME', $TEXT['NONE_FOUND']);
89
	$template->parse('list', 'list_block', true);
90
}
156
	if(!defined('WB_URL'))
157
	{
158
        $config_file = realpath('../../config.php');
159
        if(file_exists($config_file) && !defined('WB_URL'))
160
        {
161
        	require($config_file);
162
        }
163
    }
164
    if(!class_exists('admin', false)){ include(WB_PATH.'/framework/class.admin.php'); }
91 165

  
92
// Insert permissions values
93
if($admin->get_permission('users_add') != true) {
94
	$template->set_var('DISPLAY_ADD', 'hide');
95
}
96
if($admin->get_permission('users_modify') != true) {
97
	$template->set_var('DISPLAY_MODIFY', 'hide');
98
}
99
if($admin->get_permission('users_delete') != true) {
100
	$template->set_var('DISPLAY_DELETE', 'hide');
101
}
102
$HeaderTitle = $HEADING['MODIFY_DELETE_USER'].' ';
103
$HeaderTitle .= (($iUserStatus == 1) ? strtolower($TEXT['ACTIVE']) : strtolower($TEXT['DELETED']));
104
// Insert language headings
105
$template->set_var(array(
106
		'HEADING_MODIFY_DELETE_USER' => $HeaderTitle,
107
		'HEADING_ADD_USER' => $HEADING['ADD_USER']
108
		)
109
);
110
// insert urls
111
$template->set_var(array(
112
		'ADMIN_URL' => ADMIN_URL,
113
		'WB_URL' => WB_URL,
114
		'THEME_URL' => THEME_URL
115
		)
116
);
117
// Insert language text and messages
118
$template->set_var(array(
119
		'DISPLAY_WAITING_ACTIVATION' => '',
120
		'TEXT_MODIFY' => $TEXT['MODIFY'],
121
		'TEXT_DELETE' => $TEXT['DELETE'],
122
		'TEXT_MANAGE_GROUPS' => ( $admin->get_permission('groups') == true ) ? $TEXT['MANAGE_GROUPS'] : "**",
123
		'CONFIRM_DELETE' => (($iUserStatus == 1) ? $TEXT['ARE_YOU_SURE'] : $MESSAGE['USERS_CONFIRM_DELETE'])
124
		)
125
);
166
    $requestMethod = '_'.strtoupper($_SERVER['REQUEST_METHOD']);
167
    $aActionRequest = (isset(${$requestMethod})) ? ${$requestMethod} : null;
126 168

  
127
$template->set_block('main_block', 'show_confirmed_activation_block', 'show_confirmed_activation');
128
if($admin->ami_group_member('1')) {
129
        $template->set_block('show_confirmed_activation_block', 'list_confirmed_activation_block', 'list_confirmed_activation');
130
    	$template->set_var('DISPLAY_WAITING_ACTIVATION', 'Users waiting for activation');
131
		$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'users` ';
132
		$sql .= 'WHERE `confirm_timeout` != 0 ';
133
        $sql .=   'AND `active` = 0 ';
134
        $sql .=   'AND `user_id` != 1 ';
135
        if( ($oRes = $database->query($sql)) ) {
136
        	$template->set_var('DISPLAY_DELETE', '');
137
//        	$template->set_var('NAME', 'User waiting for activation');
138
//        	$template->set_var('STATUS', '' );
139
        	// Loop through users
140
            if($nNumRows = $oRes->numRows()) {
141
            	while($aUser = $oRes->fetchRow(MYSQL_ASSOC)) {
142
            		$template->set_var('VALUE',$admin->getIDKEY($aUser['user_id']));
143
               		$template->set_var('STATUS', '') ;
144
            		$template->set_var('NAME', $aUser['display_name'].' ('.$aUser['username'].')'.' ['.$aUser['email'].']');
145
            		$template->parse('list_confirmed_activation', 'list_confirmed_activation_block', true);
146
            	}
147
            	$template->parse('show_confirmed_activation', 'show_confirmed_activation_block',true);
148
            }
149
        } else { $nNumRows = 0; }
150

  
151
} else {
152
$nNumRows = 0;
153
}
154

  
155
if ( $nNumRows == 0){
156
	$template->parse('show_confirmed_activation', '');
157
}
158

  
159
if ( $admin->get_permission('groups') == true ) $template->parse("groups", "manage_groups_block", true);
160
// Parse template object
161
$template->parse('main', 'main_block', false);
162
$template->pparse('output', 'page');
163

  
164
// Setup template object, parse vars to it, then parse it
165
// Create new template object
166
$template = new Template(dirname($admin->correct_theme_source('users_form.htt')),'keep');
167
// $template->debug = true;
168
$template->set_file('page', 'users_form.htt');
169
$template->set_block('page', 'main_block', 'main');
170
$template->set_block('main_block', 'show_modify_loginname_block', 'show_modify_loginname');
171
$template->set_block('main_block', 'show_add_loginname_block', 'show_add_loginname');
172
$template->set_var('DISPLAY_EXTRA', 'display:none;');
173
$template->set_var('ACTIVE_CHECKED', ' checked="checked"');
174

  
175
$template->set_var('DISPLAY_ADD', '');
176
$template->set_var('DISPLAY_MODIFY', '');
177
$template->set_var('DISABLED_CHECKED', '');
178
$template->set_var('HEADING_MODIFY_USER', '');
179
$template->set_var('DISPLAY_HOME_FOLDERS', '');
180
$template->set_var('ACTION_URL', ADMIN_URL.'/users/add.php');
181
$template->set_var('SUBMIT_TITLE', $TEXT['ADD']);
182
$template->set_var('FTAN', $admin->getFTAN());
183
// insert urls
184
$template->set_var(array(
185
		'USER_ID' => '',
186
		'USERNAME' => '',
187
		'DISPLAY_NAME' => '',
188
		'EMAIL' => '',
189
		'ADMIN_URL' => ADMIN_URL,
190
		'WB_URL' => WB_URL,
191
		'THEME_URL' => THEME_URL
192
		)
193
);
194

  
195
// Add groups to list
196
$template->set_block('main_block', 'group_list_block', 'group_list');
197
$results = $database->query("SELECT group_id, name FROM ".TABLE_PREFIX."groups WHERE group_id != '1'");
198
if($results->numRows() > 0) {
199
	$template->set_var('ID', '');
200
	$template->set_var('NAME', $TEXT['PLEASE_SELECT'].'...');
201
	$template->set_var('SELECTED', ' selected="selected"');
202
	$template->parse('group_list', 'group_list_block', true);
203
	while($group = $results->fetchRow()) {
204
		$template->set_var('ID', $group['group_id']);
205
		$template->set_var('NAME', $group['name']);
206
		$template->set_var('SELECTED', '');
207
		$template->parse('group_list', 'group_list_block', true);
208
	}
209
}
210
// Only allow the user to add a user to the Administrators group if they belong to it
211
if(in_array(1, $admin->get_groups_id())) {
212
	$users_groups = $admin->get_groups_name();
213
	$template->set_var('ID', '1');
214
	$template->set_var('NAME', $users_groups[1]);
215
	$template->set_var('SELECTED', '');
216
	$template->parse('group_list', 'group_list_block', true);
217
} else {
218
	if($results->numRows() == 0) {
219
		$template->set_var('ID', '');
220
		$template->set_var('NAME', $TEXT['NONE_FOUND']);
221
		$template->parse('group_list', 'group_list_block', true);
222
	}
223
}
224

  
225
// Insert permissions values
226
if($admin->get_permission('users_add') != true) {
227
	$template->set_var('DISPLAY_ADD', 'hide');
228
}
229

  
230
// Generate username field name
231
$username_fieldname = 'username_';
232
$salt = "abchefghjkmnpqrstuvwxyz0123456789";
233
srand((double)microtime()*1000000);
234
$i = 0;
235
while ($i <= 7) {
236
	$num = rand() % 33;
237
	$tmp = substr($salt, $num, 1);
238
	$username_fieldname = $username_fieldname . $tmp;
239
	$i++;
240
}
241

  
242
// Work-out if home folder should be shown
243
if(!HOME_FOLDERS) {
244
	$template->set_var('DISPLAY_HOME_FOLDERS', 'display:none;');
245
}
246

  
247
// Include the WB functions file
248
require_once(WB_PATH.'/framework/functions.php');
249

  
250
// Add media folders to home folder list
251
$template->set_block('main_block', 'folder_list_block', 'folder_list');
252
foreach(directory_list(WB_PATH.MEDIA_DIRECTORY) AS $name) {
253
	$template->set_var('NAME', str_replace(WB_PATH, '', $name));
254
	$template->set_var('FOLDER', str_replace(WB_PATH.MEDIA_DIRECTORY, '', $name));
255
	$template->set_var('SELECTED', ' ');
256
	$template->parse('folder_list', 'folder_list_block', true);
257
}
258

  
259
// Insert language text and messages
260
$template->set_var(array(
261
			'TEXT_CANCEL' => $TEXT['CANCEL'],
262
			'TEXT_RESET' => $TEXT['RESET'],
263
			'TEXT_ACTIVE' => $TEXT['ACTIVE'],
264
			'TEXT_DISABLED' => $TEXT['DISABLED'],
265
			'TEXT_PLEASE_SELECT' => $TEXT['PLEASE_SELECT'],
266
			'TEXT_USERNAME' => $TEXT['USERNAME'],
267
			'TEXT_PASSWORD' => $TEXT['PASSWORD'],
268
			'TEXT_RETYPE_PASSWORD' => $TEXT['RETYPE_PASSWORD'],
269
			'TEXT_DISPLAY_NAME' => $TEXT['DISPLAY_NAME'],
270
			'TEXT_EMAIL' => $TEXT['EMAIL'],
271
			'TEXT_GROUP' => $TEXT['GROUP'],
272
			'TEXT_NONE' => $TEXT['NONE'],
273
			'TEXT_HOME_FOLDER' => $TEXT['HOME_FOLDER'],
274
			'USERNAME_FIELDNAME' => $username_fieldname,
275
			'CHANGING_PASSWORD' => $MESSAGE['USERS_CHANGING_PASSWORD']
276
			)
277
	);
278

  
279
// Parse template for add user form
280
$template->parse('show_modify_loginname', '', true);
281
$template->parse('show_add_loginname', 'show_add_loginname_block', true);
282
$template->parse('main', 'main_block', false);
283
$template->pparse('output', 'page');
284

  
285
$admin->print_footer();
169
	admin_users_index($aActionRequest);
170
	exit;
171
// end of file

Also available in: Unified diff