web/drupal/modules/user/user.admin.inc
branchdrupal
changeset 74 0ff3ba646492
equal deleted inserted replaced
73:fcf75e232c5b 74:0ff3ba646492
       
     1 <?php
       
     2 // $Id: user.admin.inc,v 1.18.2.1 2008/08/13 23:59:14 drumm Exp $
       
     3 
       
     4 /**
       
     5  * @file
       
     6  * Admin page callback file for the user module.
       
     7  */
       
     8 
       
     9 function user_admin($callback_arg = '') {
       
    10   $op = isset($_POST['op']) ? $_POST['op'] : $callback_arg;
       
    11 
       
    12   switch ($op) {
       
    13     case t('Create new account'):
       
    14     case 'create':
       
    15       $output = drupal_get_form('user_register');
       
    16       break;
       
    17     default:
       
    18       if (!empty($_POST['accounts']) && isset($_POST['operation']) && ($_POST['operation'] == 'delete')) {
       
    19         $output = drupal_get_form('user_multiple_delete_confirm');
       
    20       }
       
    21       else {
       
    22         $output = drupal_get_form('user_filter_form');
       
    23         $output .= drupal_get_form('user_admin_account');
       
    24       }
       
    25   }
       
    26   return $output;
       
    27 }
       
    28 
       
    29 /**
       
    30  * Form builder; Return form for user administration filters.
       
    31  *
       
    32  * @ingroup forms
       
    33  * @see user_filter_form_submit()
       
    34  */
       
    35 function user_filter_form() {
       
    36   $session = &$_SESSION['user_overview_filter'];
       
    37   $session = is_array($session) ? $session : array();
       
    38   $filters = user_filters();
       
    39 
       
    40   $i = 0;
       
    41   $form['filters'] = array(
       
    42     '#type' => 'fieldset',
       
    43     '#title' => t('Show only users where'),
       
    44     '#theme' => 'user_filters',
       
    45   );
       
    46   foreach ($session as $filter) {
       
    47     list($type, $value) = $filter;
       
    48     // Merge an array of arrays into one if necessary.
       
    49     $options = $type == 'permission' ? call_user_func_array('array_merge', $filters[$type]['options']) : $filters[$type]['options'];
       
    50     $params = array('%property' => $filters[$type]['title'] , '%value' => $options[$value]);
       
    51     if ($i++ > 0) {
       
    52       $form['filters']['current'][] = array('#value' => t('<em>and</em> where <strong>%property</strong> is <strong>%value</strong>', $params));
       
    53     }
       
    54     else {
       
    55       $form['filters']['current'][] = array('#value' => t('<strong>%property</strong> is <strong>%value</strong>', $params));
       
    56     }
       
    57   }
       
    58 
       
    59   foreach ($filters as $key => $filter) {
       
    60     $names[$key] = $filter['title'];
       
    61     $form['filters']['status'][$key] = array(
       
    62       '#type' => 'select',
       
    63       '#options' => $filter['options'],
       
    64     );
       
    65   }
       
    66 
       
    67   $form['filters']['filter'] = array(
       
    68     '#type' => 'radios',
       
    69     '#options' => $names,
       
    70   );
       
    71   $form['filters']['buttons']['submit'] = array(
       
    72     '#type' => 'submit',
       
    73     '#value' => (count($session) ? t('Refine') : t('Filter')),
       
    74   );
       
    75   if (count($session)) {
       
    76     $form['filters']['buttons']['undo'] = array(
       
    77       '#type' => 'submit',
       
    78       '#value' => t('Undo'),
       
    79     );
       
    80     $form['filters']['buttons']['reset'] = array(
       
    81       '#type' => 'submit',
       
    82       '#value' => t('Reset'),
       
    83     );
       
    84   }
       
    85 
       
    86   drupal_add_js('misc/form.js', 'core');
       
    87 
       
    88   return $form;
       
    89 }
       
    90 
       
    91 /**
       
    92  * Process result from user administration filter form.
       
    93  */
       
    94 function user_filter_form_submit($form, &$form_state) {
       
    95   $op = $form_state['values']['op'];
       
    96   $filters = user_filters();
       
    97   switch ($op) {
       
    98     case t('Filter'): case t('Refine'):
       
    99       if (isset($form_state['values']['filter'])) {
       
   100         $filter = $form_state['values']['filter'];
       
   101         // Merge an array of arrays into one if necessary.
       
   102         $options = $filter == 'permission' ? call_user_func_array('array_merge', $filters[$filter]['options']) : $filters[$filter]['options'];
       
   103         if (isset($options[$form_state['values'][$filter]])) {
       
   104           $_SESSION['user_overview_filter'][] = array($filter, $form_state['values'][$filter]);
       
   105         }
       
   106       }
       
   107       break;
       
   108     case t('Undo'):
       
   109       array_pop($_SESSION['user_overview_filter']);
       
   110       break;
       
   111     case t('Reset'):
       
   112       $_SESSION['user_overview_filter'] = array();
       
   113       break;
       
   114     case t('Update'):
       
   115       return;
       
   116   }
       
   117 
       
   118   $form_state['redirect'] = 'admin/user/user';
       
   119   return;
       
   120 }
       
   121 
       
   122 /**
       
   123  * Form builder; User administration page.
       
   124  *
       
   125  * @ingroup forms
       
   126  * @see user_admin_account_validate()
       
   127  * @see user_admin_account_submit()
       
   128  */
       
   129 function user_admin_account() {
       
   130   $filter = user_build_filter_query();
       
   131 
       
   132   $header = array(
       
   133     array(),
       
   134     array('data' => t('Username'), 'field' => 'u.name'),
       
   135     array('data' => t('Status'), 'field' => 'u.status'),
       
   136     t('Roles'),
       
   137     array('data' => t('Member for'), 'field' => 'u.created', 'sort' => 'desc'),
       
   138     array('data' => t('Last access'), 'field' => 'u.access'),
       
   139     t('Operations')
       
   140   );
       
   141 
       
   142   $sql = 'SELECT DISTINCT u.uid, u.name, u.status, u.created, u.access FROM {users} u LEFT JOIN {users_roles} ur ON u.uid = ur.uid '. $filter['join'] .' WHERE u.uid != 0 '. $filter['where'];
       
   143   $sql .= tablesort_sql($header);
       
   144   $query_count = 'SELECT COUNT(DISTINCT u.uid) FROM {users} u LEFT JOIN {users_roles} ur ON u.uid = ur.uid '. $filter['join'] .' WHERE u.uid != 0 '. $filter['where'];
       
   145   $result = pager_query($sql, 50, 0, $query_count, $filter['args']);
       
   146 
       
   147   $form['options'] = array(
       
   148     '#type' => 'fieldset',
       
   149     '#title' => t('Update options'),
       
   150     '#prefix' => '<div class="container-inline">',
       
   151     '#suffix' => '</div>',
       
   152   );
       
   153   $options = array();
       
   154   foreach (module_invoke_all('user_operations') as $operation => $array) {
       
   155     $options[$operation] = $array['label'];
       
   156   }
       
   157   $form['options']['operation'] = array(
       
   158     '#type' => 'select',
       
   159     '#options' => $options,
       
   160     '#default_value' => 'unblock',
       
   161   );
       
   162   $form['options']['submit'] = array(
       
   163     '#type' => 'submit',
       
   164     '#value' => t('Update'),
       
   165   );
       
   166 
       
   167   $destination = drupal_get_destination();
       
   168 
       
   169   $status = array(t('blocked'), t('active'));
       
   170   $roles = user_roles(TRUE);
       
   171   $accounts = array();
       
   172   while ($account = db_fetch_object($result)) {
       
   173     $accounts[$account->uid] = '';
       
   174     $form['name'][$account->uid] = array('#value' => theme('username', $account));
       
   175     $form['status'][$account->uid] =  array('#value' => $status[$account->status]);
       
   176     $users_roles = array();
       
   177     $roles_result = db_query('SELECT rid FROM {users_roles} WHERE uid = %d', $account->uid);
       
   178     while ($user_role = db_fetch_object($roles_result)) {
       
   179       $users_roles[] = $roles[$user_role->rid];
       
   180     }
       
   181     asort($users_roles);
       
   182     $form['roles'][$account->uid][0] = array('#value' => theme('item_list', $users_roles));
       
   183     $form['member_for'][$account->uid] = array('#value' => format_interval(time() - $account->created));
       
   184     $form['last_access'][$account->uid] =  array('#value' => $account->access ? t('@time ago', array('@time' => format_interval(time() - $account->access))) : t('never'));
       
   185     $form['operations'][$account->uid] = array('#value' => l(t('edit'), "user/$account->uid/edit", array('query' => $destination)));
       
   186   }
       
   187   $form['accounts'] = array(
       
   188     '#type' => 'checkboxes',
       
   189     '#options' => $accounts
       
   190   );
       
   191   $form['pager'] = array('#value' => theme('pager', NULL, 50, 0));
       
   192 
       
   193   return $form;
       
   194 }
       
   195 
       
   196 /**
       
   197  * Submit the user administration update form.
       
   198  */
       
   199 function user_admin_account_submit($form, &$form_state) {
       
   200   $operations = module_invoke_all('user_operations', $form_state);
       
   201   $operation = $operations[$form_state['values']['operation']];
       
   202   // Filter out unchecked accounts.
       
   203   $accounts = array_filter($form_state['values']['accounts']);
       
   204   if ($function = $operation['callback']) {
       
   205     // Add in callback arguments if present.
       
   206     if (isset($operation['callback arguments'])) {
       
   207       $args = array_merge(array($accounts), $operation['callback arguments']);
       
   208     }
       
   209     else {
       
   210       $args = array($accounts);
       
   211     }
       
   212     call_user_func_array($function, $args);
       
   213 
       
   214     drupal_set_message(t('The update has been performed.'));
       
   215   }
       
   216 }
       
   217 
       
   218 function user_admin_account_validate($form, &$form_state) {
       
   219   $form_state['values']['accounts'] = array_filter($form_state['values']['accounts']);
       
   220   if (count($form_state['values']['accounts']) == 0) {
       
   221     form_set_error('', t('No users selected.'));
       
   222   }
       
   223 }
       
   224 
       
   225 /**
       
   226  * Form builder; Configure user settings for this site.
       
   227  *
       
   228  * @ingroup forms
       
   229  * @see system_settings_form()
       
   230  */
       
   231 function user_admin_settings() {
       
   232   // User registration settings.
       
   233   $form['registration'] = array('#type' => 'fieldset', '#title' => t('User registration settings'));
       
   234   $form['registration']['user_register'] = array('#type' => 'radios', '#title' => t('Public registrations'), '#default_value' => variable_get('user_register', 1), '#options' => array(t('Only site administrators can create new user accounts.'), t('Visitors can create accounts and no administrator approval is required.'), t('Visitors can create accounts but administrator approval is required.')));
       
   235   $form['registration']['user_email_verification'] = array('#type' => 'checkbox', '#title' => t('Require e-mail verification when a visitor creates an account'), '#default_value' => variable_get('user_email_verification', TRUE), '#description' => t('If this box is checked, new users will be required to validate their e-mail address prior to logging into the site, and will be assigned a system-generated password. With it unchecked, users will be logged in immediately upon registering, and may select their own passwords during registration.'));
       
   236   $form['registration']['user_registration_help'] = array('#type' => 'textarea', '#title' => t('User registration guidelines'), '#default_value' => variable_get('user_registration_help', ''), '#description' => t('This text is displayed at the top of the user registration form and is useful for helping or instructing your users.'));
       
   237 
       
   238   // User e-mail settings.
       
   239   $form['email'] = array(
       
   240     '#type' => 'fieldset',
       
   241     '#title' => t('User e-mail settings'),
       
   242     '#description' => t('Drupal sends emails whenever new users register on your site, and optionally, may also notify users after other account actions. Using a simple set of content templates, notification e-mails can be customized to fit the specific needs of your site.'),
       
   243   );
       
   244   // These email tokens are shared for all settings, so just define
       
   245   // the list once to help ensure they stay in sync.
       
   246   $email_token_help = t('Available variables are:') .' !username, !site, !password, !uri, !uri_brief, !mailto, !date, !login_uri, !edit_uri, !login_url.';
       
   247 
       
   248   $form['email']['admin_created'] = array(
       
   249     '#type' => 'fieldset',
       
   250     '#title' => t('Welcome, new user created by administrator'),
       
   251     '#collapsible' => TRUE,
       
   252     '#collapsed' => (variable_get('user_register', 1) != 0),
       
   253     '#description' => t('Customize welcome e-mail messages sent to new member accounts created by an administrator.') .' '. $email_token_help,
       
   254   );
       
   255   $form['email']['admin_created']['user_mail_register_admin_created_subject'] = array(
       
   256     '#type' => 'textfield',
       
   257     '#title' => t('Subject'),
       
   258     '#default_value' => _user_mail_text('register_admin_created_subject'),
       
   259     '#maxlength' => 180,
       
   260   );
       
   261   $form['email']['admin_created']['user_mail_register_admin_created_body'] = array(
       
   262     '#type' => 'textarea',
       
   263     '#title' => t('Body'),
       
   264     '#default_value' => _user_mail_text('register_admin_created_body'),
       
   265     '#rows' => 15,
       
   266   );
       
   267 
       
   268   $form['email']['no_approval_required'] = array(
       
   269     '#type' => 'fieldset',
       
   270     '#title' => t('Welcome, no approval required'),
       
   271     '#collapsible' => TRUE,
       
   272     '#collapsed' => (variable_get('user_register', 1) != 1),
       
   273     '#description' => t('Customize welcome e-mail messages sent to new members upon registering, when no administrator approval is required.') .' '. $email_token_help
       
   274   );
       
   275   $form['email']['no_approval_required']['user_mail_register_no_approval_required_subject'] = array(
       
   276     '#type' => 'textfield',
       
   277     '#title' => t('Subject'),
       
   278     '#default_value' => _user_mail_text('register_no_approval_required_subject'),
       
   279     '#maxlength' => 180,
       
   280   );
       
   281   $form['email']['no_approval_required']['user_mail_register_no_approval_required_body'] = array(
       
   282     '#type' => 'textarea',
       
   283     '#title' => t('Body'),
       
   284     '#default_value' => _user_mail_text('register_no_approval_required_body'),
       
   285     '#rows' => 15,
       
   286   );
       
   287 
       
   288   $form['email']['pending_approval'] = array(
       
   289     '#type' => 'fieldset',
       
   290     '#title' => t('Welcome, awaiting administrator approval'),
       
   291     '#collapsible' => TRUE,
       
   292     '#collapsed' => (variable_get('user_register', 1) != 2),
       
   293     '#description' => t('Customize welcome e-mail messages sent to new members upon registering, when administrative approval is required.') .' '. $email_token_help,
       
   294   );
       
   295   $form['email']['pending_approval']['user_mail_register_pending_approval_subject'] = array(
       
   296     '#type' => 'textfield',
       
   297     '#title' => t('Subject'),
       
   298     '#default_value' => _user_mail_text('register_pending_approval_subject'),
       
   299     '#maxlength' => 180,
       
   300   );
       
   301   $form['email']['pending_approval']['user_mail_register_pending_approval_body'] = array(
       
   302     '#type' => 'textarea',
       
   303     '#title' => t('Body'),
       
   304     '#default_value' => _user_mail_text('register_pending_approval_body'),
       
   305     '#rows' => 8,
       
   306   );
       
   307 
       
   308   $form['email']['password_reset'] = array(
       
   309     '#type' => 'fieldset',
       
   310     '#title' => t('Password recovery email'),
       
   311     '#collapsible' => TRUE,
       
   312     '#collapsed' => TRUE,
       
   313     '#description' => t('Customize e-mail messages sent to users who request a new password.') .' '. $email_token_help,
       
   314   );
       
   315   $form['email']['password_reset']['user_mail_password_reset_subject'] = array(
       
   316     '#type' => 'textfield',
       
   317     '#title' => t('Subject'),
       
   318     '#default_value' => _user_mail_text('password_reset_subject'),
       
   319     '#maxlength' => 180,
       
   320   );
       
   321   $form['email']['password_reset']['user_mail_password_reset_body'] = array(
       
   322     '#type' => 'textarea',
       
   323     '#title' => t('Body'),
       
   324     '#default_value' => _user_mail_text('password_reset_body'),
       
   325     '#rows' => 12,
       
   326   );
       
   327 
       
   328   $form['email']['activated'] = array(
       
   329     '#type' => 'fieldset',
       
   330     '#title' => t('Account activation email'),
       
   331     '#collapsible' => TRUE,
       
   332     '#collapsed' => TRUE,
       
   333     '#description' => t('Enable and customize e-mail messages sent to users upon account activation (when an administrator activates an account of a user who has already registered, on a site where administrative approval is required).') .' '. $email_token_help,
       
   334   );
       
   335   $form['email']['activated']['user_mail_status_activated_notify'] = array(
       
   336     '#type' => 'checkbox',
       
   337     '#title' => t('Notify user when account is activated.'),
       
   338     '#default_value' => variable_get('user_mail_status_activated_notify', TRUE),
       
   339   );
       
   340   $form['email']['activated']['user_mail_status_activated_subject'] = array(
       
   341     '#type' => 'textfield',
       
   342     '#title' => t('Subject'),
       
   343     '#default_value' => _user_mail_text('status_activated_subject'),
       
   344     '#maxlength' => 180,
       
   345   );
       
   346   $form['email']['activated']['user_mail_status_activated_body'] = array(
       
   347     '#type' => 'textarea',
       
   348     '#title' => t('Body'),
       
   349     '#default_value' => _user_mail_text('status_activated_body'),
       
   350     '#rows' => 15,
       
   351   );
       
   352 
       
   353   $form['email']['blocked'] = array(
       
   354     '#type' => 'fieldset',
       
   355     '#title' => t('Account blocked email'),
       
   356     '#collapsible' => TRUE,
       
   357     '#collapsed' => TRUE,
       
   358     '#description' => t('Enable and customize e-mail messages sent to users when their accounts are blocked.') .' '. $email_token_help,
       
   359   );
       
   360   $form['email']['blocked']['user_mail_status_blocked_notify'] = array(
       
   361     '#type' => 'checkbox',
       
   362     '#title' => t('Notify user when account is blocked.'),
       
   363     '#default_value' => variable_get('user_mail_status_blocked_notify', FALSE),
       
   364   );
       
   365   $form['email']['blocked']['user_mail_status_blocked_subject'] = array(
       
   366     '#type' => 'textfield',
       
   367     '#title' => t('Subject'),
       
   368     '#default_value' => _user_mail_text('status_blocked_subject'),
       
   369     '#maxlength' => 180,
       
   370   );
       
   371   $form['email']['blocked']['user_mail_status_blocked_body'] = array(
       
   372     '#type' => 'textarea',
       
   373     '#title' => t('Body'),
       
   374     '#default_value' => _user_mail_text('status_blocked_body'),
       
   375     '#rows' => 3,
       
   376   );
       
   377 
       
   378   $form['email']['deleted'] = array(
       
   379     '#type' => 'fieldset',
       
   380     '#title' => t('Account deleted email'),
       
   381     '#collapsible' => TRUE,
       
   382     '#collapsed' => TRUE,
       
   383     '#description' => t('Enable and customize e-mail messages sent to users when their accounts are deleted.') .' '. $email_token_help,
       
   384   );
       
   385   $form['email']['deleted']['user_mail_status_deleted_notify'] = array(
       
   386     '#type' => 'checkbox',
       
   387     '#title' => t('Notify user when account is deleted.'),
       
   388     '#default_value' => variable_get('user_mail_status_deleted_notify', FALSE),
       
   389   );
       
   390   $form['email']['deleted']['user_mail_status_deleted_subject'] = array(
       
   391     '#type' => 'textfield',
       
   392     '#title' => t('Subject'),
       
   393     '#default_value' => _user_mail_text('status_deleted_subject'),
       
   394     '#maxlength' => 180,
       
   395   );
       
   396   $form['email']['deleted']['user_mail_status_deleted_body'] = array(
       
   397     '#type' => 'textarea',
       
   398     '#title' => t('Body'),
       
   399     '#default_value' => _user_mail_text('status_deleted_body'),
       
   400     '#rows' => 3,
       
   401   );
       
   402 
       
   403   // User signatures.
       
   404   $form['signatures'] = array(
       
   405     '#type' => 'fieldset',
       
   406     '#title' => t('Signatures'),
       
   407   );
       
   408   $form['signatures']['user_signatures'] = array(
       
   409     '#type' => 'radios',
       
   410     '#title' => t('Signature support'),
       
   411     '#default_value' => variable_get('user_signatures', 0),
       
   412     '#options' => array(t('Disabled'), t('Enabled')),
       
   413   );
       
   414 
       
   415   // If picture support is enabled, check whether the picture directory exists:
       
   416   if (variable_get('user_pictures', 0)) {
       
   417     $picture_path = file_create_path(variable_get('user_picture_path', 'pictures'));
       
   418     file_check_directory($picture_path, 1, 'user_picture_path');
       
   419   }
       
   420 
       
   421   $form['pictures'] = array(
       
   422     '#type' => 'fieldset',
       
   423     '#title' => t('Pictures'),
       
   424   );
       
   425   $picture_support = variable_get('user_pictures', 0);
       
   426   $form['pictures']['user_pictures'] = array(
       
   427     '#type' => 'radios',
       
   428     '#title' => t('Picture support'),
       
   429     '#default_value' => $picture_support,
       
   430     '#options' => array(t('Disabled'), t('Enabled')),
       
   431     '#prefix' => '<div class="user-admin-picture-radios">',
       
   432     '#suffix' => '</div>',
       
   433   );
       
   434   drupal_add_js(drupal_get_path('module', 'user') .'/user.js');
       
   435   // If JS is enabled, and the radio is defaulting to off, hide all
       
   436   // the settings on page load via .css using the js-hide class so
       
   437   // that there's no flicker.
       
   438   $css_class = 'user-admin-picture-settings';
       
   439   if (!$picture_support) {
       
   440     $css_class .= ' js-hide';
       
   441   }
       
   442   $form['pictures']['settings'] = array(
       
   443     '#prefix' => '<div class="'. $css_class .'">',
       
   444     '#suffix' => '</div>',
       
   445   );
       
   446   $form['pictures']['settings']['user_picture_path'] = array(
       
   447     '#type' => 'textfield',
       
   448     '#title' => t('Picture image path'),
       
   449     '#default_value' => variable_get('user_picture_path', 'pictures'),
       
   450     '#size' => 30,
       
   451     '#maxlength' => 255,
       
   452     '#description' => t('Subdirectory in the directory %dir where pictures will be stored.', array('%dir' => file_directory_path() .'/')),
       
   453   );
       
   454   $form['pictures']['settings']['user_picture_default'] = array(
       
   455     '#type' => 'textfield',
       
   456     '#title' => t('Default picture'),
       
   457     '#default_value' => variable_get('user_picture_default', ''),
       
   458     '#size' => 30,
       
   459     '#maxlength' => 255,
       
   460     '#description' => t('URL of picture to display for users with no custom picture selected. Leave blank for none.'),
       
   461   );
       
   462   $form['pictures']['settings']['user_picture_dimensions'] = array(
       
   463     '#type' => 'textfield',
       
   464     '#title' => t('Picture maximum dimensions'),
       
   465     '#default_value' => variable_get('user_picture_dimensions', '85x85'),
       
   466     '#size' => 15,
       
   467     '#maxlength' => 10,
       
   468     '#description' => t('Maximum dimensions for pictures, in pixels.'),
       
   469   );
       
   470   $form['pictures']['settings']['user_picture_file_size'] = array(
       
   471     '#type' => 'textfield',
       
   472     '#title' => t('Picture maximum file size'),
       
   473     '#default_value' => variable_get('user_picture_file_size', '30'),
       
   474     '#size' => 15,
       
   475     '#maxlength' => 10,
       
   476     '#description' => t('Maximum file size for pictures, in kB.'),
       
   477   );
       
   478   $form['pictures']['settings']['user_picture_guidelines'] = array(
       
   479     '#type' => 'textarea',
       
   480     '#title' => t('Picture guidelines'),
       
   481     '#default_value' => variable_get('user_picture_guidelines', ''),
       
   482     '#description' => t("This text is displayed at the picture upload form in addition to the default guidelines. It's useful for helping or instructing your users."),
       
   483   );
       
   484 
       
   485   return system_settings_form($form);
       
   486 }
       
   487 
       
   488 /**
       
   489  * Menu callback: administer permissions.
       
   490  *
       
   491  * @ingroup forms
       
   492  * @see user_admin_perm_submit()
       
   493  * @see theme_user_admin_perm()
       
   494  */
       
   495 function user_admin_perm($form_state, $rid = NULL) {
       
   496   if (is_numeric($rid)) {
       
   497     $result = db_query('SELECT r.rid, p.perm FROM {role} r LEFT JOIN {permission} p ON r.rid = p.rid WHERE r.rid = %d', $rid);
       
   498   }
       
   499   else {
       
   500     $result = db_query('SELECT r.rid, p.perm FROM {role} r LEFT JOIN {permission} p ON r.rid = p.rid ORDER BY name');
       
   501   }
       
   502 
       
   503   // Compile role array:
       
   504   // Add a comma at the end so when searching for a permission, we can
       
   505   // always search for "$perm," to make sure we do not confuse
       
   506   // permissions that are substrings of each other.
       
   507   while ($role = db_fetch_object($result)) {
       
   508     $role_permissions[$role->rid] = $role->perm .',';
       
   509   }
       
   510 
       
   511   // Retrieve role names for columns.
       
   512   $role_names = user_roles();
       
   513   if (is_numeric($rid)) {
       
   514     $role_names = array($rid => $role_names[$rid]);
       
   515   }
       
   516 
       
   517   // Render role/permission overview:
       
   518   $options = array();
       
   519   foreach (module_list(FALSE, FALSE, TRUE) as $module) {
       
   520     if ($permissions = module_invoke($module, 'perm')) {
       
   521       $form['permission'][] = array(
       
   522         '#value' => $module,
       
   523       );
       
   524       asort($permissions);
       
   525       foreach ($permissions as $perm) {
       
   526         $options[$perm] = '';
       
   527         $form['permission'][$perm] = array('#value' => t($perm));
       
   528         foreach ($role_names as $rid => $name) {
       
   529           // Builds arrays for checked boxes for each role
       
   530           if (strpos($role_permissions[$rid], $perm .',') !== FALSE) {
       
   531             $status[$rid][] = $perm;
       
   532           }
       
   533         }
       
   534       }
       
   535     }
       
   536   }
       
   537 
       
   538   // Have to build checkboxes here after checkbox arrays are built
       
   539   foreach ($role_names as $rid => $name) {
       
   540     $form['checkboxes'][$rid] = array('#type' => 'checkboxes', '#options' => $options, '#default_value' => isset($status[$rid]) ? $status[$rid] : array());
       
   541     $form['role_names'][$rid] = array('#value' => $name, '#tree' => TRUE);
       
   542   }
       
   543   $form['submit'] = array('#type' => 'submit', '#value' => t('Save permissions'));
       
   544 
       
   545   return $form;
       
   546 }
       
   547 
       
   548 function user_admin_perm_submit($form, &$form_state) {
       
   549   // Save permissions:
       
   550   $result = db_query('SELECT * FROM {role}');
       
   551   while ($role = db_fetch_object($result)) {
       
   552     if (isset($form_state['values'][$role->rid])) {
       
   553       // Delete, so if we clear every checkbox we reset that role;
       
   554       // otherwise permissions are active and denied everywhere.
       
   555       db_query('DELETE FROM {permission} WHERE rid = %d', $role->rid);
       
   556       $form_state['values'][$role->rid] = array_filter($form_state['values'][$role->rid]);
       
   557       if (count($form_state['values'][$role->rid])) {
       
   558         db_query("INSERT INTO {permission} (rid, perm) VALUES (%d, '%s')", $role->rid, implode(', ', array_keys($form_state['values'][$role->rid])));
       
   559       }
       
   560     }
       
   561   }
       
   562 
       
   563   drupal_set_message(t('The changes have been saved.'));
       
   564 
       
   565   // Clear the cached pages
       
   566   cache_clear_all();
       
   567 }
       
   568 
       
   569 /**
       
   570  * Theme the administer permissions page.
       
   571  *
       
   572  * @ingroup themeable
       
   573  */
       
   574 function theme_user_admin_perm($form) {
       
   575   $roles = user_roles();
       
   576   foreach (element_children($form['permission']) as $key) {
       
   577     // Don't take form control structures
       
   578     if (is_array($form['permission'][$key])) {
       
   579       $row = array();
       
   580       // Module name
       
   581       if (is_numeric($key)) {
       
   582         $row[] = array('data' => t('@module module', array('@module' => drupal_render($form['permission'][$key]))), 'class' => 'module', 'id' => 'module-'. $form['permission'][$key]['#value'], 'colspan' => count($form['role_names']) + 1);
       
   583       }
       
   584       else {
       
   585         $row[] = array('data' => drupal_render($form['permission'][$key]), 'class' => 'permission');
       
   586         foreach (element_children($form['checkboxes']) as $rid) {
       
   587           if (is_array($form['checkboxes'][$rid])) {
       
   588             $row[] = array('data' => drupal_render($form['checkboxes'][$rid][$key]), 'class' => 'checkbox', 'title' => $roles[$rid] .' : '. t($key));
       
   589           }
       
   590         }
       
   591       }
       
   592       $rows[] = $row;
       
   593     }
       
   594   }
       
   595   $header[] = (t('Permission'));
       
   596   foreach (element_children($form['role_names']) as $rid) {
       
   597     if (is_array($form['role_names'][$rid])) {
       
   598       $header[] = array('data' => drupal_render($form['role_names'][$rid]), 'class' => 'checkbox');
       
   599     }
       
   600   }
       
   601   $output = theme('table', $header, $rows, array('id' => 'permissions'));
       
   602   $output .= drupal_render($form);
       
   603   return $output;
       
   604 }
       
   605 
       
   606 /**
       
   607  * Menu callback: administer roles.
       
   608  *
       
   609  * @ingroup forms
       
   610  * @see user_admin_role_validate()
       
   611  * @see user_admin_role_submit()
       
   612  * @see theme_user_admin_new_role()
       
   613  */
       
   614 function user_admin_role() {
       
   615   $rid = arg(4);
       
   616   if ($rid) {
       
   617     if ($rid == DRUPAL_ANONYMOUS_RID || $rid == DRUPAL_AUTHENTICATED_RID) {
       
   618       drupal_goto('admin/user/roles');
       
   619     }
       
   620     // Display the edit role form.
       
   621     $role = db_fetch_object(db_query('SELECT * FROM {role} WHERE rid = %d', $rid));
       
   622     $form['name'] = array(
       
   623       '#type' => 'textfield',
       
   624       '#title' => t('Role name'),
       
   625       '#default_value' => $role->name,
       
   626       '#size' => 30,
       
   627       '#required' => TRUE,
       
   628       '#maxlength' => 64,
       
   629       '#description' => t('The name for this role. Example: "moderator", "editorial board", "site architect".'),
       
   630     );
       
   631     $form['rid'] = array(
       
   632       '#type' => 'value',
       
   633       '#value' => $rid,
       
   634     );
       
   635     $form['submit'] = array(
       
   636       '#type' => 'submit',
       
   637       '#value' => t('Save role'),
       
   638     );
       
   639     $form['delete'] = array(
       
   640       '#type' => 'submit',
       
   641       '#value' => t('Delete role'),
       
   642     );
       
   643   }
       
   644   else {
       
   645     $form['name'] = array(
       
   646       '#type' => 'textfield',
       
   647       '#size' => 32,
       
   648       '#maxlength' => 64,
       
   649     );
       
   650     $form['submit'] = array(
       
   651       '#type' => 'submit',
       
   652       '#value' => t('Add role'),
       
   653     );
       
   654     $form['#submit'][] = 'user_admin_role_submit';
       
   655     $form['#validate'][] = 'user_admin_role_validate';
       
   656   }
       
   657   return $form;
       
   658 }
       
   659 
       
   660 function user_admin_role_validate($form, &$form_state) {
       
   661   if ($form_state['values']['name']) {
       
   662     if ($form_state['values']['op'] == t('Save role')) {
       
   663       if (db_result(db_query("SELECT COUNT(*) FROM {role} WHERE name = '%s' AND rid != %d", $form_state['values']['name'], $form_state['values']['rid']))) {
       
   664         form_set_error('name', t('The role name %name already exists. Please choose another role name.', array('%name' => $form_state['values']['name'])));
       
   665       }
       
   666     }
       
   667     else if ($form_state['values']['op'] == t('Add role')) {
       
   668       if (db_result(db_query("SELECT COUNT(*) FROM {role} WHERE name = '%s'", $form_state['values']['name']))) {
       
   669         form_set_error('name', t('The role name %name already exists. Please choose another role name.', array('%name' => $form_state['values']['name'])));
       
   670       }
       
   671     }
       
   672   }
       
   673   else {
       
   674     form_set_error('name', t('You must specify a valid role name.'));
       
   675   }
       
   676 }
       
   677 
       
   678 function user_admin_role_submit($form, &$form_state) {
       
   679   if ($form_state['values']['op'] == t('Save role')) {
       
   680     db_query("UPDATE {role} SET name = '%s' WHERE rid = %d", $form_state['values']['name'], $form_state['values']['rid']);
       
   681     drupal_set_message(t('The role has been renamed.'));
       
   682   }
       
   683   else if ($form_state['values']['op'] == t('Delete role')) {
       
   684     db_query('DELETE FROM {role} WHERE rid = %d', $form_state['values']['rid']);
       
   685     db_query('DELETE FROM {permission} WHERE rid = %d', $form_state['values']['rid']);
       
   686     // Update the users who have this role set:
       
   687     db_query('DELETE FROM {users_roles} WHERE rid = %d', $form_state['values']['rid']);
       
   688 
       
   689     drupal_set_message(t('The role has been deleted.'));
       
   690   }
       
   691   else if ($form_state['values']['op'] == t('Add role')) {
       
   692     db_query("INSERT INTO {role} (name) VALUES ('%s')", $form_state['values']['name']);
       
   693     drupal_set_message(t('The role has been added.'));
       
   694   }
       
   695   $form_state['redirect'] = 'admin/user/roles';
       
   696   return;
       
   697 }
       
   698 
       
   699 /**
       
   700  * Menu callback: list all access rules
       
   701  */
       
   702 function user_admin_access_check() {
       
   703   $output = drupal_get_form('user_admin_check_user');
       
   704   $output .= drupal_get_form('user_admin_check_mail');
       
   705   $output .= drupal_get_form('user_admin_check_host');
       
   706   return $output;
       
   707 }
       
   708 
       
   709 /**
       
   710  * Menu callback: add an access rule.
       
   711  */
       
   712 function user_admin_access_add($mask = NULL, $type = NULL) {
       
   713   $edit = array();
       
   714   $edit['aid'] = 0;
       
   715   $edit['mask'] = $mask;
       
   716   $edit['type'] = $type;
       
   717   return drupal_get_form('user_admin_access_add_form', $edit, t('Add rule'));
       
   718 }
       
   719 
       
   720 /**
       
   721  * Menu callback: edit an access rule.
       
   722  */
       
   723 function user_admin_access_edit($aid = 0) {
       
   724   $edit = db_fetch_array(db_query('SELECT aid, type, status, mask FROM {access} WHERE aid = %d', $aid));
       
   725   return drupal_get_form('user_admin_access_edit_form', $edit, t('Save rule'));
       
   726 }
       
   727 
       
   728 /**
       
   729  * Form builder; Configure access rules.
       
   730  *
       
   731  * @ingroup forms
       
   732  */
       
   733 function user_admin_access_form(&$form_state, $edit, $submit) {
       
   734   $form = array();
       
   735   $form['aid'] = array(
       
   736     '#type' => 'value',
       
   737     '#value' => $edit['aid'],
       
   738   );
       
   739   $form['status'] = array(
       
   740     '#type' => 'radios',
       
   741     '#title' => t('Access type'),
       
   742     '#default_value' => isset($edit['status']) ? $edit['status'] : 0,
       
   743     '#options' => array('1' => t('Allow'), '0' => t('Deny')),
       
   744   );
       
   745   $type_options = array('user' => t('Username'), 'mail' => t('E-mail'), 'host' => t('Host'));
       
   746   $form['type'] = array(
       
   747     '#type' => 'radios',
       
   748     '#title' => t('Rule type'),
       
   749     '#default_value' => (isset($type_options[$edit['type']]) ? $edit['type'] : 'user'),
       
   750     '#options' => $type_options,
       
   751   );
       
   752   $form['mask'] = array(
       
   753     '#type' => 'textfield',
       
   754     '#title' => t('Mask'),
       
   755     '#size' => 30,
       
   756     '#maxlength' => 64,
       
   757     '#default_value' => $edit['mask'],
       
   758     '#description' => '%: '. t('Matches any number of characters, even zero characters') .'.<br />_: '. t('Matches exactly one character.'),
       
   759     '#required' => TRUE,
       
   760   );
       
   761   $form['submit'] = array('#type' => 'submit', '#value' => $submit);
       
   762   $form['#submit'] = array('user_admin_access_form_submit');
       
   763 
       
   764   return $form;
       
   765 }
       
   766 
       
   767 /**
       
   768  * Submit callback for user_admin_access_form().
       
   769  */
       
   770 function user_admin_access_form_submit($form, &$form_state) {
       
   771   $edit = $form_state['values'];
       
   772   if ($edit['aid']) {
       
   773     db_query("UPDATE {access} SET mask = '%s', type = '%s', status = '%s' WHERE aid = %d", $edit['mask'], $edit['type'], $edit['status'], $edit['aid']);
       
   774     drupal_set_message(t('The access rule has been saved.'));
       
   775   }
       
   776   else {
       
   777     db_query("INSERT INTO {access} (mask, type, status) VALUES ('%s', '%s', %d)", $edit['mask'], $edit['type'], $edit['status']);
       
   778     drupal_set_message(t('The access rule has been added.'));
       
   779   }
       
   780   $form_state['redirect'] = 'admin/user/rules';
       
   781 }
       
   782 
       
   783 function user_admin_access_check_validate($form, &$form_state) {
       
   784   if (empty($form_state['values']['test'])) {
       
   785     form_set_error($form_state['values']['type'], t('No value entered. Please enter a test string and try again.'));
       
   786   }
       
   787 }
       
   788 
       
   789 function user_admin_check_user() {
       
   790   $form['user'] = array('#type' => 'fieldset', '#title' => t('Username'));
       
   791   $form['user']['test'] = array('#type' => 'textfield', '#title' => '', '#description' => t('Enter a username to check if it will be denied or allowed.'), '#size' => 30, '#maxlength' => USERNAME_MAX_LENGTH);
       
   792   $form['user']['type'] = array('#type' => 'hidden', '#value' => 'user');
       
   793   $form['user']['submit'] = array('#type' => 'submit', '#value' => t('Check username'));
       
   794   $form['#submit'][] = 'user_admin_access_check_submit';
       
   795   $form['#validate'][] = 'user_admin_access_check_validate';
       
   796   $form['#theme'] = 'user_admin_access_check';
       
   797   return $form;
       
   798 }
       
   799 
       
   800 function user_admin_check_mail() {
       
   801   $form['mail'] = array('#type' => 'fieldset', '#title' => t('E-mail'));
       
   802   $form['mail']['test'] = array('#type' => 'textfield', '#title' => '', '#description' => t('Enter an e-mail address to check if it will be denied or allowed.'), '#size' => 30, '#maxlength' => EMAIL_MAX_LENGTH);
       
   803   $form['mail']['type'] = array('#type' => 'hidden', '#value' => 'mail');
       
   804   $form['mail']['submit'] = array('#type' => 'submit', '#value' => t('Check e-mail'));
       
   805   $form['#submit'][] = 'user_admin_access_check_submit';
       
   806   $form['#validate'][] = 'user_admin_access_check_validate';
       
   807   $form['#theme'] = 'user_admin_access_check';
       
   808   return $form;
       
   809 }
       
   810 
       
   811 function user_admin_check_host() {
       
   812   $form['host'] = array('#type' => 'fieldset', '#title' => t('Hostname'));
       
   813   $form['host']['test'] = array('#type' => 'textfield', '#title' => '', '#description' => t('Enter a hostname or IP address to check if it will be denied or allowed.'), '#size' => 30, '#maxlength' => 64);
       
   814   $form['host']['type'] = array('#type' => 'hidden', '#value' => 'host');
       
   815   $form['host']['submit'] = array('#type' => 'submit', '#value' => t('Check hostname'));
       
   816   $form['#submit'][] = 'user_admin_access_check_submit';
       
   817   $form['#validate'][] = 'user_admin_access_check_validate';
       
   818   $form['#theme'] = 'user_admin_access_check';
       
   819   return $form;
       
   820 }
       
   821 
       
   822 function user_admin_access_check_submit($form, &$form_state) {
       
   823   switch ($form_state['values']['type']) {
       
   824     case 'user':
       
   825       if (drupal_is_denied('user', $form_state['values']['test'])) {
       
   826         drupal_set_message(t('The username %name is not allowed.', array('%name' => $form_state['values']['test'])));
       
   827       }
       
   828       else {
       
   829         drupal_set_message(t('The username %name is allowed.', array('%name' => $form_state['values']['test'])));
       
   830       }
       
   831       break;
       
   832     case 'mail':
       
   833       if (drupal_is_denied('mail', $form_state['values']['test'])) {
       
   834         drupal_set_message(t('The e-mail address %mail is not allowed.', array('%mail' => $form_state['values']['test'])));
       
   835       }
       
   836       else {
       
   837         drupal_set_message(t('The e-mail address %mail is allowed.', array('%mail' => $form_state['values']['test'])));
       
   838       }
       
   839       break;
       
   840     case 'host':
       
   841       if (drupal_is_denied('host', $form_state['values']['test'])) {
       
   842         drupal_set_message(t('The hostname %host is not allowed.', array('%host' => $form_state['values']['test'])));
       
   843       }
       
   844       else {
       
   845         drupal_set_message(t('The hostname %host is allowed.', array('%host' => $form_state['values']['test'])));
       
   846       }
       
   847       break;
       
   848     default:
       
   849       break;
       
   850   }
       
   851 }
       
   852 
       
   853 /**
       
   854  * Menu callback: delete an access rule
       
   855  *
       
   856  * @ingroup forms
       
   857  * @see user_admin_access_delete_confirm_submit()
       
   858  */
       
   859 function user_admin_access_delete_confirm($form_state, $aid = 0) {
       
   860   $access_types = array('user' => t('username'), 'mail' => t('e-mail'), 'host' => t('host'));
       
   861   $edit = db_fetch_object(db_query('SELECT aid, type, status, mask FROM {access} WHERE aid = %d', $aid));
       
   862 
       
   863   $form = array();
       
   864   $form['aid'] = array('#type' => 'hidden', '#value' => $aid);
       
   865   $output = confirm_form($form,
       
   866                   t('Are you sure you want to delete the @type rule for %rule?', array('@type' => $access_types[$edit->type], '%rule' => $edit->mask)),
       
   867                   'admin/user/rules',
       
   868                   t('This action cannot be undone.'),
       
   869                   t('Delete'),
       
   870                   t('Cancel'));
       
   871   return $output;
       
   872 }
       
   873 
       
   874 function user_admin_access_delete_confirm_submit($form, &$form_state) {
       
   875   db_query('DELETE FROM {access} WHERE aid = %d', $form_state['values']['aid']);
       
   876   drupal_set_message(t('The access rule has been deleted.'));
       
   877   $form_state['redirect'] = 'admin/user/rules';
       
   878   return;
       
   879 }
       
   880 
       
   881 /**
       
   882  * Menu callback: list all access rules
       
   883  */
       
   884 function user_admin_access() {
       
   885   $header = array(array('data' => t('Access type'), 'field' => 'status'), array('data' => t('Rule type'), 'field' => 'type'), array('data' => t('Mask'), 'field' => 'mask'), array('data' => t('Operations'), 'colspan' => 2));
       
   886   $result = db_query("SELECT aid, type, status, mask FROM {access}". tablesort_sql($header));
       
   887   $access_types = array('user' => t('username'), 'mail' => t('e-mail'), 'host' => t('host'));
       
   888   $rows = array();
       
   889   while ($rule = db_fetch_object($result)) {
       
   890     $rows[] = array($rule->status ? t('allow') : t('deny'), $access_types[$rule->type], $rule->mask, l(t('edit'), 'admin/user/rules/edit/'. $rule->aid), l(t('delete'), 'admin/user/rules/delete/'. $rule->aid));
       
   891   }
       
   892   if (empty($rows)) {
       
   893     $rows[] = array(array('data' => '<em>'. t('There are currently no access rules.') .'</em>', 'colspan' => 5));
       
   894   }
       
   895   return theme('table', $header, $rows);
       
   896 }
       
   897 
       
   898 /**
       
   899  * Theme user administration overview.
       
   900  *
       
   901  * @ingroup themeable
       
   902  */
       
   903 function theme_user_admin_account($form) {
       
   904   // Overview table:
       
   905   $header = array(
       
   906     theme('table_select_header_cell'),
       
   907     array('data' => t('Username'), 'field' => 'u.name'),
       
   908     array('data' => t('Status'), 'field' => 'u.status'),
       
   909     t('Roles'),
       
   910     array('data' => t('Member for'), 'field' => 'u.created', 'sort' => 'desc'),
       
   911     array('data' => t('Last access'), 'field' => 'u.access'),
       
   912     t('Operations')
       
   913   );
       
   914 
       
   915   $output = drupal_render($form['options']);
       
   916   if (isset($form['name']) && is_array($form['name'])) {
       
   917     foreach (element_children($form['name']) as $key) {
       
   918       $rows[] = array(
       
   919         drupal_render($form['accounts'][$key]),
       
   920         drupal_render($form['name'][$key]),
       
   921         drupal_render($form['status'][$key]),
       
   922         drupal_render($form['roles'][$key]),
       
   923         drupal_render($form['member_for'][$key]),
       
   924         drupal_render($form['last_access'][$key]),
       
   925         drupal_render($form['operations'][$key]),
       
   926       );
       
   927     }
       
   928   }
       
   929   else {
       
   930     $rows[] = array(array('data' => t('No users available.'), 'colspan' => '7'));
       
   931   }
       
   932 
       
   933   $output .= theme('table', $header, $rows);
       
   934   if ($form['pager']['#value']) {
       
   935     $output .= drupal_render($form['pager']);
       
   936   }
       
   937 
       
   938   $output .= drupal_render($form);
       
   939 
       
   940   return $output;
       
   941 }
       
   942 
       
   943 /**
       
   944  * Theme the new-role form.
       
   945  *
       
   946  * @ingroup themeable
       
   947  */
       
   948 function theme_user_admin_new_role($form) {
       
   949   $header = array(t('Name'), array('data' => t('Operations'), 'colspan' => 2));
       
   950   foreach (user_roles() as $rid => $name) {
       
   951     $edit_permissions = l(t('edit permissions'), 'admin/user/permissions/'. $rid);
       
   952     if (!in_array($rid, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID))) {
       
   953       $rows[] = array($name, l(t('edit role'), 'admin/user/roles/edit/'. $rid), $edit_permissions);
       
   954     }
       
   955     else {
       
   956       $rows[] = array($name, t('locked'), $edit_permissions);
       
   957     }
       
   958   }
       
   959   $rows[] = array(drupal_render($form['name']), array('data' => drupal_render($form['submit']), 'colspan' => 2));
       
   960 
       
   961   $output = drupal_render($form);
       
   962   $output .= theme('table', $header, $rows);
       
   963 
       
   964   return $output;
       
   965 }
       
   966 
       
   967 /**
       
   968  * Theme user administration filter form.
       
   969  *
       
   970  * @ingroup themeable
       
   971  */
       
   972 function theme_user_filter_form($form) {
       
   973   $output = '<div id="user-admin-filter">';
       
   974   $output .= drupal_render($form['filters']);
       
   975   $output .= '</div>';
       
   976   $output .= drupal_render($form);
       
   977   return $output;
       
   978 }
       
   979 
       
   980 /**
       
   981  * Theme user administration filter selector.
       
   982  *
       
   983  * @ingroup themeable
       
   984  */
       
   985 function theme_user_filters($form) {
       
   986   $output = '<ul class="clear-block">';
       
   987   if (!empty($form['current'])) {
       
   988     foreach (element_children($form['current']) as $key) {
       
   989       $output .= '<li>'. drupal_render($form['current'][$key]) .'</li>';
       
   990     }
       
   991   }
       
   992 
       
   993   $output .= '<li><dl class="multiselect">'. (!empty($form['current']) ? '<dt><em>'. t('and') .'</em> '. t('where') .'</dt>' : '') .'<dd class="a">';
       
   994   foreach (element_children($form['filter']) as $key) {
       
   995     $output .= drupal_render($form['filter'][$key]);
       
   996   }
       
   997   $output .= '</dd>';
       
   998 
       
   999   $output .= '<dt>'. t('is') .'</dt><dd class="b">';
       
  1000 
       
  1001   foreach (element_children($form['status']) as $key) {
       
  1002     $output .= drupal_render($form['status'][$key]);
       
  1003   }
       
  1004   $output .= '</dd>';
       
  1005 
       
  1006   $output .= '</dl>';
       
  1007   $output .= '<div class="container-inline" id="user-admin-buttons">'. drupal_render($form['buttons']) .'</div>';
       
  1008   $output .= '</li></ul>';
       
  1009 
       
  1010   return $output;
       
  1011 }