<?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)
 *
 */
 
  /*===================================================
    SendLinkPwd()
  ===================================================*/

  function SendLinkPwd() {

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

    // vars url & form
    global $lid;

    // vars messages
    global $msg;

    // vars template
    global $title, $url, $email, $password;

    $query      = "select title, url, email, password from idx_link where link_id='$lid'";
    $result     = $dbConn->Execute($query);
    $title      = $result->Fields("title");
    $url        = $result->Fields("url");
    $email      = $result->Fields("email");
    $password   = $result->Fields("password");

    // send email

    $email_body = EvalTemplate($theme_path . "mail/send_pwd_link.mail", "\$title,\$url,\$email,\$password");
    $from       = $email_address;
    @mail("$email", $msg["10271"], $email_body, "From: $from\nX-Mailer: INDEXU_X-Mailer/1.0");

    DisplayTemplate($theme_path . "send_pwd_link_ok.html", "\$email,\$lid");
  }


  /*===================================================
    ShowFormLoginPwd()
  ===================================================*/
  
  function ShowFormLoginPwd() {

    // vars global configuration
    global $theme_path;

    // vars messages
    global $msg;

    // vars template
    global $error_msg, $username, $email;

    DisplayTemplate($theme_path . "send_pwd_form.html", "\$error_msg,\$username,\$email");
  }


  /*===================================================
    ProcessFormLoginPwd()
  ===================================================*/
  
  function ProcessFormLoginPwd() {

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

    // vars url & form
    global $email;

    // vars messages
    global $msg;

    // vars template
    global $error_msg, $username, $password, $hash;

    // verify input

    if (empty($username))
      $error_msg = $msg["10281"];
    elseif (empty($email))
      $error_msg = $msg["10282"];
    elseif (!IsEmailAddress($email))
      $error_msg = $msg["10283"];
    else {
      $query  = "select username, password, vcode from idx_users where username = '$username' and email = '$email' and status=1";
      $result = $dbConn->Execute($query);
      $total  = $result->RecordCount();

      if ($total > 0) {
        $username = $result->Fields("username");
        $password = $result->Fields("password");
        $vcode = $result->Fields("vcode");
      }
      else {
        $error_msg = $msg["10284"];
      }
    }

    if (empty($error_msg)) {
      if (USER_AUTH_TYPE == 'plain') {
        $body = EvalTemplate($theme_path . "mail/send_pwd_login.mail", "\$username,\$password");
        $from = $email_address;
        @mail($email, $msg["10273"], $body, "From: $from \nX-Mailer: INDEXU_X-Mailer/1.0");
        DisplayTemplate($theme_path . "send_pwd_login_ok.html", "\$email");
      }
      else {
        $hash = md5($username.$password.$vcode);
        $body = EvalTemplate($theme_path . "mail/send_pwd_reset.mail", "\$username,\$hash");
        $from = $email_address;
        @mail($email, $msg["10275"], $body, "From: $from \nX-Mailer: INDEXU_X-Mailer/1.0");
        DisplayTemplate($theme_path . "send_pwd_reset_ok.html", "\$email");
      }
    }
    else {
      ShowFormLoginPwd();
    }
  }


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

  include "application.php";

  RunPreFilter(__FILE__);

  if (empty($pflag)) {
    ShowFormLoginPwd();
  }
  elseif ($pflag == 'login_pwd') {
    ProcessFormLoginPwd();
  }
  elseif ($pflag == 'lp') {
    SendLinkPwd();
  }

  RunPostFilter(__FILE__);

?>