<?
/**
 * getkey.php
 * This file should be called from within the form-tags in the form which
 * calls the formHandler script. It generates a line with a hidden key.
 * This key is checked by the formHandler script. If it doesn't match then
 * the formHandler script exits.
 *
 * This script is still in alpha-testing and in development.
 * TODO:
 * - check the referer
 * - check the time this script is last accessed (preventing too hits too fast)
 * - check the # times the script is accessed since the last refresh time
 * - change the refresh time ?
 *
 */

  
include("configfile.inc.php");

  
$min_keys 10;

  function 
create_key() {
    
$rand $info.rand(1000,30000);
    return 
md5($rand)."\n";
  } 
/* function key() */

  
if (!file_exists(KEY_FILE)) {
    
// generate KEY_FILE
    
$fd=fopen(KEY_FILE,"a");
    for (
$i=0;$i<$min_keys;$i++) {
      
$new_key create_key();
      
$file[] = $new_key;
      
fwrite($fd,$new_key);
    }
    
fclose($fd);
  }
  else {
    
$file file(KEY_FILE);
  
/*
    if (sizeof($file)<$min_keys) {
      $fd=fopen(KEY_FILE,"a");
      for ($i=0;$i<$min_keys/2;$i++) {
    fwrite($fd,create_key());
      }
      fclose($fd);
    }
  */
  
}

  
$key ereg_replace("\n","",array_rand($file));
  echo 
"<input type=\"hidden\" name=\"key\" value=\"$key\">\n";