summaryrefslogtreecommitdiff
path: root/admin/index.php
blob: dd2a1cab378f812a07ed9547b6c2b5a6d478468b (plain)
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
<?php
// $Header: /cvsroot/bitweaver/_bit_users/admin/index.php,v 1.22 2007/11/14 15:30:34 joasch Exp $
// Copyright (c) 2002-2003, Luis Argerich, Garland Foster, Eduardo Polidor, et. al.
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// Initialization
require_once( '../../bit_setup_inc.php' );

function batchImportUsers() {
	global $gBitSmarty, $gBitUser, $gBitSystem;

	// get the delimiter if it's set - use comma if it not
	$delimiter = !empty( $_REQUEST['delimiter'] ) ? $_REQUEST['delimiter'] : ",";
	$fname = $_FILES['csvlist']['tmp_name'];
	$fhandle = fopen( $fname, "r" );

	//Get the field names
	$fields = fgetcsv( $fhandle, 1000, $delimiter );

	// is the file a valid CSV file?
	if( empty( $fields[0] ) ) {
		$gBitSystem->fatalError( tra( "The file is not a CSV file or has not a correct syntax" ));
	}

	//now load the users in a table
	while( !feof( $fhandle ) ) {
		if( $data = fgetcsv( $fhandle, 1000, $delimiter ) ) {
			for( $i = 0; $i < count( $fields ); $i++ ) {
				@$ar[$fields[$i]] = $data[$i];
			}
			$userRecords[] = $ar;
		}
	}
	fclose( $fhandle );

	// were there any users in the list?
	if( !is_array( $userRecords ) ) {
		$gBitSystem->fatalError( tra( "No records were found. Check the file please!" ));
	}
	// Process user array
	$added = 0;
	$i = 1;
	foreach( $userRecords as $userRecord ) {
		$newUser = new BitPermUser();
		if( $newUser->importUser( $userRecord ) ) {
			if( !empty( $userRecord['groups'] ) ) {
				// groups need to be separated by spaces since this is a csv file
				$groups = explode( " ", $userRecord['groups'] );
				foreach( $groups as $group ) {
					if( $groupId = $gBitUser->groupExists( $group, ROOT_USER_ID ) ) {
						$newUser->addUserToGroup( $newUser->mUserId, $groupId );
					}
				}
			}
			if( empty( $_REQUEST['admin_noemail_user'] ) ) {
				$ret = adminEmailUser( $userRecord );
				if( is_array( $ret ) ) {
					list($key, $val) = each($ret);
					$newUser->mLogs[$key] = $val;
				}
				$logHash['action_log']['title'] = $userRecord['login'];
				$newUser->storeActionLog( $logHash );
			}

			$added++;
		} else {
			$discarded[$i] = implode( ',', $newUser->mErrors );
		}
		unset( $newUser );
		$i++;
	}

	$gBitSmarty->assign( 'added', $added );
	if( @is_array( $discarded ) ) {
		$gBitSmarty->assign( 'discarded', count( $discarded ) );
		$gBitSmarty->assign_by_ref( 'discardlist', $discarded );
	}
}

function adminEmailUser( &$pParamHash ) {
	global $gBitSmarty, $gBitSystem;

	$ret = FALSE;
	$siteName = $gBitSystem->getConfig('site_title', $_SERVER['HTTP_HOST'] );
	$gBitSmarty->assign('siteName',$_SERVER["SERVER_NAME"]);
	$gBitSmarty->assign('mail_site',$_SERVER["SERVER_NAME"]);
	$gBitSmarty->assign('mail_user',$pParamHash['login']);
	if( !empty( $_REQUEST['admin_verify_user'] ) && !empty($pParamHash['user_store']['provpass']) ) {
		$apass = addslashes(substr(md5($gBitSystem->genPass()),0,25));
		$apass = $pParamHash['user_store']['provpass'];
		$machine = httpPrefix().USERS_PKG_URL.'confirm.php';
		// Send the mail
		$gBitSmarty->assign('mail_machine',$machine);
		$gBitSmarty->assign('mailUserId',$pParamHash['user_store']['user_id']);
		$gBitSmarty->assign('mailProvPass',$apass);
		$mail_data = $gBitSmarty->fetch('bitpackage:users/admin_validation_mail.tpl');
		mail($pParamHash['email'], $siteName.' - '.tra('Your registration information'),$mail_data,"From: ".$gBitSystem->getConfig('site_sender_email')."\r\nContent-type: text/plain;charset=utf-8\r\n");
		$gBitSmarty->assign('showmsg','n');
		
		$ret = array('confirm' => 'Validation email sent to ' . $pParamHash['email'] . '.');
	} elseif( !empty( $pParamHash['password'] ) ) {
		// Send the welcome mail
		$gBitSmarty->assign( 'mailPassword',$pParamHash['password'] );
		$gBitSmarty->assign( 'mailEmail',$pParamHash['email'] );
		$mail_data = $gBitSmarty->fetch('bitpackage:users/admin_welcome_mail.tpl');
		mail($pParamHash["email"], tra( 'Welcome to' ).' '.$siteName,$mail_data,"From: ".$gBitSystem->getConfig('site_sender_email')."\r\nContent-type: text/plain;charset=utf-8\r\n");
		$ret = array('welcome'=>'Welcome email sent to ' . $pParamHash['email'] . '.');
	}
	return $ret;
}

$gBitSystem->verifyPermission( 'p_users_admin' );

$feedback = array();

// Process the form to add a user here
if( isset($_REQUEST["newuser"] ) ) {
	$userRecord = $_REQUEST;
	$newUser = new BitPermUser();
	
	if( $newUser->importUser( $userRecord ) ) {
		$gBitSmarty->assign( 'addSuccess', "User Added Successfully" );
		if( empty( $_REQUEST['admin_noemail_user'] ) ) {
			$ret = adminEmailUser( $userRecord );
			if( is_array( $ret ) ) {
				list($key, $val) = each($ret);
				$newUser->mLogs[$key] = $val;
			}
			$logHash['action_log']['title'] = $userRecord['login'];
			$newUser->storeActionLog( $logHash );
		}
	} else {
		$gBitSmarty->assign_by_ref( 'newUser', $_REQUEST );
		$gBitSmarty->assign( 'errors', $newUser->mErrors );
	}
	// if no user data entered, check if it's a batch upload
} elseif( isset( $_REQUEST["batchimport"]) ) {
	if( $_FILES['csvlist']['size'] && is_uploaded_file($_FILES['csvlist']['tmp_name'] ) ) {
		batchImportUsers();
	}
} elseif( isset( $_REQUEST["assume_user"]) && $gBitUser->hasPermission( 'p_users_admin' ) ) {
	$assume_user = (is_numeric( $_REQUEST["assume_user"] )) ? array( 'user_id' => $_REQUEST["assume_user"] ) : array('login' => $_REQUEST["assume_user"]) ;
	$userInfo = $gBitUser->getUserInfo( $assume_user );
	if( isset( $_REQUEST["confirm"] ) ) {
		$gBitUser->verifyTicket();
		if( $gBitUser->assumeUser( $userInfo["user_id"] ) ) {
			header( 'Location: '.$gBitSystem->getDefaultPage() );
			die;
		}
	} else {
		$gBitSystem->setBrowserTitle( 'Assume User Identity' );
		$formHash['assume_user'] = $_REQUEST['assume_user'];
		$msgHash = array(
			'confirm_item' => tra( 'This will log you in as the user' )." <strong>$userInfo[real_name] ($userInfo[login])</strong>",
		);
		$gBitSystem->confirmDialog( $formHash,$msgHash );
	}
} elseif( !empty( $_REQUEST['find'] ) ) {
	$title = 'Find Users';
}

// Process actions here
// Remove user or remove user from group
if( isset( $_REQUEST["action"] ) ) {
	$formHash['action'] = $_REQUEST['action'];
	if( !empty( $_REQUEST['batch_user_ids'] ) && is_array( $_REQUEST['batch_user_ids'] ) ) {
		$gBitUser->verifyTicket();
		if( isset( $_REQUEST["confirm"] ) ) {
			$delUsers = $errDelUsers = "";
			$userClass = $gBitSystem->getConfig( 'user_class', 'BitPermUser' );
			foreach( $_REQUEST['batch_user_ids'] as $uid ) {
				$expungeUser = new $userClass( $uid );
				$userInfo = $gBitUser->getUserInfo( array( 'user_id' => $uid ) );
				if( $expungeUser->load() && $expungeUser->expunge() ) {
					$delUsers .= "<li>{$userInfo['real_name']} ({$userInfo['login']})</li>";
				} else {
					$errDelUsers .= "<li>{$userInfo['real_name']} ({$userInfo['login']})</li>";
				}
			}

			if( !empty( $delUsers ) ) {
				$feedback['success'][] = tra( 'Users deleted' ).": <ul>$delUsers</ul>";
			} elseif( !empty( $errDelUsers ) ) {
				$feedback['error'][] = tra( 'Users not deleted' ).": <ul>$errDelUsers</ul>";
			}
		} else {
			foreach( $_REQUEST['batch_user_ids'] as $uid ) {
				$userInfo = $gBitUser->getUserInfo( array( 'user_id' => $uid ) );
				$formHash['input'][] = '<input type="hidden" name="batch_user_ids[]" value="'.$uid.'"/>'."{$userInfo['real_name']} ({$userInfo['login']})";
			}
			$gBitSystem->setBrowserTitle( 'Delete users' );
			$msgHash = array(
				'confirm_item' => tra( 'Are you sure you want to remove these users?' ),
				'warning' => tra( 'This will permentally delete these users' ),
			);
			$gBitSystem->confirmDialog( $formHash, $msgHash );
		}
	} elseif( $_REQUEST["action"] == 'delete' ) {
		$gBitUser->verifyTicket();
		$formHash['user_id'] = $_REQUEST['user_id'];
		$userInfo = $gBitUser->getUserInfo( array( 'user_id' => $_REQUEST["user_id"] ) );
		if( !empty( $userInfo['user_id'] ) ) {
			if( isset( $_REQUEST["confirm"] ) ) {
				$userClass = $gBitSystem->getConfig( 'user_class', 'BitPermUser' );
				$expungeUser = new $userClass( $_REQUEST["user_id"] );
				if( $expungeUser->load() && $expungeUser->expunge() ) {
					$feedback['success'][] = tra( 'User deleted' )." <strong>{$userInfo['real_name']} ({$userInfo['login']})</strong>";
				}
			} else {
				$gBitSystem->setBrowserTitle( 'Delete user' );
				$msgHash = array(
					'confirm_item' => tra( 'Are you sure you want to remove the user?' ),
					'warning' => tra( 'This will permentally delete the user' )." <strong>$userInfo[real_name] ($userInfo[login])</strong>",
				);
				$gBitSystem->confirmDialog( $formHash,$msgHash );
			}
		} else {
			$feedback['error'][] = tra( 'User not found' );
		}
	}
	if ($_REQUEST["action"] == 'removegroup') {
		$gBitUser->removeUserFromGroup($_REQUEST["user"], $_REQUEST["group"]);
	}
}

// get default group and pass it to tpl
foreach( $gBitUser->getDefaultGroup() as $defaultGroupId => $defaultGroupName ) {
	$gBitSmarty->assign('defaultGroupId', $defaultGroupId );
	$gBitSmarty->assign('defaultGroupName', $defaultGroupName );
}

// override default max_records
$_REQUEST['max_records'] = !empty( $_REQUEST['max_records'] ) ? $_REQUEST['max_records'] : 20;
$gBitUser->getList( $_REQUEST );
$gBitSmarty->assign_by_ref('users', $_REQUEST["data"]);
$gBitSmarty->assign_by_ref('usercount', $_REQUEST["cant"]);
if (isset($_REQUEST["numrows"])) {
	$_REQUEST['listInfo']["numrows"] = $_REQUEST["numrows"];
} else {
	$_REQUEST['listInfo']["numrows"] = 10;
}
$_REQUEST['listInfo']["URL"] = USERS_PKG_URL."admin/index.php";
$gBitSmarty->assign_by_ref('listInfo', $_REQUEST['listInfo']);

$gBitUser->invokeServices( 'content_edit_function' );

// Get groups (list of groups)
$grouplist = $gBitUser->getGroups('', '', 'group_name_asc');
$gBitSmarty->assign( 'grouplist', $grouplist );
$gBitSmarty->assign( 'feedback', $feedback );

$gBitSmarty->assign( (!empty( $_REQUEST['tab'] ) ? $_REQUEST['tab'] : 'userlist').'TabSelect', 'tdefault' );

// Display the template
$gBitSystem->display( 'bitpackage:users/users_admin.tpl', (!empty( $title ) ? $title : 'Edit Users' ) );
?>