You are here

ip_anon.module in IP Anonymize 5

Same filename and directory in other branches
  1. 8 ip_anon.module
  2. 6 ip_anon.module
  3. 7 ip_anon.module

Establishes an IP address retention policy.

File

ip_anon.module
View source
<?php

/**
 * @file
 * Establishes an IP address retention policy.
 */

/**
 * Implementation of hook_menu().
 */
function ip_anon_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/ip_anon',
      'title' => t('IP anonymize'),
      'description' => t('Configure the IP address retention policy.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'ip_anon_settings',
      ),
      'access' => user_access('administer site configuration'),
    );
  }
  return $items;
}

/**
 * Implementation of hook_cron().
 *
 * Anonymize IP addresses which have exceeded the retention period.
 */
function ip_anon_cron() {
  if (variable_get('ip_anon_policy', 0)) {
    foreach (ip_anon_tables() as $table => $columns) {
      if (db_table_exists($table)) {
        db_query("UPDATE {{$table}} SET {$columns['hostname']} = '%s' WHERE {$columns['timestamp']} < %d", 0, time() - variable_get('ip_anon_period_' . $table, 10800));
      }
    }
  }
}

/**
 * Configuration options for IP anonymize.
 */
function ip_anon_settings() {
  $form = array();
  $form['ip_anon_policy'] = array(
    '#type' => 'radios',
    '#title' => t('Retention policy'),
    '#options' => array(
      0 => t('Preserve IP addresses'),
      1 => t('Anonymize IP addresses'),
    ),
    '#description' => t('This setting may be used to temporarily disable IP anonymization.'),
    '#default_value' => variable_get('ip_anon_policy', 0),
  );
  $form['ip_anon_period'] = array(
    '#type' => 'fieldset',
    '#title' => 'Retention period',
    '#description' => t('IP addresses older than the retention period will be anonymized. The flood table expects a retention period of at least @one_hour.', array(
      '@one_hour' => format_interval(3600),
    )),
  );
  $options = drupal_map_assoc(array(
    0,
    30,
    60,
    120,
    180,
    300,
    600,
    900,
    1800,
    2700,
    3600,
    5400,
    7200,
    10800,
    21600,
    32400,
    43200,
    64800,
    86400,
    172800,
    259200,
    345600,
    604800,
    1209600,
    2419200,
    4838400,
    9676800,
    31536000,
  ), 'format_interval');
  foreach (ip_anon_tables() as $table => $columns) {
    $form['ip_anon_period']['ip_anon_period_' . $table] = array(
      '#type' => 'select',
      '#title' => t('@table table', array(
        '@table' => $table,
      )),
      '#options' => $options,
      '#default_value' => variable_get('ip_anon_period_' . $table, 10800),
    );
  }
  return system_settings_form($form);
}

/**
 * Array of tables and columns which store hostnames and timestamps.
 */
function ip_anon_tables() {
  $columns = drupal_map_assoc(array(
    'hostname',
    'timestamp',
  ));
  $tables = array(
    'accesslog' => $columns,
    'comments' => $columns,
    'flood' => $columns,
    'sessions' => $columns,
    'watchdog' => $columns,
  );
  return $tables;
}

Functions

Namesort descending Description
ip_anon_cron Implementation of hook_cron().
ip_anon_menu Implementation of hook_menu().
ip_anon_settings Configuration options for IP anonymize.
ip_anon_tables Array of tables and columns which store hostnames and timestamps.