You are here

s3fs.install in S3 File System 7.3

Install, update and uninstall functions for the S3 File System module.

File

s3fs.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the S3 File System module.
 */
use Aws\Sdk;

/**
 * Implements hook_requirements().
 */
function s3fs_requirements($phase) {
  $t = get_t();
  if ($phase != 'runtime') {
    return array();
  }
  if (ini_get('allow_url_fopen')) {
    $requirements['s3fs_allow_url_fopen'] = array(
      'severity' => REQUIREMENT_OK,
      'title' => $t('allow_url_fopen'),
      'value' => $t('Enabled'),
    );
  }
  else {
    $requirements['s3fs_allow_url_fopen'] = array(
      'severity' => REQUIREMENT_ERROR,
      'title' => $t('allow_url_fopen'),
      'value' => $t('Disabled'),
      'description' => $t('The S3 File System module requires that the allow_url_fopen setting be turned on in php.ini.'),
    );
  }
  if (PHP_INT_SIZE === 8) {
    $requirements['s3fs_int64'] = array(
      'title' => $t('PHP architecture'),
      'value' => $t('64-bit'),
      'severity' => REQUIREMENT_OK,
    );
  }
  else {
    $requirements['s3fs_int64'] = array(
      'title' => $t('PHP architecture'),
      'value' => $t('32-bit'),
      'description' => $t('A 64-bit PHP installation is required in order to support files larger than 2GB.'),
      'severity' => REQUIREMENT_WARNING,
    );
  }

  // Check if Composer Manager module exists.
  if (module_exists('composer_manager')) {
    if (class_exists('Aws\\Sdk')) {
      $requirements['s3fs_awssdk'] = array(
        'title' => $t('AWS SDK for PHP'),
        'value' => $t('AWS SDK is installed.'),
        'severity' => REQUIREMENT_OK,
      );
    }
    else {
      $requirements['s3fs_awssdk'] = array(
        'title' => $t('AWS SDK for PHP'),
        'value' => $t('The S3 File System module requires the AWS SDK for PHP v3.x library.'),
        'description' => $t('The AWS SDK could not be loaded.'),
        'severity' => REQUIREMENT_ERROR,
      );
    }
  }
  elseif (module_exists('libraries')) {
    $library = libraries_detect('awssdk');
    if ($library && !empty($library['installed']) && version_compare($library['version'], '3') >= 0) {
      $requirements['s3fs_awssdk'] = array(
        'title' => $t('AWS SDK for PHP'),
        'value' => $t('Installed @ v@version.', array(
          '@version' => $library['version'],
        )),
        'severity' => REQUIREMENT_OK,
      );
    }
    else {
      $requirements['s3fs_awssdk'] = array(
        'title' => $t('AWS SDK for PHP'),
        'value' => $t('The S3 File System module requires the AWS SDK for PHP v3.x library.'),
        'description' => $t('The Libraries module reported the following error: !error_message<br>
          Please check the installation instructions for S3 File System.', array(
          '!error_message' => filter_xss($library['error message']),
        )),
        'severity' => REQUIREMENT_ERROR,
      );
    }
  }
  else {
    $requirements['s3fs_awssdk_manager'] = array(
      'title' => $t('S3FS AWS SDK Management Tool'),
      'value' => $t('Not installed.'),
      'description' => $t('S3 File System requires either the Composer Manager or Libraries
        module to manage the AWS SDK. Please install !composer_manager or !libraries.', array(
        '!composer_manager' => l('Composer Manager', 'http://www.drupal.org/project/composer_manager'),
        '!libraries' => l('Libraries', 'http://www.drupal.org/project/libraries'),
      )),
      'severity' => REQUIREMENT_ERROR,
    );
  }
  if (_s3fs_get_setting('use_instance_profile')) {
    $credentials_file = _s3fs_get_setting('credentials_file');
    $message = array(
      'title' => $t('AWS Custom Credentials File'),
    );
    if (empty($credentials_file) || !is_readable($credentials_file)) {
      $message['value'] = $t('The credentials file is NOT configured correctly in S3FS. It must be set to an
        existing folder on the server, and the webserver must have read permissions on that folder.');
      $message['severity'] = REQUIREMENT_ERROR;
    }
    else {
      $message['value'] = $t('The custom credentials file for S3FS is configured correctly.');
      $message['severity'] = REQUIREMENT_INFO;
    }
    $requirements['s3fs_credentials_file_status'] = $message;
  }
  return $requirements;
}

/**
 * Implements hook_uninstall().
 */
function s3fs_uninstall() {

  // Load our module file, since s3fs is current disabled.
  drupal_load('module', 's3fs');

  // Remove all the s3fs settings variables.
  foreach (_s3fs_get_config() as $key => $value) {
    variable_del("s3fs_{$key}");
  }

  // Remove 's3' from the list of public file schemas.
  $public_schema = variable_get('file_public_schema');
  if (is_array($public_schema) && in_array('s3', $public_schema)) {
    $public_schema = array_diff($public_schema, array(
      's3',
    ));
    variable_set('file_public_schema', $public_schema);
  }
}

/**
 * Implements hook_schema().
 */
function s3fs_schema() {
  $schema = array();
  $schema['s3fs_file'] = array(
    'description' => 'Stores metadata about files in S3.',
    'fields' => array(
      'uri' => array(
        'description' => 'The S3 URI of the file.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'binary' => TRUE,
      ),
      'filesize' => array(
        'description' => 'The size of the file in bytes.',
        'type' => 'int',
        'size' => 'big',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'timestamp' => array(
        'description' => 'UNIX timestamp for when the file was added.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'dir' => array(
        'description' => 'Boolean indicating whether or not this object is a directory.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'version' => array(
        'description' => 'The S3 VersionId of the object.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => FALSE,
        'default' => '',
      ),
    ),
    'primary key' => array(
      'uri',
    ),
  );
  return $schema;
}

/**
 * Implements hook_install().
 *
 * Adds s3:// to the the core file module's list of public schema.
 * See https://www.drupal.org/node/2305017 for more info.
 */
function s3fs_install() {
  _s3fs_register_s3_as_public_schema();
}

/**
 * Allow large filesize values in the S3 File Metadata cache.
 */
function s3fs_update_7000() {
  $spec = array(
    'description' => 'The size of the file in bytes.',
    'size' => 'big',
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0,
  );
  db_change_field('s3fs_file', 'filesize', 'filesize', $spec);
  $config = _s3fs_get_config();
  if (!empty($config['bucket']) && !empty($config['region'])) {
    _s3fs_refresh_cache($config);
  }
  else {
    drupal_set_message(t('S3 bucket name or region unknown. Unable to refresh metadata cache table.'), 'error');
  }
}

/**
 * Variable updates.
 */
function s3fs_update_7001() {

  // Rename s3fs_customhost to s3fs_use_customhost. And since the code now
  // assumes that the variable is set, set it to FALSE if it's not.
  $customhost = variable_get('s3fs_customhost', 'unset');
  if ($customhost !== 'unset') {
    variable_set('s3fs_use_customhost', $customhost);
    variable_del('s3fs_customhost');
  }
  else {
    variable_set('s3fs_use_customhost', FALSE);
  }

  // Rename s3fs_cname to s3fs_use_cname.
  $cname = variable_get('s3fs_cname', 'unset');
  if ($cname !== 'unset') {
    variable_set('s3fs_use_cname', $cname);
    variable_del('s3fs_cname');
  }
  else {
    variable_set('s3fs_use_cname', FALSE);
  }
}

/**
 * Updates the s3fs_file table to use case sensitive collation.
 */
function s3fs_update_7002() {
  $options = Database::getConnectionInfo('default');
  switch ($options['default']['driver']) {
    case 'pgsql':

      // Postgres uses binary collation by default
      break;
    case 'sqlite':

      // SQLite uses binary collation by default
      break;
    case 'mysql':

      // Set MySQL tables to use case-sensitive collation.
      db_query("ALTER TABLE {s3fs_file} CONVERT TO CHARACTER SET utf8 COLLATE utf8_bin");
      break;
  }
}

/**
 * Rename the variable s3fs_allow_relative to s3fs_use_relative_urls.
 */
function s3fs_update_7003() {
  $allow_relative = variable_get('s3fs_allow_relative', 'unset');
  if ($allow_relative !== 'unset') {
    variable_set('s3fs_use_relative_urls', $allow_relative);
    variable_del('s3fs_allow_relative');
  }
}

/**
 * Remove the s3fs_use_relative_urls variable.
 */
function s3fs_update_7004() {
  $use_relative = variable_get('s3fs_use_relative_urls', 'unset');
  if ($use_relative !== 'unset') {
    variable_del('s3fs_use_relative_urls');
  }
}

/**
 * Remove the extra amazons3 variables that are not necessary for
 * compatibility with amazons3_cors.
 */
function s3fs_update_7005() {
  $variable_names = db_select('variable', 'v')
    ->fields('v', array(
    'name',
  ))
    ->condition('name', 'amazons3_%', 'LIKE')
    ->execute()
    ->fetchCol(0);
  foreach ($variable_names as $name) {
    if ($name != 'amazons3_bucket') {
      variable_del($name);
    }
  }
}

/**
 * Add Version schema change to existing installs.
 */
function s3fs_update_7006() {
  $version = array(
    'description' => 'The S3 VersionId of the object.',
    'type' => 'varchar',
    'length' => 32,
    'not null' => FALSE,
    'default' => '',
  );
  db_add_field('s3fs_file', 'version', $version);
}

/**
 * Updates to the s3fs_file table.
 */
function s3fs_update_7200() {

  // These two fields are no longer used as of 7.x-2.0.
  db_drop_field('s3fs_file', 'uid');
  db_drop_field('s3fs_file', 'mode');

  // Changed the version field to a varchar, to save space for objects that
  // don't have a VersionID.
  $spec = array(
    'description' => 'The S3 VersionId of the object.',
    'type' => 'varchar',
    'length' => 32,
    'not null' => FALSE,
    'default' => '',
  );
  db_change_field('s3fs_file', 'version', 'version', $spec);
}

/**
 * Adds the s3:// schema to the list of public schemas.
 */
function s3fs_update_7201() {
  _s3fs_register_s3_as_public_schema();
}

/**
 * Converts the `uri` column of the metadata table to BINARY.
 */
function s3fs_update_7202() {

  // As of the introduction of this update hook, s3fs_schema() now defines
  // the `uri` column with "binary = TRUE".
  $schema = s3fs_schema();
  db_change_field('s3fs_file', 'uri', 'uri', $schema['s3fs_file']['fields']['uri']);
}

/**
 * Replace the s3fs_use_instance_profile variable with s3fs_awssdk2_use_instance_profile.
 */
function s3fs_update_7203() {
  $old_var = variable_get('s3fs_use_instance_profile', '');
  if ($old_var !== '') {
    variable_del('s3fs_use_instance_profile');
  }
  variable_set('s3fs_awssdk2_use_instance_profile', $old_var);
}

/**
 * Fix the bug in the previous update.
 */
function s3fs_update_7204() {

  # s3fs_update_7204() previously set the s3fs_awssdk2_use_instance_profile variable to 'unset' if

  # the previous version of the variable wasn't set. This is disastrous, as 'unset' equates to boolean true.

  # The problem has been fixed in the hook, but for anyone who updated s3fs before this new hook was written

  # will have gotten the wrong value stored into their s3fs_awssdk2_use_instance_profile variable.
  $var = variable_get('s3fs_awssdk2_use_instance_profile');
  if ($var == 'unset') {
    variable_del('s3fs_awssdk2_use_instance_profile');
  }
}

/**
* Adds the option to enable/disable querying of Versioned files
* during the _s3fs_reset_media_cache() Action
*/
function s3fs_update_7205() {

  # Sets the boolean flag of use_versioning to the default value of TRUE

  # This will ensure that Versioning is set to default on module installation and enabling
  variable_set('s3fs_use_versioning', TRUE);
}

/**
 * Add s3fs_domain_root as a variable, which allows for mapping of a CDN to a specific
 * folder, such as "mybucket/staging/s3fs-public". By default, map to entire bucket.
 */
function s3fs_update_7206() {
  variable_set('s3fs_domain_root', 'none');
}

/**
 * Update s3fs variable names to support AWS SDK v3.
 */
function s3fs_update_7207() {
  $var_conversions = array(
    'awssdk2_access_key' => 'awssdk_access_key',
    'awssdk2_secret_key' => 'awssdk_secret_key',
    's3fs_awssdk2_access_key' => 's3fs_awssdk_access_key',
    's3fs_awssdk2_secret_key' => 's3fs_awssdk_secret_key',
    'awssdk2_use_instance_profile' => 's3fs_use_instance_profile',
    'awssdk2_default_cache_config' => 's3fs_credentials_file',
  );
  foreach ($var_conversions as $old_var => $new_var) {
    $old_value = variable_get($old_var, 'unset');
    if ($old_value != 'unset') {
      variable_set($new_var, $old_value);
      variable_del($old_var);
    }
  }
}

/******************************************************************************
 *                            INTERNAL FUNCTIONS
 ******************************************************************************/
function _s3fs_register_s3_as_public_schema() {
  $public_schema = variable_get('file_public_schema', array(
    'public',
  ));
  if (!in_array('s3', $public_schema)) {
    $public_schema[] = 's3';
  }
  variable_set('file_public_schema', $public_schema);
}

Functions

Namesort descending Description
s3fs_install Implements hook_install().
s3fs_requirements Implements hook_requirements().
s3fs_schema Implements hook_schema().
s3fs_uninstall Implements hook_uninstall().
s3fs_update_7000 Allow large filesize values in the S3 File Metadata cache.
s3fs_update_7001 Variable updates.
s3fs_update_7002 Updates the s3fs_file table to use case sensitive collation.
s3fs_update_7003 Rename the variable s3fs_allow_relative to s3fs_use_relative_urls.
s3fs_update_7004 Remove the s3fs_use_relative_urls variable.
s3fs_update_7005 Remove the extra amazons3 variables that are not necessary for compatibility with amazons3_cors.
s3fs_update_7006 Add Version schema change to existing installs.
s3fs_update_7200 Updates to the s3fs_file table.
s3fs_update_7201 Adds the s3:// schema to the list of public schemas.
s3fs_update_7202 Converts the `uri` column of the metadata table to BINARY.
s3fs_update_7203 Replace the s3fs_use_instance_profile variable with s3fs_awssdk2_use_instance_profile.
s3fs_update_7204 Fix the bug in the previous update.
s3fs_update_7205 Adds the option to enable/disable querying of Versioned files during the _s3fs_reset_media_cache() Action
s3fs_update_7206 Add s3fs_domain_root as a variable, which allows for mapping of a CDN to a specific folder, such as "mybucket/staging/s3fs-public". By default, map to entire bucket.
s3fs_update_7207 Update s3fs variable names to support AWS SDK v3.
_s3fs_register_s3_as_public_schema