miner.php 59 KB

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