<?
session_start();

if( isset($_REQUEST['pin']) && isset($_SESSION['pin']) && ( $_REQUEST['pin'] == $_SESSION['pin'] ) )
{
	unset($_SESSION['pin']);

	$str_file_source = 'resource/graphics/type' . mt_rand(0, 1) .'/';

	$uint_key = mt_rand(123, 9876);
	if( $uint_key < 1000 )
		$str_key = '0' . (string)$uint_key;
	else
		$str_key = (string)$uint_key;

	$_SESSION['pass_key'] = $str_key;

	$arr_width = array();
	$uint_width = 0;
	$arr_width[0] = $uint_width;

	for( $i=0; $i<4; $i++ )
	{
		$arr_image_size = getimagesize($str_file_source . $str_key[$i] . '.gif');
		$uint_width = $uint_width + (int)$arr_image_size[0];
		$arr_width[$i+1] = $uint_width;
	}

	$uint_height = (int)$arr_image_size[1];

	$im0 = imagecreatetruecolor($uint_width, $uint_height);

	for( $i=0; $i<4; $i++ )
	{
		$im = imagecreatefromgif($str_file_source . $str_key[$i] . '.gif');
		imagecopy($im0, $im, $arr_width[$i], 0, 0, 0, $arr_width[$i+1]-$arr_width[$i], $uint_height);
		imagedestroy($im);
	}

	$uint_max_x = $uint_width - 1;
	$uint_max_y = $uint_height - 1;

	$array_x = array(mt_rand(0, $uint_max_x), mt_rand(0, $uint_max_x), mt_rand(0, $uint_max_x), mt_rand(0, $uint_max_x));
	$array_y = array(mt_rand(0, $uint_max_y), mt_rand(0, $uint_max_y), mt_rand(0, $uint_max_y), mt_rand(0, $uint_max_y));

	sort($array_y);

	$int_color_line = imagecolorallocate($im0, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));

	imageline($im0, $array_x[0], 0, $array_x[1], $uint_max_y, $int_color_line);
	imageline($im0, $array_x[2], 0, $array_x[3], $uint_max_y, $int_color_line);

	imageline($im0, 0, $array_y[0], $uint_max_x, $array_y[2], $int_color_line);
	imageline($im0, 0, $array_y[3], $uint_max_x, $array_y[1], $int_color_line);

	header('Content-type: image/gif');
	imagegif($im0);
	imagedestroy($im0);
}
?>