<?php

require_once 'newedge/classes/NE_Error.php';
require_once 'newedge/classes/NE_MailGateway.php';
require_once 'plugins/server/teamspeak/class.teamspeakserver.php';
require_once 'classes/domain.php';
require_once 'classes/ServerPlugin.php';
/**
* @package Plugins
*/
class PluginTeamspeak extends ServerPlugin
{
        
    /*****************************************************************/
    // function getVariables - required function
    /*****************************************************************/
    function getVariables(){
        /* Specification
              itemkey     - used to identify variable in your other functions
              type        - text,textarea,yesno,password
              description - description of the variable, displayed in ClientExec
              encryptable - used to indicate the variable's value must be encrypted in the database
        */
    
    
        $variables = array (
                   /*T*/"Name"/*/T*/ => array (
                                        "type"          => "hidden",
                                        "description"   => "Used By CE to show plugin - must match how you call the action function names",
                                        "value"         => "Teamspeak"
                                       ),
                   /*T*/"Description"/*/T*/ => array (
                                        "type"          => "hidden",
                                        "description"   => /*T*/"Description viewable by admin in server settings"/*/T*/,
                                        "value"         => /*T*/"Teamspeak voice server integration.  Note: The custom field settings are used to hold information about the clients server.  Please create these fields in admin->custom fields->packages first.  The package name on server fields for each package hold the slot count.  Suspending a server sets the slot count to 0."/*/T*/
                                       ),
                   /*T*/"Username"/*/T*/ => array (
                                        "type"          => "text",
                                        "description"   => /*T*/"Username used to connect to server"/*/T*/,
                                        "value"         => ""
                                       ),
                   /*T*/"Password"/*/T*/ => array (
                                        "type"          => "password",
                                        "description"   => /*T*/"Password used to connect to server"/*/T*/,
                                        "value"         => "",
                                        "encryptable"   => true
                                       ),
                   /*T*/"Starting Teamspeak Port Number"/*/T*/ => array(
                                        "type"          => "text",
                                        "description"   => /*T*/"Enter the starting teamspeak port number you'd like to use.  If the port is already in use it will use the next available port."/*/T*/,
                                        "value"         => "8767"
                                        ),
                   /*T*/"Client Port Custom Field"/*/T*/ => array(
                                        "type"          => "text",
                                        "description"   => /*T*/"Enter the name of the package custom field that will hold the client teamspeak port number."/*/T*/,
                                        "value"         => ""
                                        ),
                   /*T*/"Client Username Custom Field"/*/T*/ => array(
                                        "type"          => "text",
                                        "description"   => /*T*/"Enter the name of the package custom field that will hold the client teamspeak admin username."/*/T*/,
                                        "value"         => ""
                                        ),
                   /*T*/"Client Password Custom Field"/*/T*/ => array(
                                        "type"          => "text",
                                        "description"   => /*T*/"Enter the name of the package custom field that will hold the client teamspeak admin password"/*/T*/,
                                        "value"         => ""
                                        ),
                   /*T*/"Actions"/*/T*/ => array (
                                        "type"          => "hidden",
                                        "description"   => /*T*/"Current actions that are active for this plugin per server"/*/T*/,
                                        "value"         => "Create,Delete,Update,Suspend,Unsuspend"
                                       )
        );
        return $variables;
    }
    
    function createUserName($args)
    {
        return $args['DomainUsername'];
    }
    
    function create($args)
    {
        if (    $args['plugin_teamspeak_Create'] != 1                           || 
                $args['PackageNameOnServer'] == null                            ||
                $args['plugin_teamspeak_Client_Port_Custom_Field'] == ""        ||
                $args['plugin_teamspeak_Client_Username_Custom_Field'] == ""    ||
                $args['plugin_teamspeak_Client_Password_Custom_Field'] == ""
           ) return;
    	$user = $args['plugin_teamspeak_Username'];
    	$pass = $args['plugin_teamspeak_Password'];
    	$slotcount = $args['PackageNameOnServer'];
    	$package = new Domain($args['PackageID'], $this->user);
    	$package->PopulateCustomFields();
    
    	$port = "";
    	$clientuser = "";
    	$clientpass = "";
    	$package->getCustomFieldsValue($args['plugin_teamspeak_Client_Port_Custom_Field'], $port);
    	$package->getCustomFieldsValue($args['plugin_teamspeak_Client_Username_Custom_Field'], $clientuser);
    	$package->getCustomFieldsValue($args['plugin_teamspeak_Client_Password_Custom_Field'], $clientpass);
    
    	if ($clientuser == "") {
    	    $clientuser = "tsadmin";
    	    $package->UpdateCustomTag($args['plugin_teamspeak_Client_Username_Custom_Field'], $clientuser);
    	}
    	if ($clientpass == "") {
    	    // generate some random password
    	    for ($i = 0; $i < 8; $i++) {
    	        $rand = rand(1, 3);
                switch ($rand) {
                    case 1:
                        $clientpass .= chr(rand(48, 57)); // [0-9]
                        break;
                    case 2:
                        $clientpass .= chr(rand(97, 122)); // [a-z]
                        break;
                    case 3:
                        $clientpass .= chr(rand(65, 90)); // [A-Z]
                        break;
                    }
    	    }
    	    $package->UpdateCustomTag($args['plugin_teamspeak_Client_Password_Custom_Field'], $clientpass);
    	}
    
    
    
    	$tsServer = new TeamspeakServer(
                                       $args['ServerHostName'],
                                       $args['plugin_teamspeak_Username'],
                                       $args['plugin_teamspeak_Password']
    	                               );
    	$return = $tsServer->connect();
        if (is_a($return, 'NE_Error')) {
            return $return;
        }
    
        /* If a port is already defined then ensure it's available,
           otherwise find the next available port. */
    	if ($port != "") {
        	if (!$tsServer->checkPortAvailability($port)) {
        	    return new NE_Error('Port '. $port.' is not available.', 90);
        	}
    	} else {
    	    $portList = $tsServer->getPortList();
    	    $currentPort = $args['plugin_teamspeak_Starting_Teamspeak_Port_Number'];
    	    while (true) {
    	        if (!in_array($currentPort, $portList)) {
    	            $port = $currentPort;
    	            break;
    	        }
    	        $currentPort++;
    	    }
    	    $package->UpdateCustomTag($args['plugin_teamspeak_Client_Port_Custom_Field'], $port);
    	}
        $return = $tsServer->add(
                        $port,
                        $clientuser,
                        $clientpass,
                        $args['PackageNameOnServer']
                      );
        return $return;
    }
    
    function delete($args)
    {
        if (    $args['plugin_teamspeak_Delete'] != 1                           || 
                $args['PackageNameOnServer'] == null                            ||
                $args['plugin_teamspeak_Client_Port_Custom_Field'] == ""        ||
                $args['plugin_teamspeak_Client_Username_Custom_Field'] == ""    ||
                $args['plugin_teamspeak_Client_Password_Custom_Field'] == ""
           ) return;
    
    	$user = $args['plugin_teamspeak_Username'];
    	$pass = $args['plugin_teamspeak_Password'];
    	$package = new Domain($args['PackageID'], $this->user);
    	$package->PopulateCustomFields();
    
    	$port = "";
    	$package->getCustomFieldsValue($args['plugin_teamspeak_Client_Port_Custom_Field'], $port);
    	if ($port == "") return;
    
        $tsServer = new TeamspeakServer(
                                       $args['ServerHostName'],
                                       $args['plugin_teamspeak_Username'],
                                       $args['plugin_teamspeak_Password']
    	                               );
        $return = $tsServer->connect();
        if (is_a($return, 'NE_Error')) {
            return $return;
        }
    
        return $tsServer->delete($port);
    }
    
    function update($args)
    {
        if (    $args['plugin_teamspeak_Update'] != 1                           || 
                $args['PackageNameOnServer'] == null                            ||
                $args['plugin_teamspeak_Client_Port_Custom_Field'] == ""        ||
                $args['plugin_teamspeak_Client_Username_Custom_Field'] == ""    ||
                $args['plugin_teamspeak_Client_Password_Custom_Field'] == ""
           ) return;
        $user = $args['plugin_teamspeak_Username'];
    	$pass = $args['plugin_teamspeak_Password'];
    	$slotcount = $args['PackageNameOnServer'];
    	$package = new Domain($args['PackageID'], $this->user);
    	$package->PopulateCustomFields();
    
    	$port = "";
    	$package->getCustomFieldsValue($args['plugin_teamspeak_Client_Port_Custom_Field'], $port);
    	if ($port == "") return;
    
    	if (isset($args['CHANGE_PACKAGE'])) {
        	$tsServer = new TeamspeakServer(
                                           $args['ServerHostName'],
                                           $args['plugin_teamspeak_Username'],
                                           $args['plugin_teamspeak_Password']
        	                               );
            $return = $tsServer->connect();
            if (is_a($return, 'NE_Error')) {
                return $return;
            }
            return $tsServer->update($port, $args['PackageNameOnServer']);
       }
    }
    
    function suspend($args) {
        if (    $args['plugin_teamspeak_Suspend'] != 1                          || 
                $args['PackageNameOnServer'] == null                            ||
                $args['plugin_teamspeak_Client_Port_Custom_Field'] == ""        ||
                $args['plugin_teamspeak_Client_Username_Custom_Field'] == ""    ||
                $args['plugin_teamspeak_Client_Password_Custom_Field'] == ""
           ) return;
        $user = $args['plugin_teamspeak_Username'];
    	$pass = $args['plugin_teamspeak_Password'];
    	$package = new Domain($args['PackageID'], $this->user);
    	$package->PopulateCustomFields();
    
    	$port = "";
    	$package->getCustomFieldsValue($args['plugin_teamspeak_Client_Port_Custom_Field'], $port);
    	if ($port == "") return;
    	$tsServer = new TeamspeakServer(
                                       $args['ServerHostName'],
                                       $args['plugin_teamspeak_Username'],
                                       $args['plugin_teamspeak_Password']
    	                               );
        $return = $tsServer->connect();
        if (is_a($return, 'NE_Error')) {
            return $return;
        }
        return $tsServer->update($port, 0);
    }
    
    function unsuspend($args) {
        if (    $args['plugin_teamspeak_Unsuspend'] != 1                        || 
                $args['PackageNameOnServer'] == null                            ||
                $args['plugin_teamspeak_Client_Port_Custom_Field'] == ""        ||
                $args['plugin_teamspeak_Client_Username_Custom_Field'] == ""    ||
                $args['plugin_teamspeak_Client_Password_Custom_Field'] == ""
           ) return;
        $user = $args['plugin_teamspeak_Username'];
    	$pass = $args['plugin_teamspeak_Password'];
    	$package = new Domain($args['PackageID'], $this->user);
    	$package->PopulateCustomFields();
    
    	$port = "";
    	$package->getCustomFieldsValue($args['plugin_teamspeak_Client_Port_Custom_Field'], $port);
    	if ($port == "") return;
    	$tsServer = new TeamspeakServer(
                                       $args['ServerHostName'],
                                       $args['plugin_teamspeak_Username'],
                                       $args['plugin_teamspeak_Password']
    	                               );
        $return = $tsServer->connect();
        if (is_a($return, 'NE_Error')) {
            return $return;
        }
        return $tsServer->update($port, $args['PackageNameOnServer']);
    }
}
?>
