miner.php 61 KB

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