You are here

function protected_node_admin_settings in Protected Node 7

Same name and namespace in other branches
  1. 5 protected_node.module \protected_node_admin_settings()
  2. 6 protected_node.settings.inc \protected_node_admin_settings()
  3. 1.0.x protected_node.settings.inc \protected_node_admin_settings()

Define the settings form.

Return value

array $form The settings form

1 string reference to 'protected_node_admin_settings'
protected_node_menu_array in ./protected_node.settings.inc
Actual implementation of the protected_node_menu() function.

File

./protected_node.settings.inc, line 287
Configuration file for the protected_node module.

Code

function protected_node_admin_settings() {

  // Total.
  $total_count = db_select('node')
    ->fields('node', array(
    'nid',
  ))
    ->countQuery()
    ->execute()
    ->fetchField();

  // Protected.
  $protected_count = db_select('protected_nodes')
    ->fields('protected_nodes', array(
    'nid',
  ))
    ->condition('protected_node_is_protected', 1)
    ->countQuery()
    ->execute()
    ->fetchField();

  // Title.
  $title_count = db_select('protected_nodes')
    ->fields('protected_nodes', array(
    'nid',
  ))
    ->condition('protected_node_is_protected', 1)
    ->condition('protected_node_show_title', 1)
    ->countQuery()
    ->execute()
    ->fetchField();

  // Global.
  $global_count = db_select('protected_nodes')
    ->fields('protected_nodes', array(
    'nid',
  ))
    ->condition('protected_node_is_protected', 1)
    ->condition('protected_node_passwd', '')
    ->countQuery()
    ->execute()
    ->fetchField();
  $unprotected_count = $total_count - $protected_count;
  $hiding_count = $protected_count - $title_count;
  $node_password_count = $protected_count - $global_count;

  // Any nodes?
  if ($total_count > 0) {

    // Statistics.
    $form['protected_node_stats'] = array(
      '#type' => 'fieldset',
      '#title' => t('Protected Node statistics'),
      '#collapsible' => TRUE,
    );
    $column_labels = array(
      array(
        'data' => t('Label'),
        'style' => 'text-align:center',
      ),
      array(
        'data' => t('Count'),
        'style' => 'text-align:center',
      ),
      array(
        'data' => t('Percent'),
        'style' => 'text-align:center',
      ),
    );
    $rows = array();
    $rows[] = array(
      t('Total nodes'),
      array(
        'data' => $total_count,
        'style' => 'text-align:right',
      ),
      array(
        'data' => t('100%'),
        'style' => 'text-align:right',
      ),
    );
    $rows[] = array(
      t('Unprotected nodes'),
      array(
        'data' => $unprotected_count,
        'style' => 'text-align:right',
      ),
      array(
        'data' => round($unprotected_count * 100 / $total_count, 2) . '%',
        'style' => 'text-align:right',
      ),
    );
    $rows[] = array(
      t('Protected nodes'),
      array(
        'data' => $protected_count,
        'style' => 'text-align:right',
      ),
      array(
        'data' => round($protected_count * 100 / $total_count, 2) . '%',
        'style' => 'text-align:right',
      ),
    );
    if ($protected_count > 0) {
      $rows[] = array(
        t('Showing title'),
        array(
          'data' => $title_count,
          'style' => 'text-align:right',
        ),
        array(
          'data' => round($title_count * 100 / $protected_count, 2) . '%',
          'style' => 'text-align:right',
        ),
      );
      $rows[] = array(
        t('Hiding title'),
        array(
          'data' => $hiding_count,
          'style' => 'text-align:right',
        ),
        array(
          'data' => round($hiding_count * 100 / $protected_count, 2) . '%',
          'style' => 'text-align:right',
        ),
      );
      $rows[] = array(
        t('Global passwords'),
        array(
          'data' => $global_count,
          'style' => 'text-align:right',
        ),
        array(
          'data' => round($global_count * 100 / $protected_count, 2) . '%',
          'style' => 'text-align:right',
        ),
      );
      $rows[] = array(
        t('Node passwords'),
        array(
          'data' => $node_password_count,
          'style' => 'text-align:right',
        ),
        array(
          'data' => round($node_password_count * 100 / $protected_count, 2) . '%',
          'style' => 'text-align:right',
        ),
      );
    }
    $form['protected_node_stats']['protected_node_statistics'] = array(
      '#type' => 'markup',
      '#markup' => theme('table', array(
        'header' => $column_labels,
        'rows' => $rows,
      )),
    );
  }

  // Security related options.
  $form['protected_node_security'] = array(
    '#type' => 'fieldset',
    '#title' => t('Protected node security'),
    '#collapsible' => TRUE,
  );
  $form['protected_node_security']['protected_node_use_global_password'] = array(
    '#type' => 'select',
    '#title' => t('Global password handling'),
    '#default_value' => variable_get('protected_node_use_global_password', PROTECTED_NODE_PER_NODE_PASSWORD),
    '#options' => array(
      PROTECTED_NODE_PER_NODE_PASSWORD => t('Per node password'),
      PROTECTED_NODE_PER_NODE_AND_GLOBAL_PASSWORD => t('Per node password or Global password'),
      PROTECTED_NODE_GLOBAL_PASSWORD => t('Global password only'),
    ),
    '#description' => t("Selecting the global password option makes it possible to protect nodes using one password which must be provided in the 'Global password' fields below.\n      <br><strong>WARNING:</strong> Removing the global password after it has been assigned to nodes will make those nodes inaccessible except for the user with UID: 1 and the author of the node."),
  );
  $form['protected_node_security']['protected_node_global_password_field'] = array(
    '#type' => 'password_confirm',
    '#title' => t('Global password'),
    '#description' => t('The default password for all nodes. This password is necessary for global password handling.'),
  );
  $form['protected_node_security']['protected_node_show_password_strength'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show password strength in nodes'),
    '#default_value' => variable_get('protected_node_show_password_strength', TRUE),
    '#description' => t('When checked, show the password strength on nodes being edited. This will prevent the use of weak passwords.'),
  );
  $form['protected_node_security']['protected_node_show_node_titles'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show node titles by default'),
    '#default_value' => variable_get('protected_node_show_node_titles', FALSE),
    '#description' => t('Whether the node title should be shown by default. This setting can be overridden when creating and editing nodes.'),
  );
  $form['protected_node_security']['protected_node_allow_hint'] = array(
    '#type' => 'checkbox',
    '#title' => t('Allow author to enter a password hint'),
    '#default_value' => variable_get('protected_node_allow_hint', FALSE),
    '#description' => t('By default, the password is kept as secret as possible. This option allows the author to enter a hint about the password of a node. The hint can later be shown using the [node:password-hint] token.'),
  );
  $form['protected_node_auto_email'] = array(
    '#type' => 'fieldset',
    '#title' => t('Protected node email support'),
    '#description' => t('This setting allows users who have access to protect a node to send an email about the protected node.'),
    '#collapsible' => TRUE,
  );
  $form['protected_node_auto_email']['protected_node_email_activation'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable protected node email support'),
    '#default_value' => variable_get('protected_node_email_activation', FALSE),
    '#description' => t('Check this box to enable the protected node email support (NOTE: Random password support available within this feature).'),
  );
  $form['protected_node_auto_email']['protected_node_email_box_width'] = array(
    '#type' => 'textfield',
    '#title' => t('Email box width'),
    '#size' => 10,
    '#default_value' => variable_get('protected_node_email_box_width', '10'),
    '#description' => t('Enter the width (number of columns) of the email box text area. Must be a positive integer. The default value is 10.'),
    '#states' => array(
      'visible' => array(
        ':input[name="protected_node_email_activation"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  $form['protected_node_auto_email']['protected_node_email_box_height'] = array(
    '#type' => 'textfield',
    '#title' => t('Email box height'),
    '#size' => 10,
    '#default_value' => variable_get('protected_node_email_box_height', '2'),
    '#description' => t('Enter the height (number of rows) of the email box text area. Must be a positive integer. The default value is 2.'),
    '#states' => array(
      'visible' => array(
        ':input[name="protected_node_email_activation"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  $form['protected_node_auto_email']['protected_node_email_from'] = array(
    '#type' => 'textfield',
    '#title' => t('From email address'),
    '#default_value' => variable_get('protected_node_email_from', ''),
    '#description' => t('Enter the email address used in the email header. By default, [site-mail] (@mail) is used, if defined.', array(
      '@mail' => variable_get('site_mail', '<undefined>'),
    )),
    '#states' => array(
      'visible' => array(
        ':input[name="protected_node_email_activation"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  $form['protected_node_auto_email']['protected_node_email_subject'] = array(
    '#type' => 'textfield',
    '#title' => t('Email subject'),
    '#default_value' => variable_get('protected_node_email_subject', protected_node_email_subject()),
    '#description' => t('Enter the subject of the email. You may enter tokens in this field. Remember that [user-name] will be the author name.'),
    '#states' => array(
      'visible' => array(
        ':input[name="protected_node_email_activation"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  $form['protected_node_auto_email']['protected_node_email_body'] = array(
    '#type' => 'textarea',
    '#title' => t('Email content'),
    '#rows' => 15,
    '#default_value' => variable_get('protected_node_email_body', protected_node_email_body()),
    '#description' => t('Enter the body of the email. You may enter tokens in this field. Remember that [user-name] will be the author name.'),
    '#states' => array(
      'visible' => array(
        ':input[name="protected_node_email_activation"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  $form['protected_node_auto_email']['protected_node_random_password'] = array(
    '#type' => 'checkbox',
    '#title' => t('Generate a random password if necessary'),
    '#default_value' => variable_get('protected_node_random_password', FALSE),
    '#description' => t('When this flag is set, saving a protected node without re-entering the password will randomize a password and send it to your users. You may also add your email address to get notified of the password. (It otherwise will be stored in the database encrypted.)'),
    '#states' => array(
      'visible' => array(
        ':input[name="protected_node_email_activation"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  $form['protected_node_form'] = array(
    '#type' => 'fieldset',
    '#title' => t('Protected node form'),
    '#collapsible' => TRUE,
  );
  $form['protected_node_form']['protected_node_cancel'] = array(
    '#type' => 'checkbox',
    '#title' => t('Always add a cancel link'),
    '#default_value' => variable_get('protected_node_cancel', 0),
    '#description' => t('Whether a cancel link should be added to the password form. If checked and we do not have a back link, then the cancel is set to &lt;front&gt; instead.'),
  );
  $form['protected_node_form']['protected_node_title'] = array(
    '#type' => 'textfield',
    '#title' => t('Password page title'),
    '#default_value' => variable_get('protected_node_title', t('Protected page -- Enter password')),
    '#description' => t('Enter the title of the protected node page. No HTML allowed. You can use node type tokens provided by the token module if installed.'),
  );
  $form['protected_node_form']['protected_node_info'] = array(
    '#type' => 'textarea',
    '#title' => t('Password page general information'),
    '#default_value' => variable_get('protected_node_info', ''),
    '#description' => t('Enter general information for the protected node page. HTML is allowed. You can use node type tokens provided by the token module if installed.'),
  );
  $form['protected_node_form']['protected_node_description'] = array(
    '#type' => 'textarea',
    '#title' => t('Password page description (inside the field set with the password form)'),
    '#default_value' => variable_get('protected_node_description', ''),
    '#description' => t('Enter custom description for the protected node page. This description is displayed inside the fieldset with the password form. HTML is accepted. You can use node type tokens provided by the token module if installed.'),
  );
  $form['protected_node_form']['protected_node_password_label'] = array(
    '#type' => 'textfield',
    '#title' => t('Password field label on password page'),
    '#default_value' => variable_get('protected_node_password_label', ''),
    '#description' => t('Enter the text for the password label appearing on the password page. The default (when empty) is the node type name followed by the word "password". You can use tokens provided by the token module if installed.'),
  );
  if (module_exists('token')) {
    $vars = array(
      'token_types' => array(
        'global',
        'node',
        'user',
      ),
    );
    $form['protected_node_form']['protected_node_tokens'] = array(
      '#value' => theme('token_tree', $vars),
      '#description' => t("WARNING: the user tokens should only be used if anonymous users cannot access protected nodes; otherwise the result may not be what you'd expect."),
    );
  }

  // Flood control options.
  $form['protected_node_flood_control'] = array(
    '#type' => 'fieldset',
    '#title' => t('Protected node flood control'),
    '#collapsible' => TRUE,
  );
  $form['protected_node_flood_control']['protected_node_failed_password_ip_limit'] = array(
    '#type' => 'select',
    '#title' => t('Failed password (IP) limit'),
    '#options' => _protected_node_get_failed_password_ip_limit_options(),
    '#default_value' => variable_get('protected_node_failed_password_ip_limit', 50),
    '#description' => t('Defines the number of attempts per user during a limited time window.'),
  );
  $form['protected_node_flood_control']['protected_node_failed_password_ip_window'] = array(
    '#type' => 'select',
    '#title' => t('Failed password (IP) window'),
    '#options' => _protected_node_get_failed_password_ip_window_options(),
    '#default_value' => variable_get('protected_node_failed_password_ip_window', 3600),
    '#description' => t('Defines the time window (in seconds) for the allowed number of attempts.'),
  );

  // View modes.
  $node_info = entity_get_info('node');
  $view_modes = array();
  foreach ($node_info['view modes'] as $id => $item) {
    $view_modes[$id] = $item['label'];
  }
  asort($view_modes);
  $form['protected_node_view_modes'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#title' => t('Display Suite view modes'),
  );
  $form['protected_node_view_modes']['protected_node_checked_view_modes'] = array(
    '#type' => 'select',
    '#multiple' => TRUE,
    '#title' => t('Enabled view modes'),
    '#description' => t('Only check passwords for nodes that are rendered with the selected view modes.'),
    '#options' => $view_modes,
    '#default_value' => variable_get('protected_node_checked_view_modes', _protected_node_get_default_checked_view_modes()),
  );

  // A few actions to do some "mass work".
  $form['protected_node_actions'] = array(
    '#type' => 'fieldset',
    '#title' => t('Protected node actions'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );

  // Clear Sessions button.
  $form['protected_node_actions']['protected_node_clear_sessions'] = array(
    '#type' => 'fieldset',
    '#title' => t('Clear sessions'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#description' => t('Click on this button to reset all the Protected Node password sessions of your website. This means all the users who entered a password in the past will now have to log back in to review their node protection.'),
  );
  $form['protected_node_actions']['protected_node_clear_sessions']['protected_node_clear_sessions_button'] = array(
    '#type' => 'submit',
    '#value' => t('Clear sessions'),
    '#submit' => array(
      'protected_node_action_clear_sessions',
    ),
  );

  // Reset passwords button.
  $form['protected_node_actions']['protected_node_reset_passwords'] = array(
    '#type' => 'fieldset',
    '#title' => t('Reset passwords'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#description' => t('WARNING: this feature sets a password on nodes that were otherwise expected to use the global password.'),
  );
  $form['protected_node_actions']['protected_node_reset_passwords']['protected_node_reset_passwords_password'] = array(
    '#type' => 'password_confirm',
    '#title' => t('New Protected Nodes password'),
    '#description' => t('Enter the new password that will be assigned to all existing protected nodes.'),
  );
  $form['protected_node_actions']['protected_node_reset_passwords']['protected_node_reset_passwords_button'] = array(
    '#type' => 'submit',
    '#value' => t('Reset all existing passwords'),
    '#submit' => array(
      'protected_node_action_reset_passwords',
    ),
  );

  // Remove passwords button.
  $form['protected_node_actions']['protected_node_remove_passwords'] = array(
    '#type' => 'fieldset',
    '#title' => t('Use global password'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#description' => '<span style="color: red;">' . t('WARNING: this function deletes all the individual passwords. Make sure you have authorized the use of global password.') . '</span>',
  );
  $form['protected_node_actions']['protected_node_remove_passwords']['protected_node_remove_passwords_button'] = array(
    '#type' => 'submit',
    '#value' => t('Use global password on ALL nodes'),
    '#submit' => array(
      'protected_node_action_remove_passwords',
    ),
  );

  // Remove protection button.
  $form['protected_node_actions']['protected_node_remove_protection'] = array(
    '#type' => 'fieldset',
    '#title' => t('Remove protection'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#description' => '<span style="color: red;">' . t('WARNING: this function unprotects all your currently protected nodes! The password is not modifiable.') . '</span>',
  );
  $form['protected_node_actions']['protected_node_remove_protection']['protected_node_remove_protection_button'] = array(
    '#type' => 'submit',
    '#value' => t('Remove password protection from all nodes'),
    '#submit' => array(
      'protected_node_action_unprotected_all',
    ),
  );

  // Protect ALL nodes buttons.
  $form['protected_node_actions']['protected_node_protect_nodes'] = array(
    '#type' => 'fieldset',
    '#title' => t('Protect nodes'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#description' => t('WARNING: this function protects ALL your nodes making them accessible only when the password is known. Nodes that do not yet have a password will make use of the global password. You may use the Reset password feature to change that password afterwards.'),
  );
  $form['protected_node_actions']['protected_node_protect_nodes']['protected_node_protect_all_nodes_button'] = array(
    '#type' => 'submit',
    '#value' => t('Protect ALL nodes'),
    '#submit' => array(
      'protected_node_action_protect_all_nodes',
    ),
  );

  // Re-protect nodes buttons.
  $form['protected_node_actions']['protected_node_protect_password'] = array(
    '#type' => 'fieldset',
    '#title' => t('Restore protection'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#description' => t('WARNING: this function re-protects the nodes that had a password and got unprotected.'),
  );
  $form['protected_node_actions']['protected_node_protect_password']['protected_node_protect_nodes_with_password_button'] = array(
    '#type' => 'submit',
    '#value' => t('Protect nodes that already have a password'),
    '#submit' => array(
      'protected_node_action_protect_nodes_with_password',
    ),
  );
  $form['#validate'][] = '_protected_node_admin_settings_validate';
  $form['#submit'][] = '_protected_node_admin_settings_submit';
  return system_settings_form($form);
}