<?php
/**
* TS2WebAdmin - Lightweight TeamSpeak 2 Control Panel
*
* $Id: mod_users.php 2009-08-31 20:25:32 sven $
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package TS2WebAdmin
* @version 2.0.0-alpha2
* @author Sven 'ScP' Paulsen
* @copyright Copyright (c) 2009 by Planet TeamSpeak. All rights reserved.
*/
/* check if file is included correctly */
defined('TS2WA_VALID') || die('Access denied...');
/**
* @package TS2WebAdmin_Module_Users
* @category TS2WebAdmin_Module
*/
class TS2WebAdmin_Module_Users extends TS2WebAdmin_Module
{
/**
* Module requires SA permissions.
*
* @return void
*/
function checkLogin()
{
$this->isAuthorized(TS2WA_LOGIN_SERVERADMIN);
}
/**
* Displays a list of available user categories and user counts.
*
* @return void
*/
function indexAction()
{
$this->setSubMenu('users');
$server_id = (int) $this->_app->getSessionParam('server_id', 0);
if($server_info = $this->_ts2->sql_serverInfo($server_id))
{
$this->assign('count_users', $this->_ts2->sql_dbUserCount($server_id));
$this->assign('count_admins', $this->_ts2->sql_dbAdminUserCount($server_id));
}
$this->assign('serverinfo', $server_info);
$this->assign('count_sadmins', $this->_ts2->sql_dbSUserCount());
}
/**
* Displays a list of registered users from a virtual TeamSpeak server.
*
* @return void
*/
function listAction()
{
$this->setSubMenu('users');
$server_id = (int) $this->_app->getSessionParam('server_id', 0);
if(!$server_info = $this->_ts2->sql_serverInfo($server_id))
{
$this->_app->raiseError('Invalid virtual server ID', 500);
}
$filterName = $this->_app->getParam('filter', 'registered');
switch($filterName)
{
case 'admin':
$countMethod = 'sql_dbAdminUserCount';
$listMethod = 'sql_dbAdminUserList';
break;
default:
$countMethod = 'sql_dbUserCount';
$listMethod = 'sql_dbUserList';
}
$recordLimit = (int) $this->_app->getParam('l', 30);
$recordCount = (int) $this->_ts2->$countMethod($server_id);
if($recordLimit < 1 || $recordLimit > $recordCount)
{
$recordLimit = $recordCount;
}
$activePage = (int) $this->_app->getParam('p', 1);
$totalPages = (int) ceil($recordCount/$recordLimit);
if($activePage < 1 || $activePage > $totalPages)
{
$activePage = $totalPages;
}
$this->assign('filter', $filterName);
$this->assign('serverinfo', $server_info);
$this->assign('users', $this->_ts2->$listMethod($recordLimit, ($activePage-1)*$recordLimit, $server_id));
$this->assign('pagenav', buildPagination($totalPages, $activePage));
}
/**
* Displays a list of registered users matching a searched username.
*
* @return void
*/
function searchAction()
{
$this->setSubMenu('users');
$server_id = (int) $this->_app->getSessionParam('server_id', 0);
if(!$server_info = $this->_ts2->sql_serverInfo($server_id))
{
$this->_app->raiseError('Invalid virtual server ID', 500);
}
$search_pattern = $this->_app->getParam('pattern', FALSE);
if($search_pattern === FALSE || $search_pattern == 'search...')
{
$this->redirect('users');
}
$this->assign('pattern', $search_pattern);
$this->assign('serverinfo', $server_info);
$this->assign('users', $this->_ts2->sql_dbFindPlayer($search_pattern, $server_id));
}
/**
* Performs selected actions on multiple users.
*
* @return void
*/
function do_actionsAction()
{
$this->setNoRender();
$server_id = (int) $this->_app->getSessionParam('server_id', 0);
if(!$server_info = $this->_ts2->sql_serverInfo($server_id))
{
$this->_app->raiseError('Invalid virtual server ID', 500);
}
$userActions = $this->_app->getParam('user', array());
$userErrors = array();
foreach($userActions as $id => $cmd)
{
$cmdStatus = TRUE;
switch($cmd)
{
case 'demote':
$cmdStatus = $this->_ts2->sql_dbUserChangeSA($id, FALSE, $server_id);
break;
case 'promote':
$cmdStatus = $this->_ts2->sql_dbUserChangeSA($id, TRUE, $server_id);
break;
case 'delete':
$cmdStatus = $this->_ts2->sql_dbUserDel($id, $server_id);
break;
}
if(!$cmdStatus) {
$userErrors[$id]['user_id'] = $id;
$userErrors[$id]['user_cmd'] = $cmd;
$userErrors[$id]['user_rpl'] = $this->_ts2->debug_lastreply();
}
}
if(count($userErrors))
{
$this->_app->raiseError('Error processing user account actions', 500, $serverErrors);
}
$params = array();
if($urlData = parse_url($this->_app->getEnvParam('HTTP_REFERER', null)))
{
parse_str($urlData['query'], $qryData);
if(isset($qryData['filter'])) $params['filter'] = $qryData['filter'];
if(isset($qryData['p'])) $params['p'] = $qryData['p'];
if(isset($qryData['l'])) $params['l'] = $qryData['l'];
}
$this->_app->setLastEvent('All user account actions have been applied.');
$this->redirect('users', 'list', $params);
}
/**
* Displays a form to create a user.
*
* @return void
*/
function createAction()
{
$this->setSubMenu('users');
$server_id = (int) $this->_app->getSessionParam('server_id', 0);
if(!$server_info = $this->_ts2->sql_serverInfo($server_id)) {
$this->_app->raiseError('Invalid virtual server ID', 500);
}
$this->assign('serverinfo', $server_info);
}
/**
* Creates a user.
*
* @return void
*/
function do_createAction()
{
$this->setNoRender();
$server_id = (int) $this->_app->getSessionParam('server_id', 0);
if(!$server_info = $this->_ts2->sql_serverInfo($server_id)) {
$this->_app->raiseError('Invalid virtual server ID', 500);
}
if(!$this->_app->getParam('client_username'))
{
$this->_app->raiseError('Username is missing or invalid', 500);
}
elseif($this->_app->getParam('client_password1') != $this->_app->getParam('client_password2'))
{
$this->_app->raiseError('Passwords do not match', 500);
}
elseif(!$this->_app->getParam('client_password1'))
{
$this->_app->raiseError('Password is missing or invalid', 500);
}
if(!$this->_ts2->sql_dbUserAdd($this->_app->getParam('client_username'), $this->_app->getParam('client_password1'), $this->_app->getParam('client_admin', 0), $server_id, $this->_app->getConfigParam('server_md5patch', FALSE)))
{
$this->_app->raiseError('Error creating user account', 500, $this->_ts2->debug_lastreply());
}
$user_id = $this->_ts2->sql_lastInsertId($this->_app->getConfigParam('server_issqlite', TRUE));
$this->_app->setLastEvent('A new user account has been created with ID ' . $user_id . '.');
$this->redirect('users', 'list');
}
/**
* Displays a form to modify a user.
*
* @return void
*/
function editAction()
{
$this->setSubMenu('users');
$server_id = (int) $this->_app->getSessionParam('server_id', 0);
if(!$server_info = $this->_ts2->sql_serverInfo($server_id)) {
$this->_app->raiseError('Invalid virtual server ID', 500);
}
$user_id = (int) $this->_app->getParam('id', 0);
if(!$user_info = $this->_ts2->sql_dbUserInfo($user_id, $server_id)) {
$this->_app->raiseError('Invalid user ID', 500);
}
$this->_ts2->sql_dbCleanupUserChannelPrivs($user_id, $server_id);
$this->assign('userinfo', $user_info);
$this->assign('serverinfo', $server_info);
$this->assign('userprivs', $this->_ts2->sql_dbUserChannelPrivs($user_id, $server_id));
$this->assign('dbchannels', $this->_ts2->sql_dbChannelList($server_id));
}
/**
* Modifies a user in the database.
*
* @return void
*/
function do_editAction()
{
$this->setNoRender();
$server_id = (int) $this->_app->getSessionParam('server_id', 0);
if(!$server_info = $this->_ts2->sql_serverInfo($server_id)) {
$this->_app->raiseError('Invalid virtual server ID', 500);
}
$user_id = (int) $this->_app->getParam('user_id', 0);
if(!$user_info = $this->_ts2->sql_dbUserInfo($user_id, $server_id)) {
$this->_app->raiseError('Invalid user ID', 500);
}
$props = array(
'b_client_privilege_serveradmin' => $this->_app->getParam('client_admin', 0),
);
if($user_info['s_client_name'] != $this->_app->getParam('client_username', $user_info['s_client_name']))
{
$props['s_client_name'] = $this->_app->getParam('client_username', $user_info['s_client_name']);
}
if($this->_app->getParam('client_password1') != $this->_app->getParam('client_password2'))
{
$this->_app->raiseError('Passwords do not match', 500);
}
else
{
if($this->_app->getParam('client_password1')) $props['s_client_password'] = $this->_app->getParam('client_password1');
}
if(!$this->_ts2->sql_dbUserEdit($user_id, $props, $server_id, $this->_app->getConfigParam('server_md5patch', FALSE)))
{
$this->_app->raiseError('Error modifying user account', 500, $this->_ts2->debug_lastreply());
}
$this->_app->setLastEvent('The user account with ID ' . $user_id . ' has been modified.');
$this->redirect('users', 'list');
}
/**
* Modifies a users channel privs in the database.
*
* @return void
*/
function do_privaddAction()
{
$this->setNoRender();
$user_id = (int) $this->_app->getParam('user_id', 0);
$server_id = (int) $this->_app->getSessionParam('server_id', 0);
$channel_id = (int) $this->_app->getParam('channel_id', 0);
$channel_priv = $this->_app->getParam('channel_priv');
if($channel_id != 0 && $channel_priv != 'false')
{
if(!$this->_ts2->sql_serverInfo($server_id))
{
$this->_app->raiseError('Invalid virtual server ID', 500);
}
elseif(!$this->_ts2->sql_dbChannelInfo($channel_id, $server_id))
{
$this->_app->raiseError('Invalid channel ID', 500);
}
elseif(!$this->_ts2->sql_dbUserInfo($user_id, $server_id))
{
$this->_app->raiseError('Invalid user ID', 500);
}
$this->_ts2->sql_dbUpdateUserChannelPrivs($user_id, $channel_id, array($channel_priv => 1), $server_id);
$this->_app->setLastEvent('The channel privilege has been added successfully.');
}
$this->redirect('users', 'edit', array('id' => $user_id));
}
/**
* Deletes a users channel priv from the database.
*
* @return void
*/
function do_privdelAction()
{
$this->setNoRender();
$server_id = intval($this->_app->getSessionParam('server_id', 0));
if(!$this->_ts2->sql_serverInfo($server_id))
{
$this->_app->raiseError('Invalid virtual server ID', 500);
}
$user_id = intval($this->_app->getParam('user_id', 0));
if(!$this->_ts2->sql_dbUserInfo($user_id, $server_id))
{
$this->_app->raiseError('Invalid user ID', 500);
}
$this->_ts2->sql_dbDeleteUserChannelPriv($this->_app->getParam('priv_id', 0), $server_id);
$this->_app->setLastEvent('The channel privilege has been deleted successfully.');
$this->redirect('users', 'edit', array('id' => $user_id));
}
/**
* Updates a users channel privs in the database.
*
* @return void
*/
function do_privsetAction()
{
$this->setNoRender();
$server_id = intval($this->_app->getSessionParam('server_id', 0));
if(!$this->_ts2->sql_serverInfo($server_id))
{
$this->_app->raiseError('Invalid virtual server ID', 500);
}
$user_id = intval($this->_app->getParam('user_id', 0));
if(!$this->_ts2->sql_dbUserInfo($user_id, $server_id))
{
$this->_app->raiseError('Invalid user ID', 500);
}
foreach($this->_app->getParam('privs', array()) as $priv)
{
$flags = array(
'b_cp_flag_admin' => isset($priv['b_cp_flags']['b_cp_flag_admin']) ? 1 : 0,
'b_cp_flag_autoop' => isset($priv['b_cp_flags']['b_cp_flag_autoop']) ? 1 : 0,
'b_cp_flag_autovoice' => isset($priv['b_cp_flags']['b_cp_flag_autovoice']) ? 1 : 0,
);
$this->_ts2->sql_dbUpdateUserChannelPrivs($user_id, $priv['i_cp_channel_id'], $flags, $server_id);
}
$this->_app->setLastEvent('All channel privileges have been updated successfully.');
$this->redirect('users', 'edit', array('id' => $user_id));
}
/**
* Displays a form to cleanup the user database.
*
* @return void
*/
function toolsAction()
{
$this->setSubMenu('users');
$server_id = intval($this->_app->getSessionParam('server_id', 0));
if(!$server_info = $this->_ts2->sql_serverInfo($server_id))
{
$this->_app->raiseError('Invalid virtual server ID', 500);
}
$this->assign('serverinfo', $server_info);
}
/**
* Performs tasks to cleanup the user database.
*
* @return void
*/
function do_toolsAction()
{
$this->setNoRender();
$server_id = intval($this->_app->getSessionParam('server_id', 0));
if(!$this->_ts2->sql_serverInfo($server_id))
{
$this->_app->raiseError('Invalid virtual server ID', 500);
}
if($this->_app->getParam('prune_exec'))
{
$delete_ids = array();
$days = $this->_app->getParam('prune_days', 365);
$date = $this->_app->getParam('prune_date', array());
if(!isset($date['Date_Month'])) $date['Date_Month'] = date('Y');
if(!isset($date['Date_Day'])) $date['Date_Day'] = date('d');
if(!isset($date['Date_Year'])) $date['Date_Year'] = date('m');
$lastonline = strtotime('today -' . intval($days) . ' days');
$registered = strtotime($date['Date_Month'] . '/' . $date['Date_Day'] . '/' . $date['Date_Year']);
$accounts = $this->_ts2->sql_dbUserList(null, null, $server_id);
foreach($accounts as $account)
{
if($account['dt_client_lastonline'] < $lastonline && $account['dt_client_created'] < $registered)
{
if($account['b_client_privilege_serveradmin'] == 0) $delete_ids[] = $account['i_client_id'];
}
}
foreach($delete_ids as $id)
{
$this->_ts2->admin_dbUserDel($id);
}
$this->_app->setLastEvent('A total of ' . count($delete_ids) . ' inactive users have been pruned away successfully.');
}
if($this->_app->getParam('orphan_exec'))
{
$this->_ts2->sql_CleanupChannelPrivs();
if(!$this->_app->hasLastEvent())
{
$this->_app->setLastEvent('The user database has been maintained successfully.');
}
}
$this->redirect('users', 'list');
}
}