<?php
session_start();
//------------------------------------------------------------------------------
// Includes
//------------------------------------------------------------------------------
include_once('../inc/config.php');
include_once('../inc/util.inc');
include_once('../inc/validation_class.php');
include_once('../inc/html_class.php');
mysql_connect($db_server, $db_username, $db_password);
mysql_select_db($db_database);
$validation = new Validation();
$html = new Html();
//------------------------------------------------------------------------------
// Setup Theme
//------------------------------------------------------------------------------
$THEME = array(
'site_name' => getSiteName(),
'path' => '../',
'page' => 'users',
'page_title' => _('Administration'),
'theme_path' => getTheme(),
'modules' => getAdminModules(),
);
//------------------------------------------------------------------------------
// Control Structure
//------------------------------------------------------------------------------
include $THEME['path'].$THEME['theme_path'].'adminheader.php';
if (!isLoggedInAdmin()) {
handleBadLogin();
return;
}
displayMenu();
if (isset($_GET['add'])) {
displayAddUserForm();
} elseif (isset($_GET['edit']) && ctype_digit($_GET['edit'])) {
displayEditUserForm($_GET['edit']);
} elseif (isset($_POST['add-user'])) {
displayAddSubmit($_POST);
} elseif (isset($_POST['edit-user'])) {
displayEditSubmit($_POST);
} elseif (isset($_POST['delete-users']) && isset($_POST['chk-delete'])) {
displayDeleteSubmit($_POST);
} else {
displayUserList();
}
include $THEME['path'].$THEME['theme_path'].'footer.php';
return;
//------------------------------------------------------------------------------
// Functions
//------------------------------------------------------------------------------
function handleBadLogin ()
{
echo '
<div class="error-alert">'._('You must login as an Administrator to view this page.').'</div>';
displayLogin('users.php');
}
function displayMenu ()
{
echo '
<div id="sub-navigation" class="clearfix">
<ul id="sub-nav" class="clearfix">
<li class="action"><a href="?add=user">'._('Create New User').'</a></li>
</ul>
</div>';
}
/**
* displayAddSubmit
*
* @param array $data
*
* @return void
*/
function displayAddSubmit ($data)
{
global $validation;
$username = $validation->cleanInput($data['username']);
$password = $validation->cleanInput($data['password']);
$first = $validation->cleanInput($data['first']);
$last = $validation->cleanInput($data['last']);
$email = $validation->cleanInput($data['email']);
if ( strlen($username) < 1
&& strlen($password) < 1
&& strlen($first) < 1
&& strlen($last) < 1
&& strlen($email) < 1
) {
echo '
<div class="error-alert">'._('Please fill out all fields please.').'</div>';
displayAddUserForm();
return;
}
$admin = isset($data['admin']) ? 1 : 0;
$sql = "INSERT INTO `sam_user` (
`username`, `first`, `last`, `email`, `password`, `admin`, `joindate`
) VALUES (
'$username', '$first', '$last', '$email', '$password', '$admin', NOW()
)";
if (!mysql_query($sql)) {
logSqlError(__FILE__, __LINE__, mysql_error(), $sql);
echo '
<div class="error-alert">'._('Could not add user.').'</div>';
displayAddUserForm();
return;
}
echo '
<div class="ok-alert">'._('New user added.').'</div>';
displayUserList();
return;
}
/**
* displayEditSubmit
*
* @param array $data
*
* @return void
*/
function displayEditSubmit ($data)
{
global $validation;
$id = $validation->cleanInput($data['id'], 'int');
$password = isset($data['password']) ? $validation->cleanInput($data['password']) : '';
$first = isset($data['first']) ? $validation->cleanInput($data['first']) : '';
$last = isset($data['last']) ? $validation->cleanInput($data['last']) : '';
$email = isset($data['email']) ? $validation->cleanInput($data['email']) : '';
$update = '';
if (!empty($password)) {
$password = md5($password);
$update .= "`password` = '$password', ";
}
if (!empty($first)) {
$update .= "`first` = '$first', ";
}
if (!empty($last)) {
$update .= "`last` = '$last', ";
}
if (!empty($email)) {
$update .= "`email` = '$email', ";
}
$admin = isset($_POST['admin']) ? 1 : 0;
$update .= "`admin` = $admin";
$sql = "UPDATE `sam_user`
SET $update
WHERE `id` = '$id'";
if (!mysql_query($sql)) {
$error = _('Could not update user.');
displaySqlError($error, __FILE__, __LINE__, mysql_error(), $sql);
return;
}
echo '
<div class="ok-alert">'._('User updated.').'</div>';
displayUserList();
return;
}
/**
* displayDeleteSubmit
*
* @param array $data
*
* @return void
*/
function displayDeleteSubmit ($data)
{
global $validation;
foreach ($data['chk-delete'] as $id) {
$id = $validation->cleanInput($id, 'int');
if ($id > 0) {
$sql = "DELETE FROM `sam_user`
WHERE `id` = '$id'
LIMIT 1";
if (!mysql_query($sql)) {
$error = _('Could not delete user.');
displaySqlError($error, __FILE__, __LINE__, mysql_query(), $sql);
return;
}
}
}
echo '
<div class="ok-alert">'._('User deleted.').'</div>';
displayUserList();
return;
}
/**
* displayUserList
*
* Displays the list of users in a table
*
* @return void
*/
function displayUserList ()
{
echo '
<form method="post" action="users.php">
<table id="users" class="data">
<thead>
<tr>
<th class="check"> </th>
<th class="admin">'._('Admin?').'</th>
<th>'._('Username').'</th>
<th>'._('First').'</th>
<th>'._('Last').'</th>
<th>'._('Joined').'</th>
</tr>
</thead>';
$sql = "SELECT `id`, `first`, `last`, `joindate`, `admin`, `username`
FROM `sam_user`
ORDER BY `joindate`";
$result = mysql_query($sql);
if (!$result) {
$error = _('Could not get user information.');
echo '<tbody><tr><td colspan="6">';
displaySqlError($error, __FILE__, __LINE__, mysql_error(), $sql);
echo '</td></tr></tbody></table></form>';
return;
}
if (mysql_num_rows($result) < 1) {
$error = _('No users found.');
echo '<tbody><tr><td colspan="6">'.$error.'</td></tr></tbody></table></form>';
return;
}
while ($r = mysql_fetch_array($result)) {
// Is user and admin?
$admin = $r['admin'] == 1 ? 'yes' : 'no';
// Don't allow deleting of first admin
$check = $r['id'] == 1 ? ' ' : '<input type="checkbox" name="chk-delete[]" value="'.$r['id'].'"/>';
echo '
<tbody>
<tr>
<td class="check">'.$check.'</td>
<td class="admin '.$admin.'"> </td>
<td><a href="?edit='.$r['id'].'">'.$r['username'].'</a></td>
<td>'.$r['first'].'</td>
<td>'.$r['last'].'</td>
<td>'.daysAgo($r['joindate']).'</td>
</tr>
</tbody>';
}
echo '
</table>
<input class="sub1" type="submit" id="delete-users" name="delete-users" value="'._('Delete Selected').'"/>
</form>';
}
/**
* displayEditUserForm
*
* Displays the form to edit a user
*
*/
function displayEditUserForm ($id)
{
global $html, $validation;
$id = $validation->cleanInput($id);
$sql = "SELECT `id`, `username`, `first`, `last`, `email`, `admin`
FROM `sam_user`
WHERE `id` = '$id'";
$result = mysql_query($sql);
if (!$result) {
$error = _('Could not get user information.');
displaySqlError($error, __FILE__, __LINE__, mysql_error(), $sql);
return;
}
if (mysql_num_rows($result) < 1) {
$error = _('Could not get user information.');
displayError(__FILE__, __LINE__, $error);
return;
}
$r = mysql_fetch_array($result);
$check = $r['admin'] > 0 ? 'checked="checked"' : '';
// Can't remove admin status from first admin
$disabled = ($r['id'] == 1) ? 1 : 0;
$params = array(
array(
'input' => 'text',
'id' => 'username',
'label' => _('Username'),
'value' => $validation->cleanOutput($r['username']),
'disabled' => 1,
),
array(
'input' => 'text',
'id' => 'password',
'label' => _('Password'),
),
array(
'input' => 'text2',
'id' => 'first',
'value' => $validation->cleanOutput($r['first']),
'maxlength' => '50',
'label' => _('Name (First Last)'),
'id2' => 'last',
'value2' => $validation->cleanOutput($r['last']),
'maxlength2' => '50',
),
array(
'input' => 'text',
'id' => 'email',
'label' => _('Email'),
'value' => $validation->cleanOutput($r['email']),
'maxlength' => '100',
),
array(
'input' => 'checkbox',
'id' => 'admin',
'label' => _('Make Administrator?'),
'value' => 1,
'checked' => $r['admin'],
'disabled' => $disabled,
),
array(
'input' => 'hidden',
'id' => 'id',
'value' => $id,
),
array(
'input' => 'submit-cancel',
'id' => 'edit-user',
'value' => _('Edit'),
),
);
$html->displayForm('users.php', _('Edit User'), $params);
}
/**
* displayAddUserForm
*
* Displays the form to add a new user
*
*/
function displayAddUserForm ()
{
global $html;
$params = array(
array(
'input' => 'text',
'id' => 'username',
'label' => _('Username'),
'maxlength' => '20',
),
array(
'input' => 'text',
'id' => 'password',
'label' => _('Password'),
),
array(
'input' => 'text2',
'id' => 'first',
'maxlength' => '50',
'label' => _('Name (First Last)'),
'id2' => 'last',
'maxlength2' => '50',
),
array(
'input' => 'text',
'id' => 'email',
'label' => _('Email'),
'maxlength' => '100',
),
array(
'input' => 'checkbox',
'id' => 'admin',
'label' => _('Make Administrator?'),
),
array(
'input' => 'submit-cancel',
'id' => 'add-user',
'value' => _('Create'),
),
);
$html->displayForm('users.php', _('New User'), $params);
}