<?php

/*
 * This module splits user's account page on two pages: General Data (user profile form) and Reset password (changing password form)
 * Also it provides a block with links to that pages.
 * All sequrity code has taken from user.module
 *
 */
 
function change_password_menu() {

  $items['user/%user/change_password'] = array(
    'title' => 'Change password',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('change_password_form'),
    'access callback' => 'user_edit_access',
    'access arguments' => array(1),
    'type' => MENU_LOCAL_TASK,
    'weight' => 10,
  );

  return $items;
}


/**
 * Implements hook_block_info().
 */
function change_password_block_info() {
  return array(
    'change_password_form' => array(
      'info' => t('Change Password Form')
    ),
    'my_account_block' => array(
      'info' => t('My Account Block')
    )
  );
}

/**
 * Implements hook_block_view().
 */
function change_password_block_view($delta = '') {
  $block = array();
  // Only show the block for a logged-in user.
  if ($delta == 'change_password_form' && user_is_logged_in()) {
    $block['subject'] = t('Change Password');
    $block['content'] = drupal_get_form('change_password_form');
  }  
  if ($delta == 'my_account_block' && user_is_logged_in()) {
    $block['subject'] = t('My Account');
    $block['content'] = change_password_get_my_account_block_content();
  }
  return $block;
}
    
/**
 * Password change form.
 */
function change_password_form($form, &$form_state) {
  // Sanity check
  if (user_is_anonymous()) {
    return drupal_access_denied(); 
  }

  drupal_set_title(t('My Account - Reset Password'));
  
  // Get the currently logged in user object.
  $form['#account'] = user_load($GLOBALS['user']->uid);
    
  $pass_reset = isset($_SESSION['pass_reset_' . $GLOBALS['user']->uid]) && isset($_GET['pass-reset-token']) && ($_GET['pass-reset-token'] == $_SESSION['pass_reset_' . $GLOBALS['user']->uid]);
  
  // Textfield cor current password confirmation.
  $form['current_pass'] = array(
    '#type' => 'password',
    '#title' => t('Current password'),
    '#size' => 25,
    '#access' => !$pass_reset,
    '#required' => TRUE,
  );

  $form['account']['current_pass_required'] = array(
    '#type' => 'value',
    '#value' => !$pass_reset,
  );
      
  // Password confirm field.
  $form['account']['pass'] = array(
    '#type' => 'password_confirm',
    '#size' => 25,
    '#title' => t('New Password'),
    '#required' => TRUE
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit')
  );

  if (!isset($form['#attributes'])) $form['#attributes'] = array('class' => '');
  else if (!isset($form['#attributes']['class'])) $form['#attributes']['class'] = '';
  $form['#attributes']['class'] .= $pass_reset ? 'without_current_pass' : 'with_current_pass';
  
  return $form;
}

/**
 * Validate handler for change_password_form().
 */
function change_password_form_validate(&$form, &$form_state) {  
  // Make sure the password functions are present.
  require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');
  
  // Make sure the provided current password is valid for this account.
  if (!user_check_password($form_state['values']['current_pass'], $form['#account']) && $form_state['values']['current_pass_required']) {
    form_set_error('current_pass', t('The current password you provided is incorrect.'));
  }
}

 /**
 * Submit handler for change_password_form().
 */
function change_password_form_submit(&$form, &$form_state) {
  // Set up the edit array to pass to user_save()
  $edit = array('pass' => $form_state['values']['pass']);
  
  // Save the account with the new password.
  user_save($form['#account'], $edit);
  
  // Inform the user.
  drupal_set_message(t('Your password has been changed.'));
  
  unset($_SESSION['pass_reset_' . $GLOBALS['user']->uid]);
  
  drupal_goto('user/' . $GLOBALS['user']->uid . '/edit');
}

function change_password_get_my_account_block_content() {
  global $user;
  $uid = $user->uid;
  $items = array(
    array(
      'data' => t('Account Information'),
      'children' => array (
        l(t('General Data'), 'user/' . $uid . '/edit'),
        l(t('Reset Password'), 'user/' . $uid . '/change_password'),
      ),
    ),
  );
  return theme('item_list', array(
    'items' => $items,
    'title' => '',
    'type' => 'ul',
    'attributes' => array('class' => 'menu my_account_menu'),
    )
  );
}