You are here

ip_anon.pages.inc in IP Anonymize 6

Page callbacks and utility functions for IP anonymize module.

File

ip_anon.pages.inc
View source
<?php

/**
 * @file
 * Page callbacks and utility functions for IP anonymize module.
 */

/**
 * Anonymize IP addresses which have exceeded the retention period. 
 */
function ip_anon_cron_run() {
  if (variable_get('ip_anon_policy', 0)) {
    foreach (ip_anon_tables() as $table => $columns) {
      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) {
    $schema = drupal_get_schema($table);
    $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),
      '#description' => t($schema['description']),
    );
  }
  return system_settings_form($form);
}

/**
 * Array of tables and columns which store hostnames and timestamps.
 *
 * Modules may add tables by implementing hook_ip_anon_alter().
 * @see webform_ip_anon_alter()
 */
function ip_anon_tables() {
  $columns = drupal_map_assoc(array(
    'hostname',
    'timestamp',
  ));
  $tables = array(
    'accesslog' => $columns,
    'comments' => $columns,
    'flood' => $columns,
    'sessions' => $columns,
    'watchdog' => $columns,
  );
  drupal_alter('ip_anon', $tables);
  foreach ($tables as $table => $columns) {
    if (!db_table_exists($table)) {
      unset($tables[$table]);
    }
  }
  return $tables;
}

/**
 * Implements hook_ip_anon_alter() for webform module.
 */
function webform_ip_anon_alter(&$tables) {
  $tables['webform_submissions'] = array(
    'hostname' => 'remote_addr',
    'timestamp' => 'submitted',
  );
}

Functions

Namesort descending Description
ip_anon_cron_run Anonymize IP addresses which have exceeded the retention period.
ip_anon_settings Configuration options for IP anonymize.
ip_anon_tables Array of tables and columns which store hostnames and timestamps.
webform_ip_anon_alter Implements hook_ip_anon_alter() for webform module.