This repository has been archived by the owner on Nov 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ispapi_account.php
231 lines (217 loc) · 8.18 KB
/
ispapi_account.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
<?php
/**
* WHMCS ISPAPI Account Dashboard Widget
*
* This Widget allows to display your account balance.
*
* @see https://github.com/hexonet/whmcs-ispapi-widget-account/wiki/
*
* @copyright Copyright (c) Kai Schwarz, HEXONET GmbH, 2019
* @license https://github.com/hexonet/whmcs-ispapi-widget-account/blob/master/LICENSE/ MIT License
*/
// phpcs:disable PSR1.Classes.ClassDeclaration.MultipleClasses
namespace WHMCS\Module\Widget;
use WHMCS\Database\Capsule;
use PDO;
use WHMCS\Module\Registrar\Ispapi\Ispapi;
use WHMCS\Module\Registrar\Ispapi\Helper;
add_hook('AdminHomeWidgets', 1, function () {
return new IspapiAccountWidget();
});
/**
* ISPAPI Account Widget.
*/
class IspapiAccountWidget extends \WHMCS\Module\AbstractWidget
{
protected $title = 'HEXONET ISPAPI Account Overview';
protected $description = '';
protected $weight = 150;
protected $columns = 1;
protected $cache = false;
protected $cacheExpiry = 120;
protected $requiredPermission = '';
protected $currencies = null;
const VERSION = "3.0.3";
/**
* load configured currencies
* @return array assoc array, list of currencies where currency code is array key
*/
private function getCurencies()
{
$results = localAPI('GetCurrencies', array());
$currencies = array();
if ($results["result"] == "success") {
foreach ($results["currencies"]["currency"] as $idx => $d) {
$currencies[$d["code"]] = $d;
}
}
return $currencies;
}
/**
* get currency id of a currency identified by given currency code
* @param string $currency currency code
* @return null|int currency id or null if currency is not configured
*/
private function getCurrencyId($currency)
{
if (is_null($this->currencies)) {
$this->currencies = $this->getCurencies();
}
if (!isset($this->currencies[$currency])) {
return null;
}
return $this->currencies[$currency]["id"];
}
/**
* get account status from HEXONET API
* @return array|null account balance data or null in case of an error
*/
private function getAccountStatus()
{
$r = Ispapi::call(array('COMMAND' => 'StatusAccount'));
if ($r["CODE"] != "200") {
return null;
}
$balance = array();
foreach ($r["PROPERTY"] as $key => $val) {
$balance[$key] = $val[0];
}
return $balance;
}
/**
* get object statistics
* @return array|null object statistics or null in case of an error
*/
private function getObjectStatistics()
{
$stats = array();
$r = Ispapi::call(array('COMMAND' => 'QueryUserObjectStatistics'));
if ($r["CODE"] != "200" || empty($r["PROPERTY"])) {
return null;
}
foreach ($r["PROPERTY"] as $key => $val) {
$stats[$key] = $val[0];
}
return $stats;
}
/**
* return html code for error case specified by given error message
* @param string $errMsg error message to show
* @return string html code
*/
private function returnError($errMsg)
{
return <<<EOF
<div class="widget-content-padded widget-billing">
<div class="color-pink">$errMsg</div>
</div>
EOF;
}
/**
* get product title for a given product id
* @param string $productid product id
* @return string product title if found, product id otherwise
*/
private function getProductTitle($productid)
{
$map = array(
"DOMAIN" => "Domains",
"DNSZONE" => "DNS Zones",
"MANAGEDDOMAIN" => "Domains (RegOC)",
"SSLCERT" => "SSL Certs"
);
return isset($map, $productid) ? $map[$productid] : $productid;
}
/**
* Fetch data that will be provided to generateOutput method
* @return array|null data array or null in case of an error
*/
public function getData()
{
if (!class_exists(Helper::class)) {
return null;
}
return array(
"balance" => $this->getAccountStatus(),
"stats" => $this->getObjectStatistics(),
"currencies" => $this->getCurencies()
);
}
/**
* generate widget's html output
* @param array $data input data (from getData method)
* @return string html code
*/
public function generateOutput($data)
{
$logo_url = "https://raw.githubusercontent.com/hexonet/whmcs-ispapi-registrar/master/registrars/ispapi/logo.png";
if (is_null($data)) {
$git_url = "https://github.com/hexonet/whmcs-ispapi-registrar";
return $this->returnError(
'Please install and activate the HEXONET ISPAPI Registrar Module v1.7.1 or higher. ' .
'<span data-toggle="tooltip" title="The HEXONET ISPAPI Registrar Module is regularly maintained, ' .
'download and documentation available at github." class="glyphicon glyphicon-question-sign"></span><br/>' .
'<a href="' . $git_url . '">' .
'<img src="' . $logo_url . '" width="125" height="40"/>' .
'</a>'
);
}
$balance = $data["balance"];
if (is_null($balance)) {
return $this->returnError('Connection issue. Check registrar module configuration.');
}
$amount = floatval($balance["AMOUNT"]);
$currency = $balance["CURRENCY"];
$currencyid = $this->getCurrencyId($currency);
$deposit = floatval($balance["DEPOSIT"]);
$fundsav = $amount - $deposit;
if (is_null($currencyid)) {
return $this->returnError('Please configure currency <a href="configcurrencies.php">"' . $currency . '"</a>.');
}
$stats = false;
if (!is_null($data["stats"])) {
ksort($data["stats"]);
$stats = '<div class="row">';
foreach ($data["stats"] as $key => $val) {
if (!preg_match("/_/", $key)) {
$stats .= (
'<div class="col-xs-9 col-sm-8">' . $this->getProductTitle($key) . '</div>' .
'<div class="col-xs-3 col-sm-4 text-right">' . $val . '</div>'
);
}
}
$stats .= '</div>';
}
return (
'<div class="widget-billing">' .
'<div class="row" style="display: flex; flex-wrap: wrap; align-items: center;">' .
'<div class="col-sm-6 bordered-right">' .
'<div class="item text-right">' .
'<div class="data color-' . ($amount >= 0 ? "green" : "pink") . '">' .
formatCurrency($amount, $currencyid) .
'</div>' .
'<div class="note">Account Balance</div>' .
'</div>' .
(($deposit > 0) ?
'<div class="item text-right">' .
'<div class="data color-pink">- ' .
formatCurrency($deposit, $currencyid) .
'</div>' .
'<div class="note" data-toggle="tooltip" title="Deposits are automatically withdrawn from your account balance to cover impending backorders and will be returned if a backorder registration is unsuccessful.">Reserved Deposits <span class="glyphicon glyphicon-question-sign"></span></div>' .
'</div>' .
'<div class="item bordered-top text-right">' .
'<div class="data color-' . ($amount >= 0 ? "green" : "pink") . '">' .
formatCurrency($fundsav, $currencyid) .
'</div>' .
'<div class="note">Available Funds</div>' .
'</div>' : ''
) .
'</div>' .
'<div class="col-sm-6 bordered-right">' .
($stats ? $stats : '<div class="text-center"><img src="' . $logo_url . '" width="125" height="40"/></div>') .
'</div>' .
'</div>' .
'</div>'
);
}
}