Browse Source

php - add an API interface for users

master
kanoi 10 years ago
parent
commit
d22fdf78df
  1. 13
      pool/db.php
  2. 38
      pool/page_api.php
  3. 92
      pool/page_userset.php
  4. 10
      pool/prime.php

13
pool/db.php

@ -71,6 +71,19 @@ function zeip()
return $_SERVER['REMOTE_ADDR'];
}
#
function fldEncode($flds, $name, $first)
{
global $fld_sep, $val_sep;
if ($first)
$rep = '';
else
$rep = $fld_sep;
$rep .= $name . $val_sep;
if (isset($flds[$name]))
$rep .= $flds[$name];
return $rep;
}
#
function msgEncode($cmd, $id, $fields, $user)
{
global $send_sep, $fld_sep, $val_sep;

38
pool/page_api.php

@ -0,0 +1,38 @@
<?php
#
function no_api()
{
echo 'nil';
exit(0);
}
#
function show_api($page, $menu, $name, $user)
{
$u = getparam('username', true);
$api = getparam('api', true);
$ans = getAtts($u, 'KAPIKey.str');
if ($ans['STATUS'] != 'ok')
no_api();
if (!isset($ans['KAPIKey.str']))
no_api();
if ($ans['KAPIKey.str'] != $api)
no_api();
$ans = homeInfo($u);
if ($ans === false)
no_api();
$rep = fldEncode($ans, 'lastbc', true);
$rep .= fldEncode($ans, 'lastheight', false);
$rep .= fldEncode($ans, 'currndiff', false);
$rep .= fldEncode($ans, 'lastblock', false);
$rep .= fldEncode($ans, 'lastblockheight', false);
$rep .= fldEncode($ans, 'blockacc', false);
$rep .= fldEncode($ans, 'blockerr', false);
$rep .= fldEncode($ans, 'p_hashrate5m', false);
$rep .= fldEncode($ans, 'p_hashrate1hr', false);
$rep .= fldEncode($ans, 'u_hashrate5m', false);
$rep .= fldEncode($ans, 'u_hashrate1hr', false);
echo $rep;
exit(0);
}
#
?>

92
pool/page_userset.php

@ -0,0 +1,92 @@
<?php
#
function uset($data, $user, $api, $err)
{
$pg = '<h1>User Settings</h1>';
if ($err != '')
$pg .= "<span class=err>$err<br><br></span>";
$pg .= '<table cellpadding=20 cellspacing=0 border=1>';
$pg .= '<tr class=dc><td><center>';
$pg .= makeForm('userset');
$pg .= '<table cellpadding=5 cellspacing=0 border=0>';
$pg .= '<tr class=dc><td>';
if ($api === false)
$pg .= "You don't have an API Key setup yet";
else
{
$pg .= 'Your current API Key is:';
$pg .= '</td></tr><tr class=dc><td>';
$pg .= "<span class=hil>$api</span>";
}
$pg .= '</td></tr><tr class=dc><td>';
$pg .= 'Click to generate a new API key';
$pg .= ": <input type=submit name=Change value='API Key'>";
$pg .= '</td></tr>';
if ($api !== false)
{
$pg .= '<tr class=dc><td>&nbsp;</td></tr>';
$pg .= '<tr class=dc><td>You can access the API via:';
$pg .= '</td></tr><tr class=dc><td>';
$pg .= "<span class=hil>/index.php?k=api&username=$user&api=$api</span>";
$pg .= '</td></tr>';
}
$pg .= '</table></form>';
$pg .= '</center></td></tr>';
$pg .= '</table>';
return $pg;
}
#
function douserset($data, $user)
{
$err = '';
$chg = getparam('Change', false);
$api = false;
switch ($chg)
{
case 'API Key':
$ans = getAtts($user, 'KAPIKey.str,KAPIKey.dateexp');
if ($ans['STATUS'] != 'ok')
dbdown(); // Should be no other reason?
if (isset($ans['KAPIKey.dateexp']) && $ans['KAPIKey.dateexp'] == 'N')
{
$err = 'You can only change it once a day';
if (isset($ans['KAPIKey.str']))
$api = $ans['KAPIKey.str'];
}
else
{
$ran = $ans['STAMP'].$user.rand(100000000,999999999);
$api = hash('md4', $ran);
$day = 60 * 60 * 24;
$ans = setAtts($user, array('ua_KAPIKey.str' => $api,
'ua_KAPIKey.date' => "now+$day"));
if ($ans['STATUS'] != 'ok')
syserror();
}
break;
}
if ($api === false)
{
$ans = getAtts($user, 'KAPIKey.str');
if ($ans['STATUS'] != 'ok')
dbdown(); // Should be no other reason?
if (isset($ans['KAPIKey.str']))
$api = $ans['KAPIKey.str'];
}
$pg = uset($data, $user, $api, $err);
return $pg;
}
#
function show_userset($page, $menu, $name, $user)
{
gopage(NULL, 'douserset', $page, $menu, $name, $user);
}
#
?>

10
pool/prime.php

@ -34,7 +34,8 @@ function process($p, $user, $menu)
function def_menu()
{
$dmenu = array('Home' => array('Home' => ''),
'gap' => NULL,
'gap' => array( # options not shown
'API' => 'api'),
'Help' => array('Help' => 'help',
'Payouts' => 'payout'));
return $dmenu;
@ -50,14 +51,17 @@ function check()
'Account' => array(
'Workers' => 'workers',
'Payments' => 'payments',
'Settings' => 'settings'
'Settings' => 'settings',
'User Settings' => 'userset'
),
'Pool' => array(
'Stats' => 'stats',
'Blocks' => 'blocks'
),
'Admin' => NULL,
'gap' => NULL,
'gap' => array( # options not shown
'API' => 'api'
),
'Help' => array(
'Help' => 'help',
'Payouts' => 'payout'

Loading…
Cancel
Save