#
session_start();
#
$here = $_SERVER['PHP_SELF'];
#
include_once('foot.php');
#
function getparam($name, $both)
{
 $a = null;
 if (isset($_POST[$name]))
	$a = $_POST[$name];
 if (($both === true) and ($a === null))
 {
	if (isset($_GET[$name]))
		$a = $_GET[$name];
 }
 if ($a == '' || $a == null)
	return '';
 // limit to 1K just to be sure
 return substr($a, 0, 1024);
}
#
$t = $nf;
echo "
$t";
echo "$t
(Let's hope I got the statistics correct :P)
";
#
$difficultydef = '';
$btcperblock = '';
#
if (isset($_SESSION['bitvalues']))
{
 $force = getparam('force', false);
 if ($force != '')
	unset($_SESSION['bitvalues']);
 else
 {
	if (isset($_SESSION['bitvalues']['difficultydef']))
		$difficultydef = $_SESSION['bitvalues']['difficultydef'];
	if (isset($_SESSION['bitvalues']['btcperblock']))
		$btcperblock = $_SESSION['bitvalues']['btcperblock'];
 }
}
if ($difficultydef == '' or $btcperblock == '')
{
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_HEADER, 0);
 if ($difficultydef == '')
 {
	curl_setopt($ch, CURLOPT_URL, 'http://blockexplorer.com/q/getdifficulty');
	$res = curl_exec($ch);
	if (curl_errno($ch))
	{
#		$difficultydef = '1690895.80305';
		$difficultydef = '1888786.7053531';
		echo "Failed to get current difficulty - using default for 4-Aug-2011 = $difficultydef
";
	}
	else
	{
		$difficultydef = $res;
		$_SESSION['bitvalues']['difficultydef'] = $res;
	}
 }
 if ($btcperblock == '')
 {
	curl_setopt($ch, CURLOPT_URL, 'http://blockexplorer.com/q/bcperblock');
	$res = curl_exec($ch);
	if (curl_errno($ch))
	{
		$btcperblock = 50;
		echo "Failed to get current BTC per Block - using default for 4-Aug-2011 = $btcperblock
";
	}
	else
	{
		$btcperblock = $res;
		$_SESSION['bitvalues']['btcperblock'] = $res;
	}
 }
 curl_close($ch);
}
#
$poolghsdef = '60';
$failhrsdef = '100';
$sharesdef = '10000000';
#
$difficulty = $difficultydef;
$poolghs = $poolghsdef;
$failhrs = $failhrsdef;
$shares = $sharesdef;
$probfail = '';
$probshare = '';
$failmsg = '';
$sharefail = '';
$sharemsg = '';
$mhperblock = '';
$ghperblock = '';
$sharesperblock = '';
$poolavtimeperblock = '';
$tt = '';
$poolblocksperday = '';
$poolbtcperday = '';
$sub = getparam('Calculate', false);
if ($sub == 'Calculate')
{
 $poolghs = getparam('poolghs', false);
 if ($poolghs == '' or $poolghs <= 0)
	$poolghs = $poolghsdef;
 $failhrs = getparam('failhrs', false);
 if ($failhrs == '' or $failhrs <= 0)
	$failhrs = $failhrsdef;
 $shares = getparam('shares', false);
 if ($shares == '' or $shares <= 0)
	$shares = $sharesdef;
 $shares = floor($shares);
 $mhperblock = ((float)(1) / (float)(1000000)) * pow(2, 48) / (float)(65535);
 $mhperblock *= floatval($difficulty);
 $ghperblock = $mhperblock / 1000;
 // hashes per share = 4G
 $sharesperblock = number_format(round($mhperblock / pow(2, 32) * 1000000), 0) . ' shares';
 $blockprobability = 1000 * $poolghs / $mhperblock; 
 $avtimeblock = 1 / $blockprobability;
 $poolavtimeperblock = $avtimeblock;
 $s = floor($poolavtimeperblock);
 $fr = round(($poolavtimeperblock - $s) * 1000) / 1000;
 if ($fr >= 1)
 {
	$fr--;
	$s++;
 }
 $m = floor($s / 60);
 $s = $s % 60;
 $h = floor($m / 60);
 $m = $m % 60;
 $d = floor($h / 24);
 $h = $h % 24;
 if ($d == '1')
	$plu = ' ';
 else
	$plu = 's ';
 $tt = $d.'day'.$plu.$h.'hr '.$m.'min '.$s.substr($fr, 1).'sec';
 $poolblocksperday = (float)(60 * 60 * 24) / $poolavtimeperblock;
 $poolbtcperday = round(($poolblocksperday * $btcperblock) * 100000000) / 100000000;
 $probblock = 1 - (($poolghs * 60 * 10 * 1000) / $mhperblock);
 if ($failhrs != '')
 {
	$blockratio = $failhrs * 6;
	$probability = pow($probblock, $blockratio);
	$probfail = number_format($probability * 100, 8) . '%';
	$failmsg = " ${failhrs}hrs";
 }
 if ($shares != '')
 {
	// shares per block = difficulty
	// hashes per share = 4G
	$hashes = $shares * pow(2, 32);
	$shareratio = $hashes / ($poolghs * 1000000000) / 600;
	$shareprob = pow($probblock, $shareratio);
	$sharefail = number_format($shareprob * 100, 8) . '%';
	$sharemsg = ' '.number_format($shares, 0).' shares';
 }
 $poolavtimeperblock .= 'sec';
 $poolbtcperday .= ' BTC + t2';
}
#
echo "";
#
foot();
#
echo "";
#
?>