<?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');

  if (isset($HTTP_GET_VARS['action']) && ($HTTP_GET_VARS['action'] == 'process')) {
    $affiliate_username = tep_db_prepare_input($HTTP_POST_VARS['affiliate_username']);
    $affiliate_password = tep_db_prepare_input($HTTP_POST_VARS['affiliate_password']);
	$serverDate = date('Y-m-d H:i:s',getServerDate(false));
// Check if username exists
  //  $check_affiliate_query = tep_db_query("select affiliate_id, affiliate_firstname, affiliate_password, affiliate_email_address,encryption_style from " . TABLE_AFFILIATE . " where affiliate_email_address = '" . tep_db_input($affiliate_username) . "'");
	$add_option="";
	if (ACCOUNT_USERNAME=="true") $add_option=" or affiliate_username='" . tep_db_input($affiliate_username) . "'";
    $check_affiliate_query = tep_db_query("select affiliate_id, affiliate_firstname, affiliate_password, affiliate_email_address,encryption_style from " . TABLE_AFFILIATE . " where affiliate_email_address = '" . tep_db_input($affiliate_username) . "' " . $add_option);
    if (!tep_db_num_rows($check_affiliate_query)) {
      $HTTP_GET_VARS['login'] = 'fail';
    } else {
      $check_affiliate = tep_db_fetch_array($check_affiliate_query);
// Check that password is good
      if (!tep_validate_password($affiliate_password, $check_affiliate['affiliate_password'],$check_affiliate['encryption_style'])) {
        $HTTP_GET_VARS['login'] = 'fail';
      } else {
		if (!($check_affiliate['encryption_style']=="" && ENCRYPTION_STYLE=="O") && $check_affiliate['encryption_style']!=ENCRYPTION_STYLE){
			$new_password=tep_encrypt_password($affiliate_password);
			tep_db_query("UPDATE " . TABLE_AFFILIATE . " set affiliate_password='" . tep_db_input($new_password) . "', encryption_style='" . ENCRYPTION_STYLE . "' where affiliate_id=" . (int)$check_affiliate["affiliate_id"]);
		}

        $affiliate_id = $check_affiliate['affiliate_id'];
        tep_session_register('affiliate_id');

        $date_now = date('Ymd');

        tep_db_query("update " . TABLE_AFFILIATE . " set affiliate_date_of_last_logon ='". $serverDate ."', affiliate_number_of_logons = affiliate_number_of_logons + 1 where affiliate_id = '" . tep_db_input($affiliate_id) . "'");

        tep_redirect(tep_href_link(FILENAME_AFFILIATE_SUMMARY,'','SSL'));
      }
    }
  }

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

  $breadcrumb->add(NAVBAR_TITLE, tep_href_link(FILENAME_AFFILIATE, '', 'SSL'));


  $content = CONTENT_AFFILIATE;

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

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