<?php 

if (!isset($_SESSION)) {
  session_start();
}
 ?>
<?php require_once('../Connections/telmanik_press.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_telmanik_press, $telmanik_press);
$query_userlogin = "SELECT * FROM members";
$userlogin = mysql_query($query_userlogin, $telmanik_press) or die(mysql_error());
$row_userlogin = mysql_fetch_assoc($userlogin);
$totalRows_userlogin = mysql_num_rows($userlogin);
?>
<?php
// *** Validate request to login to this site.


$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($_GET['accesscheck'])) {
  $_SESSION['PrevUrl'] = $_GET['accesscheck'];
}

if (isset($_POST['myusername'])) {
  $loginUsername=$_POST['myusername'];
  $password=$_POST['mypassword'];
  $MM_fldUserAuthorization = "level";
  $MM_redirectLoginSuccess = "home.php?lang=ru";
  $MM_redirectLoginFailed = "login_failed.php";
  $MM_redirecttoReferrer = false;
  mysql_select_db($database_telmanik_press, $telmanik_press);
  	
  $LoginRS__query=sprintf("SELECT username, password, level FROM members WHERE username=%s AND password=%s",
  GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); 
   
  $LoginRS = mysql_query($LoginRS__query, $telmanik_press) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);
  if ($loginFoundUser) {
    
    $loginStrGroup  = mysql_result($LoginRS,0,'level');
    
	if (PHP_VERSION >= 5.1) {session_regenerate_id(true);} else {session_regenerate_id();}
    //declare two session variables and assign them
    $_SESSION['MM_Username'] = $loginUsername;
    $_SESSION['MM_UserGroup'] = $loginStrGroup;	      

    if (isset($_SESSION['PrevUrl']) && false) {
      $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];	
    }
    header("Location: " . $MM_redirectLoginSuccess );
  }
  else {
    header("Location: ". $MM_redirectLoginFailed );
  }
}

?>
<?php
require_once('language/class.translation.php');

if(isset($_GET['lang']))
	$translate = new Translator($_GET['lang']);
else
	$translate = new Translator('en');
?>

<?php $title="Login";?>
<?php require("assets/wrapper/login_header.php"); ?>







<table width="300" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#CCCCCC">
  <tr>
  <form ACTION="<?php echo $loginFormAction; ?>" name="form1" method="POST">
  <td>
  <table width="100%" border="0" cellpadding="3" cellspacing="0" bgcolor="#FFFFFF">
  <tr>
  <td colspan="3"><strong><?php echo $translate->__('Login');?></strong></td>
  </tr>
  <tr>
  <td width="78"><?php echo $translate->__('Username');?></td>
  <td width="6">:</td>
  <td width="294"><input name="myusername" type="text" id="myusername"></td>
  </tr>
  <tr>
  <td><?php echo $translate->__('Password')?></td>
  <td>:</td>
  <td><input name="mypassword" type="password" id="mypassword"></td>
  </tr>
  <tr>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td><input type="submit" name="Submit" value="<?php echo $translate->__('Login');?>"></td>
  </tr>
  </table>
  </td>
  </form>
  </tr>
</table><BR>
<a href="forgot_pass.php?lang=ru"><?php echo $translate->__('Forgot Password?');?></a><BR>
</div>
<?php require("assets/wrapper/login_footer.php"); ?>
<?php
mysql_free_result($userlogin);
?>
