<?php

require_once dirname(__FILE__).'/class.pleskserver.php';

function plugin_plesk_variables($args)
{
    $variables = array(
        'Name'          => array(
            'type'          => 'hidden',
            'description'   => 'Used By CE to show plugin - must match how you call the action function names',
            'value'         => 'Plesk',
                           ),
        'Description'   => array(
            'type'          => 'hidden',
            'description'   => 'Description viewable by admin in server settings',
            'value'         => 'Plesk control panel integration',
                           ),
        'Username'      => array(
            'type'          => 'text',
            'description'   => 'If you\'ll provide reseller accounts, enter here the Plesk administrator credentials. Otherwise just enter a regular user\'s credentials, but be aware that such user has to have \'Domain creation\', \'Physical hosting management\', \'Hard disk quota assignment\', \'Domain limits adjustment\'  and \'Ability to use remote XML interface\' permissions. This user will also need to have all its personal data configured in Plesk (E-mail, phone, etc), even those fields that are not marked as required.',
            'value'         => '',
                           ),
        'Password'      => array(
            'type'          => 'text',
            'value'         => '',
            'description'   => 'If you\'ll provide reseller accounts, enter here the Plesk administrator credentials. Otherwise just enter a regular user\'s credentials, but be aware that such user has to have \'Domain creation\', \'Physical hosting management\', \'Hard disk quota assignment\', \'Domain limits adjustment\'  and \'Ability to use remote XML interface\' permissions. This user will also need to have all its personal data configured in Plesk (E-mail, phone, etc), even those fields that are not marked as required.',
                           ),
        'Non-Admin Username' => array(
            'type'          => 'text',
            'description'   => 'If you\'ll provide both reseller and non-reseller accounts, use the Plesk administrator account to create a user under whom non-reseller accounts (domains) will be created and enter its credentials here. Otherwise leave this field empty. This user will also need to have all its personal data configured in Plesk (E-mail, phone, etc), even those fields that are not marked as required.',
            'value'         => '',
                           ),
        'Non-Admin Password' => array(
            'type'          => 'text',
            'description'   => 'If you\'ll provide both reseller and non-reseller accounts, use the Plesk administrator account to create a user under whom non-reseller accounts (domains) will be created and enter its credentials here. Otherwise leave this field empty. This user will also need to have all its personal data configured in Plesk (E-mail, phone, etc), even those fields that are not marked as required.',
            '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(
                                    'reseller_account' => array(
                                                            'type'          => 'yesno',
                                                            'description'   => 'Reseller Account (you need Plesk Administrator access):<br>NOTE: The username for non-reseller accounts is the domain name',
                                                            'value'         => '0',
                                                       ),
                                    'max_dom'       => array(
                                                           'type'           => 'text',
                                                           'description'    => 'Maximum number of domains (for resellers only, leave empty for unlimited)',
                                                           'value'          => '',
                                                        ),
                                    'www'           => array(
                                                           'type'           => 'yesno',
                                                           'description'    => 'Use www prefix',
                                                           'value'          => '1',
                                                        ),
                                    'max_subdom'    => array(
                                                           'type'           => 'text',
                                                           'description'    => 'Maximum number of subdomains (leave empty for unlimited)',
                                                           'value'          => '',
                                                        ),
                                    'disk_space'    => array(
                                                           'type'           => 'text',
                                                           'description'    => 'Disk space (in bytes, leave empty for unlimited)',
                                                           'value'          => '',
                                                        ),
                                    'max_traffic'   => array(
                                                           'type'           => 'text',
                                                           'description'    => 'Maximum amount of traffic (in bytes/month, leave empty for unlimited)',
                                                           'value'          => '',
                                                        ),
                                    'max_wu'        => array(
                                                           'type'           => 'text',
                                                           'description'    => 'Maximum number of web users (leave empty for unlimited)',
                                                           'value'          => '',
                                                        ),
                                    'max_db'        => array(
                                                           'type'           => 'text',
                                                           'description'    => 'Maximum number of databases (leave empty for unlimited)',
                                                           'value'          => '',
                                                        ),
                                    'max_box'       => array(
                                                           'type'           => 'text',
                                                           'description'    => 'Maximum number of mailboxes (leave empty for unlimited)',
                                                           'value'          => '',
                                                        ),
                                    'mbox_quota'    => array(
                                                           'type'           => 'text',
                                                           'description'    => 'Mailbox quota (in bytes, leave empty for unlimited)',
                                                           'value'          => '',
                                                        ),
                                    'max_redir'     => array(
                                                           'type'           => 'text',
                                                           'description'    => 'Maximum number of mail redirects (leave empty for unlimited)',
                                                           'value'          => '',
                                                        ),
                                    'max_mg'        => array(
                                                           'type'           => 'text',
                                                           'description'    => 'Maximum number of mail groups (leave empty for unlimited)',
                                                           'value'          => '',
                                                        ),
                                    'max_resp'      => array(
                                                           'type'           => 'text',
                                                           'description'    => 'Maximum number of mail autoresponders (leave empty for unlimited)',
                                                           'value'          => '',
                                                        ),
                                    'max_maillists' => array(
                                                           'type'           => 'text',
                                                           'description'    => 'Maximum number of mail lists (leave empty for unlimited)',
                                                           'value'          => '',
                                                        ),
                                    'max_webapps'   => array(
                                                           'type'           => 'text',
                                                           'description'    => 'Maximum number of Java applications (leave empty for unlimited)',
                                                           'value'          => '',
                                                        ),
                                    'ftp_quota'     => array(
                                                            'type'          => 'text',
                                                            'description'   => 'FTP quota (in bytes, leave empty for unlimited)',
                                                            'value'         => '',
                                                        ),
                                    'fp'            => array(
                                                            'type'          => 'yesno',
                                                            'description'   => 'FrontPage support',
                                                            'value'         => '0',
                                                        ),
                                    'fp_ssl'        => array(
                                                            'type'          => 'yesno',
                                                            'description'   => 'FrontPage over SSL support',
                                                            'value'         => '0',
                                                        ),
                                    'fp_auth'       => array(
                                                            'type'          => 'yesno',
                                                            'description'   => 'Allow FrontPage Authorization',
                                                            'value'         => '0',
                                                        ),
                                    'ssl'           => array(
                                                            'type'          => 'yesno',
                                                            'description'   => 'SSL support',
                                                            'value'         => '0',
                                                        ),
                                    'shell'         => array(
                                                            'type'          => 'yesno',
                                                            'description'   => 'System shell',
                                                            'value'         => '',
                                                        ),
                                    'php'           => array(
                                                            'type'          => 'yesno',
                                                            'description'   => 'PHP support',
                                                            'value'         => '0',
                                                        ),
                                    'ssi'           => array(
                                                            'type'          => 'yesno',
                                                            'description'   => 'SSI support',
                                                            'value'         => '0',
                                                        ),
                                    'cgi'           => array(
                                                            'type'          => 'yesno',
                                                            'description'   => 'CGI support',
                                                            'value'         => '0',
                                                        ),
                                    'mod_perl'      => array(
                                                            'type'          => 'yesno',
                                                            'description'   => 'mod_perl support',
                                                            'value'         => '0',
                                                        ),
                                    'mod_python'    => array(
                                                            'type'          => 'yesno',
                                                            'description'   => 'mod_python support',
                                                            'value'         => '0',
                                                        ),
                                    'asp'           => array(
                                                            'type'          => 'yesno',
                                                            'description'   => 'Apache ASP support',
                                                            'value'         => '0',
                                                        ),
                                    'asp_dot_net'   => array(
                                                            'type'          => 'yesno',
                                                            'description'   => 'ASP.NET support',
                                                            'value'         => '0',
                                                        ),
                                    'coldfusion'    => array(
                                                            'type'          => 'yesno',
                                                            'description'   => 'Coldfusion support',
                                                            'value'         => '0',
                                                        ),
                                    'webstat'       => array(
                                                            'type'          => 'text',
                                                            'description'   => 'Webserver statistics support (awstats, webalizer, smarterstats, urchin, none)',
                                                            'value'         => 'none',
                                                        ),
                                    'errdocs'       => array(
                                                            'type'          => 'yesno',
                                                            'description'   => 'Custom error documents support',
                                                            'value'         => '0',
                                                        ),
                                    'at_domains'    => array(
                                                            'type'          => 'yesno',
                                                            'description'   => '@-domains 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 plugin_plesk_CreateUsername($args)
{
    // Minimum needed RPC elements: pname, login, passwd
    return $args['DomainUsername'];
}

function plugin_plesk_Create($args, $server = false)
{
    // package add-ons handling
    if (isset($args['package_addons']['DISKSPACE'])) {
        $args['package_vars']['disk_space'] += ((int)$args['package_addons']['DISKSPACE']) * 1048576; // 1 Meg in bytes
    }
    if (isset($args['package_addons']['BANDWIDTH'])) {
        $args['package_vars']['max_traffic'] += ((int)$args['package_addons']['BANDWIDTH']) * 1073741824; // 1 Gig in bytes
    }
    if (isset($args['package_addons']['EMAIL_ACCOUNTS'])) {
        $args['package_vars']['max_box'] += ((int)$args['package_addons']['EMAIL_ACCOUNTS']);
    }
    if (isset($args['package_addons']['EMAIL_QUOTA'])) {
        $args['package_vars']['mbox_quota'] += ((int)$args['package_addons']['EMAIL_QUOTA']) * 1048576;
    }
    if (isset($args['package_addons']['SSH_ACCESS']) && $args['package_addons']['SSH_ACCESS'] == 1) {
        $args['package_vars']['shell'] = '/bin/bash';
    }
    if (isset($args['package_addons']['SSL']) && $args['package_addons']['SSL'] == 1) {
        $args['package_vars']['ssl'] = '1';
    }

    if (isset($args['package_vars']['reseller_account'])) {
        // use $server to test with mock object
        if (!$server) {
            $server = new PleskServer($args['ServerHostName'], $args['plugin_plesk_Username'], $args['plugin_plesk_Password']);
        }

        // create user in Plesk
        $userId = $server->addUser($args['DomainUsername'], $args['DomainPassword'], $args['CustomerID']);
        if (is_error($userId)) {
            return $userId;
        }
        $response = $server->addResellerPermissionsAndLimits($userId, @$args['package_vars']);
        if (is_error($response)) {
            return $response;
        }

        // add IP to user
        if (is_error($result = $server->addIpToUser($userId, $args['DomainIP']))) {
            return $result;
        }
    } else {
        if (!$server) {
            if ($args['plugin_plesk_Non-Admin_Username'] != '' && $args['plugin_plesk_Non-Admin_Password'] != '') {
                $server = new PleskServer($args['ServerHostName'], $args['plugin_plesk_Non-Admin_Username'], $args['plugin_plesk_Non-Admin_Password']);
            } else {
                $server = new PleskServer($args['ServerHostName'], $args['plugin_plesk_Username'], $args['plugin_plesk_Password']);
            }
        }
        $userId = false;
    }

    // add domain to user
    $domainId = $server->addDomainToUser(    $userId,
                                             $args['DomainUsername'],
                                             $args['DomainPassword'],
                                             $args['DomainName'],
                                             $args['DomainIP'],
                                             @$args['package_vars']);

    // Ignore error 2307: always raises even if operation was successful.
    if (is_error($domainId) && $domainId->getErrCode() != 2307) {
        return new Error_CE(90, 'There was an error creating the domain. '.$domainId->getMessage());
    }

    return array('userId' => $userId, 'domainId' => $domainId);

}

// use $server to test with mock object
function plugin_plesk_Update($args, $server = false)
{
    $userName       = $args['DomainUsername'];
    $password       = $args['DomainPassword'];
    $ip             = $args['DomainIP'];
    $packageVars    = false;

    foreach ($args as $change => $newValue) {
        switch($change) {
            case 'CHANGE_USERNAME':
                $userName = $args['CHANGE_USERNAME'];
                break;
            case 'CHANGE_PASSWORD':
                $password = $args['CHANGE_PASSWORD'];
                break;
            case 'CHANGE_PACKAGE':
                $packageVars = $args['NewPackageVars'];
                break;
            case 'CHANGE_IP':
                // this actually does nothing since plesk won't let change a domains IP
                $ip = $args['CHANGE_IP'];
                break;
        }
    }

    if (isset($args['package_vars']['reseller_account'])) {
        if (!$server) {
            $server = new PleskServer($args['ServerHostName'], $args['plugin_plesk_Username'], $args['plugin_plesk_Password']);
        }

        // update reseller account if necessary
        if ($userName != $args['DomainUsername'] || $password != $args['DomainPassword']) {
            $result = $server->updateAccount($args['CustomerID'],$args['ServerAcctProperties']['userId'], $userName, $password);
            if (is_error($result)) {
                return $result;
            }
        }
    } else {
        if (!$server) {
            if ($args['plugin_plesk_Non-Admin_Username'] != '' && $args['plugin_plesk_Non-Admin_Password'] != '') {
                $server = new PleskServer($args['ServerHostName'], $args['plugin_plesk_Non-Admin_Username'], $args['plugin_plesk_Non-Admin_Password']);
            } else {
                $server = new PleskServer($args['ServerHostName'], $args['plugin_plesk_Username'], $args['plugin_plesk_Password']);
            }
        }
    }

    // unset package attributes must be set to 0
    if ($packageVars) {
        $variables = plugin_plesk_variables(false);
        $variables = array_keys($variables['package_vars_values']['value']);
        $attributes = array();
        foreach ($variables as $variable) {
            $attributes[$variable] = 0;
        }
        $packageVars = array_merge($attributes, $packageVars);
    }

    $result = $server->updateDomain($args['ServerAcctProperties']['domainId'], $userName, $password, $ip, $packageVars);

    if (is_error($result)) {
        return $result;
    }

    return true;
}

function plugin_plesk_Delete($args, $server = false)
{
    if (isset($args['package_vars']['reseller_account'])) {
        if (!$server) {
            $server = new PleskServer($args['ServerHostName'], $args['plugin_plesk_Username'], $args['plugin_plesk_Password']);
        }

        // delete user account
        $response = $server->deleteUser($args['ServerAcctProperties']['userId']);
        if (is_error($response)) {
            return $response;
        }

    } else {
        if (!$server) {
            if ($args['plugin_plesk_Non-Admin_Username'] != '' && $args['plugin_plesk_Non-Admin_Password'] != '') {
                $server = new PleskServer($args['ServerHostName'], $args['plugin_plesk_Non-Admin_Username'], $args['plugin_plesk_Non-Admin_Password']);
            } else {
                $server = new PleskServer($args['ServerHostName'], $args['plugin_plesk_Username'], $args['plugin_plesk_Password']);
            }
        }

        // delete domain
        // Plesk is not responding well to this command
        /*$response = $server->deleteDomain($args['ServerAcctProperties']['domainId']);
        if (is_error($response)) {
            return $response;
        }*/

        return new Error_CE(107, 'For the moment Plesk doesn\'t support the DELETE event for domains');
    }

    return true;
}

function plugin_plesk_Suspend($args, $server = false)
{
    if (!$server) {
        $server = new PleskServer($args['ServerHostName'], $args['plugin_plesk_Username'], $args['plugin_plesk_Password']);
    }
	$response = $server->setDomainStatus($args['ServerAcctProperties']['domainId'], 16);
	if (is_error($response)) {
            return $response;
    }	
}

function plugin_plesk_Unsuspend($args, $server = false)
{
    if (!$server) {
        $server = new PleskServer($args['ServerHostName'], $args['plugin_plesk_Username'], $args['plugin_plesk_Password']);
    }
	$response = $server->setDomainStatus($args['ServerAcctProperties']['domainId'], 0);
	if (is_error($response)) {
            return $response;
    }
}
?>
