diff --git a/src/Form/LinkCheckerAdminSettingsForm.php b/src/Form/LinkCheckerAdminSettingsForm.php index 73cafcf..0a432c6 100644 --- a/src/Form/LinkCheckerAdminSettingsForm.php +++ b/src/Form/LinkCheckerAdminSettingsForm.php @@ -15,6 +15,7 @@ use Drupal\linkchecker\LinkCheckerResponseCodesInterface; use Drupal\linkchecker\LinkCheckerService; use Drupal\linkchecker\LinkCleanUp; use Drupal\linkchecker\LinkExtractorBatch; +use Drupal\linkchecker\LinkCheckerBatch; use Drupal\user\UserStorageInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -51,6 +52,13 @@ class LinkCheckerAdminSettingsForm extends ConfigFormBase { */ protected $extractorBatch; + /** + * The LinkCheck batch. + * + * @var \Drupal\linkchecker\LinkCheckerBatch + */ + protected $checkerBatch; + /** * The link clean up. * @@ -75,12 +83,13 @@ class LinkCheckerAdminSettingsForm extends ConfigFormBase { /** * LinkCheckerAdminSettingsForm constructor. */ - public function __construct(ConfigFactoryInterface $config_factory, DateFormatterInterface $date_formatter, FilterPluginManager $plugin_manager_filter, LinkCheckerService $linkchecker_checker, LinkExtractorBatch $extractorBatch, LinkCleanUp $linkCleanUp, UserStorageInterface $user_storage, LinkCheckerResponseCodesInterface $linkCheckerResponseCodes) { + public function __construct(ConfigFactoryInterface $config_factory, DateFormatterInterface $date_formatter, FilterPluginManager $plugin_manager_filter, LinkCheckerService $linkchecker_checker, LinkExtractorBatch $extractorBatch, LinkCheckerBatch $checkerBatch, LinkCleanUp $linkCleanUp, UserStorageInterface $user_storage, LinkCheckerResponseCodesInterface $linkCheckerResponseCodes) { parent::__construct($config_factory); $this->dateFormatter = $date_formatter; $this->extractorBatch = $extractorBatch; $this->linkCleanUp = $linkCleanUp; $this->linkCheckerService = $linkchecker_checker; + $this->checkerBatch = $checkerBatch; $this->filterPluginManager = $plugin_manager_filter; $this->userStorage = $user_storage; $this->linkCheckerResponseCodes = $linkCheckerResponseCodes; @@ -96,6 +105,7 @@ class LinkCheckerAdminSettingsForm extends ConfigFormBase { $container->get('plugin.manager.filter'), $container->get('linkchecker.checker'), $container->get('linkchecker.extractor_batch'), + $container->get('linkchecker.checker_batch'), $container->get('linkchecker.clean_up'), $container->get('entity_type.manager')->getStorage('user'), $container->get('linkchecker.response_codes') @@ -183,6 +193,20 @@ class LinkCheckerAdminSettingsForm extends ConfigFormBase { '#title' => $this->t('Base path'), '#description' => $this->t('Should not start with URL scheme'), ]; + $form['general']['localhost_url'] = [ + '#default_value' => $config->get('localhost_url'), + '#type' => 'textfield', + '#size' => 20, + '#title' => $this->t('Override default host URL'), + '#description' => $this->t('Please leave it empty. It is only for the non-standard docker envirnoment with the security issue to override the default URL.'), + ]; + $form['general']['localhost_portnumber'] = [ + '#default_value' => $config->get('localhost_portnumber'), + '#type' => 'textfield', + '#size' => 10, + '#title' => $this->t('Override host port number'), + '#description' => $this->t('Please leave it empty. it is only for non-standard docker envirnoment with the security issue.'), + ]; $form['tag'] = [ '#type' => 'details', @@ -415,6 +439,11 @@ class LinkCheckerAdminSettingsForm extends ConfigFormBase { '#value' => $this->t('Clear link data and analyze content for links'), '#submit' => ['::submitForm', '::submitClearAnalyzeLinks'], ]; + $form['clear']['linkchecker_check'] = [ + '#type' => 'submit', + '#value' => $this->t('Check links'), + '#submit' => ['::submitForm', '::submitCheck'], + ]; return parent::buildForm($form, $form_state); } @@ -465,6 +494,8 @@ class LinkCheckerAdminSettingsForm extends ConfigFormBase { ->set('check_links_types', $form_state->getValue('linkchecker_check_links_types')) ->set('default_url_scheme', $form_state->getValue('default_url_scheme')) ->set('base_path', $form_state->getValue('base_path')) + ->set('localhost_url', $form_state->getValue('localhost_url')) + ->set('localhost_portnumber', $form_state->getValue('localhost_portnumber')) ->set('extract.from_a', $form_state->getValue('linkchecker_extract_from_a')) ->set('extract.from_audio', $form_state->getValue('linkchecker_extract_from_audio')) ->set('extract.from_embed', $form_state->getValue('linkchecker_extract_from_embed')) @@ -503,4 +534,10 @@ class LinkCheckerAdminSettingsForm extends ConfigFormBase { $this->submitAnalyzeLinks($form, $form_state); } + /** + * Check all links. + */ + public function submitCheck(array &$form, FormStateInterface $form_state) { + $this->checkerBatch->batch(); + } } diff --git a/src/LinkCheckerService.php b/src/LinkCheckerService.php index 80d4097..5567ebf 100644 --- a/src/LinkCheckerService.php +++ b/src/LinkCheckerService.php @@ -144,7 +144,22 @@ class LinkCheckerService { $headers = []; $headers['User-Agent'] = $userAgent; - $uri = @parse_url($link->getUrl()); + $basepath = $this->linkcheckerSetting->get('base_path'); + $overridehosturl = $this->linkcheckerSetting->get('localhost_url'); + $overridehostport = $this->linkcheckerSetting->get('localhost_portnumber'); + $url = $link->getUrl(); + $uri = @parse_url($url); + //for non-standard environment (docker & ubuntu) on the security issue. + if (($uri['host'] == $basepath) && (isset($overridehosturl) && !empty($overridehosturl))) { + if (isset($overridehostport) && !empty($overridehostport)) { + $replacement =$overridehosturl . ':' . $overridehostport; + } + else { + $replacement =$overridehosturl; + } + $host_path = $uri['scheme'] . '://' . $uri['host']; + $url = str_replace($host_path, $replacement, $url); + } // URL contains a fragment. if (in_array($link->getRequestMethod(), ['HEAD', 'GET']) && !empty($uri['fragment'])) { @@ -169,8 +184,8 @@ class LinkCheckerService { ]; return $this->httpClient - ->requestAsync($link->getRequestMethod(), $link->getUrl(), $options) - ->then(function (ResponseInterface $response) use ($link) { + ->requestAsync($link->getRequestMethod(), $url, $options) + ->then(function (ResponseInterface $response) use ($link, $uri) { $this->statusHandling($response, $link); }, function (RequestException $e) use ($link) {