<?php







class ControllerAccountLogin extends Controller {







 var $error = array();







	function index() {







		$customer =& $this->locator->get('customer');







		$language =& $this->locator->get('language');







		$module   =& $this->locator->get('module');







		$request  =& $this->locator->get('request');







		$response =& $this->locator->get('response');







		$session  =& $this->locator->get('session');







		$template =& $this->locator->get('template');







		$url      =& $this->locator->get('url');







		$config   =& $this->locator->get('config');







		$currency =& $this->locator->get('currency');







		$database =& $this->locator->get('database');



		



		$cart     =& $this->locator->get('cart');







		 $customer_manage = $database->getRow("select status,maintenance_status from login_setting");		



		



/*		if($customer_manage['status']==1)



		{



			if (!$customer->isLogged())



			{



				header('Location: gate.html');	exit;



			}



		}	 */



        // 后台直接点购物车登录过来



		if ($request->get('email', 'get') && $request->get('password', 'get'))



		{			



			$cart->clear(); // 先清空购物车



			if ($customer->login($request->get('email'), $request->get('password'))) {



				$response->redirect($url->ssl('cart'));



			}



		}



				



		if ($customer->isLogged())



		{



			$response->redirect($url->ssl('account'));



		}



		



				



		if ($request->get('email', 'post') && $request->get('password', 'post'))



		{







		    $language->load('controller/account_login.php');







			$template->set('title', $language->get('heading_title'));







			$verify=$this->verifyAccount($request->get('email', 'post'), $request->get('password', 'post'));















		    if($verify)







			{







			     $vip_rows=$database->getRows("select * from vip where 1");







				 $General_Buyer=$Bronze_Buyer=$Silver_Buyer=$Gold_Buyer=$Platinum_Buyer=$Diamond_Buyer=0;















				 foreach ($vip_rows as $vip)







				 {







					 if ( strcasecmp ($vip['vip_rank'], 'General Buyer')==0 )







					 {







						 $General_Buyer=$vip['standard_points'];







					 }







					 if ( strcasecmp($vip['vip_rank'], 'Bronze Buyer')==0 )







					 {







						 $Bronze_Buyer=$vip['standard_points'];







					 }







					 if ( strcasecmp($vip['vip_rank'], 'Silver Buyer')==0 )







					 {







						 $Silver_Buyer=$vip['standard_points'];







					 }







					 if ( strcasecmp($vip['vip_rank'], 'Gold Buyer')==0 )







					 {







						 $Gold_Buyer=$vip['standard_points'];







					 }







					 if ( strcasecmp($vip['vip_rank'], 'Platinum Buyer')==0 )







					 {







						 $Platinum_Buyer=$vip['standard_points'];







					 }







					 if ( strcasecmp($vip['vip_rank'], 'Diamond Buyer')==0 )







					 {







						 $Diamond_Buyer=$vip['standard_points'];







					 }















				 }







				 $customer_id=(int)$customer->getId();







                 $customer_row=$database->getRow("select telephone, current_points, status, country_id from customer where customer_id = '".$customer_id."' ");















                 if($customer_row['current_points']<$Bronze_Buyer)







				 {







					 $database->query("update customer set vip_rank ='General Buyer' where customer_id = '".$customer_id."' ");







                 }







				 elseif($customer_row['current_points']<=$Silver_Buyer&&$customer_row['current_points']>=$Bronze_Buyer)







                 {







					 $database->query("update customer set vip_rank ='Bronze Buyer' where customer_id = '".$customer_id."' ");







                 }







				 elseif($customer_row['current_points']<=$Gold_Buyer&&$customer_row['current_points']>=$Silver_Buyer)







                 {







					 $database->query("update customer set vip_rank ='Silver Buyer' where customer_id = '".$customer_id."' ");







			     }







				 elseif($customer_row['current_points']<=$Platinum_Buyer&&$customer_row['current_points']>=$Gold_Buyer)







			      {







					  $database->query("update customer set vip_rank ='Gold Buyer' where customer_id = '".$customer_id."' ");







	              }







				 elseif($customer_row['current_points']<=$Diamond_Buyer&&$customer_row['current_points']>=$Platinum_Buyer)







				  {







						$database->query("update customer set vip_rank ='Platinum Buyer' where customer_id = '".$customer_id."' ");







				  }







                 else







				 {







					  $database->query("update customer set vip_rank ='Diamond Buyer' where customer_id = '".$customer_id."' ");







				 }



					$customer_info = $database->getRow("select ct.currency_code from customer as cs left join country as ct on (cs.country_id=ct.country_id) left join currency as cy on (ct.currency_code=cy.code) where cs.customer_id = '".$customer_id."'");



				if($customer_info){



					$response->redirect("index.php?controller=currency&currency='".$customer_info['currency_code']."'&action=https://www.beststoreitems.com/index.php?controller=cart");



                }



			}



		}







		$view = $this->locator->create('template');







		$invite_code=isset($_REQUEST['invite_code'])?$_REQUEST['invite_code']:"";



		



		$view->set('invite_code', $invite_code);



		



		$view->set('heading_title', $language->get('heading_title'));







		$view->set('text_new_customer', $language->get('text_new_customer'));







		$view->set('text_i_am_new_customer', $language->get('text_i_am_new_customer'));







		$view->set('text_returning_customer', $language->get('text_returning_customer'));







		$view->set('text_i_am_returning_customer', $language->get('text_i_am_returning_customer'));







		$view->set('text_create_account', $language->get('text_create_account'));







		$view->set('text_forgotten_password', $language->get('text_forgotten_password'));







		$view->set('text_forgotten_password', $language->get('text_forgotten_password'));







		$view->set('entry_email', $language->get('entry_email_address'));







		$view->set('entry_password', $language->get('entry_password'));







		$view->set('button_continue', $language->get('button_continue'));







		$view->set('button_login', $language->get('button_login'));







		$view->set('error', @$this->error['message']);







		$view->set('action', $url->ssl('account_login'));







		$view->set('message', $session->get('message'));







		$session->delete('message');







		$view->set('continue', $url->ssl('account_create'));







		$view->set('forgotten', $url->ssl('account_forgotten'));







		$view->set('countries', $database->cache('country', "select * from country where allow_ship=1 and icon!='' order by name"));







		$template->set('content', $view->fetch('content/account_login.tpl'));







		$template->set($module->fetchPublic());







		$response->set($template->fetch('layout.tpl'));







	}







/*****************************************************************************************************************************************************/







function verifyAccount($email,$password) {











$email=trim($email); 



        $email=str_replace(' ','',$email); 







		$password=str_replace(' ','',$password);







	    $database =& $this->locator->get('database');







		$customer =& $this->locator->get('customer');







		$response =& $this->locator->get('response');







	  $check_customer = $database->getRow("select customer_id, firstname, lastname, password, order_count, current_points, country_id, status from `customer` where email = '".$email."'");







  //      print_r ($check_customer);







  				 if(!$check_customer)



                   {



	                    echo '<script type="text/javascript">';







		    			echo 'alert("Login failure, your email address has not been signed up yet on our site!");';







		    			echo '</script>';







                        return FALSE;







                   }







				   if($check_customer['status']==0)



				   {







	echo "<script language=\"JavaScript\">alert(\"your account is waiting for our verification, please be patient to wait, we will send an email to you once it completed.\");window.history.back(-1);</script>";







                   return FALSE;







				   }







		        $input_password=$password;







				$first_name=str_replace(' ','',$check_customer["firstname"]);







				$last_name=str_replace(' ','',$check_customer["lastname"]);







				$full_name=str_replace(' ','',$first_name.$last_name);







//			    echo "first_name=".$first_name."<br />"; echo "last_name=".$last_name."<br />"; echo "full_name=".$full_name."<br />";







//				echo "input_password=".$input_password."<br />";







                $name_compare_result=strcasecmp($first_name, $input_password)*strcasecmp($last_name, $input_password)*strcasecmp($full_name, $input_password);







//				echo "Name_compare_result=".$name_compare_result."<br />";







				$md5_input_password=md5($input_password);







				$database_password=$check_customer['password'];







//				echo "md5_input_password=".$md5_input_password."<br />"; echo "database_password=".$database_password."<br />";







				$password_compare_result=strcasecmp($md5_input_password, $database_password);







//		        echo "Password_compare_result=".$password_compare_result."<br />";







				$last_compare_result=$name_compare_result*$password_compare_result;







//				echo "Last_compare_result=".$last_compare_result."<br />";















				   if($last_compare_result!=0) {







			        	echo '<script type="text/javascript">';







		    			echo 'alert("The password that you entered is incorrect. Please try again (passwords are case sensitive). If you forget password, pls input first name or last name as password.");';







		    			echo 'history.back()';







		    			echo '</script>';







                        return FALSE;







			      }







			  // 记�????��??�????��??







				if ($name_compare_result==0){







		 		 	$database->query("update customer set login_by_name = (login_by_name+1) where customer_id = '".$check_customer['customer_id']."' ");







				 }







			  // 记�????????�??次�?��????��??







				if ($last_compare_result==0){







					date_default_timezone_set('Asia/Hong_Kong');







		 		 	$database->query("update customer set lastlogin_time = '".date('Y-m-d H:i:s',time())."' where customer_id = '".$check_customer['customer_id']."' ");







				 }















				if (!$customer->login($email, $database_password)) {







					echo "<script language=\"JavaScript\">alert(\"Load failure\");</script>";







					$this->error['message'] = $language->get('error_login');







				}















				if (!$this->error) {







					return TRUE;







				} else {







						return FALSE;







				}







	}







/***********************************************************************************************************************/







}







?>







