<?php
// +----------------------------------------------------------------------+
// | NEXCESS.NET InterWorx                                                |
// +----------------------------------------------------------------------+
// | Copyright (c) 2000-2003 NEXCESS.NET L.L.C., All Rights Reserved.     |
// +----------------------------------------------------------------------+
// | Redistribution and use in source form, with or without modification  |
// | is NOT permitted without consent from the copyright holder.          |
// |                                                                      |
// | THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND |
// | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,    |
// | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A          |
// | PARTICULAR PURPOSE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,    |
// | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,  |
// | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR   |
// | PROFITS; OF BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY  |
// | OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT         |
// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE    |
// | USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH     |
// | DAMAGE.                                                              |
// +----------------------------------------------------------------------+
// | Authors: Chris Wells <clwells@nexcess.net>                           |
// |          Paul Oehler <poehler@nexcess.net>                           |
// +----------------------------------------------------------------------+
//

require_once 'plugins/server/interworx/functions.inc.php';
require_once 'newedge/classes/NE_Error.php';
require_once 'modules/admin/models/ServerPlugin.php';

/**
* @package Plugins
*/
class PluginInterworx extends ServerPlugin
{

    /* This function outlines variables used when setting up the plugin in
     * the Servers section of ClientExec. It is a required function.
     */
    
    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' => /*T*/'Used by ClientExec to display plugin. It must match the action function name(s).'/*/T*/,
    		        'value'       => 'InterWorx-CP'
    		       ),
    	    /*T*/'Description'/*/T*/ => array (
    	            'type'        => 'hidden',
    		        'description' => /*T*/'Description viewable by admin in server settings'/*/T*/,
    		        'value'       => /*T*/'InterWorx-CP integration.'/*/T*/
    		       ),
    	    /*T*/'Use Secure Connections'/*/T*/ => array (
    	            'type'        => 'yesno',
    		        'description' => /*T*/'Set to "NO" if you do not have PHP compiled with cURL. Set to "YES" if PHP is compiled with cURL.<br /><strong>NOTE:</strong> It is recommended that you set this option to "YES."'/*/T*/,
    		        'value'       => '1'
    		       ),
    	    /*T*/'Verify Certificate'/*/T*/ => array (
    	            'type'        => 'yesno',
    		        'description' => /*T*/'Set to "NO" if you do not wish to verify server certificate. Set to "YES" otherwise.'/*/T*/,
    		        'value'       => '1'
    		       ),
    	    /*T*/'Access Key'/*/T*/ => array (
    	            'type'        => 'textarea',
    		        'description' => /*T*/'Access key used to authenticate to server.'/*/T*/,
    		        'value'       => '',
    		        'encryptable' => true
    		       ),
    	    /*T*/'Failure E-mail'/*/T*/ => array (
    	            'type'        => 'text',
    		        'description' => /*T*/'E-mail address InterWorx-CP plugin error messages will be sent to.'/*/T*/,
    		        'value'       => ''
    		       ),
    	    /*T*/'Actions'/*/T*/ => array (
    	            'type'        => 'hidden',
    		        'description' => /*T*/'Actions currently available for this plugin.'/*/T*/,
    		        'value'       => 'Create,Update,Delete,Suspend,UnSuspend'
    		       )
    	   );
      
      return ($variables);
    }
    
    /* This function is used to determine how ClientExec creates usernames
     * on the server. If it does not matter, as is the case with InterWorx,
     * return $args ['DomainUsername'].
     */
    
    function createUserName ($args) {
      return ($args ['DomainUsername']);
    }
    
    /* This function is called after a new account is activated/approved. It
     * creates a hosting account on a remote InterWorx server.
     *
     * @param  $args Array of available arguments/variables.
     * @return       Error message, if something went wrong. Otherwise, empty string.
     */

     function create ($args) {
         $ip = $create = '';

         /* Available values in the args array:
       *   ServerHostName Server hostname entered in the server config section.
       *   DomainName     Domain that is being created.
       *   DomainUsername Username associated with the domain.
       *   DomainPassword Password to be used for this domain.
       *   DomainSharedIP 0 or 1; check DomainIP (below) for actual IP address, if 0.
       *   DomainIP       IP address this domain will be associated with.
       *
       * To get plugin-specific variables, use the following structures:
       *   $args ['plugin_interworx_VARIABLENAME']
       *
       * Example: $args ['plugin_interworx_Username']
         */

         if (!$args ['plugin_interworx_Create'] == 1) return; // Not create account.
         if ($args ['PackageNameOnServer'] == null) return; // Not InterWorx package.

         /* Check to make sure the package already exists on the InterWorx box
       * before proceeding. If it does not, return an error message.
         */

         if (!package_exists ($args ['PackageNameOnServer'], $args)) {

             $msg_body = 'Account creation failed. The package ' . $args ['PackageNameOnServer'];
             $msg_body .= ' was not found on the ' . $args ['ServerHostName'] . ' server.';

             // Log the error then throw an error
             $this->logger->log(4, "plugin_interworx::create::error: ".$msg_body);

             return new NE_Error($msg_body, 200);
         }

         /* Get an IP (shared or static) to put this account on the InterWorx
       * box, now that we have checked the existance of package on the box.
         */

         if( $args['DomainSharedIP'] == 0 ) {
             $ip = $args['DomainIP'];
         } else {
             if (!get_ip ($ip, $args)) {
                 $msg_body = 'Account creation failed. When requesting an IP from the server, ';
                 $msg_body .= 'instead of the IP, the message \'' . $ip . '\' was received.';

                 // Log the error then throw an error
                 $this->logger->log(4, "plugin_interworx::create::error: ".$msg_body);

                 return new NE_Error($msg_body, 200);
             }
         }

         /* Set up the account on the InterWorx box, now that we have checked
       * the existance of the package on the server and got an IP to put
       * this account on.
         */

         if (!create_acct ($create, $ip, $args)) {
             $msg_body = 'Account creation failed. The following error message was ';
             $msg_body .= 'received: ' . $create . '.';

             // Log the error then throw an error
             $this->logger->log(4, "plugin_interworx::create::error: ".$msg_body);

             return new NE_Error($msg_body, 200);
         }

         return; // ClientExec does not check status messages for now.
     }
    
    /* This function is called to delete an account on the InterWorx server.
     *
     * @param  $args Array of available arguments/variables.
     * @return       Error message, if something went wrong. Otherwise, empty string.
     */
    
    function delete ($args) {
        $delete = '';

        if (!delete_acct ($delete, $args)) {
            $msg_body = 'Account deletion failed. The following error message was ';
            $msg_body .= 'received: ' . $delete . '.';

            // Log the error then throw an error
            $this->logger->log(4, "plugin_interworx::create::error: ".$msg_body);

            return new NE_Error($msg_body, 200);
        }

        return; // ClientExec does not check status messages for now.
    }
    
    /* This function is called when client information, such as e-mail, password 
     * or hosting package associated with client account, is updated via the 
     * interface.
     *
     * @param  $args Array of available arguments/variables.
     * @return       Error message, if something went wrong. Otherwise, empty string. 
     */
    
     function update ($args) {
         $update = '';

         if (!$args ['plugin_interworx_Update'] == 1) return; // Not update account.

         /* Determine which variables have changed (e-mail, password, etc.) and update
       * them:
         */

         foreach (array_keys ($args) as $key) {
             switch ($key) {
                 case 'CHANGE_PASSWORD':
                     if (empty ($args ['CHANGE_PASSWORD'])) break; // No pass.

                     $update_msg =
                             array ('access_key' => $args ['plugin_interworx_Access_Key'],
                             'domain'     => $args ['DomainName'],
                             'password'   => $args ['CHANGE_PASSWORD']
                     );

                     if (!update_client_info ($update,
                     'iworx.updatepassword',
                     $update_msg,
                     $args)) {
                         $msg_body = 'Account update failed. The following error message was ';
                         $msg_body .= 'received: ' . $update . '.';

                         // Log the error then throw an error
                         $this->logger->log(4, "plugin_interworx::update::error: ".$msg_body);

                         return new NE_Error($msg_body, 200);
                     }

                     break;
                 case 'CHANGE_PACKAGE':
                     if ($args ['PackageNameOnServer'] == null)
                         break; // Not InterWorx package.
                     if (!package_exists ($args ['PackageNameOnServer'], $args))
                         break; // Package does not exist.

                     $update_msg = array ('access_key'   => $args ['plugin_interworx_Access_Key'],
                             'domain'       => $args ['DomainName'],
                             'package_name' => $args ['CHANGE_PACKAGE']
                     );

                     if (!update_client_info ($update,
                     'iworx.updatepackage',
                     $update_msg,
                     $args)) {
                         $msg_body = 'Account update failed. The following error message was ';
                         $msg_body .= 'received: ' . $update . '.';

                         // Log the error then throw an error
                         $this->logger->log(4, "plugin_interworx::update::error: ".$msg_body);

                         return new NE_Error($msg_body, 200);
                     }

                     break;
                 case 'CHANGE_EMAIL':
                     if (empty ($args ['CHANGE_EMAIL'])) break; // No pass.

                     $update_msg = array ('access_key'   => $args ['plugin_interworx_Access_Key'],
                             'domain'       => $args ['DomainName'],
                             'package_name' => $args ['CHANGE_EMAIL']
                     );

                     if (!update_client_info ($update, 'iworx.updatepassword', $update_msg, $args)) {
                         $msg_body = 'Account update failed. The following error message was ';
                         $msg_body .= 'received: ' . $update . '.';

                         // Log the error then throw an error
                         $this->logger->log(4, "plugin_interworx::update::error: ".$msg_body);

                         return new NE_Error($msg_body, 200);
                     }

                     break;
             }
         }

         return; // ClientExec does not check status messages for now.
     }
    
    /* This function is called suspend an account on the InterWorx box.
     *
     * @param  $args Array of available arguments/variables.
     * @return       Error message, if something went wrong. Otherwise, a
     *               simple success message.
     */
    
     function suspend ($args) {
         $suspend = '';

         if (!suspend_acct ($suspend, $args)) {
             $msg_body = 'Account suspension failed. The following error message was ';
             $msg_body .= 'received: ' . $suspend . '.';

             // Log the error then throw an error
             $this->logger->log(4, "plugin_interworx::suspend::error: ".$msg_body);

             return new NE_Error($msg_body, 200);
         }

         return; // ClientExec does not check status messages for now.
     }

    /* This function is called unsuspend an account on the InterWorx box.
     *
     * @param  $args Array of available arguments/variables.
     * @return       Error message, if something went wrong. Otherwise, a
     *               simple success message.
     */
    
    function unsuspend ($args) {
        $unsuspend = '';

        if (!enable_acct ($unsuspend, $args)) {
            $msg_body = 'Account unsuspension failed. The following error message was ';
            $msg_body .= 'received: ' . $unsuspend . '.';

            // Log the error then throw an error
            $this->logger->log(4, "plugin_interworx::unsuspend::error: ".$msg_body);

            return new NE_Error($msg_body, 200);
        }

        return; // ClientExec does not check status messages for now.
    }

}
?>
