-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcaasify.php
More file actions
313 lines (218 loc) · 7.95 KB
/
caasify.php
File metadata and controls
313 lines (218 loc) · 7.95 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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
use WHMCS\Database\Capsule;
require('init.php');
$admin = caasify_get_session('adminid');
if (!$admin) {
exit('You are not admin.');
}
class CaasifyController
{
const UPDATE_URL = 'https://update.caasify.com';
public function settings()
{
$systemUrl = Capsule::table('tblconfiguration')->where('setting', 'SystemURL')->first();
if (!$systemUrl) {
return $this->jsonMessage('Could not find system URL.');
}
return $this->jsonResponse([
'systemUrl' => $systemUrl->value
]);
}
public function permission()
{
$path = $this->createLocalPath();
$directory = new RecursiveDirectoryIterator($path,
RecursiveDirectoryIterator::SKIP_DOTS);
$iterator = new RecursiveIteratorIterator($directory,
RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $item) {
$item->isDir() ? chmod($item, 0755) : chmod($item, 0644);
}
return $this->jsonMessage('Permission has been updated.');
}
public function update()
{
// Send HTTP GET Request to download zip file
$url = $this->createURL(['whmcs', 'downloadedcaasify.zip']);
$response = $this->sendRequest($url);
if (!$response) {
return $this->jsonMessage('Could not download update.');
}
// Save downloaded zip file to local file
$file = $this->createLocalPath('update.zip');
$success = $this->writeToFile($file, $response);
if (!$success) {
return $this->jsonMessage('Could not write to file.');
}
// Initialize it to handle zip operations
$zip = new ZipArchive();
// Attempt to open zip file
$success = $zip->open($file);
if (!$success) {
return $this->jsonMessage('Could not open file.');
}
// Extract all files from zip to local directory
$extractPath = $this->createLocalPath();
$zip->extractTo($extractPath);
// Read marketplace file
$marketplaceFile = $zip->getFromName('templates/twenty-one/vpspricing.tpl');
// Copy marketplace file to all templates
$templatesPath = $this->createLocalPath('templates/*');
$folders = glob($templatesPath, GLOB_ONLYDIR);
foreach ($folders as $folder) {
$folderName = basename($folder);
$folderPath = $this->createLocalPath("templates/{$folderName}/vpspricing.tpl");
@file_put_contents($folderPath, $marketplaceFile);
}
$zip->close();
// Update was successful
return $this->jsonMessage('The module has been updated.');
}
public function writeToFile($file, $content)
{
return file_put_contents($file, $content);
}
public function createLocalPath($name = null)
{
$currentDirectory = dirname(__FILE__);
if ($name) {
return implode('/', [$currentDirectory, $name]);
}
return $currentDirectory;
}
public function gifts()
{
$gifts = Capsule::table('tblcaasify_gifts')->get();
return $this->jsonResponse(['data' => $gifts]);
}
public function createGift()
{
$params = json_decode(
file_get_contents('php://input'), true);
$name = caasify_get_array('name', $params);
if (empty($name)) {
return $this->jsonMessage('The name field is required');
}
$code = caasify_get_array('code', $params);
if (empty($code)) {
return $this->jsonMessage('The code field is required');
}
$percent = caasify_get_array('percent', $params);
if (empty($percent)) {
return $this->jsonMessage('The percent field is required');
}
$total = caasify_get_array('total', $params);
if (empty($total)) {
return $this->jsonMessage('The total field is required');
}
if (!is_numeric($total)) {
return $this->jsonMessage('The total field must be a number');
}
if (!is_numeric($percent)) {
return $this->jsonMessage('The percent field must be a number');
}
$params = [
'name' => $name, 'code' => $code, 'percent' => $percent, 'total' => $total
];
Capsule::table('tblcaasify_gifts')
->insert($params);
$gift = Capsule::table('tblcaasify_gifts')
->where('code', $code)->first();
return $this->jsonResponse(['data' => $gift]);
}
public function deleteGift()
{
$params = json_decode(
file_get_contents('php://input'), true);
$id = caasify_get_array('id', $params);
if (empty($id)) {
return $this->jsonMessage('The id field is required');
}
$gift = Capsule::table('tblcaasify_gifts')
->where('id', $id)->first();
if (!$gift) {
return $this->jsonMessage('The gift not found');
}
Capsule::table('tblcaasify_gifts')
->where('id', $id)->delete();
return $this->jsonResponse(['data' => $gift]);
}
public function invoices()
{
$invoices = Capsule::select('SELECT c.firstname, c.lastname, a.id, a.ratio, a.chargeamount, a.real_charge_amount, a.commission, a.transactionid, b.id as invoice_id, b.status as invoice_status, y.percent as gift_percent FROM tblcaasify_invoices a INNER JOIN tblinvoices b ON b.id = a.invoiceid INNER JOIN tblclients c ON c.id = a.whuserid LEFT JOIN tblcaasify_gift_invoice x ON x.invoice_id = a.invoiceid LEFT JOIN tblcaasify_gifts y ON y.id = x.gift_id ORDER BY a.id DESC LIMIT 100');
return $this->jsonResponse(['data' => $invoices]);
}
public function latestVersion()
{
$version = $this->getLatestVersion();
if (!$version) {
return $this->jsonMessage('Could not found version.');
}
return $this->jsonResponse(['version' => $version]);
}
public function getLatestVersion()
{
$url = $this->createURL(['whmcs', 'caasifyversion.txt']);
$response = $this->sendRequest($url);
return $response;
}
public function sendRequest($url)
{
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_USERAGENT, 'Caasify');
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
public function jsonMessage($message)
{
return $this->jsonResponse([
'message' => $message
]);
}
public function jsonResponse($data)
{
header('Content-Type: application/json');
echo json_encode($data);
}
public function createURL($segments)
{
array_unshift($segments, static::UPDATE_URL);
return implode('/', $segments);
}
public function localVersion()
{
$version = $this->getLocalVersion();
if (!$version) {
return $this->jsonMessage('Could not found version.');
}
return $this->jsonResponse(['version' => $version]);
}
public function getLocalVersion()
{
$version = $this->readFile('caasifyversion.txt');
return $version;
}
public function readFile($file)
{
$path = dirname(__FILE__) . '/' . $file;
if (file_exists($path)) {
return file_get_contents($path);
}
throw new Exception('Could not find file');
}
public function handle($action)
{
$class = new ReflectionClass($this);
$method = $class->getMethod($action);
if ($method) {
return $method->invoke($this);
}
}
}
$action = caasify_get_query('action');
$controller = new CaasifyController();
$controller->handle($action);