<?php

//============
// Attributes
//============

if (isset($_GET['oldal'])) 
  {
    $oldal=$_GET['oldal'];
	//egész PHP ezen belül van

if (isset($_GET['ifis'])) 
  { $ifis=$_GET['ifis']; }
else
  { $ifis=0; }

if (isset($_GET['nev'])) 
  { $user=$_GET['nev']; }
else
  { $user=''; }


//==============
// IP kiolvasás
//==============

  if (!empty($_SERVER['HTTP_CLIENT_IP']))
  //check ip from share internet
  {
    $ip=$_SERVER['HTTP_CLIENT_IP'];
  }
  elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
  //to check ip is pass from proxy
  {
    $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
  }
  else
  {
    $ip=$_SERVER['REMOTE_ADDR'];
  }


//===============
// Pontatlan-idő
//===============

$timeout=60;
$localtime = localtime();
$ev = end($localtime);
$ev = prev($localtime);
$ev = prev($localtime);
$ev = prev($localtime)+1900;
$honap = prev($localtime)+1;
$nap = prev($localtime);
$ora = prev($localtime);
$perc = prev($localtime);
$mperc = prev($localtime);
$idopontmost = sprintf("%04d", $ev).'-'.sprintf("%02d", $honap).'-'.sprintf("%02d", $nap).' '.sprintf("%02d", $ora).':'.sprintf("%02d", $perc).':'.sprintf("%02d", $mperc);

if ($perc>=$timeout) {
    $perc=$perc-$timeout; }
else {
    $perc=($perc-$timeout+60)%60; 
    if ($ora>=1) {
      $ora=$ora-1;  }
    elseif ($nap>1) {
     $ora=23;
     $nap=$nap-1;   }
    else {
     $ora=0;
     $perc=0;
     $mperc=0;
     }
  }
$idopontvolt = sprintf("%04d", $ev).'-'.sprintf("%02d", $honap).'-'.sprintf("%02d", $nap).' '.sprintf("%02d", $ora).':'.sprintf("%02d", $perc).':'.sprintf("%02d", $mperc);

/*
//================
// SQL megnyitása
//================

$sql = mysql_connect("localhost", "kereszturifi", "C4ufedr2") or die('Could not connect: ' . mysql_error());
mysql_select_db('kereszturifi') or die('Could not select database');


//============
// Lekérdezés
//============

$query = "SELECT * FROM latogatok WHERE IP='$ip' AND aktiv>='$idopontvolt'";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

$talalat=0;
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {  
    $elso=1;
    foreach ($line as $col_value) {
        if ($elso==1&&$talalat<$col_value) {$talalat=$col_value;}
        $elso=0; 
    }
}

mysql_free_result($result);


//===================
// Ha nem volt aktív
//===================

if ($talalat==0) {
  $query = "INSERT INTO latogatok (IP,aktiv,user,ifis,$oldal) VALUES ('$ip','$idopontmost','$user',$ifis,1)"; 
  mysql_query($query) or die('Query failed: ' . mysql_error());

	// Számláló
	if ($oldal=='kezdolap')	
	{
	  $query = "SELECT ID FROM latogatok ORDER BY ID DESC LIMIT 1";
	  $result = mysql_query($query) or die('Query failed: ' . mysql_error());
	
	  while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {  
	    foreach ($line as $col_value) {
		$total = $col_value + 2500;
	    }
	  }
	  mysql_free_result($result);
	}
}


//===============
// Ha aktív volt
//===============

else {
  $query = "UPDATE latogatok SET aktiv='$idopontmost', $oldal=$oldal+1, ifis=$ifis, user='$user' WHERE ID=$talalat"; 
  mysql_query($query) or die('Query failed: ' . mysql_error());  
  $total = $talalat + 2500;
}


//==================
// Kapcsolat bontás
//==================

mysql_close($sql);
*/

//===================
// Tovább a honlapra
//===================

if ($oldal=='kezdolap')		{Header("Location: http://kereszturifi.lutheran.hu/kezdolap.php?szamlalo=$total");}
elseif ($oldal=='rolunk')	{Header('Location: http://kereszturifi.lutheran.hu/rolunk.html');}
elseif ($oldal=='alkalmaink')	{Header('Location: http://kereszturifi.lutheran.hu/alkalmaink.html');}
elseif ($oldal=='idevarunk')	{Header('Location: http://kereszturifi.lutheran.hu/idevarunk.html');}
elseif ($oldal=='aktualis')	{Header('Location: http://kereszturifi.lutheran.hu/aktualis.html');}
elseif ($oldal=='kikicsoda')	{Header('Location: http://kereszturifi.lutheran.hu/kikicsoda.html');}
elseif ($oldal=='multidezo')	{Header('Location: http://kereszturifi.lutheran.hu/multidezo.php');}
elseif ($oldal=='lelekzet')	{Header('Location: http://kereszturifi.lutheran.hu/lelekzet.php');}
elseif ($oldal=='forum')	{Header('Location: http://kereszturifi.lutheran.hu/forum/index.php');}
elseif ($oldal=='otlettar')	{Header('Location: http://kereszturifi.lutheran.hu/otlettar.html');}
elseif ($oldal=='linkek')	{Header('Location: http://kereszturifi.lutheran.hu/linkek.html');}

  }
else
  {
    echo ('Hiba! Nincs megadva a link!');
  }
?> 