-
Notifications
You must be signed in to change notification settings - Fork 2
/
directadminapi.class.php
executable file
·275 lines (218 loc) · 6.14 KB
/
directadminapi.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php
/**
* DirectAdmin API using cURL
*
* @author Hamid Samak <[email protected]>
* @copyright 2017 Hamid Samak
* @license MIT License
* @link https://github.com/hamidsamak/directadminapi
*/
class DirectAdminAPI {
// domain name or ip address
public $host;
// connection port number
public $port = 2222;
// login username
public $user;
// login password
public $pass;
// current domain (for panels with multiple [addon] domains)
public $domain;
// SSL mode
public $ssl = false;
// path for saving cookie file (default = same directory [.directadmincookie])
public $cookie_file_path;
// result text after sending request to server (HTML output)
public $response_text;
private $url; // request URL will be made by "action" method
private $status = false; // login status
private $invalid_login_message = 'Invalid login. Please verify your Username and Password'; // message for detecting unsuccessful login attempt
/**
* set default path for cookie file
* @return void
*/
public function __construct() {
$this->cookie_file_path = __DIR__ . DIRECTORY_SEPARATOR . '.directadmincookie';
}
/**
* send request to server
* @param string $url action address
* @param array $post post fields data
* @return boolean
*/
private function request($url, $post = array()) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_PORT, $this->port);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_file_path);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_REFERER, substr($this->action(), 0, -1) . ':' . $this->port);
return curl_exec($ch);
}
/**
* create URL with given action name
* @param string $name action name in DA panel
* @return string
*/
private function action($name = null) {
$this->url = 'http';
if ($this->ssl === true)
$this->url .= 's';
$this->url .= '://' . $this->host . '/' . $name;
return $this->url;
}
/**
* login to account
* @return boolean
*/
public function login() {
$data = $this->request($this->action('CMD_LOGIN'), array(
'referer' => '/',
'username' => $this->user,
'password' => $this->pass
));
if ($data !== false && strpos($data, $this->invalid_login_message) === false) {
$this->status = true;
$this->response_text = $data;
return true;
}
return false;
}
/**
* add new domain pointer
* @param string $source_domain domain name to park
* @param boolean $alias alias / pointer mode
* @return boolean
*/
public function domain_pointer_add($source_domain, $alias = true) {
if ($this->status === false)
return false;
$data = $this->request($this->action('CMD_DOMAIN_POINTER'), array(
'domain' => $this->domain,
'action' => 'add',
'from' => $source_domain,
'alias' => ($alias === true ? 'yes' : 'no')
));
if ($data !== false) {
$this->response_text = $data;
return true;
}
return false;
}
/**
* delete domain pointer(s)
* @param string $domain_names pointed domain name(s) [comma separated for multiple names]
* @return boolean
*/
public function domain_pointer_delete($domain_names) {
if ($this->status === false)
return false;
$domain_names = explode(',', $domain_names);
$domain_names = array_map('trim', $domain_names);
$request_array = array(
'domain' => $this->domain,
'action' => 'delete',
'delete' => 'Delete'
);
foreach ($domain_names as $key => $domain_name)
$request_array['select' . $key] = $domain_name;
$data = $this->request($this->action('CMD_DOMAIN_POINTER'), $request_array);
if ($data !== false) {
$this->response_text = $data;
return true;
}
return false;
}
/**
* put contents to file
* @param string $file_path full path of new or existing file
* @param string $contents text contents
* @return boolean
*/
public function file_put($file_path, $contents = null) {
$data = $this->request($this->action('CMD_FILE_MANAGER'), array(
'action' => 'edit',
'path' => dirname($file_path),
'filename' => basename($file_path),
'text' => $contents
));
if ($data !== false) {
$this->response_text = $data;
return true;
}
return false;
}
/**
* get file contents
* @param string $file_path full path of existing file
* @return boolean
*/
public function file_get($file_path) {
$data = $this->request($this->action('CMD_FILE_MANAGER' . $file_path));
if ($data !== false)
return $data;
return false;
}
/**
* delete file or directory
* @param string $file_path full path of existing file or directory
* @return boolean
*/
public function file_delete($file_path) {
$data = $this->request($this->action('CMD_FILE_MANAGER'), array(
'action' => 'multiple',
'path' => dirname($file_path),
'select1' => $file_path,
'button' => 'delete'
));
if ($data !== false) {
$this->response_text = $data;
return true;
}
return false;
}
/**
* make new directory
* @param string $path path to make new directory
* @param string $name directory name
* @return boolean
*/
public function dir_make($path, $name) {
$data = $this->request($this->action('CMD_FILE_MANAGER'), array(
'action' => 'folder',
'path' => $path,
'name' => $name
));
if ($data !== false) {
$this->response_text = $data;
return true;
}
return false;
}
/**
* get usage log
* @param integer $lines number of logs
* @return string
*/
public function web_usage_log($lines = 0) {
$data = $this->request($this->action('CMD_SHOW_LOG') . '?domain=' . $this->domain . '&type=log' . ($lines > 0 ? '&lines=' . $lines : null));
if ($data !== false)
return $data;
return false;
}
/**
* get error log
* @param integer $lines number of logs
* @return string
*/
public function web_error_log($lines = 0) {
$data = $this->request($this->action('CMD_SHOW_LOG') . '?domain=' . $this->domain . '&type=error' . ($lines > 0 ? '&lines=' . $lines : null));
if ($data !== false)
return $data;
return false;
}
}
?>