miner.php 60 KB

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