Prepare the schema and data of the sessions table for hashed session ids.

Related topics

File

modules/system/system.install, line 3369
Install, update and uninstall functions for the system module.

Code

function system_update_7086() {

  // Update the session ID fields' description.
  $spec = array(
    'description' => "A session ID (hashed). The value is generated by Drupal's session handlers.",
    'type' => 'varchar',
    'length' => 128,
    'not null' => TRUE,
  );
  db_drop_primary_key('sessions');
  db_change_field('sessions', 'sid', 'sid', $spec, array(
    'primary key' => array(
      'sid',
      'ssid',
    ),
  ));

  // Updates the secure session ID field's description.
  $spec = array(
    'description' => "Secure session ID (hashed). The value is generated by Drupal's session handlers.",
    'type' => 'varchar',
    'length' => 128,
    'not null' => TRUE,
    'default' => '',
  );
  db_drop_primary_key('sessions');
  db_change_field('sessions', 'ssid', 'ssid', $spec, array(
    'primary key' => array(
      'sid',
      'ssid',
    ),
  ));

  // Update all existing sessions.
  if (!variable_get('do_not_hash_session_ids', FALSE)) {
    $sessions = db_query('SELECT sid, ssid FROM {sessions}');
    while ($session = $sessions
      ->fetchAssoc()) {
      $query = db_update('sessions');
      $fields = array();
      if (!empty($session['sid'])) {
        $fields['sid'] = drupal_hash_base64($session['sid']);
        $query
          ->condition('sid', $session['sid']);
      }
      if (!empty($session['ssid'])) {
        $fields['ssid'] = drupal_hash_base64($session['ssid']);
        $query
          ->condition('ssid', $session['ssid']);
      }
      $query
        ->fields($fields)
        ->execute();
    }
  }

  // This variable indicates that the database is ready for hashed session ids.
  variable_set('hashed_session_ids_supported', TRUE);
}