PHP class for communicating with DA

Re: CMD_API_ADMIN_STATS

cizgihost said:
When i try CMD_API_ADMIN_STATS command. I recevies this response


bandwidth=%38%30%37%36%30&disk%31=%2Fdev%2Fhda%36%3A%32%30%31%36%30%34%34 etc.


How to use this information. What is this %38%30%37%36%30 etc...

use urldecode :) '%2' is a slash, if you'd put it in the URL, apache might think that you are requesting a file in another map, so the query string (everything behind ?, exactly the response from the class you are getting) is encoded.
 
why that doesn't work : http://www.hollinae.com/test1.php

I have just this test

<?php

include 'httpsocket.php';

$sock = new HTTPSocket;

$sock->connect('MY IP',2222);
$sock->set_login('adminlogin','adminpassword');

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

print_r($result);

?>

Have i missed something ? :^
 
You should try to replace

PHP:
$result = $sock->fetch_parsed_body();

print_r($result);

with

PHP:
$result = $sock->fetch_body();

echo $result;

, that might work.
 
Titam said:
Thanks for your reply. I tested and ... white page :\

grr

hmm, strange, I will test it on my own server in a few hours, got some work to finish up first :)
 
PeterB said:
hmm, strange, I will test it on my own server in a few hours, got some work to finish up first :)

Ok :) let me know if it's working on your server :)
 
Ok, I found why that did'nt work, it was because of my password, containing a à and ... it doesn't like my à :'( now it's working ... I changed my password ...
 
Can someone email or PM me with information on how they have implemeneted the suspend/unsuspend commands?

Thanks!
 
I just updated the main post with the latest version of this class.

ryant: There's an example here of how to suspend or unsuspend a user.

Cheers!
 
l0rdphi1 said:
I just updated the main post with the latest version of this class.

ryant: There's an example here of how to suspend or unsuspend a user.

Cheers!

Addon for ssl query
PHP:
	/**
	 * Query ssl
	 *
	*/
	function query_ssl( $request, $content = '', $usessl = 1)
	{
		if (function_exists("curl_init")) {
		$authstr = base64_encode("$this->remote_uname:$this->remote_passwd");
		$ch = curl_init();
		if ($usessl) {
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);                
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0);
			curl_setopt($ch, CURLOPT_URL, "https://$this->remote_host:$this->remote_port" . $request);
		} else {
			curl_setopt($ch, CURLOPT_URL, "http://$this->remote_host:$this->remote_port" . $request);
                }
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
	        $curlheaders[0] = "Authorization: Basic $authstr";
                curl_setopt($ch,CURLOPT_HTTPHEADER,$curlheaders);
		$data=curl_exec ($ch);
		curl_close ($ch);
	$this->result_body = $data;
	}
	} //ent ssl query
used with httpsocket.php @version 2.0
 
@kolobok: if PHP is compliled with openssl you could also do this:

$sock = new httpsocket();
$sock->connect("ssl://IP",2222);
$sock->query("/PAGE");

@WildFyre: Yes, that is possible. However, I'm not sure if there are CMD_API functions to do that. You may need to fake POST data to DA's GUI.

Good luck.
 
Class for DA with http.php (see top)

PHP:
<?
class DirectAdmin
{
	var $s;

	function DirectAdmin($host,$auser,$apass)
	{
		$this->s = new HTTPSocket;

		$this->s->connect($host,2222);
		$this->s->set_login($auser,$apass);
		return $this->check_auth($auser,$apass);

	}

	function check_auth($user,$pass)
	{
		$this->s->query('/CMD_API_VERIFY_PASSWORD',
		    array(
		        'user' => $user,
		        'pass' => $pass,

		    ));

		$ret=$this->s->fetch_parsed_body();

		if(! isset($ret['error']))
			return 0;
		else
			return $ret['details'];

	}


	function add_user($name,$email,$passwd,$domain,$package,$ip)
	{

		$this->s->query('/CMD_API_ACCOUNT_USER', array(
		        'action' => 'create',
		        'username' => $name,
		        'email' => $email,
		        'passwd' => $passwd,
		        'passwd2' => $passwd,
		        'domain' => $domain,
		        'package' => $package,
		        'ip' => $ip,
		        'notify' => 'yes'
		    ));


        $ret=$this->s->fetch_parsed_body();

		if(!isset($ret['error']))
			return 0;
		else
			return $ret['error'].' '.$ret['details'];

	}

	function get_user_info($name)
	{
		$this->s->query('/CMD_API_SHOW_USER_CONFIG',
		    array(
		        'user' => $name,
		    ));
		$ret=$this->s->fetch_parsed_body();
		if(!isset($ret['error']))
			return $ret;
		else
			return false;

	}

	function is_suspended_user($name)
	{
		$info=$this->get_user_info($name);
		return ($info['suspended']=='no')?0:1;
	}

	function is_active_user($name)
	{
		return !$this->is_suspended_user($name);
	}

	function suspend_user($name)
	{
		if($this->is_suspended_user($name)) return 0;
		$this->s->query('/CMD_SELECT_USERS',
		    array(
		        'location' => 'CMD_SELECT_USERS',
		        'suspend' => 'Suspend',
		        'select0' => $name
		    ));
		return $this->s->fetch_body();
	}

	function activate_user($name)
	{
		if($this->is_active_user($name)) return 0;
		$this->s->query('/CMD_SELECT_USERS',
		    array(
		        'location' => 'CMD_SELECT_USERS',
		        'suspend' => 'Unsuspend',
		        'select0' => $name
		    ));
		return $this->s->fetch_body();
	}



	function set_package_user($name,$package)
	{

		$this->s->query('/CMD_API_MODIFY_USER',
		    array(
		        'action' => 'package',
		        'user' => $name,
		        'package' => $package
		    ));

        $ret=$this->s->fetch_parsed_body();


		if(!$ret['error'])
			return 0;
		else
			return $ret['error'].' '.$ret['details'];

	}

	function get_users()
	{

		$this->s->query('/CMD_API_SHOW_USERS');
		$ret=$this->s->fetch_parsed_body();
		return $ret['list'];

	}

	function get_packages()
	{

		$this->s->query('/CMD_API_PACKAGES_USER');
		$r=$this->s->fetch_parsed_body();
		return $r['list'];

	}

	function delete_user($name)
	{
        $this->s->set_method('post');
		$this->s->query('/CMD_SELECT_USERS',
		    array(
		        'location' => 'CMD_SELECT_USERS',
		        'confirmed' => 'Confirm',
		        'delete' => 'yes',
		        'select0' => $name
		    ));
		return $this->s->fetch_body();


	}



}
?>

example using

PHP:
$DAHOST='81.177.8.125';
$DAADMIN='admin';
$DAPASS='******';
$DAIP='81.177.8.125'; // assign ip for users

include'http.php';
include'daapi.php';
$da=new DirectAdmin($DAHOST,$DAADMIN,$DAPASS);

print_r($da->get_packages()); // get a packeges list

with it class you can simple create own billing for auto accept payments create / delete / suspend users
 
PHP:
<?

//include the http socket & config file;
include '../httpsocket.php';
include '../config.php';

//end config part, dont touch the stuff below!;

$Socket = new HTTPSocket;
$Socket->connect($site,$port);
$Socket->set_login($user,$password);
$Socket->query('/CMD_API_ADMIN_STATS','');

$result = $Socket->fetch_parsed_body();

if ( $Socket->get_status_code() != 200 || !empty($result['error']) )
{
    // failure.
    echo "Failed to get serverstats: {$result['text']}<br />{$result['details']}";
}

// results.
    echo "{$result['value']}";
?>

Can someone figure out why this doesnt works?

I want to retrieve all the server stats...

Thnx!!!

(using 2.6 by the way.)
 
Also I would like to know if there is an possibility to create a package from a API command....
 
Hello,
I'm trying to create a billing system, but i'm having a problem.
When the system try to suspend and already suspended user. DA turns it UNsuspended...
 
Back
Top