miner.php 56 KB

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