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

/**
 * TS2WebAdmin - Lightweight TeamSpeak 2 Control Panel
 *
 * $Id: mod_servers.php 2009-09-03 20:23:12 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_Servers
 * @category TS2WebAdmin_Module
 */
class TS2WebAdmin_Module_Servers extends TS2WebAdmin_Module
{
  /**
   * Module requires SSA permissions.
   * 
   * @return void
   */
  function checkLogin()
  {
    $this->isAuthorized(TS2WA_LOGIN_SUPERADMIN);
  }

  /**
   * Displays a list of virtual servers.
   * 
   * @return void
   */
  function indexAction()
  {
    $recordLimit = $this->_app->getParam('l', 30);
    $recordCount = $this->_ts2->sql_serverCount();

    $activePage = $this->_app->getParam('p', 1);
    $totalPages = ceil($recordCount/$recordLimit);

    if($activePage > $totalPages)
    {
      $activePage = $totalPages;
    }
    
    $serverlist = $this->_ts2->sql_serverList($recordLimit, ($activePage-1)*$recordLimit);
    
    for($i = 0; $i < count($serverlist); $i++)
    {
      if($serverlist[$i]['b_server_active'] == -1)
      {
        $serverlist[$i]['i_server_currentusers'] = ($this->_ts2->select($serverlist[$i]['i_server_udpport'])) ? $this->_ts2->info_serverCurrentUsers() : 0;
      }
      else
      {
        $serverlist[$i]['i_server_currentusers'] = 0;
      }
    }

    $this->assign('servers', $serverlist);
    $this->assign('pagenav', buildPagination($totalPages, $activePage));
  }

  /**
   * Performs selected actions on multiple virtual servers.
   * 
   * @return void
   */
  function do_actionsAction()
  {
    $this->setNoRender();

    $serverActions = $this->_app->getParam('server', array());
    $serverErrors = array();

    $currentServer = $this->_app->getSessionParam('server_port', 0);

    foreach($serverActions as $key => $val)
    {
      if(strstr($key, ':') === FALSE) continue;
      list($sid, $udp) = explode(':', $key);
      
      $cmdStatus = TRUE;

      switch($val)
      {
        case 'start':
          $cmdStatus = $this->_ts2->sadmin_serverStart($sid);
          break;

        case 'stop':     
          $cmdStatus = $this->_ts2->sadmin_serverStop($sid);
          break;

        case 'restart':
          $cmdStatus = $this->_ts2->sadmin_serverRestart($sid);
          break;

        case 'delete':
          if($udp == $currentServer) {
            $this->_app->setSessionParam('server_id', -1);
            $this->_app->setSessionParam('server_port', 0);
          }
          $cmdStatus = $this->_ts2->sadmin_serverDelete($sid);
          break;
      }

      if(!$cmdStatus)
      {
        $serverErrors[$sid]['server_id'] = $sid;
        $serverErrors[$sid]['server_port'] = $udp;
        $serverErrors[$sid]['server_cmd'] = $val;
        $serverErrors[$sid]['server_rpl'] = $this->_ts2->debug_lastreply();
      }
    }

    if(count($serverErrors))
    {
      $this->_app->raiseError('Error processing actions', 500, $serverErrors);
    }
    
    $params = array();

    if($urlData = parse_url($this->_app->getEnvParam('HTTP_REFERER', null)))
    {
      parse_str($urlData['query'], $qryData);

      if(isset($qryData['p'])) $params['p'] = $qryData['p'];
      if(isset($qryData['l'])) $params['l'] = $qryData['l'];
    }
    
    $this->_app->setLastEvent('All virtual server actions have been applied successfully.');

    $this->redirect('servers', 'index', $params);
  }

  /**
   * Displays a form to create a virtual server.
   * 
   * @return void
   */
  function createAction()
  {
    $this->assign('default_server_name', 'TeamSpeak Server');
    $this->assign('default_server_udpport', 8767);
    $this->assign('default_server_maxusers', 16);
    $this->assign('default_server_welcomemessage', '[ Welcome to TeamSpeak, check www.teamspeak.com ]');
    $this->assign('default_admin_username', 'admin');
  }

  /**
   * Creates a virtual server.
   * 
   * @return void
   */
  function do_createAction()
  {
    $this->setNoRender();

    if(!$server_udpport = (int) $this->_app->getParam('server_udpport', 0))
    {
      $this->_app->raiseError('Specified port number is invalid', 500);
    }

    if(!$this->_ts2->sadmin_serverAdd($server_udpport))
    {
      $this->_app->raiseError('Error creating virtual server', 500, $this->_ts2->debug_lastreply());
    }

    $server_id = $this->_ts2->sadmin_dbServerIdByPort($server_udpport);

    if(!$this->_ts2->sadmin_serverStatus($server_id) && !$this->_ts2->sadmin_serverStart($server_id))
    {
      $this->_app->raiseError('Error starting virtual server', 500, $this->_ts2->debug_lastreply());
    }

    $this->_ts2->sadmin_serverSet('server_name', $this->_app->getParam('server_name', 'TeamSpeak Server'));
    $this->_ts2->sadmin_serverSet('server_maxusers', $this->_app->getParam('server_maxusers', 16));
    $this->_ts2->sadmin_serverSet('server_welcomemessage', $this->_app->getParam('server_welcomemessage', '[ Welcome to TeamSpeak, check www.teamspeak.com ]'));
    $this->_ts2->sadmin_serverSet('server_password', $this->_app->getParam('server_password', '#CLEAR#'));
    $this->_ts2->sadmin_serverSet('server_clan_server', $this->_app->getParam('server_clan_server', 0));

    $this->_ts2->sadmin_serverSet('server_allow_codec_celp51', $this->_app->getParam('server_allow_codec_celp51', 0));
    $this->_ts2->sadmin_serverSet('server_allow_codec_celp63', $this->_app->getParam('server_allow_codec_celp63', 0));
    $this->_ts2->sadmin_serverSet('server_allow_codec_gsm148', $this->_app->getParam('server_allow_codec_gsm148', 0));
    $this->_ts2->sadmin_serverSet('server_allow_codec_gsm164', $this->_app->getParam('server_allow_codec_gsm164', 0));
    $this->_ts2->sadmin_serverSet('server_allow_codec_windowscelp52', $this->_app->getParam('server_allow_codec_windowscelp52', 0));

    $this->_ts2->sadmin_serverSet('server_allow_codec_speex2150', $this->_app->getParam('server_allow_codec_speex2150', 0));
    $this->_ts2->sadmin_serverSet('server_allow_codec_speex3950', $this->_app->getParam('server_allow_codec_speex3950', 0));
    $this->_ts2->sadmin_serverSet('server_allow_codec_speex5950', $this->_app->getParam('server_allow_codec_speex5950', 0));
    $this->_ts2->sadmin_serverSet('server_allow_codec_speex8000', $this->_app->getParam('server_allow_codec_speex8000', 0));
    $this->_ts2->sadmin_serverSet('server_allow_codec_speex11000', $this->_app->getParam('server_allow_codec_speex11000', 0));
    $this->_ts2->sadmin_serverSet('server_allow_codec_speex15000', $this->_app->getParam('server_allow_codec_speex15000', 0));
    $this->_ts2->sadmin_serverSet('server_allow_codec_speex18200', $this->_app->getParam('server_allow_codec_speex18200', 0));
    $this->_ts2->sadmin_serverSet('server_allow_codec_speex24600', $this->_app->getParam('server_allow_codec_speex24600', 0));

    if($this->_app->getParam('server_webpost_linkurl')) $this->_ts2->sadmin_serverSet('server_webpost_linkurl', $this->_app->getParam('server_webpost_linkurl'));
    if($this->_app->getParam('server_webpost_posturl')) $this->_ts2->sadmin_serverSet('server_webpost_posturl', $this->_app->getParam('server_webpost_posturl'));

    if($this->_app->getParam('admin_create'))
    {
      $admin_user = $this->_app->getParam('admin_username');
      $admin_pass1 = $this->_app->getParam('admin_password1');
      $admin_pass2 = $this->_app->getParam('admin_password2');
      
      if($admin_pass1 != $admin_pass2)
      {
        $this->_app->raiseError('User passwords do not match', 500, array($admin_pass1, $admin_pass2));
      }
      
      if(!$this->_ts2->sql_dbUserAdd($admin_user, $admin_pass1, TRUE, $server_id, $this->_app->getConfigParam('server_md5patch', FALSE)))
      {
        $this->_app->raiseError('Error creating admin account', 500, $this->_ts2->debug_lastreply());
      }
    }
    
    $this->_app->setLastEvent('A new virtual server has been created on port ' . $server_udpport . '.');
    
    $this->redirect('servers');
  }

  /**
   * Displays a form to edit a virtual server.
   * 
   * @return void
   */
  function editAction()
  {
    $server_id = (int) $this->_app->getParam('id', 0);

    if(!$server_info = $this->_ts2->sql_serverInfo($server_id))
    {
      $this->_app->raiseError('Invalid virtual server ID', 500);
    }

    $this->assign('serverinfo', $server_info);
  }

  /**
   * Edits a virtual server.
   * 
   * @return void
   */
  function do_editAction()
  {
    $this->setNoRender();

    $server_id = (int) $this->_app->getParam('server_id', 0);
    $server_active = ($this->_app->getParam('server_active', 0) == 1) ? -1 : 0;

    if(!$server_info = $this->_ts2->sql_serverInfo($server_id))
    {
      $this->_app->raiseError('Invalid virtual server ID', 500);
    }
    
    if($this->_ts2->sadmin_serverStatus($server_id))
    {
      if($this->_ts2->udp != $server_info['i_server_udpport'])
      {
        $this->_ts2->select($server_info['i_server_udpport']);
      }

      $this->_ts2->sadmin_serverSet('server_name', $this->_app->getParam('server_name', 'TeamSpeak Server'));
      $this->_ts2->sadmin_serverSet('server_maxusers', $this->_app->getParam('server_maxusers', 16));
      $this->_ts2->sadmin_serverSet('server_welcomemessage', $this->_app->getParam('server_welcomemessage', '[ Welcome to TeamSpeak, check www.teamspeak.com ]'));
      $this->_ts2->sadmin_serverSet('server_password', $this->_app->getParam('server_password', '#CLEAR#'));
      $this->_ts2->sadmin_serverSet('server_clan_server', $this->_app->getParam('server_clan_server', 0));

      $this->_ts2->sadmin_serverSet('server_allow_codec_celp51', $this->_app->getParam('server_allow_codec_celp51', 0));
      $this->_ts2->sadmin_serverSet('server_allow_codec_celp63', $this->_app->getParam('server_allow_codec_celp63', 0));
      $this->_ts2->sadmin_serverSet('server_allow_codec_gsm148', $this->_app->getParam('server_allow_codec_gsm148', 0));
      $this->_ts2->sadmin_serverSet('server_allow_codec_gsm164', $this->_app->getParam('server_allow_codec_gsm164', 0));
      $this->_ts2->sadmin_serverSet('server_allow_codec_windowscelp52', $this->_app->getParam('server_allow_codec_windowscelp52', 0));

      $this->_ts2->sadmin_serverSet('server_allow_codec_speex2150', $this->_app->getParam('server_allow_codec_speex2150', 0));
      $this->_ts2->sadmin_serverSet('server_allow_codec_speex3950', $this->_app->getParam('server_allow_codec_speex3950', 0));
      $this->_ts2->sadmin_serverSet('server_allow_codec_speex5950', $this->_app->getParam('server_allow_codec_speex5950', 0));
      $this->_ts2->sadmin_serverSet('server_allow_codec_speex8000', $this->_app->getParam('server_allow_codec_speex8000', 0));
      $this->_ts2->sadmin_serverSet('server_allow_codec_speex11000', $this->_app->getParam('server_allow_codec_speex11000', 0));
      $this->_ts2->sadmin_serverSet('server_allow_codec_speex15000', $this->_app->getParam('server_allow_codec_speex11000', 0));
      $this->_ts2->sadmin_serverSet('server_allow_codec_speex18200', $this->_app->getParam('server_allow_codec_speex18200', 0));
      $this->_ts2->sadmin_serverSet('server_allow_codec_speex24600', $this->_app->getParam('server_allow_codec_speex24600', 0));
      
      $server_udpport = $this->_ts2->sadmin_dbServerPortById($server_id);
    }
    else
    {
      if(!$server_udpport = (int) $this->_app->getParam('server_udpport', 0))
      {
        $this->_app->raiseError('Specified port number is invalid', 500, $server_udpport);
      }
      
      $data = array(
        "servername"           => $this->_app->getParam('server_name', 'TeamSpeak Server'),
        "serverudpport"        => $server_udpport,
        "servermaxusers"       => $this->_app->getParam('server_maxusers', 16),
        "serverpassword"       => $this->_app->getParam('server_password', ''),
        "serverwelcomemessage" => $this->_app->getParam('server_welcomemessage', '[ Welcome to TeamSpeak, check www.teamspeak.com ]'),
        "CODECCelp51"          => $this->_app->getParam('server_allow_codec_celp51', 0),
        "CODECCelp63"          => $this->_app->getParam('server_allow_codec_celp63', 0),
        "CODECGSM148"          => $this->_app->getParam('server_allow_codec_gsm148', 0),
        "CODECGSM164"          => $this->_app->getParam('server_allow_codec_gsm164', 0),
        "CODECWindowsCELP52"   => $this->_app->getParam('server_allow_codec_windowscelp52', 0),
        "CODECSPEEX2150"       => $this->_app->getParam('server_allow_codec_speex2150', 0),
        "CODECSPEEX3950"       => $this->_app->getParam('server_allow_codec_speex3950', 0),
        "CODECSPEEX5950"       => $this->_app->getParam('server_allow_codec_speex5950', 0),
        "CODECSPEEX8000"       => $this->_app->getParam('server_allow_codec_speex8000', 0),
        "CODECSPEEX11000"      => $this->_app->getParam('server_allow_codec_speex11000', 0),
        "CODECSPEEX15000"      => $this->_app->getParam('server_allow_codec_speex11000', 0),
        "CODECSPEEX18200"      => $this->_app->getParam('server_allow_codec_speex18200', 0),
        "CODECSPEEX24600"      => $this->_app->getParam('server_allow_codec_speex24600', 0),
        "serverwebpostposturl" => $this->_app->getParam('server_webpost_posturl', ''),
        "serverwebpostlinkurl" => $this->_app->getParam('server_webpost_linkurl', ''),
        "servertype"           => ($this->_app->getParam('server_clan_server', 0) ? 1 : 2),
      );
      
      $this->_ts2->sql_writeServerSettings($data, $server_id);
    }
    
    if($this->_ts2->sadmin_serverStatus($server_id) != $server_active)
    {
      if($server_active)
      {
        $this->_ts2->sadmin_serverStart($server_id);
      }
      else
      {
        $this->_ts2->sadmin_serverStop($server_id);
      }
    }
    
    if($this->_app->getParam('server_tsviewerid')) $this->_ts2->sql_setServerDescription($this->_app->getParam('server_tsviewerid'), $server_id);

    $this->_app->setLastEvent('The virtual server on port ' . $server_udpport . ' has been modified successfully.');
    
    $this->redirect('servers');
  }

  /**
   * Selects an other virtual server.
   * 
   * @return void
   */
  function do_selectAction()
  {
    $this->setNoRender();

    $udp = (int) $this->_app->getParam('port', 0);
    
    if($sid = $this->_ts2->sadmin_dbServerIdByPort($udp))
    {
      $this->_app->setSessionParam('server_id', $sid);
      $this->_app->setSessionParam('server_port', $udp);
    }
    
    $module = 'index';
    $action = 'index';
    $params = array();
    
    if($udp)
    {
      if($urlData = parse_url($this->_app->getEnvParam('HTTP_REFERER', null)))
      {
        parse_str($urlData['query'], $qryData);
        
        if(isset($qryData[$this->_app->getModuleKey()])) $module = $qryData[$this->_app->getModuleKey()];
        if(isset($qryData[$this->_app->getActionKey()])) $action = $qryData[$this->_app->getActionKey()];
        
        unset($qryData[$this->_app->getModuleKey()]);
        unset($qryData[$this->_app->getActionKey()]);
        
        $params = $qryData;
      }
    }

    if($udp)
    {
      $this->_app->setLastEvent('The virtual server on port ' . $udp . ' has been selected.');
    }
    else
    {
      $this->_app->setLastEvent('No virtual server is selected.');
    }
    
    $this->redirect($module, $action, $params);
  }
}
Return current item: TS2WebAdmin