miner.php 60 KB

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