Back to mersenne-aries.sili.net main page
prob.php:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Mersenne-aries P-1 probability calculator</title>
<link rel="stylesheet" href="/style.css" type="text/css">
</head>
<body>
<a href="/">Back to <i>mersenne-aries.sili.net</i> main page</a>
<hr size="1">
<?php
if (@$_REQUEST['showsource']) {
$filestoshow = array(basename(__FILE__), 'functions.inc.php');
foreach ($filestoshow as $key => $value) {
echo htmlentities($value).':<blockquote style="border: 1px solid #000000; padding: 10px;">';
highlight_file($value);
echo '</blockquote>';
}
exit;
}
echo '<a href="'.$_SERVER['PHP_SELF'].'?showsource=1" style="font-size: 8pt;">show source</a><hr noshade size="1">';
///////////////////////////////////////////////////////////////////////////
require_once('functions.inc.php');
///////////////////////////////////////////////////////////////////////////
if (!isset($_REQUEST['b1']) && !isset($_REQUEST['b2'])) {
if (!@$_REQUEST['exponent']) {
echo 'ERROR: must enter exponent<hr>';
} elseif (@$_REQUEST['guess'] == 'n') {
// guess for never-been-LL'd
$_REQUEST['b1'] = B1bound($_REQUEST['exponent'], false);
$_REQUEST['b2'] = B2bound($_REQUEST['exponent'], false);
} elseif (@$_REQUEST['guess'] == 'y') {
// guess for already-been-LL'd-once
$_REQUEST['b1'] = B1bound($_REQUEST['exponent'], true);
$_REQUEST['b2'] = B2bound($_REQUEST['exponent'], true);
}
}
echo 'Estimate B1 & B2: ';
echo '<a href="#" onClick="if (document.form1.exponent.value == \'\') { alert(\'Enter an exponent first\'); return false; } document.form1.submitbutton.visible = false; location = \''.$_SERVER['PHP_SELF'].'?guess=n&exponent=\' + escape(document.form1.exponent.value);">never-been-LL\'d</a> | ';
echo '<a href="#" onClick="if (document.form1.exponent.value == \'\') { alert(\'Enter an exponent first\'); return false; } document.form1.submitbutton.visible = false; location = \''.$_SERVER['PHP_SELF'].'?guess=y&exponent=\' + escape(document.form1.exponent.value);">"LL\'d-once-already"</a><br>';
echo '<table border="0">';
echo '<form method="post" name="form1">';
echo '<tr><td>Exponent:</td><td><input type="text" name="exponent" value="'.htmlentities(@$_REQUEST['exponent'], ENT_QUOTES).'"></td></tr>';
echo '<tr><td>B1:</td><td><input type="text" name="b1" value="'.htmlentities(number_format(@$_REQUEST['b1'], 0, '.', ''), ENT_QUOTES).'"></td></tr>';
echo '<tr><td>B2:</td><td><input type="text" name="b2" value="'.htmlentities(number_format(@$_REQUEST['b2'], 0, '.', ''), ENT_QUOTES).'"></td></tr>';
echo '<tr><td>Factored Bits:</td><td><select name="factorbits">';
echo '<option value="">-default-</option>';
for ($i = 60; $i <= 80; $i++) {
echo '<option value="'.$i.'"'.(($i == @$_REQUEST['factorbits']) ? ' selected' : '').'>'.$i.'</option>';
}
echo '</select></td></tr>';
echo '<tr><td colspan="2"><input type="submit" name="submitbutton" value="Calculate"> ';
echo '</form>';
echo '</table>';
if (@$_REQUEST['exponent']) {
echo '<hr>';
echo 'M'.$_REQUEST['exponent'].', factored to '.(@$_REQUEST['factorbits'] ? @$_REQUEST['factorbits'] : FactorBits($_REQUEST['exponent'])).' bits, with B1='.number_format(@$_REQUEST['b1'], 0).' and B2='.number_format(@$_REQUEST['b2'], 0).'<br>';
echo 'Probability = <b>'.number_format(100 * FactorProbability($_REQUEST['exponent'], @$_REQUEST['b1'], @$_REQUEST['b2'], @$_REQUEST['factorbits']), 5).'%</b><br>';
echo 'Should take about '.number_format(CalcGHzDaysPminus1($_REQUEST['exponent'], @$_REQUEST['b1'], @$_REQUEST['b2']), 2).' GHz-days<br>';
}
?>
</body>
</html>
functions.inc.php:
<?php
require_once('db.inc.php');
error_reporting(E_ALL);
ini_set('display_errors', '1');
define('STATS_HTML_CACHE_DIR', dirname(__FILE__).'/statsHTMLcache/');
///////////////////////////////////////////////////////////////////////////
function filesize_remote($remotefile, $timeout=10) {
static $filesize_cache = array();
if (!@$filesize_cache[$remotefile]) {
$filesize_cache[$remotefile] = false;
$url = parse_url($remotefile);
if ($fp = @fsockopen($url['host'], ($url['port'] ? $url['port'] : 80), $errno, $errstr, $timeout)) {
fwrite($fp, 'HEAD '.@$url['path'].@$url['query'].' HTTP/1.0'."\r\n");
fwrite($fp, 'Host: '.@$url['host']."\r\n");
if (@$url['user'] || @$url['pass']) {
fwrite($fp, 'Authorization: Basic '.base64_encode($url['user'].':'.$url['pass'])."\r\n");
}
fwrite($fp, "\r\n");
stream_set_timeout($fp, $timeout);
while (!feof($fp)) {
$headerline = fgets($fp, 4096);
if (eregi('^Content-Length: (.*)', $headerline, $matches)) {
$filesize_cache[$remotefile] = intval($matches[1]);
break;
}
}
fclose ($fp);
}
}
return $filesize_cache[$remotefile];
}
function filedate_remote($remotefile, $timeout=10) {
static $filedate_cache = array();
if (!@$filedate_cache[$remotefile]) {
$filedate_cache[$remotefile] = false;
$url = parse_url($remotefile);
if ($fp = @fsockopen($url['host'], ($url['port'] ? $url['port'] : 80), $errno, $errstr, $timeout)) {
fwrite($fp, 'HEAD '.@$url['path'].@$url['query'].' HTTP/1.0'."\r\n");
fwrite($fp, 'Host: '.@$url['host']."\r\n");
if (@$url['user'] || @$url['pass']) {
fwrite($fp, 'Authorization: Basic '.base64_encode($url['user'].':'.$url['pass'])."\r\n");
}
fwrite($fp, "\r\n");
stream_set_timeout($fp, $timeout);
while (!feof($fp)) {
$headerline = fgets($fp, 4096);
if (eregi('^Last-Modified: (.*)', $headerline, $matches)) {
$filedate_cache[$remotefile] = strtotime($matches[1]) - date('Z');
break;
}
}
fclose($fp);
}
}
return $filedate_cache[$remotefile];
}
function P90years2GHzDays($P90years) {
// straight from George :)
// http://www.mersenneforum.org/showpost.php?p=133207&postcount=3
// One core of a C2D = 1.68 P4
// A P4 = 3.44 PIIs
// A PII = 1.12 Pentium
// Thus, a P-90 CPU year = 365 days * 1 C2GHD * (90MHz / 1000MHz) / 1.68 / 3.44 / 1.12 = 5.075 C2GHDs
return $P90years * 5.075;
}
function GHzDays2P90years($GHzDays) {
// straight from George :)
// http://www.mersenneforum.org/showpost.php?p=133207&postcount=3
// One core of a C2D = 1.68 P4
// A P4 = 3.44 PIIs
// A PII = 1.12 Pentium
// Thus, a P-90 CPU year = 365 days * 1 C2GHD * (90MHz / 1000MHz) / 1.68 / 3.44 / 1.12 = 5.075 C2GHDs
return $GHzDays / 5.075;
}
function CalcGHzDaysPminus1($exponent, $b1=false, $b2=false) {
if ($b1 === false) {
//$alreadyLLd = true; // most Mersenne-aries work will be done with Pfactor=exp,bits,1
$alreadyLLd = false;
$b1 = B1bound($exponent, $alreadyLLd);
}
if ($b2 == false) {
$b2 = $b1 * 25; // rough approximation of default auto-selection
} elseif ($b2 < $b1) {
$b2 = $b1;
}
require_once('cpucredit.inc.php');
return credit_cpu_PM1_factoring($exponent, 0, $b1, $b2);
}
function CalcP90yearsPminus1($exponent, $b1=false, $b2=false) {
return GHzDays2P90years(CalcGHzDaysPminus1($exponent, $b1, $b2));
}
function CalcGHzDaysECM($exponent, $curves_run, $b1, $b2) {
require_once('cpucredit.inc.php');
return credit_cpu_ECM($exponent, 0, $curves_run, $b1, $b2);
}
function CalcP90yearsECM($exponent, $curves_run, $b1, $b2) {
return GHzDays2P90years(CalcGHzDaysECM($exponent, $curves_run, $b1, $b2));
}
function CalcGHzDaysLL($exponent) {
require_once('cpucredit.inc.php');
return credit_cpu_LL($exponent, 0);
}
function CalcP90yearsLL($exponent) {
return GHzDays2P90years(CalcGHzDaysLL($exponent));
}
function BitsInFactor($factor, $intversion=false) {
return ($intversion ? ceil(log($factor, 2)) : log($factor, 2));
}
function ExponentFactorThreshold() {
static $ExponentThreshold = array(
// These breakeven points we're calculated on a 2.0 GHz P4 Northwood:
516000000 => 80,
420400000 => 79,
337400000 => 78,
264600000 => 77,
227300000 => 76,
186400000 => 75,
147500000 => 74,
115300000 => 73,
96830000 => 72,
75670000 => 71,
58520000 => 70,
47450000 => 69,
37800000 => 68,
29690000 => 67,
23390000 => 66,
// These breakevens we're calculated a long time ago on unknown hardware:
13380000 => 65,
8250000 => 64,
6515000 => 63,
5160000 => 62,
3960000 => 61,
2950000 => 60,
2360000 => 59,
1930000 => 58,
1480000 => 57,
1000000 => 56,
0 => 40,
// OLD values from the before-time
//79300000=>72, // Prime95 can't currently handle exponents bigger than this...
//71000000=>72,
//57020000=>71,
//44150000=>70,
//35100000=>69,
//28130000=>68,
//21590000=>67,
//17850000=>66,
);
return $ExponentThreshold;
}
function FactorBits($exponent) {
$ExponentThreshold = ExponentFactorThreshold();
$factorbits = 0;
foreach ($ExponentThreshold as $limit => $bits) {
if ($exponent >= $limit) {
$factorbits = $bits;
break;
}
}
return $factorbits;
}
function CalcGHzDaysTrialFactor($exponent, $frombits=0, $limit=false, $factor='') {
if ($limit === false) {
// how far will Prime95 factor this exponent by default
$limit = FactorBits($exponent);
}
if (!$frombits && !$factor) {
// this is a hack for pre-v25 which didn't always report starting point of TF
// most trial factoring is done from <whatever-the-current-level-is> to
// the reported bit depth, but it doesn't report the current level. So guess :)
if ($limit <= 58) {
$frombits = 32; // anything factored to low bit depths started from nothing, or close-to-nothing (CPU time to factor from 2^0 to 2^32 is insignificant)
} else {
$frombits = $limit - 1; // assume most submitted results will be LMH results increasing factor depth by 1 level
}
}
require_once('cpucredit.inc.php');
if ($factor) {
$frombits = floor(log($factor, 2));
return credit_cpu_TF_factor_p95($exponent, $factor, $frombits);
} else {
return credit_cpu_TF_no_factor($exponent, $frombits, $limit);
}
}
function CalcP90yearsTrialFactor($exponent, $frombits=0, $limit=false, $factor='') {
return GHzDays2P90years(CalcGHzDaysTrialFactor($exponent, $frombits, $limit, $factor));
}
function B1bound($exponent, $alreadyLLd=false) {
// formulae taken from Excel graph of actual values from Prime95
// it's a little off around FFT size changes, but close enough
$exp = $exponent / 1000000;
if ($alreadyLLd) {
//$B1 = (-11 * $exp * $exp) + (5800 * $exp); // projection data up to 79M
$B1 = (-1.3309 * $exp * $exp) + (5232 * $exp); // updated formula from 2008-12-13 with projection data up to 500M
} else {
//$B1 = (-24.529 * $exp * $exp) + (12434 * $exp); // projection data up to 79M
$B1 = (-3.2698 * $exp * $exp) + (11182 * $exp); // updated formula from 2008-12-13 with projection data up to 500M
}
return round($B1 / 5000) * 5000;
}
function B2bound($exponent, $alreadyLLd=false) {
return B1bound($exponent, $alreadyLLd) * ($alreadyLLd ? 20 : 25);
}
function DickmansFunction($x) {
static $savedF = array(
0, 0, 0, 0, 0, 0, 3.3513e-215, 5.63754e-208, 4.00865e-201,
1.65407e-194, 4.53598e-188, 8.93587e-182, 1.33115e-175,
1.55557e-169, 1.46609e-163, 1.13896e-157, 7.42296e-152,
3.80812e-146, 1.56963e-140, 5.32886e-135, 1.51923e-129,
3.69424e-124, 7.76066e-119, 1.42371e-113, 2.30187e-108,
3.30619e-103, 4.24793e-098, 4.80671e-093, 4.78516e-088,
4.22768e-083, 3.33979e-078, 2.37455e-073, 1.52822e-068,
8.94846e-064, 4.78909e-059, 4.65696e-057, 4.49802e-055, 4.31695e-053,
4.07311e-051, 3.81596e-049, 3.61043e-047, 1.73046e-045, 8.26375e-044,
3.9325e-042, 1.86471e-040, 8.8102e-039, 4.14402e-037, 1.99497e-035,
1.83001e-034, 1.59023e-033, 1.45505e-032, 1.24603e-031, 1.15674e-030,
9.70832e-030, 9.23876e-029, 4.20763e-028, 4.24611e-027, 1.61371e-026,
6.59556e-026, 3.17069e-025, 1.12205e-024, 4.65874e-024, 2.01267e-023,
6.2941e-023, 3.02604e-022, 7.84622e-022, 2.3526e-021, 6.7049e-021,
1.88634e-020, 4.59378e-020, 1.37233e-019, 4.00682e-019, 8.34209e-019,
2.21612e-018, 4.84252e-018, 1.02457e-017, 2.03289e-017, 4.07704e-017,
1.33778e-016, 2.4263e-016, 4.14981e-016, 7.0383e-016, 1.20511e-015,
3.85644e-015, 6.52861e-015, 1.06563e-014, 1.67897e-014, 2.79916e-014,
4.54319e-014, 9.83296e-014, 1.66278e-013, 2.61858e-013, 4.03872e-013,
5.98967e-013, 1.09674e-012, 1.70553e-012, 2.56573e-012, 3.72723e-012,
6.14029e-012, 9.33636e-012, 1.36469e-011, 1.89881e-011, 2.68391e-011,
4.12016e-011, 5.94394e-011, 8.43746e-011, 1.12903e-010, 1.66987e-010,
2.36959e-010, 3.11726e-010, 4.28713e-010, 5.90781e-010, 7.79892e-010,
1.05264e-009, 1.4016e-009, 1.87506e-009, 2.42521e-009, 3.14508e-009,
4.38605e-009, 5.43307e-009, 6.96737e-009, 8.84136e-009, 1.16286e-008,
1.42343e-008, 1.79697e-008, 2.30867e-008, 2.88832e-008, 3.52583e-008,
4.31032e-008, 5.46444e-008, 6.66625e-008, 8.06132e-008, 1.00085e-007,
1.20952e-007, 1.4816e-007, 1.80608e-007, 2.13125e-007, 2.5324e-007,
3.094e-007, 3.64545e-007, 4.31692e-007, 5.19078e-007, 6.03409e-007,
7.21811e-007, 8.53856e-007, 9.71749e-007, 1.13949e-006, 1.37042e-006,
1.53831e-006, 1.79066e-006, 2.15143e-006, 2.40216e-006, 2.76872e-006,
3.20825e-006, 3.61263e-006, 4.21315e-006, 4.76404e-006, 5.43261e-006,
6.2041e-006, 6.96243e-006, 7.94979e-006, 8.89079e-006, 1.01387e-005,
1.13376e-005, 1.2901e-005, 1.44183e-005, 1.59912e-005, 1.79752e-005,
1.99171e-005, 2.22665e-005, 2.47802e-005, 2.7678e-005, 3.0492e-005,
3.34189e-005, 3.71902e-005, 4.12605e-005, 4.54706e-005, 4.98411e-005,
5.48979e-005, 6.06015e-005, 6.61278e-005, 7.22258e-005, 7.97193e-005,
8.66574e-005, 9.48075e-005, 0.00010321, 0.000112479, 0.000121776,
0.000133344, 0.000144023, 0.000156667, 0.000168318, 0.000183192,
0.000196527, 0.00021395, 0.000228389, 0.000249223, 0.000264372,
0.000289384, 0.000305707, 0.000333992, 0.000353287, 0.000379868,
0.000408274, 0.00043638, 0.000465319, 0.000496504, 0.000530376,
0.000566008, 0.000602621, 0.000642286, 0.000684543, 0.000723853,
0.000772655, 0.000819418, 0.000868533, 0.000920399, 0.000975529,
0.00103188, 0.00109478, 0.00115777, 0.00122087, 0.00128857,
0.00136288, 0.00143557, 0.00151714, 0.00159747, 0.00167572,
0.00176556, 0.00186199, 0.00195063, 0.00205239, 0.00216102,
0.00225698, 0.00236962, 0.00249145, 0.00259636, 0.00272455,
0.00287006, 0.00297545, 0.00312346, 0.0032634, 0.00340298,
0.00355827, 0.00371195, 0.00387288, 0.00404725, 0.00420016,
0.00439746, 0.00456332, 0.00475936, 0.00495702, 0.00514683,
0.00535284, 0.00557904, 0.00578084, 0.00601028, 0.00623082,
0.00647765, 0.00673499, 0.00696553, 0.00722529, 0.00748878,
0.00775537, 0.00803271, 0.00832199, 0.00861612, 0.00889863,
0.00919876, 0.00953343, 0.00985465, 0.0101993, 0.0105042, 0.0108325,
0.0112019, 0.0115901, 0.0119295, 0.0123009, 0.0127191, 0.0130652,
0.0134855, 0.0139187, 0.0142929, 0.0147541, 0.0151354, 0.0156087,
0.0160572, 0.0165382, 0.0169669, 0.0174693, 0.0179460, 0.0184202,
0.0189555, 0.0194336, 0.0200107, 0.0204863, 0.0210242, 0.0216053,
0.0221361, 0.0226858, 0.0232693, 0.0239027, 0.0244779, 0.025081,
0.0257169, 0.0263059, 0.0269213, 0.0275533, 0.0282065, 0.0289028,
0.0295670, 0.0302268, 0.0309193, 0.0316619, 0.0323147, 0.0330398,
0.0338124, 0.0345267, 0.0353038, 0.0360947, 0.0368288, 0.0376202,
0.0383784, 0.0391894, 0.0399684, 0.0408148, 0.0416403, 0.042545,
0.0433662, 0.0442498, 0.0451003, 0.0460350, 0.0468801, 0.0478059,
0.0487442, 0.0496647, 0.0505752, 0.0515123, 0.0524792, 0.0534474,
0.0544682, 0.0554579, 0.0565024, 0.0574619, 0.0584757, 0.0595123,
0.0605988, 0.0615874, 0.0627190, 0.0637876, 0.0648830, 0.0659551,
0.0670567, 0.0681256, 0.0692764, 0.0704584, 0.0715399, 0.0727237,
0.0738803, 0.0750377, 0.0762275, 0.0773855, 0.0785934, 0.0797802,
0.0810061, 0.0822205, 0.0834827, 0.0847140, 0.0858734, 0.0871999,
0.0884137, 0.0896948, 0.0909820, 0.0922797, 0.0936350, 0.0948243,
0.0961283, 0.0974718, 0.0988291, 0.1000970, 0.1014330, 0.102847,
0.104222, 0.105492, 0.106885, 0.108330, 0.109672, 0.111048, 0.112438,
0.113857, 0.115311, 0.116730, 0.118133, 0.119519, 0.120990, 0.122452,
0.123905, 0.125445, 0.126852, 0.128326, 0.129793, 0.131277, 0.132817,
0.134305, 0.135772, 0.137284, 0.138882, 0.140372, 0.141920, 0.143445,
0.144940, 0.146515, 0.148145, 0.149653, 0.151199, 0.152879, 0.154368,
0.155958, 0.157674, 0.159211, 0.160787, 0.162410, 0.164043, 0.165693,
0.167281, 0.168956, 0.170589, 0.172252, 0.173884, 0.175575, 0.177208,
0.178873, 0.180599, 0.182240, 0.183975, 0.185654, 0.187363, 0.189106,
0.190729, 0.192520, 0.194158, 0.195879, 0.197697, 0.199391, 0.201164,
0.202879, 0.204602, 0.206413, 0.208180, 0.209911, 0.211753, 0.213484,
0.215263, 0.217050, 0.218869, 0.220677, 0.222384, 0.224253, 0.226071,
0.227886, 0.229726, 0.231529, 0.233373, 0.235234, 0.237081, 0.238853,
0.240735, 0.242606, 0.244465, 0.246371, 0.248218, 0.250135, 0.251944,
0.253836, 0.255708, 0.257578, 0.259568, 0.261424, 0.263308, 0.265313,
0.267160, 0.269073, 0.271046, 0.272921, 0.274841, 0.276819, 0.278735,
0.280616, 0.282653, 0.284613, 0.286558, 0.288478, 0.290472, 0.292474,
0.294459, 0.296379, 0.298382, 0.300357, 0.302378, 0.304340, 0.306853
);
if ($x >= 1.0) {
return 1;
} elseif ($x >= 0.5) {
return 1 + log($x);
}
$i = floor($x * 1000);
if (!isset($savedF[$i]) || !isset($savedF[$i + 1])) {
return false;
}
return ($savedF[$i] + (($x * 1000.0) - $i) * ($savedF[$i + 1] - $savedF[$i]));
}
function FactorProbability($exponent, $B1, $B2, $HFF=false) {
if ($B1 < 30) {
return 0;
}
$B2 = max($B2, $B1);
if (!$HFF) {
$HFF = FactorBits($exponent);
}
$ProbabiltyThreshold = 0.001;
$LogK = log(1.5) + (($HFF - 1) * log(2)) - log($exponent);
$LogTemp = $LogK - log($B1 + $B2) + log(2);
$FactorProbability = 0;
$H = $HFF;
while (true) {
$Prob1 = DickmansFunction(log($B1) / $LogK);
$Aux = DickmansFunction(log($B2) / $LogTemp);
$Prob2 = $Prob1 + (DickmansFunction(log($B2) / $LogK) - $Prob1);
if ($Aux != 0) {
$Prob2 *= DickmansFunction(log($B1) / $LogTemp) / $Aux;
}
$H++;
$FactorProbability += $Prob2 / $H;
if ($Prob2 < $ProbabiltyThreshold) {
break;
}
$LogK += log(2);
$LogTemp += log(2);
}
$FudgeFactor = 1.16; // I don't know why, but my numbers are off by approx this much from Prime95
$FactorProbability *= $FudgeFactor;
return $FactorProbability;
}
function AdminErrorEmail($file, $line, $subject, $body) {
$body .= "\n\n".str_repeat('-', 40)."\n";
$body .= '$_REQUEST:'."\n".print_r($_REQUEST, true)."\n".str_repeat('-', 40)."\n";
$body .= '$_GET:' ."\n".print_r($_GET, true)."\n".str_repeat('-', 40)."\n";
$body .= '$_POST:' ."\n".print_r($_POST, true)."\n".str_repeat('-', 40)."\n";
$body .= '$_COOKIE:' ."\n".print_r($_COOKIE, true)."\n".str_repeat('-', 40)."\n";
$body .= '$_SERVER:' ."\n".print_r($_SERVER, true)."\n".str_repeat('-', 40)."\n";
$headers = '';
$headers .= 'From: mersenne-aries.sili.net <'.EMAIL_JAMES.'>'."\r\n";
$headers .= 'Reply-To: '.EMAIL_JAMES."\r\n";
return mail(EMAIL_JAMES, $subject, '==['.basename($file).'::'.$line."]==\n\n".$body, $headers);
}
function SafeSQLquery($SQLquery, $linenum='') {
$result = @mysql_query($SQLquery);
$error = mysql_error();
if ($error) {
if (@$_SERVER['HTTP_HOST'] == 'www.example.com') {
echo '<hr>'.$error.'<hr>';
} elseif (eregi('^Duplicate entry', $error)) {
// ignore
} else {
AdminErrorEmail(__FILE__, __LINE__, 'Mersenne-Aires SQL error', $error."\n\n".'On line '.$linenum."\n".'While processing query:'."\n".$SQLquery);
}
}
return $result;
}
function getUserCompID($usernamestring, $compidstring) {
static $UserCompIDlookup = array();
if (!isset($UserCompIDlookup[$usernamestring][$compidstring])) {
$SQLquery = 'SELECT `usercompid`';
$SQLquery .= ' FROM `usercomp`';
$SQLquery .= ' WHERE (`userid` = "'.mysql_escape_string($usernamestring).'")';
$SQLquery .= ' AND (`compid` = "'.mysql_escape_string($compidstring).'")';
$result = mysql_query($SQLquery);
if ($row = mysql_fetch_assoc($result)) {
$UserCompIDlookup[$usernamestring][$compidstring] = $row['usercompid'];
} else {
$SQLquery = 'INSERT INTO `usercomp` (`userid`, `compid`) VALUES (';
$SQLquery .= '"'.mysql_escape_string($usernamestring).'", ';
$SQLquery .= '"'.mysql_escape_string($compidstring).'")';
mysql_query($SQLquery);
$UserCompIDlookup[$usernamestring][$compidstring] = mysql_insert_id();
}
}
if (!$UserCompIDlookup[$usernamestring][$compidstring]) {
AdminErrorEmail(__FILE__, __LINE__, 'Mersenne-aries: failed to getUserCompID('.$usernamestring.', '.$compidstring.')', $SQLquery."\n\n".mysql_error());
echo 'An error has occurred, sorry. The site administrator has been notified and will fix it shortly.';
exit;
}
return $UserCompIDlookup[$usernamestring][$compidstring];
}
function getCompName($usercompid) {
static $CompNameLookup = array();
if (!isset($CompNameLookup[$usercompid])) {
$CompNameLookup[$usercompid] = '';
$SQLquery = 'SELECT `compid`';
$SQLquery .= ' FROM `usercomp`';
$SQLquery .= ' WHERE (`usercompid` = "'.mysql_escape_string($usercompid).'")';
$result = mysql_query($SQLquery);
if ($row = mysql_fetch_assoc($result)) {
$CompNameLookup[$usercompid] = $row['compid'];
}
}
return $CompNameLookup[$usercompid];
}
function EmailAttachment($from, $to, $subject, $textbody, &$attachmentdata, $attachmentfilename, $base64encode=true) {
$boundary = '_NextPart_'.time().'_'.md5($attachmentdata).'_';
$textheaders = '--'.$boundary."\n";
$textheaders .= 'Content-Type: text/plain; format=flowed; charset="iso-8859-1"'."\n";
$textheaders .= 'Content-Transfer-Encoding: 7bit'."\n\n";
$attachmentheaders = '--'.$boundary."\n";
if ($base64encode) {
$attachmentheaders .= 'Content-Type: application/octet-stream; name="'.$attachmentfilename.'"'."\n";
$attachmentheaders .= 'Content-Transfer-Encoding: base64'."\n";
} else {
$attachmentheaders .= 'Content-Type: text/plain; name="'.$attachmentfilename.'"'."\n";
$attachmentheaders .= 'Content-Transfer-Encoding: 7bit'."\n";
}
$attachmentheaders .= 'Content-Disposition: attachment; filename="'.$attachmentfilename.'"'."\n\n";
$headers[] = 'From: '.$from;
$headers[] = 'Content-Type: multipart/mixed; boundary="'.$boundary.'"';
return @mail($to, $subject, $textheaders.ereg_replace("[\x80-\xFF]", '?', $textbody)."\n\n".$attachmentheaders.($base64encode ? wordwrap(base64_encode($attachmentdata), 76, "\n", true) : $attachmentdata)."\n\n".'--'.$boundary."--\n\n", implode("\r\n", $headers));
}
function caseinsensitivesortsub($a, $b) {
$a = strtolower($a);
$b = strtolower($b);
if ($a == $b) {
return 0;
} elseif ($a > $b) {
return 1;
}
return -1;
}
function BarGraph($width, $height=10, $color='0033CC', $title='') {
return '<img src="index.php?px='.$color.'" height="'.max(1, round($height)).'" width="'.max(1, round($width)).'" border="0" title="'.htmlentities($title).'">';
}
function GetPct($numerator, $denominator) {
if ($denominator == 0) {
return 0;
}
return (($numerator / $denominator) * 100);
}
function NonZeroMin() {
$arg_list = func_get_args();
$NonZeroValues = array();
if ((func_num_args() == 1) && is_array($arg_list[0])) {
$values_to_check = $arg_list[0];
} else {
$values_to_check = $arg_list;
}
foreach ($values_to_check as $key => $value) {
if ($value) {
$NonZeroValues[] = $value;
}
}
if (count($NonZeroValues) == 0) {
return false;
}
return min($NonZeroValues);
}
function Range2bgcolor($value, $maxvalue, $emptyvalue=null) {
if ((strlen($value) == 0) && !is_null($emptyvalue)) {
return $emptyvalue;
}
$value = min(max(0, $value), $maxvalue);
$scale = round(($value / $maxvalue) * 255);
$r = str_pad(dechex( $scale), 2, '0', STR_PAD_LEFT);
$g = str_pad(dechex( 255 - $scale), 2, '0', STR_PAD_LEFT);
$b = str_pad(dechex(round(255 * (1 - abs((128 - $scale) / 128)))), 2, '0', STR_PAD_LEFT);
return $r.$g.$b;
}
function NiceTimeFormatting($seconds, $precision=1, $returnparts=false) {
$sign = (($seconds < 0) ? -1 : 1);
$seconds = abs($seconds);
do {
if ($seconds < 100) {
$value = number_format($seconds, 0);
$unit = 'second'.(($seconds > 1) ? 's' : '');
break;
}
$minutes = $seconds / 60;
if ($minutes < 60) {
$value = number_format($minutes, $precision);
$unit = 'minutes';
break;
}
$hours = $seconds / 3600;
if ($hours < 24) {
$value = number_format($hours, $precision);
$unit = 'hours';
break;
}
$days = $seconds / 86400;
if ($days < 60) {
$value = number_format($days, $precision);
$unit = 'days';
break;
}
$months = $seconds / (30 * 86400);
if ($months < 12) {
$value = number_format($months, $precision);
$unit = 'months';
break;
}
$years = $seconds / (365 * 86400);
if (true) {
$value = number_format($years, $precision);
$unit = 'years';
break;
}
} while (false);
$value *= $sign;
if ($returnparts) {
return array($value, $unit);
}
return $value.' '.$unit;
}
?>