miner.php 54 KB

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