IpAddressValidator.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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\ProxmoxAddon\App\UI\Validators;
  20. use ModulesGarden\ProxmoxAddon\Core\UI\Widget\Forms\Validators\BaseValidator;
  21. /**
  22. * Description of NumberValidator
  23. *
  24. * @author Pawel Kopec <pawelk@modulesgardne.com>
  25. */
  26. class IpAddressValidator extends BaseValidator
  27. {
  28. protected $required = false;
  29. protected $public = true;
  30. public function __construct($required = false, $public = true)
  31. {
  32. $this->required = $required;
  33. $this->public = $public;
  34. }
  35. protected function validate($data, $additionalData = null)
  36. {
  37. if (!$this->required && empty($data))
  38. {
  39. return true;
  40. }
  41. if (!filter_var($data, FILTER_VALIDATE_IP))
  42. {
  43. $this->addValidationError('IP Address is not valid', false);
  44. return false;
  45. }
  46. if ($additionalData && $this->public && $additionalData->get('formData')['private'] == "off" &&
  47. !filter_var($data, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE))
  48. {
  49. $this->addValidationError('The IP Address is not public', false);
  50. return false;
  51. }
  52. return true;
  53. }
  54. }