<?

require_once ("include/bittorrent.php");

if (! preg_match ( ':^/(\d{1,10})/([\w]{32})/(.+)$:', $_SERVER ["PATH_INFO"], $matches ))
	die ();

$id = ( int ) $matches [1];
$md5 = $matches [2];
$email = urldecode ( $matches [3] );

if (! $id)
	die ();

dbconn ();

$res = sql_query ( "SELECT editsecret FROM users WHERE id = $id" );
$row = mysql_fetch_array ( $res );

if (! $row)
	die ();

$sec = hash_pad ( $row ["editsecret"] );
if (preg_match ( '/^ *$/s', $sec ))
	die ();
if ($md5 != md5 ( $sec . $email . $sec ))
	die ();

sql_query ( "UPDATE users SET editsecret='', email=" . sqlesc ( $email ) . " WHERE id = $id AND editsecret = " . sqlesc ( $row ["editsecret"] ) );

if (! mysql_affected_rows ())
	die ();

location ( "my.php?emailch=1" );

?>