PHP class for communicating with DA

Well, I can report that I have it working. I want to complete a couple more API commands and will then release the interface, including forms and calls to the API. I will do this tonight so everybody can use it as they see fit.

Kind Regards,
Onno Vrijburg
 
Here is what I have so far. I am making this available as open source and would appreciate it if other people woud contribute by building some of the classes or by fixing bugs in the code.

I am fully aware that the code is not 100% and some results are not shown (could use help on this) but at least most of the completed classes work well.


Content of the ZIP file
/code - classes for the API
/config - Configuration file (admin userID, password ...)
/forms - Forms for entering data (links 1-1 to API classes)
/protocol - httpsocket Class


Kind Regards,
Onno Vrijburg
 

Attachments

Excellent package you have there, Onno. Your open-source package will be a HUGE help to those who are just getting started with DA's API.

Cheers! :)
 
1/ Thanks for that Onno.

Most functions seem to work for me.... except it still refuses to create a database! Your function reports that it worked OK - when it doesn't!

I get the feeling there is a bug in my system somewhere.

----------------------------------------
2/There are a couple of changes I would suggest to improve the output of some of the functions
eg for

da_list_accounts.php

i would replace

echo $result;

with

foreach ($result['list'] as $key => $value) {
echo "$key : $value<br />\n";
}
 
Last edited:
Hi there.

I'm having the same problem as mike_p, and I'm also using the exact same code that's been posted here (which is correct according to the API definitions here on da.com).

If anyone has any more suggestions they'd be very welcome.

edit: I'm also on DA 1.23

Paul van der Walt
 
Last edited:
This seems to be a problem in DA 1.23. I have just tried it on a box running DA 1.22 and it works, on another box running DA 1.23 it does not work.

DASupport can you confirm that this is a problem in DA 1.23? Or help us solve the problem.

Kind Regards,
Onno Vrijburg
 
I had the problem before I upgraded to v1.23 (as well as after!).

Does the API use the same system to connect to the database as the main DirectAdmin app? I'm wondering whether it may be a MySQL client library version issue.

i.e. the mysql_connect function is deprecated in later versions
and mysql_real_connect function takes different parameters depending on the version.
 
Not sure, could be. Did minimal research on the 'segmentation fault' error, found this bugreport: http://bugs.php.net/bug.php?id=25400, that doesn't seem to be useful except that it's MySQL's fault. The version mentioned there is 4.0.14, I have version 4.0.16.

I find it very strange, I'm still getting that error even though I'm trying to fake a POST command which is essentially identical to the one used in the DA control panel (i.e. not using the API). Unfortunately I don't have any ideas.
 
Onno -

If you have a box on which it works and one where it doesn't, could you please compare the mysql client libraries in use on each?

Perhaps there is a conflict depending on whether you use the
mysql-shared-compat or the mysql-shared libraries.
On my server I've got the shared-compat libraries. (MySQL-shared-compat-4.0.16)

Mike
 
Hi. I had a slight problem with the CMD_API_SHOW_USER_DOMAINS api. The problem is this: the api definition says that the return will be in the form of Array(domain.com,domain1.com) etc, and this is true when I open http://adminuser:[email protected]:2222/CMD_API_SHOW_USER_DOMAINS?user=myuser. I then get the following:

Code:
userdomain.com=14.6848:unlimited:1.86753:4.5522:0:no:unlimited:ON:ON:ON:

The funny thing is that when I use the http socket library to access the api the same is returned, but with the format "userdomain_com" as the variable name in the array. Very strange. So for example this code:

PHP:
$listSocket = new HTTPSocket;

$listSocket->connect($siteurl,$siteport);
$listSocket->set_login($adminusername,$adminpassword);

$listSocket->query('/CMD_API_SHOW_USER_DOMAINS',array('user'=>'myuser'));

$listresult = $listSocket->fetch_parsed_body();
print_r($listresult);

...I get the following output:

Code:
Array
(
[userdomain_com] => 14.3521:unlimited:1.86654:4.48669:0:no:unlimited:ON:ON:ON:
)

Does anyone have any idea why this is? I have implemented a workaround but would still like to know the reason.

Thanks,
 
I have
PHP:
<?php

include 'httpsocket.php';

$sock = new HTTPSocket;

$sock->connect('p44.org',2222);
$sock->set_login('admin','passs');

$sock->query('/CMD_API_SHOW_ALL_USERS');
$result = $sock->fetch_parsed_body();

foreach ($result as $user) {
echo "$user <br />";
}

?>
and I know I screwed up something, anyone with a spare second wanna help? i want to list all users without the array crap showing.
 
PHP:
...
foreach ($result as $user) {
echo "$user <br />";
}
...

I have a feeling the foreach needs the following changes:

PHP:
foreach ($result['list'] as $key => $user) {
echo "$user <br />";
}

Give that a try and tell me what happens :) HTH
 
Problem:

Authentication doesn't work when having special characters like:

< or + or / or -

A lot of passwords on my server have those characters and is accepted by DA.

But when using the DA Class it isn't working at all. Somehow DA or the API translates it the wrong way.

Please can someone verify this by just using a password with a < or a + or something or both :P

Thx!
 
Hello,

Looking at my source, passwords as passed to DA like this:
$array_headers['Authorization'] = 'Basic '.base64_encode("$this->remote_uname:$this->remote_passwd");

That's a pretty standard format, so I'm not sure where the problem is. Perhaps this is a problem with DA? Or you're doing something silly?

I'd confirm right now if I wasn't so busy :(

Phi1.
 
I have tested it with curl_exec and it goes exactly the same way wrong.

Edit: I have changed the password without those characters and it goes great!

So there's definitly something wrong with the encoding procedure. Or DA's way of translating the passwords out of the headers!
 
Last edited:
PHP:
<pre><?
ob_start();
include 'httpsocket.php';

include 'funkcje.php';

polaczenie();

$da = new HTTPSocket;

$da->connect('thc.net.pl','2222');
$da->set_login('admin','***');
$sql = "SELECT login FROM thc_konta WHERE oplacone = '0' AND powiadomiony = '1'";
$r = zapytanie($sql);
 while($w = mysql_fetch_assoc($r))

{
    $tab = array ('location' => '/CMD_SELECT_USERS',
          'suspend' => 'Suspend',
          'select' => "$w[login]");
    $da->query('/CMD_SELECT_USERS', $tab); 
print_r($da);
    $rez = $da->fetch_parsed_body();
    $r2 = $da -> fetch_body();
    echo $r2;
    ?>
    <br><hr><b> <? echo $w[login];?> </b>
    <?
    print_r($rez);
    if( !empty($rez[error]))
    {
    echo "User ".$w[login]." nadal ma konto<br>";
    }
    else
    {
    echo "User ".$w[login]." ma zablokowane konto :-) <br>";
    }
}
echo "<br> Lacznie : ".mysql_num_rows($r);

ob_end_flush();
?>
result is:
httpsocket Object
(
[version] => 2.3
[method] => GET
[remote_host] => thc.net.pl
[remote_port] => 2222
[remote_uname] => admin
[remote_passwd] => ***
[result] => HTTP/1.1 404 Not found
Server: DirectAdmin Daemon v1.23.1 Registered to Piotr Kloc
Content-Type: text/html

Error: document not found

[result_header] => HTTP/1.1 404 Not found
Server: DirectAdmin Daemon v1.23.1 Registered to Piotr Kloc
Content-Type: text/html
[result_body] => Error: document not found

[bind_host] =>
[error] => Array
(
)

[query_cache] => Array
(
[0] => GET /CMD_SELECT_USERS?location=%2FCMD_SELECT_USERS&suspend=Suspend&select=gigi1990 HTTP/1.0
User-Agent: HTTPSocket/2.3
Host: thc.net.pl:2222
Accept: */*
Connection: Keep-Alive
Authorization: Basic YWRtaW46dGdpNUFwZUY=


[1] => GET //CMD_SELECT_USERS HTTP/1.0
User-Agent: HTTPSocket/2.3
Host: thc.net.pl:2222
Accept: */*
Connection: Keep-Alive
Authorization: Basic YWRtaW46dGdpNUFwZUY=


)

[doFollowLocationHeader] => 1
)
Error: document not found

wft? :|
 
Back
Top