IpAddressValidator.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxAddon product developed. (Aug 23, 2018)
  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\ProxmoxCloudVps\App\UI\Validators;
  20. use ModulesGarden\ProxmoxAddon\App\Models\VmIpAddress;
  21. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Validators\BaseValidator;
  22. /**
  23. * Description of NumberValidator
  24. *
  25. * @author Pawel Kopec <pawelk@modulesgardne.com>
  26. */
  27. class IpAddressValidator extends BaseValidator
  28. {
  29. protected $required = false;
  30. protected $public = true;
  31. public function __construct($required = false , $public = true)
  32. {
  33. $this->required = $required;
  34. $this->public = $public;
  35. }
  36. protected function validate($data, $additionalData = null)
  37. {
  38. if (!$this->required && empty($data))
  39. {
  40. return true;
  41. }
  42. if (!filter_var($data, FILTER_VALIDATE_IP))
  43. {
  44. $this->addValidationError('IP Address is not valid', false);
  45. return false;
  46. }
  47. if (!$this->public && filter_var($data, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE))
  48. {
  49. $this->addValidationError('The IP Address is not private', false);
  50. return false;
  51. }
  52. if ($this->required && VmIpAddress::ofIp($data)->count())
  53. {
  54. $this->addValidationError('IP Address already exist', false);
  55. return false;
  56. }
  57. return true;
  58. }
  59. }