miner.php 59 KB

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