miner.php 59 KB

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