[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/modules/user/ -> user.admin.inc (source)

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


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7