<?php
session_start();
ini_set("display_errors", 0);
error_reporting(0);

Header('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0', true);
Header('Expires', 'Sat, 26 Jul 1997 05:00:00 GMT', true);
Header('Last-Modified', gmdate("D, d M Y H:i:s")." GMT", true);
Header('Pragma', 'no-cache', true);

if (file_exists("install.php")) {
  Header("Location: install.php");
  echo '<script type="text/javascript">location.replace("install.php")</script>';
  exit();
};

if (!file_exists("config.php")) {
  Header("Location: error.php?error=7");
  echo '<script type="text/javascript">location.replace("error.php?error=7")</script>';
  exit();
};

include "config.php";

if ((isset($_SESSION['username']) && isset($_SESSION['userpassword'])) or (isset($_COOKIE["jtcms_jmeno"]) && isset($_COOKIE["jtcms_heslo"]))) {
  if (isset($_SESSION['username']) && isset($_SESSION['userpassword'])) {
    $jmeno = mysql_real_escape_string($_SESSION['username']);
    $heslo = mysql_real_escape_string($_SESSION['userpassword']);
  } elseif (isset($_COOKIE["jtcms_jmeno"]) && isset($_COOKIE["jtcms_heslo"])) {
    $jmeno = mysql_real_escape_string($_COOKIE["jtcms_jmeno"]);
    $heslo = mysql_real_escape_string($_COOKIE["jtcms_heslo"]);
  }; 
  
  $query = mysql_query("SELECT id FROM jtcms_uzivatele WHERE jmeno='".$jmeno."' and heslo='".$heslo."'");
  $radku = mysql_num_rows($query);
  
  if ($radku == 1) {
    $result = mysql_fetch_array($query);
    $_SESSION['userid'] = $result['id'];
    Header("Location: login.php");
    echo '<script type="text/javascript">location.replace("login.php")</script>';
    exit();
  };
};

$theme = mysql_query("SELECT hodnota FROM jtcms_nastaveni WHERE typ='vzhled' LIMIT 1");
$theme = mysql_fetch_array($theme);
$theme = $theme['hodnota'];

  $query_lang = mysql_query("SELECT hodnota, hodnota2 FROM jtcms_nastaveni WHERE typ='jazyk' LIMIT 1");
  $query_lang = mysql_fetch_array($query_lang);
  $set_lang = $query_lang['hodnota'];
  $set_lang2 = $query_lang['hodnota2'];

include "stuff/lang/".$set_lang2.".php";
include "stuff/func/general.php";

$meta2 = '<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>';

include_head_short($lang['index_title']);
?> 
   <div id="login_form">
   <a href="../" class="zpet"><?php echo $lang['index_back_to_web']; ?></a>
   <span class="administrace"><?php echo $lang['index_headline']; ?></span><br />
   <form method="post" action="index.php" autocomplete="off">
   <br />
   <label for="login_jmeno"><?php echo $lang['index_input_1']; ?></label>
   <input type="text" id="login_jmeno" class="input" name="jmeno" placeholder="<?php echo $lang['index_input_1_pl']; ?>" /><br />
   <label for="login_heslo"><?php echo $lang['index_input_2']; ?></label>
   <input type="password" id="login_heslo" class="input" name="heslo" placeholder="<?php echo $lang['index_input_2_pl']; ?>" /><br />
   <br />
   <input type="checkbox" name="prihlasen" id="login_prihlasen" value="JO" /> <label for="login_prihlasen" class="nlabel"><?php echo $lang['index_remember_me']; ?></label><br />
   <br />
   <input type="submit" name="submit" class="submit" value="<?php echo $lang['index_submit']; ?>" />
   <br /><br />
   </form>
   </div>
<?php
if (!empty($_POST)) {

  $jmeno = htmlspecialchars($_POST['jmeno']);
  $heslo = htmlspecialchars($_POST['heslo']);
  $prihlasen = $_POST['prihlasen'];
  
  if (!empty($jmeno) && !empty($heslo)) {
     
     $jmeno = mysql_real_escape_string($jmeno);
     $heslo = sha1($heslo);
     $heslo = mysql_real_escape_string($heslo);
     
     $query = mysql_query("SELECT * FROM jtcms_uzivatele WHERE jmeno='".$jmeno."' and heslo='".$heslo."'");
     $radku = mysql_num_rows($query);
     $result = mysql_fetch_array($query);
     $id = $result['id'];
     $ip = $_SERVER['REMOTE_ADDR'];
     
     if ($radku == 1) {
       $_SESSION['userid'] = $id;
       $_SESSION['username'] = $result['jmeno'];
       $_SESSION['userpassword'] = $result['heslo'];
       mysql_query("UPDATE jtcms_uzivatele SET posledni_ip='".$ip."', posledni_pr=NOW() WHERE id='".$id."' LIMIT 1"); 
     
        if ($prihlasen == "JO") {
          Header("Location: cookie.php?akce=set"); 
        } else {
          Header("Location: login.php"); 
        }; 
     
     } else {
     echo '<br />';
     echo '<div id="login_error">';
     echo '<div class="chyba">'.$lang['index_error'].'</div>';
     echo '<p>'.$lang['index_error_p'].'</p>';
     echo '</div>';
echo '<script type="text/javascript">
  $(document).ready(function() {
    
$(function () {
      $("form").effect("shake", { times:3 }, 150);
});

  });
</script>';
     };

   };
  };
  
include_end_short();
?>   
