0);
do
{
$mcast_soc = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
if ($mcast_soc === false || $mcast_soc == null)
{
$msg = "ERR: mcast send socket create(UDP) failed";
if ($rigipsecurity === false)
{
$error = socket_strerror(socket_last_error());
$error = "$msg '$error'\n";
}
else
$error = "$msg\n";
socket_close($rep_soc);
return;
}
$buf = "cgminer-$mcastcode-$mcastlistport";
socket_sendto($mcast_soc, $buf, strlen($buf), 0, $mcastaddr, $mcastport);
socket_close($mcast_soc);
$stt = microtime(true);
while (true)
{
$got = @socket_recvfrom($rep_soc, $buf, 32, MSG_DONTWAIT, $ip, $p);
if ($got !== false && $got > 0)
{
$ans = explode('-', $buf, 4);
if (count($ans) >= 3 && $ans[0] == 'cgm' && $ans[1] == 'FTW')
{
$rp = intval($ans[2]);
if (count($ans) > 3)
$mdes = str_replace("\0", '', $ans[3]);
else
$mdes = '';
if (strlen($mdes) > 0)
$rig = "$ip:$rp:$mdes";
else
$rig = "$ip:$rp";
if (!in_array($rig, $rigs))
$rigs[] = $rig;
}
}
if ((microtime(true) - $stt) >= $mcasttimeout)
break;
usleep(100000);
}
if ($mcastexpect > 0 && count($rigs) >= $mcastexpect)
$doretry = false;
} while ($doretry && --$retries > 0);
socket_close($rep_soc);
}
#
function getrigs()
{
global $rigs;
mcastrigs();
sort($rigs);
}
#
function getsock($rig, $addr, $port)
{
global $rigport, $rigips, $rignames, $rigipsecurity;
global $haderror, $error, $socksndtimeoutsec, $sockrcvtimeoutsec;
$port = trim($port);
if (strlen($port) == 0)
$port = $rigport;
$error = null;
$socket = null;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false || $socket === null)
{
$haderror = true;
if ($rigipsecurity === false)
{
$error = socket_strerror(socket_last_error());
$msg = "socket create(TCP) failed";
$error = "ERR: $msg '$error'\n";
}
else
$error = "ERR: socket create(TCP) failed\n";
return null;
}
// Ignore if this fails since the socket connect may work anyway
// and nothing is gained by aborting if the option cannot be set
// since we don't know in advance if it can connect
socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array('sec' => $socksndtimeoutsec, 'usec' => 0));
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $sockrcvtimeoutsec, 'usec' => 0));
$res = socket_connect($socket, $addr, $port);
if ($res === false)
{
$haderror = true;
if ($rigipsecurity === false)
{
$error = socket_strerror(socket_last_error());
$msg = "socket connect($addr,$port) failed";
$error = "ERR: $msg '$error'\n";
}
else
$error = "ERR: socket connect($rig) failed\n";
socket_close($socket);
return null;
}
if ($rignames !== false && !isset($rigips[$addr]))
if (socket_getpeername($socket, $ip) == true)
$rigips[$addr] = $ip;
return $socket;
}
#
function readsockline($socket)
{
$line = '';
while (true)
{
$byte = socket_read($socket, 1);
if ($byte === false || $byte === '')
break;
if ($byte === "\0")
break;
$line .= $byte;
}
return $line;
}
#
function api_convert_escape($str)
{
$res = '';
$len = strlen($str);
for ($i = 0; $i < $len; $i++)
{
$ch = substr($str, $i, 1);
if ($ch != '\\' || $i == ($len-1))
$res .= $ch;
else
{
$i++;
$ch = substr($str, $i, 1);
switch ($ch)
{
case '|':
$res .= "\1";
break;
case '\\':
$res .= "\2";
break;
case '=':
$res .= "\3";
break;
case ',':
$res .= "\4";
break;
default:
$res .= $ch;
}
}
}
return $res;
}
#
function revert($str)
{
return str_replace(array("\1", "\2", "\3", "\4"), array("|", "\\", "=", ","), $str);
}
#
function api($rig, $cmd)
{
global $haderror, $error;
global $miner, $port, $hidefields, $mmcmd;
$socket = getsock($rig, $miner, $port);
if ($socket != null)
{
if ($cmd == 'mm')
socket_write($socket, $mmcmd, strlen($mmcmd));
else
socket_write($socket, $cmd, strlen($cmd));
$line = readsockline($socket);
socket_close($socket);
if (strlen($line) == 0)
{
$haderror = true;
$error = "WARN: '$cmd' returned nothing\n";
return $line;
}
# print "$cmd returned '$line'\n";
$line = api_convert_escape($line);
$data = array();
$objs = explode('|', $line);
foreach ($objs as $obj)
{
if (strlen($obj) > 0)
{
$items = explode(',', $obj);
$item = $items[0];
$id = explode('=', $items[0], 2);
if (count($id) == 1 or !ctype_digit($id[1]))
$name = $id[0];
else
$name = $id[0].$id[1];
if (strlen($name) == 0)
$name = 'null';
$sectionname = preg_replace('/\d/', '', $name);
if (isset($data[$name]))
{
$num = 1;
while (isset($data[$name.$num]))
$num++;
$name .= $num;
}
$counter = 0;
foreach ($items as $item)
{
$id = explode('=', $item, 2);
if (isset($hidefields[$sectionname.'.'.$id[0]]))
continue;
if (count($id) == 2)
$data[$name][$id[0]] = revert($id[1]);
else
$data[$name][$counter] = $id[0];
$counter++;
}
}
}
if ($cmd == 'mm')
{
$data2 = array();
$num = 0;
foreach ($data as $name => $values)
{
if (substr($name, 0, 5) !== 'STATS')
$data2[$name] = $values;
else
{
if (substr($values['ID'], 0, 4) == 'POOL')
continue;
$snum = substr($name, 5);
#
if (isset($values['Connector']))
$auc = $values['Connector'];
else
$auc = 'AUC';
$auc .= $snum;
#
foreach ($values as $nam => $val)
{
if (substr($nam, 0, 5) !== 'MM ID')
continue;
$mm = 'MM'.$num;
#
$data2[$mm] = array('MM' => $num,
'ID' => $values['ID'],
'MMID' => substr($nam,5),
'Connecter' => $auc);
#
$len = strlen($val);
$pos = 0;
while ($pos < $len)
{
while ($val[$pos] == ' ')
$pos++;
$brl = strpos($val, '[', $pos+1);
if ($brl === false)
break;
$brr = strpos($val, ']', $brl+1);
if ($brr === false)
break;
#
$subn = substr($val, $pos, $brl - $pos);
$data2[$mm][$subn] = substr($val, $brl+1, $brr-$brl-1);
#
$pos = $brr+1;
}
#
$num++;
}
}
}
return $data2;
}
return $data;
}
return null;
}
#
function getparam($name, $both = false)
{
$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 null;
// limit to 1K just to be safe
return substr($a, 0, 1024);
}
#
function newtable()
{
global $tablebegin, $rownum;
echo $tablebegin;
$rownum = 0;
}
#
function newrow()
{
echo '';
}
#
function othrow($row)
{
return " $row ";
}
#
function otherrow($row)
{
echo othrow($row);
}
#
function endrow()
{
global $rownum;
echo '';
$rownum++;
}
#
function endtable()
{
global $tableend;
echo $tableend;
}
#
function classlastshare($when, $alldata, $warnclass, $errorclass)
{
global $checklastshare;
if ($checklastshare === false)
return '';
if ($when == 0)
return '';
if (!isset($alldata['MHS av']))
return '';
if ($alldata['MHS av'] == 0)
return '';
if (!isset($alldata['Last Share Time']))
return '';
if (!isset($alldata['Last Share Difficulty']))
return '';
$expected = pow(2, 32) / ($alldata['MHS av'] * pow(10, 6));
// If the share difficulty changes while waiting on a share,
// this calculation will of course be incorrect
$expected *= $alldata['Last Share Difficulty'];
$howlong = $when - $alldata['Last Share Time'];
if ($howlong < 1)
$howlong = 1;
if ($howlong > ($expected * 12))
return $errorclass;
if ($howlong > ($expected * 8))
return $warnclass;
return '';
}
#
function endzero($num)
{
$rep = preg_replace('/0*$/', '', $num);
if ($rep === '')
$rep = '0';
return $rep;
}
#
function fmt($section, $name, $value, $when, $alldata, $cf = NULL)
{
global $dfmt, $rownum;
global $avatmaxmed, $avatmaxhi, $avatempmed, $avatemphi, $avaghsmmmin;
global $s9tempmed, $s9temphi, $s9chainmin, $s9acn;
if ($alldata == null)
$alldata = array();
$errorclass = 'err';
$warnclass = 'warn';
$lstclass = 'lst';
$hiclass = 'hi';
$loclass = 'lo';
$c2class = 'two';
$totclass = 'tot';
$b = ' ';
$class = '';
$nams = explode('.', $name);
if (count($nams) > 1)
$name = $nams[count($nams)-1];
$done = false;
if ($value === null)
{
$ret = $b;
$done = true;
}
else
if ($cf != NULL and function_exists($cf))
{
list($ret, $class) = $cf($section, $name, $value, $when, $alldata,
$warnclass, $errorclass, $hiclass, $loclass, $totclass);
if ($ret !== '')
$done = true;
}
if ($done === false)
{
$ret = $value;
/*
* To speed up the PHP, the case statement is just $name
* It used to be $section.'.'.$name
* If any names clash, the case code will need to check the value of
* $section to resolve the clash - as with 'Last Share Time' below
* If the code picks up a field that you wish to format differently,
* then you'll need a customsummarypage 'fmt' extension
* This also removes trailing numbers so multiple fields like in the
* S9 don't require many case lines
*/
switch (preg_replace('/\d*$/', '', $name))
{
case '0':
break;
case 'Last Share Time':
if ($section == 'total')
break;
if ($section == 'POOL')
{
if ($value == 0)
$ret = 'Never';
else
$ret = date('H:i:s d-M', $value);
}
else
{
if ($value == 0
|| (isset($alldata['Last Share Pool']) && $alldata['Last Share Pool'] == -1))
{
$ret = 'Never';
$class = $warnclass;
}
else
{
$ret = date('H:i:s', $value);
$class = classlastshare($when, $alldata, $warnclass, $errorclass);
}
}
break;
case 'Last getwork':
case 'Last Valid Work':
if ($section == 'total')
break;
if ($value == 0)
$ret = 'Never';
else
$ret = ($value - $when) . 's';
break;
case 'Last Share Pool':
if ($section == 'total')
break;
if ($value == -1)
{
$ret = 'None';
$class = $warnclass;
}
break;
case 'Elapsed':
case 'Device Elapsed':
if ($section == 'total')
break;
$s = $value % 60;
$value -= $s;
$value /= 60;
if ($value == 0)
$ret = $s.'s';
else
{
$m = $value % 60;
$value -= $m;
$value /= 60;
if ($value == 0)
$ret = sprintf("%dm$b%02ds", $m, $s);
else
{
$h = $value % 24;
$value -= $h;
$value /= 24;
if ($value == 0)
$ret = sprintf("%dh$b%02dm$b%02ds", $h, $m, $s);
else
{
if ($value == 1)
$days = '';
else
$days = 's';
$ret = sprintf("%dday$days$b%02dh$b%02dm$b%02ds", $value, $h, $m, $s);
}
}
}
break;
case 'Last Well':
if ($section == 'total')
break;
if ($value == '0')
{
$ret = 'Never';
$class = $warnclass;
}
else
$ret = date('H:i:s', $value);
break;
case 'Last Not Well':
if ($section == 'total')
break;
if ($value == '0')
$ret = 'Never';
else
{
$ret = date('H:i:s', $value);
$class = $errorclass;
}
break;
case 'Reason Not Well':
if ($section == 'total')
break;
if ($value != 'None')
$class = $errorclass;
break;
case 'Utility':
$ret = number_format($value, 2).'/m';
if ($value == 0)
$class = $errorclass;
else
if (isset($alldata['Difficulty Accepted'])
&& isset($alldata['Accepted'])
&& isset($alldata['MHS av'])
&& ($alldata['Difficulty Accepted'] > 0)
&& ($alldata['Accepted'] > 0))
{
$expected = 60 * $alldata['MHS av'] * (pow(10, 6) / pow(2, 32));
if ($expected == 0)
$expected = 0.000001; // 1 H/s
$da = $alldata['Difficulty Accepted'];
$a = $alldata['Accepted'];
$expected /= ($da / $a);
$ratio = $value / $expected;
if ($ratio < 0.9)
$class = $loclass;
else
if ($ratio > 1.1)
$class = $hiclass;
}
break;
case 'Work Utility':
$ret = number_format($value, 2).'/m';
break;
case 'Temperature':
if ($section == 'total')
break;
$ret = $value.'°C';
if (!isset($alldata['GPU']))
{
if ($value == 0)
$ret = ' ';
break;
}
case 'GPU Clock':
case 'Memory Clock':
case 'GPU Voltage':
case 'GPU Activity':
if ($section == 'total')
break;
if ($value == 0)
$class = $warnclass;
break;
case 'Fan Percent':
if ($section == 'total')
break;
if ($value == 0)
$class = $warnclass;
else
{
if ($value == 100)
$class = $errorclass;
else
if ($value > 85)
$class = $warnclass;
}
break;
case 'Fan Speed':
if ($section == 'total')
break;
if ($value == 0)
$class = $warnclass;
else
if (isset($alldata['Fan Percent']))
{
$test = $alldata['Fan Percent'];
if ($test == 100)
$class = $errorclass;
else
if ($test > 85)
$class = $warnclass;
}
break;
case 'MHS av':
case 'MHS 5s':
case 'MHS 1m':
case 'MHS 5m':
case 'MHS 15m':
$parts = explode('.', $value, 2);
if (count($parts) == 1)
$dec = '';
else
$dec = '.'.$parts[1];
$ret = number_format((float)$parts[0]).$dec;
if ($value == 0)
$class = $errorclass;
else
if (isset($alldata['Difficulty Accepted'])
&& isset($alldata['Accepted'])
&& isset($alldata['Utility'])
&& ($alldata['Difficulty Accepted'] > 0)
&& ($alldata['Accepted'] > 0))
{
$expected = 60 * $value * (pow(10, 6) / pow(2, 32));
if ($expected == 0)
$expected = 0.000001; // 1 H/s
$da = $alldata['Difficulty Accepted'];
$a = $alldata['Accepted'];
$expected /= ($da / $a);
$ratio = $alldata['Utility'] / $expected;
if ($ratio < 0.9)
$class = $hiclass;
else
if ($ratio > 1.1)
$class = $loclass;
}
break;
case 'Total MH':
case 'Getworks':
case 'Works':
case 'Accepted':
case 'Rejected':
case 'Local Work':
case 'Discarded':
case 'Diff1 Shares':
case 'Diff1 Work':
case 'Times Sent':
case 'Bytes Sent':
case 'Net Bytes Sent':
case 'Times Recv':
case 'Bytes Recv':
case 'Net Bytes Recv':
$parts = explode('.', $value, 2);
if (count($parts) == 1)
$dec = '';
else
$dec = '.'.$parts[1];
$ret = number_format((float)$parts[0]).$dec;
break;
case 'Hs':
case 'W':
case 'history_time':
case 'Pool Wait':
case 'Pool Max':
case 'Pool Min':
case 'Pool Av':
case 'Min Diff':
case 'Max Diff':
case 'Work Diff':
$parts = explode('.', $value, 2);
if (count($parts) == 1)
$dec = '';
else
$dec = '.'.endzero($parts[1]);
$ret = number_format((float)$parts[0]).$dec;
break;
case 'Status':
if ($section == 'total')
break;
if ($value != 'Alive')
$class = $errorclass;
break;
case 'Enabled':
if ($section == 'total')
break;
if ($value != 'Y')
$class = $warnclass;
break;
case 'No Device':
if ($section == 'total')
break;
if ($value != 'false')
$class = $errorclass;
break;
case 'When':
case 'Current Block Time':
if ($section == 'total')
break;
$ret = date($dfmt, $value);
break;
case 'Current Block Version':
if ($section == 'total')
break;
$ret = sprintf("0x%08x", $value);
break;
case 'Last Share Difficulty':
if ($section == 'total')
break;
case 'Difficulty Accepted':
case 'Difficulty Rejected':
case 'Difficulty Stale':
case 'Network Difficulty':
if ($value != '')
$ret = number_format((float)$value, 2);
break;
case 'Device Hardware%':
case 'Device Rejected%':
case 'Pool Rejected%':
case 'Pool Stale%':
if ($section == 'total')
break;
if ($value != '')
$ret = number_format((float)$value, 2) . '%';
break;
case 'Best Share':
if ($section == 'total')
break;
case 'Hardware Errors':
if ($value != '')
$ret = number_format((float)$value);
break;
// Ava specific stats fields
case 'GHSmm':
case 'AVATHS':
if ($value != '')
$ret = number_format((float)$value, 2);
if ($section != 'total')
{
if ($value == 0)
$class = $errorclass;
else
{
if ($name == 'GHSmm')
{
if ($value < $avaghsmmmin)
$class = $warnclass;
}
else
{
if ($value < ($avaghsmmmin/1000.0))
$class = $warnclass;
}
}
}
break;
case 'Temp':
if ($section == 'total')
break;
$ret = $value.'°C';
if ($section == 'MM')
{
if ($value == 0 || $value >= $avatemphi)
$class = $errorclass;
else
if ($value >= $avatempmed)
$class = $warnclass;
}
break;
case 'TMax':
if ($section == 'total')
break;
$ret = $value.'°C';
if ($value == 0 || $value >= $avatmaxhi)
$class = $errorclass;
else
if ($value >= $avatmaxmed)
$class = $warnclass;
break;
case 'PG':
if ($section == 'total')
break;
if ($value != '15')
{
if ($value > 10)
$class = $warnclass;
else
$class = $errorclass;
}
$ret = $value;
break;
case 'ECHU':
if ($section == 'total')
break;
$ret = $value;
$chk = str_replace(array(' ','0'), '', $value);
if ($chk != '')
{
$class = $warnclass;
$vals = explode(' ', $value);
$ret = '';
$sep = '';
foreach ($vals as $one)
{
if ($one == '0' || $one == '1')
$ret .= $sep.$one;
else
$ret .= $sep.'x'.dechex($one);
$sep = ' ';
}
}
$ret = str_replace(' ', ' ', $ret);
break;
case 'Vi':
case 'Vo':
if ($section == 'total')
break;
$ret = str_replace(' ', ' ', $value);
break;
case 'ECMM':
if ($section == 'total')
break;
if ($value != '0')
{
$class = $warnclass;
$ret = 'x'.dechex($value);
}
else
$ret = $value;
break;
case 'Ver':
if ($section == 'total')
break;
$ret = str_replace('-', '‑', $value);
break;
// S9 specific stats fields
case 'fan_num':
if ($section == 'total')
break;
$ret = $value;
if ($value < 1)
$class = $warnclass;
break;
case 'temp2_':
if ($section == 'total')
break;
$ret = $value.'°C';
if ($value == 0 || $value >= $s9temphi)
$class = $errorclass;
else
if ($value >= $s9tempmed)
$class = $warnclass;
break;
case 'chain_acn':
if ($section == 'total')
break;
$ret = $value;
if ($value != $s9acn)
$class = $warnclass;
break;
case 'chain_rate':
if ($value != '')
$ret = number_format((float)$value, 2);
if ($section != 'total')
{
if ($value == 0)
$class = $errorclass;
else
{
if ($value < $s9chainmin)
$class = $warnclass;
}
}
break;
case 'total_rate':
if ($value != '')
$ret = number_format((float)$value, 2);
if ($value == 0)
$class = $errorclass;
break;
// BUTTON.
case 'Rig':
case 'Pool':
case 'GPU':
break;
// Sample GEN fields
case 'Mined':
if ($value != '')
$ret = number_format((float)$value * 100.0, 3) . '%';
break;
case 'Acc':
case 'Rej':
if ($value != '')
$ret = number_format((float)$value * 100.0, 2) . '%';
break;
case 'GHS av':
case 'GHS 5m':
case 'GHS WU':
case 'GHS Acc':
case 'THS':
case 'THS av':
case 'THS 5m':
case 'THS WU':
case 'THS Acc':
if ($value != '')
$ret = number_format((float)$value, 2);
break;
case 'AvShr':
if ($section == 'total')
break;
if ($value != '')
$ret = number_format((float)$value, 2);
if ($value == 0)
$class = $warnclass;
break;
}
}
if ($section == 'NOTIFY' && substr($name, 0, 1) == '*' && $value != '0')
$class = $errorclass;
if ($class == '' && $section != 'POOL')
$class = classlastshare($when, $alldata, $lstclass, $lstclass);
if ($class == '' && $section == 'total')
$class = $totclass;
if ($class == '' && ($rownum % 2) == 0)
$class = $c2class;
if ($ret === '')
$ret = $b;
if ($class !== '')
$class = " class=$class";
return array($ret, $class);
}
#
global $poolcmd;
$poolcmd = array( 'Switch to' => 'switchpool',
'Enable' => 'enablepool',
'Disable' => 'disablepool',
'Remove' => 'removepool' );
#
function showhead($cmd, $values, $justnames = false)
{
global $poolcmd, $readonly;
newrow();
foreach ($values as $name => $value)
{
if ($name == '0' or $name == '')
$name = ' ';
echo "$name ";
}
if ($justnames === false && $cmd == 'pools' && $readonly === false)
foreach ($poolcmd as $name => $pcmd)
echo "$name ";
endrow();
}
#
function showdatetime()
{
global $dfmt;
otherrow('Date: '.date($dfmt).' ');
}
#
global $singlerigsum;
$singlerigsum = array(
'devs' => array('MHS av' => 1, 'MHS 5s' => 1, 'MHS 1m' => 1, 'MHS 5m' => 1,
'MHS 15m' => 1, 'Accepted' => 1, 'Rejected' => 1,
'Hardware Errors' => 1, 'Utility' => 1, 'Total MH' => 1,
'Diff1 Shares' => 1, 'Diff1 Work' => 1,
'Difficulty Accepted' => 1, 'Difficulty Rejected' => 1),
'pools' => array('Getworks' => 1, 'Accepted' => 1, 'Rejected' => 1, 'Discarded' => 1,
'Stale' => 1, 'Get Failures' => 1, 'Remote Failures' => 1,
'Diff1 Shares' => 1, 'Diff1 Work' => 1,
'Difficulty Accepted' => 1, 'Difficulty Rejected' => 1,
'Difficulty Stale' => 1),
'notify' => array('*' => 1));
#
function showtotal($total, $when, $oldvalues)
{
global $rigtotals;
list($showvalue, $class) = fmt('total', '', 'Total:', $when, null);
echo "$showvalue ";
$skipfirst = true;
foreach ($oldvalues as $name => $value)
{
if ($skipfirst === true)
{
$skipfirst = false;
continue;
}
if (isset($total[$name]))
$newvalue = $total[$name];
else
$newvalue = '';
list($showvalue, $class) = fmt('total', $name, $newvalue, $when, null);
echo "$showvalue ";
}
}
#
function details($cmd, $list, $rig)
{
global $dfmt, $poolcmd, $readonly, $showndate;
global $rownum, $rigtotals, $forcerigtotals, $singlerigsum;
$when = 0;
$stas = array('S' => 'Success', 'W' => 'Warning', 'I' => 'Informational', 'E' => 'Error', 'F' => 'Fatal');
newtable();
if ($showndate === false)
{
showdatetime();
endtable();
newtable();
$showndate = true;
}
if (isset($list['STATUS']))
{
newrow();
echo 'Computer: '.$list['STATUS']['Description'].' ';
if (isset($list['STATUS']['When']))
{
echo 'When: '.date($dfmt, $list['STATUS']['When']).' ';
$when = $list['STATUS']['When'];
}
$sta = $list['STATUS']['STATUS'];
echo 'Status: '.$stas[$sta].' ';
echo 'Message: '.$list['STATUS']['Msg'].' ';
endrow();
}
if ($rigtotals === true && isset($singlerigsum[$cmd]))
$dototal = $singlerigsum[$cmd];
else
$dototal = array();
$total = array();
$section = '';
$oldvalues = null;
foreach ($list as $item => $values)
{
if ($item == 'STATUS')
continue;
$sectionname = preg_replace('/\d/', '', $item);
// Handle 'devs' possibly containing >1 table
if ($sectionname != $section)
{
if ($oldvalues != null && count($total) > 0
&& ($rownum > 2 || $forcerigtotals === true))
showtotal($total, $when, $oldvalues);
endtable();
newtable();
showhead($cmd, $values);
$section = $sectionname;
}
newrow();
foreach ($values as $name => $value)
{
list($showvalue, $class) = fmt($section, $name, $value, $when, $values);
echo "$showvalue ";
if (isset($dototal[$name])
|| (isset($dototal['*']) and substr($name, 0, 1) == '*'))
{
if (isset($total[$name]))
$total[$name] += $value;
else
$total[$name] = $value;
}
}
if ($cmd == 'pools' && $readonly === false)
{
reset($values);
$pool = current($values);
foreach ($poolcmd as $name => $pcmd)
{
list($ignore, $class) = fmt('BUTTON', 'Pool', '', $when, $values);
echo "";
if ($pool === false)
echo ' ';
else
{
echo " ";
}
echo ' ';
}
}
endrow();
$oldvalues = $values;
}
if ($oldvalues != null && count($total) > 0
&& ($rownum > 2 || $forcerigtotals === true))
showtotal($total, $when, $oldvalues);
endtable();
}
#
global $devs;
$devs = null;
#
function gpubuttons($count, $rig)
{
global $devs;
$basic = array( 'GPU', 'Enable', 'Disable', 'Restart' );
$options = array( 'intensity' => 'Intensity',
'fan' => 'Fan Percent',
'engine' => 'GPU Clock',
'mem' => 'Memory Clock',
'vddc' => 'GPU Voltage' );
newtable();
newrow();
foreach ($basic as $head)
echo "$head ";
foreach ($options as $name => $des)
echo "$des ";
$n = 0;
for ($c = 0; $c < $count; $c++)
{
endrow();
newrow();
foreach ($basic as $name)
{
list($ignore, $class) = fmt('BUTTON', 'GPU', '', 0, null);
echo "";
if ($name == 'GPU')
echo $c;
else
{
echo " ";
}
echo ' ';
}
foreach ($options as $name => $des)
{
list($ignore, $class) = fmt('BUTTON', 'GPU', '', 0, null);
echo "";
if (!isset($devs["GPU$c"][$des]))
echo ' ';
else
{
$value = $devs["GPU$c"][$des];
echo " ";
echo " ";
$n++;
}
echo ' ';
}
}
endrow();
endtable();
}
#
function processgpus($rig)
{
global $error;
global $warnfont, $warnoff;
$gpus = api($rig, 'gpucount');
if ($error != null)
otherrow("Error getting GPU count: $warnfont$error$warnoff ");
else
{
if (!isset($gpus['GPUS']['Count']))
{
$rw = 'No GPU count returned: '.$warnfont;
$rw .= $gpus['STATUS']['STATUS'].' '.$gpus['STATUS']['Msg'];
$rw .= $warnoff.' ';
otherrow($rw);
}
else
{
$count = $gpus['GPUS']['Count'];
if ($count == 0)
otherrow('No GPUs ');
else
gpubuttons($count, $rig);
}
}
}
#
function showpoolinputs($rig, $ans)
{
global $readonly, $poolinputs;
if ($readonly === true || $poolinputs === false)
return;
newtable();
newrow();
$inps = array('Pool URL' => array('purl', 20),
'Worker Name' => array('pwork', 10),
'Worker Password' => array('ppass', 10));
$b = ' ';
echo " Add a pool: ";
foreach ($inps as $text => $name)
echo "$text: ";
echo " ";
endrow();
if (count($ans) > 1)
{
newrow();
echo ' Set pool priorities: ';
echo " Comma list of pool numbers: ";
echo " ";
endrow();
}
endtable();
}
#
function process($cmds, $rig)
{
global $error, $devs;
global $warnfont, $warnoff;
$count = count($cmds);
foreach ($cmds as $cmd => $des)
{
$process = api($rig, $cmd);
if ($error != null)
{
otherrow("Error getting $des: $warnfont$error$warnoff ");
break;
}
else
{
details($cmd, $process, $rig);
if ($cmd == 'devs')
$devs = $process;
if ($cmd == 'pools')
showpoolinputs($rig, $process);
# Not after the last one
if (--$count > 0)
otherrow(' ');
}
}
}
#
function rigname($rig, $rigname)
{
global $rigs, $rignames, $rigips;
if (isset($rigs[$rig]))
{
$parts = explode(':', $rigs[$rig], 3);
if (count($parts) == 3)
$rigname = $parts[2];
else
if ($rignames !== false)
{
switch ($rignames)
{
case 'ip':
if (isset($parts[0]) && isset($rigips[$parts[0]]))
{
$ip = explode('.', $rigips[$parts[0]]);
if (count($ip) == 4)
$rigname = intval($ip[3]);
}
break;
case 'ipx':
if (isset($parts[0]) && isset($rigips[$parts[0]]))
{
$ip = explode('.', $rigips[$parts[0]]);
if (count($ip) == 4)
$rigname = intval($ip[3], 16);
}
break;
}
}
}
return $rigname;
}
#
function riginput($rig, $rigname, $usebuttons)
{
$rigname = rigname($rig, $rigname);
if ($usebuttons === true)
return " ";
else
return "$rigname ";
}
#
function rigbutton($rig, $rigname, $when, $row, $usebuttons)
{
list($value, $class) = fmt('BUTTON', 'Rig', '', $when, $row);
if ($rig === '')
$ri = ' ';
else
$ri = riginput($rig, $rigname, $usebuttons);
return "$ri ";
}
#
function showrigs($anss, $headname, $rigname)
{
global $rigbuttons;
$dthead = array($headname => 1, 'STATUS' => 1, 'Description' => 1, 'When' => 1, 'API' => 1, 'CGMiner' => 1);
showhead('', $dthead);
foreach ($anss as $rig => $ans)
{
if ($ans == null)
continue;
newrow();
$when = 0;
if (isset($ans['STATUS']['When']))
$when = $ans['STATUS']['When'];
foreach ($ans as $item => $row)
{
if ($item != 'STATUS' && $item != 'VERSION')
continue;
foreach ($dthead as $name => $x)
{
if ($item == 'STATUS' && $name == $headname)
echo rigbutton($rig, $rigname.$rig, $when, null, $rigbuttons);
else
{
if (isset($row[$name]))
{
list($showvalue, $class) = fmt('STATUS', $name, $row[$name], $when, null);
echo "$showvalue ";
}
}
}
}
endrow();
}
}
#
function refreshbuttons()
{
global $ignorerefresh, $changerefresh, $autorefresh;
if ($ignorerefresh == false && $changerefresh == true)
{
echo ' ';
echo " ";
echo " ";
echo " ";
}
}
#
function pagebuttons($rig, $pg)
{
global $readonly, $rigs, $rigbuttons, $userlist, $ses;
global $allowcustompages, $customsummarypages;
if ($rig === null)
{
$prev = null;
$next = null;
if ($pg === null)
$refresh = '';
else
$refresh = "&pg=$pg";
}
else
{
switch (count($rigs))
{
case 0:
case 1:
$prev = null;
$next = null;
break;
case 2:
$prev = null;
$next = ($rig + 1) % count($rigs);
break;
default:
$prev = ($rig - 1) % count($rigs);
$next = ($rig + 1) % count($rigs);
break;
}
$refresh = "&rig=$rig";
}
echo ' ";
}
#
function doOne($rig, $preprocess)
{
global $haderror, $readonly, $notify, $rigs;
global $placebuttons;
if ($placebuttons == 'top' || $placebuttons == 'both')
pagebuttons($rig, null);
if ($preprocess != null)
process(array($preprocess => $preprocess), $rig);
$cmds = array( 'devs' => 'device list',
'summary' => 'summary information',
'pools' => 'pool list');
if ($notify)
$cmds['notify'] = 'device status';
$cmds['config'] = 'cgminer config';
process($cmds, $rig);
if ($haderror == false && $readonly === false)
processgpus($rig);
if ($placebuttons == 'bot' || $placebuttons == 'both')
pagebuttons($rig, null);
}
#
global $sectionmap;
# map sections to their api command
# DEVS is a special case that will match GPU, PGA or ASC
# so you can have a single table with both in it
# DATE and $ect are hard coded so not in here
$sectionmap = array(
'RIGS' => 'version',
'SUMMARY' => 'summary',
'POOL' => 'pools',
'DEVS' => 'devs',
'EDEVS' => 'edevs',
'GPU' => 'devs', // You would normally use DEVS
'PGA' => 'devs', // You would normally use DEVS
'ASC' => 'devs', // You would normally use DEVS
'NOTIFY' => 'notify',
'DEVDETAILS' => 'devdetails',
'STATS' => 'stats',
'ESTATS' => 'estats',
'CONFIG' => 'config',
'COIN' => 'coin',
'USBSTATS' => 'usbstats',
'MM' => 'mm');
#
function joinfields($section1, $section2, $join, $results)
{
global $sectionmap;
$name1 = $sectionmap[$section1];
$name2 = $sectionmap[$section2];
$newres = array();
// foreach rig in section1
foreach ($results[$name1] as $rig => $result)
{
$status = null;
// foreach answer section in the rig api call
foreach ($result as $name1b => $fields1b)
{
if ($name1b == 'STATUS')
{
// remember the STATUS from section1
$status = $result[$name1b];
continue;
}
// foreach answer section in the rig api call (for the other api command)
foreach ($results[$name2][$rig] as $name2b => $fields2b)
{
if ($name2b == 'STATUS')
continue;
// If match the same field values of fields in $join
$match = true;
foreach ($join as $field)
if ($fields1b[$field] != $fields2b[$field])
{
$match = false;
break;
}
if ($match === true)
{
if ($status != null)
{
$newres[$rig]['STATUS'] = $status;
$status = null;
}
$subsection = $section1.'+'.$section2;
$subsection .= preg_replace('/[^0-9]/', '', $name1b.$name2b);
foreach ($fields1b as $nam => $val)
$newres[$rig][$subsection]["$section1.$nam"] = $val;
foreach ($fields2b as $nam => $val)
$newres[$rig][$subsection]["$section2.$nam"] = $val;
}
}
}
}
return $newres;
}
#
function joinlr($section1, $section2, $join, $results)
{
global $sectionmap;
$name1 = $sectionmap[$section1];
$name2 = $sectionmap[$section2];
$newres = array();
// foreach rig in section1
foreach ($results[$name1] as $rig => $result)
{
$status = null;
// foreach answer section in the rig api call
foreach ($result as $name1b => $fields1b)
{
if ($name1b == 'STATUS')
{
// remember the STATUS from section1
$status = $result[$name1b];
continue;
}
// Build L string to be matched
// : means a string constant otherwise it's a field name
$Lval = '';
foreach ($join['L'] as $field)
{
if (substr($field, 0, 1) == ':')
$Lval .= substr($field, 1);
else
$Lval .= $fields1b[$field];
}
// foreach answer section in the rig api call (for the other api command)
foreach ($results[$name2][$rig] as $name2b => $fields2b)
{
if ($name2b == 'STATUS')
continue;
// Build R string and compare
// : means a string constant otherwise it's a field name
$Rval = '';
foreach ($join['R'] as $field)
{
if (substr($field, 0, 1) == ':')
$Rval .= substr($field, 1);
else
$Rval .= $fields2b[$field];
}
if ($Lval === $Rval)
{
if ($status != null)
{
$newres[$rig]['STATUS'] = $status;
$status = null;
}
$subsection = $section1.'+'.$section2;
$subsection .= preg_replace('/[^0-9]/', '', $name1b.$name2b);
foreach ($fields1b as $nam => $val)
$newres[$rig][$subsection]["$section1.$nam"] = $val;
foreach ($fields2b as $nam => $val)
$newres[$rig][$subsection]["$section2.$nam"] = $val;
}
}
}
}
return $newres;
}
#
function joinall($section1, $section2, $results)
{
global $sectionmap;
$name1 = $sectionmap[$section1];
$name2 = $sectionmap[$section2];
$newres = array();
// foreach rig in section1
foreach ($results[$name1] as $rig => $result)
{
// foreach answer section in the rig api call
foreach ($result as $name1b => $fields1b)
{
if ($name1b == 'STATUS')
{
// copy the STATUS from section1
$newres[$rig][$name1b] = $result[$name1b];
continue;
}
// foreach answer section in the rig api call (for the other api command)
foreach ($results[$name2][$rig] as $name2b => $fields2b)
{
if ($name2b == 'STATUS')
continue;
$subsection = $section1.'+'.$section2;
$subsection .= preg_replace('/[^0-9]/', '', $name1b.$name2b);
foreach ($fields1b as $nam => $val)
$newres[$rig][$subsection]["$section1.$nam"] = $val;
foreach ($fields2b as $nam => $val)
$newres[$rig][$subsection]["$section2.$nam"] = $val;
}
}
}
return $newres;
}
#
function joinsections($sections, $results, $errors)
{
global $sectionmap, $ect;
// GPU's don't have Name,ID fields - so create them
foreach ($results as $section => $res)
foreach ($res as $rig => $result)
foreach ($result as $name => $fields)
{
$subname = preg_replace('/\d/', '', $name);
if ($subname == 'GPU' and isset($result[$name]['GPU']))
{
$results[$section][$rig][$name]['Name'] = 'GPU';
$results[$section][$rig][$name]['ID'] = $result[$name]['GPU'];
}
}
foreach ($sections as $section => $fields)
{
$sec = preg_replace('/\d/', '', $section);
if ($sec != 'DATE' && $sec != $ect && !isset($sectionmap[$sec]))
{
$both = explode('+', $sec, 2);
if (count($both) > 1)
{
switch($both[0])
{
case 'SUMMARY':
switch($both[1])
{
case 'POOL':
case 'DEVS':
case 'EDEVS':
case 'CONFIG':
case 'COIN':
$sectionmap[$sec] = $sec;
$results[$sec] = joinall($both[0], $both[1], $results);
break;
default:
$errors[] = "Error: Invalid section '$section/$sec'";
break;
}
break;
case 'DEVS':
case 'EDEVS':
switch($both[1])
{
case 'NOTIFY':
case 'DEVDETAILS':
case 'USBSTATS':
$join = array('Name', 'ID');
$sectionmap[$sec] = $sec;
$results[$sec] = joinfields($both[0], $both[1], $join, $results);
break;
case 'STATS':
case 'ESTATS':
$join = array('L' => array('Name','ID'), 'R' => array('ID'));
$sectionmap[$sec] = $sec;
$results[$sec] = joinlr($both[0], $both[1], $join, $results);
break;
default:
$errors[] = "Error: Invalid section '$section/$sec'";
break;
}
break;
case 'POOL':
switch($both[1])
{
case 'STATS':
$join = array('L' => array(':POOL','POOL'), 'R' => array('ID'));
$sectionmap[$sec] = $sec;
$results[$sec] = joinlr($both[0], $both[1], $join, $results);
break;
default:
$errors[] = "Error: Invalid section '$section/$sec'";
break;
}
break;
default:
$errors[] = "Error: Invalid section '$section/$sec'";
break;
}
}
else
$errors[] = "Error: Invalid section '$section/$sec'";
}
}
return array($results, $errors);
}
#
function secmatch($section, $field)
{
$sec = preg_replace('/\d/', '', $section);
$fld = preg_replace('/\d/', '', $field);
if ($sec == $fld)
return true;
# API estats returns data as STATS
if ($sec == 'ESTATS' && $fld == 'STATS')
return true;
# API devs and edevs return the device type
if (($sec == 'DEVS' || $sec == 'EDEVS')
&& ($fld == 'GPU' || $fld == 'PGA' || $fld == 'ASC'))
return true;
return false;
}
#
function customset($showfields, $sum, $section, $rig, $isbutton, $result, $total, $cf = NULL)
{
global $rigbuttons;
$rn = 0;
foreach ($result as $sec => $row)
{
$secname = preg_replace('/\d/', '', $sec);
if ($sec != 'total')
if (!secmatch($section, $secname))
continue;
newrow();
$when = 0;
if (isset($result['STATUS']['When']))
$when = $result['STATUS']['When'];
if ($isbutton)
echo rigbutton($rig, $rig, $when, $row, $rigbuttons);
else
{
list($ignore, $class) = fmt('total', '', '', $when, $row, $cf);
echo "$rig ";
}
foreach ($showfields as $name => $one)
{
if ($name === '#' and $sec != 'total')
{
$rn++;
$value = $rn;
if (isset($total[$name]))
$total[$name]++;
else
$total[$name] = 1;
}
elseif (isset($row[$name]))
{
$value = $row[$name];
if (isset($sum[$section][$name]))
{
if (isset($total[$name]))
$total[$name] += $value;
else
$total[$name] = $value;
}
}
else
{
if ($sec == 'total' && isset($total[$name]))
$value = $total[$name];
else
$value = null;
}
if (strpos($secname, '+') === false)
list($showvalue, $class) = fmt($secname, $name, $value, $when, $row, $cf);
else
{
if ($name != '#')
$parts = explode('.', $name, 2);
else
$parts[0] = $parts[1] = '#';
list($showvalue, $class) = fmt($parts[0], $parts[1], $value, $when, $row, $cf);
}
echo "$showvalue ";
}
endrow();
}
return $total;
}
#
function docalc($func, $data)
{
switch ($func)
{
case 'sum':
$tot = 0;
foreach ($data as $val)
$tot += $val;
return $tot;
case 'avg':
$tot = 0;
foreach ($data as $val)
$tot += $val;
return ($tot / count($data));
case 'min':
$ans = null;
foreach ($data as $val)
if ($ans === null)
$ans = $val;
else
if ($val < $ans)
$ans = $val;
return $ans;
case 'max':
$ans = null;
foreach ($data as $val)
if ($ans === null)
$ans = $val;
else
if ($val > $ans)
$ans = $val;
return $ans;
case 'lo':
$ans = null;
foreach ($data as $val)
if ($ans === null)
$ans = $val;
else
if (strcasecmp($val, $ans) < 0)
$ans = $val;
return $ans;
case 'hi':
$ans = null;
foreach ($data as $val)
if ($ans === null)
$ans = $val;
else
if (strcasecmp($val, $ans) > 0)
$ans = $val;
return $ans;
case 'count':
return count($data);
case 'any':
default:
return $data[0];
}
}
#
function docompare($row, $test)
{
// invalid $test data means true
if (count($test) < 2)
return true;
if (isset($row[$test[0]]))
$val = $row[$test[0]];
else
$val = null;
if ($test[1] == 'set')
return ($val !== null);
if ($val === null || count($test) < 3)
return true;
switch($test[1])
{
case '=':
return ($val == $test[2]);
case '<':
return ($val < $test[2]);
case '<=':
return ($val <= $test[2]);
case '>':
return ($val > $test[2]);
case '>=':
return ($val >= $test[2]);
case 'eq':
return (strcasecmp($val, $test[2]) == 0);
case 'lt':
return (strcasecmp($val, $test[2]) < 0);
case 'le':
return (strcasecmp($val, $test[2]) <= 0);
case 'gt':
return (strcasecmp($val, $test[2]) > 0);
case 'ge':
return (strcasecmp($val, $test[2]) >= 0);
case 'sub':
return (strcasecmp(substr($val, 0, strlen($test[2])), $test[2]) == 0);
case '!sub':
return (strcasecmp(substr($val, 0, strlen($test[2])), $test[2]) != 0);
default:
return true;
}
}
#
function processcompare($which, $ext, $section, $res)
{
if (isset($ext[$section][$which]))
{
$proc = $ext[$section][$which];
if ($proc !== null)
{
$res2 = array();
foreach ($res as $rig => $result)
foreach ($result as $sec => $row)
{
$secname = preg_replace('/\d/', '', $sec);
if (!secmatch($section, $secname))
$res2[$rig][$sec] = $row;
else
{
$keep = true;
foreach ($proc as $test)
if (!docompare($row, $test))
{
$keep = false;
break;
}
if ($keep)
$res2[$rig][$sec] = $row;
}
}
$res = $res2;
}
}
return $res;
}
#
function ss($a, $b)
{
$la = strlen($a);
$lb = strlen($b);
if ($la != $lb)
return $lb - $la;
return strcmp($a, $b);
}
#
# If you are developing a customsummarypage that uses BGEN or GEN,
# you may want to remove the '@' in front of '@eval()' to help with debugging
# The '@' removes php comments from the web log about missing fields
# Since there are many forks of cgminer that break the API or do not
# keep their fork up to date with current cgminer, the addition of
# '@' solves the problem of generating unnecessary and excessive web logs
# about the eval()
function genfld($row, $calc)
{
uksort($row, "ss");
foreach ($row as $name => $value)
if (strstr($calc, $name) !== FALSE)
$calc = str_replace($name, $value, $calc);
@eval("\$val = $calc;");
if (!isset($val))
return '';
else
return $val;
}
#
function dogen($ext, $wg, $gname, $section, &$res, &$fields)
{
$gen = $ext[$section][$wg];
foreach ($gen as $fld => $calc)
$fields[] = "$gname.$fld";
foreach ($res as $rig => $result)
foreach ($result as $sec => $row)
{
$secname = preg_replace('/\d/', '', $sec);
if (secmatch($section, $secname))
foreach ($gen as $fld => $calc)
{
$name = "$gname.$fld";
$val = genfld($row, $calc);
$res[$rig][$sec][$name] = $val;
}
}
}
# $section is the full name including digits
function processext($ext, $section, $res, &$fields)
{
global $allowgen;
$res = processcompare('where', $ext, $section, $res);
// Generated fields (functions of other fields before grouping)
if ($allowgen === true && isset($ext[$section]['bgen']))
dogen($ext, 'bgen', 'BGEN', $section, $res, $fields);
if (isset($ext[$section]['group']))
{
$grp = $ext[$section]['group'];
$calc = $ext[$section]['calc'];
if ($grp !== null)
{
$interim = array();
$res2 = array();
$cou = 0;
foreach ($res as $rig => $result)
foreach ($result as $sec => $row)
{
$secname = preg_replace('/\d/', '', $sec);
if (!secmatch($section, $secname))
{
// STATUS may be problematic ...
if (!isset($res2[$sec]))
$res2[$sec] = $row;
}
else
{
$grpkey = '';
$newrow = array();
foreach ($grp as $field)
{
if (isset($row[$field]))
{
$grpkey .= $row[$field].'.';
$newrow[$field] = $row[$field];
}
else
$grpkey .= '.';
}
if (!isset($interim[$grpkey]))
{
$interim[$grpkey]['grp'] = $newrow;
$interim[$grpkey]['sec'] = $secname.$cou;
$cou++;
}
if ($calc !== null)
foreach ($calc as $field => $func)
{
if (isset($row[$field]))
{
if (!isset($interim[$grpkey]['cal'][$field]))
$interim[$grpkey]['cal'][$field] = array();
$interim[$grpkey]['cal'][$field][] = $row[$field];
}
}
}
}
// Build the rest of $res2 from $interim
foreach ($interim as $rowkey => $row)
{
$key = $row['sec'];
foreach ($row['grp'] as $field => $value)
$res2[$key][$field] = $value;
foreach ($row['cal'] as $field => $data)
$res2[$key][$field] = docalc($calc[$field], $data);
}
$res = array('' => $res2);
}
}
// Generated fields (functions of other fields after grouping)
if ($allowgen === true && isset($ext[$section]['gen']))
dogen($ext, 'gen', 'GEN', $section, $res, $fields);
return processcompare('having', $ext, $section, $res);
}
# Avalon Error values in ECMM and ECHU
function showect()
{
$ecv = array(
'x00001' => 'Idle W',
'x00002' => 'MMCRCFailed W',
'x00004' => 'NoFan F',
'x00008' => 'Lock W',
'x00010' => 'APIfifoOverflow W',
'x00020' => 'RBOverflow W',
'x00040' => 'TooHot F',
'x00080' => 'HotBefore W',
'x00100' => 'LoopFailed F',
'x00200' => 'CoreTestFailed W',
'x00400' => 'InvalidPMU F',
'x00800' => 'PGFailed F',
'x01000' => 'NTCErr F',
'x02000' => 'VolErr F',
'x04000' => 'VCoreERR F',
'x08000' => 'PMUCRCFailed W',
'x10000' => 'InvalidPLLValue W',
'x20000' => 'HUFailed W');
otherrow("ECHU and ECMM error codes (mouse over) ");
$count = 0;
foreach ($ecv as $val => $name)
{
if (($count % 9) == 0)
{
if ($count > 0)
{
endrow();
}
newrow();
}
switch (substr($name, -2))
{
case ' W':
$name .= 'arning';
break;
case ' F':
$name .= 'atal';
break;
}
echo " $val ";
$count++;
}
endrow();
}
#
function processcustompage($pagename, $sections, $sum, $ext, $namemap)
{
global $sectionmap, $ect;
global $miner, $port;
global $rigs, $error;
global $warnfont, $warnoff;
global $dfmt;
global $readonly, $showndate;
$cmds = array();
$errors = array();
foreach ($sections as $section => $fields)
{
$all = explode('+', $section);
foreach ($all as $section)
{
$sec = preg_replace('/\d/', '', $section);
if (isset($sectionmap[$sec]))
{
$cmd = $sectionmap[$sec];
if (!isset($cmds[$cmd]))
$cmds[$cmd] = 1;
}
else
if ($sec != 'DATE' && $sec != $ect)
$errors[] = "Error: unknown section '$section/$sec' in custom summary page '$pagename'";
}
}
$results = array();
foreach ($rigs as $num => $rig)
{
$parts = explode(':', $rig, 3);
if (count($parts) >= 1)
{
$miner = $parts[0];
if (count($parts) >= 2)
$port = $parts[1];
else
$port = '';
if (count($parts) > 2)
$name = $parts[2];
else
$name = $rig;
foreach ($cmds as $cmd => $one)
{
$process = api($name, $cmd);
if ($error != null)
{
$errors[] = "Error getting $cmd for $name $warnfont$error$warnoff";
break;
}
else
$results[$cmd][$num] = $process;
}
}
else
otherrow('Bad "$rigs" array ');
}
// Show API errors at the top
if (count($errors) > 0)
{
foreach ($errors as $err)
otherrow("$err ");
$errors = array();
}
$shownsomething = false;
if (count($results) > 0)
{
list($results, $errors) = joinsections($sections, $results, $errors);
$first = true;
foreach ($sections as $section => $fields)
{
$sec = preg_replace('/\d/', '', $section);
if ($sec === 'DATE')
{
if ($shownsomething)
otherrow(' ');
newtable();
showdatetime();
endtable();
// On top of the next table
$shownsomething = false;
continue;
}
if ($sec === $ect)
{
newtable();
showect();
endtable();
// On top of the next table
$shownsomething = false;
continue;
}
if ($sec === 'RIGS')
{
if ($shownsomething)
otherrow(' ');
newtable();
showrigs($results['version'], 'Rig', '');
endtable();
$shownsomething = true;
continue;
}
if (isset($results[$sectionmap[$sec]]))
{
// use the full section name
if (isset($ext[$section]['fmt']))
$cf = $ext[$section]['fmt'];
else
$cf = NULL;
$rigresults = processext($ext, $section, $results[$sectionmap[$sec]], $fields);
$showfields = array();
$showhead = array();
foreach ($fields as $field)
foreach ($rigresults as $result)
foreach ($result as $secn => $row)
{
$secname = preg_replace('/\d/', '', $secn);
if (secmatch($section, $secname))
{
if ($field === '*')
{
foreach ($row as $f => $v)
{
$showfields[$f] = 1;
$map = $section.'.'.$f;
if (isset($namemap[$map]))
$showhead[$namemap[$map]] = 1;
else
$showhead[$f] = 1;
}
}
elseif ($field === '#')
{
$showfields[$field] = 1;
$showhead[$field] = 1;
}
elseif (isset($row[$field]))
{
$showfields[$field] = 1;
$map = $section.'.'.$field;
if (isset($namemap[$map]))
$showhead[$namemap[$map]] = 1;
else
$showhead[$field] = 1;
}
}
}
if (count($showfields) > 0)
{
if ($shownsomething)
otherrow(' ');
newtable();
if (count($rigresults) == 1 && isset($rigresults['']))
$ri = array('' => 1) + $showhead;
else
$ri = array('Rig' => 1) + $showhead;
showhead('', $ri, true);
$total = array();
$add = array('total' => array());
foreach ($rigresults as $num => $result)
$total = customset($showfields, $sum, $section, $num, true, $result, $total, $cf);
if (count($total) > 0)
customset($showfields, $sum, $section, 'Σ', false, $add, $total, $cf);
$first = false;
endtable();
$shownsomething = true;
}
}
}
}
if (count($errors) > 0)
{
if (count($results) > 0)
otherrow(' ');
foreach ($errors as $err)
otherrow("$err ");
}
}
#
function showcustompage($pagename, $systempage = false)
{
global $customsummarypages;
global $placebuttons;
if ($placebuttons == 'top' || $placebuttons == 'both')
pagebuttons(null, $pagename);
if ($systempage === false && !isset($customsummarypages[$pagename]))
{
otherrow("Unknown custom summary page '$pagename' ");
return;
}
$csp = getcsp($pagename, $systempage);
if ($csp === false)
{
otherrow("Invalid custom summary page '$pagename' ");
return;
}
degen($csp);
$page = $csp[0];
$namemap = array();
foreach ($page as $name => $fields)
{
if ($fields === null)
$page[$name] = array();
else
foreach ($fields as $num => $field)
{
$pos = strpos($field, '=');
if ($pos !== false)
{
$names = explode('=', $field, 2);
if (strlen($names[1]) > 0)
$namemap[$name.'.'.$names[0]] = $names[1];
$page[$name][$num] = $names[0];
}
}
}
$ext = null;
if (isset($csp[2]))
$ext = $csp[2];
$sum = $csp[1];
if ($sum === null)
$sum = array();
// convert them to searchable via isset()
foreach ($sum as $section => $fields)
{
$newfields = array();
foreach ($fields as $field)
$newfields[$field] = 1;
$sum[$section] = $newfields;
}
if (count($page) <= 1)
{
otherrow("Invalid custom summary page '$pagename' no content ");
return;
}
processcustompage($pagename, $page, $sum, $ext, $namemap);
if ($placebuttons == 'bot' || $placebuttons == 'both')
pagebuttons(null, $pagename);
}
#
function onlylogin()
{
global $here;
htmlhead('', false, null, null, true);
?>
No rigs $action");
return;
}
else
{
if ($mcast === true && count($rigs) < $mcastexpect)
$mcerr = othrow('Found '.count($rigs)." rigs but expected at least $mcastexpect ");
}
if ($ignorerefresh == false)
{
$ref = trim(getparam('ref', true));
if ($ref != null && $ref != '')
$autorefresh = intval($ref);
}
if ($pagesonly !== true)
{
$rig = trim(getparam('rig', true));
$arg = trim(getparam('arg', true));
$preprocess = null;
if ($arg != null and $arg != '')
{
if ($rig != null and $rig != '' and $rig >= 0 and $rig < count($rigs))
{
$parts = explode(':', $rigs[$rig], 3);
if (count($parts) >= 1)
{
$miner = $parts[0];
if (count($parts) >= 2)
$port = $parts[1];
else
$port = '';
if ($readonly !== true)
$preprocess = $arg;
}
}
}
}
if ($allowcustompages === true)
{
$pg = urlencode(trim(getparam('pg', true)));
if ($pagesonly === true)
{
if ($pg !== null && $pg !== '')
{
if ($userlist !== null && isset($userlist['def'])
&& !in_array($pg, $userlist['def']))
$pg = null;
}
else
{
if ($userlist !== null && isset($userlist['def']))
foreach ($userlist['def'] as $pglook)
if (getcsp($pglook) !== false)
{
$pg = $pglook;
break;
}
}
}
if ($pg !== null && $pg !== '')
{
htmlhead($mcerr, false, null, $pg);
showcustompage($pg);
return;
}
}
if ($pagesonly === true)
{
onlylogin();
return;
}
if (count($rigs) == 1)
{
$parts = explode(':', $rigs[0], 3);
if (count($parts) >= 1)
{
$miner = $parts[0];
if (count($parts) >= 2)
$port = $parts[1];
else
$port = '';
htmlhead($mcerr, true, 0);
doOne(0, $preprocess);
}
else
{
minhead($mcerr);
otherrow('Invalid "$rigs" array ');
}
return;
}
if ($rig != null and $rig != '' and $rig >= 0 and $rig < count($rigs))
{
$parts = explode(':', $rigs[$rig], 3);
if (count($parts) >= 1)
{
$miner = $parts[0];
if (count($parts) >= 2)
$port = $parts[1];
else
$port = '';
htmlhead($mcerr, true, 0);
doOne($rig, $preprocess);
}
else
{
minhead($mcerr);
otherrow('Invalid "$rigs" array ');
}
return;
}
htmlhead($mcerr, false, null);
if ($preprocess != null)
process(array($preprocess => $preprocess), $rig);
if (getcsp('Summary', true) !== false)
showcustompage('Summary', true);
}
#
if ($mcast === true)
getrigs();
display();
#
?>