PageRenderTime 66ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/miner.php

https://github.com/bkkcoins/cgminer-klondike
PHP | 2750 lines | 2292 code | 279 blank | 179 comment | 513 complexity | a95946abbb43fb51168419987f6fdd9b MD5 | raw file
Possible License(s): LGPL-2.1, JSON, GPL-3.0, LGPL-3.0, GPL-2.0
  1. <?php
  2. session_start();
  3. #
  4. global $title, $miner, $port, $readonly, $notify, $rigs;
  5. global $rigipsecurity, $rigtotals, $forcerigtotals;
  6. global $socksndtimeoutsec, $sockrcvtimeoutsec;
  7. global $checklastshare, $poolinputs, $hidefields;
  8. global $ignorerefresh, $changerefresh, $autorefresh;
  9. global $allowcustompages, $customsummarypages;
  10. global $miner_font_family, $miner_font_size;
  11. global $colouroverride, $placebuttons, $userlist;
  12. #
  13. # See API-README for more details of these variables and how
  14. # to configure miner.php
  15. #
  16. # Web page title
  17. $title = 'Mine';
  18. #
  19. # Set $readonly to true to force miner.php to be readonly
  20. # Set $readonly to false then it will check cgminer 'privileged'
  21. $readonly = false;
  22. #
  23. # Set $userlist to null to allow anyone access or read API-README
  24. $userlist = null;
  25. #
  26. # Set $notify to false to NOT attempt to display the notify command
  27. # Set $notify to true to attempt to display the notify command
  28. $notify = true;
  29. #
  30. # Set $checklastshare to true to do the following checks:
  31. # If a device's last share is 12x expected ago then display as an error
  32. # If a device's last share is 8x expected ago then display as a warning
  33. # If either of the above is true, also display the whole line highlighted
  34. # This assumes shares are 1 difficulty shares
  35. $checklastshare = true;
  36. #
  37. # Set $poolinputs to true to show the input fields for adding a pool
  38. # and changing the pool priorities
  39. # N.B. also if $readonly is true, it will not display the fields
  40. $poolinputs = false;
  41. #
  42. # Set $rigs to an array of your cgminer rigs that are running
  43. # format: 'IP:Port' or 'Host:Port' or 'Host:Port:Name'
  44. $rigs = array('127.0.0.1:4028');
  45. #
  46. # Set $rigipsecurity to false to show the IP/Port of the rig
  47. # in the socket error messages and also show the full socket message
  48. $rigipsecurity = true;
  49. #
  50. # Set $rigtotals to true to display totals on the single rig page
  51. # 'false' means no totals (and ignores $forcerigtotals)
  52. # You can force it to always show rig totals when there is only
  53. # one line by setting $forcerigtotals = true;
  54. $rigtotals = true;
  55. $forcerigtotals = false;
  56. #
  57. # These should be OK for most cases
  58. $socksndtimeoutsec = 10;
  59. $sockrcvtimeoutsec = 40;
  60. #
  61. # List of fields NOT to be displayed
  62. # This example would hide the slightly more sensitive pool information
  63. #$hidefields = array('POOL.URL' => 1, 'POOL.User' => 1);
  64. $hidefields = array();
  65. #
  66. # Auto-refresh of the page (in seconds) - integers only
  67. # $ignorerefresh = true/false always ignore refresh parameters
  68. # $changerefresh = true/false show buttons to change the value
  69. # $autorefresh = default value, 0 means dont auto-refresh
  70. $ignorerefresh = false;
  71. $changerefresh = true;
  72. $autorefresh = 0;
  73. #
  74. # Should we allow custom pages?
  75. # (or just completely ignore them and don't display the buttons)
  76. $allowcustompages = true;
  77. #
  78. # OK this is a bit more complex item: Custom Summary Pages
  79. # As mentioned above, see API-README
  80. # see the example below (if there is no matching data, no total will show)
  81. $mobilepage = array(
  82. 'DATE' => null,
  83. 'RIGS' => null,
  84. 'SUMMARY' => array('Elapsed', 'MHS av', 'Found Blocks=Blks', 'Accepted', 'Rejected=Rej', 'Utility'),
  85. 'DEVS+NOTIFY' => array('DEVS.Name=Name', 'DEVS.ID=ID', 'DEVS.Status=Status', 'DEVS.Temperature=Temp',
  86. 'DEVS.MHS av=MHS av', 'DEVS.Accepted=Accept', 'DEVS.Rejected=Rej',
  87. 'DEVS.Utility=Utility', 'NOTIFY.Last Not Well=Not Well'),
  88. 'POOL' => array('POOL', 'Status', 'Accepted', 'Rejected=Rej', 'Last Share Time'));
  89. $mobilesum = array(
  90. 'SUMMARY' => array('MHS av', 'Found Blocks', 'Accepted', 'Rejected', 'Utility'),
  91. 'DEVS+NOTIFY' => array('DEVS.MHS av', 'DEVS.Accepted', 'DEVS.Rejected', 'DEVS.Utility'),
  92. 'POOL' => array('Accepted', 'Rejected'));
  93. #
  94. $statspage = array(
  95. 'DATE' => null,
  96. 'RIGS' => null,
  97. 'SUMMARY' => array('Elapsed', 'MHS av', 'Found Blocks=Blks',
  98. 'Accepted', 'Rejected=Rej', 'Utility',
  99. 'Hardware Errors=HW Errs', 'Network Blocks=Net Blks',
  100. 'Work Utility'),
  101. 'COIN' => array('*'),
  102. 'STATS' => array('*'));
  103. #
  104. $statssum = array(
  105. 'SUMMARY' => array('MHS av', 'Found Blocks', 'Accepted',
  106. 'Rejected', 'Utility', 'Hardware Errors',
  107. 'Work Utility'));
  108. #
  109. $poolspage = array(
  110. 'DATE' => null,
  111. 'RIGS' => null,
  112. 'SUMMARY' => array('Elapsed', 'MHS av', 'Found Blocks=Blks', 'Accepted', 'Rejected=Rej',
  113. 'Utility', 'Hardware Errors=HW Errs', 'Network Blocks=Net Blks',
  114. 'Work Utility'),
  115. 'POOL+STATS' => array('STATS.ID=ID', 'POOL.URL=URL', 'POOL.Difficulty Accepted=Diff Acc',
  116. 'POOL.Difficulty Rejected=Diff Rej',
  117. 'POOL.Has Stratum=Stratum', 'POOL.Stratum Active=StrAct',
  118. 'POOL.Has GBT=GBT', 'STATS.Times Sent=TSent',
  119. 'STATS.Bytes Sent=BSent', 'STATS.Net Bytes Sent=NSent',
  120. 'STATS.Times Recv=TRecv', 'STATS.Bytes Recv=BRecv',
  121. 'STATS.Net Bytes Recv=NRecv'));
  122. #
  123. $poolssum = array(
  124. 'SUMMARY' => array('MHS av', 'Found Blocks', 'Accepted',
  125. 'Rejected', 'Utility', 'Hardware Errors',
  126. 'Work Utility'),
  127. 'POOL+STATS' => array('POOL.Difficulty Accepted', 'POOL.Difficulty Rejected',
  128. 'STATS.Times Sent', 'STATS.Bytes Sent', 'STATS.Net Bytes Sent',
  129. 'STATS.Times Recv', 'STATS.Bytes Recv', 'STATS.Net Bytes Recv'));
  130. #
  131. $poolsext = array(
  132. 'POOL+STATS' => array(
  133. 'where' => null,
  134. 'group' => array('POOL.URL', 'POOL.Has Stratum', 'POOL.Stratum Active', 'POOL.Has GBT'),
  135. 'calc' => array('POOL.Difficulty Accepted' => 'sum', 'POOL.Difficulty Rejected' => 'sum',
  136. 'STATS.Times Sent' => 'sum', 'STATS.Bytes Sent' => 'sum',
  137. 'STATS.Net Bytes Sent' => 'sum', 'STATS.Times Recv' => 'sum',
  138. 'STATS.Bytes Recv' => 'sum', 'STATS.Net Bytes Recv' => 'sum'),
  139. 'having' => array(array('STATS.Bytes Recv', '>', 0)))
  140. );
  141. #
  142. # customsummarypages is an array of these Custom Summary Pages
  143. $customsummarypages = array('Mobile' => array($mobilepage, $mobilesum),
  144. 'Stats' => array($statspage, $statssum),
  145. 'Pools' => array($poolspage, $poolssum, $poolsext));
  146. #
  147. $here = $_SERVER['PHP_SELF'];
  148. #
  149. global $tablebegin, $tableend, $warnfont, $warnoff, $dfmt;
  150. #
  151. $tablebegin = '<tr><td><table border=1 cellpadding=5 cellspacing=0>';
  152. $tableend = '</table></td></tr>';
  153. $warnfont = '<font color=red><b>';
  154. $warnoff = '</b></font>';
  155. $dfmt = 'H:i:s j-M-Y \U\T\CP';
  156. #
  157. $miner_font_family = 'verdana,arial,sans';
  158. $miner_font_size = '13pt';
  159. #
  160. # Edit this or redefine it in myminer.php to change the colour scheme
  161. # See $colourtable below for the list of names
  162. $colouroverride = array();
  163. #
  164. # Where to place the buttons: 'top' 'bot' 'both'
  165. # anything else means don't show them - case sensitive
  166. $placebuttons = 'top';
  167. #
  168. # This below allows you to put your own settings into a seperate file
  169. # so you don't need to update miner.php with your preferred settings
  170. # every time a new version is released
  171. # Just create the file 'myminer.php' in the same directory as
  172. # 'miner.php' - and put your own settings in there
  173. if (file_exists('myminer.php'))
  174. include_once('myminer.php');
  175. #
  176. # This is the system default that must always contain all necessary
  177. # colours so it must be a constant
  178. # You can override these values with $colouroverride
  179. # The only one missing is in $warnfont
  180. # - which you can override directly anyway
  181. global $colourtable;
  182. $colourtable = array(
  183. 'body bgcolor' => '#ecffff',
  184. 'td color' => 'blue',
  185. 'td.two color' => 'blue',
  186. 'td.two background' => '#ecffff',
  187. 'td.h color' => 'blue',
  188. 'td.h background' => '#c4ffff',
  189. 'td.err color' => 'black',
  190. 'td.err background' => '#ff3050',
  191. 'td.warn color' => 'black',
  192. 'td.warn background' => '#ffb050',
  193. 'td.sta color' => 'green',
  194. 'td.tot color' => 'blue',
  195. 'td.tot background' => '#fff8f2',
  196. 'td.lst color' => 'blue',
  197. 'td.lst background' => '#ffffdd',
  198. 'td.hi color' => 'blue',
  199. 'td.hi background' => '#f6ffff',
  200. 'td.lo color' => 'blue',
  201. 'td.lo background' => '#deffff'
  202. );
  203. #
  204. # Don't touch these 2
  205. $miner = null;
  206. $port = null;
  207. #
  208. # Ensure it is only ever shown once
  209. global $showndate;
  210. $showndate = false;
  211. #
  212. # For summary page to stop retrying failed rigs
  213. global $rigerror;
  214. $rigerror = array();
  215. #
  216. global $rownum;
  217. $rownum = 0;
  218. #
  219. // Login
  220. global $ses;
  221. $ses = 'rutroh';
  222. #
  223. function getcss($cssname, $dom = false)
  224. {
  225. global $colourtable, $colouroverride;
  226. $css = '';
  227. foreach ($colourtable as $cssdata => $value)
  228. {
  229. $cssobj = explode(' ', $cssdata, 2);
  230. if ($cssobj[0] == $cssname)
  231. {
  232. if (isset($colouroverride[$cssdata]))
  233. $value = $colouroverride[$cssdata];
  234. if ($dom == true)
  235. $css .= ' '.$cssobj[1].'='.$value;
  236. else
  237. $css .= $cssobj[1].':'.$value.'; ';
  238. }
  239. }
  240. return $css;
  241. }
  242. #
  243. function getdom($domname)
  244. {
  245. return getcss($domname, true);
  246. }
  247. #
  248. function htmlhead($checkapi, $rig, $pg = null, $noscript = false)
  249. {
  250. global $title, $miner_font_family, $miner_font_size;
  251. global $error, $readonly, $poolinputs, $here;
  252. global $ignorerefresh, $autorefresh;
  253. $extraparams = '';
  254. if ($rig != null && $rig != '')
  255. $extraparams = "&rig=$rig";
  256. else
  257. if ($pg != null && $pg != '')
  258. $extraparams = "&pg=$pg";
  259. if ($ignorerefresh == true || $autorefresh == 0)
  260. $refreshmeta = '';
  261. else
  262. {
  263. $url = "$here?ref=$autorefresh$extraparams";
  264. $refreshmeta = "\n<meta http-equiv='refresh' content='$autorefresh;url=$url'>";
  265. }
  266. if ($readonly === false && $checkapi === true)
  267. {
  268. $error = null;
  269. $access = api($rig, 'privileged');
  270. if ($error != null
  271. || !isset($access['STATUS']['STATUS'])
  272. || $access['STATUS']['STATUS'] != 'S')
  273. $readonly = true;
  274. }
  275. $miner_font = "font-family:$miner_font_family; font-size:$miner_font_size;";
  276. echo "<html><head>$refreshmeta
  277. <title>$title</title>
  278. <style type='text/css'>
  279. td { $miner_font ".getcss('td')."}
  280. td.two { $miner_font ".getcss('td.two')."}
  281. td.h { $miner_font ".getcss('td.h')."}
  282. td.err { $miner_font ".getcss('td.err')."}
  283. td.warn { $miner_font ".getcss('td.warn')."}
  284. td.sta { $miner_font ".getcss('td.sta')."}
  285. td.tot { $miner_font ".getcss('td.tot')."}
  286. td.lst { $miner_font ".getcss('td.lst')."}
  287. td.hi { $miner_font ".getcss('td.hi')."}
  288. td.lo { $miner_font ".getcss('td.lo')."}
  289. </style>
  290. </head><body".getdom('body').">\n";
  291. if ($noscript === false)
  292. {
  293. echo "<script type='text/javascript'>
  294. function pr(a,m){if(m!=null){if(!confirm(m+'?'))return}window.location='$here?ref=$autorefresh'+a}\n";
  295. if ($ignorerefresh == false)
  296. echo "function prr(a){if(a){v=document.getElementById('refval').value}else{v=0}window.location='$here?ref='+v+'$extraparams'}\n";
  297. if ($readonly === false && $checkapi === true)
  298. {
  299. echo "function prc(a,m){pr('&arg='+a,m)}
  300. function prs(a,r){var c=a.substr(3);var z=c.split('|',2);var m=z[0].substr(0,1).toUpperCase()+z[0].substr(1)+' GPU '+z[1];prc(a+'&rig='+r,m)}
  301. function prs2(a,n,r){var v=document.getElementById('gi'+n).value;var c=a.substr(3);var z=c.split('|',2);var m='Set GPU '+z[1]+' '+z[0].substr(0,1).toUpperCase()+z[0].substr(1)+' to '+v;prc(a+','+v+'&rig='+r,m)}\n";
  302. if ($poolinputs === true)
  303. echo "function cbs(s){var t=s.replace(/\\\\/g,'\\\\\\\\'); return t.replace(/,/g, '\\\\,')}\nfunction pla(r){var u=document.getElementById('purl').value;var w=document.getElementById('pwork').value;var p=document.getElementById('ppass').value;pr('&rig='+r+'&arg=addpool|'+cbs(u)+','+cbs(w)+','+cbs(p),'Add Pool '+u)}\nfunction psp(r){var p=document.getElementById('prio').value;pr('&rig='+r+'&arg=poolpriority|'+p,'Set Pool Priorities to '+p)}\n";
  304. }
  305. echo "</script>\n";
  306. }
  307. ?>
  308. <table width=100% height=100% border=0 cellpadding=0 cellspacing=0 summary='Mine'>
  309. <tr><td align=center valign=top>
  310. <table border=0 cellpadding=4 cellspacing=0 summary='Mine'>
  311. <?php
  312. }
  313. #
  314. global $haderror, $error;
  315. $haderror = false;
  316. $error = null;
  317. #
  318. function getsock($rig, $addr, $port)
  319. {
  320. global $rigipsecurity;
  321. global $haderror, $error, $socksndtimeoutsec, $sockrcvtimeoutsec;
  322. $error = null;
  323. $socket = null;
  324. $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  325. if ($socket === false || $socket === null)
  326. {
  327. $haderror = true;
  328. if ($rigipsecurity === false)
  329. {
  330. $error = socket_strerror(socket_last_error());
  331. $msg = "socket create(TCP) failed";
  332. $error = "ERR: $msg '$error'\n";
  333. }
  334. else
  335. $error = "ERR: socket create(TCP) failed\n";
  336. return null;
  337. }
  338. // Ignore if this fails since the socket connect may work anyway
  339. // and nothing is gained by aborting if the option cannot be set
  340. // since we don't know in advance if it can connect
  341. socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array('sec' => $socksndtimeoutsec, 'usec' => 0));
  342. socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $sockrcvtimeoutsec, 'usec' => 0));
  343. $res = socket_connect($socket, $addr, $port);
  344. if ($res === false)
  345. {
  346. $haderror = true;
  347. if ($rigipsecurity === false)
  348. {
  349. $error = socket_strerror(socket_last_error());
  350. $msg = "socket connect($addr,$port) failed";
  351. $error = "ERR: $msg '$error'\n";
  352. }
  353. else
  354. $error = "ERR: socket connect($rig) failed\n";
  355. socket_close($socket);
  356. return null;
  357. }
  358. return $socket;
  359. }
  360. #
  361. function readsockline($socket)
  362. {
  363. $line = '';
  364. while (true)
  365. {
  366. $byte = socket_read($socket, 1);
  367. if ($byte === false || $byte === '')
  368. break;
  369. if ($byte === "\0")
  370. break;
  371. $line .= $byte;
  372. }
  373. return $line;
  374. }
  375. #
  376. function api_convert_escape($str)
  377. {
  378. $res = '';
  379. $len = strlen($str);
  380. for ($i = 0; $i < $len; $i++)
  381. {
  382. $ch = substr($str, $i, 1);
  383. if ($ch != '\\' || $i == ($len-1))
  384. $res .= $ch;
  385. else
  386. {
  387. $i++;
  388. $ch = substr($str, $i, 1);
  389. switch ($ch)
  390. {
  391. case '|':
  392. $res .= "\1";
  393. break;
  394. case '\\':
  395. $res .= "\2";
  396. break;
  397. case '=':
  398. $res .= "\3";
  399. break;
  400. case ',':
  401. $res .= "\4";
  402. break;
  403. default:
  404. $res .= $ch;
  405. }
  406. }
  407. }
  408. return $res;
  409. }
  410. #
  411. function revert($str)
  412. {
  413. return str_replace(array("\1", "\2", "\3", "\4"), array("|", "\\", "=", ","), $str);
  414. }
  415. #
  416. function api($rig, $cmd)
  417. {
  418. global $haderror, $error;
  419. global $miner, $port, $hidefields;
  420. $socket = getsock($rig, $miner, $port);
  421. if ($socket != null)
  422. {
  423. socket_write($socket, $cmd, strlen($cmd));
  424. $line = readsockline($socket);
  425. socket_close($socket);
  426. if (strlen($line) == 0)
  427. {
  428. $haderror = true;
  429. $error = "WARN: '$cmd' returned nothing\n";
  430. return $line;
  431. }
  432. # print "$cmd returned '$line'\n";
  433. $line = api_convert_escape($line);
  434. $data = array();
  435. $objs = explode('|', $line);
  436. foreach ($objs as $obj)
  437. {
  438. if (strlen($obj) > 0)
  439. {
  440. $items = explode(',', $obj);
  441. $item = $items[0];
  442. $id = explode('=', $items[0], 2);
  443. if (count($id) == 1 or !ctype_digit($id[1]))
  444. $name = $id[0];
  445. else
  446. $name = $id[0].$id[1];
  447. if (strlen($name) == 0)
  448. $name = 'null';
  449. $sectionname = preg_replace('/\d/', '', $name);
  450. if (isset($data[$name]))
  451. {
  452. $num = 1;
  453. while (isset($data[$name.$num]))
  454. $num++;
  455. $name .= $num;
  456. }
  457. $counter = 0;
  458. foreach ($items as $item)
  459. {
  460. $id = explode('=', $item, 2);
  461. if (isset($hidefields[$sectionname.'.'.$id[0]]))
  462. continue;
  463. if (count($id) == 2)
  464. $data[$name][$id[0]] = revert($id[1]);
  465. else
  466. $data[$name][$counter] = $id[0];
  467. $counter++;
  468. }
  469. }
  470. }
  471. return $data;
  472. }
  473. return null;
  474. }
  475. #
  476. function getparam($name, $both = false)
  477. {
  478. $a = null;
  479. if (isset($_POST[$name]))
  480. $a = $_POST[$name];
  481. if (($both === true) and ($a === null))
  482. {
  483. if (isset($_GET[$name]))
  484. $a = $_GET[$name];
  485. }
  486. if ($a == '' || $a == null)
  487. return null;
  488. // limit to 1K just to be safe
  489. return substr($a, 0, 1024);
  490. }
  491. #
  492. function newtable()
  493. {
  494. global $tablebegin, $rownum;
  495. echo $tablebegin;
  496. $rownum = 0;
  497. }
  498. #
  499. function newrow()
  500. {
  501. echo '<tr>';
  502. }
  503. #
  504. function otherrow($row)
  505. {
  506. echo "<tr>$row</tr>";
  507. }
  508. #
  509. function endrow()
  510. {
  511. global $rownum;
  512. echo '</tr>';
  513. $rownum++;
  514. }
  515. #
  516. function endtable()
  517. {
  518. global $tableend;
  519. echo $tableend;
  520. }
  521. #
  522. function classlastshare($when, $alldata, $warnclass, $errorclass)
  523. {
  524. global $checklastshare;
  525. if ($checklastshare === false)
  526. return '';
  527. if ($when == 0)
  528. return '';
  529. if (!isset($alldata['MHS av']))
  530. return '';
  531. if ($alldata['MHS av'] == 0)
  532. return '';
  533. if (!isset($alldata['Last Share Time']))
  534. return '';
  535. if (!isset($alldata['Last Share Difficulty']))
  536. return '';
  537. $expected = pow(2, 32) / ($alldata['MHS av'] * pow(10, 6));
  538. // If the share difficulty changes while waiting on a share,
  539. // this calculation will of course be incorrect
  540. $expected *= $alldata['Last Share Difficulty'];
  541. $howlong = $when - $alldata['Last Share Time'];
  542. if ($howlong < 1)
  543. $howlong = 1;
  544. if ($howlong > ($expected * 12))
  545. return $errorclass;
  546. if ($howlong > ($expected * 8))
  547. return $warnclass;
  548. return '';
  549. }
  550. #
  551. function endzero($num)
  552. {
  553. $rep = preg_replace('/0*$/', '', $num);
  554. if ($rep === '')
  555. $rep = '0';
  556. return $rep;
  557. }
  558. #
  559. function fmt($section, $name, $value, $when, $alldata)
  560. {
  561. global $dfmt, $rownum;
  562. if ($alldata == null)
  563. $alldata = array();
  564. $errorclass = ' class=err';
  565. $warnclass = ' class=warn';
  566. $lstclass = ' class=lst';
  567. $hiclass = ' class=hi';
  568. $loclass = ' class=lo';
  569. $c2class = ' class=two';
  570. $totclass = ' class=tot';
  571. $b = '&nbsp;';
  572. $ret = $value;
  573. $class = '';
  574. $nams = explode('.', $name);
  575. if (count($nams) > 1)
  576. $name = $nams[count($nams)-1];
  577. if ($value === null)
  578. $ret = $b;
  579. else
  580. switch ($section.'.'.$name)
  581. {
  582. case 'GPU.Last Share Time':
  583. case 'PGA.Last Share Time':
  584. case 'ASC.Last Share Time':
  585. case 'DEVS.Last Share Time':
  586. if ($value == 0
  587. || (isset($alldata['Last Share Pool']) && $alldata['Last Share Pool'] == -1))
  588. {
  589. $ret = 'Never';
  590. $class = $warnclass;
  591. }
  592. else
  593. {
  594. $ret = date('H:i:s', $value);
  595. $class = classlastshare($when, $alldata, $warnclass, $errorclass);
  596. }
  597. break;
  598. case 'GPU.Last Valid Work':
  599. case 'PGA.Last Valid Work':
  600. case 'ASC.Last Valid Work':
  601. case 'DEVS.Last Valid Work':
  602. if ($value == 0)
  603. $ret = 'Never';
  604. else
  605. $ret = ($value - $when) . 's';
  606. break;
  607. case 'POOL.Last Share Time':
  608. if ($value == 0)
  609. $ret = 'Never';
  610. else
  611. $ret = date('H:i:s d-M', $value);
  612. break;
  613. case 'GPU.Last Share Pool':
  614. case 'PGA.Last Share Pool':
  615. case 'ASC.Last Share Pool':
  616. case 'DEVS.Last Share Pool':
  617. if ($value == -1)
  618. {
  619. $ret = 'None';
  620. $class = $warnclass;
  621. }
  622. break;
  623. case 'SUMMARY.Elapsed':
  624. case 'STATS.Elapsed':
  625. $s = $value % 60;
  626. $value -= $s;
  627. $value /= 60;
  628. if ($value == 0)
  629. $ret = $s.'s';
  630. else
  631. {
  632. $m = $value % 60;
  633. $value -= $m;
  634. $value /= 60;
  635. if ($value == 0)
  636. $ret = sprintf("%dm$b%02ds", $m, $s);
  637. else
  638. {
  639. $h = $value % 24;
  640. $value -= $h;
  641. $value /= 24;
  642. if ($value == 0)
  643. $ret = sprintf("%dh$b%02dm$b%02ds", $h, $m, $s);
  644. else
  645. {
  646. if ($value == 1)
  647. $days = '';
  648. else
  649. $days = 's';
  650. $ret = sprintf("%dday$days$b%02dh$b%02dm$b%02ds", $value, $h, $m, $s);
  651. }
  652. }
  653. }
  654. break;
  655. case 'NOTIFY.Last Well':
  656. if ($value == '0')
  657. {
  658. $ret = 'Never';
  659. $class = $warnclass;
  660. }
  661. else
  662. $ret = date('H:i:s', $value);
  663. break;
  664. case 'NOTIFY.Last Not Well':
  665. if ($value == '0')
  666. $ret = 'Never';
  667. else
  668. {
  669. $ret = date('H:i:s', $value);
  670. $class = $errorclass;
  671. }
  672. break;
  673. case 'NOTIFY.Reason Not Well':
  674. if ($value != 'None')
  675. $class = $errorclass;
  676. break;
  677. case 'GPU.Utility':
  678. case 'PGA.Utility':
  679. case 'ASC.Utility':
  680. case 'DEVS.Utility':
  681. case 'SUMMARY.Utility':
  682. case 'total.Utility':
  683. $ret = $value.'/m';
  684. if ($value == 0)
  685. $class = $errorclass;
  686. else
  687. if (isset($alldata['Difficulty Accepted'])
  688. && isset($alldata['Accepted'])
  689. && isset($alldata['MHS av'])
  690. && ($alldata['Difficulty Accepted'] > 0)
  691. && ($alldata['Accepted'] > 0))
  692. {
  693. $expected = 60 * $alldata['MHS av'] * (pow(10, 6) / pow(2, 32));
  694. if ($expected == 0)
  695. $expected = 0.000001; // 1 H/s
  696. $da = $alldata['Difficulty Accepted'];
  697. $a = $alldata['Accepted'];
  698. $expected /= ($da / $a);
  699. $ratio = $value / $expected;
  700. if ($ratio < 0.9)
  701. $class = $loclass;
  702. else
  703. if ($ratio > 1.1)
  704. $class = $hiclass;
  705. }
  706. break;
  707. case 'SUMMARY.Work Utility':
  708. case 'total.Work Utility':
  709. $ret = $value.'/m';
  710. break;
  711. case 'GPU.Temperature':
  712. case 'PGA.Temperature':
  713. case 'ASC.Temperature':
  714. case 'DEVS.Temperature':
  715. $ret = $value.'&deg;C';
  716. if (!isset($alldata['GPU']))
  717. break;
  718. case 'GPU.GPU Clock':
  719. case 'DEVS.GPU Clock':
  720. case 'GPU.Memory Clock':
  721. case 'DEVS.Memory Clock':
  722. case 'GPU.GPU Voltage':
  723. case 'DEVS.GPU Voltage':
  724. case 'GPU.GPU Activity':
  725. case 'DEVS.GPU Activity':
  726. if ($value == 0)
  727. $class = $warnclass;
  728. break;
  729. case 'GPU.Fan Percent':
  730. case 'DEVS.Fan Percent':
  731. if ($value == 0)
  732. $class = $warnclass;
  733. else
  734. {
  735. if ($value == 100)
  736. $class = $errorclass;
  737. else
  738. if ($value > 85)
  739. $class = $warnclass;
  740. }
  741. break;
  742. case 'GPU.Fan Speed':
  743. case 'DEVS.Fan Speed':
  744. if ($value == 0)
  745. $class = $warnclass;
  746. else
  747. if (isset($alldata['Fan Percent']))
  748. {
  749. $test = $alldata['Fan Percent'];
  750. if ($test == 100)
  751. $class = $errorclass;
  752. else
  753. if ($test > 85)
  754. $class = $warnclass;
  755. }
  756. break;
  757. case 'GPU.MHS av':
  758. case 'PGA.MHS av':
  759. case 'ASC.MHS av':
  760. case 'DEVS.MHS av':
  761. case 'SUMMARY.MHS av':
  762. case 'total.MHS av':
  763. $parts = explode('.', $value, 2);
  764. if (count($parts) == 1)
  765. $dec = '';
  766. else
  767. $dec = '.'.$parts[1];
  768. $ret = number_format((float)$parts[0]).$dec;
  769. if ($value == 0)
  770. $class = $errorclass;
  771. else
  772. if (isset($alldata['Difficulty Accepted'])
  773. && isset($alldata['Accepted'])
  774. && isset($alldata['Utility'])
  775. && ($alldata['Difficulty Accepted'] > 0)
  776. && ($alldata['Accepted'] > 0))
  777. {
  778. $expected = 60 * $value * (pow(10, 6) / pow(2, 32));
  779. if ($expected == 0)
  780. $expected = 0.000001; // 1 H/s
  781. $da = $alldata['Difficulty Accepted'];
  782. $a = $alldata['Accepted'];
  783. $expected /= ($da / $a);
  784. $ratio = $alldata['Utility'] / $expected;
  785. if ($ratio < 0.9)
  786. $class = $hiclass;
  787. else
  788. if ($ratio > 1.1)
  789. $class = $loclass;
  790. }
  791. break;
  792. case 'GPU.Total MH':
  793. case 'PGA.Total MH':
  794. case 'ASC.Total MH':
  795. case 'DEVS.Total MH':
  796. case 'SUMMARY.Total MH':
  797. case 'total.Total MH':
  798. case 'SUMMARY.Getworks':
  799. case 'POOL.Getworks':
  800. case 'total.Getworks':
  801. case 'GPU.Accepted':
  802. case 'PGA.Accepted':
  803. case 'ASC.Accepted':
  804. case 'DEVS.Accepted':
  805. case 'SUMMARY.Accepted':
  806. case 'POOL.Accepted':
  807. case 'total.Accepted':
  808. case 'GPU.Rejected':
  809. case 'PGA.Rejected':
  810. case 'ASC.Rejected':
  811. case 'DEVS.Rejected':
  812. case 'SUMMARY.Rejected':
  813. case 'POOL.Rejected':
  814. case 'total.Rejected':
  815. case 'SUMMARY.Local Work':
  816. case 'total.Local Work':
  817. case 'SUMMARY.Discarded':
  818. case 'POOL.Discarded':
  819. case 'total.Discarded':
  820. case 'POOL.Diff1 Shares':
  821. case 'total.Diff1 Shares':
  822. case 'GPU.Diff1 Work':
  823. case 'PGA.Diff1 Work':
  824. case 'ASC.Diff1 Work':
  825. case 'total.Diff1 Work':
  826. case 'STATS.Times Sent':
  827. case 'STATS.Bytes Sent':
  828. case 'STATS.Net Bytes Sent':
  829. case 'STATS.Times Recv':
  830. case 'STATS.Bytes Recv':
  831. case 'STATS.Net Bytes Recv':
  832. case 'total.Times Sent':
  833. case 'total.Bytes Sent':
  834. case 'total.Net Bytes Sent':
  835. case 'total.Times Recv':
  836. case 'total.Bytes Recv':
  837. case 'total.Net Bytes Recv':
  838. $parts = explode('.', $value, 2);
  839. if (count($parts) == 1)
  840. $dec = '';
  841. else
  842. $dec = '.'.$parts[1];
  843. $ret = number_format((float)$parts[0]).$dec;
  844. break;
  845. case 'STATS.Hs':
  846. case 'STATS.W':
  847. case 'STATS.history_time':
  848. case 'STATS.Pool Wait':
  849. case 'STATS.Pool Max':
  850. case 'STATS.Pool Min':
  851. case 'STATS.Pool Av':
  852. case 'STATS.Min Diff':
  853. case 'STATS.Max Diff':
  854. case 'STATS.Work Diff':
  855. $parts = explode('.', $value, 2);
  856. if (count($parts) == 1)
  857. $dec = '';
  858. else
  859. $dec = '.'.endzero($parts[1]);
  860. $ret = number_format((float)$parts[0]).$dec;
  861. break;
  862. case 'GPU.Status':
  863. case 'PGA.Status':
  864. case 'ASC.Status':
  865. case 'DEVS.Status':
  866. case 'POOL.Status':
  867. if ($value != 'Alive')
  868. $class = $errorclass;
  869. break;
  870. case 'GPU.Enabled':
  871. case 'PGA.Enabled':
  872. case 'ASC.Enabled':
  873. case 'ASC.Enabled':
  874. case 'DEVS.Enabled':
  875. if ($value != 'Y')
  876. $class = $warnclass;
  877. break;
  878. case 'STATUS.When':
  879. case 'COIN.Current Block Time':
  880. $ret = date($dfmt, $value);
  881. break;
  882. case 'BUTTON.Rig':
  883. case 'BUTTON.Pool':
  884. case 'BUTTON.GPU':
  885. $ret = $value;
  886. break;
  887. case 'SUMMARY.Difficulty Accepted':
  888. case 'GPU.Difficulty Accepted':
  889. case 'PGA.Difficulty Accepted':
  890. case 'ASC.Difficulty Accepted':
  891. case 'DEVS.Difficulty Accepted':
  892. case 'POOL.Difficulty Accepted':
  893. case 'total.Difficulty Accepted':
  894. case 'SUMMARY.Difficulty Rejected':
  895. case 'GPU.Difficulty Rejected':
  896. case 'PGA.Difficulty Rejected':
  897. case 'ASC.Difficulty Rejected':
  898. case 'DEVS.Difficulty Rejected':
  899. case 'POOL.Difficulty Rejected':
  900. case 'total.Difficulty Rejected':
  901. case 'SUMMARY.Difficulty Stale':
  902. case 'POOL.Difficulty Stale':
  903. case 'total.Difficulty Stale':
  904. case 'GPU.Last Share Difficulty':
  905. case 'PGA.Last Share Difficulty':
  906. case 'ASC.Last Share Difficulty':
  907. case 'DEVS.Last Share Difficulty':
  908. case 'POOL.Last Share Difficulty':
  909. if ($value != '')
  910. $ret = number_format((float)$value, 2);
  911. break;
  912. }
  913. if ($section == 'NOTIFY' && substr($name, 0, 1) == '*' && $value != '0')
  914. $class = $errorclass;
  915. if ($class == '' && $section != 'POOL')
  916. $class = classlastshare($when, $alldata, $lstclass, $lstclass);
  917. if ($class == '' && $section == 'total')
  918. $class = $totclass;
  919. if ($class == '' && ($rownum % 2) == 0)
  920. $class = $c2class;
  921. if ($ret === '')
  922. $ret = $b;
  923. return array($ret, $class);
  924. }
  925. #
  926. global $poolcmd;
  927. $poolcmd = array( 'Switch to' => 'switchpool',
  928. 'Enable' => 'enablepool',
  929. 'Disable' => 'disablepool',
  930. 'Remove' => 'removepool' );
  931. #
  932. function showhead($cmd, $values, $justnames = false)
  933. {
  934. global $poolcmd, $readonly;
  935. newrow();
  936. foreach ($values as $name => $value)
  937. {
  938. if ($name == '0' or $name == '')
  939. $name = '&nbsp;';
  940. echo "<td valign=bottom class=h>$name</td>";
  941. }
  942. if ($justnames === false && $cmd == 'pools' && $readonly === false)
  943. foreach ($poolcmd as $name => $pcmd)
  944. echo "<td valign=bottom class=h>$name</td>";
  945. endrow();
  946. }
  947. #
  948. function showdatetime()
  949. {
  950. global $dfmt;
  951. otherrow('<td class=sta>Date: '.date($dfmt).'</td>');
  952. }
  953. #
  954. global $singlerigsum;
  955. $singlerigsum = array(
  956. 'devs' => array('MHS av' => 1, 'MHS 5s' => 1, 'Accepted' => 1, 'Rejected' => 1,
  957. 'Hardware Errors' => 1, 'Utility' => 1, 'Total MH' => 1,
  958. 'Diff1 Shares' => 1, 'Diff1 Work' => 1, 'Difficulty Accepted' => 1,
  959. 'Difficulty Rejected' => 1),
  960. 'pools' => array('Getworks' => 1, 'Accepted' => 1, 'Rejected' => 1, 'Discarded' => 1,
  961. 'Stale' => 1, 'Get Failures' => 1, 'Remote Failures' => 1,
  962. 'Diff1 Shares' => 1, 'Diff1 Work' => 1, 'Difficulty Accepted' => 1,
  963. 'Difficulty Rejected' => 1, 'Difficulty Stale' => 1),
  964. 'notify' => array('*' => 1));
  965. #
  966. function showtotal($total, $when, $oldvalues)
  967. {
  968. global $rigtotals;
  969. list($showvalue, $class) = fmt('total', '', 'Total:', $when, null);
  970. echo "<td$class align=right>$showvalue</td>";
  971. $skipfirst = true;
  972. foreach ($oldvalues as $name => $value)
  973. {
  974. if ($skipfirst === true)
  975. {
  976. $skipfirst = false;
  977. continue;
  978. }
  979. if (isset($total[$name]))
  980. $newvalue = $total[$name];
  981. else
  982. $newvalue = '';
  983. list($showvalue, $class) = fmt('total', $name, $newvalue, $when, null);
  984. echo "<td$class";
  985. if ($rigtotals === true)
  986. echo ' align=right';
  987. echo ">$showvalue</td>";
  988. }
  989. }
  990. #
  991. function details($cmd, $list, $rig)
  992. {
  993. global $dfmt, $poolcmd, $readonly, $showndate;
  994. global $rownum, $rigtotals, $forcerigtotals, $singlerigsum;
  995. $when = 0;
  996. $stas = array('S' => 'Success', 'W' => 'Warning', 'I' => 'Informational', 'E' => 'Error', 'F' => 'Fatal');
  997. newtable();
  998. if ($showndate === false)
  999. {
  1000. showdatetime();
  1001. endtable();
  1002. newtable();
  1003. $showndate = true;
  1004. }
  1005. if (isset($list['STATUS']))
  1006. {
  1007. newrow();
  1008. echo '<td>Computer: '.$list['STATUS']['Description'].'</td>';
  1009. if (isset($list['STATUS']['When']))
  1010. {
  1011. echo '<td>When: '.date($dfmt, $list['STATUS']['When']).'</td>';
  1012. $when = $list['STATUS']['When'];
  1013. }
  1014. $sta = $list['STATUS']['STATUS'];
  1015. echo '<td>Status: '.$stas[$sta].'</td>';
  1016. echo '<td>Message: '.$list['STATUS']['Msg'].'</td>';
  1017. endrow();
  1018. }
  1019. if ($rigtotals === true && isset($singlerigsum[$cmd]))
  1020. $dototal = $singlerigsum[$cmd];
  1021. else
  1022. $dototal = array();
  1023. $total = array();
  1024. $section = '';
  1025. $oldvalues = null;
  1026. foreach ($list as $item => $values)
  1027. {
  1028. if ($item == 'STATUS')
  1029. continue;
  1030. $sectionname = preg_replace('/\d/', '', $item);
  1031. // Handle 'devs' possibly containing >1 table
  1032. if ($sectionname != $section)
  1033. {
  1034. if ($oldvalues != null && count($total) > 0
  1035. && ($rownum > 2 || $forcerigtotals === true))
  1036. showtotal($total, $when, $oldvalues);
  1037. endtable();
  1038. newtable();
  1039. showhead($cmd, $values);
  1040. $section = $sectionname;
  1041. }
  1042. newrow();
  1043. foreach ($values as $name => $value)
  1044. {
  1045. list($showvalue, $class) = fmt($section, $name, $value, $when, $values);
  1046. echo "<td$class";
  1047. if ($rigtotals === true)
  1048. echo ' align=right';
  1049. echo ">$showvalue</td>";
  1050. if (isset($dototal[$name])
  1051. || (isset($dototal['*']) and substr($name, 0, 1) == '*'))
  1052. {
  1053. if (isset($total[$name]))
  1054. $total[$name] += $value;
  1055. else
  1056. $total[$name] = $value;
  1057. }
  1058. }
  1059. if ($cmd == 'pools' && $readonly === false)
  1060. {
  1061. reset($values);
  1062. $pool = current($values);
  1063. foreach ($poolcmd as $name => $pcmd)
  1064. {
  1065. list($ignore, $class) = fmt('BUTTON', 'Pool', '', $when, $values);
  1066. echo "<td$class>";
  1067. if ($pool === false)
  1068. echo '&nbsp;';
  1069. else
  1070. {
  1071. echo "<input type=button value='Pool $pool'";
  1072. echo " onclick='prc(\"$pcmd|$pool&rig=$rig\",\"$name Pool $pool\")'>";
  1073. }
  1074. echo '</td>';
  1075. }
  1076. }
  1077. endrow();
  1078. $oldvalues = $values;
  1079. }
  1080. if ($oldvalues != null && count($total) > 0
  1081. && ($rownum > 2 || $forcerigtotals === true))
  1082. showtotal($total, $when, $oldvalues);
  1083. endtable();
  1084. }
  1085. #
  1086. global $devs;
  1087. $devs = null;
  1088. #
  1089. function gpubuttons($count, $rig)
  1090. {
  1091. global $devs;
  1092. $basic = array( 'GPU', 'Enable', 'Disable', 'Restart' );
  1093. $options = array( 'intensity' => 'Intensity',
  1094. 'fan' => 'Fan Percent',
  1095. 'engine' => 'GPU Clock',
  1096. 'mem' => 'Memory Clock',
  1097. 'vddc' => 'GPU Voltage' );
  1098. newtable();
  1099. newrow();
  1100. foreach ($basic as $head)
  1101. echo "<td class=h>$head</td>";
  1102. foreach ($options as $name => $des)
  1103. echo "<td class=h nowrap>$des</td>";
  1104. $n = 0;
  1105. for ($c = 0; $c < $count; $c++)
  1106. {
  1107. endrow();
  1108. newrow();
  1109. foreach ($basic as $name)
  1110. {
  1111. list($ignore, $class) = fmt('BUTTON', 'GPU', '', 0, null);
  1112. echo "<td$class>";
  1113. if ($name == 'GPU')
  1114. echo $c;
  1115. else
  1116. {
  1117. echo "<input type=button value='$name $c' onclick='prs(\"gpu";
  1118. echo strtolower($name);
  1119. echo "|$c\",$rig)'>";
  1120. }
  1121. echo '</td>';
  1122. }
  1123. foreach ($options as $name => $des)
  1124. {
  1125. list($ignore, $class) = fmt('BUTTON', 'GPU', '', 0, null);
  1126. echo "<td$class>";
  1127. if (!isset($devs["GPU$c"][$des]))
  1128. echo '&nbsp;';
  1129. else
  1130. {
  1131. $value = $devs["GPU$c"][$des];
  1132. echo "<input type=button value='Set $c:' onclick='prs2(\"gpu$name|$c\",$n,$rig)'>";
  1133. echo "<input size=7 type=text name=gi$n value='$value' id=gi$n>";
  1134. $n++;
  1135. }
  1136. echo '</td>';
  1137. }
  1138. }
  1139. endrow();
  1140. endtable();
  1141. }
  1142. #
  1143. function processgpus($rig)
  1144. {
  1145. global $error;
  1146. global $warnfont, $warnoff;
  1147. $gpus = api($rig, 'gpucount');
  1148. if ($error != null)
  1149. otherrow("<td>Error getting GPU count: $warnfont$error$warnoff</td>");
  1150. else
  1151. {
  1152. if (!isset($gpus['GPUS']['Count']))
  1153. {
  1154. $rw = '<td>No GPU count returned: '.$warnfont;
  1155. $rw .= $gpus['STATUS']['STATUS'].' '.$gpus['STATUS']['Msg'];
  1156. $rw .= $warnoff.'</td>';
  1157. otherrow($rw);
  1158. }
  1159. else
  1160. {
  1161. $count = $gpus['GPUS']['Count'];
  1162. if ($count == 0)
  1163. otherrow('<td>No GPUs</td>');
  1164. else
  1165. gpubuttons($count, $rig);
  1166. }
  1167. }
  1168. }
  1169. #
  1170. function showpoolinputs($rig, $ans)
  1171. {
  1172. global $readonly, $poolinputs;
  1173. if ($readonly === true || $poolinputs === false)
  1174. return;
  1175. newtable();
  1176. newrow();
  1177. $inps = array('Pool URL' => array('purl', 20),
  1178. 'Worker Name' => array('pwork', 10),
  1179. 'Worker Password' => array('ppass', 10));
  1180. $b = '&nbsp;';
  1181. echo "<td align=right class=h> Add a pool: </td><td>";
  1182. foreach ($inps as $text => $name)
  1183. echo "$text: <input name='".$name[0]."' id='".$name[0]."' value='' type=text size=".$name[1]."> ";
  1184. echo "</td><td align=middle><input type=button value='Add' onclick='pla($rig)'></td>";
  1185. endrow();
  1186. if (count($ans) > 1)
  1187. {
  1188. newrow();
  1189. echo '<td align=right class=h> Set pool priorities: </td>';
  1190. echo "<td> Comma list of pool numbers: <input type=text name=prio id=prio size=20>";
  1191. echo "</td><td align=middle><input type=button value='Set' onclick='psp($rig)'></td>";
  1192. endrow();
  1193. }
  1194. endtable();
  1195. }
  1196. #
  1197. function process($cmds, $rig)
  1198. {
  1199. global $error, $devs;
  1200. global $warnfont, $warnoff;
  1201. $count = count($cmds);
  1202. foreach ($cmds as $cmd => $des)
  1203. {
  1204. $process = api($rig, $cmd);
  1205. if ($error != null)
  1206. {
  1207. otherrow("<td colspan=100>Error getting $des: $warnfont$error$warnoff</td>");
  1208. break;
  1209. }
  1210. else
  1211. {
  1212. details($cmd, $process, $rig);
  1213. if ($cmd == 'devs')
  1214. $devs = $process;
  1215. if ($cmd == 'pools')
  1216. showpoolinputs($rig, $process);
  1217. # Not after the last one
  1218. if (--$count > 0)
  1219. otherrow('<td><br><br></td>');
  1220. }
  1221. }
  1222. }
  1223. #
  1224. function rigname($rig, $rigname)
  1225. {
  1226. global $rigs;
  1227. if (isset($rigs[$rig]))
  1228. {
  1229. $parts = explode(':', $rigs[$rig], 3);
  1230. if (count($parts) == 3)
  1231. $rigname = $parts[2];
  1232. }
  1233. return $rigname;
  1234. }
  1235. #
  1236. function riginput($rig, $rigname)
  1237. {
  1238. $rigname = rigname($rig, $rigname);
  1239. return "<input type=button value='$rigname' onclick='pr(\"&rig=$rig\",null)'>";
  1240. }
  1241. #
  1242. function rigbutton($rig, $rigname, $when, $row)
  1243. {
  1244. list($value, $class) = fmt('BUTTON', 'Rig', '', $when, $row);
  1245. if ($rig === '')
  1246. $ri = '&nbsp;';
  1247. else
  1248. $ri = riginput($rig, $rigname);
  1249. return "<td align=middle$class>$ri</td>";
  1250. }
  1251. #
  1252. function showrigs($anss, $headname, $rigname)
  1253. {
  1254. $dthead = array($headname => 1, 'STATUS' => 1, 'Description' => 1, 'When' => 1, 'API' => 1, 'CGMiner' => 1);
  1255. showhead('', $dthead);
  1256. foreach ($anss as $rig => $ans)
  1257. {
  1258. if ($ans == null)
  1259. continue;
  1260. newrow();
  1261. $when = 0;
  1262. if (isset($ans['STATUS']['When']))
  1263. $when = $ans['STATUS']['When'];
  1264. foreach ($ans as $item => $row)
  1265. {
  1266. if ($item != 'STATUS' && $item != 'VERSION')
  1267. continue;
  1268. foreach ($dthead as $name => $x)
  1269. {
  1270. if ($item == 'STATUS' && $name == $headname)
  1271. echo rigbutton($rig, $rigname.$rig, $when, null);
  1272. else
  1273. {
  1274. if (isset($row[$name]))
  1275. {
  1276. list($showvalue, $class) = fmt('STATUS', $name, $row[$name], $when, null);
  1277. echo "<td$class align=right>$showvalue</td>";
  1278. }
  1279. }
  1280. }
  1281. }
  1282. endrow();
  1283. }
  1284. }
  1285. #
  1286. # $head is a hack but this is just a demo anyway :)
  1287. function doforeach($cmd, $des, $sum, $head, $datetime)
  1288. {
  1289. global $miner, $port;
  1290. global $error, $readonly, $notify, $rigs;
  1291. global $warnfont, $warnoff, $dfmt;
  1292. global $rigerror;
  1293. $when = 0;
  1294. $header = $head;
  1295. $anss = array();
  1296. $count = 0;
  1297. $preverr = count($rigerror);
  1298. foreach ($rigs as $num => $rig)
  1299. {
  1300. $anss[$num] = null;
  1301. if (isset($rigerror[$rig]))
  1302. continue;
  1303. $parts = explode(':', $rig, 3);
  1304. if (count($parts) >= 2)
  1305. {
  1306. $miner = $parts[0];
  1307. $port = $parts[1];
  1308. if (count($parts) > 2)
  1309. $name = $parts[2];
  1310. else
  1311. $name = $num;
  1312. $ans = api($name, $cmd);
  1313. if ($error != null)
  1314. {
  1315. $rw = "<td colspan=100>Error on rig $name getting ";
  1316. $rw .= "$des: $warnfont$error$warnoff</td>";
  1317. otherrow($rw);
  1318. $rigerror[$rig] = $error;
  1319. $error = null;
  1320. }
  1321. else
  1322. {
  1323. $anss[$num] = $ans;
  1324. $count++;
  1325. }
  1326. }
  1327. }
  1328. if ($count == 0)
  1329. {
  1330. $rw = '<td>Failed to access any rigs successfully';
  1331. if ($preverr > 0)
  1332. $rw .= ' (or rigs had previous errors)';
  1333. $rw .= '</td>';
  1334. otherrow($rw);
  1335. return;
  1336. }
  1337. if ($datetime)
  1338. {
  1339. showdatetime();
  1340. endtable();
  1341. newtable();
  1342. showrigs($anss, '', 'Rig ');
  1343. endtable();
  1344. otherrow('<td><br><br></td>');
  1345. newtable();
  1346. return;
  1347. }
  1348. $total = array();
  1349. foreach ($anss as $rig => $ans)
  1350. {
  1351. if ($ans == null)
  1352. continue;
  1353. foreach ($ans as $item => $row)
  1354. {
  1355. if ($item == 'STATUS')
  1356. continue;
  1357. if (count($row) > count($header))
  1358. {
  1359. $header = $head;
  1360. foreach ($row as $name => $value)
  1361. if (!isset($header[$name]))
  1362. $header[$name] = '';
  1363. }
  1364. if ($sum != null)
  1365. foreach ($sum as $name)
  1366. {
  1367. if (isset($row[$name]))
  1368. {
  1369. if (isset($total[$name]))
  1370. $total[$name] += $row[$name];
  1371. else
  1372. $total[$name] = $row[$name];
  1373. }
  1374. }
  1375. }
  1376. }
  1377. if ($sum != null)
  1378. $anss['total']['total'] = $total;
  1379. showhead('', $header);
  1380. foreach ($anss as $rig => $ans)
  1381. {
  1382. if ($ans == null)
  1383. continue;
  1384. $when = 0;
  1385. if (isset($ans['STATUS']['When']))
  1386. $when = $ans['STATUS']['When'];
  1387. foreach ($ans as $item => $row)
  1388. {
  1389. if ($item == 'STATUS')
  1390. continue;
  1391. newrow();
  1392. $section = preg_replace('/\d/', '', $item);
  1393. foreach ($header as $name => $x)
  1394. {
  1395. if ($name == '')
  1396. {
  1397. if ($rig === 'total')
  1398. {
  1399. list($ignore, $class) = fmt($rig, '', '', $when, $row);
  1400. echo "<td align=right$class>Total:</td>";
  1401. }
  1402. else
  1403. echo rigbutton($rig, "Rig $rig", $when, $row);
  1404. }
  1405. else
  1406. {
  1407. if (isset($row[$name]))
  1408. $value = $row[$name];
  1409. else
  1410. $value = null;
  1411. list($showvalue, $class) = fmt($section, $name, $value, $when, $row);
  1412. echo "<td$class align=right>$showvalue</td>";
  1413. }
  1414. }
  1415. endrow();
  1416. }
  1417. }
  1418. }
  1419. #
  1420. function refreshbuttons()
  1421. {
  1422. global $ignorerefresh, $changerefresh, $autorefresh;
  1423. if ($ignorerefresh == false && $changerefresh == true)
  1424. {
  1425. echo '&nbsp;&nbsp;&nbsp;&nbsp;';
  1426. echo "<input type=button value='Auto Refresh:' onclick='prr(true)'>";
  1427. echo "<input type=text name='refval' id='refval' size=2 value='$autorefresh'>";
  1428. echo "<input type=button value='Off' onclick='prr(false)'>";
  1429. }
  1430. }
  1431. #
  1432. function pagebuttons($rig, $pg)
  1433. {
  1434. global $readonly, $rigs, $userlist, $ses;
  1435. global $allowcustompages, $customsummarypages;
  1436. if ($rig === null)
  1437. {
  1438. $prev = null;
  1439. $next = null;
  1440. if ($pg === null)
  1441. $refresh = '';
  1442. else
  1443. $refresh = "&pg=$pg";
  1444. }
  1445. else
  1446. {
  1447. switch (count($rigs))
  1448. {
  1449. case 0:
  1450. case 1:
  1451. $prev = null;
  1452. $next = null;
  1453. break;
  1454. case 2:
  1455. $prev = null;
  1456. $next = ($rig + 1) % count($rigs);
  1457. break;
  1458. default:
  1459. $prev = ($rig - 1) % count($rigs);
  1460. $next = ($rig + 1) % count($rigs);
  1461. break;
  1462. }
  1463. $refresh = "&rig=$rig";
  1464. }
  1465. echo '<tr><td><table cellpadding=0 cellspacing=0 border=0><tr><td nowrap>';
  1466. if ($userlist === null || isset($_SESSION[$ses]))
  1467. {
  1468. if ($prev !== null)
  1469. echo riginput($prev, 'Prev').'&nbsp;';
  1470. echo "<input type=button value='Refresh' onclick='pr(\"$refresh\",null)'>&nbsp;";
  1471. if ($next !== null)
  1472. echo riginput($next, 'Next').'&nbsp;';
  1473. echo '&nbsp;';
  1474. if (count($rigs) > 1)
  1475. echo "<input type=button value='Summary' onclick='pr(\"\",null)'>&nbsp;";
  1476. }
  1477. if ($allowcustompages === true)
  1478. {
  1479. if ($userlist === null || isset($_SESSION[$ses]))
  1480. $list = $customsummarypages;
  1481. else
  1482. {
  1483. if ($userlist !== null && isset($userlist['def']))
  1484. $list = array_flip($userlist['def']);
  1485. else
  1486. $list = array();
  1487. }
  1488. foreach ($list as $pagename => $data)
  1489. echo "<input type=button value='$pagename' onclick='pr(\"&pg=$pagename\",null)'>&nbsp;";
  1490. }
  1491. echo '</td><td width=100%>&nbsp;</td><td nowrap>';
  1492. if ($rig !== null && $readonly === false)
  1493. {
  1494. $rg = '';
  1495. if (count($rigs) > 1)
  1496. $rg = " Rig $rig";
  1497. echo "<input type=button value='Restart' onclick='prc(\"restart&rig=$rig\",\"Restart CGMiner$rg\")'>";
  1498. echo "&nbsp;<input type=button value='Quit' onclick='prc(\"quit&rig=$rig\",\"Quit CGMiner$rg\")'>";
  1499. }
  1500. refreshbuttons();
  1501. if (isset($_SESSION[$ses]))
  1502. echo "&nbsp;<input type=button value='Logout' onclick='pr(\"&logout=1\",null)'>";
  1503. else
  1504. if ($userlist !== null)
  1505. echo "&nbsp;<input type=button value='Login' onclick='pr(\"&login=1\",null)'>";
  1506. echo "</td></tr></table></td></tr>";
  1507. }
  1508. #
  1509. function doOne($rig, $preprocess)
  1510. {
  1511. global $haderror, $readonly, $notify, $rigs;
  1512. global $placebuttons;
  1513. htmlhead(true, $rig);
  1514. if ($placebuttons == 'top' || $placebuttons == 'both')
  1515. pagebuttons($rig, null);
  1516. if ($preprocess != null)
  1517. process(array($preprocess => $preprocess), $rig);
  1518. $cmds = array( 'devs' => 'device list',
  1519. 'summary' => 'summary information',
  1520. 'pools' => 'pool list');
  1521. if ($notify)
  1522. $cmds['notify'] = 'device status';
  1523. $cmds['config'] = 'cgminer config';
  1524. process($cmds, $rig);
  1525. if ($haderror == false && $readonly === false)
  1526. processgpus($rig);
  1527. if ($placebuttons == 'bot' || $placebuttons == 'both')
  1528. pagebuttons($rig, null);
  1529. }
  1530. #
  1531. global $sectionmap;
  1532. # map sections to their api command
  1533. # DEVS is a special case that will match GPU, PGA or ASC
  1534. # so you can have a single table with both in it
  1535. # DATE is hard coded so not in here
  1536. $sectionmap = array(
  1537. 'RIGS' => 'version',
  1538. 'SUMMARY' => 'summary',
  1539. 'POOL' => 'pools',
  1540. 'DEVS' => 'devs',
  1541. 'GPU' => 'devs', // You would normally use DEVS
  1542. 'PGA' => 'devs', // You would normally use DEVS
  1543. 'ASC' => 'devs', // You would normally use DEVS
  1544. 'NOTIFY' => 'notify',
  1545. 'DEVDETAILS' => 'devdetails',
  1546. 'STATS' => 'stats',
  1547. 'CONFIG' => 'config',
  1548. 'COIN' => 'coin',
  1549. 'USBSTATS' => 'usbstats');
  1550. #
  1551. function joinfields($section1, $section2, $join, $results)
  1552. {
  1553. global $sectionmap;
  1554. $name1 = $sectionmap[$section1];
  1555. $name2 = $sectionmap[$section2];
  1556. $newres = array();
  1557. // foreach rig in section1
  1558. foreach ($results[$name1] as $rig => $result)
  1559. {
  1560. $status = null;
  1561. // foreach answer section in the rig api call
  1562. foreach ($result as $name1b => $fields1b)
  1563. {
  1564. if ($name1b == 'STATUS')
  1565. {
  1566. // remember the STATUS from section1
  1567. $status = $result[$name1b];
  1568. continue;
  1569. }
  1570. // foreach answer section in the rig api call (for the other api command)
  1571. foreach ($results[$name2][$rig] as $name2b => $fields2b)
  1572. {
  1573. if ($name2b == 'STATUS')
  1574. continue;
  1575. // If match the same field values of fields in $join
  1576. $match = true;
  1577. foreach ($join as $field)
  1578. if ($fields1b[$field] != $fields2b[$field])
  1579. {
  1580. $match = false;
  1581. break;
  1582. }
  1583. if ($match === true)
  1584. {
  1585. if ($status != null)
  1586. {
  1587. $newres[$rig]['STATUS'] = $status;
  1588. $status = null;
  1589. }
  1590. $subsection = $section1.'+'.$section2;
  1591. $subsection .= preg_replace('/[^0-9]/', '', $name1b.$name2b);
  1592. foreach ($fields1b as $nam => $val)
  1593. $newres[$rig][$subsection]["$section1.$nam"] = $val;
  1594. foreach ($fields2b as $nam => $val)
  1595. $newres[$rig][$subsection]["$section2.$nam"] = $val;
  1596. }
  1597. }
  1598. }
  1599. }
  1600. return $newres;
  1601. }
  1602. #
  1603. function joinlr($section1, $section2, $join, $results)
  1604. {
  1605. global $sectionmap;
  1606. $name1 = $sectionmap[$section1];
  1607. $name2 = $sectionmap[$section2];
  1608. $newres = array();
  1609. // foreach rig in section1
  1610. foreach ($results[$name1] as $rig => $result)
  1611. {
  1612. $status = null;
  1613. // foreach answer section in the rig api call
  1614. foreach ($result as $name1b => $fields1b)
  1615. {
  1616. if ($name1b == 'STATUS')
  1617. {
  1618. // remember the STATUS from section1
  1619. $status = $result[$name1b];
  1620. continue;
  1621. }
  1622. // Build L string to be matched
  1623. // : means a string constant otherwise it's a field name
  1624. $Lval = '';
  1625. foreach ($join['L'] as $field)
  1626. {
  1627. if (substr($field, 0, 1) == ':')
  1628. $Lval .= substr($field, 1);
  1629. else
  1630. $Lval .= $fields1b[$field];
  1631. }
  1632. // foreach answer section in the rig api call (for the other api command)
  1633. foreach ($results[$name2][$rig] as $name2b => $fields2b)
  1634. {
  1635. if ($name2b == 'STATUS')
  1636. continue;
  1637. // Build R string and compare
  1638. // : means a string constant otherwise it's a field name
  1639. $Rval = '';
  1640. foreach ($join['R'] as $field)
  1641. {
  1642. if (substr($field, 0, 1) == ':')
  1643. $Rval .= substr($field, 1);
  1644. else
  1645. $Rval .= $fields2b[$field];
  1646. }
  1647. if ($Lval === $Rval)
  1648. {
  1649. if ($status != null)
  1650. {
  1651. $newres[$rig]['STATUS'] = $status;
  1652. $status = null;
  1653. }
  1654. $subsection = $section1.'+'.$section2;
  1655. $subsection .= preg_replace('/[^0-9]/', '', $name1b.$name2b);
  1656. foreach ($fields1b as $nam => $val)
  1657. $newres[$rig][$subsection]["$section1.$nam"] = $val;
  1658. foreach ($fields2b as $nam => $val)
  1659. $newres[$rig][$subsection]["$section2.$nam"] = $val;
  1660. }
  1661. }
  1662. }
  1663. }
  1664. return $newres;
  1665. }
  1666. #
  1667. function joinall($section1, $section2, $results)
  1668. {
  1669. global $sectionmap;
  1670. $name1 = $sectionmap[$section1];
  1671. $name2 = $sectionmap[$section2];
  1672. $newres = array();
  1673. // foreach rig in section1
  1674. foreach ($results[$name1] as $rig => $result)
  1675. {
  1676. // foreach answer section in the rig api call
  1677. foreach ($result as $name1b => $fields1b)
  1678. {
  1679. if ($name1b == 'STATUS')
  1680. {
  1681. // copy the STATUS from section1
  1682. $newres[$rig][$name1b] = $result[$name1b];
  1683. continue;
  1684. }
  1685. // foreach answer section in the rig api call (for the other api command)
  1686. foreach ($results[$name2][$rig] as $name2b => $fields2b)
  1687. {
  1688. if ($name2b == 'STATUS')
  1689. continue;
  1690. $subsection = $section1.'+'.$section2;
  1691. $subsection .= preg_replace('/[^0-9]/', '', $name1b.$name2b);
  1692. foreach ($fields1b as $nam => $val)
  1693. $newres[$rig][$subsection]["$section1.$nam"] = $val;
  1694. foreach ($fields2b as $nam => $val)
  1695. $newres[$rig][$subsection]["$section2.$nam"] = $val;
  1696. }
  1697. }
  1698. }
  1699. return $newres;
  1700. }
  1701. #
  1702. function joinsections($sections, $results, $errors)
  1703. {
  1704. global $sectionmap;
  1705. // GPU's don't have Name,ID fields - so create them
  1706. foreach ($results as $section => $res)
  1707. foreach ($res as $rig => $result)
  1708. foreach ($result as $name => $fields)
  1709. {
  1710. $subname = preg_replace('/[0-9]/', '', $name);
  1711. if ($subname == 'GPU' and isset($result[$name]['GPU']))
  1712. {
  1713. $results[$section][$rig][$name]['Name'] = 'GPU';
  1714. $results[$section][$rig][$name]['ID'] = $result[$name]['GPU'];
  1715. }
  1716. }
  1717. foreach ($sections as $section => $fields)
  1718. if ($section != 'DATE' && !isset($sectionmap[$section]))
  1719. {
  1720. $both = explode('+', $section, 2);
  1721. if (count($both) > 1)
  1722. {
  1723. switch($both[0])
  1724. {
  1725. case 'SUMMARY':
  1726. switch($both[1])
  1727. {
  1728. case 'POOL':
  1729. case 'DEVS':
  1730. case 'CONFIG':
  1731. case 'COIN':
  1732. $sectionmap[$section] = $section;
  1733. $results[$section] = joinall($both[0], $both[1], $results);
  1734. break;
  1735. default:
  1736. $errors[] = "Error: Invalid section '$section'";
  1737. break;
  1738. }
  1739. break;
  1740. case 'DEVS':
  1741. switch($both[1])
  1742. {
  1743. case 'NOTIFY':
  1744. case 'DEVDETAILS':
  1745. case 'USBSTATS':
  1746. $join = array('Name', 'ID');
  1747. $sectionmap[$section] = $section;
  1748. $results[$section] = joinfields($both[0], $both[1], $join, $results);
  1749. break;
  1750. case 'STATS':
  1751. $join = array('L' => array('Name','ID'), 'R' => array('ID'));
  1752. $sectionmap[$section] = $section;
  1753. $results[$section] = joinlr($both[0], $both[1], $join, $results);
  1754. break;
  1755. default:
  1756. $errors[] = "Error: Invalid section '$section'";
  1757. break;
  1758. }
  1759. break;
  1760. case 'POOL':
  1761. switch($both[1])
  1762. {
  1763. case 'STATS':
  1764. $join = array('L' => array(':POOL','POOL'), 'R' => array('ID'));
  1765. $sectionmap[$section] = $section;
  1766. $results[$section] = joinlr($both[0], $both[1], $join, $results);
  1767. break;
  1768. default:
  1769. $errors[] = "Error: Invalid section '$section'";
  1770. break;
  1771. }
  1772. break;
  1773. default:
  1774. $errors[] = "Error: Invalid section '$section'";
  1775. break;
  1776. }
  1777. }
  1778. else
  1779. $errors[] = "Error: Invalid section '$section'";
  1780. }
  1781. return array($results, $errors);
  1782. }
  1783. #
  1784. function secmatch($section, $field)
  1785. {
  1786. if ($section == $field)
  1787. return true;
  1788. if ($section == 'DEVS'
  1789. && ($field == 'GPU' || $field == 'PGA' || $field == 'ASC'))
  1790. return true;
  1791. return false;
  1792. }
  1793. #
  1794. function customset($showfields, $sum, $section, $rig, $isbutton, $result, $total)
  1795. {
  1796. foreach ($result as $sec => $row)
  1797. {
  1798. $secname = preg_replace('/\d/', '', $sec);
  1799. if ($sec != 'total')
  1800. if (!secmatch($section, $secname))
  1801. continue;
  1802. newrow();
  1803. $when = 0;
  1804. if (isset($result['STATUS']['When']))
  1805. $when = $result['STATUS']['When'];
  1806. if ($isbutton)
  1807. echo rigbutton($rig, $rig, $when, $row);
  1808. else
  1809. {
  1810. list($ignore, $class) = fmt('total', '', '', $when, $row);
  1811. echo "<td align=middle$class>$rig</td>";
  1812. }
  1813. foreach ($showfields as $name => $one)
  1814. {
  1815. if (isset($row[$name]))
  1816. {
  1817. $value = $row[$name];
  1818. if (isset($sum[$section][$name]))
  1819. {
  1820. if (isset($total[$name]))
  1821. $total[$name] += $value;
  1822. else
  1823. $total[$name] = $value;
  1824. }
  1825. }
  1826. else
  1827. {
  1828. if ($sec == 'total' && isset($total[$name]))
  1829. $value = $total[$name];
  1830. else
  1831. $value = null;
  1832. }
  1833. if (strpos($secname, '+') === false)
  1834. list($showvalue, $class) = fmt($secname, $name, $value, $when, $row);
  1835. else
  1836. {
  1837. $parts = explode('.', $name, 2);
  1838. list($showvalue, $class) = fmt($parts[0], $parts[1], $value, $when, $row);
  1839. }
  1840. echo "<td$class align=right>$showvalue</td>";
  1841. }
  1842. endrow();
  1843. }
  1844. return $total;
  1845. }
  1846. #
  1847. function docalc($func, $data)
  1848. {
  1849. switch ($func)
  1850. {
  1851. case 'sum':
  1852. $tot = 0;
  1853. foreach ($data as $val)
  1854. $tot += $val;
  1855. return $tot;
  1856. case 'avg':
  1857. $tot = 0;
  1858. foreach ($data as $val)
  1859. $tot += $val;
  1860. return ($tot / count($data));
  1861. case 'min':
  1862. $ans = null;
  1863. foreach ($data as $val)
  1864. if ($ans === null)
  1865. $ans = $val;
  1866. else
  1867. if ($val < $ans)
  1868. $ans = $val;
  1869. return $ans;
  1870. case 'max':
  1871. $ans = null;
  1872. foreach ($data as $val)
  1873. if ($ans === null)
  1874. $ans = $val;
  1875. else
  1876. if ($val > $ans)
  1877. $ans = $val;
  1878. return $ans;
  1879. case 'lo':
  1880. $ans = null;
  1881. foreach ($data as $val)
  1882. if ($ans === null)
  1883. $ans = $val;
  1884. else
  1885. if (strcasecmp($val, $ans) < 0)
  1886. $ans = $val;
  1887. return $ans;
  1888. case 'hi':
  1889. $ans = null;
  1890. foreach ($data as $val)
  1891. if ($ans === null)
  1892. $ans = $val;
  1893. else
  1894. if (strcasecmp($val, $ans) > 0)
  1895. $ans = $val;
  1896. return $ans;
  1897. case 'count':
  1898. return count($data);
  1899. case 'any':
  1900. default:
  1901. return $data[0];
  1902. }
  1903. }
  1904. #
  1905. function docompare($row, $test)
  1906. {
  1907. // invalid $test data means true
  1908. if (count($test) < 2)
  1909. return true;
  1910. if (isset($row[$test[0]]))
  1911. $val = $row[$test[0]];
  1912. else
  1913. $val = null;
  1914. if ($test[1] == 'set')
  1915. return ($val !== null);
  1916. if ($val === null || count($test) < 3)
  1917. return true;
  1918. switch($test[1])
  1919. {
  1920. case '=':
  1921. return ($val == $test[2]);
  1922. case '<':
  1923. return ($val < $test[2]);
  1924. case '<=':
  1925. return ($val <= $test[2]);
  1926. case '>':
  1927. return ($val > $test[2]);
  1928. case '>=':
  1929. return ($val >= $test[2]);
  1930. case 'eq':
  1931. return (strcasecmp($val, $test[2]) == 0);
  1932. case 'lt':
  1933. return (strcasecmp($val, $test[2]) < 0);
  1934. case 'le':
  1935. return (strcasecmp($val, $test[2]) <= 0);
  1936. case 'gt':
  1937. return (strcasecmp($val, $test[2]) > 0);
  1938. case 'ge':
  1939. return (strcasecmp($val, $test[2]) >= 0);
  1940. default:
  1941. return true;
  1942. }
  1943. }
  1944. #
  1945. function processcompare($which, $ext, $section, $res)
  1946. {
  1947. if (isset($ext[$section][$which]))
  1948. {
  1949. $proc = $ext[$section][$which];
  1950. if ($proc !== null)
  1951. {
  1952. $res2 = array();
  1953. foreach ($res as $rig => $result)
  1954. foreach ($result as $sec => $row)
  1955. {
  1956. $secname = preg_replace('/\d/', '', $sec);
  1957. if (!secmatch($section, $secname))
  1958. $res2[$rig][$sec] = $row;
  1959. else
  1960. {
  1961. $keep = true;
  1962. foreach ($proc as $test)
  1963. if (!docompare($row, $test))
  1964. {
  1965. $keep = false;
  1966. break;
  1967. }
  1968. if ($keep)
  1969. $res2[$rig][$sec] = $row;
  1970. }
  1971. }
  1972. $res = $res2;
  1973. }
  1974. }
  1975. return $res;
  1976. }
  1977. #
  1978. function processext($ext, $section, $res)
  1979. {
  1980. $res = processcompare('where', $ext, $section, $res);
  1981. if (isset($ext[$section]['group']))
  1982. {
  1983. $grp = $ext[$section]['group'];
  1984. $calc = $ext[$section]['calc'];
  1985. if ($grp !== null)
  1986. {
  1987. $interim = array();
  1988. $res2 = array();
  1989. $cou = 0;
  1990. foreach ($res as $rig => $result)
  1991. foreach ($result as $sec => $row)
  1992. {
  1993. $secname = preg_replace('/\d/', '', $sec);
  1994. if (!secmatch($section, $secname))
  1995. {
  1996. // STATUS may be problematic ...
  1997. if (!isset($res2[$sec]))
  1998. $res2[$sec] = $row;
  1999. }
  2000. else
  2001. {
  2002. $grpkey = '';
  2003. $newrow = array();
  2004. foreach ($grp as $field)
  2005. {
  2006. if (isset($row[$field]))
  2007. {
  2008. $grpkey .= $row[$field].'.';
  2009. $newrow[$field] = $row[$field];
  2010. }
  2011. else
  2012. $grpkey .= '.';
  2013. }
  2014. if (!isset($interim[$grpkey]))
  2015. {
  2016. $interim[$grpkey]['grp'] = $newrow;
  2017. $interim[$grpkey]['sec'] = $secname.$cou;
  2018. $cou++;
  2019. }
  2020. if ($calc !== null)
  2021. foreach ($calc as $field => $func)
  2022. {
  2023. if (!isset($interim[$grpkey]['cal'][$field]))
  2024. $interim[$grpkey]['cal'][$field] = array();
  2025. $interim[$grpkey]['cal'][$field][] = $row[$field];
  2026. }
  2027. }
  2028. }
  2029. // Build the rest of $res2 from $interim
  2030. foreach ($interim as $rowkey => $row)
  2031. {
  2032. $key = $row['sec'];
  2033. foreach ($row['grp'] as $field => $value)
  2034. $res2[$key][$field] = $value;
  2035. foreach ($row['cal'] as $field => $data)
  2036. $res2[$key][$field] = docalc($calc[$field], $data);
  2037. }
  2038. $res = array('' => $res2);
  2039. }
  2040. }
  2041. return processcompare('having', $ext, $section, $res);
  2042. }
  2043. #
  2044. function processcustompage($pagename, $sections, $sum, $ext, $namemap)
  2045. {
  2046. global $sectionmap;
  2047. global $miner, $port;
  2048. global $rigs, $error;
  2049. global $warnfont, $warnoff;
  2050. global $dfmt;
  2051. global $readonly, $showndate;
  2052. $cmds = array();
  2053. $errors = array();
  2054. foreach ($sections as $section => $fields)
  2055. {
  2056. $all = explode('+', $section);
  2057. foreach ($all as $section)
  2058. {
  2059. if (isset($sectionmap[$section]))
  2060. {
  2061. $cmd = $sectionmap[$section];
  2062. if (!isset($cmds[$cmd]))
  2063. $cmds[$cmd] = 1;
  2064. }
  2065. else
  2066. if ($section != 'DATE')
  2067. $errors[] = "Error: unknown section '$section' in custom summary page '$pagename'";
  2068. }
  2069. }
  2070. $results = array();
  2071. foreach ($rigs as $num => $rig)
  2072. {
  2073. $parts = explode(':', $rig, 3);
  2074. if (count($parts) >= 2)
  2075. {
  2076. $miner = $parts[0];
  2077. $port = $parts[1];
  2078. if (count($parts) > 2)
  2079. $name = $parts[2];
  2080. else
  2081. $name = $rig;
  2082. foreach ($cmds as $cmd => $one)
  2083. {
  2084. $process = api($name, $cmd);
  2085. if ($error != null)
  2086. {
  2087. $errors[] = "Error getting $cmd for $name $warnfont$error$warnoff";
  2088. break;
  2089. }
  2090. else
  2091. $results[$cmd][$num] = $process;
  2092. }
  2093. }
  2094. }
  2095. $shownsomething = false;
  2096. if (count($results) > 0)
  2097. {
  2098. list($results, $errors) = joinsections($sections, $results, $errors);
  2099. $first = true;
  2100. foreach ($sections as $section => $fields)
  2101. {
  2102. if ($section === 'DATE')
  2103. {
  2104. if ($shownsomething)
  2105. otherrow('<td>&nbsp;</td>');
  2106. newtable();
  2107. showdatetime();
  2108. endtable();
  2109. // On top of the next table
  2110. $shownsomething = false;
  2111. continue;
  2112. }
  2113. if ($section === 'RIGS')
  2114. {
  2115. if ($shownsomething)
  2116. otherrow('<td>&nbsp;</td>');
  2117. newtable();
  2118. showrigs($results['version'], 'Rig', '');
  2119. endtable();
  2120. $shownsomething = true;
  2121. continue;
  2122. }
  2123. if (isset($results[$sectionmap[$section]]))
  2124. {
  2125. $rigresults = processext($ext, $section, $results[$sectionmap[$section]]);
  2126. $showfields = array();
  2127. $showhead = array();
  2128. foreach ($fields as $field)
  2129. foreach ($rigresults as $result)
  2130. foreach ($result as $sec => $row)
  2131. {
  2132. $secname = preg_replace('/\d/', '', $sec);
  2133. if (secmatch($section, $secname))
  2134. {
  2135. if ($field === '*')
  2136. {
  2137. foreach ($row as $f => $v)
  2138. {
  2139. $showfields[$f] = 1;
  2140. $map = $section.'.'.$f;
  2141. if (isset($namemap[$map]))
  2142. $showhead[$namemap[$map]] = 1;
  2143. else
  2144. $showhead[$f] = 1;
  2145. }
  2146. }
  2147. elseif (isset($row[$field]))
  2148. {
  2149. $showfields[$field] = 1;
  2150. $map = $section.'.'.$field;
  2151. if (isset($namemap[$map]))
  2152. $showhead[$namemap[$map]] = 1;
  2153. else
  2154. $showhead[$field] = 1;
  2155. }
  2156. }
  2157. }
  2158. if (count($showfields) > 0)
  2159. {
  2160. if ($shownsomething)
  2161. otherrow('<td>&nbsp;</td>');
  2162. newtable();
  2163. if (count($rigresults) == 1 && isset($rigresults['']))
  2164. $ri = array('' => 1) + $showhead;
  2165. else
  2166. $ri = array('Rig' => 1) + $showhead;
  2167. showhead('', $ri, true);
  2168. $total = array();
  2169. $add = array('total' => array());
  2170. foreach ($rigresults as $num => $result)
  2171. $total = customset($showfields, $sum, $section, $num, true, $result, $total);
  2172. if (count($total) > 0)
  2173. customset($showfields, $sum, $section, '&Sigma;', false, $add, $total);
  2174. $first = false;
  2175. endtable();
  2176. $shownsomething = true;
  2177. }
  2178. }
  2179. }
  2180. }
  2181. if (count($errors) > 0)
  2182. {
  2183. if (count($results) > 0)
  2184. otherrow('<td>&nbsp;</td>');
  2185. foreach ($errors as $err)
  2186. otherrow("<td colspan=100>$err</td>");
  2187. }
  2188. }
  2189. #
  2190. function showcustompage($pagename)
  2191. {
  2192. global $customsummarypages;
  2193. global $placebuttons;
  2194. htmlhead(false, null, $pagename);
  2195. if ($placebuttons == 'top' || $placebuttons == 'both')
  2196. pagebuttons(null, $pagename);
  2197. if (!isset($customsummarypages[$pagename]))
  2198. {
  2199. otherrow("<td colspan=100>Unknown custom summary page '$pagename'</td>");
  2200. return;
  2201. }
  2202. $c = count($customsummarypages[$pagename]);
  2203. if ($c < 2 || $c > 3)
  2204. {
  2205. $rw = "<td colspan=100>Invalid custom summary page '$pagename' (";
  2206. $rw .= count($customsummarypages[$pagename]).')</td>';
  2207. otherrow($rw);
  2208. return;
  2209. }
  2210. $page = $customsummarypages[$pagename][0];
  2211. $namemap = array();
  2212. foreach ($page as $name => $fields)
  2213. {
  2214. if ($fields === null)
  2215. $page[$name] = array();
  2216. else
  2217. foreach ($fields as $num => $field)
  2218. {
  2219. $pos = strpos($field, '=');
  2220. if ($pos !== false)
  2221. {
  2222. $names = explode('=', $field, 2);
  2223. if (strlen($names[1]) > 0)
  2224. $namemap[$name.'.'.$names[0]] = $names[1];
  2225. $page[$name][$num] = $names[0];
  2226. }
  2227. }
  2228. }
  2229. $ext = null;
  2230. if (isset($customsummarypages[$pagename][2]))
  2231. $ext = $customsummarypages[$pagename][2];
  2232. $sum = $customsummarypages[$pagename][1];
  2233. if ($sum === null)
  2234. $sum = array();
  2235. // convert them to searchable via isset()
  2236. foreach ($sum as $section => $fields)
  2237. {
  2238. $newfields = array();
  2239. foreach ($fields as $field)
  2240. $newfields[$field] = 1;
  2241. $sum[$section] = $newfields;
  2242. }
  2243. if (count($page) <= 1)
  2244. {
  2245. otherrow("<td colspan=100>Invalid custom summary page '$pagename' no content </td>");
  2246. return;
  2247. }
  2248. processcustompage($pagename, $page, $sum, $ext, $namemap);
  2249. if ($placebuttons == 'bot' || $placebuttons == 'both')
  2250. pagebuttons(null, $pagename);
  2251. }
  2252. #
  2253. function onlylogin()
  2254. {
  2255. global $here;
  2256. htmlhead(false, null, null, true);
  2257. ?>
  2258. <tr height=15%><td>&nbsp;</td></tr>
  2259. <tr><td>
  2260. <center>
  2261. <table width=384 height=368 cellpadding=0 cellspacing=0 border=0>
  2262. <tr><td>
  2263. <table width=100% height=100% border=0 align=center cellpadding=5 cellspacing=0>
  2264. <tr><td><form action='<?php echo $here; ?>' method=post>
  2265. <table width=200 border=0 align=center cellpadding=5 cellspacing=0>
  2266. <tr><td height=120 colspan=2>&nbsp;</td></tr>
  2267. <tr><td colspan=2 align=center valign=middle>
  2268. <h2>LOGIN</h2></td></tr>
  2269. <tr><td align=center valign=middle><div align=right>Username:</div></td>
  2270. <td height=33 align=center valign=middle>
  2271. <input type=text name=rut size=18></td></tr>
  2272. <tr><td align=center valign=middle><div align=right>Password:</div></td>
  2273. <td height=33 align=center valign=middle>
  2274. <input type=password name=roh size=18></td></tr>
  2275. <tr valign=top><td></td><td><input type=submit value=Login>
  2276. </td></tr>
  2277. </table></form></td></tr>
  2278. </table></td></tr>
  2279. </table></center>
  2280. </td></tr>
  2281. <?php
  2282. }
  2283. #
  2284. function checklogin()
  2285. {
  2286. global $allowcustompages, $customsummarypages;
  2287. global $readonly, $userlist, $ses;
  2288. $out = trim(getparam('logout', true));
  2289. if ($out !== null && $out !== '' && isset($_SESSION[$ses]))
  2290. unset($_SESSION[$ses]);
  2291. $login = trim(getparam('login', true));
  2292. if ($login !== null && $login !== '')
  2293. {
  2294. if (isset($_SESSION[$ses]))
  2295. unset($_SESSION[$ses]);
  2296. onlylogin();
  2297. return 'login';
  2298. }
  2299. if ($userlist === null)
  2300. return false;
  2301. $rut = trim(getparam('rut', true));
  2302. $roh = trim(getparam('roh', true));
  2303. if (($rut !== null && $rut !== '') || ($roh !== null && $roh !== ''))
  2304. {
  2305. if (isset($_SESSION[$ses]))
  2306. unset($_SESSION[$ses]);
  2307. if ($rut !== null && $rut !== '' && $roh !== null && $roh !== '')
  2308. {
  2309. if (isset($userlist['sys']) && isset($userlist['sys'][$rut])
  2310. && ($userlist['sys'][$rut] === $roh))
  2311. {
  2312. $_SESSION[$ses] = true;
  2313. return false;
  2314. }
  2315. if (isset($userlist['usr']) && isset($userlist['usr'][$rut])
  2316. && ($userlist['usr'][$rut] === $roh))
  2317. {
  2318. $_SESSION[$ses] = false;
  2319. $readonly = true;
  2320. return false;
  2321. }
  2322. }
  2323. }
  2324. if (isset($_SESSION[$ses]))
  2325. {
  2326. if ($_SESSION[$ses] == false)
  2327. $readonly = true;
  2328. return false;
  2329. }
  2330. if (isset($userlist['def']) && $allowcustompages === true)
  2331. {
  2332. // Ensure at least one exists
  2333. foreach ($userlist['def'] as $pg)
  2334. if (isset($customsummarypages[$pg]))
  2335. return true;
  2336. }
  2337. onlylogin();
  2338. return 'login';
  2339. }
  2340. #
  2341. function display()
  2342. {
  2343. global $miner, $port;
  2344. global $readonly, $notify, $rigs;
  2345. global $ignorerefresh, $autorefresh;
  2346. global $allowcustompages, $customsummarypages;
  2347. global $placebuttons;
  2348. global $userlist, $ses;
  2349. $pagesonly = checklogin();
  2350. if ($pagesonly === 'login')
  2351. return;
  2352. if ($rigs == null or count($rigs) == 0)
  2353. {
  2354. otherrow("<td>No rigs defined</td>");
  2355. return;
  2356. }
  2357. if ($ignorerefresh == false)
  2358. {
  2359. $ref = trim(getparam('ref', true));
  2360. if ($ref != null && $ref != '')
  2361. $autorefresh = intval($ref);
  2362. }
  2363. if ($pagesonly !== true)
  2364. {
  2365. $rig = trim(getparam('rig', true));
  2366. $arg = trim(getparam('arg', true));
  2367. $preprocess = null;
  2368. if ($arg != null and $arg != '')
  2369. {
  2370. if ($rig != null and $rig != '' and $rig >= 0 and $rig < count($rigs))
  2371. {
  2372. $parts = explode(':', $rigs[$rig], 3);
  2373. if (count($parts) >= 2)
  2374. {
  2375. $miner = $parts[0];
  2376. $port = $parts[1];
  2377. if ($readonly !== true)
  2378. $preprocess = $arg;
  2379. }
  2380. }
  2381. }
  2382. }
  2383. if ($allowcustompages === true)
  2384. {
  2385. $pg = trim(getparam('pg', true));
  2386. if ($pagesonly === true)
  2387. {
  2388. if ($pg !== null && $pg !== '')
  2389. {
  2390. if ($userlist !== null && isset($userlist['def'])
  2391. && !in_array($pg, $userlist['def']))
  2392. $pg = null;
  2393. }
  2394. else
  2395. {
  2396. if ($userlist !== null && isset($userlist['def']))
  2397. foreach ($userlist['def'] as $pglook)
  2398. if (isset($customsummarypages[$pglook]))
  2399. {
  2400. $pg = $pglook;
  2401. break;
  2402. }
  2403. }
  2404. }
  2405. if ($pg !== null && $pg !== '')
  2406. {
  2407. showcustompage($pg);
  2408. return;
  2409. }
  2410. }
  2411. if ($pagesonly === true)
  2412. {
  2413. onlylogin();
  2414. return;
  2415. }
  2416. if (count($rigs) == 1)
  2417. {
  2418. $parts = explode(':', $rigs[0], 3);
  2419. if (count($parts) >= 2)
  2420. {
  2421. $miner = $parts[0];
  2422. $port = $parts[1];
  2423. doOne(0, $preprocess);
  2424. }
  2425. else
  2426. otherrow('<td>Invalid "$rigs" array</td>');
  2427. return;
  2428. }
  2429. if ($rig != null and $rig != '' and $rig >= 0 and $rig < count($rigs))
  2430. {
  2431. $parts = explode(':', $rigs[$rig], 3);
  2432. if (count($parts) >= 2)
  2433. {
  2434. $miner = $parts[0];
  2435. $port = $parts[1];
  2436. doOne($rig, $preprocess);
  2437. }
  2438. else
  2439. otherrow('<td>Invalid "$rigs" array</td>');
  2440. return;
  2441. }
  2442. htmlhead(false, null);
  2443. if ($placebuttons == 'top' || $placebuttons == 'both')
  2444. pagebuttons(null, null);
  2445. if ($preprocess != null)
  2446. process(array($preprocess => $preprocess), $rig);
  2447. newtable();
  2448. doforeach('version', 'rig summary', array(), array(), true);
  2449. $sum = array('MHS av', 'Getworks', 'Found Blocks', 'Accepted', 'Rejected', 'Discarded', 'Stale', 'Utility', 'Local Work', 'Total MH', 'Work Utility', 'Diff1 Shares', 'Diff1 Work', 'Difficulty Accepted', 'Difficulty Rejected', 'Difficulty Stale');
  2450. doforeach('summary', 'summary information', $sum, array(), false);
  2451. endtable();
  2452. otherrow('<td><br><br></td>');
  2453. newtable();
  2454. doforeach('devs', 'device list', $sum, array(''=>'','ID'=>'','Name'=>''), false);
  2455. endtable();
  2456. otherrow('<td><br><br></td>');
  2457. newtable();
  2458. doforeach('pools', 'pool list', $sum, array(''=>''), false);
  2459. endtable();
  2460. if ($placebuttons == 'bot' || $placebuttons == 'both')
  2461. pagebuttons(null, null);
  2462. }
  2463. #
  2464. display();
  2465. #
  2466. ?>
  2467. </table></td></tr></table>
  2468. </body></html>