You are here

function encrypt_admin_settings_submit_handler in Encrypt 6

Same name and namespace in other branches
  1. 7 includes/encrypt.admin.inc \encrypt_admin_settings_submit_handler()

Submit form function for encrypt_admin_settings(). Creates key file if new none exist in the path settings

1 string reference to 'encrypt_admin_settings_submit_handler'
encrypt_admin_settings in includes/encrypt.admin.inc
Menu callback; displays the encrypt module settings page.

File

includes/encrypt.admin.inc, line 104
This file holds the functions for the encrypt Admin settings.

Code

function encrypt_admin_settings_submit_handler($form, &$form_state) {
  $key_path = $form_state['values']['encrypt_secure_key_path'];

  // Check path
  if (!empty($key_path)) {
    $key_path = rtrim($key_path, '/\\');
    $key_file = $key_path . '/' . ENCRYPT_SECURE_KEY_FILE;

    // Check for contents of file; do not write over the key
    if (file_exists($key_file)) {
      if (file_get_contents($key_file)) {
        return;
      }
    }

    // Attempt to open file
    if (!($open_file = fopen($key_file, 'wb'))) {
      drupal_set_message(t('Unable to open key file'));
    }
    else {

      // Create a default key.
      $new_key = md5(uniqid(mt_rand(0, mt_rand()), TRUE)) . md5(uniqid(mt_rand(0, mt_rand()), TRUE));

      // Create file with new key in it.
      fwrite($open_file, $new_key);
      fclose($open_file);

      // Ensure that the file is only readable and writable by owner
      // TODO: Test this more in different systems
      if (drupal_strtoupper(drupal_substr(PHP_OS, 0, 3)) != "WIN") {
        chmod($key_file, 0600);
      }
      drupal_set_message(t('New key created.'));
    }
  }
}