[Functions] confirm_code, confirm_code_submit

This forum is now closed as part of retiring phpBB2.
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

This forum is now closed due to phpBB2.0 being retired.
Post Reply
A.I. BOT
Registered User
Posts: 1848
Joined: Thu Apr 17, 2003 11:43 pm
Location: Newfoundland, Canada
Contact:

[Functions] confirm_code, confirm_code_submit

Post by A.I. BOT » Fri Jul 15, 2005 5:22 pm

This function is for displaying and confirming - confirm code.

SQL
ALTER TABLE `phpbb_users` ADD `confirm_code` TEXT NOT NULL;

CODE

Code: Select all

//
// BEGIN function confirm_code
// USEAGE confirm_code ( start_letter:end_letter, start_number:end_number, number_of_letters:number_of_numbers, $userdata[ 'user_id' ] );
// EXAMPLE confirm_code ( 'A:Z', '0:10', '3:2', $userdata[ 'user_id' ] );
//
function confirm_code ( $letter_range, $number_range, $ammount, $user )
{
	global $db, $template;

	// Do the explosions to get the values
	$explode	= explode ( ':', $letter_range );
	$explode_2	= explode ( ':', $number_range );
	$explode_3	= explode ( ':', $ammount );

	// Set our letter and number ranges and shuffle for randomness
	$letters	= range ( $explode[ 0 ], $explode[ 1 ] );
	shuffle ( $letters );
	$numbers	= range ( $explode_2[ 0 ], $explode_2[ 1 ] );
	shuffle ( $numbers );

	// Lets get how many letters we are planning to use
	$total_letters	= count ( $letters ) - $explode_3[ 0 ];
	$total_letters	= $total_letters;

	// Run the loop from our results
	for ( $i = 0; $i < $total_letters; $i++ )
	{
		// Set the repeating variables
		$letter		= strtoupper ( $letters[ $i ] );
		$image		= imagecreatefromstring ( $letter );
		header('Content-Type: image/png');
		$letter_img	= imagepng ( $image );

		// Assign them to a block
		$template->assign_block_vars ( 'block_letters', array (
			'S_LETTER'	=> $letter,
			'I_LETTER'	=> $letter_img )
		);
	}

	// Lets get how many numbers we are planning to use
	$total_numbers	= count ( numbers ) - $explode_3[ 1 ];
	$total_numbers	= $total_numbers;

	// Runt he loop from our results
	for ( $x = 0; $x < $total_numbers; $x++ )
	{
		// Set the repeating variables
		$number		= $numbers[ $x ];
		$image		= imagecreatefromstring ( $number );
		header('Content-Type: image/png');
		$number_img	= imagepng ( $image );

		// Assign them to a block
		$template->assign_block_vars ( 'block_numbers', array (
			'S_NUMBER'	=> $number,
			'I_NUMBER'	=> $number_img )
		);
	}

	// Get the user's information
	$sql	= 'SELECT * FROM ' . USERS_TABLE . ' WHERE user_id = ' . $user;
	if ( ! ( $result = $db->sql_query ( $sql ) ) )
	{
		message_die ( GENERAL_ERROR, 'Can not get user information on query.', '', __LINE__, __FILE__, $sql );
	}
	$row	= $db->sql_fetchrow ( $result );

	// Check if the user's confirm_code is set or not
	if ( !empty ( $row[ 'confirm_code' ] ) )
	{
		// Update the SQL to empty confirm_code
		$sql	= 'UPDATE ' . USERS_TABLE . ' SET confirm_code = "" WHERE user_id = ' . $user;
		if ( ! ( $result = $db->sql_query ( $sql ) ) )
		{
			message_die ( GENERAL_ERROR, 'Can not reset user confirm code.', '', __LINE__, __FILE__, $sql );
		}
	}

	// Update the SQL to set the confirm_code
	$sql	= 'UPDATE ' . USERS_TABLE . ' SET confirm_code = "' . $letter . $number . '" WHERE user_id = ' . $user;
	if ( ! ( $result = $db->sql_query ( $sql ) ) )
	{
		message_die ( GENERAL_ERROR, 'Can not set user confirm code.', '', __LINE__, __FILE__, $sql );
	}
}
//
// END function confirm_code
//

//
// BEGIN function confirm_code_submit
// USEAGE confirm_code_submit ( HTTP/POST variable );
// EXAMPLE confirm_code_submit ( $HTTP_POST_VARS[ 'confirm_code' ] );
//
function confirm_code_submit ( $submit, $user )
{
	global $db, $HTTP_POST_VARS, $HTTP_GET_VARS;

	// Get the user's information
	$sql	= 'SELECT * FROM ' . USERS_TABLE . ' WHERE user_id = ' . $user;
	if ( ! ( $result = $db->sql_query ( $sql ) ) )
	{
		message_die ( GENERAL_ERROR, 'Can not get user information on query.', '', __LINE__, __FILE__, $sql );
	}
	$row	= $db->sql_fetchrow ( $result );

	if ( $row[ 'confirm_code' ] == $submit )
	{
		$result	= true;
	}
	else
	{
		$result	= false;
	}

	// Update the SQL to empty confirm_code
	$sql	= 'UPDATE ' . USERS_TABLE . ' SET confirm_code = "" WHERE user_id = ' . $user;
	if ( ! ( $result = $db->sql_query ( $sql ) ) )
	{
		message_die ( GENERAL_ERROR, 'Can not reset user confirm code.', '', __LINE__, __FILE__, $sql );
	}

	// Return true or false for the IFs
	return $result;
}
//
// END function confirm_code_submit
//
EXAMPLE PHP

Code: Select all

if ( !empty ( $HTTP_POST_VARS[ 'submit' ] ) )
{
	if ( confirm_code_submit ( $HTTP_POST_VARS[ 'confirm_code' ], $userdata[ 'user_id' ] ) == true )
	{
		message_die ( GENERAL_MESSAGE, 'Works :)' );
	}
	else
	{
		message_die ( GENERAL_MESSAGE, 'Works? :(' );
	}
}
else
{
	$template->set_filenames ( array (
		'body'	=> 'confirm.tpl' )
	);
	include ( $phpbb_root_path . 'includes/page_header.'.$phpEx );

	confirm_code ( 'A:Z', '0:10', '2:3', $userdata[ 'user_id' ] );

	$template->assign_vars ( array (
		'U_ACTION'	=> append_sid ( 'confirm.php' ) )
	);

	$template->pparse ( 'body' );
}
EXAMPLE TPL
<form action="{U_ACTION}" method="POST">
<!-- BEGIN block_letters -->
{block_letters.I_LETTER}
<!-- END block_letters -->
<!-- BEGIN block_numbers -->
{block_numbers.I_NUMBER}
<!-- END block_numbers -->
<br />
<input type="text" name="confirm_code"><br />
<input type="submit" name="submit" value="submit">
</form>


Thanks

NOTE: i dont have GD so let me no if it dont work
Last edited by A.I. BOT on Fri Jul 15, 2005 8:38 pm, edited 2 times in total.

User avatar
Flaming_cows
Registered User
Posts: 761
Joined: Sat Jul 05, 2003 1:43 am
Contact:

Post by Flaming_cows » Fri Jul 15, 2005 7:43 pm

This isn't very secure since you can determine what the visual text is by looking at the filenames, You should look into the GD library or Imagick for actual image generation.

A.I. BOT
Registered User
Posts: 1848
Joined: Thu Apr 17, 2003 11:43 pm
Location: Newfoundland, Canada
Contact:

Post by A.I. BOT » Fri Jul 15, 2005 8:23 pm

okey ill look into it :)

edit: function updated

A.I. BOT
Registered User
Posts: 1848
Joined: Thu Apr 17, 2003 11:43 pm
Location: Newfoundland, Canada
Contact:

Post by A.I. BOT » Fri Jul 15, 2005 11:55 pm

does the GD work ok?

User avatar
webmacster87
Former Team Member
Posts: 3758
Joined: Fri Jun 11, 2004 2:30 am
Location: San Mateo, CA
Name: Douglas Bell
Contact:

Post by webmacster87 » Sat Jul 15, 2006 8:10 pm

This needs to be updated to use the new code introduced in phpBB 2.0.21.

A.I. BOT
Registered User
Posts: 1848
Joined: Thu Apr 17, 2003 11:43 pm
Location: Newfoundland, Canada
Contact:

Post by A.I. BOT » Sat Jul 15, 2006 8:29 pm

Which new code is that ?

I'm planning to update the code there to this code that I wrote for a another project: http://205.251.205.161/captcha.phps ... is that ok for me to use ?

User avatar
webmacster87
Former Team Member
Posts: 3758
Joined: Fri Jun 11, 2004 2:30 am
Location: San Mateo, CA
Name: Douglas Bell
Contact:

Post by webmacster87 » Sat Jul 15, 2006 8:31 pm

A.I. BOT wrote: Which new code is that ?

I'm planning to update the code there to this code that I wrote for a another project: http://205.251.205.161/captcha.phps ... is that ok for me to use ?

In 2.0.21 the VC code was changed with some code backported from Olympus to enable the Visual Confirmation to work properly when zlib isn't enabled.

User avatar
exam4sure
Registered User
Posts: 2
Joined: Thu Apr 03, 2008 2:19 pm
Contact:

Re: [Functions] confirm_code, confirm_code_submit

Post by exam4sure » Mon Apr 07, 2008 2:23 pm

okey ill look into it
plz visit exam4sure get ccna, scjp, mcts cert Q&A.

Post Reply

Return to “[2.0.x] MOD Writers Discussion”