miner.php 59 KB

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