Módulo VPSManager para WHMCS con configuración externa, acceso seguro al panel y métricas Graphite para LX.
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

623 wiersze
16 KiB

  1. <?php
  2. /**
  3. * WHMCS VPSManager Module
  4. *
  5. */
  6. if (!defined('WHMCS')) {
  7. http_response_code(403);
  8. exit;
  9. }
  10. require_once __DIR__ . '/lib/VPSManager.php';
  11. require_once __DIR__ . '/lib/ClientView.php';
  12. require_once __DIR__ . '/lib/Backups.php';
  13. require_once __DIR__ . '/WhmcsHelpers/CustomField.php';
  14. use VPSManager\VPSManager;
  15. use WHMCS\Database\Capsule;
  16. use VPSManager\WhmcsHelpers\CustomField;
  17. function vpsmanager_Metadata() {
  18. return array(
  19. 'DisplayName' => 'o6h VPSManager Module',
  20. 'APIVersion' => '1.1',
  21. 'RequiresServer' => true,
  22. 'DefaultNonSSLPort' => '8080',
  23. 'DefaultSSLPort' => '8080',
  24. );
  25. }
  26. function vpsmanager_ConfigOptions( $params ) {
  27. $vpsmanager = new VPSManager();
  28. try {
  29. $nodes = $vpsmanager->apiCall('nodes');
  30. $packs = $vpsmanager->apiCall('packs');
  31. } catch (\Throwable $e) {
  32. $nodes = [];
  33. $packs = [];
  34. }
  35. return [
  36. 'VPSManager Remote Username' => [
  37. 'Type' => 'text',
  38. 'Size' => '16',
  39. 'Description' => 'Remote Username configured in VPSManager.'
  40. ],
  41. 'VPSManager Remote Password' => [
  42. 'Type' => 'password',
  43. 'Size' => '16',
  44. 'Description' => 'Remote Password configured in VPSManager.'
  45. ],
  46. 'VPSManager URL' => [
  47. 'Type' => 'text',
  48. 'Size' => '32',
  49. 'Description' => 'E.g. vpsmanager.example.tld:8080'
  50. ],
  51. 'VPSManager SSL' => [
  52. 'Type' => 'yesno',
  53. 'Description' => 'Tick if you enabled SSL on your VPSManager'
  54. . ' Controlpanel Web Interface.'
  55. ],
  56. 'Nodo' => [
  57. 'Type' => 'dropdown',
  58. 'Options' => $nodes,
  59. 'SimpleMode' => false,
  60. ],
  61. 'Pack' => [
  62. 'Type' => "dropdown", # Dropdown Choice of Options
  63. 'Options' => $packs
  64. ],
  65. ];
  66. }
  67. function vpsmanager_CreateAccount( $params ) {
  68. return 'success';
  69. }
  70. function vpsmanager_TerminateAccount( $params ) {
  71. }
  72. function vpsmanager_ChangePackage( $params ) {
  73. }
  74. function vpsmanager_SuspendAccount( $params )
  75. {
  76. return vpsmanager_m_suspend($params);
  77. }
  78. function vpsmanager_UnsuspendAccount( $params )
  79. {
  80. return vpsmanager_m_unsuspend($params);
  81. }
  82. function vpsmanager_ChangePassword( $params ) {
  83. }
  84. function vpsmanager_ClientAreaCustomButtonArray(array $params = [])
  85. {
  86. $buttons = array(
  87. "Reiniciar" => "Reiniciar",
  88. "Detener" => "Detener",
  89. "Iniciar" => "Iniciar",
  90. "Firewall" => "Firewall",
  91. "Snapshots" => "Snapshots",
  92. );
  93. if (\VPSManager\Backups::button($params)) $buttons["Backups"] = "Backups";
  94. return $buttons;
  95. }
  96. function vpsmanager_Backups(array $params)
  97. {
  98. return \VPSManager\Backups::page($params);
  99. }
  100. function vpsmanager_Firewall(array $params)
  101. {
  102. if (isset($_POST["a"])) {
  103. return vpsmanager_m_firewall($params);
  104. }
  105. return [
  106. 'templatefile' => 'templates/firewall',
  107. 'vars' => [
  108. 'action' => 'Firewall',
  109. 'selfip' => ($_SERVER['REMOTE_ADDR'] ?? '')
  110. ]
  111. ];
  112. }
  113. function vpsmanager_Reiniciar(array $params)
  114. {
  115. if (isset($_POST["a"])) {
  116. return vpsmanager_m_reboot($params);
  117. }
  118. return [
  119. 'templatefile' => 'templates/alert',
  120. 'vars' => [
  121. 'action' => 'Reiniciar',
  122. 'description' => 'reiniciar el servidor'
  123. ]
  124. ];
  125. }
  126. function vpsmanager_Detener(array $params)
  127. {
  128. if (isset($_POST["a"])) {
  129. return vpsmanager_m_shutdown($params);
  130. }
  131. return [
  132. 'templatefile' => 'templates/alert',
  133. 'vars' => [
  134. 'action' => 'Detener',
  135. 'description' => 'detener el servidor'
  136. ]
  137. ];
  138. }
  139. function vpsmanager_Iniciar(array $params)
  140. {
  141. if (isset($_POST["a"])) {
  142. return vpsmanager_m_boot($params);
  143. }
  144. return [
  145. 'templatefile' => 'templates/alert',
  146. 'vars' => [
  147. 'action' => 'Iniciar',
  148. 'description' => 'iniciar el servidor'
  149. ]
  150. ];
  151. }
  152. function vpsmanager_Snapshots(array $params)
  153. {
  154. // si recibimos un post lo procesamos en la otra funcion
  155. if (isset($_POST["a"])) {
  156. return vpsmanager_m_snapshots($params);
  157. }
  158. $datos = $params['customfields'];
  159. $gzid = $datos['gzid'];
  160. $uuid = $datos['uuid'];
  161. $serverhostname = $params['domain'];
  162. // hacemos la llamada para popular la tabla de snapshots
  163. try {
  164. $globalurl = \VPSManager\Config::url(\VPSManager\Config::load(), 'vpsmanager_api_url', false);
  165. if (!function_exists('curl_init')) {
  166. return 'Transporte del módulo no disponible.';
  167. }
  168. } catch (\Throwable $e) {
  169. return 'Configuración del módulo no disponible.';
  170. }
  171. $url = $globalurl . '/getsnap';
  172. $ch = curl_init($url);
  173. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  174. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  175. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  176. //setup request to send json via POST
  177. $data = array(
  178. 'gzid' => $gzid,
  179. 'uuid' => $uuid
  180. );
  181. $payload = json_encode($data);
  182. curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
  183. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  184. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  185. $result = curl_exec($ch);
  186. //Decode Json
  187. $datos = json_decode($result,true);
  188. curl_close($ch);
  189. return [
  190. 'templatefile' => 'templates/snapshots',
  191. 'vars' => [
  192. 'action' => 'Snapshots',
  193. 'selfip' => ($_SERVER['REMOTE_ADDR'] ?? ''),
  194. 'uuid' => $uuid,
  195. 'serverhostname' => $serverhostname,
  196. 'size' => $datos['size'] ?? '',
  197. 'date' => $datos['date'] ?? ''
  198. ]
  199. ];
  200. }
  201. function vpsmanager_m_snapshots(array $params)
  202. {
  203. if (isset($_POST["abort"])) {
  204. return "operacion cancelada por el usuario";
  205. }
  206. $datos = $params['customfields'];
  207. $gzid = $datos['gzid'];
  208. $uuid = $datos['uuid'];
  209. try {
  210. $globalurl = \VPSManager\Config::url(\VPSManager\Config::load(), 'vpsmanager_api_url', false);
  211. if (!function_exists('curl_init')) {
  212. return 'Transporte del módulo no disponible.';
  213. }
  214. } catch (\Throwable $e) {
  215. return 'Configuración del módulo no disponible.';
  216. }
  217. $url = $globalurl . '/restoresnap';
  218. //create a new cURL resource
  219. $ch = curl_init($url);
  220. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  221. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  222. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  223. //setup request to send json via POST
  224. $data = array(
  225. 'gzid' => $gzid,
  226. 'uuid' => $uuid
  227. );
  228. $payload = json_encode($data);
  229. //attach encoded JSON string to the POST fields
  230. curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
  231. //set the content type to application/json
  232. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
  233. //execute the POST request
  234. $result = curl_exec($ch);
  235. //close cURL resource
  236. curl_close($ch);
  237. return 'success';
  238. }
  239. function vpsmanager_m_reboot(array $params)
  240. {
  241. if (isset($_POST["abort"])) {
  242. return "Operacion cancelada por el usuario";
  243. }
  244. $datos = $params['customfields'];
  245. $gzid = $datos['gzid'];
  246. $uuid = $datos['uuid'];
  247. try {
  248. $globalurl = \VPSManager\Config::url(\VPSManager\Config::load(), 'vpsmanager_api_url', false);
  249. if (!function_exists('curl_init')) {
  250. return 'Transporte del módulo no disponible.';
  251. }
  252. } catch (\Throwable $e) {
  253. return 'Configuración del módulo no disponible.';
  254. }
  255. $url = $globalurl . '/command/reboot';
  256. //create a new cURL resource
  257. $ch = curl_init($url);
  258. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  259. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  260. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  261. //setup request to send json via POST
  262. $data = array(
  263. 'gzid' => $gzid,
  264. 'uuid' => $uuid
  265. );
  266. $payload = json_encode($data);
  267. //attach encoded JSON string to the POST fields
  268. curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
  269. //set the content type to application/json
  270. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  271. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  272. //execute the POST request
  273. $result = curl_exec($ch);
  274. //close cURL resource
  275. curl_close($ch);
  276. return 'success';
  277. }
  278. function vpsmanager_m_shutdown(array $params)
  279. {
  280. if (isset($_POST["abort"])) {
  281. return "operacion cancelada por el usuario";
  282. }
  283. $datos = $params['customfields'];
  284. $gzid = $datos['gzid'];
  285. $uuid = $datos['uuid'];
  286. try {
  287. $globalurl = \VPSManager\Config::url(\VPSManager\Config::load(), 'vpsmanager_api_url', false);
  288. if (!function_exists('curl_init')) {
  289. return 'Transporte del módulo no disponible.';
  290. }
  291. } catch (\Throwable $e) {
  292. return 'Configuración del módulo no disponible.';
  293. }
  294. $url = $globalurl . '/command/shutdown';
  295. //create a new cURL resource
  296. $ch = curl_init($url);
  297. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  298. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  299. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  300. //setup request to send json via POST
  301. $data = array(
  302. 'gzid' => $gzid,
  303. 'uuid' => $uuid
  304. );
  305. $payload = json_encode($data);
  306. //attach encoded JSON string to the POST fields
  307. curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
  308. //set the content type to application/json
  309. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
  310. //execute the POST request
  311. $result = curl_exec($ch);
  312. //close cURL resource
  313. curl_close($ch);
  314. return 'success';
  315. }
  316. function vpsmanager_m_firewall(array $params)
  317. {
  318. if (isset($_POST["abort"])) {
  319. return "operacion cancelada por el usuario";
  320. }
  321. try {
  322. $globalurl = \VPSManager\Config::url(\VPSManager\Config::load(), 'vpsmanager_api_url', false);
  323. if (!function_exists('curl_init')) {
  324. return 'Transporte del módulo no disponible.';
  325. }
  326. } catch (\Throwable $e) {
  327. return 'Configuración del módulo no disponible.';
  328. }
  329. $datos = $params['customfields'];
  330. $gzid = $datos['gzid'];
  331. $uuid = $datos['uuid'];
  332. $ip = $_POST["ip"] ?? '';
  333. if (!is_string($ip) || !filter_var($ip, FILTER_VALIDATE_IP)) {
  334. return 'Dirección IP no válida.';
  335. }
  336. if ( ($_POST["proceed"] ?? null) == "Desbloquear" ) {
  337. $url = $globalurl . '/unblock';
  338. } elseif ( ($_POST["proceed"] ?? null) == "Bloquear" ) {
  339. $url = $globalurl . '/block';
  340. } else {
  341. return 'Acción de firewall no válida.';
  342. }
  343. //create a new cURL resource
  344. $ch = curl_init($url);
  345. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  346. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  347. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  348. //setup request to send json via POST
  349. $data = array(
  350. 'gzid' => $gzid,
  351. 'uuid' => $uuid,
  352. 'ip' => $ip
  353. );
  354. $payload = json_encode($data);
  355. //attach encoded JSON string to the POST fields
  356. curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
  357. //set the content type to application/json
  358. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
  359. //execute the POST request
  360. $result = curl_exec($ch);
  361. //close cURL resource
  362. curl_close($ch);
  363. return 'success';
  364. }
  365. function vpsmanager_m_boot(array $params)
  366. {
  367. if (isset($_POST["abort"])) {
  368. return "operacion cancelada por el usuario";
  369. }
  370. $datos = $params['customfields'];
  371. $gzid = $datos['gzid'];
  372. $uuid = $datos['uuid'];
  373. try {
  374. $globalurl = \VPSManager\Config::url(\VPSManager\Config::load(), 'vpsmanager_api_url', false);
  375. if (!function_exists('curl_init')) {
  376. return 'Transporte del módulo no disponible.';
  377. }
  378. } catch (\Throwable $e) {
  379. return 'Configuración del módulo no disponible.';
  380. }
  381. $url = $globalurl . '/command/boot';
  382. //create a new cURL resource
  383. $ch = curl_init($url);
  384. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  385. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  386. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  387. //setup request to send json via POST
  388. $data = array(
  389. 'gzid' => $gzid,
  390. 'uuid' => $uuid
  391. );
  392. $payload = json_encode($data);
  393. //attach encoded JSON string to the POST fields
  394. curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
  395. //set the content type to application/json
  396. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
  397. //execute the POST request
  398. $result = curl_exec($ch);
  399. //close cURL resource
  400. curl_close($ch);
  401. return 'success';
  402. }
  403. function vpsmanager_m_suspend(array $params)
  404. {
  405. $datos = $params['customfields'];
  406. $gzid = $datos['gzid'];
  407. $uuid = $datos['uuid'];
  408. try {
  409. $globalurl = \VPSManager\Config::url(\VPSManager\Config::load(), 'vpsmanager_api_url', false);
  410. if (!function_exists('curl_init')) {
  411. return 'Transporte del módulo no disponible.';
  412. }
  413. } catch (\Throwable $e) {
  414. return 'Configuración del módulo no disponible.';
  415. }
  416. $url = $globalurl . '/suspend';
  417. //create a new cURL resource
  418. $ch = curl_init($url);
  419. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  420. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  421. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  422. //setup request to send json via POST
  423. $data = array(
  424. 'gzid' => $gzid,
  425. 'uuid' => $uuid
  426. );
  427. $payload = json_encode($data);
  428. //attach encoded JSON string to the POST fields
  429. curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
  430. //set the content type to application/json
  431. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
  432. //execute the POST request
  433. $result = curl_exec($ch);
  434. //close cURL resource
  435. curl_close($ch);
  436. return 'success';
  437. }
  438. function vpsmanager_m_unsuspend(array $params)
  439. {
  440. $datos = $params['customfields'];
  441. $gzid = $datos['gzid'];
  442. $uuid = $datos['uuid'];
  443. try {
  444. $globalurl = \VPSManager\Config::url(\VPSManager\Config::load(), 'vpsmanager_api_url', false);
  445. if (!function_exists('curl_init')) {
  446. return 'Transporte del módulo no disponible.';
  447. }
  448. } catch (\Throwable $e) {
  449. return 'Configuración del módulo no disponible.';
  450. }
  451. $url = $globalurl . '/unsuspend';
  452. //create a new cURL resource
  453. $ch = curl_init($url);
  454. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  455. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  456. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  457. //setup request to send json via POST
  458. $data = array(
  459. 'gzid' => $gzid,
  460. 'uuid' => $uuid
  461. );
  462. $payload = json_encode($data);
  463. //attach encoded JSON string to the POST fields
  464. curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
  465. //set the content type to application/json
  466. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
  467. //execute the POST request
  468. $result = curl_exec($ch);
  469. //close cURL resource
  470. curl_close($ch);
  471. return 'success';
  472. }
  473. function vpsmanager_ClientArea($params)
  474. {
  475. // WHMCS resolves and authorizes this service before calling the module.
  476. // Only a whitelisted range is read from the request, never uuid/vtype/id.
  477. return \VPSManager\ClientView::render($params, $_GET['metrics_range'] ?? '24h');
  478. }
  479. function vpsmanager_AdminServicesTabFields($params)
  480. {
  481. return ['' => \VPSManager\ClientView::links($params)];
  482. }
  483. function vpsmanager_LoginLink($params)
  484. {
  485. return [];
  486. }