<?php
/*
	osCommerce, Open Source E-Commerce Solutions 
	http://www.oscommerce.com 
	
	Copyright (c) 2003 osCommerce 
	
	osCommRes, Services Oline 
	http://www.oscommres.com 
	Copyright (c) 2005 osCommRes 
	
	Freeway eCommerce 
	http://www.zac-ware.com/freeway 
	Copyright (c) 2007 ZacWare
	
	Released under the GNU General Public License 

*/
// Set flag that this is a parent file
	define( '_FEXEC', 1 );

  require('includes/application_top.php');

  require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_AFFILIATE_PASSWORD_FORGOTTEN);

  if (isset($HTTP_GET_VARS['action']) && ($HTTP_GET_VARS['action'] == 'process')) {
    $check_affiliate_query = tep_db_query("select affiliate_email_address,affiliate_firstname, affiliate_lastname, affiliate_password, affiliate_id from " . TABLE_AFFILIATE . " where affiliate_email_address = '" . tep_db_input(tep_db_prepare_input($HTTP_POST_VARS['email_address'])) . "'");
    if (tep_db_num_rows($check_affiliate_query)) {
      $check_affiliate = tep_db_fetch_array($check_affiliate_query);
      // Crypted password mods - create a new password, update the database and mail it to them
      $newpass = tep_create_random_value(ENTRY_PASSWORD_MIN_LENGTH);
      $crypted_password = tep_encrypt_password($newpass);
      tep_db_query("update " . TABLE_AFFILIATE . " set affiliate_password = '" . tep_db_input($crypted_password) . "',encryption_style='" . ENCRYPTION_STYLE . "' where affiliate_id = '" . tep_db_input($check_affiliate['affiliate_id']) . "'");

      $merge_details=array(TEXT_FN=>$check_affiliate['affiliate_firstname'],
	                                      TEXT_LN=>$check_affiliate['affiliate_lastname'],
	                                      TEXT_AD=>$check_affiliate['affiliate_id'],
	                                      TEXT_AU=>$check_affiliate['affiliate_email_address'],
										  TEXT_AP=>$newpass,
										  TEXT_FL=>HTTP_SERVER . DIR_WS_CATALOG . FILENAME_AFFILIATE);

      $send_details=array(array('to_name'=>$check_affiliate['affiliate_firstname'] . " " . $check_affiliate['affiliate_lastname'],
	                                             'to_email'=>tep_db_prepare_input($HTTP_POST_VARS['email_address']),
												 'from_name'=>STORE_OWNER,
												 'from_email'=>AFFILIATE_EMAIL_ADDRESS));
     tep_send_default_email('AFR',$merge_details,$send_details);
      
      tep_redirect(tep_href_link(FILENAME_AFFILIATE, 'info_message=' . urlencode(TEXT_PASSWORD_SENT), 'SSL', true, false));
    } else {
      tep_redirect(tep_href_link(FILENAME_AFFILIATE_PASSWORD_FORGOTTEN, 'email=nonexistent', 'SSL'));
    }
  } else {

  $breadcrumb->add(NAVBAR_TITLE_1, tep_href_link(FILENAME_AFFILIATE, '', 'SSL'));
  $breadcrumb->add(NAVBAR_TITLE_2, tep_href_link(FILENAME_AFFILIATE_PASSWORD_FORGOTTEN, '', 'SSL'));
  }
   $content = CONTENT_AFFILIATE_PASSWORD_FORGOTTEN;

  require(DIR_WS_TEMPLATES . TEMPLATE_NAME . '/' . TEMPLATENAME_MAIN_PAGE);

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