You are here

public function video_s3::admin_settings_validate in Video 6.5

Same name and namespace in other branches
  1. 6.4 plugins/video_s3/filesystem/video_s3.inc \video_s3::admin_settings_validate()

Overrides video_plugin::admin_settings_validate

File

plugins/video_s3/filesystem/video_s3.inc, line 217
Class file used to store videos in Amazon S3.

Class

video_s3

Code

public function admin_settings_validate($form, &$form_state) {

  // Check for CURL
  if (!extension_loaded('curl') && !@dl(PHP_SHLIB_SUFFIX == 'so' ? 'curl.so' : 'php_curl.dll')) {
    form_set_error('amazon_s3', t('The CURL extension is not loaded.'));
    return;
  }
  $bucket = $form_state['values']['amazon_s3_bucket'];

  // S3 buckets must contain only lower case alphanumeric characters, dots and dashes.
  if (!$this
    ->isValidBucketName($bucket)) {
    form_set_error('amazon_s3_bucket', t('S3 bucket names must contain only lower case alphanumeric characters, dots and dashes.'));
  }
  $ssl = isset($form_state['values']['amazon_s3_ssl']) && $form_state['values']['amazon_s3_ssl'];
  $access_key = $form_state['values']['amazon_s3_access_key'];
  $secret_key = $form_state['values']['amazon_s3_secret_access_key'];
  if (empty($access_key) || empty($secret_key)) {

    // There is no point in continuing if there is no access info
    return;
  }

  // Lets verify our credentials and verify our bucket exists, if not attempt to create it.
  $s3 = new video_amazon_s3();
  $s3
    ->connect($access_key, $secret_key, $ssl);
  $buckets = $s3->s3
    ->get_bucket_list();
  if (!in_array($bucket, $buckets)) {

    // Create a bucket with public read access
    // @todo: region selection
    $response = $s3->s3
      ->create_bucket($bucket, AmazonS3::REGION_US_E1, AmazonS3::ACL_PUBLIC);
    if ($response
      ->isOK()) {
      drupal_set_message(t('Successfully created the bucket %bucket.', array(
        '%bucket' => $bucket,
      )));
    }
    else {
      form_set_error('amazon_s3_bucket', t('Could not verify or create the bucket %bucket.', array(
        '%bucket' => $bucket,
      )));
      $bucket = NULL;
    }
  }

  // Always check the access rights, in case the bucket was created
  // outside of Drupal or before the Zencoder module was active.
  if ($bucket != NULL && module_exists('video_zencoder')) {
    if ($s3
      ->setZencoderAccessPolicy($bucket)) {
      drupal_set_message(t('Successfully granted write access for bucket %bucket to Zencoder.', array(
        '%bucket' => $bucket,
      )));
    }
  }
}