<?php

/**
 *
 * INDEXU
 * Copyright(C), Nicecoder, 2000-2006, All Rights Reserved.
 *
 * INDEXU(tm) is protected by Indonesia and International copyright laws.
 * Unauthorized use or distribution of INDEXU(tm) is strictly prohibited,
 * violators will be prosecuted. To obtain a license for using INDEXU(tm),
 * please register at Nicecoder home page at http://www.nicecoder.com
 *
 * Author:
 *    Dody Rachmat Wicaksono (dody@nicecoder.com)
 *    M. Zuber (zubby@nicecoder.com)
 *
 */
 
  /*===================================================
    ShowFormResetPwd()
  ===================================================*/
  
  function ShowFormResetPwd() {

    // vars global configuration
    global $dbConn, $theme_path;

    // vars messages
    global $msg;

    // vars template
    global $error_msg, $u, $hash;
    
    $query  = "select * from idx_users where username = '$u' and status=1";
    $row = $dbConn->FetchOne($query);
    if (!$row['username'] || md5($row['username'].$row['password'].$row['vcode']) != $hash) {
      DisplayTemplate($theme_path . "reset_pwd_confirm_error.html");
    }
    else {
      DisplayTemplate($theme_path . "reset_pwd_form.html", "\$error_msg,\$u,\$hash");
    }
  }


  /*===================================================
    ProcessFormResetPwd()
  ===================================================*/
  
  function ProcessFormResetPwd() {

    // vars global configuration
    global $dbConn, $theme_path;

    // vars url & form
    global $email;

    // vars messages
    global $msg;

    // vars template
    global $error_msg, $u, $hash, $password, $password2;

    // verify input

    if ($_POST[captcha_key] != $_SESSION['captcha_key'])
      $error_msg = $msg["10361"];
    elseif (empty($password))
      $error_msg = $msg["10362"];
    elseif (empty($password2))
      $error_msg = $msg["10363"];
    elseif ($password != $password2)
      $error_msg = $msg["10364"];

    if (empty($error_msg)) {
      $query  = "select * from idx_users where username = '$u' and status=1";
      $row = $dbConn->FetchOne($query);
      if (!$row['username'] || md5($row['username'].$row['password'].$row['vcode']) != $hash) {
        DisplayTemplate($theme_path . "reset_pwd_confirm_error.html");
      }
      else {
        $users_obj             = new clsUsers;
        $users_obj->table_name = "idx_users";
        $auth = new auth();
        $users_obj->SetPassword($row['username'], $auth->Convert($password));      
        DisplayTemplate($theme_path . "reset_pwd_ok.html");
      }
    }
    else {
      ShowFormResetPwd();
    }
  }


  /*===================================================
    main
  ===================================================*/

  include "application.php";

  RunPreFilter(__FILE__);

  if (empty($pflag)) {
    ShowFormResetPwd();
  }
  elseif ($pflag == 'reset_pwd') {
    ProcessFormResetPwd();
  }

  RunPostFilter(__FILE__);

?>