-
Notifications
You must be signed in to change notification settings - Fork 1
/
easywiapi.php
285 lines (251 loc) · 8.67 KB
/
easywiapi.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
276
277
278
279
280
281
282
283
284
285
<?php
class EasyWiRestAPI {
// define internal vars
private $method,$timeout,$connect=false,$user,$pwd,$handle=null,$ssl,$port,$url;
protected $response=array();
// Constructor that sets defaults which can be overwritten
__construct($url,$user,$pwd,$timeout=10,$ssl=false,$port=80,$method='xml',$connect='curl') {
$this->timeout=$timeout;
// check if curl is choosen and available and initiate cURL-Session
if ($connect=='curl' and function_exists('curl_init')) {
if ($this->startCurl($url,$ssl,$port)===true) {
$this->connect='curl';
}
// Use and or fallback to fsockopen if possible and create socket
} else if (($connect=='fsockopen' or !function_exists('curl_init')) and function_exists('fsockopen')) {
if ($this->startSocket($url,$ssl,$port)===true) {
$this->connect='fsockopen';
}
}
// If connection was successfull, go on and set values
if ($this->connect!==false) {
$this->user=$user;
$this->pwd=$pwd;
$this->ssl=$ssl;
$this->port=$port;
$this->url=$url;
// Use json, or xml to communicate
if ($method=='json') {
$this->method='json';
} else {
$this->method='xml';
}
} else {
$this->throwException(10);
}
}
// False usage of the object needs to be handled and execution stopped
private function throwException ($rawError,$extraText=false) {
// If an exception is caught from imbedded class use the raw error
if (is_object($rawError)) {
$errorcode=$rawError->getMessage();
// else use the custom messages
} else {
// default custom messages
$errorArray=array(
1=>'Bad data: Only Strings and Integers are allowed!',
2=>'Bad data: Only Strings are allowed!',
3=>'Bad data: Only Integers are allowed!',
4=>'Bad data: Only arrays are allowed!',
5=>'Bad data: Unknown Error!',
6=>'Bad data: Empty values!',
10=>'Connection Error: Could not connect to!'.$this->url
);
// if the message is not predifined use the raw input
if (array_key_exists($rawError,$errorArray)) {
$errorcode=$errorArray["${rawError}"];
} else {
$errorcode=$rawError;
}
}
// Add some extra info if given
if ($extraText!==false) {
$errorcode.=$extraText;
}
throw new Exception('<p>'.$errorcode.'</p>');
die;
}
//
private function startCurl ($url,$ssl,$port) {
// create the URL to call
if (substr($url,-1)=='/') {
$url=substr($url,0,-1);
}
$url=str_replace(array('http://','https://',':8080',':80',':443'),'',$url);
if ($ssl==true) {
$url='https://'.$url;
} else {
$url='http://'.$url;
}
$url=$url.'/api.php';
// create cURL-Handle
$this->handle=curl_init($url);
// check success
if ($this->handle===false) {
return false;
} else {
// Set options
$this->setbasicCurlOpts();
return true;
}
}
// in case of curl setopts
private function setbasicCurlOpts () {
curl_setopt($this->handle,CURLOPT_CONNECTTIMEOUT,$this->timeout);
curl_setopt($this->handle,CURLOPT_USERAGENT,"cURL (Easy-WI; 1.0; Linux)");
curl_setopt($this->handle,CURLOPT_RETURNTRANSFER,true);
curl_setopt($this->handle,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($this->handle,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($this->handle,CURLOPT_HEADER,1);
//curl_setopt($this->handle,CURLOPT_ENCODING,'deflate');
if (($this->ssl===true and $this->port!=443) or ($this->ssl===false and $this->port!=80)) {
curl_setopt($this->handle,CURLOPT_PORT,$this->port);
}
}
// method to execute a curl request
private function execCurl($type,$send) {
// Setting up POST data and add it to the opts
$postArray['user']=$this->user;
$postArray['pwd']=$this->pwd;
$postArray['type']=$type;
$postArray['xmlstring']=$send;
curl_setopt($this->handle,CURLOPT_POSTFIELDS,$postArray);
// Execute request, get the response and return it.
$this->response=curl_exec($this->handle);
$this->header=curl_getinfo($this->handle);
return $this->response;
}
// Ioncube obfuscated files add sometimes data to the REST responses.
// This will be picked up if fsockopen is used.
// So there is a need to strip this data.
private function convertRawData ($rawdata) {
if ($this->method=='json') {
$checkStart='{';
$checkStop='}';
} else {
$checkStart='<';
$checkStop='>';
}
$response=$rawdata;
while (substr($response,0,1)!=$checkStart and strlen($response)>0) {
$response=substr($response,1);
}
while (substr($response,-1)!=$checkStop and strlen($response)>0) {
$response=substr($response,0,-1);
}
// Decode the rest of the response string into an object.
if ($this->method=='json') {
$decoded=@json_decode($response);
} else {
$decoded=@simplexml_load_string($response);
}
// If decoding was not possible return the raw response, else return the object.
if ($decoded) {
unset($rawdata);
return $decoded;
} else if ($this->connect=='fsockopen') {
return substr($rawdata,4,-3);
} else {
return $rawdata;
}
unset($decoded);
}
// create the JSON that will be send to the API
private function JSONPostValue ($paramArray,$action,$params) {
$jsonArray=array();
foreach ($paramArray as $param) {
if (array_key_exists($param,$params)) {
if (is_array($params[$param])) {
$jsonArray[$param]=array();
foreach ($params[$param] as $val) {
$jsonArray[$param][]=$params[$param];
}
} else {
$jsonArray[$param]=$params[$param];
}
} else {
$jsonArray[$param]='';
}
}
$json=json_encode($jsonArray);
unset($type,$params,$paramArray,$jsonArray);
return $json;
}
// create the XML that will be send to the API
private function XMLPostValue ($paramArray,$action,$params) {
$xml=new SimpleXMLElement(<<<XML
<?xml version='1.0' standalone='yes'?>
<server></server>
XML
);
foreach ($paramArray as $param) {
if (array_key_exists($param,$params)) {
if (is_array($params[$param])) {
foreach ($params[$param] as $val) {
$xml->addChild($param,$val);
}
} else {
$xml->addChild($param,$params[$param]);
}
} else {
$jsonArray[$param]='';
}
}
unset($type,$params,$paramArray);
return $xml;
}
// Method the external script calls
public function makeRestCall($type,$action,$params) {
// some param validation. On fail throw an exception
if (!is_string($type)) {
$this->throwException(2,': $type');
}
if (!is_string($action)) {
$this->throwException(2,': $action');
}
if (!is_array($params)) {
$this->throwException(4,': $params');
}
if (!in_array($type,array('user','gserver','mysql','voice','restart'))) {
$this->throwException('Error: $type is not defined correctly. Allowed methods are (user, gserver, mysql, vserver, restart)');
}
if (!in_array($action,array('mod','add','del','ls','st','re'))) {
$this->throwException('Error: $action is not defined correctly. Allowed methods are (md, ad, dl, st, re, list)');
}
// Array keys that all methods have in common
$generalArray=array('username','user_localid','active');
// Array keys server have in common
$generalServerArray=array('identify_user_by','user_externalid','identify_server_by','server_external_id','server_local_id','master_server_id','master_server_external_id');
// Keys specfic to user
$paramArray['user']=array('identify_by','external_id','localid','email','password');
// Keys specfic to gserver
$paramArray['gserver']=array('private','shorten','slots','primary','taskset','cores','eacallowed','tvenable','pallowed','opt1','opt2','opt3','opt4','opt5','port2','port3','port4','port5','minram','maxram','brandname');
// Keys specfic to voice
$paramArray['voice']=array('private','shorten','slots','max_download_total_bandwidth','max_upload_total_bandwidth','maxtraffic','forcebanner','forcebutton','forceservertag','forcewelcome');
// Keys specfic to mysql
$paramArray['mysql']=array();
// create the post value
if ($this->method=='json') {
$post=$this->JSONPostValue(array_unique(array_merge($generalArray,$generalServerArray,$paramArray[$type])),$action,$params);
} else {
$post=$this->XMLPostValue(array_unique(array_merge($generalArray,$generalServerArray,$paramArray[$type])),$action,$params);
}
// Call method to send the data depending on the connection type
if ($this->connect=='curl' and is_recource($this->handle)) {
$this->execCurl($type,$post);
} else if ($this->connect=='fsockopen' and is_recource($this->handle)) {
fclose($this->handle);
} else {
$this->throwException(10);
}
}
// destructor
__destruct () {
if ($this->connect=='curl' and is_recource($this->handle)) {
curl_close($this->handle);
} else if ($this->connect=='fsockopen' and is_recource($this->handle)) {
fclose($this->handle);
}
unset($method,$timeout,$connect,$user,$pwd,$handle,$ssl,$port,$response);
}
}