miner.php 60 KB

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