<?php
require_once 'classes/ServerPlugin.php';
require_once 'classes/domain.php';
require_once 'classes/user.php';
require_once 'newedge/classes/NE_MailGateway.php';
require_once 'newedge/classes/NE_Network.php';

/**
* @package Plugins
*/
class PluginHypervm extends ServerPlugin
{
    function getVariables()
    {
        /* Specification
              itemkey     - used to identify variable in your other functions
              type        - text,textarea,yesno,password,hidden ( type hidden are variables used by CE and are required )
              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"=>"Hypervm"
                                       ),
                   /*T*/"Description"/*/T*/ => array (
                                        "type"=>"hidden",
                                        "description"=>/*T*/"Description viewable by admin in server settings"/*/T*/,
                                        "value"=>/*T*/"HyperVM control panel integration"/*/T*/
                                       ),
                    /*T*/"HyperVM API url"/*/T*/ => array (
                                    "type"            =>    "text",
                                    "description"     =>    /*T*/"Example http://cp.yourdomain.com:8888/webcommand.php"/*/T*/,
                                    "value"           =>    ""
                            ),
                   /*T*/"Username"/*/T*/ => array (
                                        "type"=>"text",
                                        "description"=>/*T*/"Username used to connect to HyperVM"/*/T*/,
                                        "value"=>""
                                       ),
                   /*T*/"Password"/*/T*/ => array (
                                        "type"=>"password",
                                        "description"=>/*T*/"Password used to connect to HyperVM"/*/T*/,
                                        'value'         => '',
                                        'encryptable'   => true,
                                       ),
                   /*T*/"Failure E-mail"/*/T*/ => array (
                                        "type"=>"text",
                                        "description"=>/*T*/"E-mail address HyperVM error messages will be sent to"/*/T*/,
                                        "value"=>""
                                        ),
                    /*T*/"VM Type"/*/T*/ => array(
                                    "type"            =>    "text",
                                    "description"     => /*T*/"Enter the type of VM this server creates.  (openvz or xen)"/*/T*/,
                                    "value"           => "openvz"
                            ),
                    /*T*/"VM Name Custom Field"/*/T*/ => array(
                                    "type"            =>    "text",
                                    "description"     => /*T*/"Enter the name of the package custom field that will hold the VM name for HyperVM."/*/T*/,
                                    "value"           => ""
                            ),
                    /*T*/"VM Password Custom Field"/*/T*/ => array(
                                    "type"            => "text",
                                    "description"     => /*T*/"Enter the name of the package custom field that will hold the VM password"/*/T*/,
                                    "value"           => ""
                            ),
                    /*T*/"Number Of Ips Custom Field"/*/T*/ => array(
                                    "type"            =>    "text",
                                    "description"     => /*T*/"Enter the name of the package custom field that will hold the Number Of Ips for HyperVM."/*/T*/,
                                    "value"           => ""
                            ),
                    /*T*/"Hostname Custom Field"/*/T*/ => array(
                                    "type"            =>    "text",
                                    "description"     => /*T*/"Enter the name of the package custom field that will hold the Hostname for HyperVM."/*/T*/,
                                    "value"           => ""
                            ),
                    /*T*/"Ostemplate Custom Field"/*/T*/ => array(
                                    "type"            =>    "text",
                                    "description"     => /*T*/"Enter the name of the package custom field that will hold the Ostemplate for HyperVM."/*/T*/,
                                    "value"           => ""
                            ),
                   /*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) {
        $args = $this->set_all_args($args);
        $newVPS = array();

        $newVPS['login-class'] = "client";
        $newVPS['action'] = "add";
        $newVPS['class'] = "vps";
        $newVPS['v-type'] = trim($args['plugin_hypervm_VM_Type']);
        
        if ($newVPS['v-type'] != 'openvz' && $newVPS['v-type'] != 'xen') {
            $this->logger->log(4, "HyperVM Create Fail: VM type must be one of openvz or xen.");
            return new NE_Error($this->user->lang("HyperVM Create Fail: VM type must be one of openvz or xen.", 200));
        }

        if(isset($args['vmname']) && ($args['vmname'] != "")) {
             $newVPS['name'] = $args['vmname'];
        }
        if(isset($args['vmnumofips']) && ($args['vmnumofips'] != "")) {
             $newVPS['v-num_ipaddress_f'] = $args['vmnumofips'];
        }
        if(isset($args['UserEmail']) && ($args['UserEmail'] != "")) {
             $newVPS['v-contactemail'] = $args['UserEmail'];
        }

        $newVPS['v-send_welcome_f'] = "on";

        if(isset($args['vmpassword']) && ($args['vmpassword'] != "")) {
             $newVPS['v-password'] = $args['vmpassword'];
        }
        if(isset($args['vmostemplate']) && ($args['vmostemplate'] != "")) {
             $newVPS['v-ostemplate'] = $args['vmostemplate'];
        }
        if(isset($args['DomainIP']) && ($args['DomainIP'] != "")) {
             $newVPS['v-syncserver'] = $args['DomainIP'];
        }
        if(isset($args['PackageNameOnServer']) && ($args['PackageNameOnServer'] != "")) {
             $newVPS['v-plan_name'] = $args['PackageNameOnServer'];
        }
        if(isset($args['vmhostname']) && ($args['vmhostname'] != "")) {
             $newVPS['v-hostname'] = $args['vmhostname'];
        }

        $create = $this->sendtohypervm($args,$newVPS);
        if (is_a($create, 'NE_Error')) {
            $error = true;
        } else {
            $this->logger->log(4, "HyperVm:: Returned" . print_r($create,true));
        }

    }

    // HyperVM does not support this currently.
    function update($args) {
        $args = $this->set_all_args($args);
        $this->logger->log(4, "HyperVm:: Args" . print_r($args,true));
        $updateVPS = array();
        $updateVPS['login-class'] = "client";
        $updateVPS['class'] = "vps";
        $updateVPS['name'] = $args['vmname'].".vm";
        $updateVPS['action'] = "update";
        $updateVPS['subaction'] = "change_plan";
        $updateVPS['v-resourceplan_name'] = $args['PackageNameOnServer'];
        $update = $this->sendtohypervm($args,$updateVPS);
        if (is_a($update, 'NE_Error')) {
            $errormsg = "HyperVM Update Account Failure for VPS {$args['vmname']}.vm";
            return $errormsg;
        } else {
            $this->logger->log(4, "HyperVm:: Returned" . print_r($update,true));
        }
    }

    function suspend($args){

        $args = $this->set_all_args($args);
        $suspendVPS = array();
        $suspendVPS['login-class'] = "client";
        $suspendVPS['class'] = "vps";
        $suspendVPS['name'] = $args['vmname'].".vm";
        $suspendVPS['action'] = "update";
        $suspendVPS['subaction'] = "disable";
        $suspend = $this->sendtohypervm($args,$suspendVPS);
        if (is_a($suspend, 'NE_Error')) {
            $errormsg = "HyperVM Suspend Account Failure for VPS {$args['vmname']}.vm";
            return $errormsg;
        } else {
            $this->logger->log(4, "HyperVm:: Returned" . print_r($suspend,true));
        }
    }

    function unsuspend($args){ 

        $args = $this->set_all_args($args);
        $suspendVPS = array();
        $suspendVPS['login-class'] = "client";
        $suspendVPS['class'] = "vps";
        $suspendVPS['name'] = $args['vmname'].".vm";
        $suspendVPS['action'] = "update";
        $suspendVPS['subaction'] = "enable";
        $suspend = $this->sendtohypervm($args,$suspendVPS);
        if (is_a($suspend, 'NE_Error')) {
            $errormsg = "HyperVM Unsuspend Account Failure for VPS {$args['vmname']}.vm";
            return $errormsg;
        } else {
            $this->logger->log(4, "HyperVm:: Returned" . print_r($suspend,true));
        }
    }

    function delete($args){
        $args = $this->set_all_args($args);
        $suspendVPS = array();
        $suspendVPS['login-class'] = "client";
        $suspendVPS['class'] = "vps";
        $suspendVPS['name'] = $args['vmname'].".vm";
        $suspendVPS['action'] = "delete";
        $suspend = $this->sendtohypervm($args,$suspendVPS);
        if (is_a($suspend, 'NE_Error')) {
            $errormsg = "HyperVM Delete Account Failure for VPS {$args['vmname']}.vm";
            return $errormsg;
        } else {
            $this->logger->log(4, "HyperVm:: Returned" . print_r($suspend,true));
        }
    }

    function createUserName($args){
    }

    function set_all_args($args) {

        $package = new Domain($args['PackageID'], $this->user);
        $package->PopulateCustomFields();
        $vmname = "";
        $vmpass = "";
        $noofip = "";
        $vmhostname = "";
        $ostemplate = "";

        $package->getCustomFieldsValue($args['plugin_hypervm_VM_Name_Custom_Field'], $vmname);
        $package->getCustomFieldsValue($args['plugin_hypervm_VM_Password_Custom_Field'], $vmpass);
        $package->getCustomFieldsValue($args['plugin_hypervm_Number_Of_Ips_Custom_Field'], $noofip);
        $package->getCustomFieldsValue($args['plugin_hypervm_Hostname_Custom_Field'], $vmhostname);
        $package->getCustomFieldsValue($args['plugin_hypervm_Ostemplate_Custom_Field'], $ostemplate);

        $args['vmname'] = $vmname;
        $args['vmpassword'] = $vmpass;
        $args['vmnumofips'] = $noofip;
        $args['vmhostname'] = $vmhostname;
        $args['vmostemplate'] = $ostemplate;
        $args['package_class'] = $package;
        return $args;
    }

    function sendtohypervm($args,$values) {
        $post = 'login-name='.urlencode($args['plugin_hypervm_Username']).'&login-password='.urlencode($args['plugin_hypervm_Password']);
        foreach ($values as $key => $value) {
            $post .= "&$key=".urlencode(str_replace('\'','',$value));
        }
        $url = $args['plugin_hypervm_HyperVM_API_url'];
        $result = NE_Network::curlRequest($this->settings, $url, $post, false, true);
        return $result;
    }
}

?>
