<?php
/**
  @licence GPL 2005-2011  The osCSS developers - osCSS Open Source E-commerce
  @portion code Copyright (c) 2002 osCommerce
  @package osCSS-2 <www http://www.oscss.org>
  @version 2.1.0
  @date  12/04/11, 15:34
  @author oscim <mail aurelien@oscim.fr> <www http://www.oscim.fr>
  @encode UTF-8
 *\brief Page process passport perdu
*/
  require('includes/application_top.php');

  $page->init(basename(__FILE__),$language);

  if (isset($_GET['action']) && ($_GET['action'] == 'process')) {

    $cust=customer::getInstance();

    $email_address=tep_db_prepare_input($_POST['email_address']);
    $customers_id=customer::search('customers_email_address', $email_address);

    /// user no found redirect
    if($customers_id==false ){
      $page->messageStack->add_session('create_account', TEXT_NO_EMAIL_ADDRESS_FOUND);
      tep_redirect(tep_href_link(FILENAME_CREATE_ACCOUNT,'', 'SSL'));
    }

    $u=$cust->the_customer($customers_id);
    $user=$cust->detail_long_cust();

      /// include the password crypto functions
      require_once(DIR_WS_FUNCTIONS . 'password_funcs.php');

      /**
       * TODO: Utiliser class page.customer.php
       */
      $new_password = tep_create_random_value(ENTRY_PASSWORD_MIN_LENGTH);
      $crypted_password = tep_encrypt_password($new_password);

      $DB->query("update " . TABLE_CUSTOMERS . " set customers_password = '" . tep_db_input($crypted_password) . "' where customers_id = '" . (int)$user['customers_id'] . "'");

      tep_mail($user['customers_firstname'] . ' ' . $user['customers_lastname'], $email_address, EMAIL_PASSWORD_REMINDER_SUBJECT, sprintf(EMAIL_PASSWORD_REMINDER_BODY, $new_password), STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS);

      $page->messageStack->add_session('login', SUCCESS_PASSWORD_SENT, 'success');

      tep_redirect(tep_href_link(FILENAME_LOGIN, '', 'SSL'));

  }
  /* Arrivé via admin */
  elseif (isset($_GET['cID']) && is_numeric($_GET['cID'])){
    $cID = tep_db_prepare_input($_GET['cID']);
    $check_customer_query=$DB->query = tep_db_query("select customers_email_address from " . TABLE_CUSTOMERS . " where customers_id = '" . tep_db_input($cID) . "'");
    if ($check_customer_query__get('NumRows')>0) {
      $check_customer = $check_customer_query->fetchAssoc();
      $email_address=$check_customer['customers_email_address'];
    }
  }

  $page->breadcrumb->add(NAVBAR_TITLE_1, tep_href_link(FILENAME_LOGIN, '', 'SSL'));
  $page->post_init();

  require($page->path_gabarit());

  require(DIR_WS_INCLUDES . 'application_bottom.php');
?>