Advertisement
Guest User

DirectAdmin API

a guest
May 28th, 2011
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.33 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * DirectAdmin API Class
  5.  *
  6.  * With this class you can communicate with the DirectAdmin API
  7.  *
  8.  * @author Rick Doorakkers
  9.  * @version 0.1
  10.  */
  11.  
  12. class DirectAdmin
  13. {
  14.     private $_method,
  15.             $_host,
  16.             $_port,
  17.             $_username,
  18.             $_password,
  19.             $_bindHost,
  20.             $_socket,
  21.             $_error,
  22.             $_result;
  23.  
  24.     /**
  25.      * DirectAdmin Constructor
  26.      *
  27.      * This constructor will save the settings and try to connect to the DirectAdmin API.
  28.      *
  29.      * @param String $host
  30.      * @param Int $port
  31.      * @param String $username
  32.      * @param String $password
  33.      * @param String $method
  34.      */
  35.     function __construct( $host, $port, $username, $password, $method = "GET" )
  36.     {
  37.         $this->_host        = $host;
  38.         $this->_port        = $port;
  39.         $this->_username    = $username;
  40.         $this->_password    = $password;
  41.         $this->_method          = $method;
  42.         $this->_bindHost        = '****';
  43.     }
  44.  
  45.     /**
  46.      * This function will return a array with all the users
  47.      *
  48.      * @param String $resellerName
  49.      */
  50.     public function getResellerUserList( $resellerName )
  51.     {
  52.         $this->query( '/CMD_API_SHOW_USERS', array( 'reseller' => $resellerName ));
  53.     }
  54.    
  55.     /**
  56.      * This function will return a array with all the users
  57.      *
  58.      * @param String $resellerName
  59.      */
  60.     public function getResellerList()
  61.     {
  62.         $this->query( '/CMD_API_SHOW_RESELLERS', array());
  63.     }    
  64.    
  65.     private function query( $request, $content = array() )
  66.     {
  67.         $this->_error = array();
  68.        
  69.         $headers = array(
  70.                          'User-Agent'   => 'HTTPSocket/2.6',
  71.                          'Host'         => $this->_host . ':' . $this->_port,
  72.                          'Accept'       => '*/*',
  73.                          'Connection'   => 'Close'
  74.                          );
  75.        
  76.         $this->_socket = socket_create( AF_INET, SOCK_STREAM, SOL_TCP );
  77.         socket_bind( $this->_socket, $this->_bindHost );
  78.                
  79.         if( !@socket_connect( $this->_socket, $this->_host, $this->_port ) )
  80.         {
  81.             $this->_error[] = 'Er kon geen verbinding worden gemaakt met de server.';
  82.             return false;
  83.         }
  84.        
  85.         $headers['Authorization']   = 'Basic ' . base64_encode($this->_username . ':' . $this->_password);
  86.        
  87.         if( count( $content ) > 0 )
  88.         {
  89.             $pairs = array();
  90.            
  91.             foreach( $content as $key => $value )
  92.             {
  93.                 $pairs[] = $key . '=' . urlencode($value);
  94.             }
  95.            
  96.             $request .= '?' . implode('&', $pairs);
  97.         }
  98.        
  99.         echo $request;
  100.        
  101.         /*
  102.         $query = $this->_method . ' ' . $request . " HTTP/1.0\n";
  103.         foreach( $headers as $key => $value )
  104.         {
  105.             $query .= $key . ':' . $value . "\n";
  106.         } */
  107.        
  108.         echo '<pre>' , $query , '</pre>';
  109.        
  110.         socket_write( $this->_socket, $query );
  111.        
  112.         while ( $out = socket_read( $this->_socket, 2048 ) )
  113.         {
  114.             $this->_result .= $out;
  115.         }
  116.        
  117.         echo '<pre>' , $this->_result , '</pre>';
  118.        
  119.         socket_close( $this->_socket );
  120.     }
  121. }
  122.  
  123. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement