Módulo VPSManager para WHMCS con configuración externa, acceso seguro al panel y métricas Graphite para LX.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

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