You are here

function amazons3_admin_validate in AmazonS3 7

Same name and namespace in other branches
  1. 7.2 amazons3.admin.inc \amazons3_admin_validate()

Validate the admin form.

File

./amazons3.module, line 183
Provides S3 stream wrapper

Code

function amazons3_admin_validate($form, &$form_state) {
  $bucket = $form_state['values']['amazons3_bucket'];
  $cloudfront = $form_state['values']['amazons3_cloudfront'];
  if ($cloudfront) {
    $keypair = variable_get('aws_cloudfront_keypair', '');
    $pem = variable_get('aws_cloudfront_pem', '');
    if (empty($keypair) || empty($pem)) {
      form_set_error('amazons3_cloudfront', t('You must configure your CloudFront credentials in the awksdk module.'));
    }
  }
  if (!libraries_load('awssdk')) {
    form_set_error('amazons3_bucket', t('Unable to load the AWS SDK. Please check you have installed the library correctly and configured your S3 credentials.'));
  }
  elseif (!class_exists('AmazonS3')) {
    form_set_error('amazons3_bucket', t('Cannot load AmazonS3 class. Please check the awssdk is installed correctly'));
  }
  else {
    try {
      $s3 = new AmazonS3();
      if (!empty($form_state['values']['amazons3_hostname'])) {
        $s3
          ->set_hostname($form_state['values']['amazons3_hostname']);
      }
      $response = $s3
        ->list_objects($bucket, array(
        'max-keys' => 1,
      ));
      if (!$s3
        ->if_bucket_exists($bucket) || !$response
        ->isOK()) {
        form_set_error('amazons3_bucket', t('The S3 access credentials are invalid or the bucket does not exist'));
      }
    } catch (RequestCore_Exception $e) {
      if (strstr($e
        ->getMessage(), 'SSL certificate problem')) {
        form_set_error('amazons3_bucket', t('There was a problem with the SSL certificate. Try setting AWS_CERTIFICATE_AUTHORITY to true in "libraries/awssdk/config.inc.php". You may also have a curl library (e.g. the default shipped with MAMP) that does not contain trust certificates for the major authorities. The following exception was thrown: @exception', array(
          '@exception' => $e
            ->getMessage(),
        )));
      }
      else {
        form_set_error('amazons3_bucket', t('There was a problem connecting to S3. The following exception was thrown: @exception', array(
          '@exception' => $e
            ->getMessage(),
        )));
      }
    } catch (Exception $e) {
      form_set_error('amazons3_bucket', t('There was a problem using S3. The following exception was thrown: @exception', array(
        '@exception' => $e
          ->getMessage(),
      )));
    }
  }
}