ContainerService.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\App\Services\Vps;
  3. use ModulesGarden\ProxmoxAddon\App\Models\KeyPair;
  4. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  5. use ModulesGarden\ProxmoxAddon\App\Services\Utility;
  6. use ModulesGarden\ProxmoxAddon\Core\UI\Traits\WhmcsParams;
  7. use ModulesGarden\ProxmoxAddon\App\Enum\Vps\CustomField;
  8. use ModulesGarden\ProxmoxAddon\Core\Traits\Smarty;
  9. class ContainerService
  10. {
  11. use ApiService;
  12. use WhmcsParams;
  13. use HostingService;
  14. use Smarty;
  15. use ProductService;
  16. public function hostname()
  17. {
  18. $this->setHostingId($this->getWhmcsParamByKey("serviceid"));
  19. if ($this->configuration()->isQemu())
  20. {
  21. $domain = $this->getWhmcsCustomField(CustomField::HOSTNAME, $this->getWhmcsParamByKey('domain')) ;
  22. $nameForVm = $this->configuration()->getClientNameForContainer();
  23. if ($nameForVm == "emptyHostnameOnly" && empty($domain))
  24. { // Yes [only when hostname is empty]
  25. $domain = $this->getWhmcsParamByKey('clientsdetails')['companyname'] ? $this->getWhmcsParamByKey('clientsdetails')['companyname'] : $this->getWhmcsParamByKey('clientsdetails')['lastname'] . $this->getWhmcsParamByKey('clientsdetails')['firstname'];
  26. $domain = trim(preg_replace('/[^A-Za-z0-9\.]/', '', Utility::replaceSpecialChars($domain)), '.');
  27. $this->hosting()->update(["domain" => $domain]);
  28. }
  29. else
  30. {
  31. if ($nameForVm == "overwriteHostname")
  32. { //Yes [overwrite hostname]
  33. $domain = $this->getWhmcsParamByKey('clientsdetails')['companyname'] ? $this->getWhmcsParamByKey('clientsdetails')['companyname'] : $this->getWhmcsParamByKey('clientsdetails')['lastname'] . $this->getWhmcsParamByKey('clientsdetails')['firstname'];
  34. $domain = trim(preg_replace('/[^A-Za-z0-9\.]/', '', Utility::replaceSpecialChars($domain)), '.');
  35. $this->hosting()->update(["domain" => $domain]);
  36. }
  37. else
  38. {
  39. if ($nameForVm == "overwriteHostnameWithPrefix" || $nameForVm == "0")
  40. { //NO
  41. if (empty($domain) || $nameForVm == "overwriteHostnameWithPrefix")
  42. {
  43. $domain = $this->configuration()->getContainerPrefix() . $this->getWhmcsParamByKey('serviceid');
  44. $this->hosting()->update(["domain" => $domain]);
  45. }
  46. }
  47. }
  48. }
  49. return $domain;
  50. }
  51. else
  52. {
  53. if ($this->configuration()->isLxc())
  54. {
  55. if ($this->configuration()->isRandomHostname())
  56. {
  57. $domain = parse_url($GLOBALS['CONFIG']['Domain']);
  58. return $this->getWhmcsParamByKey('serviceid') . '.' . $domain['host'];
  59. }
  60. else
  61. {
  62. return $this->getWhmcsCustomField(CustomField::HOSTNAME, $this->getWhmcsParamByKey('domain'));
  63. }
  64. }
  65. }
  66. }
  67. public function description()
  68. {
  69. if (!$this->params['model'])
  70. {
  71. $this->params['model'] = \WHMCS\Service\Service::where("id", $this->params['serviceid'])->first();
  72. }
  73. $vars = [
  74. 'client_name' => $this->getWhmcsParamByKey('clientsdetails')['firstname'] . ' ' . $this->getWhmcsParamByKey('clientsdetails')['lastname'] . (($this->getWhmcsParamByKey('clientsdetails')['companyname']) ? (' (' . $this->getWhmcsParamByKey('clientsdetails')['companyname'] . ')') : ''),
  75. 'client_id' => $this->getWhmcsParamByKey('userid'),
  76. 'client_email' => $this->getWhmcsParamByKey('clientsdetails')['email'],
  77. 'product_name' => $this->hosting()->product->name,
  78. 'product_id' => $this->getWhmcsParamByKey('pid'),
  79. 'service_id' => $this->getWhmcsParamByKey('serviceid'),
  80. 'service_domain' => $this->hosting()->domain,
  81. 'service_dedicated_ip' => $this->hosting()->dedicatedip,
  82. 'service_assigned_ips' => $this->hosting()->assignedips
  83. ];
  84. return $this->getSmarty()->fetchString($this->configuration()->getDescription(), $vars);
  85. }
  86. /**
  87. * @return KeyPair
  88. */
  89. public function makeKeyPairs()
  90. {
  91. //delete
  92. KeyPair::ofHostingId($this->getWhmcsParamByKey('serviceid'))->delete();
  93. $keyPairs = KeyPair::make();
  94. $keyPairs->setHostingId($this->getWhmcsParamByKey('serviceid'))
  95. ->save();
  96. return $keyPairs;
  97. }
  98. }