miner.php 60 KB

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