MyBB Community Forums

Full Version: PHP Error Help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm having some trouble with a bit of PHP. I only added a single line to an otherwise working script and it's suddenly throwing errors at me. There is supposedly an unexpected "}" on line 37 (i've highlighted it)


Quote:<?php
//require config.php
require("./config.php");
$email = addslashes($_GET['email']);
$cipher = mcrypt_module_open(MCRYPT_3DES, '', 'cbc', '');

$con = mysql_connect($mysql_host.":".$mysql_port,$mysql_username,$mysql_password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($mysql_db, $con);
$result = mysql_query("SELECT * FROM RegistrationInfo WHERE email = '".$email."'");
$row = mysql_fetch_array($result);
//build string
$randomString = (time() + rand(12,24))* rand(3,5);


$xml = "<root><RegistrationInfo><Name>".$row['name']."</Name><Email>".$email."</Email><Key>".$row['key']."</Key><Type>".$row['type']."</Type><ExpiryTimestamp>".$row['expiryTimestamp']."</ExpiryTimestamp><randomString>".$randomString."</randomString></RegistrationInfo></root>";
mysql_close($con);
$xmlString = encrypt($xml);
echo $xmlString;


function Encrypt($buffer) {
global $key, $iv, $cipher;

//make sure the key and IV are the correct lengths
if (strlen($key) < 24) {
$keylength = strlen($key);
$keyfinal = $key;
for ($i = 1; $i <= 24; $i++) {
$keyfinal = $keyfinal.$key;
}
$key = $keyfinal;
$key = substr($key, 0, 24)

}


if (strlen($iv) < 8) {
$ivlength = strlen($iv);
$ivfinal = $iv;
for ($i = 1; $i <= 8; $i++) {
$ivfinal = $ivfinal.$iv;
}
$iv = $ivfinal;
$iv = substr($iv, 0, 8)
}


// get the amount of bytes to pad
$extra = 8 - (strlen($buffer) % 8);
//printvar($extra, 'Padding with n zeros');

// add the zero padding
if($extra > 0) {
for($i = 0; $i < $extra; $i++) {
$buffer .= "\0";
}
}

mcrypt_generic_init($cipher, $key, $iv);
$result = bin2hex(mcrypt_generic($cipher, $buffer));
mcrypt_generic_deinit($cipher);
return $result;
}

function Decrypt($buffer) {
global $key, $iv, $cipher;
//make sure the key and IV are the correct lengths
if (strlen($key) < 24) {
$keylength = strlen($key);
$keyfinal = $key;
for ($i = 1; $i <= 24; $i++) {
$keyfinal = $keyfinal.$key;
}
$key = $keyfinal;
$key = substr($key, 0, 24)

}

if (strlen($iv) < 8) {
$ivlength = strlen($iv);
$ivfinal = $iv;
for ($i = 1; $i <= 8; $i++) {
$ivfinal = $ivfinal.$iv;
}
$iv = $ivfinal;
$iv = substr($iv, 0, 8)
}


mcrypt_generic_init($cipher, $key, $iv);
$result = rtrim(mdecrypt_generic($cipher, hex2bin($buffer)), "\0");
mcrypt_generic_deinit($cipher);
return $result;
}

function hex2bin($data)
{
$len = strlen($data);
return pack("H" . $len, $data);
}


?>

Any ideas?

Also, sorry for the lack of whitespace, FF doesnt seem to like it XD
Try this:

<?php
//require config.php
require("./config.php");
$email = addslashes($_GET['email']);
$cipher = mcrypt_module_open(MCRYPT_3DES, '', 'cbc', '');

$con = mysql_connect($mysql_host.":".$mysql_port,$mysql_username,$mysql_password);
if (!$con)
{
	die('Could not connect: ' . mysql_error());
}

mysql_select_db($mysql_db, $con);
$result = mysql_query("SELECT * FROM RegistrationInfo WHERE email = '".$email."'");
$row = mysql_fetch_array($result);
//build string
$randomString = (time() + rand(12,24))* rand(3,5);


$xml = "<root><RegistrationInfo><Name>".$row['name']."</Name><Email>".$email."</Email><Key>".$row['key']."</Key><Type>".$row['type']."</Type><ExpiryTimestamp>".$row['expiryTimestamp']."</ExpiryTimestamp><randomString>".$randomString."</randomString></RegistrationInfo></root>";
mysql_close($con);
$xmlString = encrypt($xml);
echo $xmlString;


function Encrypt($buffer) {
	global $key, $iv, $cipher;

	//make sure the key and IV are the correct lengths
	if (strlen($key) < 24) {
		$keylength = strlen($key);
		$keyfinal = $key;
		
		for ($i = 1; $i <= 24; $i++) {
			$keyfinal = $keyfinal.$key;
		}
		
		$key = $keyfinal;
		$key = substr($key, 0, 24);
	}

	if (strlen($iv) < 8) {
		$ivlength = strlen($iv);
		$ivfinal = $iv;
		
		for ($i = 1; $i <= 8; $i++) {
			$ivfinal = $ivfinal.$iv;
		}
		
		$iv = $ivfinal;
		$iv = substr($iv, 0, 8)
	}


	// get the amount of bytes to pad
	$extra = 8 - (strlen($buffer) % 8);
	//printvar($extra, 'Padding with n zeros');

	// add the zero padding
	if($extra > 0) {
		for($i = 0; $i < $extra; $i++) {
			$buffer .= "\0";
		}
	}

	mcrypt_generic_init($cipher, $key, $iv);
	$result = bin2hex(mcrypt_generic($cipher, $buffer));
	mcrypt_generic_deinit($cipher);
	return $result;
}

function Decrypt($buffer) {
	global $key, $iv, $cipher;
	//make sure the key and IV are the correct lengths
	if (strlen($key) < 24) {
		$keylength = strlen($key);
		$keyfinal = $key;
		
		for ($i = 1; $i <= 24; $i++) {
			$keyfinal = $keyfinal.$key;
		}
		$key = $keyfinal;
		$key = substr($key, 0, 24)	
	}

	if (strlen($iv) < 8) {
		$ivlength = strlen($iv);
		$ivfinal = $iv;

		for ($i = 1; $i <= 8; $i++) {
			$ivfinal = $ivfinal.$iv;
		}
		
		$iv = $ivfinal;
		$iv = substr($iv, 0, 8)
	}


	mcrypt_generic_init($cipher, $key, $iv);
	$result = rtrim(mdecrypt_generic($cipher, hex2bin($buffer)), "\0");
	mcrypt_generic_deinit($cipher);
	return $result;
}

function hex2bin($data)
{
	$len = strlen($data);
	return pack("H" . $len, $data);
}
?>

You'd missed a semi-colon to finish the line on line 36 Wink
XD wow, i am pretty bad at PHP now XD

Thanks Smile

I wish PHP gave errors like vb.NET does Confused VB even highlights the exact line where everything goes wrong.
No problem Smile

And yeah, it would be nice, though you can usually work out where the error is from the error logs Smile
The way it checks for errors is annoying. If you miss a semi-colon, it may not really show up in your code until you write a ( or { XD