From 086523c564bcd97fb73804a8584740ba5edd67f5 Mon Sep 17 00:00:00 2001 From: Nitesh Sethia Date: Sat, 30 Jul 2016 22:10:47 +0530 Subject: [PATCH] Adding a new class for the helper function of validation --- .../install/domain_301_redirect.settings.yml | 4 +- domain_301_redirect.services.yml | 4 ++ src/Domain301RedirectManager.php | 69 ++++++++++++++++++ src/Domain301RedirectManagerInterface.php | 18 +++++ src/Form/Domain301RedirectSettings.php | 70 ++++++++++--------- 5 files changed, 129 insertions(+), 36 deletions(-) create mode 100644 src/Domain301RedirectManager.php create mode 100644 src/Domain301RedirectManagerInterface.php diff --git a/config/install/domain_301_redirect.settings.yml b/config/install/domain_301_redirect.settings.yml index c305561..59f8d1c 100644 --- a/config/install/domain_301_redirect.settings.yml +++ b/config/install/domain_301_redirect.settings.yml @@ -3,5 +3,5 @@ domain_301_redirect_check_period: 60 * 60 * 3 domain_301_redirect_domain_check_retries: 3 domain_301_redirect_domain_check_reenable: true domain_301_redirect_disabled_by_check: false -domain_301_redirect_enabled: 0 -domain_301_redirect_last_checked: 0 \ No newline at end of file +domain_301_redirect_last_checked: 0 +domain_301_redirect_loop_max_redirects: 3 \ No newline at end of file diff --git a/domain_301_redirect.services.yml b/domain_301_redirect.services.yml index 740fd7b..2a305a8 100644 --- a/domain_301_redirect.services.yml +++ b/domain_301_redirect.services.yml @@ -5,3 +5,7 @@ services: tags: - { name: event_subscriber } + domain_301_redirect.manager: + class: Drupal\domain_301_redirect\Domain301RedirectManager + tags: + - {name: domain_301_redirect_manager} diff --git a/src/Domain301RedirectManager.php b/src/Domain301RedirectManager.php new file mode 100644 index 0000000..d87b450 --- /dev/null +++ b/src/Domain301RedirectManager.php @@ -0,0 +1,69 @@ +get('domain_301_redirect_loop_max_redirects'); + $checked = 0; + + // Make a request to the domain that is being configured, following a + // configured number of redirects. This has to be done individually, because + // if checking all 3 levels at once, we might happen to get the wrong one back + // (if the redirect loop has multiple levels). + do { + $client = \Drupal::httpClient(); + $response = $client->get($domain, array('max_redirects' => 0, 'method' => 'HEAD', 'headers' => array('Accept' => 'text/plain'))); + + if (!empty($response->redirect_url)) { + // Request target for the next request loop. + $domain = $response->redirect_url; + // Check if any host names match the redirect host name. + $location_host = Unicode::strtolower(parse_url($response->redirect_url, PHP_URL_HOST)); + if ($redirect_host == $location_host || $host == $location_host) { + $redirect_loop = TRUE; + } + } + $checked++; + // Don't check the redirect code, as it's possible there may be another + // redirect service in operation that does not use 301. + } while (!$redirect_loop && !empty($response->redirect_url) && $checked < $redirects_to_check); + + return $redirect_loop; + } +} diff --git a/src/Domain301RedirectManagerInterface.php b/src/Domain301RedirectManagerInterface.php new file mode 100644 index 0000000..b29c19f --- /dev/null +++ b/src/Domain301RedirectManagerInterface.php @@ -0,0 +1,18 @@ +getUserInput(); - // dsm($userInputValues); - // if (!empty($userInputValues['domain_301_redirect_enabled'])) { - // $domain = trim($userInputValues['domain_301_redirect_domain']); - // if (!preg_match('|^https?://|', $domain)) { - // $domain = 'http://' . $domain; - // } - // if (!valid_url($domain, TRUE)) { - // form_set_error('domain_301_redirect_enabled', t('Domain 301 redirection can not be enabled if no valid domain is set.')); - // } - // else { - // $domain_parts = parse_url($domain); - // $domain = $domain_parts['scheme'] . '://' . $domain_parts['host'] . (!empty($domain_parts['port']) ? ':' . $domain_parts['port'] : ''); - // form_set_value($form['domain_301_redirect_domain'], $domain, $form_state); - - // if (!domain_301_redirect_check_domain($domain)) { - // form_set_error('domain_301_redirect_enabled', t('Domain 301 redirection can not be enabled as the domain you set does not currently point to this site.')); - // } - // else { - // // Clean up if someone is manually disabling. We don't want the system to - // // re-enable if the disabling was via the admin form. - // variable_set('domain_301_redirect_disabled_by_check', false); - // } - - // if (domain_301_redirect_check_loop($domain)) { - // form_set_error('domain_301_redirect_domain', t('The domain cannot be set, as it causes a redirect loop (within @num redirects).', array('@num' => variable_get('domain_301_redirect_loop_max_redirects', 3)))); - // } - // } - // } - // } + + public function validateForm(array &$form, FormStateInterface $form_state) { + $config = \Drupal::config('domain_301_redirect.settings'); + $userInputValues = $form_state->getUserInput(); + if (!empty($userInputValues['domain_301_redirect_enabled'])) { + $domain = trim($userInputValues['domain_301_redirect_domain']); + if (!preg_match('|^https?://|', $domain)) { + $domain = 'http://' . $domain; + } + if (!UrlHelper::isValid($domain, TRUE)) { + $form_state->setErrorByName('domain_301_redirect_enabled', t('Domain 301 redirection can not be enabled if no valid domain is set.')); + } + else { + $domain_parts = parse_url($domain); + $domain = $domain_parts['scheme'] . '://' . $domain_parts['host'] . (!empty($domain_parts['port']) ? ':' . $domain_parts['port'] : ''); + $form_state->setValueForElement($form['domain_301_redirect_domain'], $domain); + $service = \Drupal::service('domain_301_redirect.manager'); + + if (!$service->domain_301_redirect_check_loop($domain)) { + $form_state->setErrorByName('domain_301_redirect_enabled', t('Domain 301 redirection can not be enabled as the domain you set does not currently point to this site.')); + } + else { + // Clean up if someone is manually disabling. We don't want the system to + // re-enable if the disabling was via the admin form. + Drupal::state()->set('domain_301_redirect_disabled_by_check', TRUE); + } + + if ($service->domain_301_redirect_check_loop($domain)){ + $form_state->setErrorByName('domain_301_redirect_domain', t('The domain cannot be set, as it causes a redirect loop (within @num redirects).', array('@num' => $config->get('domain_301_redirect_loop_max_redirects')))); + } + } + } + } + public function submitForm(array &$form, FormStateInterface $form_state) { $userInputValues = $form_state->getUserInput(); @@ -158,6 +159,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) { ->set('domain_301_redirect_domain_check_reenable', $userInputValues['domain_301_redirect_domain_check_reenable']) ->set('domain_301_redirect_applicability', $userInputValues['domain_301_redirect_applicability']) ->set('domain_301_redirect_pages', $userInputValues['domain_301_redirect_pages']) + ->set('domain_301_redirect_loop_max_redirects', $userInputValues['domain_301_redirect_loop_max_redirects']) ->save(); parent::submitForm($form, $form_state);