This repository has been archived by the owner on Oct 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathclass.api.php
153 lines (130 loc) · 5.29 KB
/
class.api.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
<?php
if (!class_exists('RadWebHosting_API')) {
class RadWebHosting_API {
protected $_ch = null;
protected $_timeout = 40;
protected $_response = null;
protected $_url = null;
protected $_email = null;
protected $_key = null;
protected $_debug = true;
protected $_domainid = null;
public function __construct( $email, $key) {
$this->_url = "https://api.radwebhosting.com/index.php";
$this->_email = $email;
$this->_key = $key;
}
protected function getSecretApiVars(){
return array('key','transfersecret');
}
public function setDebugMode($mode = true) {
$this->_debug = $mode;
}
public function getError() {
return $this->_error;
}
public function getResponse() {
return $this->_response;
}
public function getDomainID()
{
return $this->_domainid;
}
public function setTimeout($timeout) {
$this->_timeout = $timeout;
}
/**
*
* @param string $method POST|GET|PUT|DELETE
* @param array $postdata
* @return array|bool json_decode
*/
public function simpleCall($method, array $postdata = array()) {
$this->_error = null;
$alowed_methods = array('GET', 'POST', 'PUT', 'DELETE');
if (!in_array($method, $alowed_methods)) {
$this->_error = 'Wrong request method.';
return false;
}
if (!$this->_email || !$this->_key || !$this->_url) {
$this->_error = 'Wrong reseller API configuration';
return false;
}
$postdata['token'] = $this->_key;
$postdata['authemail'] = $this->_email;
$data = $postdata;
$this->_ch = curl_init();
curl_setopt($this->_ch, CURLOPT_URL, $this->_url);
curl_setopt($this->_ch, CURLOPT_TIMEOUT, $this->_timeout);
curl_setopt($this->_ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($this->_ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($this->_ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->_ch, CURLOPT_HEADER, true);
curl_setopt($this->_ch, CURLINFO_HEADER_OUT, 1);
if(version_compare(phpversion(), '5.5.19', '<=')) {
curl_setopt($this->_ch, CURLOPT_SSLVERSION, 4);
}
$result = array();
$result['response_body'] = curl_exec($this->_ch);
$result['info'] = curl_getinfo($this->_ch);
$curlHeaderSize = $result['info']['header_size'];
$result['headers'] = substr($result['response_body'], 0, $curlHeaderSize);
$result['response_body'] = substr($result['response_body'], $curlHeaderSize);
$this->_response = $result;
//echo $this->_response['response_body'];die();
$res = json_decode($this->_response['response_body'], true);
if(is_string($this->_response['response_body']) && empty($res)){
$this->_error = $this->_response['response_body'];
return;
}
// adds log
if ($this->_debug && !empty($res)){
$this->logModuleCall($postdata['action'], $postdata, print_r($res, true));
}
if (empty($res)){
$curlErr = curl_error($this->_ch);
$this->_error = $curlErr ? $curlErr : 'Response body is empty for method: ' . $method;
return false;
} else {
if (!empty($res['errors'])){
$this->_error = implode(' ', $res['errors']);
return false;
}
if(!empty($res['result']) && $res['result']=='error'){
if(!empty($res['domainid']))
{
$this->_domainid = $res['domainid'];
}
$this->_error = $res['msg'];
return false;
}
return $res;
}
}
/**
* WHMCS Module Log call
* @param string $method
* @param string $resource
* @param array $params
* @param string $response
* @return type
*/
private function logModuleCall($action, array $params, $response) {
if (function_exists('logModuleCall')) {
// hide some values
foreach ($this->getSecretApiVars() as $key){
if (isset($params[$key]))
$params[$key] = substr($params[$key], 0, 3) . '...';
}
return logModuleCall(
'RadWebHosting',
$action,
print_r($params, true),
'',
$response
);
}
}
}
}