remote_stream_wrapper.module in Remote Stream Wrapper 7
Same filename and directory in other branches
Provides a remote stream wrapper and file field integration.
@todo Add a 'Remote URL' file field widget.
File
remote_stream_wrapper.moduleView source
<?php
/**
* @file
* Provides a remote stream wrapper and file field integration.
*
* @todo Add a 'Remote URL' file field widget.
*/
/**
* Stream wrapper type flag -- visible and readable using remote files that act like local files.
*/
define('STREAM_WRAPPERS_REMOTE', STREAM_WRAPPERS_LOCAL | STREAM_WRAPPERS_READ | STREAM_WRAPPERS_VISIBLE);
/**
* Implements hook_menu().
*/
function remote_stream_wrapper_menu() {
$items = array();
$items['file/add/remote'] = array(
'title' => 'Remote',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'remote_stream_wrapper_file_add_form',
),
'access callback' => 'remote_stream_wrapper_media_browser_plugin_access',
'type' => MENU_LOCAL_TASK,
);
// Add image style generation paths for external URLs.
if (module_exists('image')) {
$wrappers = file_get_remote_stream_wrappers();
$directory_path = file_stream_wrapper_get_instance_by_scheme(file_default_scheme())
->getDirectoryPath();
$pos = count(explode('/', $directory_path)) + 1;
$item = array(
'page callback' => 'remote_stream_wrapper_image_style_deliver',
'page arguments' => array(
$pos,
$pos + 1,
),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
'file' => 'remote_stream_wrapper.image.inc',
);
foreach (array_keys($wrappers) as $scheme) {
$items[$directory_path . '/styles/%image_style/' . $scheme] = $item;
}
}
return $items;
}
/**
* Implements hook_permission().
*/
function remote_stream_wrapper_permission() {
$permission = array();
if (module_exists('media')) {
$permission['add media from remote urls'] = array(
'title' => t('Add media from remote URLs'),
'description' => t('Add media from remote URLs.'),
);
}
return $permission;
}
/**
* Implements hook_stream_wrappers().
*/
function remote_stream_wrapper_stream_wrappers() {
$info['http'] = array(
'name' => t('HTTP URLs'),
'class' => 'DrupalRemoteStreamWrapper',
'description' => t('Remote files.'),
'type' => STREAM_WRAPPERS_REMOTE,
'remote' => TRUE,
);
$info['https'] = $info['http'];
$info['https']['name'] = t('HTTPS URLs');
$info['feed'] = $info['http'];
$info['feed']['name'] = t('Feed URLs');
return $info;
}
/**
* Return a list of remote stream wrappers.
*/
function file_get_remote_stream_wrappers() {
$wrappers =& drupal_static(__FUNCTION__);
if (!isset($wrappers)) {
$wrappers = file_get_stream_wrappers(STREAM_WRAPPERS_REMOTE);
foreach ($wrappers as $scheme => $wrapper) {
if (empty($wrapper['remote'])) {
unset($wrappers[$scheme]);
}
}
//$wrappers = array_diff_key($wrappers, file_get_stream_wrappers(STREAM_WRAPPERS_LOCAL_NORMAL
}
return $wrappers;
}
/**
* Check if a stream wrapper scheme is a remote stream wrapper.
*/
function file_is_scheme_remote($scheme) {
$wrappers = file_get_remote_stream_wrappers();
return isset($wrappers[$scheme]);
}
/**
* Implements hook_file_url_alter().
*/
function remote_stream_wrapper_file_url_alter(&$uri) {
$scheme = file_uri_scheme($uri);
if ($scheme && file_is_scheme_remote($scheme) && strpos($uri, "{$scheme}://styles/") === 0) {
$uri = file_default_scheme() . '://' . file_uri_target($uri);
if (!variable_get('clean_url') && file_uri_scheme($uri) == 'public' && !file_exists($uri)) {
$directory_path = file_stream_wrapper_get_instance_by_uri($uri)
->getDirectoryPath();
$uri = url($directory_path . '/' . file_uri_target($uri), array(
'absolute' => TRUE,
));
}
}
}
/**
* Copy of image_style_path() for use with remote images.
*
* When image_style_path() is give a file like 'http://example.com/image.png'
* it is converted into 'http://styles/stylename/http/example.com/image.png'
* which will fail image_style_deliver().
*/
function remote_stream_wrapper_image_style_path($style_name, $uri) {
// Force the image style to be returned with the default file scheme, but
// with the file's original scheme in the path.
return file_default_scheme() . '://styles/' . $style_name . '/' . file_uri_scheme($uri) . '/' . file_uri_target($uri);
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Manually add support for the remote stream wrapper in file fields since
* it is read-only.
*/
function remote_stream_wrapper_form_field_ui_field_edit_form_alter(&$form, &$form_state) {
if ($form['#field']['type'] == 'file' || $form['#field']['type'] == 'image') {
$form['field']['settings']['uri_scheme']['#description'] .= ' ' . t('This only applies to uploaded files and not remote files.');
}
}
/**
* Validation callback for remote URLs.
*/
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,
)));
}
}
}
/**
* Load a file object by URI.
*
* @param string $uri
* A string containing the URI, path, or filename.
*
* @return
* A file object, or FALSE if not found.
*/
function remote_stream_wrapper_file_load_by_uri($uri) {
$uri = file_stream_wrapper_uri_normalize($uri);
$files = entity_load('file', FALSE, array(
'uri' => $uri,
));
return !empty($files) ? reset($files) : FALSE;
}
/**
* Helper functon to assemble a new file entity object by URI.
*
* @param string $uri
* A string containing the URI, path, or filename.
*/
function remote_stream_wrapper_file_create_by_uri($uri) {
$uri = file_stream_wrapper_uri_normalize($uri);
$file = new stdClass();
$file->fid = NULL;
$file->uri = $uri;
$file->filename = basename($file->uri);
$file->filemime = file_get_mimetype($file->uri);
$file->uid = $GLOBALS['user']->uid;
$file->status = FILE_STATUS_PERMANENT;
return $file;
}
/**
* Implements hook_media_browser_plugin_info().
*/
function remote_stream_wrapper_media_browser_plugin_info() {
$plugins['remote_file'] = array(
'title' => t('Remote URL'),
'class' => 'RemoteStreamWrapperMediaBrowser',
// Support for Media 1.x browser plugin API.
'#title' => t('Remote URL'),
'access callback' => 'remote_stream_wrapper_media_browser_plugin_access',
);
return $plugins;
}
/**
* Media 1.x browser plugin access callback.
*
* Duplicate of RemoteStreamWrapperMediaBrowser::access().
*/
function remote_stream_wrapper_media_browser_plugin_access() {
return user_access('administer files') || user_access('add media from remote urls');
}
/**
* Implements hook_media_browser_plugin_view().
*/
function remote_stream_wrapper_media_browser_plugin_view($plugin_name, $params) {
if ($plugin_name == 'remote_file') {
if (remote_stream_wrapper_media_browser_plugin_access()) {
$params += array(
'types' => array(),
);
$form = drupal_get_form('remote_stream_wrapper_file_add_form', $params);
return array(
'#title' => t('Remote URL'),
'form' => array(
$form,
),
);
}
}
}
/**
* Provides a form for adding media items from remote URLs.
*
* @see remote_stream_wrapper_media_browser_form_submit()
*/
function remote_stream_wrapper_file_add_form($form, &$form_state, array $options = array()) {
$form['url'] = array(
'#type' => 'textfield',
'#title' => 'URL',
'#attributes' => array(
'class' => array(
'media-add-from-remote-url',
),
),
'#maxlength' => 255,
// Maximum length of {file_managed}.uri
'#element_validate' => array(
'remote_stream_wrapper_validate_url',
),
'#required' => TRUE,
);
// @todo Validate against file field allowed types.
$form['#validators'] = array();
$form['actions'] = array(
'#type' => 'actions',
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
/**
* Save a file record based on a remote URL.
*
* @see remote_stream_wrapper_media_browser_form()
* @see file_save()
* @see DrupalRemoteStreamWrapper
*/
function remote_stream_wrapper_file_add_form_submit($form, &$form_state) {
$uri = $url = trim($form_state['values']['url']);
try {
$file = remote_stream_wrapper_file_load_by_uri($uri);
if (!$file) {
$file = remote_stream_wrapper_file_create_by_uri($uri);
file_save($file);
}
} catch (Exception $e) {
form_set_error('url', $e
->getMessage());
return;
}
if (empty($file->fid)) {
form_set_error('url', t('Unable to add file from URL %file.', array(
'%file' => $url,
)));
return;
}
else {
$form_state['file'] = $file;
}
if (drupal_valid_path('file/' . $file->fid . '/edit')) {
$destination = array(
'destination' => 'admin/content/file',
);
if (isset($_GET['destination'])) {
$destination = drupal_get_destination();
unset($_GET['destination']);
}
$form_state['redirect'] = array(
'file/' . $file->fid . '/edit',
array(
'query' => $destination,
),
);
}
else {
$form_state['redirect'] = 'admin/content/file';
}
}
/**
* Implements hook_file_delete().
*/
function remote_stream_wrapper_file_delete($file) {
$scheme = file_uri_scheme($file->uri);
if ($scheme && file_is_scheme_remote($scheme)) {
remote_stream_wrapper_image_path_flush($file->uri);
}
}
function remote_stream_wrapper_image_path_flush($path) {
$styles = image_styles();
foreach ($styles as $style) {
$image_path = remote_stream_wrapper_image_style_path($style['name'], $path);
if (file_exists($image_path)) {
file_unmanaged_delete($image_path);
}
}
}
Functions
Constants
Name | Description |
---|---|
STREAM_WRAPPERS_REMOTE | Stream wrapper type flag -- visible and readable using remote files that act like local files. |