Location: PHPKode > projects > TS2WebAdmin > ts2webadmin/app/mod_admins.php
<?php

/**
 * TS2WebAdmin - Lightweight TeamSpeak 2 Control Panel
 *
 * $Id: mod_admins.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_Admins
 * @category TS2WebAdmin_Module
 */
class TS2WebAdmin_Module_Admins extends TS2WebAdmin_Module
{
  /**
   * Module requires SSA permissions.
   * 
   * @return void
   */
  function checkLogin()
  {
    $this->isAuthorized(TS2WA_LOGIN_SUPERADMIN);
  }

  /**
   * Displays a list of SSA accounts from the TeamSpeak servers database.
   * 
   * @return void
   */
  function indexAction()
  {
    $this->setSubMenu('users');
    
    $recordLimit = (int) $this->_app->getParam('l', 30);
    $recordCount = (int) $this->_ts2->sql_dbSUserCount();

    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('users', $this->_ts2->sql_dbSUserList($recordLimit, ($activePage-1)*$recordLimit));
    $this->assign('pagenav', buildPagination($totalPages, $activePage));
  }
  
  /**
   * Performs selected actions on multiple SSA accounts.
   * 
   * @return void
   */
  function do_actionsAction()
  {
    $this->setNoRender();
    
    $userActions = $this->_app->getParam('user', array());
    $userErrors = array();

    foreach($userActions as $id => $cmd)
    {
      $cmdStatus = TRUE;

      switch($cmd)
      {
        case 'delete':
          $cmdStatus = $this->_ts2->sql_dbSUserDel($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 administrator account actions', 500, $userErrors);
    }

    $this->_app->setLastEvent('All administrator account actions have been applied.');

    $this->redirect('admins');
  }
  
  /**
   * Displays a form to create a new SSA account in the TeamSpeak servers database.
   * 
   * @return void
   */
  function createAction()
  {
    $this->setSubMenu('users');
  }
  
  /**
   * Creates a new SSA account in the TeamSpeak servers database.
   * 
   * @return void
   */
  function do_createAction()
  {
    $this->setNoRender();
    
    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_dbSUserAdd($this->_app->getParam('client_username'), $this->_app->getParam('client_password1'), $this->_app->getConfigParam('server_md5patch', FALSE)))
    {
      $this->_app->raiseError('Error creating administrator account', 500, $this->_ts2->debug_lastreply());
    }
    
    $user_id = $this->_ts2->sql_lastInsertId($this->_app->getConfigParam('server_issqlite', TRUE));
  
    $this->_app->setLastEvent('A new administrator account has been created with ID ' . $user_id . '.');
    
    $this->redirect('admins');
  }
  
  /**
   * Displays a form to modify an existing SSA account in the TeamSpeak servers database.
   * 
   * @return void
   */
  function editAction()
  {
    $this->setSubMenu('users');

    $user_id = (int) $this->_app->getParam('id', 0);

    if(!$user_info = $this->_ts2->sql_dbSUserInfo($user_id)) {
      $this->_app->raiseError('Invalid user ID', 500);
    }
    
    $this->assign('userinfo', $user_info);
  }
  
  /**
   * Modifies a SSA in the database.
   * 
   * @return void
   */
  function do_editAction()
  {
    $this->setNoRender();

    $user_id = (int) $this->_app->getParam('user_id', 0);
    
    if(!$user_info = $this->_ts2->sql_dbSUserInfo($user_id)) {
      $this->_app->raiseError('Invalid user ID', 500);
    }
    
    $props = array();
    
    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);
    }
    elseif($this->_app->getParam('client_password1'))
    {
      $props['s_client_password'] = $this->_app->getParam('client_password1');
    }
    
    if(!$this->_ts2->sql_dbSUserEdit($user_id, $props, $this->_app->getConfigParam('server_md5patch', FALSE)))
    {
      $this->_app->raiseError('Error modifying administrator account', 500, $this->_ts2->debug_lastreply());
    }

    $this->_app->setLastEvent('The administrator account with ID ' . $user_id . ' has been modified.');
    
    $this->redirect('admins');
  }
}
Return current item: TS2WebAdmin