httpbl-8.x-1.x-dev/httpbl.install
httpbl.install
<?php
use Drupal\Core\Link;
use Drupal\Core\Url;
/**
* @file
* Install, update and uninstall functions for the httpbl module.
*/
/**
* Implements hook_requirements().
*
* Shows some basic usage statistics for the module in the Status Report.
*/
function httpbl_requirements($phase) {
$requirements = array();
if ($phase == 'runtime') {
// Checks for Access Key, and Comment or Page Checking enabled.
if (!\Drupal::state()->get('httpbl.accesskey') ?: NULL || !\Drupal::state()->get('httpbl.check') ?: HTTPBL_CHECK_NONE ) {
// Make Config page link.
$config_link = Link::fromTextAndUrl(t('Http:BL config'), Url::fromRoute('httpbl.admin_config'))->toString();
$requirements['httpbl'] = array(
'description' => t('IP blacklist lookups are currently disabled; enter your access key on the @settings page and enable checks to enable blacklist lookups.', array('@settings' => $config_link)),
'severity' => REQUIREMENT_WARNING,
'value' => t('Disabled'),
);
// Checks for page bottom honeypot links.
if (\Drupal::state()->get('httpbl.footer') ?: FALSE) {
$requirements['httpbl']['severity'] = REQUIREMENT_INFO;
}
}
else {
// Gather the stats to be displayed.
$stat_black = \Drupal::state()->get('httpbl.stat_black');
$stat_comment = \Drupal::state()->get('httpbl.stat_comment');
$stat_grey = \Drupal::state()->get('httpbl.stat_grey');
// Check of stats collecting/display is enabled.
if (!\Drupal::state()->get('httpbl.stats') ?: FALSE) {
$requirements['httpbl'] = array(
'description' => t('Http:BL is enabled.'),
'severity' => REQUIREMENT_OK,
'value' => t('Enabled'),
);
}
// Checks if only comment blocking..
elseif (\Drupal::state()->get('httpbl.check') == HTTPBL_CHECK_COMMENTS) {
$requirements['httpbl'] = array(
'description' => t('Http:BL is enabled and has blocked @c comments.', array('@c' => $stat_comment)),
'severity' => REQUIREMENT_OK,
'value' => t('Enabled'),
);
}
// Checks if enabled to block visitors on all pages.
elseif (\Drupal::state()->get('httpbl.check') == HTTPBL_CHECK_ALL) {
$requirements['httpbl'] = array(
'description' => t('Http:BL is enabled and has blocked @t visits (@b blacklisted and @g greylisted).', array('@t' => $stat_grey+$stat_black, '@b' => $stat_black, '@g' => $stat_grey)),
'severity' => REQUIREMENT_OK,
'value' => t('Enabled'),
);
// Show a status error if core Internal Page Cache (page_cache) is enabled.
if (\Drupal::hasService('http_middleware.page_cache') ) {
$requirements['httpbl'] = array(
'title' => t('Http:BL'),
'description' => t('Using HttpBL for evaluating page requests is allowed but NOT recommended when core extension Internal Page Cache (page_cache) enabled.', array()),
'severity' => REQUIREMENT_ERROR,
);
$requirements['httpbl']['description'] .= ' ' . t('Recommended action: Configure Http:BL for comment blocking only or uninstall page_cache. Note: Use with Internal Dynamic Page Cache (dynamic_page_cache) is ok.');
return $requirements;
}
// Checks if also auto-banning blacklisted IPs (via the Core Ban module).
if (\Drupal::state()->get('httpbl.storage') == HTTPBL_DB_HH_DRUPAL) {
$requirements['httpbl']['severity'] = REQUIREMENT_INFO;
$requirements['httpbl']['description'] .= ' ' . t('Auto-banning of blacklisted IPs enabled.');
}
else {
$requirements['httpbl']['severity'] = REQUIREMENT_INFO;
$requirements['httpbl']['description'] .= ' ' . t('Auto-banning of blacklisted IPs DISABLED.');
}
}
// Checks if also using honeypot page bottom links.
if (!\Drupal::state()->get('httpbl.footer') ?: FALSE) {
$requirements['httpbl']['severity'] = REQUIREMENT_INFO;
$requirements['httpbl']['description'] .= ' ' . t('Page bottom Honeypot links are DISABLED.');
}
else {
$requirements['httpbl']['severity'] = REQUIREMENT_INFO;
$requirements['httpbl']['description'] .= ' ' . t('Page bottom Honeypot links enabled.');
}
}
$requirements['httpbl']['title'] = t('Http:BL');
}
return $requirements;
}
/**
* Implements hook_uninstall().
*/
function httpbl_uninstall() {
// Clean up any remaining variables (located in key_value table).
// @todo Is this even needed anymore? Find out.
\Drupal::database()->delete('key_value')
->condition('name', 'httpbl.%%', 'LIKE')
->execute();
}
