<?php 
require_once('scripts/sb_functions.php');
global $logged_in;
$logged_in = logged_in( false, true );
	
read_config();
	
sb_language( 'rating' );

/////////////////////////////////////////////////////////////////////
// temporarly store referer info ....
// append_to_file('content/rate_cgi.log',$_SERVER['HTTP_REFERER']."\t".$_SERVER['REMOTE_ADDR']."\t".$_SERVER['REMOTE_HOST']."\n");


// check if the HTTP REFERER is the same as the SERVER NAME
// if not --> re-direct immediately

$server=str_replace('http://','',$_SERVER['HTTP_REFERER']);
list($server)=explode('/',$server);
if ($server != $_SERVER['SERVER_NAME']){
    redirect_to_url( 'index.php' );
}

/*
html_header('',false);
foreach ($_REQUEST as $key => $value){
  echo "$key = $value <br>";
}
*/

if ( array_key_exists( 'y', $_REQUEST ) && array_key_exists( 'm', $_REQUEST ) && array_key_exists( 'entry', $_REQUEST ) ) {
  $rating = false;
  for ($star=1; $star <= 5; $star++){
    if (array_key_exists('rating'.$star,$_POST)){
      $rating = $star;
      break;
    }
    if (array_key_exists('rating'.$star.'_x',$_POST)){
      $rating = $star;
      break;
    }
  }
  if ( $rating && $rating >= 1 && $rating <= 5 ) {		
	if ( strpos( $_REQUEST[ 'y' ], array( '/', '.', '\\', '%' ) ) === false && strlen( $_REQUEST[ 'y' ] ) == 2 &&
	     strpos( $_REQUEST[ 'm' ], array( '/', '.', '\\', '%' ) ) === false && strlen( $_REQUEST[ 'm' ] ) == 2 &&
	     strpos( $_REQUEST[ 'entry' ], array( '/', '.', '\\', '%' ) ) === false && strlen( $_REQUEST[ 'entry' ] ) == 18 ) {
	    $y = $_REQUEST[ 'y' ];
	    $m = $_REQUEST[ 'm' ];
	    $entry = $_REQUEST[ 'entry' ];
	    if ( entry_exists( $y, $m, $entry ) ) {
	      write_rating( $y, $m, $entry, $rating );
	      redirect_to_url( 'index.php?entry='.$entry );
	    }
	}
    }
}
	
// Even if it failed to store the vote, we still want to return to the home page.
$ok = true;
if ( $ok === true ) {
    redirect_to_url( 'index.php' );
}

html_header('',false);

function page_content() {
    global $lang_string, $user_colors;
    
    if ( $ok !== true ) {
	echo( $lang_string[ 'error' ] . $ok . '<p />' );
    }
		
    echo( '<a href="index.php">'.$lang_string[ 'home' ] . '</a><br /><br />' );
}
theme_pagelayout();
html_footer();

?>
