<?php

  require_once 'locale.php';
  require_once 'dbconst.php';
  require_once 'cmn_frame.php';

function _wu($text) {
  return iconv("cp1250", "utf-8", $text);
}

function save_user() {
  $firstname = isset($_POST['_firstname']) ? _wu($_POST['_firstname']) : 'NULL';
  $name = isset($_POST['_name']) ? _wu($_POST['_name']) : 'NULL';
  $organization = isset($_POST['_organization']) ? _wu($_POST['_organization']) : 'NULL';
  $country = isset($_POST['_country']) ? _wu($_POST['_country']) : 'NULL';
  $email = isset($_POST['_email']) ? _wu($_POST['_email']) : 'NULL';
  $ip = $_SERVER['REMOTE_ADDR'];
  $filename = _wu($_POST['_filename']);
  $now = date("Y-m-d H:i:s");

  $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWD)
          or die (DB_ERROR.': '.mysql_error().' ('.mysql_errno().')');

  mysql_select_db (DB_NAME)
  or die (DB_ERROR.': '.mysql_error().' ('.mysql_errno().')');

  /* Ustawiamy kodowanie */
  mysql_query("SET NAMES '".DB_CHARSET."'")
  or die (DB_ERROR.': '.mysql_error().' ('.mysql_errno().')');

  $sql = "INSERT INTO `utt_downloaders`(`firstname`, `name`, ".
  	   	 		 "`organization`, `country`, `email`, ".
  				 "`ip`, `filename`, `date`) ".
  	     "VALUES('$firstname', '$name', ".
				 "'$organization', '$country', '$email', ".
				 "'$ip', '$filename', '$now')";
//echo "Execute query: $sql<br />\n";
  mysql_query($sql)
  or die(DB_ERROR.': '.mysql_error().' ('.mysql_errno().')');

  mysql_close($link);
}


// main part
//  print_r($_SERVER);
//  print_r($_POST);
  if(isset($_POST['_filename'])) {
    save_user();
    $head = '<title>UAM Text Tools - '.DOWNLOAD_THANKS_PAGE_TITLE."</title>\n";
    $head .= "<meta HTTP-EQUIV=\"REFRESH\" content=\"5; url=files/".$_POST['_filename']."\">\n";
	$content = DOWNLOAD_THANKS_CAPTION."<br />\n".
    		   "<br />".DOWNLOAD_THANKS_EXTRA_LINK.
			   " <a href=\"files/".$_POST['_filename']."\">".$_POST['_filename']."</a>.<br />\n".
			   "<a href=index.php>".DOWNLOAD_THANKS_MAINPAGE_LINK."</a>\n";

     getMainPage($head, $content);
  }
  else {
    header('Location: http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']).'/down.php');
  }

?>
