BeezNest 2011
*/
$cidReset = true;
require_once __DIR__.'/../inc/global.inc.php';
api_protect_session_admin_list_users();
$urlId = api_get_current_access_url_id();
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
// Login as can be used by different roles
if (isset($_GET['user_id']) && $action === 'login_as') {
$check = Security::check_token('get');
if ($check && api_can_login_as($_GET['user_id'])) {
$result = UserManager::loginAsUser($_GET['user_id']);
if ($result) {
$userInfo = api_get_user_info();
$userId = $userInfo['id'];
$message = sprintf(
get_lang('AttemptingToLoginAs'),
$userInfo['complete_name_with_username'],
'',
$userId
);
$url = api_get_path(WEB_PATH).'user_portal.php';
$goTo = sprintf(get_lang('LoginSuccessfulGoToX'), Display::url($url, $url));
Display::display_header(get_lang('UserList'));
echo Display::return_message($message, 'normal', false);
echo Display::return_message($goTo, 'normal', false);
Display::display_footer();
exit;
} else {
api_not_allowed(true);
}
}
Security::clear_token();
}
api_protect_admin_script(true);
trimVariables();
$url = api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=get_user_courses';
$urlSession = api_get_path(WEB_AJAX_PATH).'session.ajax.php?a=get_user_sessions';
$extraField = new ExtraField('user');
$variables = $extraField->get_all_extra_field_by_type(ExtraField::FIELD_TYPE_TAG);
$variablesSelect = $extraField->get_all_extra_field_by_type(ExtraField::FIELD_TYPE_SELECT);
if (!empty($variablesSelect)) {
$variables = array_merge($variables, $variablesSelect);
}
$variablesToShow = [];
if ($variables) {
foreach ($variables as $variableId) {
$extraFieldInfo = $extraField->get($variableId);
$variablesToShow[] = $extraFieldInfo['variable'];
}
}
Session::write('variables_to_show', $variablesToShow);
$htmlHeadXtra[] = '';
$this_section = SECTION_PLATFORM_ADMIN;
/**
* Trim variable values to avoid trailing spaces.
*/
function trimVariables()
{
$filterVariables = [
'keyword',
'keyword_firstname',
'keyword_lastname',
'keyword_username',
'keyword_email',
'keyword_officialcode',
];
foreach ($filterVariables as $variable) {
if (isset($_GET[$variable])) {
$_GET[$variable] = trim($_GET[$variable]);
}
}
}
/**
* Prepares the shared SQL query for the user table.
* See get_user_data() and get_number_of_users().
*
* @param bool $getCount Whether to count, or get data
*
* @return string SQL query
*/
function prepare_user_sql_query($getCount)
{
$sql = '';
$user_table = Database::get_main_table(TABLE_MAIN_USER);
$admin_table = Database::get_main_table(TABLE_MAIN_ADMIN);
$isMultipleUrl = (api_is_platform_admin() || api_is_session_admin()) && api_get_multiple_access_url();
$urlId = api_get_current_access_url_id();
if ($getCount) {
$sql .= "SELECT COUNT(u.id) AS total_number_of_items FROM $user_table u";
} else {
$sql .= 'SELECT u.id AS col0, u.official_code AS col2, ';
if (api_is_western_name_order()) {
$sql .= 'u.firstname AS col3, u.lastname AS col4, ';
} else {
$sql .= 'u.lastname AS col3, u.firstname AS col4, ';
}
// set columns name to sort
if (api_get_setting('login_is_email') === 'true') {
$sql .= " u.username AS col5,
u.email,
u.status AS col6,
u.active AS col7,
u.registration_date AS col8,
u.last_login as col9,
u.id AS col10,";
} else {
$sql .= " u.username AS col5,
u.email AS col6,
u.status AS col7,
u.active AS col8,
u.registration_date AS col9,
u.last_login as col10,
u.id AS col11,";
}
$sql .= " u.expiration_date AS exp,
u.password
FROM $user_table u";
}
// adding the filter to see the user's only of the current access_url
if ($isMultipleUrl) {
$access_url_rel_user_table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
$sql .= " INNER JOIN $access_url_rel_user_table url_rel_user
ON (u.id=url_rel_user.user_id)";
}
$classId = isset($_REQUEST['class_id']) && !empty($_REQUEST['class_id']) ? (int) $_REQUEST['class_id'] : 0;
if ($classId) {
$userGroupTable = Database::get_main_table(TABLE_USERGROUP_REL_USER);
$sql .= " INNER JOIN $userGroupTable ug ON (ug.user_id = u.id)";
}
$keywordList = [
'keyword_firstname',
'keyword_lastname',
'keyword_username',
'keyword_email',
'keyword_officialcode',
'keyword_status',
'keyword_active',
'keyword_inactive',
'check_easy_passwords',
];
$keywordListValues = [];
$atLeastOne = false;
foreach ($keywordList as $keyword) {
$keywordListValues[$keyword] = null;
if (isset($_GET[$keyword]) && !empty($_GET[$keyword])) {
$keywordListValues[$keyword] = Security::remove_XSS($_GET[$keyword]);
$atLeastOne = true;
}
}
if ($atLeastOne == false) {
$keywordListValues = [];
}
if (isset($_GET['keyword']) && !empty($_GET['keyword'])) {
$keywordFiltered = Database::escape_string("%".$_GET['keyword']."%");
$sql .= " WHERE (
u.firstname LIKE '$keywordFiltered' OR
u.lastname LIKE '$keywordFiltered' OR
concat(u.firstname, ' ', u.lastname) LIKE '$keywordFiltered' OR
concat(u.lastname,' ',u.firstname) LIKE '$keywordFiltered' OR
u.username LIKE '$keywordFiltered' OR
u.official_code LIKE '$keywordFiltered' OR
u.email LIKE '$keywordFiltered'
)
";
} elseif (isset($keywordListValues) && !empty($keywordListValues)) {
$query_admin_table = '';
$keyword_admin = '';
if (isset($keywordListValues['keyword_status']) &&
$keywordListValues['keyword_status'] == PLATFORM_ADMIN
) {
$query_admin_table = " , $admin_table a ";
$keyword_admin = ' AND a.user_id = u.id ';
$keywordListValues['keyword_status'] = '';
}
if ($keywordListValues['keyword_status'] === '%') {
$keywordListValues['keyword_status'] = '';
}
$keyword_extra_value = '';
$sql .= " $query_admin_table
WHERE ( 1 = 1 ";
if (!empty($keywordListValues['keyword_firstname'])) {
$sql .= "AND u.firstname LIKE '".Database::escape_string("%".$keywordListValues['keyword_firstname']."%")."'";
}
if (!empty($keywordListValues['keyword_lastname'])) {
$sql .= "AND u.lastname LIKE '".Database::escape_string("%".$keywordListValues['keyword_lastname']."%")."'";
}
if (!empty($keywordListValues['keyword_username'])) {
$sql .= "AND u.username LIKE '".Database::escape_string("%".$keywordListValues['keyword_username']."%")."'";
}
if (!empty($keywordListValues['keyword_email'])) {
$sql .= "AND u.email LIKE '".Database::escape_string("%".$keywordListValues['keyword_email']."%")."'";
}
if (!empty($keywordListValues['keyword_status'])) {
$sql .= "AND u.status = '".Database::escape_string($keywordListValues['keyword_status'])."'";
}
if (!empty($keywordListValues['keyword_officialcode'])) {
$sql .= " AND u.official_code LIKE '".Database::escape_string("%".$keywordListValues['keyword_officialcode']."%")."' ";
}
$sql .= " $keyword_admin $keyword_extra_value ";
if (isset($keywordListValues['keyword_active']) &&
!isset($keywordListValues['keyword_inactive'])
) {
$sql .= ' AND u.active = 1';
} elseif (isset($keywordListValues['keyword_inactive']) &&
!isset($keywordListValues['keyword_active'])
) {
$sql .= ' AND u.active = 0';
}
$sql .= ' ) ';
} else {
$sql .= ' WHERE 1 = 1 ';
}
if ($classId) {
$sql .= " AND ug.usergroup_id = $classId";
}
$preventSessionAdminsToManageAllUsers = api_get_setting('prevent_session_admins_to_manage_all_users');
$extraConditions = '';
if (api_is_session_admin() && $preventSessionAdminsToManageAllUsers === 'true') {
$extraConditions .= ' AND u.creator_id = '.api_get_user_id();
}
// adding the filter to see the user's only of the current access_url
if ($isMultipleUrl) {
$extraConditions .= ' AND url_rel_user.access_url_id = '.$urlId;
}
$sql .= $extraConditions;
$variables = Session::read('variables_to_show', []);
$extraFields = api_get_configuration_value('user_search_on_extra_fields');
if (!empty($extraFields) && isset($extraFields['extra_fields']) && isset($_GET['keyword'])) {
$extraFieldList = $extraFields['extra_fields'];
if (!empty($extraFieldList)) {
foreach ($extraFieldList as $variable) {
$_GET['extra_'.$variable] = Security::remove_XSS($_GET['keyword']);
}
}
$variables = array_merge($extraFieldList, $variables);
}
if (!empty($variables)) {
$extraField = new ExtraField('user');
$extraFieldResult = [];
$extraFieldHasData = [];
foreach ($variables as $variable) {
if (isset($_GET['extra_'.$variable])) {
if (is_array($_GET['extra_'.$variable])) {
$values = $_GET['extra_'.$variable];
} else {
$values = [$_GET['extra_'.$variable]];
}
if (empty($values)) {
continue;
}
$info = $extraField->get_handler_field_info_by_field_variable($variable);
if (empty($info)) {
continue;
}
foreach ($values as $value) {
if (empty($value)) {
continue;
}
if ($info['field_type'] == ExtraField::FIELD_TYPE_TAG) {
$result = $extraField->getAllUserPerTag($info['id'], $value);
$result = empty($result) ? [] : array_column($result, 'user_id');
} else {
$result = UserManager::get_extra_user_data_by_value($variable, $value, true);
}
$extraFieldHasData[] = true;
if (!empty($result)) {
$extraFieldResult = array_merge($extraFieldResult, $result);
}
}
}
}
$condition = ' AND ';
// If simple search then use "OR"
if (isset($_GET['keyword']) && !empty($_GET['keyword'])) {
$condition = ' OR ';
}
if (!empty($extraFieldHasData) && !empty($extraFieldResult)) {
$sql .= " $condition (u.id IN ('".implode("','", $extraFieldResult)."') $extraConditions ) ";
}
}
return $sql;
}
/**
* Get the total number of users on the platform.
*
* @see SortableTable#get_total_number_of_items()
*/
function get_number_of_users()
{
$sql = prepare_user_sql_query(true);
$res = Database::query($sql);
$obj = Database::fetch_object($res);
return $obj->total_number_of_items;
}
/**
* Get the users to display on the current page (fill the sortable-table).
*
* @param int offset of first user to recover
* @param int Number of users to get
* @param int Column to sort on
* @param string Order (ASC,DESC)
*
* @return array Users list
*
* @see SortableTable#get_table_data($from)
*/
function get_user_data($from, $number_of_items, $column, $direction)
{
$sql = prepare_user_sql_query(false);
if (!in_array($direction, ['ASC', 'DESC'])) {
$direction = 'ASC';
}
$column = (int) $column;
$from = (int) $from;
$number_of_items = (int) $number_of_items;
if (in_array($column, [0, 1, 11])) {
$column = 3;
$direction = 'ASC';
}
$sql .= " ORDER BY col$column $direction ";
$sql .= " LIMIT $from, $number_of_items";
$res = Database::query($sql);
$users = [];
$t = time();
while ($user = Database::fetch_array($res)) {
$userPicture = UserManager::getUserPicture(
$user[0],
USER_IMAGE_SIZE_SMALL
);
$photo = '';
if (1 == $user[7] && !empty($user['exp'])) {
// check expiration date
$expiration_time = convert_sql_date($user['exp']);
// if expiration date is passed, store a special value for active field
if ($expiration_time < $t) {
$user[7] = '-1';
}
}
if (api_get_configuration_value('admin_user_list_add_first_connexion_column')) {
$firstConnectionDate = Tracking::get_first_connection_date($user[0]);
if ($firstConnectionDate == '') {
$firstConnectionDate = get_lang('NoConnexion');
}
// forget about the expiration date field
$users[] = [
$user[0], // id
$photo,
$user[1],
$user[2],
$user[3],
$user[4], // username
$user[5], // email
$user[6],
$user[7], // active
api_get_local_time($user[8]),
api_get_local_time($user[9], null, null, true),
$firstConnectionDate,
$user[0],
];
} else {
// forget about the expiration date field
$users[] = [
$user[0], // id
$photo,
$user[1],
$user[2],
$user[3],
$user[4], // username
$user[5], // email
$user[6],
$user[7], // active
api_get_local_time($user[8]),
api_get_local_time($user[9], null, null, true),
$user[0],
];
}
}
return $users;
}
/**
* Returns a mailto-link.
*
* @param string $email An email-address
*
* @return string HTML-code with a mailto-link
*/
function email_filter($email)
{
return Display::encrypted_mailto_link($email, cut($email, 26), 'small clickable_email_link');
}
/**
* Returns a mailto-link.
*
* @param string $name
* @param array $params Deprecated
* @param array $row
*
* @return string HTML-code with a mailto-link
*/
function user_filter($name, $params, $row)
{
$name = cut($name, 26);
return ''.$name.'';
}
/**
* Build the modify-column of the table.
*
* @param int The user id
* @param string URL params to add to table links
* @param array Row of elements to alter
*
* @throws Exception
*
* @return string Some HTML-code with modify-buttons
*/
function modify_filter($user_id, $url_params, $row)
{
$_admins_list = Session::read('admin_list', []);
$is_admin = in_array($user_id, $_admins_list);
$statusname = api_get_status_langvars();
$user_is_anonymous = false;
$current_user_status_label = $statusname[$row['7']];
if ($current_user_status_label == $statusname[ANONYMOUS]) {
$user_is_anonymous = true;
}
$result = '';
if (!$user_is_anonymous) {
$icon = Display::return_icon(
'course.png',
get_lang('Courses'),
['onmouseout' => 'clear_course_list (\'div_'.$user_id.'\')']
);
$result .= '
'.$icon.'