Merge pull request #98 from TheSerapher/admin-panel

Admin panel
This commit is contained in:
Sebastian Grewe 2013-06-04 04:25:27 -07:00
commit ad4b1e9a74
13 changed files with 200 additions and 10 deletions

View File

@ -54,7 +54,7 @@ class Statistics {
$stmt = $this->mysqli->prepare("
SELECT b.*, a.username as finder
FROM " . $this->block->getTableName() . " AS b
LEFT JOIN accounts AS a
LEFT JOIN " . $this->user->getTableName() . " AS a
ON b.account_id = a.id
ORDER BY height DESC LIMIT ?");
if ($this->checkStmt($stmt) && $stmt->bind_param("i", $limit) && $stmt->execute() && $result = $stmt->get_result())
@ -175,6 +175,34 @@ class Statistics {
return false;
}
/**
* Admin panel specific query
* @return data array invlid and valid shares for all accounts
**/
public function getAllUserStats($filter='%') {
$this->debug->append("STA " . __METHOD__, 4);
if ($data = $this->memcache->get(__FUNCTION__ . $filter)) return $data;
$stmt = $this->mysqli->prepare("
SELECT
a.id AS id,
a.username AS username,
a.donate_percent AS donate_percent,
a.email AS email,
COUNT(s.id) AS shares,
ROUND(COUNT(s.id) * POW(2," . $this->config['difficulty'] . ") / 600 / 1000,2) AS hashrate
FROM " . $this->user->getTableName() . " AS a
LEFT JOIN " . $this->share->getTableName() . " AS s
ON a.username = SUBSTRING_INDEX( s.username, '.', 1 )
WHERE
a.username LIKE ?
GROUP BY username
ORDER BY username
");
if ($this->checkStmt($stmt) && $stmt->bind_param('s', $filter) && $stmt->execute() && $result = $stmt->get_result()) {
return $this->memcache->setCache(__FUNCTION__ . $filter, $result->fetch_all(MYSQLI_ASSOC));
}
}
/**
* Same as getUserShares for Hashrate
* @param account_id integer User ID

View File

@ -26,26 +26,28 @@ class User {
public function getError() {
return $this->sError;
}
public function getUserName($id) {
return $this->getSingle($id, 'username', 'id');
}
public function getUserId($username) {
return $this->getSingle($username, 'id', 'username', 's');
}
public function getUserEmail($username) {
return $this->getSingle($username, 'email', 'username', 's');
}
public function getUserAdmin($id) {
return $this->getSingle($id, 'admin', 'id');
}
public function getUserToken($id) {
return $this->getSingle($id, 'token', 'id');
}
public function getIdFromToken($token) {
return $this->getSingle($token, 'id', 'token', 's');
}
public function isAdmin($id) {
if ($this->getUserAdmin($id) == 1) return true;
return false;
}
public function setUserToken($id) {
$field = array(
@ -56,6 +58,18 @@ class User {
return $this->updateSingle($id, $field);
}
/**
* Fetch all users for administrative tasks
* @param none
* @return data array All users with db columns as array fields
**/
public function getUsers($filter='%') {
$stmt = $this->mysqli->prepare("SELECT * FROM " . $this->getTableName() . " WHERE username LIKE ?");
if ($this->checkStmt($stmt) && $stmt->bind_param('s', $filter) && $stmt->execute() && $result = $stmt->get_result()) {
return $result->fetch_all(MYSQLI_ASSOC);
}
}
/**
* Check user login
* @param username string Username
@ -266,15 +280,15 @@ class User {
private function checkUserPassword($username, $password) {
$this->debug->append("STA " . __METHOD__, 4);
$user = array();
$stmt = $this->mysqli->prepare("SELECT username, id FROM $this->table WHERE username=? AND pass=? LIMIT 1");
$stmt = $this->mysqli->prepare("SELECT username, id, admin FROM $this->table WHERE username=? AND pass=? LIMIT 1");
if ($this->checkStmt($stmt)) {
$stmt->bind_param('ss', $username, hash('sha256', $password.$this->salt));
$stmt->execute();
$stmt->bind_result($row_username, $row_id);
$stmt->bind_result($row_username, $row_id, $row_admin);
$stmt->fetch();
$stmt->close();
// Store the basic login information
$this->user = array('username' => $row_username, 'id' => $row_id);
$this->user = array('username' => $row_username, 'id' => $row_id, 'admin' => $row_admin);
return $username === $row_username;
}
return false;

View File

@ -0,0 +1,15 @@
<?php
// Make sure we are called from index.php
if (!defined('SECURITY'))
die('Hacking attempt');
// Check user to ensure they are admin
if (!$user->isAdmin($_SESSION['USERDATA']['id'])) {
header("HTTP/1.1 404 Page not found");
die();
}
// Tempalte specifics
$smarty->assign("CONTENT", "default.tpl");
?>

View File

@ -0,0 +1,37 @@
<?php
// Make sure we are called from index.php
if (!defined('SECURITY'))
die('Hacking attempt');
// Check user to ensure they are admin
if (!$user->isAdmin($_SESSION['USERDATA']['id'])) {
header("HTTP/1.1 404 Page not found");
die();
}
$aRoundShares = $statistics->getRoundShares();
if ($_POST['query']) {
// Fetch requested users
$aUsers = $statistics->getAllUserStats($_POST['query']);
// Add additional stats to each user
// This is not optimized yet, best is a proper SQL
// Query against the stats table? Currently cached though.
foreach ($aUsers as $iKey => $aUser) {
$aUser['balance'] = $transaction->getBalance($aUser['id']);
$aUser['payout']['est_block'] = round(( (int)$aUser['shares'] / (int)$aRoundShares['valid'] ) * (int)$config['reward'], 3);
$aUser['payout']['est_fee'] = round(($config['fees'] / 100) * $aUser['payout']['est_block'], 3);
$aUser['payout']['est_donation'] = round((( $aUser['donate_percent'] / 100) * ($aUser['payout']['est_block'] - $aUser['payout']['est_fee'])), 3);
$aUser['payout']['est_payout'] = round($aUser['payout']['est_block'] - $aUser['payout']['est_donation'] - $aUser['payout']['est_fee'], 3);
$aUsers[$iKey] = $aUser;
}
}
// Assign our variables
$smarty->assign("USERS", $aUsers);
// Tempalte specifics
$smarty->assign("CONTENT", "default.tpl");
?>

View File

@ -465,6 +465,12 @@ a:hover {
.block table tr th.right{
text-align: right;
}
.block table tr td.center{
text-align: center;
}
.block table tr th.center{
text-align: center;
}
.block table tr td.delete a { color: #666; }

Binary file not shown.

After

Width:  |  Height:  |  Size: 720 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 737 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 736 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 745 B

View File

@ -66,6 +66,10 @@ $(function () {
widgets: ['zebra']
});
$("table.pagesort")
.tablesorter({ widgets: ['zebra'] })
.tablesorterPager({ positionFixed: false, container: $("#pager") });
$('.block table tr th.header').css('cursor', 'pointer');
// Check / uncheck all checkboxes

View File

@ -0,0 +1,3 @@
{include file="global/block_header.tpl" BLOCK_HEADER="Admin Panel"}
<p>Welcome to the admin panel. Please select an option from the drop-down menu.</p>
{include file="global/block_footer.tpl"}

View File

@ -0,0 +1,77 @@
{include file="global/block_header.tpl" BLOCK_HEADER="Query User Database"}
<form action="{$smarty.server.PHP_SELF}" method="POST">
<input type="hidden" name="page" value="{$smarty.request.page}">
<input type="hidden" name="action" value="{$smarty.request.action}">
<input type="text" class="pin" name="query" value="{$smarty.request.query|default:"%"}">
<input type="submit" class="submit small" value="Query">
</form>
{include file="global/block_footer.tpl"}
{include file="global/block_header.tpl" BLOCK_HEADER="User Information"}
<center>
<div id="pager">
<form>
<img src="{$PATH}/images/first.png" class="first"/>
<img src="{$PATH}/images/prev.png" class="prev"/>
<input type="text" class="pagedisplay"/>
<img src="{$PATH}/images/next.png" class="next"/>
<img src="{$PATH}/images/last.png" class="last"/>
<select class="pagesize">
<option selected="selected" value="10">10</option>
<option value="20">20</option>
<option value="30">30</option>
<option value="40">40</option>
</select>
</form>
</div>
</center>
<table width="100%" class="pagesort">
<thead>
<tr>
<th class="center">ID</th>
<th>Username</th>
<th>E-Mail</th>
<th class="right">Hashrate&nbsp;&nbsp;</th>
<th class="right">Shares&nbsp;&nbsp;</th>
<th class="right">Est. Donation&nbsp;&nbsp;</th>
<th class="right">Est. Payout&nbsp;&nbsp;&nbsp;</th>
<th class="right">Balance&nbsp;&nbsp;&nbsp;</th>
<th class="center">Admin</th>
</tr>
</thead>
<tbody>
{section name=user loop=$USERS|default}
<tr>
<td class="center">{$USERS[user].id}</td>
<td>{$USERS[user].username}</td>
<td>{$USERS[user].email}</td>
<td class="right">{$USERS[user].hashrate / 1024}</td>
<td class="right">{$USERS[user].shares}</td>
<td class="right">{$USERS[user].payout.est_donation|number_format:"8"}</td>
<td class="right">{$USERS[user].payout.est_payout|number_format:"8"}</td>
<td class="right">{$USERS[user].balance|number_format:"8"}</td>
<td class="center">
<img src="{$PATH}/images/{if $USERS[user].admin}success{else}error{/if}.gif" />
</td>
</tr>
{sectionelse}
<tr>
<td colspan="9"></td>
</tr>
{/section}
</tbody>
<tfoot>
<tr>
<th class="center">ID</th>
<th>Username</th>
<th>E-Mail</th>
<th class="right">Hashrate</th>
<th class="center">Shares</th>
<th class="right">Est. Donation</th>
<th class="right">Est. Payout</th>
<th class="right">Balance</th>
<th class="center">Admin</th>
</tr>
</tfoot>
</table>
{include file="global/block_footer.tpl"}

View File

@ -10,7 +10,13 @@
</ul>
</li>
{/if}
{if $smarty.session.AUTHENTICATED|default:"0" == 1 && $GLOBAL.userdata.admin == 1}<li><a href="#">Admin Panel</a></li>{/if}
{if $smarty.session.AUTHENTICATED|default:"0" == 1 && $GLOBAL.userdata.admin == 1}
<li><a href="{$smarty.server.PHP_SELF}?page=admin">Admin Panel</a>
<ul>
<li><a href="{$smarty.server.PHP_SELF}?page=admin&action=user">User Info</a></li>
</ul>
</li>
{/if}
<li><a href="{$smarty.server.PHP_SELF}?page=statistics">Statistics</a>
<ul>
<li><a href="{$smarty.server.PHP_SELF}?page=statistics&action=pool">Pool Stats</a></li>