miner.php 60 KB

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