<?php
/*****************************************************************/
// WHMSonic ClientExec Plugin v1.0 Support: admin@whmsonic.com
/*****************************************************************/
require_once 'modules/admin/models/ServerPlugin.php';
require_once 'newedge/classes/NE_Error.php';

class PluginWhmsonic extends ServerPlugin
{

    /*****************************************************************/
    // function getVariables - required function
    /*****************************************************************/

    function getVariables()
    {
        $variables = array (
                   /*T*/"Name"/*/T*/ => array (
                                        "type"=>"hidden",
                                        "description"=>"Used By CE to show plugin",
                                        "value"=>"WHMSonic"
                                       ),
                   /*T*/"Description"/*/T*/ => array (
                                        "type"=>"hidden",
                                        "description"=>"ShoutCast Admin Pro",
                                        "value"=>""
                                       ),
                   /*T*/"Username"/*/T*/ => array (
                                        "type"=>"text",
                                        "description"=>/*T*/"Root Username used to connect to server"/*/T*/,
                                        "value"=>"root"
                                       ),
                   /*T*/"Password"/*/T*/ => array (
                                        "type"=>"password",
                                        "description"=>/*T*/"Your root password used to connect to server"/*/T*/,
                                        "value"=>"",
                                        "encryptable"=>true
                                       ),
                   /*T*/"Actions"/*/T*/ => array (
                                        "type"=>"hidden",
                                        "description"=>/*T*/"Current actions that are active for this plugin per server"/*/T*/,
                                        "value"=>"Create,Delete,Suspend,Unsuspend"
                                       )
        );
        return $variables;
    }


    function create($args)
    {
        $username = str_replace(".", "", $args['DomainName']);
        
        // Usernames cannot begin with a number.
        $username = preg_replace("/^\d*/", '', $username);
        $username = mb_substr($username,0,5); // 5 characters we are using sc_ prefix.
        $username = str_replace('-','',$username); //make sure there are no - characters in the username
        $password = urlencode($args['DomainPassword']);
        $cemail = $args['UserEmail'];
        $cid = $args['CustomerID'];
        $sAuth = base64_encode($args['plugin_whmsonic_Username'].":".$args['plugin_whmsonic_Password']);
        $geturl = "GET /whmsonic/tools/clientexec.php?setup=yes&rad_username=sc_$username&limit=".$args['package_addons']['Listeners']."&ac_bitrate=".$args['package_addons']['Bitrate']."&ip=".$args['DomainIP']."&bw=".$args['package_addons']['Traffic']."&rad_dj=No&racc=yes&password=$password&cemail=$cemail&id=$cid";
        $sHTTP = "$geturl.&x=\r\n HTTP/1.0\r\nAuthorization: Basic $sAuth\r\n";
        $hSocket = fsockopen($args['DomainIP'], 2086, $errno, $errstr, 20);
        if (!$hSocket) {
            return new NE_Error("Socket Connection Failed ($errstr)", 200);
        } else {
            fputs($hSocket, $sHTTP);
            fputs( $hSocket, "\n" , 1);
            $output = "";
            while( !feof( $hSocket ) ) {
                $output .= fgets( $hSocket, 1024);
            }
            fclose( $hSocket);
        
            if (strpos($output,"download") == true) {
                return new NE_Error("Download Problem $output", 200);
            }
            if (strpos($output,"porterror") == true) {
                return new NE_Error("Port Error: Possible technical or firewall problem please contact us http://www.whmsonic.com/support/", 200);
            }
            if (strpos($output,"setupok") == true) {
               return "success";
            } else { 
                if ($debug_output) { 
                    return new NE_Error("Error on the shoutcast setup: <textarea cols=160 rows=50>$output</textarea>", 200);
                }
                return new NE_Error("Error on the shoutcast setup", 200);
            }
        }
    }

    function delete($args)
    {
        $sAuth = base64_encode($args['plugin_whmsonic_Username'].":".$args['plugin_whmsonic_Password']);
        $geturl = "GET /whmsonic/tools/clientexec.php?terminate=yes&id=".$args['CustomerID']."";
        $sHTTP = "$geturl.&x=\r\n HTTP/1.0\r\nAuthorization: Basic $sAuth\r\n";
        $hSocket = fsockopen($args['DomainIP'], 2086, $errno, $errstr, 20);
        if (!$hSocket) {
            return new NE_Error("Socket Connection Failed ($errstr)", 200);
        } else {
            fputs($hSocket, $sHTTP);
            fputs( $hSocket, "\n" , 1);
            $output = "";
            while( !feof( $hSocket ) ) {
                $output .= fgets( $hSocket, 1024);
            }
            fclose( $hSocket);
        }
    }
    
    function suspend($args)
    {
        $sAuth = base64_encode($args['plugin_whmsonic_Username'].":".$args['plugin_whmsonic_Password']);
        $geturl = "GET /whmsonic/tools/clientexec.php?suspend=yes&id=".$args['CustomerID']."";
        $sHTTP = "$geturl.&x=\r\n HTTP/1.0\r\nAuthorization: Basic $sAuth\r\n";
        $hSocket = fsockopen($args['DomainIP'], 2086, $errno, $errstr, 20);
        if (!$hSocket) {
            return new NE_Error("Socket Connection Failed ($errstr)", 200);
        } else {
            fputs($hSocket, $sHTTP);
            fputs( $hSocket, "\n" , 1);
            $output = "";
            while( !feof( $hSocket ) ) {
                $output .= fgets( $hSocket, 1024);
            }
            fclose( $hSocket);
        }
    }


    function unsuspend($args){
        $sAuth = base64_encode($args['plugin_whmsonic_Username'].":".$args['plugin_whmsonic_Password']);
        $geturl = "GET /whmsonic/tools/clientexec.php?unsuspend=yes&id=".$args['CustomerID']."";
        $sHTTP = "$geturl.&x=\r\n HTTP/1.0\r\nAuthorization: Basic $sAuth\r\n";
        $hSocket = fsockopen($args['DomainIP'], 2086, $errno, $errstr, 20);
        if (!$hSocket) {
            return new NE_Error("Socket Connection Failed ($errstr)", 200);
        } else {
            fputs($hSocket, $sHTTP);
            fputs( $hSocket, "\n" , 1);
            $output = "";
            while( !feof( $hSocket ) ) {
                $output .= fgets( $hSocket, 1024);
            }
            fclose( $hSocket);
        }
    }

    function update($args)
    {
        // WHMSonic doesn't support this.
    }

}
?>
