<?php require_once('Connections/clinic.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;
}
}


?>
<?php
// *** Validate request to login to this site.
if (!isset($_SESSION)) {
  session_start();
}

$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($_GET['accesscheck'])) {
  $_SESSION['PrevUrl'] = $_GET['accesscheck'];
}

if (isset($_POST['login'])) {
  $loginUsername=$_POST['login'];
  $password=md5($_POST['login-password']);
  $MM_fldUserAuthorization = "";
  $MM_redirectLoginSuccess = "index_CM.php?token=".$_SESSION['form_token'];
  $MM_redirectLoginFailed = "logout.php";
  $MM_redirecttoReferrer = false;
  mysql_select_db($database_clinic, $clinic);
  
  $LoginRS__query=sprintf("SELECT username, password FROM phpc_users WHERE username=%s AND password=%s",
    GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); 
   
  $LoginRS = mysql_query($LoginRS__query, $clinic) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);
  if ($loginFoundUser) {
     $loginStrGroup = "";
    
	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'];	
    }
	
	mysql_select_db($database_clinic, $clinic);
$query_user = "SELECT uid FROM phpc_users WHERE username = '$loginUsername'";
$user = mysql_query($query_user, $clinic) or die(mysql_error());
$row_user = mysql_fetch_assoc($user);
$totalRows_user = mysql_num_rows($user);

mysql_select_db($database_clinic, $clinic);
$query_logins = "SELECT * FROM phpc_logins WHERE uid = '$row_user[uid]' and logout = '0000-00-00 00:00:00'";
$logins = mysql_query($query_logins, $clinic) ;//or die(mysql_error());
$row_logins = mysql_fetch_assoc($logins);
$totalRows_logins = mysql_num_rows($logins);
if($totalRows_logins>0){
	$date_now = date('Y-m-d H:i:s');
	$insertSQL = sprintf("UPDATE phpc_logins SET  logout='$date_now' WHERE token = '$row_logins[token]'");
  mysql_select_db($database_clinic, $clinic);
  $Result1 = mysql_query($insertSQL, $clinic) or die(mysql_error());
	
	}

   $ip=$_SERVER['REMOTE_ADDR'];
	
	$insertSQL = sprintf("INSERT INTO phpc_logins (token,uid,ip) VALUES (%s,'$row_user[uid]','$ip')",
                       GetSQLValueString($_POST['token'], "text"));

  mysql_select_db($database_clinic, $clinic);
  $Result1 = mysql_query($insertSQL, $clinic) or die(mysql_error());
	
    header("Location: " . $MM_redirectLoginSuccess );
  }
  else {
    header("Location: ". $MM_redirectLoginFailed );
  }
}
?>
<?php

/*** begin our session ***/
session_start();

/*** set a form token ***/
$form_token = md5( uniqid('auth', true) );

/*** set the session form token ***/
$_SESSION['form_token'] = $form_token;
?>
<?php 
require_once("Connections/clinic.php");

include 'inc/config.php'; // Configuration php file ?>
<!DOCTYPE html>
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
    <head>
        <meta charset="utf-8">

        <title><?php echo $clinicmanager_label ?></title>

        <meta name="description" content="<?php echo $template['description'] ?>">
        <meta name="author" content="<?php echo $template['author'] ?>">
        <meta name="robots" content="noindex, nofollow">

        <meta name="viewport" content="width=device-width,initial-scale=1">

        <!-- Icons -->
        <!-- The following icons can be replaced with your own, they are used by desktop and mobile browsers -->
        <link rel="shortcut icon" href="img/custom/logo.png">
        <link rel="apple-touch-icon" href="img/custom/logo.png" sizes="57x57">
        <link rel="apple-touch-icon" href="img/custom/logo.png" sizes="72x72">
        <link rel="apple-touch-icon" href="img/custom/logo.png" sizes="76x76">
        <link rel="apple-touch-icon" href="img/custom/logo.png" sizes="114x114">
        <link rel="apple-touch-icon" href="img/custom/logo.png" sizes="120x120">
        <link rel="apple-touch-icon" href="img/custom/logo.png" sizes="144x144">
        <link rel="apple-touch-icon" href="img/custom/logo.png" sizes="152x152">
        <!-- END Icons -->

        <!-- Stylesheets -->
        <!-- Bootstrap is included in its original form, unaltered -->
        <link rel="stylesheet" href="css/bootstrap.css">

        <!-- Related styles of various javascript plugins -->
        <link rel="stylesheet" href="css/plugins.css">

        <!-- The main stylesheet of this template. All Bootstrap overwrites are defined in here -->
        <link rel="stylesheet" href="css/main.css">
        <!-- END Stylesheets -->

        <!-- Modernizr (browser feature detection library) & Respond.js (Enable responsive CSS code on browsers that don't support it, eg IE8) -->
        <script src="js/vendor/modernizr-2.7.1-respond-1.4.2.min.js"></script>
    </head>

    <body class="login">
        <!-- Login Container -->
        <div id="login-container">
            <div id="login-logo">
                <a href="">
                    <img src="img/custom/logo.png" alt="logo" width="100">
                </a>
      </div>

            <!-- Login Buttons -->
            <div id="login-buttons">
                <h5 class="page-header-sub">Login</h5>
           
                <button id="login-btn-email" class="btn btn-default"><i class="gi gi-play"></i></button>
            </div>
            <!-- END Login Buttons -->

            <!-- Login Form -->
            <form id="login-form" action="<?php echo $loginFormAction; ?>" method="POST" class="form-horizontal">
                <div class="form-group">
                    <a href="javascript:void(0)" class="login-back"><i class="fa fa-arrow-left"></i></a>
                </div>
                <div class="form-group">
                    <div class="col-xs-12">
                        <div class="input-group">
                            <input type="text" id="login" name="login" placeholder="Cuenta.." class="form-control">
                            <span class="input-group-addon"><i class="gi gi-user"></i></span>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-xs-12">
                        <div class="input-group">
                            <input type="password" id="login-password" name="login-password" placeholder="Password.." class="form-control">
                            <span class="input-group-addon"><i class="fa fa-asterisk fa-fw"></i></span>
                        </div>
                    </div>
                </div>
                <div class="clearfix">
                    <div class="btn-group btn-group-sm pull-right">
                        <input type="hidden" name="token" id="token" value="<?php echo $form_token?>" />
                      <button type="submit" class="btn btn-success"><i class="fa fa-arrow-right"></i> Login</button>
                    </div>
                  	
                </div>
          </form>
            <!-- END Login Form -->
        </div>
        <!-- END Login Container -->

        <!-- Include Jquery library from Google's CDN but if something goes wrong get Jquery from local file (Remove 'http:' if you have SSL) -->
        <script src="js/vendor/jquery-1.11.0.min.js"></script>
        <script>!window.jQuery && document.write(unescape('%3Cscript src="js/vendor/jquery-1.11.0.min.js"%3E%3C/script%3E'));</script>

        <!-- Bootstrap.js -->
        <script src="js/vendor/bootstrap.min.js"></script>

        <!-- Jquery plugins and custom javascript code -->
        <script src="js/plugins.js"></script>
        <script src="js/main.js"></script>

        <!-- Javascript code only for this page -->
        <script>
            $(function (){
                var loginButtons = $('#login-buttons');
                var loginForm = $('#login-form');

                // Reveal login form
                $('#login-btn-email').click(function(){
                    loginButtons.slideUp(600);
                    loginForm.slideDown(450);
                });

                // Hide login form
                $('.login-back').click(function(){
                    loginForm.slideUp(450);
                    loginButtons.slideDown(600);
                });
            });
        </script>
    </body>
</html>    <?php
mysql_free_result($user);

mysql_free_result($logins);
?>
