1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BPT\tools; |
4
|
|
|
use stdClass; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Simple class for working with cpanel functions and staff |
8
|
|
|
*/ |
9
|
|
|
class cpanel { |
10
|
|
|
private static string $cpanelUser; |
11
|
|
|
private static string $cpanelPassword; |
12
|
|
|
private static string $cpanelUrl; |
13
|
|
|
private static int $cpanelPort; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Use this method and fill all arguments with right value for using this class |
17
|
|
|
* |
18
|
|
|
* @param string $cpanelUser |
19
|
|
|
* @param string $cpanelPassword |
20
|
|
|
* @param string $cpanelUrl |
21
|
|
|
* @param int $cpanelPort |
22
|
|
|
* |
23
|
|
|
* @return void |
24
|
|
|
*/ |
25
|
|
|
public static function init (string $cpanelUser, string $cpanelPassword, string $cpanelUrl = '127.0.0.1', int $cpanelPort = 2083): void { |
26
|
|
|
self::$cpanelUser = $cpanelUser; |
27
|
|
|
self::$cpanelPassword = $cpanelPassword; |
28
|
|
|
self::$cpanelUrl = $cpanelUrl; |
29
|
|
|
self::$cpanelPort = $cpanelPort; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
private static function createCurl () { |
33
|
|
|
$curl = curl_init(); |
34
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); |
35
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); |
36
|
|
|
curl_setopt($curl, CURLOPT_HEADER, 0); |
37
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); |
38
|
|
|
$header[0] = 'Authorization: Basic ' . base64_encode(self::$cpanelUser . ':' . self::$cpanelPassword) . "\n\r"; |
|
|
|
|
39
|
|
|
curl_setopt($curl, CURLOPT_HTTPHEADER, $header); |
40
|
|
|
return $curl; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
private static function execute (string $query = ''): bool|stdClass|array { |
44
|
|
|
$curl = self::createCurl(); |
45
|
|
|
curl_setopt($curl, CURLOPT_URL, 'https://' . self::$cpanelUrl . ':' . self::$cpanelPort . '/execute/' . $query); |
46
|
|
|
$result = curl_exec($curl); |
47
|
|
|
if (!$result) { |
48
|
|
|
error_log('curl_exec threw error `' . curl_error($curl) . "` for $query"); |
49
|
|
|
} |
50
|
|
|
curl_close($curl); |
51
|
|
|
$result = json_decode($result); |
|
|
|
|
52
|
|
|
if (!$result->status) { |
53
|
|
|
return $result->errors; |
54
|
|
|
} |
55
|
|
|
return $result->data ?? true; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
private static function executev2 (string $module, string $function, array $vars = []) { |
|
|
|
|
59
|
|
|
$curl = self::createCurl(); |
60
|
|
|
$vars = array_merge([ |
61
|
|
|
'cpanel_jsonapi_user' => 'user', |
62
|
|
|
'cpanel_jsonapi_apiversion' => '2', |
63
|
|
|
'cpanel_jsonapi_module' => $module, |
64
|
|
|
'cpanel_jsonapi_func' => $function, |
65
|
|
|
], $vars); |
66
|
|
|
curl_setopt($curl, CURLOPT_URL, 'https://' . self::$cpanelUrl . ':' . self::$cpanelPort . '/json-api/cpanel?' . http_build_query($vars)); |
67
|
|
|
$result = curl_exec($curl); |
68
|
|
|
if (!$result) { |
69
|
|
|
error_log('curl_exec threw error `' . curl_error($curl) . '` for ' . json_encode($vars)); |
70
|
|
|
} |
71
|
|
|
curl_close($curl); |
72
|
|
|
return $result; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $database |
77
|
|
|
* @param string $user |
78
|
|
|
* @param string $password |
79
|
|
|
* @param array $privileges |
80
|
|
|
* |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
public static function mysqlWizard (string $database, string $user, string $password, array $privileges = []): array { |
84
|
|
|
$create_database = self::createMysqlDatabase($database); |
85
|
|
|
$create_user = self::createMysqlUser($user, $password); |
86
|
|
|
if (empty($privileges)) { |
87
|
|
|
$set_privileges = self::setMysqlPrivilegesAll($database, $user); |
88
|
|
|
} |
89
|
|
|
else { |
90
|
|
|
$set_privileges = self::setMysqlPrivileges($database, $user, $privileges); |
91
|
|
|
} |
92
|
|
|
return [ |
93
|
|
|
'create_database' => $create_database, |
94
|
|
|
'create_user' => $create_user, |
95
|
|
|
'set_privileges' => $set_privileges, |
96
|
|
|
]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $database |
101
|
|
|
* |
102
|
|
|
* @return bool|array|stdClass |
103
|
|
|
*/ |
104
|
|
|
public static function createMysqlDatabase (string $database): bool|array|stdClass { |
105
|
|
|
if (!str_starts_with($database, self::$cpanelUser)) { |
106
|
|
|
$database = self::$cpanelUser . '_' . $database; |
107
|
|
|
} |
108
|
|
|
return self::execute("Mysql/create_database?name=$database"); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param string $database |
113
|
|
|
* |
114
|
|
|
* @return bool|array|stdClass |
115
|
|
|
*/ |
116
|
|
|
public static function deleteMysqlDatabase (string $database): bool|array|stdClass { |
117
|
|
|
if (!str_starts_with($database, self::$cpanelUser)) { |
118
|
|
|
$database = self::$cpanelUser . '_' . $database; |
119
|
|
|
} |
120
|
|
|
return self::execute("Mysql/delete_database?name=$database"); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param string $user |
125
|
|
|
* @param string $password |
126
|
|
|
* |
127
|
|
|
* @return bool|array|stdClass |
128
|
|
|
*/ |
129
|
|
|
public static function createMysqlUser (string $user, string $password): bool|array|stdClass { |
130
|
|
|
if (!str_starts_with($user, self::$cpanelUser)) { |
131
|
|
|
$user = self::$cpanelUser . '_' . $user; |
132
|
|
|
} |
133
|
|
|
return self::execute("Mysql/create_user?name=$user&password=$password"); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string $user |
138
|
|
|
* |
139
|
|
|
* @return bool|array|stdClass |
140
|
|
|
*/ |
141
|
|
|
public static function deleteMysqlUser (string $user): bool|array|stdClass { |
142
|
|
|
if (!str_starts_with($user, self::$cpanelUser)) { |
143
|
|
|
$user = self::$cpanelUser . '_' . $user; |
144
|
|
|
} |
145
|
|
|
return self::execute("Mysql/delete_user?name=$user"); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param string $database |
150
|
|
|
* @param string $user |
151
|
|
|
* @param array $privileges |
152
|
|
|
* |
153
|
|
|
* @return bool|array|stdClass |
154
|
|
|
*/ |
155
|
|
|
public static function setMysqlPrivileges (string $database, string $user, array $privileges): bool|array|stdClass { |
156
|
|
|
if (!str_starts_with($database, self::$cpanelUser)) { |
157
|
|
|
$database = self::$cpanelUser . '_' . $database; |
158
|
|
|
} |
159
|
|
|
if (!str_starts_with($user, self::$cpanelUser)) { |
160
|
|
|
$user = self::$cpanelUser . '_' . $user; |
161
|
|
|
} |
162
|
|
|
$all_privileges = [ |
163
|
|
|
'ALTER', |
164
|
|
|
'ALTER ROUTINE', |
165
|
|
|
'CREATE', |
166
|
|
|
'CREATE ROUTINE', |
167
|
|
|
'CREATE TEMPORARY TABLES', |
168
|
|
|
'CREATE VIEW', |
169
|
|
|
'DELETE', |
170
|
|
|
'DROP', |
171
|
|
|
'EVENT', |
172
|
|
|
'EXECUTE', |
173
|
|
|
'INDEX', |
174
|
|
|
'INSERT', |
175
|
|
|
'LOCK TABLES', |
176
|
|
|
'REFERENCES', |
177
|
|
|
'SELECT', |
178
|
|
|
'SHOW VIEW', |
179
|
|
|
'TRIGGER', |
180
|
|
|
'UPDATE', |
181
|
|
|
]; |
182
|
|
|
$privileges = array_intersect($all_privileges, $privileges); |
183
|
|
|
if (empty($privileges)) { |
184
|
|
|
return false; |
185
|
|
|
} |
186
|
|
|
$privileges = urlencode(implode(',', $privileges)); |
187
|
|
|
return self::execute("Mysql/set_privileges_on_database?user=$user&database=$database&privileges=$privileges"); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param string $database |
192
|
|
|
* @param string $user |
193
|
|
|
* |
194
|
|
|
* @return bool|array|stdClass |
195
|
|
|
*/ |
196
|
|
|
public static function setMysqlPrivilegesAll (string $database, string $user): bool|array|stdClass { |
197
|
|
|
if (!str_starts_with($database, self::$cpanelUser)) { |
198
|
|
|
$database = self::$cpanelUser . '_' . $database; |
199
|
|
|
} |
200
|
|
|
if (!str_starts_with($user, self::$cpanelUser)) { |
201
|
|
|
$user = self::$cpanelUser . '_' . $user; |
202
|
|
|
} |
203
|
|
|
return self::execute("Mysql/set_privileges_on_database?user=$user&database=$database&privileges=ALL"); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param string $old_name |
208
|
|
|
* @param string $new_name |
209
|
|
|
* |
210
|
|
|
* @return bool|array|stdClass |
211
|
|
|
*/ |
212
|
|
|
public static function changeMysqlDatabaseName (string $old_name, string $new_name): bool|array|stdClass { |
213
|
|
|
if (!str_starts_with($old_name, self::$cpanelUser)) { |
214
|
|
|
$old_name = self::$cpanelUser . '_' . $old_name; |
215
|
|
|
} |
216
|
|
|
if (!str_starts_with($new_name, self::$cpanelUser)) { |
217
|
|
|
$new_name = self::$cpanelUser . '_' . $new_name; |
218
|
|
|
} |
219
|
|
|
return self::execute("Mysql/rename_database?oldname=$old_name&newname=$new_name"); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @param string $old_name |
224
|
|
|
* @param string $new_name |
225
|
|
|
* |
226
|
|
|
* @return bool|array|stdClass |
227
|
|
|
*/ |
228
|
|
|
public static function changeMysqlUserName (string $old_name, string $new_name): bool|array|stdClass { |
229
|
|
|
if (!str_starts_with($old_name, self::$cpanelUser)) { |
230
|
|
|
$old_name = self::$cpanelUser . '_' . $old_name; |
231
|
|
|
} |
232
|
|
|
if (!str_starts_with($new_name, self::$cpanelUser)) { |
233
|
|
|
$new_name = self::$cpanelUser . '_' . $new_name; |
234
|
|
|
} |
235
|
|
|
return self::execute("Mysql/rename_user?oldname=$old_name&newname=$new_name"); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @param string $database |
240
|
|
|
* |
241
|
|
|
* @return bool|array|stdClass |
242
|
|
|
*/ |
243
|
|
|
public static function dumpMysqlDatabaseSchema (string $database): bool|array|stdClass { |
244
|
|
|
if (!str_starts_with($database, self::$cpanelUser)) { |
245
|
|
|
$database = self::$cpanelUser . '_' . $database; |
246
|
|
|
} |
247
|
|
|
return self::execute("Mysql/dump_database_schema?dbname=$database"); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @return bool|array|stdClass |
252
|
|
|
*/ |
253
|
|
|
public static function mysqlDatabases (): bool|array|stdClass { |
254
|
|
|
return self::execute('Mysql/list_databases'); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @return bool|array|stdClass |
259
|
|
|
*/ |
260
|
|
|
public static function mysqlUsers (): bool|array|stdClass { |
261
|
|
|
return self::execute('Mysql/list_users'); |
262
|
|
|
} |
263
|
|
|
} |