miner.php 59 KB

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