miner.php 55 KB

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