<?php
require_once("../../config.php");

$vimage = addslashes($_GET['vimage']);

function dbconnect($db_host, $db_user, $db_pass, $db_name, $db_port) {
	global $db_connect, $db_prefix;

	// Create connection
	$db_connect = new mysqli($db_host, $db_user, $db_pass, $db_name, $db_port);
	unset($db_host, $db_port, $db_user, $db_pass);

	// Check connection
	if ($db_connect->connect_error) {
		die("Unable to establish connection to MySQL
		".$db_connect->connect_error);
	}
}
function dbquery($query){
	global $db_connect;

	if (!$result = $db_connect->query($query)) {
		if ($db_connect->error) echo "Error: ".$db_connect->error."
		";
		return FALSE;
	} else {
		return $result;
	}
}
// Establish mySQL database connection
dbconnect($db_host, $db_user, $db_pass, $db_name, 3306);
unset($db_host, $db_user, $db_pass);

// Code ermitteln und grafisch anzeigen
$sql_q = "SELECT captcha_string FROM `".DB_PREFIX."captcha` WHERE `captcha_encode` = '$vimage'";
    $sql_r=dbquery($sql_q);
    $sql_row= mysqli_fetch_array($sql_r);
	//$code = $db_connect->query($query))
    $code=$sql_row['captcha_string'];

if ($code!="") {
	$imf = rand(3,5); $imx = rand(15,40); $imy = rand(2,7);
	$im = ImageCreateFromJPEG("./images/validate_bg.jpg");
	$tcolor = ImageColorAllocate($im, 40, 40, 40);
	Header("Content-type: image/jpeg");
	ImageString ($im, $imf, $imx, $imy, $code, $tcolor);
	ImageJPEG($im, NULL, 80);
	ImageDestroy($im);
}

