Using the CMD_ACCOUNT_USER

drazhar

Verified User
Joined
Dec 5, 2005
Messages
49
Hi there, im using the above command to generate multiple user-accounts, using packages already on the server, and taking the settings from a couple of CSV files.

Now the problem im having is that the accounts are created but the limits used by the various packages are not set.

Is there something i need to run after the command is sent

Here is the code i use, with the da-connect-class.php
PHP:
<?
include('da-connect-class.php');

//DirectAdmin Default Settings
$GLOBALS['da_port'] =   2222;
$GLOBALS['da_socket'] = null;

/*-----------get server settings-----------*/
//hostname,ip,notify,reseller
$handle = fopen("server_settings.txt", "r");
$serv_settings = fgetcsv($handle, 1000, ",");
fclose($handle);
$GLOBALS['da_host'] = $serv_settings[0];
$GLOBALS['ip'] = $serv_settings[1];
$GLOBALS['notify'] = $serv_settings[2];
$GLOBALS['reseller'] = $serv_settings[3];
$GLOBALS['reseller_pass'] = $serv_settings[4];
/*-----------end get server settings-----------*/

/*-----------get User Accounts-----------*/
//userdomain,email,package|userdomain2,email2,package
$handle = fopen("accounts.txt", "r");
//setting the char limit at 50000, this may need to be edited depending
//on how many users are being added at one time
$accounts = fgetcsv($handle, 50000, "|");
//Each member of the array $accounts will be a an comma seperate string of
//the user settings
fclose($handle);
/*-----------end get user accounts-----------*/
/*--Debug Information--*/
print "The following data was found\n
In settings.txt:\n
DA_Host: ".$GLOBALS['da_host']."\n
IP: ".$GLOBALS['ip']."\n
Notify: ".$GLOBALS['notify']."\n
Reseller: ".$GLOBALS['reseller']."\n\n

In accounts.txt:\n
";
print "=========================================================\n";

for ( $i = 0; $i<count($accounts); $i++ )
{
    $tmparray =  $accounts[$i];
    $array = explode(",",$tmparray);
    $userdomain = $array[0];
    $email = $array[1];
    $package = $array[2];
    $password = createRandomPassword();
    $offset = 0;
    $offset = strpos($userdomain, '.');
    $username = substr($userdomain, 0, $offset);

    $length = strlen($username);
    
    /*--If a user's domain is under 5 characters in length, two 0's are appended to the domain--*/
    if ($length<5){
        $username .="00";
    }
    /*--Sets the upperlimit of 8 chars for the possible username--*/
    elseif ($length>8){
        $username = substr($userdomain,0, 9);
    }
    /*--Echo'ed to the command line to provide the creator with the account information in addition to the emails sent out--*/
    echo $username."\n";
    echo $password."\n";
    echo $userdomain."\n";
    echo $email."\n";
    echo $package."\n";
    
    $value = add_directadmin_account($username, $password, $userdomain, $email, $package);

    if ($value==0){
    print "
    Domain: " . $userdomain ."\tEmail: ".$email."\tPackage: ".$package."\n
    Username: ".$username."\tPassword: ".$password."\n";
    print"\$value:\t$value\n";
    print "=========================================================\n";
  }
  else {
    print"Account Failed to Add";
  }
}
function add_directadmin_account($username, $password, $domain, $email, $package)
{
    /*
    'action' =>'create',
    'username' =>'userloginlol',
    'email' =>'[email protected]',
    'passwd' =>'passwrdw',
    'passwd2' =>'passwrdw',
    'domain' =>'testtesttest.XXXXX.com',
    'package' =>'dsagfsdg',
    'ip' =>'xx.xxx.xxx.xx',
    'notify' =>'yes'
    */
    
    /*    if($quota == -1)
    $quota = $GLOBALS['da_quota'];*/
    //make sure we have a directadmin connection
    directadmin_connect();
    $GLOBALS['da_socket']->
    query("/CMD_ACCOUNT_USER", array(
    "action" =>"create",
    "username" => $username,
    "email" => $email,
    "passwd" => $password,
    "passwd2" => $password,
    "domain" =>$domain,
    "package" => $package,
    "ip" => $GLOBALS['ip'],
    "notify" => $GLOBALS['notify'],
    ));
    $result = $GLOBALS['da_socket']->
    fetch_parsed_body();
    //0 == success, account created
    //    if($result['error'] != 0)
    //        die("Couldn't create email account: ". $result['text'] . "\n");
} //}}}

function createRandomPassword(){
    $chars = "abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ023456789@#!";
    srand((double)microtime()*1000000);
    $i = 0;
    $pass = '' ;
    while ($i<= 8) {
        $num = rand() % 33;
        $tmp = substr($chars, $num, 1);
        $pass = $pass . $tmp;
        $i++;
    }
    return $pass;
}
?>

Find attached an example screenshot of a user setup with this script
 

Attachments

  • image1.jpg
    image1.jpg
    106.7 KB · Views: 500
I hade the same problem. It`s problem with "package" var, you must enter data manually :)
 
oh bugger :( DirectAdmin, Fix kindly, thank you please :)
 
Last edited:
Is the structure of the Create Custom User strict, as in, do i have to include every u... (unlimited variable)

Also, is the format strict, does each variable have to appear in the array being passed as the API lists them. I.E. does the vdomains variables have to following teh quota variables?
 
Hello,

That is the exact same command that gets use when you create a user through DA (which uses "package" correctly). If it works through DA but not the script... it would likely point to a difference between the form in DA and what is being passed through the API with the script.

Here is the form breakdown:
CMD_ACCOUNT_USER
method: POST

action=create
username=username
email=[email protected]
passwd=pass
passwd2=pass
domain=domain.com
package=packagename
ip=1.2.3.4
notify=yes
add=Submit


Where the boldentries are modified.
That's exactly what you use through DA, so if you can duplicate that, there wont be any issues.

Make sure you are *not* passing a variable called customize because if you do, then you start confusing DA as to what you want.

If there was a problem with the command itself, then it wouldn't work the usual way through DA.

John
 
Hello,

Just to double check everything, I created a "Create User" script.
Tested it, works fine:
PHP:
<?php

include 'httpsocket.php';

$sock = new HTTPSocket;
$sock->connect('localhost',2222);

$sock->set_login("admin","secret");

$sock->set_method('POST');

$sock->query('/CMD_ACCOUNT_USER',
        array(
                'action' => 'create',
                'username' => 'apitest',
                'email' => '[email protected]',
                'passwd' => 'secret',
                'passwd2' => 'secret',
                'domain' => 'apidomain.com',
                'package' => 'gold',
                'ip' => '192.168.0.30',
                'notify' => 'yes',
                'add' => 'Submit'
    ));

$result = $sock->fetch_body();

echo $result;

?>
Also, FYI, you can use CMD_API_ACCOUNT_USER to do the same thing, but get nice parseable results ;)
http://www.directadmin.com/features.php?id=167

John
 
Back
Top