Index: masquerade.info
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/masquerade/masquerade.info,v
retrieving revision 1.1.2.2
diff -u -r1.1.2.2 masquerade.info
--- masquerade.info	18 Jun 2007 23:06:50 -0000	1.1.2.2
+++ masquerade.info	10 Dec 2007 14:30:52 -0000
@@ -1,3 +1,4 @@
 ; $Id: masquerade.info,v 1.1.2.2 2007/06/18 23:06:50 dww Exp $
 name = Masquerade
 description = "This module allows permitted users to masquerade as other users."
+core = 6.x
\ No newline at end of file
Index: masquerade.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/masquerade/masquerade.install,v
retrieving revision 1.3.2.1
diff -u -r1.3.2.1 masquerade.install
--- masquerade.install	3 May 2007 11:25:43 -0000	1.3.2.1
+++ masquerade.install	10 Dec 2007 14:30:52 -0000
@@ -8,39 +8,50 @@
  */

 /**
+ * Implementation of hook_schema().
+ *
+ * @return array
+ */
+function masquerade_schema() {
+  return array(
+    'masquerade' => array(
+      'fields' => array(
+        'sid' => array(
+          'type' => 'varchar',
+          'length' => '32',
+          'not null' => true,
+          'default' => ''),
+        'uid_from' => array(
+          'type' => 'int',
+          'not null' => true,
+          'default' => 0,
+          'disp-width' => '10'),
+        'uid_as' => array(
+          'type' => 'int',
+          'not null' => true,
+          'default' => 0,
+          'disp-width' => '10')
+      ),
+      'indexes' => array(
+        'sid' => array('sid', 'uid_from'),
+        'sid_2' => array('sid', 'uid_as')
+      )
+    )
+  );
+}
+
+/**
  * Implementation of hook_install().
  */
 function masquerade_install() {
-  switch ($GLOBALS['db_type']) {
-    case 'mysql':
-    case 'mysqli':
-      db_query("create table {masquerade} (
-          sid varchar(32) NOT NULL default '',
-          uid_from int(10) NOT NULL default 0,
-          uid_as int(10) NOT NULL default 0,
-          key (sid, uid_from),
-          key (sid, uid_as)
-        ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
-      drupal_set_message(t('The required database tables for Masquerade module were created successfully.'));
-      break;
-    case 'pgsql':
-      db_query("CREATE TABLE {masquerade} (
-        sid varchar(32) NOT NULL default '',
-        uid_from numeric(10) NOT NULL default 0,
-        uid_as numeric(10) NOT NULL default 0
-      );");
-      db_query("CREATE INDEX idx_masquerade_sid_uid_from ON {masquerade} (sid, uid_from);");
-      db_query("CREATE INDEX idx_masquerade_sid_uid_as ON {masquerade} (sid, uid_as);");
-      drupal_set_message(t('The required database tables for Masquerade module were created successfully.'));
-      break;
-  }
+  drupal_install_schema('masquerade');
 }

 /**
  * Implementation of hook_uninstall().
  */
 function masquerade_uninstall() {
-  db_query('DROP TABLE {masquerade}');
+  drupal_uninstall_schema('masquerade');
   variable_del('masquerade_test_user');
   variable_del('masquerade_admin_roles');
 }
Index: masquerade.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/masquerade/masquerade.module,v
retrieving revision 1.15.2.3
diff -u -r1.15.2.3 masquerade.module
--- masquerade.module	3 May 2007 11:25:43 -0000	1.15.2.3
+++ masquerade.module	10 Dec 2007 14:30:52 -0000
@@ -13,7 +13,7 @@
 function masquerade_help($section) {
   switch ($section) {
     case 'admin/help#masquerade':
-      return t("<p>The masquerade module adds a link on a user's profile page that allows permitted users to masquerade as that user. Upon masquerading, the link to 'switch back' to the original user will appear in the menu. While masquerading, the option to masquerade as another user will not appear. All masquerading transactions are logged, and $user->masquerading will be set; this could be displayed via theme.</p><p>In the masquerade settings a list of roles are presented; any checked role is considered an 'administrator' and requires the second level 'masquerade as admin' permission to masquerade as. User #1 is automatically considered an administrator, regardless of roles.</p>");
+      return t('<p>The masquerade module adds a link on a user\'s profile page that allows permitted users to masquerade as that user. Upon masquerading, the link to "switch back" to the original user will appear in the menu. While masquerading, the option to masquerade as another user will not appear. All masquerading transactions are logged, and $user->masquerading will be set; this could be displayed via theme.</p><p>In the masquerade settings a list of roles are presented; any checked role is considered an "administrator" and requires the second level "masquerade as admin" permission to masquerade as. User #1 is automatically considered an administrator, regardless of roles.</p>');
     case 'admin/settings/masquerade':
       return t('Only the users with <strong>masquerade as admin</strong> permission, will be able to masquerade as the users who belong to the roles selected below. User #1 is automatically considered an administrator, regardless of roles.');
   }
@@ -21,6 +21,8 @@

 /**
  * Implementation of hook_perm().
+ *
+ * @return array
  */
 function masquerade_perm() {
   return array('masquerade as user', 'masquerade as admin');
@@ -46,102 +48,116 @@
 /**
  * Implementation of hook_menu().
  */
-function masquerade_menu($may_cache) {
+function masquerade_menu() {
   $items = array();

-  if ($may_cache) {
-    $default_test_user = user_load(array('name' => variable_get('masquerade_test_user', '')));
-    $items[] = array('path' => 'masquerade/switch',
-      'title' => t('Switch user'),
-      'callback' => 'masquerade_switch_user',
-      'access' => !$GLOBALS['masquerading'] && (user_access('masquerade as user') || user_access('masquerade as admin')),
-      'type' => MENU_CALLBACK,
-    );
-    if ($default_test_user->uid) {
-      $items[] = array('path' => 'masquerade/switch/'. $default_test_user->uid,
-        'title' => t('Masquerade as @testuser', array('@testuser' => $default_test_user->name)),
-        'access' => !$GLOBALS['masquerading'] && (user_access('masquerade as user') || user_access('masquerade as admin')),
-        'type' => MENU_NORMAL_ITEM,
-      );
-    }
-    $items[] = array('path' => 'masquerade/unswitch',
-      'title' => t('Switch back'),
-      'callback' => 'masquerade_switch_back',
-      'access' => $GLOBALS['masquerading'],
-      'type' => MENU_NORMAL_ITEM,
-    );
-    $items[] = array('path' => 'masquerade/autocomplete',
-    'title' => t('Masquerade autocomplete'),
-      'callback' => 'masquerade_autocomplete',
-    'access' => $GLOBALS['masquerading'] || (user_access('masquerade as user') || user_access('masquerade as admin')),
-      'type' => MENU_CALLBACK,
-    );
-    $items[] = array('path' => 'admin/settings/masquerade',
-      'title' => t('Masquerade'),
-      'description' => t('Masquerade module allows administrators to masquerade as other users.'),
-      'callback' => 'drupal_get_form',
-      'callback arguments' => 'masquerade_settings',
-      'type' => MENU_NORMAL_ITEM,
-    );
-  }
+  $items['masquerade/switch/%'] = array(
+    'title' => 'Masquerading',
+    'page callback' => 'masquerade_switch_user',
+    'page arguments' => array(2),
+    'access callback' => 'masquerade_access',
+    'access arguments' => array('switch'),
+    'type' => MENU_NORMAL_ITEM,
+  );
+  $items['masquerade/unswitch'] = array(
+    'title' => 'Switch back',
+    'page callback' => 'masquerade_switch_back',
+    'access callback' => 'masquerade_access',
+    'access arguments' => array('unswitch'),
+    'type' => MENU_NORMAL_ITEM,
+  );
+  $items['masquerade/autocomplete'] = array(
+    'title' => 'Masquerade autocomplete',
+    'page callback' => 'masquerade_autocomplete',
+    'access callback' => 'masquerade_access',
+    'access arguments' => array('autocomplete'),
+    'type' => MENU_CALLBACK,
+  );
+  $items['admin/settings/masquerade'] = array(
+    'title' => 'Masquerade',
+    'description' => 'Masquerade module allows administrators to masquerade as other users.',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('masquerade_admin_settings'),
+    'access callback' => 'user_access',
+    'access arguments' => array('administer permissions'),
+    'type' => MENU_NORMAL_ITEM,
+  );

   return $items;
 }

+function masquerade_access($type) {
+  switch($type) {
+    case 'unswitch':
+      return $GLOBALS['masquerading'];
+    case 'autocomplete':
+      return $GLOBALS['masquerading'] || (user_access('masquerade as user') || user_access('masquerade as admin'));
+      break;
+    case 'switch':
+      return empty($GLOBALS['masquerading']) && (user_access('masquerade as user') || user_access('masquerade as admin'));
+      break;
+  }
+}
+
 /**
  * Implementation of hook_settings().
  */
-function masquerade_settings() {
+function masquerade_admin_settings() {
   // create a list of roles; all selected roles are considered administrative.
   $rids = array();
   $result = db_query("SELECT r.rid, r.name FROM {role} r ORDER BY r.name");
   while ($obj = db_fetch_object($result)) {
     $rids[$obj->rid] = $obj->name;
   }
-  $test_user = user_load(array('name' => variable_get('masquerade_test_user', '')));

   $form['masquerade_admin_roles'] = array(
     '#type' => 'checkboxes',
-    '#title' => t("Roles that are considered 'administrator' for masquerading."),
+    '#title' => t('Roles that are considered "administrator" for masquerading'),
     '#options' => $rids,
     '#default_value' => variable_get('masquerade_admin_roles', array()),
   );
+
+  $test_name = _masquerade_test_user();
   $form['masquerade_test_user'] = array(
     '#type' => 'textfield',
     '#title' => t('Default test user'),
     '#autocomplete_path' => 'masquerade/autocomplete',
-    '#default_value' => check_plain($test_user->name),
+    '#default_value' => check_plain($test_name->name),
     '#description' => t('Enter the username of an account you wish to switch easily between. The name must be an exisiting user.'),
   );
-  $form['masquerade_version_information'] = array(
-    '#value' => masquerade_version(),
-  );
-  $form['#validate'] = array(
-    'masquerade_settings_validate' => array(),
-  );

   return system_settings_form($form);
 }

-function masquerade_settings_validate($form_id, $form_value) {
-  $test_user = user_load(array('name' => $form_value['masquerade_test_user']));
+function masquerade_admin_settings_validate($form, &$form_state) {
+  unset($form);
+  $test_user = user_load(array('name' => $form_state['values']['masquerade_test_user']));
   if (!$test_user) {
     form_set_error('masquerade_test_user', t('No such user exists. Please enter a valid username.'));
   }
 }

+function _masquerade_test_user() {
+  $test_user->uid = 0;
+  $test_user->name = '';
+
+  $test_user = user_load(array('name' => variable_get('masquerade_test_user', $test_user->name)));
+
+  return $test_user;
+}
+
 /**
  * Implementation of hook_user().
  */
 function masquerade_user($op, &$edit, &$edit_user, $category = NULL) {
   switch ($op) {
-
+
     case 'logout':
       if ($edit_user->masquerading) {
         global $user;
         cache_clear_all($user->uid, 'cache_menu', true);
         $real_user = user_load(array('uid' => $user->masquerading));
-        watchdog('masquerade', t("User %user no longer masquerading as %masq_as.", array('%user' => $real_user->name, '%masq_as' => $user->name)));
+        watchdog('masquerade', "User %user no longer masquerading as %masq_as.", array('%user' => $real_user->name, '%masq_as' => $user->name), WATCHDOG_INFO);
         db_query("DELETE FROM {masquerade} WHERE sid = '%s' AND uid_as = %d", session_id(), $edit_user->uid);
       }
       break;
@@ -149,17 +165,17 @@
     case 'view':
       // check if user qualifies as admin
       $roles = array_keys(array_filter(variable_get('masquerade_admin_roles', array())));
-      $perm = $edit_user->uid == 1 || array_intersect(array_keys($edit_user->roles), $roles) ?
+      $perm = $edit_user->uid == 1 || array_intersect(array_keys($edit_user->roles), $roles) ?
         'masquerade as admin' :
         'masquerade as user';

       global $user;
-      if (user_access($perm) && !$edit_user->masquerading && $user->uid != $edit_user->uid) {
-        $items[] = array(
-          'value' => l(t('Masquerade as !user', array('!user' => $edit_user->name)), 'masquerade/switch/'. $edit_user->uid, array('destination' => $_GET['q'])),
-          'class' => 'masquerade',
+      if (user_access($perm) && empty($edit_user->masquerading) && $user->uid != $edit_user->uid) {
+        $edit_user->content['Masquerade'] = array(
+          '#value' => l(t('Masquerade as !user', array('!user' => $edit_user->name)), 'masquerade/switch/'. $edit_user->uid, array('destination' => $_GET['q'])),
+          '#weight' => 10
         );
-        return array(t('Masquerade') => $items);
+//        return array(t('Masquerade') => $items);
       }
       break;
   }
@@ -174,15 +190,16 @@
       $blocks[0]['info'] =  t('Masquerade');
       return $blocks;
     case 'view':
-    if ($GLOBALS['masquerading'] || (user_access('masquerade as user') || user_access('masquerade as admin'))) {
-      switch($delta) {
-        case 0:
-          $block['subject'] = t('Masquerade');
-          $block['content'] = drupal_get_form('_masquerade_block_1', $form);
-          break;
+      if (masquerade_access('autocomplete')) {
+        switch($delta) {
+          case 0:
+            $block['subject'] = t('Masquerade');
+            $block['content'] = drupal_get_form('_masquerade_block_1');
+            break;
+        }
+        return $block;
       }
-      return $block;
-    }
+      break;
   }
 }

@@ -190,31 +207,29 @@
  * Masquerade block form.
  */
 function _masquerade_block_1($record) {
-  $attributes = $GLOBALS['masquerading'] ? array('disabled' => 'disabled') : array();
   if ($GLOBALS['masquerading']) {
     global $user;
     $quick_switch_link[] = l(t('Switch back'), 'masquerade/unswitch', array());
     $markup_value = t('You are masquerading as:<br />%masq_as', array('%masq_as' => $user->name)) . theme('item_list', $quick_switch_link);
   }
   else {
-    $default_test_user = user_load(array('name' => variable_get('masquerade_test_user', '')));
+    $default_test_user = _masquerade_test_user();
     $quick_switch_link[] = $default_test_user->uid ? l(t('Quick switch to @user', array('@user' => $default_test_user->name)), 'masquerade/switch/'. $default_test_user->uid, array()) : '';
     $markup_value = t('Enter username to masquerade.') . theme('item_list', $quick_switch_link);
+
+    $form['masquerade_user_field'] = array(
+      '#prefix' => '<div class="container-inline">',
+      '#type' => 'textfield',
+      '#size' => '20',
+      '#default_value' => $GLOBALS['masquerading'] ? 'Switch back to use' : $default_test_user->name,
+      '#autocomplete_path' => 'masquerade/autocomplete',
+    );
+    $form['submit'] = array(
+      '#type' => 'submit',
+      '#value' => t('Go'),
+      '#suffix' => '</div>',
+    );
   }
-  $form['masquerade_user_field'] = array(
-    '#prefix' => '<div class="container-inline">',
-    '#type' => 'textfield',
-    '#size' => '20',
-    '#default_value' => $GLOBALS['masquerading'] ? 'Switch back to use' : $default_test_user->name,
-    '#attributes' => $attributes,
-    '#autocomplete_path' => 'masquerade/autocomplete',
-  );
-  $form['submit'] = array(
-    '#type' => 'submit',
-    '#value' => t('Go'),
-    '#attributes' => $attributes,
-    '#suffix' => '</div>',
-  );
   $form['masquerade_desc'] = array(
     '#prefix' => '<div class="form-item"><div class="description">',
     '#type' => 'markup',
@@ -227,17 +242,18 @@
 /**
  * Masquerade block form validation. Implementation of hook_validate().
  */
-function _masquerade_block_1_validate($form_id, $form_value) {
-  if ($form_value['masquerade_user_field'] == '') {
+function _masquerade_block_1_validate($form, &$form_state) {
+  unset($form);
+  if ($form_state['values']['masquerade_user_field'] == '') {
     form_set_error('masquerade_user_field', t('You cannot masquerade as %anonymous!', array('%anonymous' => variable_get('anonymous', 'Anonymous'))));
   }
   if ($GLOBALS['masquerading']) {
     form_set_error('masquerade_user_field', t('You are already masquerading!'));
   }
   global $user;
-  $masq_user = user_load(array('name' => $form_value['masquerade_user_field']));
+  $masq_user = user_load(array('name' => $form_state['values']['masquerade_user_field']));
   if (!$masq_user) {
-    form_set_error('masquerade_user_field', t('User %masq_as does not exist. Please enter a valid username.', array('%masq_as' => $form_value['masquerade_user_field'])));
+    form_set_error('masquerade_user_field', t('User %masq_as does not exist. Please enter a valid username.', array('%masq_as' => $form_state['values']['masquerade_user_field'])));
   }
   if ($masq_user->uid == $user->uid) {
     form_set_error('masquerade_user_field', t('You cannot masquerade on yourself!'));
@@ -247,8 +263,9 @@
 /**
  * Masquerade block form submission. Implementation of hook_submit().
  */
-function _masquerade_block_1_submit($form_id, $form_values) {
-  $masq_user = user_load(array('name' => $form_values['masquerade_user_field']));
+function _masquerade_block_1_submit($form, &$form_state) {
+  unset($form);
+  $masq_user = user_load(array('name' => $form_state['values']['masquerade_user_field']));
   masquerade_switch_user($masq_user->uid);
 }

@@ -257,7 +274,7 @@
  */
 function masquerade_autocomplete($string) {
   $matches = array();
-  $result = db_query_range("SELECT name FROM {users} WHERE LOWER(name) LIKE LOWER('%s%%')", $string, 0, 10);
+  $result = db_query_range("SELECT u.name FROM {users} u WHERE LOWER(u.name) LIKE LOWER('%s%%')", $string, 0, 10);
   while ($user = db_fetch_object($result)) {
     $matches[$user->name] = check_plain($user->name);
   }
@@ -270,13 +287,19 @@
  * the selected user.
  */
 function masquerade_switch_user($uid) {
+  if (!is_numeric($uid)) {
+    drupal_set_message(t('A user id was not correctly passed to the switching function.'));
+    watchdog('masquerade', 'The user id provided to switch users was not numeric.', NULL, WATCHDOG_ERROR);
+    return drupal_goto(referer_uri());
+  }
+
   $new_user = user_load(array('uid' => $uid));
   if (!$new_user) {
     return drupal_not_found();
   }

   $roles = array_keys(array_filter(variable_get('masquerade_admin_roles', array())));
-  $perm = $uid == 1 || array_intersect(array_keys($new_user->roles), $roles) ?
+  $perm = $uid == 1 || array_intersect(array_keys($new_user->roles), $roles) ?
     'masquerade as admin' :
     'masquerade as user';
   // check to see if we need admin permission
@@ -287,15 +310,15 @@
   global $user;
   // write record

-  if ($user->uid == $uid || $user->masquerading) {
+  if ($user->uid == $uid || isset($user->masquerading)) {
     return drupal_access_denied();
   }

   db_query("INSERT INTO {masquerade} (uid_from, uid_as, sid) VALUES (%d, %d, '%s')",
-    $user->uid, $new_user->uid, session_id());
+  $user->uid, $new_user->uid, session_id());
   // switch user

-  watchdog('masquerade', t('User %user now masquerading as %masq_as.', array('%user' => $user->name, '%masq_as' => $new_user->name)));
+  watchdog('masquerade', 'User %user now masquerading as %masq_as.', array('%user' => $user->name, '%masq_as' => $new_user->name), WATCHDOG_INFO);
   drupal_set_message(t('Now masquerading as %masq_as.', array('%masq_as' => $new_user->name)));
   $user->masquerading = $new_user->uid;
   $user = $new_user;
@@ -310,16 +333,12 @@
   // switch user
   global $user;
   cache_clear_all($user->uid, 'cache_menu', true);
-  $uid = db_result(db_query("SELECT uid_from FROM {masquerade} WHERE sid = '%s' AND uid_as = %d ", session_id(), $user->uid));
+  $uid = db_result(db_query("SELECT m.uid_from FROM {masquerade} m WHERE m.sid = '%s' AND m.uid_as = %d ", session_id(), $user->uid));
   // erase record
   db_query("DELETE FROM {masquerade} WHERE sid = '%s' AND uid_as = %d ", session_id(), $user->uid);
-  $oldname = $user->name ? $user->name : variable_get('anonymous', 'Anonymous');
+  $oldname = !empty($user->name) ? $user->name : variable_get('anonymous', 'Anonymous');
   $user = user_load(array('uid' => $uid));
-  watchdog('masquerade', t('User %user no longer masquerading as %masq_as.', array('%user' => $user->name, '%masq_as' => $oldname)));
+  watchdog('masquerade', 'User %user no longer masquerading as %masq_as.', array('%user' => $user->name, '%masq_as' => $oldname), WATCHDOG_INFO);
   drupal_set_message(t('No longer masquerading as %masq_as.', array('%masq_as' => $oldname)));
   drupal_goto(referer_uri());
 }
-
-function masquerade_version(){
-  return str_replace(array('$RCSf'.'ile:', ',v', '$Re'.'vision: ', '$Da'.'te: ', '$'), '', '<p style="font-size:x-small">$RCSfile: masquerade.module,v $ version: <b>$Revision: 1.15.2.3 $</b>, $Date: 2007/05/03 11:25:43 $</p>');
-}
