From cdfb0740762f989bc7a27bae1397573d22e032bf Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Wed, 29 May 2013 10:56:08 +0200 Subject: [PATCH] Re-adding LTC/usd to website ministats header * Use tickerupdate.php cron to update setting value * Added new configuration variables for ticket updates * Added some missing configuration vars for some URLs --- cronjobs/tickerupdate.php | 36 ++++++++++++++++ public/include/autoloader.inc.php | 2 +- .../{settings.class.php => setting.class.php} | 19 +++++++-- public/include/classes/tools.class.php | 41 +++++++++++++++++++ public/include/config/global.inc.dist.php | 6 +++ public/include/smarty_globals.inc.php | 5 ++- public/templates/mmcFE/global/header.tpl | 1 + 7 files changed, 104 insertions(+), 6 deletions(-) create mode 100755 cronjobs/tickerupdate.php rename public/include/classes/{settings.class.php => setting.class.php} (57%) create mode 100644 public/include/classes/tools.class.php diff --git a/cronjobs/tickerupdate.php b/cronjobs/tickerupdate.php new file mode 100755 index 00000000..b806654d --- /dev/null +++ b/cronjobs/tickerupdate.php @@ -0,0 +1,36 @@ +#!/usr/bin/php +getApi($config['price']['url'], $config['price']['target'])) { + if (!$setting->setValue('price', $aData['ticker']['avg'])) + verbose("ERR Table update failed"); +} else { + verbose("ERR Failed download JSON data from " . $config['price']['url'].$config['price']['target'] . "\n"); +} + +?> diff --git a/public/include/autoloader.inc.php b/public/include/autoloader.inc.php index 0671a34e..ea16564e 100644 --- a/public/include/autoloader.inc.php +++ b/public/include/autoloader.inc.php @@ -13,4 +13,4 @@ require_once(CLASS_DIR . '/share.class.php'); require_once(CLASS_DIR . '/worker.class.php'); require_once(CLASS_DIR . '/statistics.class.php'); require_once(CLASS_DIR . '/transaction.class.php'); -require_once(CLASS_DIR . '/settings.class.php'); +require_once(CLASS_DIR . '/setting.class.php'); diff --git a/public/include/classes/settings.class.php b/public/include/classes/setting.class.php similarity index 57% rename from public/include/classes/settings.class.php rename to public/include/classes/setting.class.php index 3c8f9dc0..44ef8315 100644 --- a/public/include/classes/settings.class.php +++ b/public/include/classes/setting.class.php @@ -4,7 +4,7 @@ if (!defined('SECURITY')) die('Hacking attempt'); -class Settings { +class Setting { public function __construct($debug, $mysqli, $salt) { $this->debug = $debug; $this->mysqli = $mysqli; @@ -13,7 +13,7 @@ class Settings { } public function getValue($name) { - $query = $this->mysqli->prepare("SELECT value FROM $this->table WHERE setting=? LIMIT 1"); + $query = $this->mysqli->prepare("SELECT value FROM $this->table WHERE name=? LIMIT 1"); if ($query) { $query->bind_param('s', $name); $query->execute(); @@ -26,6 +26,19 @@ class Settings { } return $value; } + + public function setValue($name, $value) { + $stmt = $this->mysqli->prepare(" + INSERT INTO $this->table (name, value) + VALUES (?, ?) + ON DUPLICATE KEY UPDATE value = ? + "); + if ($stmt && $stmt->bind_param('sss', $name, $value, $value) && $stmt->execute()) + return true; + $this->debug->append("Failed to set $name to $value"); + echo $this->mysqli->error; + return false; + } } -$settings = new Settings($debug, $mysqli, SALT); +$setting = new Setting($debug, $mysqli, SALT); diff --git a/public/include/classes/tools.class.php b/public/include/classes/tools.class.php new file mode 100644 index 00000000..530316e6 --- /dev/null +++ b/public/include/classes/tools.class.php @@ -0,0 +1,41 @@ +debug = $debug; + } + + /** + * Fetch JSON data from an API + * @param url string API URL + * @param target string API method + * @param auth array Optional authentication data to be sent with + * @return dec array JSON decoded PHP array + **/ + public function getApi($url, $target, $auth=NULL) { + static $ch = null; + static $ch = null; + if (is_null($ch)) { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; BTCE PHP client; '.php_uname('s').'; PHP/'.phpversion().')'); + } + curl_setopt($ch, CURLOPT_URL, $url . $target); + // curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); + + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); + + // run the query + $res = curl_exec($ch); + if ($res === false) throw new Exception('Could not get reply: '.curl_error($ch)); + $dec = json_decode($res, true); + if (!$dec) throw new Exception('Invalid data received, please make sure connection is working and requested API exists'); + return $dec; + } +} + +$tools = new Tools($debug); diff --git a/public/include/config/global.inc.dist.php b/public/include/config/global.inc.dist.php index f684c2bb..f4b8b245 100644 --- a/public/include/config/global.inc.dist.php +++ b/public/include/config/global.inc.dist.php @@ -21,11 +21,17 @@ define('DEBUG', 0); define('SALT', 'PLEASEMAKEMESOMETHINGRANDOM'); $config = array( + 'price' => array( + 'url' => 'https://btc-e.com/api/2', + 'target' => '/ltc_usd/ticker' + ), 'website' => array( 'name' => 'The Pool', 'slogan' => 'Resistance is futile', 'email' => 'test@example.com', // Mail address used for notifications ), + 'blockexplorer' => 'http://explorer.litecoin.net/search?q=', // URL for block searches, prefixed to each block number + 'chaininfo' => 'http://allchains.info', // Link to Allchains for Difficulty information 'fees' => 0, 'difficulty' => '31', // Target difficulty for this pool as set in pushpoold json 'reward' => '50', // Reward for finding blocks, fixed value but changes someday diff --git a/public/include/smarty_globals.inc.php b/public/include/smarty_globals.inc.php index c383dfb0..8c7891e6 100644 --- a/public/include/smarty_globals.inc.php +++ b/public/include/smarty_globals.inc.php @@ -23,8 +23,9 @@ $aGlobal = array( 'fees' => $config['fees'], 'confirmations' => $config['confirmations'], 'reward' => $config['reward'], - 'blockexplorer' => 'http://explorer.litecoin.net/search?q=', - 'chaininfo' => 'http://allchains.info' + 'price' => $setting->getValue('price'), + 'blockexplorer' => $config['blockexplorer'], + 'chaininfo' => $config['chaininfo'] ); // We don't want these session infos cached diff --git a/public/templates/mmcFE/global/header.tpl b/public/templates/mmcFE/global/header.tpl index 1f08a366..89948e71 100644 --- a/public/templates/mmcFE/global/header.tpl +++ b/public/templates/mmcFE/global/header.tpl @@ -4,6 +4,7 @@
+
  • LTC/usd: {$GLOBAL.price}    
  • Pool Hashrate: {$GLOBAL.hashrate / 1000} MH/s    
  • Pool Sharerate: {$GLOBAL.sharerate} Shares/s    
  • Pool Workers: {$GLOBAL.workers}