miner.php 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753
  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. $list[$item] = $values = array('Device' => $repr) + array_slice($values, 1);
  1001. unset($values['Name']);
  1002. unset($values['ID']);
  1003. }
  1004. $namesByIndex = array_keys($values);
  1005. $nameCount = count($namesByIndex);
  1006. for ($i = 0; $i < $nameCount; ++$i)
  1007. {
  1008. $name = $namesByIndex[$i];
  1009. if (isset($columns[$name]))
  1010. continue;
  1011. $value = $values[$name];
  1012. $before = null;
  1013. for ($j = $i + 1; $j < $nameCount; ++$j)
  1014. {
  1015. $maybebefore = $namesByIndex[$j];
  1016. if (isset($columns[$maybebefore]))
  1017. {
  1018. $before = $columns[$maybebefore];
  1019. break;
  1020. }
  1021. }
  1022. if (!$before)
  1023. {
  1024. $columns[$name] = array_push($columnsByIndex, $name) - 1;
  1025. continue;
  1026. }
  1027. array_splice($columnsByIndex, $before, 0, $name);
  1028. $columns[$name] = $before;
  1029. $columnCount = count($columnsByIndex);
  1030. for ($j = $before + 1; $j < $columnCount; ++$j)
  1031. $columns[$columnsByIndex[$j]] = $j;
  1032. }
  1033. }
  1034. asort($columns);
  1035. endtable();
  1036. newtable();
  1037. showhead($cmd, $columns);
  1038. foreach ($list as $item => $values)
  1039. {
  1040. if ($item == 'STATUS')
  1041. continue;
  1042. newrow();
  1043. foreach ($columns as $name => $columnidx)
  1044. {
  1045. if (!isset($values[$name]))
  1046. {
  1047. echo '<td></td>';
  1048. continue;
  1049. }
  1050. $value = $values[$name];
  1051. list($showvalue, $class) = fmt($section, $name, $value, $when, $values);
  1052. echo "<td$class";
  1053. if ($rigtotals === true)
  1054. echo ' align=right';
  1055. echo ">$showvalue</td>";
  1056. if (isset($dototal[$name])
  1057. || (isset($dototal['*']) and substr($name, 0, 1) == '*'))
  1058. {
  1059. if (isset($total[$name]))
  1060. $total[$name] += $value;
  1061. else
  1062. $total[$name] = $value;
  1063. }
  1064. }
  1065. if ($cmd == 'pools' && $readonly === false)
  1066. {
  1067. reset($values);
  1068. $pool = current($values);
  1069. foreach ($poolcmd as $name => $pcmd)
  1070. {
  1071. list($ignore, $class) = fmt('BUTTON', 'Pool', '', $when, $values);
  1072. echo "<td$class>";
  1073. if ($pool === false)
  1074. echo '&nbsp;';
  1075. else
  1076. {
  1077. echo "<input type=button value='Pool $pool'";
  1078. echo " onclick='prc(\"$pcmd|$pool&rig=$rig\",\"$name Pool $pool\")'>";
  1079. }
  1080. echo '</td>';
  1081. }
  1082. }
  1083. endrow();
  1084. $oldvalues = $values;
  1085. }
  1086. if ($oldvalues != null && count($total) > 0
  1087. && ($rownum > 2 || $forcerigtotals === true))
  1088. showtotal($total, $when, $columns);
  1089. endtable();
  1090. }
  1091. #
  1092. global $devs;
  1093. $devs = null;
  1094. #
  1095. function gpubuttons($count, $rig)
  1096. {
  1097. global $devs;
  1098. $basic = array( 'GPU', 'Enable', 'Disable', 'Restart' );
  1099. $options = array( 'intensity' => 'Intensity',
  1100. 'fan' => 'Fan Percent',
  1101. 'engine' => 'GPU Clock',
  1102. 'mem' => 'Memory Clock',
  1103. 'vddc' => 'GPU Voltage' );
  1104. newtable();
  1105. newrow();
  1106. foreach ($basic as $head)
  1107. echo "<td class=h>$head</td>";
  1108. foreach ($options as $name => $des)
  1109. echo "<td class=h nowrap>$des</td>";
  1110. $n = 0;
  1111. for ($c = 0; $c < $count; $c++)
  1112. {
  1113. endrow();
  1114. newrow();
  1115. foreach ($basic as $name)
  1116. {
  1117. list($ignore, $class) = fmt('BUTTON', 'GPU', '', 0, null);
  1118. echo "<td$class>";
  1119. if ($name == 'GPU')
  1120. echo $c;
  1121. else
  1122. {
  1123. echo "<input type=button value='$name $c' onclick='prs(\"gpu";
  1124. echo strtolower($name);
  1125. echo "|$c\",$rig)'>";
  1126. }
  1127. echo '</td>';
  1128. }
  1129. foreach ($options as $name => $des)
  1130. {
  1131. list($ignore, $class) = fmt('BUTTON', 'GPU', '', 0, null);
  1132. echo "<td$class>";
  1133. if (!isset($devs["GPU$c"][$des]))
  1134. echo '&nbsp;';
  1135. else
  1136. {
  1137. $value = $devs["GPU$c"][$des];
  1138. echo "<input type=button value='Set $c:' onclick='prs2(\"gpu$name|$c\",$n,$rig)'>";
  1139. echo "<input size=7 type=text name=gi$n value='$value' id=gi$n>";
  1140. $n++;
  1141. }
  1142. echo '</td>';
  1143. }
  1144. }
  1145. endrow();
  1146. endtable();
  1147. }
  1148. #
  1149. function processgpus($rig)
  1150. {
  1151. global $error;
  1152. global $warnfont, $warnoff;
  1153. $gpus = api($rig, 'gpucount');
  1154. if ($error != null)
  1155. otherrow("<td>Error getting GPU count: $warnfont$error$warnoff</td>");
  1156. else
  1157. {
  1158. if (!isset($gpus['GPUS']['Count']))
  1159. {
  1160. $rw = '<td>No GPU count returned: '.$warnfont;
  1161. $rw .= $gpus['STATUS']['STATUS'].' '.$gpus['STATUS']['Msg'];
  1162. $rw .= $warnoff.'</td>';
  1163. otherrow($rw);
  1164. }
  1165. else
  1166. {
  1167. $count = $gpus['GPUS']['Count'];
  1168. if ($count == 0)
  1169. otherrow('<td>No GPUs</td>');
  1170. else
  1171. gpubuttons($count, $rig);
  1172. }
  1173. }
  1174. }
  1175. #
  1176. function showpoolinputs($rig, $ans)
  1177. {
  1178. global $readonly, $poolinputs;
  1179. if ($readonly === true || $poolinputs === false)
  1180. return;
  1181. newtable();
  1182. newrow();
  1183. $inps = array('Pool URL' => array('purl', 20),
  1184. 'Worker Name' => array('pwork', 10),
  1185. 'Worker Password' => array('ppass', 10));
  1186. $b = '&nbsp;';
  1187. echo "<td align=right class=h> Add a pool: </td><td>";
  1188. foreach ($inps as $text => $name)
  1189. echo "$text: <input name='".$name[0]."' id='".$name[0]."' value='' type=text size=".$name[1]."> ";
  1190. echo "</td><td align=middle><input type=button value='Add' onclick='pla($rig)'></td>";
  1191. endrow();
  1192. if (count($ans) > 1)
  1193. {
  1194. newrow();
  1195. echo '<td align=right class=h> Set pool priorities: </td>';
  1196. echo "<td> Comma list of pool numbers: <input type=text name=prio id=prio size=20>";
  1197. echo "</td><td align=middle><input type=button value='Set' onclick='psp($rig)'></td>";
  1198. endrow();
  1199. }
  1200. endtable();
  1201. }
  1202. #
  1203. function process($cmds, $rig)
  1204. {
  1205. global $error, $devs;
  1206. global $warnfont, $warnoff;
  1207. $count = count($cmds);
  1208. foreach ($cmds as $cmd => $des)
  1209. {
  1210. $process = api($rig, $cmd);
  1211. if ($error != null)
  1212. {
  1213. otherrow("<td colspan=100>Error getting $des: $warnfont$error$warnoff</td>");
  1214. break;
  1215. }
  1216. else
  1217. {
  1218. details($cmd, $process, $rig);
  1219. if ($cmd == 'devs')
  1220. $devs = $process;
  1221. if ($cmd == 'pools')
  1222. showpoolinputs($rig, $process);
  1223. # Not after the last one
  1224. if (--$count > 0)
  1225. otherrow('<td><br><br></td>');
  1226. }
  1227. }
  1228. }
  1229. #
  1230. function rigname($rig, $rigname)
  1231. {
  1232. global $rigs;
  1233. if (isset($rigs[$rig]))
  1234. {
  1235. $parts = explode(':', $rigs[$rig], 3);
  1236. if (count($parts) == 3)
  1237. $rigname = $parts[2];
  1238. }
  1239. return $rigname;
  1240. }
  1241. #
  1242. function riginput($rig, $rigname)
  1243. {
  1244. $rigname = rigname($rig, $rigname);
  1245. return "<input type=button value='$rigname' onclick='pr(\"&rig=$rig\",null)'>";
  1246. }
  1247. #
  1248. function rigbutton($rig, $rigname, $when, $row)
  1249. {
  1250. list($value, $class) = fmt('BUTTON', 'Rig', '', $when, $row);
  1251. if ($rig === '')
  1252. $ri = '&nbsp;';
  1253. else
  1254. $ri = riginput($rig, $rigname);
  1255. return "<td align=middle$class>$ri</td>";
  1256. }
  1257. #
  1258. function showrigs($anss, $headname, $rigname)
  1259. {
  1260. $dthead = array($headname => 1, 'STATUS' => 1, 'Description' => 1, 'When' => 1, 'API' => 1, 'CGMiner' => 1);
  1261. showhead('', $dthead);
  1262. foreach ($anss as $rig => $ans)
  1263. {
  1264. if ($ans == null)
  1265. continue;
  1266. newrow();
  1267. $when = 0;
  1268. if (isset($ans['STATUS']['When']))
  1269. $when = $ans['STATUS']['When'];
  1270. foreach ($ans as $item => $row)
  1271. {
  1272. if ($item != 'STATUS' && $item != 'VERSION')
  1273. continue;
  1274. foreach ($dthead as $name => $x)
  1275. {
  1276. if ($item == 'STATUS' && $name == $headname)
  1277. echo rigbutton($rig, $rigname.$rig, $when, null);
  1278. else
  1279. {
  1280. if (isset($row[$name]))
  1281. {
  1282. list($showvalue, $class) = fmt('STATUS', $name, $row[$name], $when, null);
  1283. echo "<td$class align=right>$showvalue</td>";
  1284. }
  1285. }
  1286. }
  1287. }
  1288. endrow();
  1289. }
  1290. }
  1291. #
  1292. # $head is a hack but this is just a demo anyway :)
  1293. function doforeach($cmd, $des, $sum, $head, $datetime)
  1294. {
  1295. global $miner, $port;
  1296. global $error, $readonly, $notify, $rigs;
  1297. global $warnfont, $warnoff, $dfmt;
  1298. global $rigerror;
  1299. $when = 0;
  1300. $header = $head;
  1301. $anss = array();
  1302. $count = 0;
  1303. $preverr = count($rigerror);
  1304. foreach ($rigs as $num => $rig)
  1305. {
  1306. $anss[$num] = null;
  1307. if (isset($rigerror[$rig]))
  1308. continue;
  1309. $parts = explode(':', $rig, 3);
  1310. if (count($parts) >= 2)
  1311. {
  1312. $miner = $parts[0];
  1313. $port = $parts[1];
  1314. if (count($parts) > 2)
  1315. $name = $parts[2];
  1316. else
  1317. $name = $num;
  1318. $ans = api($name, $cmd);
  1319. if ($error != null)
  1320. {
  1321. $rw = "<td colspan=100>Error on rig $name getting ";
  1322. $rw .= "$des: $warnfont$error$warnoff</td>";
  1323. otherrow($rw);
  1324. $rigerror[$rig] = $error;
  1325. $error = null;
  1326. }
  1327. else
  1328. {
  1329. $anss[$num] = $ans;
  1330. $count++;
  1331. }
  1332. }
  1333. }
  1334. if ($count == 0)
  1335. {
  1336. $rw = '<td>Failed to access any rigs successfully';
  1337. if ($preverr > 0)
  1338. $rw .= ' (or rigs had previous errors)';
  1339. $rw .= '</td>';
  1340. otherrow($rw);
  1341. return;
  1342. }
  1343. if ($datetime)
  1344. {
  1345. showdatetime();
  1346. endtable();
  1347. newtable();
  1348. showrigs($anss, '', 'Rig ');
  1349. endtable();
  1350. otherrow('<td><br><br></td>');
  1351. newtable();
  1352. return;
  1353. }
  1354. $total = array();
  1355. foreach ($anss as $rig => $ans)
  1356. {
  1357. if ($ans == null)
  1358. continue;
  1359. foreach ($ans as $item => $row)
  1360. {
  1361. if ($item == 'STATUS')
  1362. continue;
  1363. if (count($row) > count($header))
  1364. {
  1365. $header = $head;
  1366. foreach ($row as $name => $value)
  1367. if (!isset($header[$name]))
  1368. $header[$name] = '';
  1369. }
  1370. if ($sum != null)
  1371. foreach ($sum as $name)
  1372. {
  1373. if (isset($row[$name]))
  1374. {
  1375. if (isset($total[$name]))
  1376. $total[$name] += $row[$name];
  1377. else
  1378. $total[$name] = $row[$name];
  1379. }
  1380. }
  1381. }
  1382. }
  1383. if ($sum != null)
  1384. $anss['total']['total'] = $total;
  1385. showhead('', $header);
  1386. foreach ($anss as $rig => $ans)
  1387. {
  1388. if ($ans == null)
  1389. continue;
  1390. $when = 0;
  1391. if (isset($ans['STATUS']['When']))
  1392. $when = $ans['STATUS']['When'];
  1393. foreach ($ans as $item => $row)
  1394. {
  1395. if ($item == 'STATUS')
  1396. continue;
  1397. newrow();
  1398. $section = preg_replace('/\d/', '', $item);
  1399. foreach ($header as $name => $x)
  1400. {
  1401. if ($name == '')
  1402. {
  1403. if ($rig === 'total')
  1404. {
  1405. list($ignore, $class) = fmt($rig, '', '', $when, $row);
  1406. echo "<td align=right$class>Total:</td>";
  1407. }
  1408. else
  1409. echo rigbutton($rig, "Rig $rig", $when, $row);
  1410. }
  1411. else
  1412. {
  1413. if (isset($row[$name]))
  1414. $value = $row[$name];
  1415. else
  1416. $value = null;
  1417. list($showvalue, $class) = fmt($section, $name, $value, $when, $row);
  1418. echo "<td$class align=right>$showvalue</td>";
  1419. }
  1420. }
  1421. endrow();
  1422. }
  1423. }
  1424. }
  1425. #
  1426. function refreshbuttons()
  1427. {
  1428. global $ignorerefresh, $changerefresh, $autorefresh;
  1429. if ($ignorerefresh == false && $changerefresh == true)
  1430. {
  1431. echo '&nbsp;&nbsp;&nbsp;&nbsp;';
  1432. echo "<input type=button value='Auto Refresh:' onclick='prr(true)'>";
  1433. echo "<input type=text name='refval' id='refval' size=2 value='$autorefresh'>";
  1434. echo "<input type=button value='Off' onclick='prr(false)'>";
  1435. }
  1436. }
  1437. #
  1438. function pagebuttons($rig, $pg)
  1439. {
  1440. global $readonly, $rigs, $userlist, $ses;
  1441. global $allowcustompages, $customsummarypages;
  1442. if ($rig === null)
  1443. {
  1444. $prev = null;
  1445. $next = null;
  1446. if ($pg === null)
  1447. $refresh = '';
  1448. else
  1449. $refresh = "&pg=$pg";
  1450. }
  1451. else
  1452. {
  1453. switch (count($rigs))
  1454. {
  1455. case 0:
  1456. case 1:
  1457. $prev = null;
  1458. $next = null;
  1459. break;
  1460. case 2:
  1461. $prev = null;
  1462. $next = ($rig + 1) % count($rigs);
  1463. break;
  1464. default:
  1465. $prev = ($rig - 1) % count($rigs);
  1466. $next = ($rig + 1) % count($rigs);
  1467. break;
  1468. }
  1469. $refresh = "&rig=$rig";
  1470. }
  1471. echo '<tr><td><table cellpadding=0 cellspacing=0 border=0><tr><td nowrap>';
  1472. if ($userlist === null || isset($_SESSION[$ses]))
  1473. {
  1474. if ($prev !== null)
  1475. echo riginput($prev, 'Prev').'&nbsp;';
  1476. echo "<input type=button value='Refresh' onclick='pr(\"$refresh\",null)'>&nbsp;";
  1477. if ($next !== null)
  1478. echo riginput($next, 'Next').'&nbsp;';
  1479. echo '&nbsp;';
  1480. if (count($rigs) > 1)
  1481. echo "<input type=button value='Summary' onclick='pr(\"\",null)'>&nbsp;";
  1482. }
  1483. if ($allowcustompages === true)
  1484. {
  1485. if ($userlist === null || isset($_SESSION[$ses]))
  1486. $list = $customsummarypages;
  1487. else
  1488. {
  1489. if ($userlist !== null && isset($userlist['def']))
  1490. $list = array_flip($userlist['def']);
  1491. else
  1492. $list = array();
  1493. }
  1494. foreach ($list as $pagename => $data)
  1495. echo "<input type=button value='$pagename' onclick='pr(\"&pg=$pagename\",null)'>&nbsp;";
  1496. }
  1497. echo '</td><td width=100%>&nbsp;</td><td nowrap>';
  1498. if ($rig !== null && $readonly === false)
  1499. {
  1500. $rg = '';
  1501. if (count($rigs) > 1)
  1502. $rg = " Rig $rig";
  1503. echo "<input type=button value='Restart' onclick='prc(\"restart&rig=$rig\",\"Restart BFGMiner$rg\")'>";
  1504. echo "&nbsp;<input type=button value='Quit' onclick='prc(\"quit&rig=$rig\",\"Quit BFGMiner$rg\")'>";
  1505. }
  1506. refreshbuttons();
  1507. if (isset($_SESSION[$ses]))
  1508. echo "&nbsp;<input type=button value='Logout' onclick='pr(\"&logout=1\",null)'>";
  1509. else
  1510. if ($userlist !== null)
  1511. echo "&nbsp;<input type=button value='Login' onclick='pr(\"&login=1\",null)'>";
  1512. echo "</td></tr></table></td></tr>";
  1513. }
  1514. #
  1515. function doOne($rig, $preprocess)
  1516. {
  1517. global $haderror, $readonly, $notify, $rigs;
  1518. global $placebuttons;
  1519. htmlhead(true, $rig);
  1520. if ($placebuttons == 'top' || $placebuttons == 'both')
  1521. pagebuttons($rig, null);
  1522. if ($preprocess != null)
  1523. process(array($preprocess => $preprocess), $rig);
  1524. $cmds = array( 'devs' => 'device list',
  1525. 'summary' => 'summary information',
  1526. 'pools' => 'pool list');
  1527. if ($notify)
  1528. $cmds['notify'] = 'device status';
  1529. $cmds['config'] = 'BFGMiner config';
  1530. process($cmds, $rig);
  1531. if ($haderror == false && $readonly === false)
  1532. processgpus($rig);
  1533. if ($placebuttons == 'bot' || $placebuttons == 'both')
  1534. pagebuttons($rig, null);
  1535. }
  1536. #
  1537. global $sectionmap;
  1538. # map sections to their api command
  1539. # DEVS is a special case that will match GPU or PGA
  1540. # so you can have a single table with both in it
  1541. # DATE is hard coded so not in here
  1542. $sectionmap = array(
  1543. 'RIGS' => 'version',
  1544. 'SUMMARY' => 'summary',
  1545. 'POOL' => 'pools',
  1546. 'DEVS' => 'devs',
  1547. 'GPU' => 'devs', // You would normally use DEVS
  1548. 'PGA' => 'devs', // You would normally use DEVS
  1549. 'NOTIFY' => 'notify',
  1550. 'DEVDETAILS' => 'devdetails',
  1551. 'STATS' => 'stats',
  1552. 'CONFIG' => 'config',
  1553. 'COIN' => 'coin');
  1554. #
  1555. function joinfields($section1, $section2, $join, $results)
  1556. {
  1557. global $sectionmap;
  1558. $name1 = $sectionmap[$section1];
  1559. $name2 = $sectionmap[$section2];
  1560. $newres = array();
  1561. // foreach rig in section1
  1562. foreach ($results[$name1] as $rig => $result)
  1563. {
  1564. $status = null;
  1565. // foreach answer section in the rig api call
  1566. foreach ($result as $name1b => $fields1b)
  1567. {
  1568. if ($name1b == 'STATUS')
  1569. {
  1570. // remember the STATUS from section1
  1571. $status = $result[$name1b];
  1572. continue;
  1573. }
  1574. // foreach answer section in the rig api call (for the other api command)
  1575. foreach ($results[$name2][$rig] as $name2b => $fields2b)
  1576. {
  1577. if ($name2b == 'STATUS')
  1578. continue;
  1579. // If match the same field values of fields in $join
  1580. $match = true;
  1581. foreach ($join as $field)
  1582. if ($fields1b[$field] != $fields2b[$field])
  1583. {
  1584. $match = false;
  1585. break;
  1586. }
  1587. if ($match === true)
  1588. {
  1589. if ($status != null)
  1590. {
  1591. $newres[$rig]['STATUS'] = $status;
  1592. $status = null;
  1593. }
  1594. $subsection = $section1.'+'.$section2;
  1595. $subsection .= preg_replace('/[^0-9]/', '', $name1b.$name2b);
  1596. foreach ($fields1b as $nam => $val)
  1597. $newres[$rig][$subsection]["$section1.$nam"] = $val;
  1598. foreach ($fields2b as $nam => $val)
  1599. $newres[$rig][$subsection]["$section2.$nam"] = $val;
  1600. }
  1601. }
  1602. }
  1603. }
  1604. return $newres;
  1605. }
  1606. #
  1607. function join_get_field($field, $fields)
  1608. {
  1609. // : means a string constant otherwise it's a field name
  1610. // ProcID field name is converted to a lowercase letter
  1611. if (substr($field, 0, 1) == ':')
  1612. return substr($field, 1);
  1613. else
  1614. if ($field == 'ProcID')
  1615. return chr(97 + $fields[$field]);
  1616. else
  1617. return $fields[$field];
  1618. }
  1619. #
  1620. function joinlr($section1, $section2, $join, $results)
  1621. {
  1622. global $sectionmap;
  1623. $name1 = $sectionmap[$section1];
  1624. $name2 = $sectionmap[$section2];
  1625. $newres = array();
  1626. // foreach rig in section1
  1627. foreach ($results[$name1] as $rig => $result)
  1628. {
  1629. $status = null;
  1630. // foreach answer section in the rig api call
  1631. foreach ($result as $name1b => $fields1b)
  1632. {
  1633. if ($name1b == 'STATUS')
  1634. {
  1635. // remember the STATUS from section1
  1636. $status = $result[$name1b];
  1637. continue;
  1638. }
  1639. // Build L string to be matched
  1640. $Lval = '';
  1641. foreach ($join['L'] as $field)
  1642. $Lval .= join_get_field($field, $fields1b);
  1643. // foreach answer section in the rig api call (for the other api command)
  1644. foreach ($results[$name2][$rig] as $name2b => $fields2b)
  1645. {
  1646. if ($name2b == 'STATUS')
  1647. continue;
  1648. // Build R string and compare
  1649. $Rval = '';
  1650. foreach ($join['R'] as $field)
  1651. $Rval .= join_get_field($field, $fields2b);
  1652. if ($Lval === $Rval)
  1653. {
  1654. if ($status != null)
  1655. {
  1656. $newres[$rig]['STATUS'] = $status;
  1657. $status = null;
  1658. }
  1659. $subsection = $section1.'+'.$section2;
  1660. $subsection .= preg_replace('/[^0-9]/', '', $name1b.$name2b);
  1661. foreach ($fields1b as $nam => $val)
  1662. $newres[$rig][$subsection]["$section1.$nam"] = $val;
  1663. foreach ($fields2b as $nam => $val)
  1664. $newres[$rig][$subsection]["$section2.$nam"] = $val;
  1665. }
  1666. }
  1667. }
  1668. }
  1669. return $newres;
  1670. }
  1671. #
  1672. function joinall($section1, $section2, $results)
  1673. {
  1674. global $sectionmap;
  1675. $name1 = $sectionmap[$section1];
  1676. $name2 = $sectionmap[$section2];
  1677. $newres = array();
  1678. // foreach rig in section1
  1679. foreach ($results[$name1] as $rig => $result)
  1680. {
  1681. // foreach answer section in the rig api call
  1682. foreach ($result as $name1b => $fields1b)
  1683. {
  1684. if ($name1b == 'STATUS')
  1685. {
  1686. // copy the STATUS from section1
  1687. $newres[$rig][$name1b] = $result[$name1b];
  1688. continue;
  1689. }
  1690. // foreach answer section in the rig api call (for the other api command)
  1691. foreach ($results[$name2][$rig] as $name2b => $fields2b)
  1692. {
  1693. if ($name2b == 'STATUS')
  1694. continue;
  1695. $subsection = $section1.'+'.$section2;
  1696. $subsection .= preg_replace('/[^0-9]/', '', $name1b.$name2b);
  1697. foreach ($fields1b as $nam => $val)
  1698. $newres[$rig][$subsection]["$section1.$nam"] = $val;
  1699. foreach ($fields2b as $nam => $val)
  1700. $newres[$rig][$subsection]["$section2.$nam"] = $val;
  1701. }
  1702. }
  1703. }
  1704. return $newres;
  1705. }
  1706. #
  1707. function joinsections($sections, $results, $errors)
  1708. {
  1709. global $sectionmap;
  1710. // GPU's don't have Name,ID,ProcID fields - so create them
  1711. foreach ($results as $section => $res)
  1712. foreach ($res as $rig => $result)
  1713. foreach ($result as $name => $fields)
  1714. {
  1715. $subname = preg_replace('/[0-9]/', '', $name);
  1716. if ($subname == 'GPU' and isset($result[$name]['GPU']))
  1717. {
  1718. $results[$section][$rig][$name]['Name'] = 'GPU';
  1719. $results[$section][$rig][$name]['ID'] = $result[$name]['GPU'];
  1720. $results[$section][$rig][$name]['ProcID'] = 0;
  1721. }
  1722. }
  1723. foreach ($sections as $section => $fields)
  1724. if ($section != 'DATE' && !isset($sectionmap[$section]))
  1725. {
  1726. $both = explode('+', $section, 2);
  1727. if (count($both) > 1)
  1728. {
  1729. switch($both[0])
  1730. {
  1731. case 'SUMMARY':
  1732. switch($both[1])
  1733. {
  1734. case 'POOL':
  1735. case 'DEVS':
  1736. case 'CONFIG':
  1737. case 'COIN':
  1738. $sectionmap[$section] = $section;
  1739. $results[$section] = joinall($both[0], $both[1], $results);
  1740. break;
  1741. default:
  1742. $errors[] = "Error: Invalid section '$section'";
  1743. break;
  1744. }
  1745. break;
  1746. case 'DEVS':
  1747. switch($both[1])
  1748. {
  1749. case 'NOTIFY':
  1750. case 'DEVDETAILS':
  1751. case 'USBSTATS':
  1752. $join = array('Name', 'ID', 'ProcID');
  1753. $sectionmap[$section] = $section;
  1754. $results[$section] = joinfields($both[0], $both[1], $join, $results);
  1755. break;
  1756. case 'STATS':
  1757. $join = array('L' => array('Name','ID','ProcID'), 'R' => array('ID'));
  1758. $sectionmap[$section] = $section;
  1759. $results[$section] = joinlr($both[0], $both[1], $join, $results);
  1760. break;
  1761. default:
  1762. $errors[] = "Error: Invalid section '$section'";
  1763. break;
  1764. }
  1765. break;
  1766. case 'POOL':
  1767. switch($both[1])
  1768. {
  1769. case 'STATS':
  1770. $join = array('L' => array(':POOL','POOL'), 'R' => array('ID'));
  1771. $sectionmap[$section] = $section;
  1772. $results[$section] = joinlr($both[0], $both[1], $join, $results);
  1773. break;
  1774. default:
  1775. $errors[] = "Error: Invalid section '$section'";
  1776. break;
  1777. }
  1778. break;
  1779. default:
  1780. $errors[] = "Error: Invalid section '$section'";
  1781. break;
  1782. }
  1783. }
  1784. else
  1785. $errors[] = "Error: Invalid section '$section'";
  1786. }
  1787. return array($results, $errors);
  1788. }
  1789. #
  1790. function secmatch($section, $field)
  1791. {
  1792. if ($section == $field)
  1793. return true;
  1794. if ($section == 'DEVS'
  1795. && ($field == 'GPU' || $field == 'PGA'))
  1796. return true;
  1797. return false;
  1798. }
  1799. #
  1800. function customset($showfields, $sum, $section, $rig, $isbutton, $result, $total)
  1801. {
  1802. foreach ($result as $sec => $row)
  1803. {
  1804. $secname = preg_replace('/\d/', '', $sec);
  1805. if ($sec != 'total')
  1806. if (!secmatch($section, $secname))
  1807. continue;
  1808. newrow();
  1809. $when = 0;
  1810. if (isset($result['STATUS']['When']))
  1811. $when = $result['STATUS']['When'];
  1812. if ($isbutton)
  1813. echo rigbutton($rig, $rig, $when, $row);
  1814. else
  1815. {
  1816. list($ignore, $class) = fmt('total', '', '', $when, $row);
  1817. echo "<td align=middle$class>$rig</td>";
  1818. }
  1819. foreach ($showfields as $name => $one)
  1820. {
  1821. if (isset($row[$name]))
  1822. {
  1823. $value = $row[$name];
  1824. if (isset($sum[$section][$name]))
  1825. {
  1826. if (isset($total[$name]))
  1827. $total[$name] += $value;
  1828. else
  1829. $total[$name] = $value;
  1830. }
  1831. }
  1832. else
  1833. {
  1834. if ($sec == 'total' && isset($total[$name]))
  1835. $value = $total[$name];
  1836. else
  1837. $value = null;
  1838. }
  1839. if (strpos($secname, '+') === false)
  1840. list($showvalue, $class) = fmt($secname, $name, $value, $when, $row);
  1841. else
  1842. {
  1843. $parts = explode('.', $name, 2);
  1844. list($showvalue, $class) = fmt($parts[0], $parts[1], $value, $when, $row);
  1845. }
  1846. echo "<td$class align=right>$showvalue</td>";
  1847. }
  1848. endrow();
  1849. }
  1850. return $total;
  1851. }
  1852. #
  1853. function docalc($func, $data)
  1854. {
  1855. switch ($func)
  1856. {
  1857. case 'sum':
  1858. $tot = 0;
  1859. foreach ($data as $val)
  1860. $tot += $val;
  1861. return $tot;
  1862. case 'avg':
  1863. $tot = 0;
  1864. foreach ($data as $val)
  1865. $tot += $val;
  1866. return ($tot / count($data));
  1867. case 'min':
  1868. $ans = null;
  1869. foreach ($data as $val)
  1870. if ($ans === null)
  1871. $ans = $val;
  1872. else
  1873. if ($val < $ans)
  1874. $ans = $val;
  1875. return $ans;
  1876. case 'max':
  1877. $ans = null;
  1878. foreach ($data as $val)
  1879. if ($ans === null)
  1880. $ans = $val;
  1881. else
  1882. if ($val > $ans)
  1883. $ans = $val;
  1884. return $ans;
  1885. case 'lo':
  1886. $ans = null;
  1887. foreach ($data as $val)
  1888. if ($ans === null)
  1889. $ans = $val;
  1890. else
  1891. if (strcasecmp($val, $ans) < 0)
  1892. $ans = $val;
  1893. return $ans;
  1894. case 'hi':
  1895. $ans = null;
  1896. foreach ($data as $val)
  1897. if ($ans === null)
  1898. $ans = $val;
  1899. else
  1900. if (strcasecmp($val, $ans) > 0)
  1901. $ans = $val;
  1902. return $ans;
  1903. case 'any':
  1904. default:
  1905. return $data[0];
  1906. }
  1907. }
  1908. #
  1909. function docompare($row, $test)
  1910. {
  1911. // invalid $test data means true
  1912. if (count($test) < 2)
  1913. return true;
  1914. if (isset($row[$test[0]]))
  1915. $val = $row[$test[0]];
  1916. else
  1917. $val = null;
  1918. if ($test[1] == 'set')
  1919. return ($val !== null);
  1920. if ($val === null || count($test) < 3)
  1921. return true;
  1922. switch($test[1])
  1923. {
  1924. case '=':
  1925. return ($val == $test[2]);
  1926. case '<':
  1927. return ($val < $test[2]);
  1928. case '<=':
  1929. return ($val <= $test[2]);
  1930. case '>':
  1931. return ($val > $test[2]);
  1932. case '>=':
  1933. return ($val >= $test[2]);
  1934. case 'eq':
  1935. return (strcasecmp($val, $test[2]) == 0);
  1936. case 'lt':
  1937. return (strcasecmp($val, $test[2]) < 0);
  1938. case 'le':
  1939. return (strcasecmp($val, $test[2]) <= 0);
  1940. case 'gt':
  1941. return (strcasecmp($val, $test[2]) > 0);
  1942. case 'ge':
  1943. return (strcasecmp($val, $test[2]) >= 0);
  1944. default:
  1945. return true;
  1946. }
  1947. }
  1948. #
  1949. function processcompare($which, $ext, $section, $res)
  1950. {
  1951. if (isset($ext[$section][$which]))
  1952. {
  1953. $proc = $ext[$section][$which];
  1954. if ($proc !== null)
  1955. {
  1956. $res2 = array();
  1957. foreach ($res as $rig => $result)
  1958. foreach ($result as $sec => $row)
  1959. {
  1960. $secname = preg_replace('/\d/', '', $sec);
  1961. if (!secmatch($section, $secname))
  1962. $res2[$rig][$sec] = $row;
  1963. else
  1964. {
  1965. $keep = true;
  1966. foreach ($proc as $test)
  1967. if (!docompare($row, $test))
  1968. {
  1969. $keep = false;
  1970. break;
  1971. }
  1972. if ($keep)
  1973. $res2[$rig][$sec] = $row;
  1974. }
  1975. }
  1976. $res = $res2;
  1977. }
  1978. }
  1979. return $res;
  1980. }
  1981. #
  1982. function processext($ext, $section, $res)
  1983. {
  1984. $res = processcompare('where', $ext, $section, $res);
  1985. if (isset($ext[$section]['group']))
  1986. {
  1987. $grp = $ext[$section]['group'];
  1988. $calc = $ext[$section]['calc'];
  1989. if ($grp !== null)
  1990. {
  1991. $interim = array();
  1992. $res2 = array();
  1993. $cou = 0;
  1994. foreach ($res as $rig => $result)
  1995. foreach ($result as $sec => $row)
  1996. {
  1997. $secname = preg_replace('/\d/', '', $sec);
  1998. if (!secmatch($section, $secname))
  1999. {
  2000. // STATUS may be problematic ...
  2001. if (!isset($res2[$sec]))
  2002. $res2[$sec] = $row;
  2003. }
  2004. else
  2005. {
  2006. $grpkey = '';
  2007. $newrow = array();
  2008. foreach ($grp as $field)
  2009. {
  2010. if (isset($row[$field]))
  2011. {
  2012. $grpkey .= $row[$field].'.';
  2013. $newrow[$field] = $row[$field];
  2014. }
  2015. else
  2016. $grpkey .= '.';
  2017. }
  2018. if (!isset($interim[$grpkey]))
  2019. {
  2020. $interim[$grpkey]['grp'] = $newrow;
  2021. $interim[$grpkey]['sec'] = $secname.$cou;
  2022. $cou++;
  2023. }
  2024. if ($calc !== null)
  2025. foreach ($calc as $field => $func)
  2026. {
  2027. if (!isset($interim[$grpkey]['cal'][$field]))
  2028. $interim[$grpkey]['cal'][$field] = array();
  2029. $interim[$grpkey]['cal'][$field][] = $row[$field];
  2030. }
  2031. }
  2032. }
  2033. // Build the rest of $res2 from $interim
  2034. foreach ($interim as $rowkey => $row)
  2035. {
  2036. $key = $row['sec'];
  2037. foreach ($row['grp'] as $field => $value)
  2038. $res2[$key][$field] = $value;
  2039. foreach ($row['cal'] as $field => $data)
  2040. $res2[$key][$field] = docalc($calc[$field], $data);
  2041. }
  2042. $res = array('' => $res2);
  2043. }
  2044. }
  2045. return processcompare('having', $ext, $section, $res);
  2046. }
  2047. #
  2048. function processcustompage($pagename, $sections, $sum, $ext, $namemap)
  2049. {
  2050. global $sectionmap;
  2051. global $miner, $port;
  2052. global $rigs, $error;
  2053. global $warnfont, $warnoff;
  2054. global $dfmt;
  2055. global $readonly, $showndate;
  2056. $cmds = array();
  2057. $errors = array();
  2058. foreach ($sections as $section => $fields)
  2059. {
  2060. $all = explode('+', $section);
  2061. foreach ($all as $section)
  2062. {
  2063. if (isset($sectionmap[$section]))
  2064. {
  2065. $cmd = $sectionmap[$section];
  2066. if (!isset($cmds[$cmd]))
  2067. $cmds[$cmd] = 1;
  2068. }
  2069. else
  2070. if ($section != 'DATE')
  2071. $errors[] = "Error: unknown section '$section' in custom summary page '$pagename'";
  2072. }
  2073. }
  2074. $results = array();
  2075. foreach ($rigs as $num => $rig)
  2076. {
  2077. $parts = explode(':', $rig, 3);
  2078. if (count($parts) >= 2)
  2079. {
  2080. $miner = $parts[0];
  2081. $port = $parts[1];
  2082. if (count($parts) > 2)
  2083. $name = $parts[2];
  2084. else
  2085. $name = $rig;
  2086. foreach ($cmds as $cmd => $one)
  2087. {
  2088. $process = api($name, $cmd);
  2089. if ($error != null)
  2090. {
  2091. $errors[] = "Error getting $cmd for $name $warnfont$error$warnoff";
  2092. break;
  2093. }
  2094. else
  2095. $results[$cmd][$num] = $process;
  2096. }
  2097. }
  2098. }
  2099. $shownsomething = false;
  2100. if (count($results) > 0)
  2101. {
  2102. list($results, $errors) = joinsections($sections, $results, $errors);
  2103. $first = true;
  2104. foreach ($sections as $section => $fields)
  2105. {
  2106. if ($section === 'DATE')
  2107. {
  2108. if ($shownsomething)
  2109. otherrow('<td>&nbsp;</td>');
  2110. newtable();
  2111. showdatetime();
  2112. endtable();
  2113. // On top of the next table
  2114. $shownsomething = false;
  2115. continue;
  2116. }
  2117. if ($section === 'RIGS')
  2118. {
  2119. if ($shownsomething)
  2120. otherrow('<td>&nbsp;</td>');
  2121. newtable();
  2122. showrigs($results['version'], 'Rig', '');
  2123. endtable();
  2124. $shownsomething = true;
  2125. continue;
  2126. }
  2127. if (isset($results[$sectionmap[$section]]))
  2128. {
  2129. $rigresults = processext($ext, $section, $results[$sectionmap[$section]]);
  2130. $showfields = array();
  2131. $showhead = array();
  2132. foreach ($fields as $field)
  2133. foreach ($rigresults as $result)
  2134. foreach ($result as $sec => $row)
  2135. {
  2136. $secname = preg_replace('/\d/', '', $sec);
  2137. if (secmatch($section, $secname))
  2138. {
  2139. if ($field === '*')
  2140. {
  2141. foreach ($row as $f => $v)
  2142. {
  2143. $showfields[$f] = 1;
  2144. $map = $section.'.'.$f;
  2145. if (isset($namemap[$map]))
  2146. $showhead[$namemap[$map]] = 1;
  2147. else
  2148. $showhead[$f] = 1;
  2149. }
  2150. }
  2151. elseif (isset($row[$field]))
  2152. {
  2153. $showfields[$field] = 1;
  2154. $map = $section.'.'.$field;
  2155. if (isset($namemap[$map]))
  2156. $showhead[$namemap[$map]] = 1;
  2157. else
  2158. $showhead[$field] = 1;
  2159. }
  2160. }
  2161. }
  2162. if (count($showfields) > 0)
  2163. {
  2164. if ($shownsomething)
  2165. otherrow('<td>&nbsp;</td>');
  2166. newtable();
  2167. if (count($rigresults) == 1 && isset($rigresults['']))
  2168. $ri = array('' => 1) + $showhead;
  2169. else
  2170. $ri = array('Rig' => 1) + $showhead;
  2171. showhead('', $ri, true);
  2172. $total = array();
  2173. $add = array('total' => array());
  2174. foreach ($rigresults as $num => $result)
  2175. $total = customset($showfields, $sum, $section, $num, true, $result, $total);
  2176. if (count($total) > 0)
  2177. customset($showfields, $sum, $section, '&Sigma;', false, $add, $total);
  2178. $first = false;
  2179. endtable();
  2180. $shownsomething = true;
  2181. }
  2182. }
  2183. }
  2184. }
  2185. if (count($errors) > 0)
  2186. {
  2187. if (count($results) > 0)
  2188. otherrow('<td>&nbsp;</td>');
  2189. foreach ($errors as $err)
  2190. otherrow("<td colspan=100>$err</td>");
  2191. }
  2192. }
  2193. #
  2194. function showcustompage($pagename)
  2195. {
  2196. global $customsummarypages;
  2197. global $placebuttons;
  2198. htmlhead(false, null, $pagename);
  2199. if ($placebuttons == 'top' || $placebuttons == 'both')
  2200. pagebuttons(null, $pagename);
  2201. if (!isset($customsummarypages[$pagename]))
  2202. {
  2203. otherrow("<td colspan=100>Unknown custom summary page '$pagename'</td>");
  2204. return;
  2205. }
  2206. $c = count($customsummarypages[$pagename]);
  2207. if ($c < 2 || $c > 3)
  2208. {
  2209. $rw = "<td colspan=100>Invalid custom summary page '$pagename' (";
  2210. $rw .= count($customsummarypages[$pagename]).')</td>';
  2211. otherrow($rw);
  2212. return;
  2213. }
  2214. $page = $customsummarypages[$pagename][0];
  2215. $namemap = array();
  2216. foreach ($page as $name => $fields)
  2217. {
  2218. if ($fields === null)
  2219. $page[$name] = array();
  2220. else
  2221. foreach ($fields as $num => $field)
  2222. {
  2223. $pos = strpos($field, '=');
  2224. if ($pos !== false)
  2225. {
  2226. $names = explode('=', $field, 2);
  2227. if (strlen($names[1]) > 0)
  2228. $namemap[$name.'.'.$names[0]] = $names[1];
  2229. $page[$name][$num] = $names[0];
  2230. }
  2231. }
  2232. }
  2233. $ext = null;
  2234. if (isset($customsummarypages[$pagename][2]))
  2235. $ext = $customsummarypages[$pagename][2];
  2236. $sum = $customsummarypages[$pagename][1];
  2237. if ($sum === null)
  2238. $sum = array();
  2239. // convert them to searchable via isset()
  2240. foreach ($sum as $section => $fields)
  2241. {
  2242. $newfields = array();
  2243. foreach ($fields as $field)
  2244. $newfields[$field] = 1;
  2245. $sum[$section] = $newfields;
  2246. }
  2247. if (count($page) <= 1)
  2248. {
  2249. otherrow("<td colspan=100>Invalid custom summary page '$pagename' no content </td>");
  2250. return;
  2251. }
  2252. processcustompage($pagename, $page, $sum, $ext, $namemap);
  2253. if ($placebuttons == 'bot' || $placebuttons == 'both')
  2254. pagebuttons(null, $pagename);
  2255. }
  2256. #
  2257. function onlylogin()
  2258. {
  2259. global $here;
  2260. htmlhead(false, null, null, true);
  2261. ?>
  2262. <tr height=15%><td>&nbsp;</td></tr>
  2263. <tr><td>
  2264. <center>
  2265. <table width=384 height=368 cellpadding=0 cellspacing=0 border=0>
  2266. <tr><td>
  2267. <table width=100% height=100% border=0 align=center cellpadding=5 cellspacing=0>
  2268. <tr><td><form action='<?php echo $here; ?>' method=post>
  2269. <table width=200 border=0 align=center cellpadding=5 cellspacing=0>
  2270. <tr><td height=120 colspan=2>&nbsp;</td></tr>
  2271. <tr><td colspan=2 align=center valign=middle>
  2272. <h2>LOGIN</h2></td></tr>
  2273. <tr><td align=center valign=middle><div align=right>Username:</div></td>
  2274. <td height=33 align=center valign=middle>
  2275. <input type=text name=rut size=18></td></tr>
  2276. <tr><td align=center valign=middle><div align=right>Password:</div></td>
  2277. <td height=33 align=center valign=middle>
  2278. <input type=password name=roh size=18></td></tr>
  2279. <tr valign=top><td></td><td><input type=submit value=Login>
  2280. </td></tr>
  2281. </table></form></td></tr>
  2282. </table></td></tr>
  2283. </table></center>
  2284. </td></tr>
  2285. <?php
  2286. }
  2287. #
  2288. function checklogin()
  2289. {
  2290. global $allowcustompages, $customsummarypages;
  2291. global $readonly, $userlist, $ses;
  2292. $out = trim(getparam('logout', true));
  2293. if ($out !== null && $out !== '' && isset($_SESSION[$ses]))
  2294. unset($_SESSION[$ses]);
  2295. $login = trim(getparam('login', true));
  2296. if ($login !== null && $login !== '')
  2297. {
  2298. if (isset($_SESSION[$ses]))
  2299. unset($_SESSION[$ses]);
  2300. onlylogin();
  2301. return 'login';
  2302. }
  2303. if ($userlist === null)
  2304. return false;
  2305. $rut = trim(getparam('rut', true));
  2306. $roh = trim(getparam('roh', true));
  2307. if (($rut !== null && $rut !== '') || ($roh !== null && $roh !== ''))
  2308. {
  2309. if (isset($_SESSION[$ses]))
  2310. unset($_SESSION[$ses]);
  2311. if ($rut !== null && $rut !== '' && $roh !== null && $roh !== '')
  2312. {
  2313. if (isset($userlist['sys']) && isset($userlist['sys'][$rut])
  2314. && ($userlist['sys'][$rut] === $roh))
  2315. {
  2316. $_SESSION[$ses] = true;
  2317. return false;
  2318. }
  2319. if (isset($userlist['usr']) && isset($userlist['usr'][$rut])
  2320. && ($userlist['usr'][$rut] === $roh))
  2321. {
  2322. $_SESSION[$ses] = false;
  2323. $readonly = true;
  2324. return false;
  2325. }
  2326. }
  2327. }
  2328. if (isset($_SESSION[$ses]))
  2329. {
  2330. if ($_SESSION[$ses] == false)
  2331. $readonly = true;
  2332. return false;
  2333. }
  2334. if (isset($userlist['def']) && $allowcustompages === true)
  2335. {
  2336. // Ensure at least one exists
  2337. foreach ($userlist['def'] as $pg)
  2338. if (isset($customsummarypages[$pg]))
  2339. return true;
  2340. }
  2341. onlylogin();
  2342. return 'login';
  2343. }
  2344. #
  2345. function display()
  2346. {
  2347. global $miner, $port;
  2348. global $readonly, $notify, $rigs;
  2349. global $ignorerefresh, $autorefresh;
  2350. global $allowcustompages, $customsummarypages;
  2351. global $placebuttons;
  2352. global $userlist, $ses;
  2353. $pagesonly = checklogin();
  2354. if ($pagesonly === 'login')
  2355. return;
  2356. if ($rigs == null or count($rigs) == 0)
  2357. {
  2358. otherrow("<td>No rigs defined</td>");
  2359. return;
  2360. }
  2361. if ($ignorerefresh == false)
  2362. {
  2363. $ref = trim(getparam('ref', true));
  2364. if ($ref != null && $ref != '')
  2365. $autorefresh = intval($ref);
  2366. }
  2367. if ($pagesonly !== true)
  2368. {
  2369. $rig = trim(getparam('rig', true));
  2370. $arg = trim(getparam('arg', true));
  2371. $preprocess = null;
  2372. if ($arg != null and $arg != '')
  2373. {
  2374. if ($rig != null and $rig != '' and $rig >= 0 and $rig < count($rigs))
  2375. {
  2376. $parts = explode(':', $rigs[$rig], 3);
  2377. if (count($parts) >= 2)
  2378. {
  2379. $miner = $parts[0];
  2380. $port = $parts[1];
  2381. if ($readonly !== true)
  2382. $preprocess = $arg;
  2383. }
  2384. }
  2385. }
  2386. }
  2387. if ($allowcustompages === true)
  2388. {
  2389. $pg = trim(getparam('pg', true));
  2390. if ($pagesonly === true)
  2391. {
  2392. if ($pg !== null && $pg !== '')
  2393. {
  2394. if ($userlist !== null && isset($userlist['def'])
  2395. && !in_array($pg, $userlist['def']))
  2396. $pg = null;
  2397. }
  2398. else
  2399. {
  2400. if ($userlist !== null && isset($userlist['def']))
  2401. foreach ($userlist['def'] as $pglook)
  2402. if (isset($customsummarypages[$pglook]))
  2403. {
  2404. $pg = $pglook;
  2405. break;
  2406. }
  2407. }
  2408. }
  2409. if ($pg !== null && $pg !== '')
  2410. {
  2411. showcustompage($pg);
  2412. return;
  2413. }
  2414. }
  2415. if ($pagesonly === true)
  2416. {
  2417. onlylogin();
  2418. return;
  2419. }
  2420. if (count($rigs) == 1)
  2421. {
  2422. $parts = explode(':', $rigs[0], 3);
  2423. if (count($parts) >= 2)
  2424. {
  2425. $miner = $parts[0];
  2426. $port = $parts[1];
  2427. doOne(0, $preprocess);
  2428. }
  2429. else
  2430. otherrow('<td>Invalid "$rigs" array</td>');
  2431. return;
  2432. }
  2433. if ($rig != null and $rig != '' and $rig >= 0 and $rig < count($rigs))
  2434. {
  2435. $parts = explode(':', $rigs[$rig], 3);
  2436. if (count($parts) >= 2)
  2437. {
  2438. $miner = $parts[0];
  2439. $port = $parts[1];
  2440. doOne($rig, $preprocess);
  2441. }
  2442. else
  2443. otherrow('<td>Invalid "$rigs" array</td>');
  2444. return;
  2445. }
  2446. htmlhead(false, null);
  2447. if ($placebuttons == 'top' || $placebuttons == 'both')
  2448. pagebuttons(null, null);
  2449. if ($preprocess != null)
  2450. process(array($preprocess => $preprocess), $rig);
  2451. newtable();
  2452. doforeach('version', 'rig summary', array(), array(), true);
  2453. $sum = array('MHS av', 'Getworks', 'Found Blocks', 'Accepted', 'Rejected', 'Discarded', 'Stale', 'Utility', 'Local Work', 'Total MH');
  2454. doforeach('summary', 'summary information', $sum, array(), false);
  2455. endtable();
  2456. otherrow('<td><br><br></td>');
  2457. newtable();
  2458. doforeach('devs', 'device list', $sum, array(''=>'','ProcID'=>'','ID'=>'','Name'=>''), false);
  2459. endtable();
  2460. otherrow('<td><br><br></td>');
  2461. newtable();
  2462. doforeach('pools', 'pool list', $sum, array(''=>''), false);
  2463. endtable();
  2464. if ($placebuttons == 'bot' || $placebuttons == 'both')
  2465. pagebuttons(null, null);
  2466. }
  2467. #
  2468. display();
  2469. #
  2470. ?>
  2471. </table></td></tr></table>
  2472. </body></html>