From 5b853879b0675e0708ef5efa315500d9d991b719 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Tue, 8 Mar 2016 22:10:50 +0100 Subject: [PATCH] PropertyModifierGetHostByName: new modifier --- .../PropertyModifierGetHostByName.php | 50 +++++++++++++++++++ run.php | 1 + 2 files changed, 51 insertions(+) create mode 100644 library/Director/PropertyModifier/PropertyModifierGetHostByName.php diff --git a/library/Director/PropertyModifier/PropertyModifierGetHostByName.php b/library/Director/PropertyModifier/PropertyModifierGetHostByName.php new file mode 100644 index 00000000..34e2f779 --- /dev/null +++ b/library/Director/PropertyModifier/PropertyModifierGetHostByName.php @@ -0,0 +1,50 @@ +addElement('select', 'on_failure', array( + 'label' => 'On failure', + 'description' => $form->translate('What should we do if the host (DNS) lookup fails?'), + 'multiOptions' => $form->optionalEnum(array( + 'null' => $form->translate('Set no value (null)'), + 'keep' => $form->translate('Keep the property (hostname) as is'), + 'fail' => $form->translate('Let the whole import run fail'), + )), + 'required' => true, + )); + } + + public function getName() + { + return 'Get host by name (DNS lookup)'; + } + + public function transform($value) + { + $host = gethostbyname($value); + if (strlen(@inet_pton($host)) !== 4) { + switch($this->getSetting('on_failure')) { + case 'null': + return null; + case 'keep': + return $value; + case 'fail': + default: + throw new InvalidPropertyException( + 'Host lookup failed for "%s"', + $value + ); + } + } + + return $host; + } +} diff --git a/run.php b/run.php index f151f20a..8d188cb3 100644 --- a/run.php +++ b/run.php @@ -22,3 +22,4 @@ $this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\Pro $this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierStripDomain'); $this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierSubstring'); $this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierUppercase'); +$this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierGetHostByName');