| 1 |
<?php |
<?php |
| 2 |
// $Id: masquerade.module,v 1.16.2.28 2009/09/10 20:58:21 deviantintegral Exp $ |
// $Id: masquerade.module,v 1.16.2.29 2009/10/02 15:56:53 deviantintegral Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file masquerade.module |
* @file masquerade.module |
| 62 |
function masquerade_menu() { |
function masquerade_menu() { |
| 63 |
$items = array(); |
$items = array(); |
| 64 |
|
|
| 65 |
$default_test_user = _masquerade_test_user(); |
$default_test_user = _masquerade_user_load(variable_get('masquerade_test_user', '')); |
| 66 |
if ($default_test_user->uid) { |
if ($default_test_user && ($default_test_user->uid || $default_test_user->name == variable_get('anonymous', t('Anonymous')))) { |
| 67 |
$items['masquerade/switch/' . $default_test_user->uid] = array( |
$items['masquerade/switch/' . $default_test_user->uid] = array( |
| 68 |
'title' => 'Masquerade as @testuser', |
'title' => 'Masquerade as @testuser', |
| 69 |
'title arguments' => array('@testuser' => $default_test_user->name), |
'title arguments' => array('@testuser' => $default_test_user->name), |
| 70 |
'page callback' => 'masquerade_switch_user', |
'page callback' => 'masquerade_switch_user', |
| 71 |
'page arguments' => array(2), |
'page arguments' => array(2), |
| 72 |
'access callback' => 'masquerade_access', |
'access callback' => 'masquerade_access', |
| 173 |
'#default_value' => variable_get('masquerade_admin_roles', array()), |
'#default_value' => variable_get('masquerade_admin_roles', array()), |
| 174 |
); |
); |
| 175 |
|
|
| 176 |
$test_name = _masquerade_test_user(); |
$test_name = _masquerade_user_load(variable_get('masquerade_test_user', '')); |
| 177 |
|
|
| 178 |
$form['masquerade_test_user'] = array( |
$form['masquerade_test_user'] = array( |
| 179 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 180 |
'#title' => t('Menu <em>Quick Switch</em> user'), |
'#title' => t('Menu <em>Quick Switch</em> user'), |
| 181 |
'#autocomplete_path' => 'masquerade/autocomplete', |
'#autocomplete_path' => 'masquerade/autocomplete', |
| 182 |
'#default_value' => check_plain($test_name->name), |
'#default_value' => isset($test_name->name) ? check_plain($test_name->name) : '', |
| 183 |
'#description' => t('Enter the username of an account you wish to switch easily between via a menu item.'), |
'#description' => t('Enter the username of an account you wish to switch easily between via a menu item.'), |
| 184 |
'#maxlength' => NULL, |
'#maxlength' => NULL, |
| 185 |
); |
); |
| 187 |
$quick_switch_users = array(); |
$quick_switch_users = array(); |
| 188 |
foreach ((variable_get('masquerade_quick_switches', array())) as $uid) { |
foreach ((variable_get('masquerade_quick_switches', array())) as $uid) { |
| 189 |
$u = user_load(array('uid' => $uid)); |
$u = user_load(array('uid' => $uid)); |
| 190 |
|
if ($uid == 0) { |
| 191 |
|
$u->name = variable_get('anonymous', t('Anonymous')); |
| 192 |
|
} |
| 193 |
$quick_switch_users[] = $u->name; |
$quick_switch_users[] = $u->name; |
| 194 |
} |
} |
| 195 |
$form['masquerade_quick_switches'] = array( |
$form['masquerade_quick_switches'] = array( |
| 210 |
|
|
| 211 |
function masquerade_admin_settings_validate($form, &$form_state) { |
function masquerade_admin_settings_validate($form, &$form_state) { |
| 212 |
unset($form); |
unset($form); |
| 213 |
$test_user = user_load(array('name' => $form_state['values']['masquerade_test_user'])); |
if (!empty($form_state['values']['masquerade_test_user'])) { |
| 214 |
if (!$test_user) { |
$test_user = _masquerade_user_load($form_state['values']['masquerade_test_user']); |
| 215 |
form_set_error('masquerade_test_user', t('%user does not exist. Please enter a valid username.', array('%user' => $form_state['values']['masquerade_test_user']))); |
if (!$test_user) { |
| 216 |
|
form_set_error('masquerade_test_user', t('%user does not exist. Please enter a valid username.', array('%user' => $form_state['values']['masquerade_test_user']))); |
| 217 |
|
} |
| 218 |
} |
} |
| 219 |
|
|
| 220 |
// A comma-separated list of users. |
// A comma-separated list of users. |
| 221 |
$masquerade_switches = drupal_explode_tags($form_state['values']['masquerade_quick_switches']); |
$masquerade_switches = drupal_explode_tags($form_state['values']['masquerade_quick_switches']); |
| 222 |
foreach ($masquerade_switches as $switch_user) { |
foreach ($masquerade_switches as $switch_user) { |
| 223 |
$test_user = user_load(array('name' => $switch_user)); |
$test_user = _masquerade_user_load($switch_user); |
| 224 |
if (!$test_user) { |
if (!$test_user) { |
| 225 |
form_set_error('masquerade_quick_switches', t('%user does not exist. Please enter a valid username.', array('%user' => $switch_user))); |
form_set_error('masquerade_quick_switches', t('%user does not exist. Please enter a valid username.', array('%user' => $switch_user))); |
| 226 |
} |
} |
| 232 |
$masquerade_switches = drupal_explode_tags($form_state['values']['masquerade_quick_switches']); |
$masquerade_switches = drupal_explode_tags($form_state['values']['masquerade_quick_switches']); |
| 233 |
$masquerade_uids = array(); |
$masquerade_uids = array(); |
| 234 |
foreach ($masquerade_switches as $masquerade_name) { |
foreach ($masquerade_switches as $masquerade_name) { |
| 235 |
$u = user_load(array('name' => $masquerade_name)); |
$u = _masquerade_user_load($masquerade_name); |
| 236 |
$masquerade_uids[] = $u->uid; |
$masquerade_uids[] = $u->uid; |
| 237 |
} |
} |
| 238 |
variable_set('masquerade_quick_switches', $masquerade_uids); |
variable_set('masquerade_quick_switches', $masquerade_uids); |
| 245 |
menu_rebuild(); |
menu_rebuild(); |
| 246 |
} |
} |
| 247 |
|
|
| 248 |
function _masquerade_test_user() { |
/** |
| 249 |
$test_user->uid = 0; |
* Wrapper around user_load() to allow the loading of anonymous users. |
| 250 |
$test_user->name = ''; |
* |
| 251 |
|
* @param $username |
| 252 |
$test_user = user_load(array('name' => variable_get('masquerade_test_user', $test_user->name))); |
* The username of the user you wish to load (i.e. $user->name). To load the |
| 253 |
|
* anonymous user, pass the value of the 'anonymous' variable. |
| 254 |
return $test_user; |
* |
| 255 |
|
* @return |
| 256 |
|
* A fully-loaded $user object upon successful user load or FALSE if user |
| 257 |
|
* cannot be loaded. |
| 258 |
|
*/ |
| 259 |
|
function _masquerade_user_load($username) { |
| 260 |
|
if (!empty($username)) { |
| 261 |
|
$user = ''; |
| 262 |
|
$anon = variable_get('anonymous', t('Anonymous')); |
| 263 |
|
if ($username == $anon) { |
| 264 |
|
$user = user_load(array('name' => '')); |
| 265 |
|
$user->name = $anon; |
| 266 |
|
} |
| 267 |
|
else { |
| 268 |
|
$user = user_load(array('name' => $username)); |
| 269 |
|
} |
| 270 |
|
return $user; |
| 271 |
|
} |
| 272 |
|
return FALSE; |
| 273 |
} |
} |
| 274 |
|
|
| 275 |
/** |
/** |
| 414 |
} |
} |
| 415 |
|
|
| 416 |
foreach ($masquerade_switches as $switch_user) { |
foreach ($masquerade_switches as $switch_user) { |
| 417 |
if ($switch_user != $_SESSION['user']->uid) { |
if (!isset($_SESSION['user']->uid) || $switch_user != $_SESSION['user']->uid) { |
| 418 |
$user_name = user_load(array('uid' => $switch_user)); |
$user_name = user_load(array('uid' => $switch_user)); |
| 419 |
if ($user_name->uid) { |
if ($user_name->uid) { |
| 420 |
$quick_switch_link[] = l($user_name->name, 'masquerade/switch/'. $user_name->uid); |
$quick_switch_link[] = l($user_name->name, 'masquerade/switch/'. $user_name->uid); |
| 421 |
} |
} |
| 422 |
|
if ($switch_user == 0) { |
| 423 |
|
$user_name->name = variable_get('anonymous', t('Anonymous')); |
| 424 |
|
$quick_switch_link[] = l($user_name->name, 'masquerade/switch/'. $user_name->uid); |
| 425 |
|
} |
| 426 |
} |
} |
| 427 |
} |
} |
| 428 |
|
|
| 535 |
while ($user = db_fetch_object($result)) { |
while ($user = db_fetch_object($result)) { |
| 536 |
$matches[$prefix . $user->name] = check_plain($user->name); |
$matches[$prefix . $user->name] = check_plain($user->name); |
| 537 |
} |
} |
| 538 |
|
// This will add anonymous to the list, but not sorted. |
| 539 |
|
if (stripos(variable_get('anonymous', t('Anonymous')), $last_string) === 0) { |
| 540 |
|
$matches[$prefix . variable_get('anonymous', t('Anonymous'))] = variable_get('anonymous', t('Anonymous')); |
| 541 |
|
} |
| 542 |
if (module_exists('alt_login')) { |
if (module_exists('alt_login')) { |
| 543 |
$result = db_query_range("SELECT alt_login FROM {alt_login} u WHERE LOWER(alt_login) LIKE LOWER('%s%%')", $string, 0, 10); |
$result = db_query_range("SELECT alt_login FROM {alt_login} u WHERE LOWER(alt_login) LIKE LOWER('%s%%')", $string, 0, 10); |
| 544 |
while ($user = db_fetch_object($result)) { |
while ($user = db_fetch_object($result)) { |