You are here

gdpr_dump.module in General Data Protection Regulation 7

Module file for the GDPR Sql-Dump module.

File

modules/gdpr_dump/gdpr_dump.module
View source
<?php

/**
 * @file
 * Module file for the GDPR Sql-Dump module.
 */

/**
 * Implements hook_menu().
 */
function gdpr_dump_menu() {
  $items['admin/config/gdpr/dump-settings'] = [
    'title' => 'SQL Dump settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => [
      'gdpr_dump_settings_form',
    ],
    'access arguments' => [
      'administer site configuration',
    ],
    'type' => MENU_NORMAL_ITEM,
  ];
  return $items;
}

/**
 * Gdpr dump settings form.
 */
function gdpr_dump_settings_form($form, &$form_state) {
  $form['#tree'] = TRUE;
  $form['description'] = [
    '#markup' => t('Apply anonymization for each table columns containing sensitive data!'),
  ];
  $form['tables'] = [
    '#type' => 'container',
  ];
  $plugins = [];

  // @todo: implement sanitize plugins
  foreach (gdpr_dump_get_sanitizer_plugins() as $plugin_name => $plugin) {
    $plugins[$plugin_name] = $plugin['title'];
  }
  $sanitationOptions = [
    '#type' => 'select',
    '#title' => t('Sanitation plugin'),
    '#options' => $plugins,
    '#empty_value' => 'none',
    '#empty_option' => t('- No -'),
    '#title_display' => 'invisible',
  ];
  $mapping = variable_get('gdpr_dump_table_map', []);
  $empty_tables = variable_get('gdpr_dump_empty_tables', []);
  $table_header = [
    t('Field'),
    t('Type'),
    t('Description'),
    t('Apply anonymization'),
  ];
  $db_schema = gdpr_get_complete_schema();
  ksort($db_schema);
  $more_header = [
    t('Table name'),
    t('Description'),
    t('Columns'),
  ];
  $form['more_wrapper'] = [
    '#type' => 'fieldset',
    '#title' => t('More tables'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    'more_tables' => [
      '#caption' => t("Select from the following tables to be able to configure more for anonymization, then press the 'Refresh form' button below to add them to the form."),
      '#header' => $more_header,
      '#type' => 'tableselect',
      '#options' => [],
      '#js_select' => FALSE,
    ],
    'refresh' => [
      '#type' => 'button',
      '#value' => t('Refresh form'),
    ],
  ];
  $added = [];
  if (isset($form_state['values'])) {
    $added = drupal_array_get_nested_value($form_state['values'], [
      'more_wrapper',
      'more_tables',
    ]);
    $added = array_filter($added);
  }

  // Force some tables to always show in the form if they exist.
  $forced = [
    'comment' => TRUE,
    'contact_message' => TRUE,
    'users' => TRUE,
    'webform_submission' => TRUE,
    'webform_submitted_data' => TRUE,
  ];
  foreach ($db_schema as $table => $table_schema) {
    $table_comment = isset($table_schema['description']) ? $table_schema['description'] : NULL;
    $table_configured = isset($mapping[$table]) || isset($empty_tables[$table]);
    $table_forced = isset($forced[$table]);
    $table_added = isset($added[$table]);
    if ($table_configured || $table_added || $table_forced) {
      $rows = [];
      foreach ($table_schema['fields'] as $column_name => $column_info) {
        $currentOptions = $sanitationOptions;
        if (isset($mapping[$table][$column_name])) {
          $currentOptions['#default_value'] = $mapping[$table][$column_name];
        }
        $rows[$column_name] = [
          'name' => [
            '#markup' => '<strong>' . $column_name . '</strong>',
          ],
          'type' => [
            '#markup' => '<strong>' . $column_info['type'] . '</strong>',
          ],
          'description' => [
            '#markup' => '<strong>' . (empty($column_info['description']) ? '-' : $column_info['description']) . '</strong>',
          ],
          'option' => $currentOptions,
        ];
      }
      $form['tables'][$table] = [
        '#type' => 'fieldset',
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        '#title' => t('Table: %table', [
          '%table' => $table,
        ]),
        '#description' => $table_comment,
        'empty_table' => [
          '#type' => 'checkbox',
          '#title' => t('Empty this table'),
          '#default_value' => isset($empty_tables[$table]) ? $empty_tables[$table] : NULL,
          '#weight' => 1,
        ],
        'columns' => [
          '#type' => 'container',
          '#theme' => 'gdpr_dump_table_columns',
          '#header' => $table_header,
          '#weight' => 0,
        ] + $rows,
      ];
    }
    if (!$table_configured && !$table_forced) {
      $row = [
        [
          'data' => [
            '#markup' => '<strong>' . $table . '</strong>',
          ],
        ],
      ];
      $row[] = $table_comment;
      $row[] = implode(', ', array_keys($table_schema['fields']));
      $form['more_wrapper']['more_tables']['#options'][$table] = $row;
    }
  }
  $form['actions'] = [
    '#type' => 'actions',
    'save' => [
      '#type' => 'submit',
      '#value' => t('Save configuration'),
    ],
  ];
  $form['more_wrapper']['#access'] = !empty($form['more_wrapper']['more_tables']['#options']);
  return $form;
}

/**
 * Gdpr dump settings form submit callback.
 */
function gdpr_dump_settings_form_submit($form, &$form_state) {
  $tables = $form_state['values']['tables'];
  $config = [];
  $empty_tables = [];
  foreach ($tables as $table => $row) {
    if ($row['empty_table'] === 1) {
      $empty_tables[$table] = 1;
    }
    foreach ($row['columns'] as $column_name => $data) {
      if ($data['option'] !== 'none') {
        $config[$table][$column_name] = $data['option'];
      }
    }
  }
  variable_set('gdpr_dump_empty_tables', $empty_tables);
  variable_set('gdpr_dump_table_map', $config);
}

/**
 * Gets the whole database schema with field descriptions.
 */
function gdpr_get_complete_schema() {
  $schema = [];
  if (function_exists('module_load_all_includes')) {
    module_list(TRUE);
    module_load_all_includes('install');
  }
  require_once DRUPAL_ROOT . '/includes/common.inc';

  // Invoke hook_schema for all modules.
  foreach (module_implements('schema') as $module) {
    $current = (array) module_invoke($module, 'schema');
    _drupal_schema_initialize($current, $module, FALSE);
    $schema = array_merge($schema, $current);
  }
  drupal_alter('schema', $schema);
  return $schema;
}

/**
 * Implements hook_ctools_plugin_directory().
 */
function gdpr_dump_ctools_plugin_directory($module, $plugin) {
  if ($module == 'gdpr_dump' && \array_key_exists($plugin, gdpr_dump_ctools_plugin_type())) {
    return 'plugins/' . $plugin;
  }
}

/**
 * Implements hook_ctools_plugin_type().
 */
function gdpr_dump_ctools_plugin_type() {
  return [
    'sanitizer' => [
      'label' => 'sanitizer',
      'use hooks' => FALSE,
    ],
  ];
}

/**
 * Helper function for listing gdpr sanitizer plugins.
 */
function gdpr_dump_get_sanitizer_plugins($id = NULL) {
  ctools_include('plugins');
  return ctools_get_plugins('gdpr_dump', 'sanitizer', $id);
}

/**
 * Random word generator function.
 *
 * @deprecated in Drupal 7.x-1.0-alpha6 and will be removed before 7.x-1.0.
 *   Use GdprDumpUtilRandom::word() instead.
 */
function gdpr_random_word($length = 8) {
  $random = new GdprDumpUtilRandom();
  return $random
    ->word($length);
}

/**
 * Random sentence generator.
 *
 * @deprecated in Drupal 7.x-1.0-alpha6 and will be removed before 7.x-1.0.
 *   Use GdprDumpUtilRandom::sentences() instead.
 */
function gdpr_random_sentence($word_count) {
  $random = new GdprDumpUtilRandom();
  return $random
    ->sentences($word_count);
}

/**
 * Random paragraph generator.
 *
 * @deprecated in Drupal 7.x-1.0-alpha6 and will be removed before 7.x-1.0.
 *   Use GdprDumpUtilRandom::paragraphs() instead.
 */
function gdpr_random_paragraphs($paragraph_count) {
  $random = new GdprDumpUtilRandom();
  return $random
    ->paragraphs($paragraph_count);
}

/**
 * Helper function to load gdpr dump service.
 */
function gdpr_dump_service() {
  $service =& drupal_static(__FUNCTION__);
  if (empty($service)) {
    module_load_include('inc', 'gdpr_dump', 'inc/GdprDumpGdprSqlDump');
    $service = new GdprDumpGdprSqlDump();
  }
  return $service;
}

/**
 * Implements hook_theme().
 */
function gdpr_dump_theme($existing, $type, $theme, $path) {
  return array(
    'gdpr_dump_table_columns' => array(
      'render element' => 'table',
    ),
  );
}

/**
 * Table format for table columns.
 */
function theme_gdpr_dump_table_columns($variables) {
  $element = $variables['table'];
  $rows = [];
  foreach (element_children($element) as $column_name) {
    $row = [];
    foreach (element_children($element[$column_name]) as $col) {
      $data =& $element[$column_name][$col];
      $row[$col] = drupal_render($data);
    }
    $rows[] = $row;
  }
  return theme('table', [
    'header' => $element['#header'],
    'rows' => $rows,
  ]);
}

Functions

Namesort descending Description
gdpr_dump_ctools_plugin_directory Implements hook_ctools_plugin_directory().
gdpr_dump_ctools_plugin_type Implements hook_ctools_plugin_type().
gdpr_dump_get_sanitizer_plugins Helper function for listing gdpr sanitizer plugins.
gdpr_dump_menu Implements hook_menu().
gdpr_dump_service Helper function to load gdpr dump service.
gdpr_dump_settings_form Gdpr dump settings form.
gdpr_dump_settings_form_submit Gdpr dump settings form submit callback.
gdpr_dump_theme Implements hook_theme().
gdpr_get_complete_schema Gets the whole database schema with field descriptions.
gdpr_random_paragraphs Deprecated Random paragraph generator.
gdpr_random_sentence Deprecated Random sentence generator.
gdpr_random_word Deprecated Random word generator function.
theme_gdpr_dump_table_columns Table format for table columns.