<?php

// Please send all bugreports to support@ispsystem.com
// with "ClientExec integration plugin" mark.

require_once dirname(__FILE__).'/../../../classes/error_ce.php';
require_once dirname(__FILE__).'/helper.ispmanager.php';

/*****************************************************************/
// function plugin_ispmanager_variables - required function
/*****************************************************************/

function Plugin_ISPmanager_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"=>"ISPmanager"
                                   ),
               "Description" => array (
                                    "type"=>"hidden",
                                    "description"=>"Description visable by admin in server settings",
                                    "value"=>"ISPmanager Panel Integration"
                                   ),
               "Username" => array (
                                    "type"=>"text",
                                    "description"=>"Username used to connect to server",
                                    "value"=>""
                                   ),
               "Password" => array (
                                    "type"=>"password",
                                    "description"=>"Password used to connect to server",
                                    "value"=>""
                                   ),
               "Actions" => array (
                                    "type"=>"hidden",
                                    "description"=>"current actions that are active for this plugin_per server",
                                    "value"=>"Create,Delete,Update,Suspend,UnSuspend"
                                   ),
        'package_vars'  => array(
            'type'          => 'hidden',
            'description'   => 'Whether package settings are set',
            'value'         => '0',
                           ),
        'package_vars_values' => array(
            'type'          => 'hidden',
            'description'   => 'Hosting account parameters',
            'value'         => array(
                                    'ftplimit'       => array(
                                                           'type'           => 'text',
                                                           'description'    => 'Maximum number of ftp-users',
                                                           'value'          => '',
                                                        ),
                                    'maillimit'           => array(
                                                           'type'           => 'text',
                                                           'description'    => 'Maximum number of mail boxes',
                                                           'value'          => '1',
                                                        ),
                                    'domainlimit'    => array(
                                                           'type'           => 'text',
                                                           'description'    => 'Maximum number of domain name zones',
                                                           'value'          => '',
                                                        ),
                                    'disklimit'    => array(
                                                           'type'           => 'text',
                                                           'description'    => 'Disk space (in bytes, leave empty for unlimited)',
                                                           'value'          => '',
                                                        ),
                                    'webdomainlimit'   => array(
                                                           'type'           => 'text',
                                                           'description'    => 'Maximum number of web sites',
                                                           'value'          => '',
                                                        ),
                                    'maildomainlimit'        => array(
                                                           'type'           => 'text',
                                                           'description'    => 'Maximum number of mail domains',
                                                           'value'          => '',
                                                        ),
                                    'baselimit'        => array(
                                                           'type'           => 'text',
                                                           'description'    => 'Maximum number of databases',
                                                           'value'          => '',
                                                        ),
                                    'baseuserlimit'       => array(
                                                           'type'           => 'text',
                                                           'description'    => 'Maximum number of database users',
                                                           'value'          => '',
                                                        ),
                                    'bandwidthlimit'    => array(
                                                           'type'           => 'text',
                                                           'description'    => 'Traffic quota (in kbytes)',
                                                           'value'          => '',
                                                        ),
                                    'ssl'           => array(
                                                            'type'          => 'yesno',
                                                            'description'   => 'SSL support',
                                                            'value'         => '0',
                                                        ),
                                    'shell'         => array(
                                                            'type'          => 'yesno',
                                                            'description'   => 'System shell',
                                                            'value'         => '',
                                                        ),
                                    'phpmod'           => array(
                                                            'type'          => 'yesno',
                                                            'description'   => 'PHP as Apache Module support',
                                                            'value'         => '0',
                                                        ),
                                    'phpcgi'           => array(
                                                            'type'          => 'yesno',
                                                            'description'   => 'PHP as CGI support',
                                                            'value'         => '0',
                                                        ),
                                    'phpfcgi'           => array(
                                                            'type'          => 'yesno',
                                                            'description'   => 'PHP as FastCGI support',
                                                            'value'         => '0',
                                                        ),
                                    'ssi'           => array(
                                                            'type'          => 'yesno',
                                                            'description'   => 'SSI support',
                                                            'value'         => '0',
                                                        ),
                                    'cgi'           => array(
                                                            'type'          => 'yesno',
                                                            'description'   => 'CGI support',
                                                            'value'         => '0',
                                                        ),
                                ),
            ),
        'package_addons' => array(
            'type'          => 'hidden',
            'description'   => 'Supported signup addons variables',
            'value'         => array(
                'DISKSPACE', 'BANDWIDTH', 'EMAIL_ACCOUNTS', 'EMAIL_QUOTA', 'SSH_ACCESS', 'SSL'
            ),
        )

    );
    return $variables;


}

function ISP_Request($args, $req, $outtype = 'xml') {
    $ret = "";
    $url = "https://".$args['ServerHostName']."/manager/ispmgr?authinfo=".$args['plugin_ispmanager_Username'].":".$args['plugin_ispmanager_Password']."&out=".$outtype."&".$req;
    $ret = curlRequest($url, '', '', true, false);
    return $ret;
}

function ISP_Restart($args) {
    $res = false;
    $result = ISP_Request($args, "func=restart", "text");
    if ($result == "OK ")
	$res = true;
    return $res;    
}

function ISP_Check_Preset($args, $preset) {
    $preset_found = false;
    $ret = ISP_Request($args, "func=preset", "xml");
    $objXML = new xml2Array();
    $arrOutput = $objXML->parse($ret);
    if (isset($arrOutput[0]['children'])) {
    foreach ($arrOutput[0]['children'] as $el) {
	foreach ($el['children'] as $elem) {
            if ($elem['name'] == "NAME" && $elem['tagData'] == $preset) $preset_found = true;
    //        echo print_r($elem);
        }
    }
    }
    // Making Preset if not exists
    if (!$preset_found) $preset_found = ISP_Create_Preset($args, $preset);

    return $preset_found;
}

function ISP_Create_Preset($args, $name) {
    $ssi = "off"; $ssl = "off"; $shell = "off"; $cgi = "off";
    $phpmod = "off"; $phpcgi="off"; $phpfcgi="off"; $ptype = "user";
    $pv = $args['package_vars'];
    $res = false;
    if (isset($pv['ssi']) && $pv['ssi'] == 1) $ssi = "on";
    if (isset($pv['ssl']) && $pv['ssl'] == 1) $ssl = "on";
    if (isset($pv['cgi']) && $pv['cgi'] == 1) $cgi = "on";
    if (isset($pv['shell']) && $pv['shell'] == 1) $shell = "on";
    if (isset($pv['phpcgi']) && $pv['phpcgi'] == 1) $phpcgi = "on";
    if (isset($pv['phpmod']) && $pv['phpmod'] == 1) $phpmod = "on";
    if (isset($pv['phpfcgi']) && $pv['phpfcgi'] == 1) $phpfcgi = "on";
    $q = "name=".$name."&ptype=".$ptype."&disklimit=".$pv['disklimit']."&ftplimit=".$pv['ftplimit']."&maillimit=".$pv['maillimit']."&domainlimit=".$pv['domainlimit']."&webdomainlimit=".$pv['webdomainlimit']."&maildomainlimit=".$pv['maildomainlimit']."&baselimit=".$pv['baselimit']."&baseuserlimit=".$pv['baseuserlimit']."&bandwidthlimit=".$pv['bandwidthlimit']."&ssi=".$ssi."&ssl=".$ssl."&shell=".$shell."&cgi=".$cgi."&phpfcgi=".$phpfcgi."&phpcgi=".$phpcgi."&phpmod=".$phpmod."&func=preset.edit&elid=&sok=ok&suok=++++Ok++++";
    $result = ISP_Request($args, $q, "text");
    if ($result == "OK ") { 
	$res = true;
    } else {
	return new Error_CE(201, "ISPmanager can't create Preset");
    }
    return $res;
}

function Plugin_ISPmanager_CreateUsername($args) {
    return $args['DomainUsername'];
}
	
//plugin function called after new account is activated ( approved )
function Plugin_ISPmanager_Create($args){
    if ( ISP_Check_Preset($args, $args['PackageName']) ) {
	$ssi = "off"; $ssl = "off"; $shell = "off"; $cgi = "off";
        $phpmod = "off"; $phpcgi="off"; $phpfcgi="off"; $ptype = "user";
        $pv = $args['package_vars'];
        if (isset($pv['ssi']) && $pv['ssi'] == 1) $ssi = "on";
        if (isset($pv['ssl']) && $pv['ssl'] == 1) $ssl = "on";
        if (isset($pv['cgi']) && $pv['cgi'] == 1) $cgi = "on";
        if (isset($pv['shell']) && $pv['shell'] == 1) $shell = "on";
        if (isset($pv['phpcgi']) && $pv['phpcgi'] == 1) $phpcgi = "on";
        if (isset($pv['phpmod']) && $pv['phpmod'] == 1) $phpmod = "on";
        if (isset($pv['phpfcgi']) && $pv['phpfcgi'] == 1) $phpfcgi = "on";
        $q = "name=".$args['PassedUserName']."&passwd=".$args['DomainPassword']."&confirm=".$args['DomainPassword']."&ptype=".$ptype."&domain=".$args['DomainName']."&ip=".$args['DomainIP']."&preset=".$args['PackageName']."&disklimit=".$pv['disklimit']."&ftplimit=".$pv['ftplimit']."&maillimit=".$pv['maillimit']."&domainlimit=".$pv['domainlimit']."&webdomainlimit=".$pv['webdomainlimit']."&maildomainlimit=".$pv['maildomainlimit']."&baselimit=".$pv['baselimit']."&baseuserlimit=".$pv['baseuserlimit']."&bandwidthlimit=".$pv['bandwidthlimit']."&ssi=".$ssi."&ssl=".$ssl."&shell=".$shell."&cgi=".$cgi."&phpfcgi=".$phpfcgi."&phpcgi=".$phpcgi."&phpmod=".$phpmod."&func=user.edit&elid=&sok=ok&suok=++++Ok++++";
        $result = ISP_Request($args, $q, "xml");
        $objXML = new xml2Array();
        $arrOutput = $objXML->parse($result);
	foreach ($arrOutput[0]['children'] as $el) {
	    if ($el['name'] == 'ERROR')
    		return new Error_CE(201, "ISPManager error #".$el['attrs']['CODE']." in object \"".$el['attrs']['OBJ']."\"");
	    if ($el['name'] == 'OK') {
		if ($el['tagData'] == 'restart') return ISP_Restart($args);
	    }
	}
    }
    return false;
}

function Plugin_ISPmanager_Delete($args){
    $result = ISP_Request($args, "func=user.delete&elid=".$args['DomainUsername'], "xml");
    $objXML = new xml2Array();
    $arrOutput = $objXML->parse($result);
    foreach ($arrOutput[0]['children'] as $el) {
	if ($el['name'] == 'OK') {
	    if ($el['tagData'] == 'restart') return ISP_Restart($args);
	}
    }
    return false;
}

function Plugin_ISPmanager_Update($args){
if ( ISP_Check_Preset($args, $args['CHANGE_PACKAGE']) ) {
    $ssi = "off"; $ssl = "off"; $shell = "off"; $cgi = "off";
    $phpmod = "off"; $phpcgi="off"; $phpfcgi="off"; $ptype = "user";
    $pv = $args['NewPackageVars'];
    if (isset($pv['ssi']) && $pv['ssi'] == 1) $ssi = "on";
    if (isset($pv['ssl']) && $pv['ssl'] == 1) $ssl = "on";
    if (isset($pv['cgi']) && $pv['cgi'] == 1) $cgi = "on";
    if (isset($pv['shell']) && $pv['shell'] == 1) $shell = "on";
    if (isset($pv['phpcgi']) && $pv['phpcgi'] == 1) $phpcgi = "on";
    if (isset($pv['phpmod']) && $pv['phpmod'] == 1) $phpmod = "on";
    if (isset($pv['phpfcgi']) && $pv['phpfcgi'] == 1) $phpfcgi = "on";
    $q = "name=".$args['DomainUsername']."&passwd=".$args['CHANGE_PASSWORD']."&confirm=".$args['CHANGE_PASSWORD']."&ptype=".$ptype."&ip=".$args['DomainIP']."&preset=".$args['CHANGE_PACKAGE']."&disklimit=".$pv['disklimit']."&ftplimit=".$pv['ftplimit']."&maillimit=".$pv['maillimit']."&domainlimit=".$pv['domainlimit']."&webdomainlimit=".$pv['webdomainlimit']."&maildomainlimit=".$pv['maildomainlimit']."&baselimit=".$pv['baselimit']."&baseuserlimit=".$pv['baseuserlimit']."&bandwidthlimit=".$pv['bandwidthlimit']."&ssi=".$ssi."&ssl=".$ssl."&shell=".$shell."&cgi=".$cgi."&phpfcgi=".$phpfcgi."&phpcgi=".$phpcgi."&phpmod=".$phpmod."&func=user.edit&elid=".$args['DomainUsername']."&sok=ok&suok=++++Ok++++";
    $result = ISP_Request($args, $q, "xml");
    $objXML = new xml2Array();
    $arrOutput = $objXML->parse($result);
    foreach ($arrOutput[0]['children'] as $el) {
        if ($el['name'] == 'ERROR')
    	    return new Error_CE(201, "ISPManager error #".$el['attrs']['CODE']." in object \"".$el['attrs']['OBJ']."\"");
        if ($el['name'] == 'OK') {
	    if (isset($el['tagData']) && $el['tagData'] == 'restart') return ISP_Restart($args);
        }
    }
}
return false;    
}

function Plugin_ISPmanager_Suspend($args){
    $result = ISP_Request($args, "func=user.disable&elid=".$args['DomainUsername'], "xml");
    $objXML = new xml2Array();
    $arrOutput = $objXML->parse($result);
    foreach ($arrOutput[0]['children'] as $el) {
	if ($el['name'] == 'OK') {
	    if ($el['tagData'] == 'restart') return ISP_Restart($args);
	}
    }
    return false;
}

function Plugin_ISPmanager_UnSuspend($args){
    $result = ISP_Request($args, "func=user.enable&elid=".$args['DomainUsername'], "xml");
    $objXML = new xml2Array();
    $arrOutput = $objXML->parse($result);
    foreach ($arrOutput[0]['children'] as $el) {
	if ($el['name'] == 'OK') {
	    if ($el['tagData'] == 'restart') return ISP_Restart($args);
	}
    }
    return false;
}

?>
