further cleaned up user class, created worker class, removed ledger and paid calls from user

This commit is contained in:
Sebastian Grewe 2013-05-12 11:39:22 +02:00
parent a6e0b98552
commit 187fb92272
4 changed files with 99 additions and 110 deletions

View File

@ -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');

View File

@ -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' );

View File

@ -0,0 +1,90 @@
<?php
// Make sure we are called from index.php
if (!defined('SECURITY'))
die('Hacking attempt');
class Worker {
private $sError = '';
private $table = 'workers';
public function __construct($debug, $mysqli, $user) {
$this->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);

View File

@ -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');