<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
// +----------------------------------------------------------------------+
// | PHP version 4/5                                                      |
// +----------------------------------------------------------------------+
// | This source file is a part of iScripts Multicart                     |
// +----------------------------------------------------------------------+
// | Authors: Programmer<programmer@armia.com>        		              |
// +----------------------------------------------------------------------+
// | Copyrights Armia Systems, Inc and iScripts.com � 2005                |
// | All rights reserved                                                  |
// +----------------------------------------------------------------------+
// | This script may not be distributed, sold, given away for free to     |
// | third party, or used as a part of any internet services such as      |
// | webdesign etc.                                                       |
// +----------------------------------------------------------------------+
?>
<?php
include "../includes/config.php";
include "../includes/session.php";
include "../includes/functions.php";

if (get_magic_quotes_gpc()) {
    $_POST = array_map('stripslashes_deep', $_POST);
    $_GET = array_map('stripslashes_deep', $_GET);
    $_COOKIE = array_map('stripslashes_deep', $_COOKIE); 
} 

if(isset( $_SESSION["sess_adminname"]) and $_SESSION["sess_adminname"]!= ""){
	header("Location:adminmain.php");
	exit;
}

//Remember Me
$remembered = false;

$json = isset($_COOKIE['mcart_admin']) ? stripslashes($_COOKIE['mcart_admin']) : '';

if($json != '')
{
    $cookieArr = json_decode($json);
    if($cookieArr->usr != '' && $cookieArr->pwd != '')
    {
        $txtUserName = base64_decode($cookieArr->usr);
        $txtPassword = base64_decode($cookieArr->pwd);
        $remembered = true;
    }
}

if(isset($_POST["txtUserName"]))
{

    $txtUserName = $_POST["txtUserName"];
$txtPassword = $_POST["txtPassword"];
/*----------Multicart 2.0 Upgradation starts-----------*/

	
/*---------Checking the password----------------*/	
	/*		
	$select_password = "SELECT admin_password FROM ".$tableprefix."settings WHERE admin_name = ".GetSQLValueString($txtUserName,"text");
	$result_password = mysql_query($select_password) or die(mysql_error());
				
	if(mysql_num_rows($result_password) > 0)
	{
	
		$password_row = mysql_fetch_assoc($result_password);
						
		if(GetSQLValueString($password_row['admin_password'],"text") != GetSQLValueString(md5($txtPassword),"text"))
		{
		
			$message = "<img src='../images/error.gif' /> &nbsp;The password you provided is incorrect. Please check it and try again.";
			$password_flag = 1;
			
		}
			
	}
	

	
		$select_username = "SELECT admin_name FROM ".$tableprefix."settings WHERE admin_password = ".GetSQLValueString(md5($txtPassword),"text");
		$result_username = mysql_query($select_username) or die(mysql_error());			
		if(mysql_num_rows($result_username) > 0)
		{
		
			while($username_row = mysql_fetch_array($result_username))
			{
				if(GetSQLValueString($username_row['admin_name'],"text") != GetSQLValueString($txtUserName,"text"))
				{
				
					$message = "<img src='../images/error.gif' /> &nbsp;The username you provided is incorrect. Please check it and try again";
					$username_flag = 1;
				
				}
				else
				{
												
					$admin_name = $username_row['admin_name'];
				
				}
			}	
		
		
		}
	
		if($username_flag != 1 && $password_flag != 1)
		{*/	
		
			$sql=" SELECT nsettings_id,admin_name FROM ".$tableprefix."settings
				   WHERE admin_name = '".mysql_real_escape_string($txtUserName)."'
				    AND admin_password = '".mysql_real_escape_string(md5($txtPassword)) . "'";
			$result=mysql_query($sql) or die(mysql_error());
			if(mysql_num_rows($result) > 0 )
			{		
				$row = mysql_fetch_array($result);
				$_SESSION["sess_adminname"] = $row["admin_name"];
                                
                                if(isset($_POST['rememberme']))
                                {
                                    $value = array('usr' => base64_encode($txtUserName), 'pwd' => base64_encode($txtPassword));
                                    setcookie("mcart_admin", json_encode($value), time()+3600*24*30);
                                }
                                else
                                {
                                    setcookie("mcart_admin", json_encode($value), time() - 3600);
                                }
				header("Location:adminmain.php");
				
/*---------------Multicart 2.0 Upgradation------------------*/				
				
/*-------function call to manage the different sessions--------*/

				manageSession(1);
				
/*---------------Multicart 2.0 Upgradation------------------*/								
				
				exit;
			}
			else
			{
				$message = "<img src='../images/error.gif' /> &nbsp;The username or password you provided are incorrect. Please check them and try again.";
			}
		
		//}
	
	/*$sql = " SELECT admin_name,admin_password FROM " . $tableprefix . "settings ";
    $result = mysql_query($sql) or die(mysql_error());
    if (mysql_num_rows($result) > 0) {
        $row = mysql_fetch_array($result);
        $adminname = $row["admin_name"];
        $adminpassword = $row["admin_password"];
        if (($adminname == $txtUserName) AND ($adminpassword == addslashes(md5($txtPassword)))) {*/
		
/*----------Multicart 2.0 Upgradation starts-----------*/		
		
            /*if($username_flag == 0 && $password_flag == 0)
			{
			
				$_SESSION["sess_adminname"] = $row["admin_name"];
				header("Location:adminmain.php");
				exit;
			}*/
			
/*----------Multicart 2.0 Upgradation starts-----------*/			
			
        /*} else {
            $message = "<img src='../images/error.gif' /> &nbsp;Invalid Login! Please retry.";
        } 
    } else {
        $message = "<img src='../images/error.gif' /> &nbsp;Error! Please retry.";
    }*/
} 
include "../includes/htmltop.php";
?>
<script>
	function checkAdminLogin()
	{
	
	  var form_name = new Array('txtUserName','txtPassword');		
	  var display_name = new Array('<?php echo UNAME; ?>','<?php echo PASSWORD; ?>');
	  var count = 2;	
	
	  if(check_login('frmLoginAdmin',form_name,display_name,count))	
	  {
	  
		
			   
	  }
	  else
	  {
	  
			return false;
	  
	  }
	   
	}
</script>

		<body>
		<center>
		
		<div align="left" id="layout">
				
<?php

	include_once("../includes/topheader_admin.php")

?>				
				
				<div align="left" class="main_body">
				<!--main body start -->
		
						<div align="center" class="incorrect_text">
								<?php echo $message?> 
						</div>
				
						<div align="left" class="login_box">
						<!--login area start -->		
						
								<div align="left" class="login_area">
								  <div align="left" class="login_text"><?php echo LOGIN; ?></div>
								  <table width="100%" cellpadding="0" cellspacing="0" class="login_txtbx" border="0">
								  <form name="frmLoginAdmin" action="<?php echo $_SERVER["PHP_SELF"]?>" method="POST" onSubmit="return checkAdminLogin();">
                                    <tr>
                                      <td width="34%" align="left" valign="middle"><?php echo UNAME; ?>&nbsp;</td>
                                      <td width="66%" align="left" valign="middle"><input name="txtUserName" type="text" class="textarea" value="<?php echo $txtUserName ; ?>"  tabindex="1"/></td>
                                    </tr>
                                    <tr>
                                      <td align="left" valign="middle"><?php echo PASSWORD;?>&nbsp;</td>
                                      <td align="left" valign="middle"><input name="txtPassword" type="password" value="<?php echo $txtPassword ; ?>" class="textarea" tabindex="2"/></td>
                                    </tr>
                                    <tr>
                                      <td>&nbsp;</td>
                                      <td><input style="width: 28px;" type="checkbox" <?php echo $remembered === true ? 'checked="checked"' : '' ?> name="rememberme"><?php echo REMEMBER_ME; ?></td>
                                    </tr>
                                    <tr>
                                      <td align="left" valign="middle">&nbsp;</td>
                                      <td align="left" valign="middle">
                                          <input name="btnLogin" type="submit" class="login_bttn" value=" <?php echo LOGIN;?>" tabindex="3"/>
                                      <a href="forgotpassword.php" title="forgot password"><?php echo FORGOT_PWD;?></a>
									 </td>
                                    </tr>
									</form>
                                  </table>
						  </div>
								
						<!--login area end-->		
				  </div>
						
						
						
						
				
				        <!--main body end -->
				</div>

<?php
include "../includes/footer.php";
?>





