You are here

public function TranscoderAbstractionFactoryZencoder::adminSettingsValidate in Video 7.2

Validate admin settings. This will call when Drupal admin settings validate.

Overrides TranscoderFactoryInterface::adminSettingsValidate

File

transcoders/TranscoderAbstractionFactoryZencoder.inc, line 464
File containing class TranscoderAbstractionFactoryZencoder

Class

TranscoderAbstractionFactoryZencoder
Class that handles Zencoder transcoding.

Code

public function adminSettingsValidate($form, &$form_state) {
  $v = $form_state['values'];
  if (variable_get('video_zencoder_api_key', FALSE)) {

    // Workaround for the use of the same variable in FFmpeg
    $form_state['values']['video_thumbnail_count'] = $form_state['values']['video_thumbnail_count_zc'];
    unset($form_state['values']['video_thumbnail_count_zc']);

    // Check for username if password was provided.
    if (empty($v['video_zencoder_http_username']) && !empty($v['video_zencoder_http_password'])) {
      form_set_error('video_zencoder_http_username', t('You must also provide a username.'));
    }

    // Check for password if username was provided.
    if (!empty($v['video_zencoder_http_username']) && empty($v['video_zencoder_http_password'])) {

      // If no password was provided, set the stored password. If there is no stored
      // password, set form error.
      $stored_password = variable_get('video_zencoder_http_password');
      if (empty($stored_password)) {
        form_set_error('video_zencoder_http_password', t('You must also provide a password.'));
      }
      else {
        form_set_value($form['zencoder_info']['video_zencoder_http_password'], $stored_password, $form_state);
      }
    }

    // Check the postback URL if validation hasn't been disabled
    if (empty($v['video_zencoder_postback_donotvalidate'])) {
      $testurl = $v['video_zencoder_postback'];
      $testcode = md5(mt_rand(0, REQUEST_TIME));
      if (strpos($testurl, '?') === FALSE) {
        $testurl .= '?test=1';
      }
      else {
        $testurl .= '&test=1';
      }
      variable_set('video_postback_test', $testcode);
      $result = drupal_http_request($testurl);
      variable_del('video_postback_test');
      $error = NULL;
      if ($result->code != 200) {
        $error = t('The postback URL cannot be retrieved: @error (@code).', array(
          '@code' => $result->code,
          '@error' => empty($result->error) ? t('unknown error') : $result->error,
        ));
      }
      elseif (empty($result->data) || trim($result->data) != $testcode) {
        $error = t('The postback URL is not valid: returned data contains unexpected value "@value".', array(
          '@value' => $result->data,
        ));
      }
      if ($error != NULL) {
        form_error($form['zencoder_info']['video_zencoder_postback'], $error);
      }
    }
  }
  else {

    // check terms and condition
    if ($form_state['values']['agree_terms_zencoder'] == 0) {
      form_set_error('agree_terms_zencoder', t('You must agree to the !link.', array(
        '!link' => l(t('terms and conditions'), 'http://zencoder.com/terms'),
      )));
    }

    // check for email exists
    // Validate the e-mail address:
    if ($error = user_validate_mail($form_state['values']['zencoder_username'])) {
      form_set_error('zencoder_username', $error);
    }

    // get the API key from zencoder and save it to variable
    if (!form_get_errors()) {
      $mail = $form_state['values']['zencoder_username'];
      $result = $this
        ->createUser($mail);
      if ($result !== TRUE) {
        form_set_error('zencoder_username', $result);
      }
      else {

        // Unset the form values because they do not need to be saved.
        unset($form_state['values']['zencoder_username']);
        unset($form_state['values']['agree_terms_zencoder']);
      }
    }
  }
}