From 1e54a1a2d6695963c3be9b06d5a48655526fe5a0 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Sun, 26 May 2013 19:33:19 +0200 Subject: [PATCH] initial commit of a working API page --- public/include/classes/user.class.php | 13 ++++++ public/include/pages/api.inc.php | 57 +++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 public/include/pages/api.inc.php diff --git a/public/include/classes/user.class.php b/public/include/classes/user.class.php index d54b2da5..a8067a17 100644 --- a/public/include/classes/user.class.php +++ b/public/include/classes/user.class.php @@ -225,6 +225,19 @@ class User { return false; } + /** + * Check API key for authentication + * @param key string API key hash + * @return bool + **/ + public function checkApiKey($key) { + $this->debug->append("STA " . __METHOD__, 4); + $stmt = $this->mysqli->prepare("SELECT api_key FROM $this->table WHERE api_key = ?"); + if ($this->checkStmt($stmt) && $stmt->bind_param("s", $key) && $stmt->execute() && $stmt->bind_result($api_key) && $stmt->fetch()) + return $key === $api_key; + return false; + } + private function checkUserPassword($username, $password) { $this->debug->append("STA " . __METHOD__, 4); $user = array(); diff --git a/public/include/pages/api.inc.php b/public/include/pages/api.inc.php new file mode 100644 index 00000000..23332c3a --- /dev/null +++ b/public/include/pages/api.inc.php @@ -0,0 +1,57 @@ +checkApiKey($_REQUEST['api_key'])) { + header("HTTP/1.1 401 Unauthorized"); + die(); +} + +// Fetch data from litecoind +if ($bitcoin->can_connect() === true){ + if (!$dDifficulty = $memcache->get('dDifficulty')) { + $dDifficulty = $bitcoin->query('getdifficulty'); + $memcache->set('dDifficulty', $dDifficulty); + } + if (!$iBlock = $memcache->get('iBlock')) { + $iBlock = $bitcoin->query('getblockcount'); + $memcache->set('iBlock', $iBlock); + } +} else { + $iDifficulty = 1; + $iBlock = 0; + $_SESSION['POPUP'][] = array('CONTENT' => 'Unable to connect to pushpool service: ' . $bitcoin->can_connect(), 'TYPE' => 'errormsg'); +} + +// Grab the last 10 blocks found +$iLimit = 10; +$aBlocksFoundData = $statistics->getBlocksFound($iLimit); +$aBlockData = $aBlocksFoundData[0]; + +// Estimated time to find the next block +$iCurrentPoolHashrate = $statistics->getCurrentHashrate(); +// Time in seconds, not hours, using modifier in smarty to translate +$iEstTime = $dDifficulty * pow(2,32) / ($iCurrentPoolHashrate * 1000); + +// Time since last block +$now = new DateTime( "now" ); +if (!empty($aBlockData)) { + $dTimeSinceLast = ($now->getTimestamp() - $aBlockData['time']); +} else { + $dTimeSinceLast = 0; +} + +$aData = array( + 'est_time' => $iEstTime, + 'time_last' => $dTimeSinceLast, + 'blocks_found' => $aBlocksFoundData, + 'cur_block' => $iBlock, + 'last_block' => $aBlockData['height'], + 'difficulty' => $iDifficulty, +); + +$supress_master = 1; +echo json_encode($aData); +?>