forked from caasify/Caasify-WHMCS-Module
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaasify.php
More file actions
207 lines (146 loc) · 4.68 KB
/
caasify.php
File metadata and controls
207 lines (146 loc) · 4.68 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
<?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);
$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 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);