You are here

function remote_stream_wrapper_validate_url in Remote Stream Wrapper 7

Validation callback for remote URLs.

1 string reference to 'remote_stream_wrapper_validate_url'
remote_stream_wrapper_file_add_form in ./remote_stream_wrapper.module
Provides a form for adding media items from remote URLs.

File

./remote_stream_wrapper.module, line 151
Provides a remote stream wrapper and file field integration.

Code

function remote_stream_wrapper_validate_url($element, &$form_state) {
  $value = trim($element['#value']);
  if ($value != '' && !valid_url($value, TRUE)) {
    form_error($element, t('Invalid URL %url.', array(
      '%url' => $value,
    )));
  }
  elseif (!file_stream_wrapper_valid_scheme(file_uri_scheme($value))) {

    // Check that the scheme is supported.
    form_error($element, t('Remote URLs with the %scheme scheme are not supported.', array(
      '%scheme' => file_uri_scheme($value),
    )));
  }
  else {

    // Check that the file exists.
    $request = drupal_http_request($value, array(
      'method' => 'HEAD',
    ));
    if (!empty($request->error)) {
      form_error($element, t('Unable to fetch file from URL %url (error @code: %error).', array(
        '%url' => $value,
        '@code' => $request->code,
        '%error' => $request->error,
      )));
    }
  }
}