TestConnection.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**********************************************************************
  3. * ProxmoxVPS developed. (26.03.19)
  4. * *
  5. *
  6. * CREATED BY MODULESGARDEN -> http://modulesgarden.com
  7. * CONTACT -> contact@modulesgarden.com
  8. *
  9. *
  10. * This software is furnished under a license and may be used and copied
  11. * only in accordance with the terms of such license and with the
  12. * inclusion of the above copyright notice. This software or any other
  13. * copies thereof may not be provided or otherwise made available to any
  14. * other person. No title to and ownership of the software is hereby
  15. * transferred.
  16. *
  17. *
  18. **********************************************************************/
  19. namespace ModulesGarden\Servers\ProxmoxVps\App\Http\Actions;
  20. use MGProvision\Proxmox\v2\Api;
  21. use ModulesGarden\Servers\ProxmoxVps\App\Helpers\ProxmoxAddonValidator;
  22. use ModulesGarden\Servers\ProxmoxVps\Core\App\Controllers\Instances\AddonController;
  23. class TestConnection extends AddonController
  24. {
  25. public function execute($params = null)
  26. {
  27. try
  28. {
  29. if(isset($GLOBALS['disable_hook_loading']) && $GLOBALS['disable_hook_loading']){
  30. throw new \Exception("Enabled hooks are required by the module. Please disable \$disable_hook_loading in cofiguration.php ");
  31. }
  32. ProxmoxAddonValidator::isInstalledOrFail();
  33. if (!$params['serverip'] && !$params['serverhostname'])
  34. {
  35. throw new \Exception("Server Hostname or IP Address is empty.");
  36. }
  37. if ($params['serverip'])
  38. {
  39. if(is_numeric($params['serverport'])){
  40. $params['serverip'] .=":".$params['serverport'];
  41. }
  42. $api = new Api($params['serverip'], $params['serverusername'], $params['serveraccesshash'], $params['serverpassword']);
  43. $api->debug();
  44. $api->get("/nodes");
  45. }
  46. if ($params['serverhostname'])
  47. {
  48. if (preg_match("/[\/]/", $params['serverhostname']))
  49. {
  50. throw new \Exception(sprintf("Server Hostname '%s' is invalid", $params['serverhostname']));
  51. }
  52. if(is_numeric($params['serverport'])){
  53. $params['serverhostname'] .=":".$params['serverport'];
  54. }
  55. $api = new Api($params['serverhostname'], $params['serverusername'], $params['serveraccesshash'], $params['serverpassword']);
  56. $api->debug();
  57. $api->get("/nodes");
  58. }
  59. return ['success' => true];
  60. }
  61. catch (\Exception $e)
  62. {
  63. return ['error' => $e->getMessage()];
  64. }
  65. }
  66. }