-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDathostAPI.php
More file actions
203 lines (167 loc) · 7.08 KB
/
DathostAPI.php
File metadata and controls
203 lines (167 loc) · 7.08 KB
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
<?php
class DathostAPI {
private $username;
private $password;
public $errors = array();
public $warnings = array();
public $status_code = 0;
public $verify_ssl = true;
public $result = false;
public $apiURL = 'https://dathost.net/api/0.1/';
public function __construct($email='', $password='') {
$this->username = $email;
$this->password = $password;
}
public function makeCall($path, $params=[], $method, $curlheaders=[]) {
// Clear the public vars
$this->errors = [];
$this->status_code = 0;
$this->result = false;
$call_url = $path;
$curl_handle=curl_init();
// Common settings
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_USERPWD, $this->username . ":" . $this->password);
if (!$this->verify_ssl) {
// WARNING: this would prevent curl from detecting a 'man in the middle' attack
curl_setopt ($curl_handle, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($curl_handle, CURLOPT_SSL_VERIFYPEER, 0);
}
// Determine REST verb and set up params
switch( strtolower($method) ) {
case "post":
$fields = http_build_query($params);
if(empty($curlheaders)) {
$curlheaders[] = 'Content-Length: ' . strlen($fields);
}
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $fields);
break;
case 'put':
$fields = http_build_query($params, '', '&');
if(empty($curlheaders)) {
$curlheaders[] = 'Content-Length: ' . strlen($fields);
}
curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $fields);
break;
case 'delete':
$fields = http_build_query($params, '', '&');
if(empty($curlheaders)) {
$curlheaders[] = 'Content-Length: ' . strlen($fields);
}
curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, "DELETE");
break;
case "get":
default:
$call_url .= "?".http_build_query($params, "", "&");
}
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $curlheaders);
curl_setopt($curl_handle, CURLOPT_URL, $call_url);
$curl_result = curl_exec($curl_handle);
$info = curl_getinfo($curl_handle);
$this->status_code = (int) $info['http_code'];
$return = false;
if ($curl_result === false) {
// CURL Failed
$this->errors[] = curl_error($curl_handle);
} else {
switch ($this->status_code) {
case 400: // Validation errors
$return = $this->result = $curl_result;
break;
case 405: // Validation errors
$return = $this->result = $curl_result;
break;
case 404: // Not found/Not in scope of account
$return = $this->result = $curl_result;
if(!empty($return->error)) {
foreach($return->error as $error) {
$this->errors[] = $error;
}
}
$return = false;
break;
case 500: // Oh snap!
$return = $this->result = false;
$this->errors[] = "Server returned HTTP 500";
break;
case 200:
$return = $this->result = $curl_result;
// Check if the result set is nil/empty
if (empty($return)) {
$this->errors[] = "Result set empty";
$return = false;
}
break;
default:
$this->errors[] = "Server returned unexpected HTTP Code ($this->status_code)";
$return = false;
}
}
curl_close($curl_handle);
return $return;
//return array($return, $this->errors, $call_url); // for debugging
}
public function getAccountInfo($params=[]) {
return $this->makeCall($this->apiURL . 'account', $params, 'get');
}
public function getGameServers($params=[]) {
return $this->makeCall($this->apiURL . 'game-servers', $params, 'get');
}
public function createGameServer($params=[]) {
return $this->makeCall($this->apiURL . 'game-servers', $params, 'post');
}
public function deleteGameServer($serverID) {
return $this->makeCall($this->apiURL . 'game-servers/' . $serverID, [], 'delete');
}
public function getGameServerInfo($serverID) {
return $this->makeCall($this->apiURL . 'game-servers/' . $serverID, [], 'get');
}
public function updateGameServerInfo($serverID, $params=[]) {
return $this->makeCall($this->apiURL . 'game-servers/' . $serverID, $params, 'put');
}
public function getGameServerConsoleLogs($serverID, $params=[]) {
return $this->makeCall($this->apiURL . 'game-servers/' . $serverID . '/console', $params, 'get');
}
public function sendTextToGameServerConsole($serverID, $params=[]) {
return $this->makeCall($this->apiURL . 'game-servers/' . $serverID . '/console', $params, 'post');
}
public function duplicateGameServer($serverID) {
return $this->makeCall($this->apiURL . 'game-servers/' . $serverID . '/duplicate', [], 'post');
}
public function listFilesOnGameServer($serverID, $params=[]) {
return $this->makeCall($this->apiURL . 'game-servers/' . $serverID . '/files', $params, 'get');
}
public function deleteFileOrPathFromGameServer($serverID, $path) {
return $this->makeCall($this->apiURL . 'game-servers/' . $serverID . '/' . $path, [], 'delete');
}
public function downloadFileFromGameServer($serverID, $path, $params=[]) {
return $this->makeCall($this->apiURL . 'game-servers/' . $serverID . '/files/' . $path, $params, 'get');
}
public function uploadFileToGameServer($serverID, $path, $params=[]) {
return $this->makeCall($this->apiURL . 'game-servers/' . $serverID. '/files/' . $path, $params, 'post');
}
public function moveFileInGameServer($serverID, $path, $params=[]) {
return $this->makeCall($this->apiURL . 'game-servers/' . $serverID . '/files/' . $path, $params, 'put');
}
public function getGameServerMetrics($serverID) {
return $this->makeCall($this->apiURL . 'game-servers/' . $serverID . '/metrics', [], 'get');
}
public function regenerateGameServerPasswordFTP($serverID) {
return $this->makeCall($this->apiURL. 'game-servers/' . $serverID . '/regenerate-ftp-password', [], 'post');
}
public function startGameServer($serverID, $params=[]) {
return $this->makeCall($this->apiURL. 'game-servers/' . $serverID . '/start', $params, 'post');
}
public function stopGameServer($serverID, $params=[]) {
return $this->makeCall($this->apiURL. 'game-servers/' . $serverID . '/stop', $params, 'post');
}
public function syncFilesBetweenLocalCacheAndGameServer($serverID) {
return $this->makeCall($this->apiURL . 'game-servers/' . $serverID . '/sync-files', [], 'post');
}
public function unzipFileOnGameServer($serverID, $path, $params=[]) {
return $this->makeCall($this->apiURL . 'game-servers/' . $serverID . '/unzip/' . $path, $params, 'post');
}
}