From 187fb92272ee5a44e5eed1eb93bff0b11307d2db Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Sun, 12 May 2013 11:39:22 +0200 Subject: [PATCH] further cleaned up user class, created worker class, removed ledger and paid calls from user --- public/include/autoloader.inc.php | 3 +- public/include/classes/user.class.php | 102 ------------------- public/include/classes/worker.class.php | 90 ++++++++++++++++ public/include/pages/account/workers.inc.php | 14 +-- 4 files changed, 99 insertions(+), 110 deletions(-) create mode 100644 public/include/classes/worker.class.php diff --git a/public/include/autoloader.inc.php b/public/include/autoloader.inc.php index dad1ff39..a291b64f 100644 --- a/public/include/autoloader.inc.php +++ b/public/include/autoloader.inc.php @@ -5,8 +5,9 @@ require_once(CLASS_DIR . '/bitcoin.class.php'); require_once(INCLUDE_DIR . '/database.inc.php'); require_once(INCLUDE_DIR . '/smarty.inc.php'); // Load classes that need the above as dependencies -require_once(CLASS_DIR . '/user.class.php'); require_once(CLASS_DIR . '/block.class.php'); +require_once(CLASS_DIR . '/user.class.php'); +require_once(CLASS_DIR . '/worker.class.php'); require_once(CLASS_DIR . '/share.class.php'); require_once(CLASS_DIR . '/statistics.class.php'); require_once(CLASS_DIR . '/transaction.class.php'); diff --git a/public/include/classes/user.class.php b/public/include/classes/user.class.php index 4622874f..63b02cdb 100644 --- a/public/include/classes/user.class.php +++ b/public/include/classes/user.class.php @@ -10,8 +10,6 @@ class User { private $table = 'accounts'; private $user = array(); private $tableAccountBalance = 'accountBalance'; - private $tablePoolWorker = 'pool_worker'; - private $tableLedger = 'ledger'; public function __construct($debug, $mysqli, $salt) { $this->debug = $debug; @@ -76,17 +74,6 @@ class User { return false; } - public function addLedger($userID, $balance, $address, $fee=0.1) { - $stmt = $this->mysqli->prepare("INSERT INTO $this->tableLedger (userId, transType, amount, sendAddress, feeAmount) VALUES (?, 'Debit_MP', ?, ?, ?)"); - if ($this->checkStmt($stmt)) { - $stmt->bind_param('idsd', $userID, $balance, $address, $fee); - $stmt->execute(); - $stmt->close(); - return true; - } - return false; - } - private function checkStmt($bState) { if ($bState ===! true) { $this->debug->append("Failed to prepare statement: " . $this->mysqli->error); @@ -135,28 +122,6 @@ class User { } return false; } - // set/get methods - public function getPaid($userID) { - return $this->getSingle($userID, 'userId', 'paid', $this->tableAccountBalance); - } - public function getBalance($userID) { - return $this->getSingle($userID, 'userId', 'balance', $this->tableAccountBalance); - } - public function getLtcAddress($userID) { - return $this->getSingle($userID, 'id', 'coin_address', $this->table); - } - public function getUserName($userID) { - return $this->getSingle($userID, 'id', 'username', $this->table); - } - - public function setPaid($userID, $paid) { - $field = array('name' => 'paid', 'type' => 'd', 'value' => $paid); - return $this->updateSingle($userID, $field, $this->tableAccountBalance); - } - public function setBalance($userID, $balance) { - $field = array('name' => 'balance', 'type' => 'd', 'value' => $balance); - return $this->updateSingle($userID, $field, $this->tableAccountBalance); - } private function checkUserPassword($username, $password) { $user = array(); @@ -210,73 +175,6 @@ class User { return false; } - // Get 15 most recent transactions - public function getTransactions($userID, $start=0) { - $stmt = $this->mysqli->prepare("SELECT * FROM $this->tableLedger where userId = ? ORDER BY timestamp DESC LIMIT ?,15"); - if ($this->checkStmt($stmt)) { - if(!$stmt->bind_param('ii', $userID, $start)) return false; - $stmt->execute(); - $result = $stmt->get_result(); - return $result->fetch_all(MYSQLI_ASSOC); - } - return false; - } - - // Worker code, could possibly be moved to it's own class someday - public function updateWorkers($userID, $data) { - $username = $this->getUserName($userID); - foreach ($data as $key => $value) { - // Prefix the WebUser to Worker name - $value['username'] = "$username." . $value['username']; - $stmt = $this->mysqli->prepare("UPDATE $this->tablePoolWorker SET password = ?, username = ? WHERE associatedUserId = ? AND id = ?"); - if ($this->checkStmt($stmt)) { - if (!$stmt->bind_param('ssii', $value['password'], $value['username'], $userID, $key)) return false; - if (!$stmt->execute()) return false; - $stmt->close(); - } - } - return true; - } - public function getWorkers($userID) { - $stmt = $this->mysqli->prepare("SELECT id, username, password, active, hashrate FROM $this->tablePoolWorker WHERE associatedUserId = ? ORDER BY username ASC"); - if ($this->checkStmt($stmt)) { - if (!$stmt->bind_param('i', $userID)) return false; - if (!$stmt->execute()) return false; - $result = $stmt->get_result(); - $stmt->close(); - return $result->fetch_all(MYSQLI_ASSOC); - } - return false; - } - public function addWorker($userID, $workerName, $workerPassword) { - $username = $this->getUserName($userID); - $workerName = "$username.$workerName"; - $stmt = $this->mysqli->prepare("INSERT INTO pool_worker (associatedUserId, username, password) VALUES(?, ?, ?)"); - if ($this->checkStmt($stmt)) { - $stmt->bind_param('iss', $userID, $workerName, $workerPassword); - if (!$stmt->execute()) { - $this->setErrorMessage( 'Failed to add worker' ); - if ($stmt->sqlstate == '23000') $this->setErrorMessage( 'Worker already exists' ); - return false; - } - return true; - } - return false; - } - public function deleteWorker($userID, $workerID) { - $stmt = $this->mysqli->prepare("DELETE FROM $this->tablePoolWorker WHERE associatedUserId = ? AND id = ?"); - if ($this->checkStmt($stmt)) { - $stmt->bind_param('ii', $userID, $workerID); - if ($stmt->execute() && $stmt->affected_rows == 1) { - $stmt->close; - return true; - } else { - $this->setErrorMessage( 'Unable to delete worker' ); - } - } - return false; - } - public function register($username, $password1, $password2, $pin, $email1='', $email2='') { if (strlen($password1) < 8) { $this->setErrorMessage( 'Password is too short, minimum of 8 characters required' ); diff --git a/public/include/classes/worker.class.php b/public/include/classes/worker.class.php new file mode 100644 index 00000000..94c4013f --- /dev/null +++ b/public/include/classes/worker.class.php @@ -0,0 +1,90 @@ +debug = $debug; + $this->mysqli = $mysqli; + $this->user = $user; + $this->debug->append("Instantiated Worker class", 2); + } + + // get and set methods + private function setErrorMessage($msg) { + $this->sError = $msg; + } + public function getError() { + return $this->sError; + } + + private function checkStmt($bState) { + if ($bState ===! true) { + $this->debug->append("Failed to prepare statement: " . $this->mysqli->error); + $this->setErrorMessage('Internal application Error'); + return false; + } + return true; + } + // Worker code, could possibly be moved to it's own class someday + public function updateWorkers($account_id, $data) { + $username = $this->user->getUserName($account_id); + foreach ($data as $key => $value) { + // Prefix the WebUser to Worker name + $value['username'] = "$username." . $value['username']; + $stmt = $this->mysqli->prepare("UPDATE $this->table SET password = ?, username = ? WHERE account_id = ? AND id = ?"); + if ($this->checkStmt($stmt)) { + if (!$stmt->bind_param('ssii', $value['password'], $value['username'], $account_id, $key)) return false; + if (!$stmt->execute()) return false; + $stmt->close(); + } + } + return true; + } + public function getWorkers($account_id) { + $stmt = $this->mysqli->prepare("SELECT id, username, password, active, hashrate FROM $this->table WHERE account_id = ? ORDER BY username ASC"); + if ($this->checkStmt($stmt)) { + if (!$stmt->bind_param('i', $account_id)) return false; + if (!$stmt->execute()) return false; + $result = $stmt->get_result(); + $stmt->close(); + return $result->fetch_all(MYSQLI_ASSOC); + } + return false; + } + public function addWorker($account_id, $workerName, $workerPassword) { + $username = $this->user->getUserName($account_id); + $workerName = "$username.$workerName"; + $stmt = $this->mysqli->prepare("INSERT INTO $this->table (account_id, username, password) VALUES(?, ?, ?)"); + if ($this->checkStmt($stmt)) { + $stmt->bind_param('iss', $account_id, $workerName, $workerPassword); + if (!$stmt->execute()) { + $this->setErrorMessage( 'Failed to add worker' ); + if ($stmt->sqlstate == '23000') $this->setErrorMessage( 'Worker already exists' ); + return false; + } + return true; + } + return false; + } + public function deleteWorker($account_id, $id) { + $stmt = $this->mysqli->prepare("DELETE FROM $this->table WHERE account_id = ? AND id = ?"); + if ($this->checkStmt($stmt)) { + $stmt->bind_param('ii', $account_id, $id); + if ($stmt->execute() && $stmt->affected_rows == 1) { + $stmt->close; + return true; + } else { + $this->setErrorMessage( 'Unable to delete worker' ); + } + } + return false; + } +} + +$worker = new Worker($debug, $mysqli, $user); diff --git a/public/include/pages/account/workers.inc.php b/public/include/pages/account/workers.inc.php index fb2db840..67bd0e19 100644 --- a/public/include/pages/account/workers.inc.php +++ b/public/include/pages/account/workers.inc.php @@ -6,29 +6,29 @@ if (!$_SESSION['AUTHENTICATED']) header('Location: index.php?page=home'); switch ($_REQUEST['do']) { case 'delete': - if ($user->deleteWorker($_SESSION['USERDATA']['id'], $_GET['id'])) { + if ($worker->deleteWorker($_SESSION['USERDATA']['id'], $_GET['id'])) { $_SESSION['POPUP'][] = array('CONTENT' => 'Worker removed'); } else { - $_SESSION['POPUP'][] = array('CONTENT' => $user->error, 'TYPE' => 'errormsg'); + $_SESSION['POPUP'][] = array('CONTENT' => $worker->getError(), 'TYPE' => 'errormsg'); } break; case 'add': - if ($user->addWorker($_SESSION['USERDATA']['id'], $_POST['username'], $_POST['password'])) { + if ($worker->addWorker($_SESSION['USERDATA']['id'], $_POST['username'], $_POST['password'])) { $_SESSION['POPUP'][] = array('CONTENT' => 'Worker added'); } else { - $_SESSION['POPUP'][] = array('CONTENT' => $user->error, 'TYPE' => 'errormsg'); + $_SESSION['POPUP'][] = array('CONTENT' => $worker->getError(), 'TYPE' => 'errormsg'); } break; case 'update': - if ($user->updateWorkers($_SESSION['USERDATA']['id'], $_POST['data'])) { + if ($worker->updateWorkers($_SESSION['USERDATA']['id'], $_POST['data'])) { $_SESSION['POPUP'][] = array('CONTENT' => 'Worker updated'); } else { - $_SESSION['POPUP'][] = array('CONTENT' => $user->error, 'TYPE' => 'errormsg'); + $_SESSION['POPUP'][] = array('CONTENT' => $worker->getError(), 'TYPE' => 'errormsg'); } break; } -$aWorkers = $user->getWorkers($_SESSION['USERDATA']['id']); +$aWorkers = $worker->getWorkers($_SESSION['USERDATA']['id']); if (!$aWorkers) $_SESSION['POPUP'][] = array('CONTENT' => 'You have no workers configured', 'TYPE' => 'errormsg'); $smarty->assign('CONTENT', 'default.tpl');