IpLogService.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\App\Services;
  3. use ModulesGarden\ProxmoxAddon\App\Enum\Vps\CustomField;
  4. use ModulesGarden\ProxmoxAddon\App\Models\IpLog;
  5. use ModulesGarden\ProxmoxAddon\Core\UI\Traits\WhmcsParams;
  6. use function ModulesGarden\ProxmoxAddon\Core\Helper\sl;
  7. class IpLogService
  8. {
  9. use WhmcsParams;
  10. public function assigned(array $ipAddresses){
  11. $ipLog = new IpLog();
  12. $ipLog->hosting_id = $this->getWhmcsParamByKey('serviceid');
  13. $msg = count($ipAddresses) >1 ? "IP Addresses: %s have been assigned, Client ID #%s, Hosting ID #%s" : "IP Address: %s has been assigned, Client ID #%s, Hosting ID #%s";
  14. $ipLog->message = sprintf($msg, implode(", ", $ipAddresses), $this->getWhmcsParamByKey('userid') ,$ipLog->hosting_id );
  15. if($this->getWhmcsCustomField(CustomField::VMID)){
  16. $ipLog->message .= ", VMID: ".$this->getWhmcsCustomField(CustomField::VMID);
  17. }elseif(sl('Vm')->hasVmModel() && sl('Vm')->getVmModel()->vmid){
  18. $ipLog->message .= ", VMID: ".sl('Vm')->getVmModel()->vmid;
  19. }
  20. $ipLog->created_at = date("Y-m-d H:i:s");
  21. $ipLog->save();
  22. return $ipLog;
  23. }
  24. public function unassigned(array $ipAddresses){
  25. $ipLog = new IpLog();
  26. $ipLog->hosting_id = $this->getWhmcsParamByKey('serviceid');
  27. $msg = count($ipAddresses) >1 ? "IP Addresses: %s have been unassigned, Client ID #%s, Hosting ID#%s" : "IP Address: %s has been unassigned, Client ID #%s, Hosting ID #%s";
  28. $ipLog->message = sprintf($msg, implode(", ", $ipAddresses ), $this->getWhmcsParamByKey('userid') ,$ipLog->hosting_id);
  29. if($this->getWhmcsCustomField(CustomField::VMID)){
  30. $ipLog->message .= ", VMID: ".$this->getWhmcsCustomField(CustomField::VMID);
  31. }
  32. elseif(sl('Vm')->hasVmModel() && sl('Vm')->getVmModel()->vmid){
  33. $ipLog->message .= ", VMID: ".sl('Vm')->getVmModel()->vmid;
  34. }
  35. $ipLog->created_at = date("Y-m-d H:i:s");
  36. $ipLog->save();
  37. return $ipLog;
  38. }
  39. }