<?php

/**
 * @file
 * The password policy password tab module adds a separate tab to change
 * password.
 */

/****************************************************************************/
/* Core API hooks                                                           */
/****************************************************************************/

/**
 * Implements hook_menu().
 */
function password_policy_password_tab_menu() {
  return array(
    'admin/settings/password_policy/password_tab' => array(
      'title' => 'Password tab',
      'page callback' => 'drupal_get_form',
      'page arguments' => array('password_policy_password_tab_admin_settings'),
      'access arguments' => array('administer site configuration'),
      'file' => 'password_policy_password_tab.admin.inc',
    ),
    'user/%user_category/edit/password' => array(
      'title' => 'Password',
      'page callback' => 'drupal_get_form',
      'page arguments' => array('password_policy_password_tab', 1),
      'access callback' => 'user_edit_access',
      'access arguments' => array(1),
      'type' => MENU_LOCAL_TASK,
      'load arguments' => array('%map', '%index'),
      'file' => 'password_policy_password_tab.pages.inc',
    ),
  );
}

/**
 * Implements hook_user().
 */
function password_policy_password_tab_user($op, &$edit, &$account, $category = NULL) {
  if ($op === 'categories') {
    return array(array(
      'name' => 'password',
      'title' => t('Password'),
      'weight' => 0,
    ));
  }
}

/**
 * Implements hook_form_alter().
 */
function password_policy_password_tab_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    case "user_profile_form":
      // Hide core password field from user edit form.
      unset($form['account']['pass']);
      // Also hide password field added by Password Confirm module.
      if (module_exists('password_change')) {
        unset($form['pass_current']);
      }
      break;
  }
}

/**
 * Implements hook_exit().
 *
 * This function acts on a drupal_goto from user_pass_reset form. The user is
 * redirected to the password change tab instead of the account edit form.
 */
function password_policy_password_tab_exit($destination = NULL) {
  static $processed = FALSE;

  // Check if this is a drupal_goto from the password reset page.
  if (!$processed && isset($destination) && arg(0) == 'user' && arg(1) == 'reset') {
    $url_parts = parse_url($destination);
    // Check if the redirect has a path.
    if (isset($url_parts['path'])) {
      // Check if the redirect path is user/%user/edit.
      $path = drupal_substr($url_parts['path'], 1);
      $args = arg(NULL, $path);
      if (count($args) == 3 && $args[0] == 'user' && $args[2] == 'edit') {
        // Prevent loops.
        $processed = TRUE;
        // Change the drupal_goto to our change password tab.
        $path .= '/password';
        $query = isset($url_parts['query']) ? $url_parts['query'] : NULL;
        $fragment = isset($url_parts['fragment']) ? $url_parts['fragment'] : NULL;
        drupal_goto($path, $query, $fragment);
      }
    }
  }
}
