├── README.md ├── cacert.pem └── directadmin.class.php /README.md: -------------------------------------------------------------------------------- 1 | # DirectAdmin API 2 | Alternative PHP DirectAdmin API using Curl extension. 3 | 4 | ```php 5 | require_once("directadmin.class.php"); 6 | 7 | $da = new DirectAdmin("https://HOST:2222", "USERNAME", "PASSWORD"); 8 | $result = $da->query("CMD_API_SHOW_USER_USAGE"); 9 | 10 | if (!$da->error) { 11 | print_r($result); 12 | } else { 13 | die("Error!"); 14 | } 15 | ``` 16 | 17 | --- 18 | 19 | ## Installation 20 | It is pretty simple to utilize this class, you just need to require it. 21 | 22 | ```php 23 | require_once("directadmin.class.php"); 24 | ``` 25 | 26 | ## Initialization 27 | Simple initialization. You can even leave it nothing and set it later. 28 | 29 | ```php 30 | // Initialize with Connect and Login in one function. 31 | // - (string/null) $host : DirectAdmin URL. (HTTP and HTTPS are supported.) 32 | // - (string/null) $username : DirectAdmin Username. 33 | // - (string/null) $password : DirectAdmin Password or Login Key. 34 | // - (bool) $ssl : Enable verify the peer's SSL certificate. (Enable by default.) 35 | $da = new DirectAdmin($host, $username, $password, $ssl); 36 | 37 | // Initialize without Connect and Login. (You need to set them later.) 38 | $da = new DirectAdmin(); 39 | ``` 40 | ## Set/Change DirectAdmin URL 41 | You can set/change DirectAdmin URL by $this->connect() function. 42 | 43 | Please note that it doesn't reset credentials, you don't need to re-login if your credentials still valid. 44 | 45 | ```php 46 | $da->connect("https://www.example.com:2222"); 47 | ``` 48 | 49 | ## Set/Change DirectAdmin Credentials 50 | You can set/change DirectAdmin Username and Password by $this->login() function. 51 | 52 | Please note that it will reset Login-As, but you can use $this->login_as() function later. 53 | 54 | ```php 55 | $da->login("username", "password"); 56 | ``` 57 | 58 | ## Control User 59 | You can Login-As any user under your authority level by $this->login_as() function. 60 | 61 | Ths function allows you to control your users without having their passwords. 62 | 63 | ```php 64 | $da->login_as("username"); 65 | ``` 66 | 67 | ## Logout from User 68 | You can use $this->logout() function to logout from your Login-As account or even your own account. 69 | 70 | If you want to logout on both accounts at the same time, you can use $this->logout(true) like below. 71 | 72 | ```php 73 | $da->login("first", "password"); // Login on "first" account. 74 | $da->login_as("second"); // Login on "second" account under "first" account. 75 | 76 | // Logout from "second" account then logout "first" account again. 77 | $da->logout(); // Logout from "second" account and return back to "first" account. 78 | $da->logout(); // Logout from "first" account and reset DirectAdmin Credentials. 79 | ``` 80 | 81 | ```php 82 | $da->login("first", "password"); // Login on "first" account. 83 | $da->login_as("second"); // Login on "second" account under "first" account. 84 | 85 | // Logout from both accounts at the same time. 86 | $da->logout(true); // Logout from both accounts and reset DirectAdmin Credentials. 87 | ``` 88 | 89 | ## Execute Query 90 | You can execute all commands those available under your authority level by $this->query() function. 91 | 92 | ```php 93 | // - (string) $command : DirectAdmin Command. (Should starts with "CMD_API_") 94 | // - (null/array/string) $form : Fields and Values to execute command. (Should be Array or Null) 95 | // - (string) $method : Method to execute command. (GET/POST) 96 | $result = $da->query($command, $form, $method); 97 | ``` 98 | 99 | It isn't necessary to set $form and $method parameters on some commands. 100 | 101 | You can leave it nothing like example below. 102 | 103 | ```php 104 | // Example for Retrieve the user's usages. (There is no need for $form and $method parameters on this command.) 105 | $result = $da->query("CMD_API_SHOW_USER_USAGE"); 106 | 107 | // Example for Retrieve the user's usages. (With all parameters) 108 | $result = $da->query("CMD_API_SHOW_USER_USAGE", null, "GET"); 109 | 110 | // Example for Create subdomain. (sub.example.com) 111 | $result = $da->query("CMD_API_SUBDOMAINS", array( 112 | "domain" => "example.com", 113 | "action" => "create", 114 | "subdomain" => "sub" 115 | ), "POST"); 116 | ``` 117 | 118 | If the command is starts with "CMD_API_", it'll automatically parse the response to Array. 119 | 120 | However, you can do manual parse with $this->parse() function below. 121 | 122 | ## Manual Parse 123 | You can manual parse Query response to Array by $this->parse() function. 124 | 125 | You don't need to parse the response if the command is starts with "CMD_API_". 126 | 127 | ```php 128 | $da->parse($response); 129 | ``` 130 | 131 | It will not parse the response if it contains "\" tag. 132 | 133 | You can force it to parse even it contains "\" tag by $this->parse($response, true). 134 | 135 | ```php 136 | $da->parse($response, true); 137 | ``` 138 | 139 | --- 140 | 141 | ## Editable Variables 142 | You can update these variables to make it works better for you. 143 | 144 | - (bool) $this->list_result : Return the list[] as result directly if it exists. (You can turn it off by set this to False.) 145 | - (resource) $this->handle : Curl Handle. (You can set options to this handle.) 146 | 147 | ## Non-editable Variables 148 | You can use these variables but you shouldn't update them. 149 | 150 | - (bool) $this->login : Local Login State. (True on $this->login() function and False on $this->logout() function.) 151 | - (bool) $this->error : If it found Error, it will become True. (This variable will update every execution/parse.) 152 | - (string) $this->host : Current DirectAdmin URL. 153 | - (string) $this->username : Current DirectAdmin Username. 154 | - (string) $this->password : Current DirectAdmin Password. 155 | - (bool/string) $this->login_as : Current DirectAdmin Login-As Username. (Should be False if it doesn't Login-As any user.) 156 | 157 | --- 158 | 159 | ### Note 160 | - $this->query() function will automatically parse the response to Array if the command starts with "CMD_API_". 161 | - $this->query() function will update $this->error to True if there is any problem while executing. 162 | - It is better to check error by check $this->error variable. (Example: echo !$da->error ? "OK" : "Error"; ) 163 | - It will return plain response if there is any error or contains "\" tag. 164 | - Connecting Timeout is 30 seconds and Timeout is 60 seconds by default. You can change it by set options to Curl Handle. 165 | - If cacert.pem file exists, CURLOPT_CAINFO will set to cacert.pem's realpath by default for security reasons. 166 | -------------------------------------------------------------------------------- /directadmin.class.php: -------------------------------------------------------------------------------- 1 | handle = curl_init(); 18 | curl_setopt_array($this->handle, array( 19 | CURLOPT_RETURNTRANSFER => true, 20 | CURLOPT_FOLLOWLOCATION => false, 21 | CURLOPT_SSL_VERIFYPEER => $ssl, 22 | CURLOPT_CONNECTTIMEOUT => 30, 23 | CURLOPT_TIMEOUT => 60 24 | )); 25 | if ($ssl && file_exists(__DIR__."/cacert.pem")) { 26 | curl_setopt($this->handle, CURLOPT_CAINFO, realpath(__DIR__."/cacert.pem")); 27 | } 28 | $this->connect($host)->login($username, $password); 29 | } 30 | 31 | private function set_auth($auth) { 32 | $header = $auth ? array("Authorization: Basic ".base64_encode($auth)) : array(); 33 | curl_setopt($this->handle, CURLOPT_HTTPHEADER, $header); 34 | return $this; 35 | } 36 | 37 | public function connect($host) { 38 | $this->host = rtrim(strval($host), "/"); 39 | $this->login = $this->host == "" || $this->username == "" || $this->password == "" ? false : true; 40 | return $this; 41 | } 42 | 43 | public function login($username, $password) { 44 | $this->username = strval($username); 45 | $this->password = strval($password); 46 | $this->login_as = false; 47 | $this->login = $this->host == "" || $this->username == "" || $this->password == "" ? false : true; 48 | $this->set_auth($this->username.":".$this->password); 49 | return $this; 50 | } 51 | 52 | public function login_as($username) { 53 | $this->login_as = strval($username); 54 | $this->set_auth($this->username."|".$this->login_as.":".$this->password); 55 | return $this; 56 | } 57 | 58 | public function logout($all = false) { 59 | if ($all || !$this->login_as) { 60 | $this->username = ""; 61 | $this->password = ""; 62 | $this->login_as = false; 63 | $this->login = false; 64 | $this->set_auth(false); 65 | } else { 66 | $this->login($this->username, $this->password); 67 | } 68 | return $this; 69 | } 70 | 71 | public function query($command, $form = null, $method = "GET") { 72 | if ($this->host == "" || $this->username == "" || $this->password == "") { 73 | $this->login = false; 74 | $this->error = true; 75 | return false; 76 | } 77 | $command = ltrim($command, "/"); 78 | $form = is_array($form) ? http_build_query($form) : (is_string($form) ? $form : null); 79 | curl_setopt_array($this->handle, array( 80 | CURLOPT_URL => $this->host."/".$command.($method === "GET" && is_string($form) ? "?".$form : ""), 81 | CURLOPT_CUSTOMREQUEST => $method, 82 | CURLOPT_POSTFIELDS => $method !== "GET" ? $form : null 83 | )); 84 | $response = curl_exec($this->handle); 85 | if (curl_errno($this->handle) === 0) { 86 | return $this->parse($response, false, $command); 87 | } else { 88 | $this->error = true; 89 | return $response; 90 | } 91 | } 92 | 93 | public function parse($response, $force = true, $command = "CMD_API_") { 94 | if ($force || substr($command, 0, 8) === "CMD_API_") { 95 | if (!$force && stripos($response, "") !== false) { 96 | $this->error = true; 97 | return $response; 98 | } 99 | parse_str($response, $array); 100 | if (!isset($array["error"]) || $array["error"] === "0") { 101 | $this->error = false; 102 | if ($this->list_result && !isset($array["error"]) && isset($array["list"])) { 103 | $array = $array["list"]; 104 | } 105 | } else { 106 | $this->error = true; 107 | } 108 | return $array; 109 | } else { 110 | $this->error = null; 111 | return $response; 112 | } 113 | } 114 | } 115 | 116 | ?> 117 | --------------------------------------------------------------------------------