PHP class for communicating with DA

It works.

We had to use ssl://<IP> instead of only the IP or http(s)://<IP>.

This might be because we use Direct Admin within a https environement, but we're not sure about that, didn't test it. After all, SSL is a much safer connection type.

Some comments about the class itself, and please, change this in the original source code, so other people won't be bugged about it:

The class doesn't show information AT ALL why the connection failed, we had to modify the class to show a tiny bit of needed information.

Original code:
PHP:
        // instance connection
        if ($this->bind_host)
        {
            $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
            socket_bind($socket,$this->bind_host);

            if (!@socket_connect($socket,$this->remote_host,$this->remote_port))
            {
                $OK = FALSE;
            }

        }
        else
        {
            $socket = @fsockopen( $this->remote_host, $this->remote_port, $sock_errno, $sock_errstr, 10 );
        }

        if ( !$socket || !$OK )
        {
            $this->error[] = "Can't create socket connection to $this->remote_host:$this->remote_port.";
            return 0;
        }

Fixed code:
PHP:
        // instance connection
        if ($this->bind_host)
        {
            $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
			if (!$socket)
			{
				$OK = false;
				$this->error[] = socket_strerror(socket_last_error());
			}
			else if (!socket_bind($socket,$this->bind_host))
			{
				$OK = false;
				$this->error[] = socket_strerror(socket_last_error());
			}
			else if (!@socket_connect($socket,$this->remote_host,$this->remote_port))
			{
				$OK = false;
				$this->error[] = socket_strerror(socket_last_error());
			}
        }
        else
        {
            $socket = @fsockopen( $this->remote_host, $this->remote_port, $sock_errno, $sock_errstr, 10 );
			$this->error[] = $sock_errstr;
        }

		if ( !$OK || !$socket )
		{
            $this->error[] = "Can't create socket connection to $this->remote_host:$this->remote_port.";
            return false;
		}

v2.6.1 ;)
 
function killSite($username) {
$sock = new HTTPSocket;
$sock->connect('domain.com',2222);
$sock->set_login('username','password');
$sock->query('/CMD_SELECT_USERS',array(
'delete' => 'yes',
'confirmed' => 'Confirm',
'select0' => $username)
);
$result = $sock->fetch_body();
echo $result;
}

Ok guys, this is the function I have to streamline the removal of an account. I try this with the reseller login details which has created the account, and it shows me the DA delete complete page, with one big mistake...

It doesnt delete the account... Any ideas? It is seriously doing my head in, and I will promise a bottle of bourbon to be shared with the person who can help me fix this....

Thanks for your help :)
 
Last edited:
I'm pretty new when it comes to this....but is there a way to store server, username and ip so that you don't have to enter it manually into every fuction do you.
 
I'm pretty new when it comes to this....but is there a way to store server, username and ip so that you don't have to enter it manually into every fuction do you.

What I would do, is have a .php file somewhere on your site - call it maybe da.php.. Its contents could have something like..

<?

$daip = "xxx.xxx.xxx.xxx"; // Server IP Address
$dauser = "username"; // username
$dapass = "password"; // password

?>

Then all you need to do, is "include" the file and that way you always have those variables with the values...

<? include("da.php"); ?>

Tis that simple :)
 
What I would do, is have a .php file somewhere on your site - call it maybe da.php.. Its contents could have something like..

<?

$daip = "xxx.xxx.xxx.xxx"; // Server IP Address
$dauser = "username"; // username
$dapass = "password"; // password

?>

Then all you need to do, is "include" the file and that way you always have those variables with the values...

<? include("da.php"); ?>

Tis that simple :)

Ya but what happens when you have 5 servers and you want different users to use this script to login....I need to pass a username/password/server into this script to use it.
 
Then try using an array

$dauser['server1'] = "XXXXXXX";
$dapass['server1'] = "YYYYYYYYY";
$daip['server1'] = "xxx.xxx.xxx.xxx";

Then you just call up the specific $dauser['Whichever']...

Would that do the job?
 
Then try using an array

$dauser['server1'] = "XXXXXXX";
$dapass['server1'] = "YYYYYYYYY";
$daip['server1'] = "xxx.xxx.xxx.xxx";

Then you just call up the specific $dauser['Whichever']...

Would that do the job?

Not really because I don't know the users username/server/ip. I need them to enter that into a form and then pass those form variables into the script.
 
is there a way to know through API ...how long an account exist... the date of creation ?!
 
hi, using the API

Code:
$sock->query("/CMD_API_SHOW_USER_CONFIG?name=client1");
$result = $sock->fetch_parsed_body();
...
 echo $result['package'];

i get 'admin', i did create this account with user admin, altough client1, uses a configured package now (was using a custom package in the creation date), so i suppose that's why hte pakcage key..gives me the username of the crerator.

my question is, how can find the actual package a user is using?!
 
ups...my bad..just found out my error.

it's not name= but user=.


sorry all!
 
Can this script be used for batch mail account creation?

Hey.
This is a small PHP class put together to help communicate with DirectAdmin. It is simple enough, and most anyone can make good use of it.

I think this script could possibly save me days of work, but I would need some help with this. I'd like to use this script in order to migrate all email accounts from one server (running cpanel) to another server (running directadmin).

I posted another thread related to this migration I need to perform on http://www.directadmin.com/forum/showthread.php?p=152554#post152554

But perhaps your script is the only way to solve my problem on the short terms, so I thought it would be usefull to post a help request on this thread also.

Any help would be greatly appreciated!!!
 
I ran the script (show_all_users.php script) on a remote webserver (apache on my laptop) using my resellers username and password and my server gives me the following error:

You cannot execute that command

Details

The request you've made cannot be executed because it does not exist in your authority level

I`ve used my reseller username and password, also tried a client's account user and password. Could it be that DA would only accept requests from localhost? And reject any http request that comes from another IP / host? Are my options getting slimmer? Is there a command in the api that shows me the authority level I have and the commands I can execute?
 
Last edited:
I ran the script (show_all_users.php script) on a remote webserver (apache on my laptop) using my resellers username and password and my server gives me the following error:

You cannot execute that command
----------------------------------
Details

The request you\'ve made cannot be executed because it does not exist in your authority level

I`ve used my reseller username and password, also tried a client's account user and password. Could it be that DA would only accept requests from localhost? And reject any http request that comes from another IP / host? Are my options getting slimmer? Is there a command in the api that shows me the authority level I have and the commands I can execute?
Uhm :( I`ve uploaded the same script to my server and used "localhost" as hostname, the result is the same, DA says I don`t have the authority level. Heeeeeellllllpppppp!!!!
 
See my last post. You have to run the php script as the user which DirectAdmin expects. You've just proved my theory.

Jeff
 
Could someone please give me some help with handling cronjobs with the API?
( http://www.directadmin.com/features.php?id=364 )

I want to edit only 1 cronjob via a PHP script.
When I have a total of 5 cronjobs, I want to load only 1 cronjob, so I need to choose the "id" of the cronjob and then load all the information.

But how can I do this? I have 2 cronjobs now, but when I'm using the following code, I dont see anything?

PHP:
$socket = new HTTPSocket;
	$socket->connect('****.nl',2222);

	$socket->set_login('*****', '******');
	
	$socket->query('/CMD_CRON_JOBS');	
	
	$result = $socket->fetch_body();

echo $result;

I logged in with a reseller account that has a few cronjobs on user-level but i'm keep getting the error:
Can't create socket connection to domain.nl:2222.

Hope someone can help me with this.
 
Last edited:
How Create email a new by php



da_create_mail.php

PHP:
php 

include 'httpsocket.php'; 

$passwd     = $_POST[passwd]; 
$domain     ='*****'; 
$user     = $_POST[user]; 

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

$sock->set_login("******","********"); 

$sock->set_method('POST'); 

$sock->query('CMD_API_POP', 
    array( 
        'action' => 'create', 
        'domain' => '$domain', 
        'user' => '$user', 
        'passwd' => '$passwd', 
        'quota' => '10' 
    )); 
$result = $sock->fetch_body(); 

echo $result; 

?>



index.php




HTML:
<form action="da_create_mail.php" method="post" name="form_mail_accounts" id="form_mail_accounts">
  <div align="center"></div>
  <table width="90%"  border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
      <td colspan="3"><div align="center"><span class="style1">add</span></div></td>
    </tr>
    <tr>
   <td width="55%">user<input name="user" type="text" id="user" size="25" maxlength="12"></td>
      <td width="55%">pass<input name="passwd" type="text" id="passwd" size="25" maxlength="12"></td>
    </tr>
  </table>
  <div align="center"></div>
  <p align="center">
    <input type="submit" name="Submit" value="add">
    <input type="reset" name="Submit2" value="Reset Form">
</p>
</form>
 
the error means that you've not provided a valid host address, or maybe you're behind a firewall.
are you sure that "domain.nl:2222" is accessible?
 
PHP:
[PHP]
$sock->query('/CMD_API_POP','domain=$domain&quota='.$_POST['quota'].'&action=create&user='.$_POST['user'].'&passwd='.$_POST['passwd'].'');
///echo $sock->result;
if(eregi('error=0', $sock->result)){
 echo '<p>Email<br />
 username:    '.$_POST['user'].'@$domain<br />
 password:    '.$_POST['passwd'].'<br />
 Quota:        '.$_POST['quota'].'<br />
 POP3 server:    mail.$domain<br />
 SMTP server:    mail.$domain*</p>';
}
if(eregi('error=1', $sock->result)){
 echo 'error';
}
[/PHP]
 
Back
Top