<?php

require_once dirname(__FILE__).'/../../../classes/error_ce.php';
require_once dirname(__FILE__).'/../../../common/mailfunctions.php';
require_once dirname(__FILE__).'/class.teamspeakserver.php';
require_once dirname(__FILE__).'/../../../classes/domain.php';

/*****************************************************************/
// function plugin_teamspeak_variables - required function
/*****************************************************************/
function plugin_teamspeak_variables($args){
    /* Specification
          itemkey     - used to identify variable in your other functions
          type        - text,textarea,yesno,password
          description - description of the variable, displayed in ClientExec
    */


    $variables = array (
               "Name" => array (
                                    "type"          => "hidden",
                                    "description"   => "Used By CE to show plugin - must match how you call the action function names",
                                    "value"         => "Teamspeak"
                                   ),
               "Description" => array (
                                    "type"          => "hidden",
                                    "description"   => "Description visable by admin in server settings",
                                    "value"         => "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."
                                   ),
               "Username" => array (
                                    "type"          => "text",
                                    "description"   => "Username used to connect to server",
                                    "value"         => ""
                                   ),
               "Password" => array (
                                    "type"          => "password",
                                    "description"   => "Password used to connect to server",
                                    "value"         => ""
                                   ),
               "Starting Teamspeak Port Number" => array(
                                    "type"          => "text",
                                    "description"   => "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.",
                                    "value"         => "8767"
                                    ),
               "Client Port Custom Field" => array(
                                    "type"          => "text",
                                    "description"   => "Enter the name of the package custom field that will hold the client teamspeak port number.",
                                    "value"         => ""
                                    ),
               "Client Username Custom Field" => array(
                                    "type"          => "text",
                                    "description"   => "Enter the name of the package custom field that will hold the client teamspeak admin username.",
                                    "value"         => ""
                                    ),
               "Client Password Custom Field" => array(
                                    "type"          => "text",
                                    "description"   => "Enter the name of the package custom field that will hold the client teamspeak admin password",
                                    "value"         => ""
                                    ),
               "Actions" => array (
                                    "type"=>"hidden",
                                    "description"=>"current actions that are active for this plugin_per server",
                                    "value"=>"Create,Delete,Update,Suspend,Unsuspend"
                                   )
    );
    return $variables;
}

function plugin_teamspeak_CreateUsername($args)
{
    return $args['DomainUsername'];
}

function plugin_teamspeak_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']);
	$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']
	                               );
    if (!($return = $tsServer->connect())) {
        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 Error_CE(90, 'Port '. $port.' is not available.');
    	}
	} 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 plugin_teamspeak_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']);
	$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']
	                               );
    if (!($return = $tsServer->connect())) {
        return $return;
    }

    return $tsServer->delete($port);
}

function plugin_teamspeak_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']);
	$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']
    	                               );
        if (!($return = $tsServer->connect())) {
            return $return;
        }
        return $tsServer->update($port, $args['PackageNameOnServer']);
   }
}

function plugin_teamspeak_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']);
	$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']
	                               );
    if (!($return = $tsServer->connect())) {
        return $return;
    }
    return $tsServer->update($port, 0);
}

function plugin_teamspeak_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']);
	$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']
	                               );
    if (!($return = $tsServer->connect())) {
        return $return;
    }
    return $tsServer->update($port, $args['PackageNameOnServer']);
}
?>
