From 02f085f2d7bdcfffe1d11b3b98538e6656e4b936 Mon Sep 17 00:00:00 2001 From: Neozonz Date: Wed, 22 Jan 2014 00:13:58 +0000 Subject: [PATCH] [FIX] MySQLi Queries --- public/include/classes/statistics.class.php | 74 +++++++++++-------- .../pages/api/getdashboarddata.inc.php | 4 +- .../pages/api/gethourlyhashrates.inc.php | 4 +- .../include/pages/api/getuserhashrate.inc.php | 2 +- .../pages/api/getusersharerate.inc.php | 2 +- .../include/pages/api/getuserstatus.inc.php | 2 +- public/include/smarty_globals.inc.php | 4 +- 7 files changed, 53 insertions(+), 39 deletions(-) diff --git a/public/include/classes/statistics.class.php b/public/include/classes/statistics.class.php index a84dae5c..4b71f4b0 100644 --- a/public/include/classes/statistics.class.php +++ b/public/include/classes/statistics.class.php @@ -353,7 +353,7 @@ class Statistics extends Base { * @param username str username * @return data array invalid and valid share counts **/ - public function getUserShares($username, $account_id) { + public function getUserShares($username, $account_id=NULL) { $this->debug->append("STA " . __METHOD__, 4); // Dual-caching, try statistics cron first, then fallback to local, then fallbock to SQL if ($data = $this->memcache->get(STATISTICS_ALL_USER_SHARES)) { @@ -365,12 +365,15 @@ class Statistics extends Base { if ($data = $this->memcache->get(__FUNCTION__ . $account_id)) return $data; $stmt = $this->mysqli->prepare(" SELECT - ROUND(IFNULL(SUM(IF(our_result='Y', IF(s.difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), s.difficulty), 0)), 0) / POW(2, (" . $this->config['difficulty'] . " - 16)), 0) AS valid, - ROUND(IFNULL(SUM(IF(our_result='N', IF(s.difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), s.difficulty), 0)), 0) / POW(2, (" . $this->config['difficulty'] . " - 16)), 0) AS invalid + ROUND(IFNULL(SUM(IF(our_result='Y', IF(difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), difficulty), 0)), 0) / POW(2, (" . $this->config['difficulty'] . " - 16)), 0) AS valid, + ROUND(IFNULL(SUM(IF(our_result='N', IF(difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), difficulty), 0)), 0) / POW(2, (" . $this->config['difficulty'] . " - 16)), 0) AS invalid FROM " . $this->share->getTableName() . " - WHERE username LIKE '?.%' + WHERE username LIKE ? AND UNIX_TIMESTAMP(time) >IFNULL((SELECT MAX(b.time) FROM " . $this->block->getTableName() . " AS b),0)"); - if ($stmt && $stmt->bind_param("i", $username) && $stmt->execute() && $result = $stmt->get_result()) + + $username = $username . ".%"; + + if ($stmt && $stmt->bind_param("s", $username) && $stmt->execute() && $result = $stmt->get_result()) return $this->memcache->setCache(__FUNCTION__ . $account_id, $result->fetch_assoc()); return $this->sqlError(); } @@ -495,7 +498,7 @@ class Statistics extends Base { * @param $username string username * @return data integer Current Hashrate in khash/s **/ - public function getUserHashrate($username, $account_id, $interval=180) { + public function getUserHashrate($username, $account_id=NULL, $interval=180) { $this->debug->append("STA " . __METHOD__, 4); // Dual-caching, try statistics cron first, then fallback to local, then fallbock to SQL if ($data = $this->memcache->getStatic(STATISTICS_ALL_USER_HASHRATES)) { @@ -513,7 +516,7 @@ class Statistics extends Base { id, our_result, IF(s.difficulty = 0, POW(2, (" . $this->config['difficulty'] . " - 16)), difficulty) AS difficulty FROM shares - WHERE username LIKE '?.%' + WHERE username LIKE ? AND time > DATE_SUB(now(), INTERVAL ? SECOND) AND our_result = 'Y' UNION @@ -521,26 +524,31 @@ class Statistics extends Base { share_id, our_result, IF(s.difficulty = 0, POW(2, (" . $this->config['difficulty'] . " - 16)), difficulty) AS difficulty FROM shares_archive - WHERE username LIKE '?.%' + WHERE username LIKE ? AND time > DATE_SUB(now(), INTERVAL ? SECOND) - AND our_result = 'Y' - ) AS temp"); - if ($this->checkStmt($stmt) && $stmt->bind_param("iiiii", $username, $interval, $username, $interval) && $stmt->execute() && $result = $stmt->get_result() ) + AND our_result = 'Y') AS temp"); + + $username = $username . ".%"; + + if ($this->checkStmt($stmt) && $stmt->bind_param("iiiii", $interval, $username, $interval, $username, $interval) && $stmt->execute() && $result = $stmt->get_result() ) return $this->memcache->setCache(__FUNCTION__ . $account_id, $result->fetch_object()->hashrate); return $this->sqlError(); } - public function getUserUnpaidPPSShares($username, $last_paid_pps_id) { + public function getUserUnpaidPPSShares($username, $account_id=NULL, $last_paid_pps_id) { $this->debug->append("STA " . __METHOD__, 4); if ($this->getGetCache() && $data = $this->memcache->get(__FUNCTION__ . $account_id)) return $data; $stmt = $this->mysqli->prepare(" SELECT - ROUND(IFNULL(SUM(IF(s.difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), s.difficulty)), 0) / POW(2, (" . $this->config['difficulty'] . " - 16)), 0) AS total + ROUND(IFNULL(SUM(IF(difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), difficulty)), 0) / POW(2, (" . $this->config['difficulty'] . " - 16)), 0) AS total FROM " . $this->share->getTableName() . " - WHERE username LIKE '?.%' + WHERE username LIKE ? AND id > ? AND our_result = 'Y'"); - if ($this->checkStmt($stmt) && $stmt->bind_param("ii", $username, $last_paid_pps_id) && $stmt->execute() && $result = $stmt->get_result() ) + + $username = $username . ".%"; + + if ($this->checkStmt($stmt) && $stmt->bind_param("si", $username, $last_paid_pps_id) && $stmt->execute() && $result = $stmt->get_result() ) return $this->memcache->setCache(__FUNCTION__ . $account_id, $result->fetch_object()->total); return $this->sqlError(); } @@ -551,7 +559,7 @@ class Statistics extends Base { * @param interval int Data interval in seconds * @return double Share difficulty or 0 **/ - public function getUserShareDifficulty($username, $account_id, $interval=180) { + public function getUserShareDifficulty($username, $account_id=NULL, $interval=180) { $this->debug->append("STA " . __METHOD__, 4); // Dual-caching, try statistics cron first, then fallback to local, then fallbock to SQL if ($data = $this->memcache->getStatic(STATISTICS_ALL_USER_HASHRATES)) { @@ -566,11 +574,11 @@ class Statistics extends Base { IFNULL(AVG(IF(difficulty=0, pow(2, (" . $this->config['difficulty'] . " - 16)), difficulty)), 0) AS avgsharediff, COUNT(s.id) AS total FROM " . $this->share->getTableName() . " AS s - WHERE username LIKE '?.%' + WHERE username LIKE ?.% AND time > DATE_SUB(now(), INTERVAL ? SECOND) AND our_result = 'Y' "); - if ($this->checkStmt($stmt) && $stmt->bind_param("ii", $username, $interval) && $stmt->execute() && $result = $stmt->get_result() ) + if ($this->checkStmt($stmt) && $stmt->bind_param("si", $username, $interval) && $stmt->execute() && $result = $stmt->get_result() ) return $this->memcache->setCache(__FUNCTION__ . $account_id, $result->fetch_object()->avgsharediff); return $this->sqlError(); } @@ -580,7 +588,7 @@ class Statistics extends Base { * @param username string username * @return data integer Current Sharerate in shares/s **/ - public function getUserSharerate($username, $account_id, $interval=180) { + public function getUserSharerate($username, $account_id=NULL, $interval=180) { $this->debug->append("STA " . __METHOD__, 4); // Dual-caching, try statistics cron first, then fallback to local, then fallbock to SQL if ($data = $this->memcache->getStatic(STATISTICS_ALL_USER_HASHRATES)) { @@ -598,7 +606,7 @@ class Statistics extends Base { id FROM shares - WHERE username LIKE '?.%' + WHERE username LIKE ? AND time > DATE_SUB(now(), INTERVAL ? SECOND) AND our_result = 'Y' UNION @@ -606,11 +614,14 @@ class Statistics extends Base { share_id FROM shares_archive - WHERE username LIKE '?.%' + WHERE username LIKE ? AND time > DATE_SUB(now(), INTERVAL ? SECOND) AND our_result = 'Y' ) AS temp"); - if ($this->checkStmt($stmt) && $stmt->bind_param("iiiii", $username, $interval, $username, $interval) && $stmt->execute() && $result = $stmt->get_result() ) + + $username = $username . ".%"; + + if ($this->checkStmt($stmt) && $stmt->bind_param("isisi", $interval, $username, $interval, $username, $interval) && $stmt->execute() && $result = $stmt->get_result() ) return $this->memcache->setCache(__FUNCTION__ . $account_id, $result->fetch_object()->sharerate); return $this->sqlError(); } @@ -620,16 +631,16 @@ class Statistics extends Base { * @param username string username * @return data int Current hashrate in khash/s **/ - public function getWorkerHashrate($worker_id, $interval=180) { + public function getWorkerHashrate($workername, $worker_id=NULL, $interval=180) { $this->debug->append("STA " . __METHOD__, 4); - if ($data = $this->memcache->get(__FUNCTION__ . $account_id)) return $data; + if ($data = $this->memcache->get(__FUNCTION__ . $worker_id)) return $data; $stmt = $this->mysqli->prepare(" SELECT IFNULL(ROUND(SUM(IF(difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), difficulty)) * POW(2, " . $this->config['target_bits'] . ") / 600 / 1000), 0) AS hashrate FROM " . $this->share->getTableName() . " AS WHERE username = '?' AND our_result = 'Y' - AND time > DATE_SUB(now(), INTERVAL ? SECOND)); - if ($this->checkStmt($stmt) && $stmt->bind_param("i", $worker_id, $interval) && $stmt->execute() && $result = $stmt->get_result() ) + AND time > DATE_SUB(now(), INTERVAL ? SECOND)"); + if ($this->checkStmt($stmt) && $stmt->bind_param("si", $workername, $interval) && $stmt->execute() && $result = $stmt->get_result()) return $this->memcache->setCache(__FUNCTION__ . $worker_id, $result->fetch_object()->hashrate); return $this->sqlError(); } @@ -714,7 +725,7 @@ class Statistics extends Base { * @param username string Username * @return data array NOT FINISHED YET **/ - public function getHourlyHashrateByAccount($username, $account_id) { + public function getHourlyHashrateByAccount($username, $account_id=NULL) { $this->debug->append("STA " . __METHOD__, 4); if ($data = $this->memcache->get(__FUNCTION__ . $account_id)) return $data; $stmt = $this->mysqli->prepare(" @@ -725,7 +736,7 @@ class Statistics extends Base { WHERE time <= FROM_UNIXTIME(FLOOR(UNIX_TIMESTAMP(NOW())/(60*60))*(60*60)) AND time >= FROM_UNIXTIME(FLOOR(UNIX_TIMESTAMP(NOW())/(60*60))*(60*60)) - INTERVAL 24 HOUR AND our_result = 'Y' - AND username LIKE '?.%' + AND username LIKE ? GROUP BY HOUR(time) UNION SELECT @@ -736,9 +747,12 @@ class Statistics extends Base { WHERE time <= FROM_UNIXTIME(FLOOR(UNIX_TIMESTAMP(NOW())/(60*60))*(60*60)) AND time >= FROM_UNIXTIME(FLOOR(UNIX_TIMESTAMP(NOW())/(60*60))*(60*60)) - INTERVAL 24 HOUR AND our_result = 'Y' - AND username LIKE '?.%' + AND username LIKE ? GROUP BY HOUR(time)"); - if ($this->checkStmt($stmt) && $stmt->bind_param('ii', $username, $username) && $stmt->execute() && $result = $stmt->get_result()) { + + $username = $username . ".%"; + + if ($this->checkStmt($stmt) && $stmt->bind_param('ss', $username, $username) && $stmt->execute() && $result = $stmt->get_result()) { $iStartHour = date('G'); // Initilize array for ($i = 0; $i < 24; $i++) $aData[($iStartHour + $i) % 24] = 0; diff --git a/public/include/pages/api/getdashboarddata.inc.php b/public/include/pages/api/getdashboarddata.inc.php index 3ca71365..b5a73801 100644 --- a/public/include/pages/api/getdashboarddata.inc.php +++ b/public/include/pages/api/getdashboarddata.inc.php @@ -22,7 +22,7 @@ $supress_master = 1; // Check user token and access level permissions $user_id = $api->checkAccess($user->checkApiKey($_REQUEST['api_key']), @$_REQUEST['id']); -$username = $user->getUsername($user_id) +$username = $user->getUsername($user_id); // Fetch RPC information if ($bitcoin->can_connect() === true) { @@ -58,7 +58,7 @@ if ($config['payout_system'] != 'pps') { $aEstimates = $statistics->getUserEstimates($aRoundShares, $aUserRoundShares, $user->getUserDonatePercent($user_id), $user->getUserNoFee($user_id)); $dUnpaidShares = 0; } else { - $dUnpaidShares = $statistics->getUserUnpaidPPSShares($username, $setting->getValue('pps_last_share_id')); + $dUnpaidShares = $statistics->getUserUnpaidPPSShares($username, $user_id, $setting->getValue('pps_last_share_id')); $aEstimates = $statistics->getUserEstimates($dPersonalSharerate, $dPersonalShareDifficulty, $user->getUserDonatePercent($user_id), $user->getUserNoFee($user_id), $statistics->getPPSValue()); } diff --git a/public/include/pages/api/gethourlyhashrates.inc.php b/public/include/pages/api/gethourlyhashrates.inc.php index 54410745..64ee56b8 100644 --- a/public/include/pages/api/gethourlyhashrates.inc.php +++ b/public/include/pages/api/gethourlyhashrates.inc.php @@ -8,11 +8,11 @@ $api->isActive(); // Check user token $user_id = $api->checkAccess($user->checkApiKey($_REQUEST['api_key']), @$_REQUEST['id']); -$username = $user->getUsername($user_id) +$username = $user->getUsername($user_id); // Output JSON format $data = array( - 'mine' => $statistics->getHourlyHashrateByAccount($username,$user_id), + 'mine' => $statistics->getHourlyHashrateByAccount($username, $user_id), 'pool' => $statistics->getHourlyHashrateByPool() ); diff --git a/public/include/pages/api/getuserhashrate.inc.php b/public/include/pages/api/getuserhashrate.inc.php index 51e69e85..48c1d327 100644 --- a/public/include/pages/api/getuserhashrate.inc.php +++ b/public/include/pages/api/getuserhashrate.inc.php @@ -8,7 +8,7 @@ $api->isActive(); // Check user token $user_id = $api->checkAccess($user->checkApiKey($_REQUEST['api_key']), @$_REQUEST['id']); -$username = $user->getUsername($user_id) +$username = $user->getUsername($user_id); // Fetch some settings if ( ! $interval = $setting->getValue('statistics_ajax_data_interval')) $interval = 300; diff --git a/public/include/pages/api/getusersharerate.inc.php b/public/include/pages/api/getusersharerate.inc.php index 3549c0f4..c1edb01e 100644 --- a/public/include/pages/api/getusersharerate.inc.php +++ b/public/include/pages/api/getusersharerate.inc.php @@ -8,7 +8,7 @@ $api->isActive(); // Check user token $user_id = $api->checkAccess($user->checkApiKey($_REQUEST['api_key']), @$_REQUEST['id']); -$username = $user->getUsername($user_id) +$username = $user->getUsername($user_id); // Fetch settings if ( ! $interval = $setting->getValue('statistics_ajax_data_interval')) $interval = 300; diff --git a/public/include/pages/api/getuserstatus.inc.php b/public/include/pages/api/getuserstatus.inc.php index 23ef53c7..c17bb77f 100644 --- a/public/include/pages/api/getuserstatus.inc.php +++ b/public/include/pages/api/getuserstatus.inc.php @@ -8,7 +8,7 @@ $api->isActive(); // Check user token $user_id = $api->checkAccess($user->checkApiKey($_REQUEST['api_key']), @$_REQUEST['id']); -$username = $user->getUsername($user_id) +$username = $user->getUsername($user_id); // Fetch transaction summary $aTransactionSummary = $transaction->getTransactionSummary($user_id); diff --git a/public/include/smarty_globals.inc.php b/public/include/smarty_globals.inc.php index e3637cd3..60902514 100644 --- a/public/include/smarty_globals.inc.php +++ b/public/include/smarty_globals.inc.php @@ -151,10 +151,10 @@ if (@$_SESSION['USERDATA']['id']) { $aGlobal['userdata']['estimates'] = $aEstimates; break; case 'pps': - $aGlobal['userdata']['pps']['unpaidshares'] = $statistics->getUserUnpaidPPSShares($_SESSION['USERDATA']['id'], $setting->getValue('pps_last_share_id')); + $aGlobal['userdata']['pps']['unpaidshares'] = $statistics->getUserUnpaidPPSShares($_SESSION['USERDATA']['username'], $_SESSION['USERDATA']['id'], $setting->getValue('pps_last_share_id')); $aGlobal['ppsvalue'] = number_format($statistics->getPPSValue(), 12); $aGlobal['poolppsvalue'] = $aGlobal['ppsvalue'] * pow(2, $config['difficulty'] - 16); - $aGlobal['userdata']['sharedifficulty'] = $statistics->getUserShareDifficulty($_SESSION['USERDATA']['id']); + $aGlobal['userdata']['sharedifficulty'] = $statistics->getUserShareDifficulty($_SESSION['USERDATA']['username'], $_SESSION['USERDATA']['id']); $aGlobal['userdata']['estimates'] = $statistics->getUserEstimates($aGlobal['userdata']['sharerate'], $aGlobal['userdata']['sharedifficulty'], $aGlobal['userdata']['donate_percent'], $aGlobal['userdata']['no_fees'], $aGlobal['ppsvalue']); break; }