Projet

Général

Profil

Paste
Télécharger (8,95 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / remote_stream_wrapper / remote_stream_wrapper.module @ 87dbc3bf

1
<?php
2

    
3
/**
4
 * @file
5
 * Provides a remote stream wrapper and file field integration.
6
 *
7
 * @todo Add a 'Remote URL' file field widget.
8
 */
9

    
10
/**
11
 * Stream wrapper type flag -- visible and readable using remote files that act like local files.
12
 */
13
define('STREAM_WRAPPERS_REMOTE', STREAM_WRAPPERS_LOCAL | STREAM_WRAPPERS_READ | STREAM_WRAPPERS_VISIBLE);
14

    
15
/**
16
 * Implements hook_menu().
17
 */
18
function remote_stream_wrapper_menu() {
19
  $items = array();
20

    
21
  $items['file/add/remote'] = array(
22
    'title' => 'Remote',
23
    'page callback' => 'drupal_get_form',
24
    'page arguments' => array('remote_stream_wrapper_file_add_form'),
25
    'access callback' => 'remote_stream_wrapper_media_browser_plugin_access',
26
    'type' => MENU_LOCAL_TASK,
27
  );
28

    
29
  // Add image style generation paths for external URLs.
30
  if (module_exists('image')) {
31
    $wrappers = file_get_remote_stream_wrappers();
32
    $directory_path = file_stream_wrapper_get_instance_by_scheme(file_default_scheme())->getDirectoryPath();
33
    $pos = count(explode('/', $directory_path)) + 1;
34
    $item = array(
35
      'page callback' => 'remote_stream_wrapper_image_style_deliver',
36
      'page arguments' => array($pos, $pos + 1),
37
      'access callback' => TRUE,
38
      'type' => MENU_CALLBACK,
39
      'file' => 'remote_stream_wrapper.image.inc',
40
    );
41
    foreach (array_keys($wrappers) as $scheme) {
42
      $items[$directory_path . '/styles/%image_style/' . $scheme] = $item;
43
    }
44
  }
45

    
46
  return $items;
47
}
48

    
49
/**
50
 * Implements hook_permission().
51
 */
52
function remote_stream_wrapper_permission() {
53
  $permission = array();
54

    
55
  if (module_exists('media')) {
56
    $permission['add media from remote urls'] = array(
57
      'title' => t('Add media from remote URLs'),
58
      'description' => t('Add media from remote URLs.'),
59
    );
60
  }
61

    
62
  return $permission;
63
}
64

    
65
/**
66
 * Implements hook_stream_wrappers().
67
 */
68
function remote_stream_wrapper_stream_wrappers() {
69
  $info['http'] = array(
70
    'name' => t('HTTP URLs'),
71
    'class' => 'DrupalRemoteStreamWrapper',
72
    'description' => t('Remote files.'),
73
    'type' => STREAM_WRAPPERS_REMOTE,
74
    'remote' => TRUE,
75
  );
76
  $info['https'] = $info['http'];
77
  $info['https']['name'] = t('HTTPS URLs');
78
  $info['feed'] = $info['http'];
79
  $info['feed']['name'] = t('Feed URLs');
80

    
81
  return $info;
82
}
83

    
84
/**
85
 * Return a list of remote stream wrappers.
86
 */
87
function file_get_remote_stream_wrappers() {
88
  $wrappers = file_get_stream_wrappers(STREAM_WRAPPERS_REMOTE);
89
  foreach ($wrappers as $scheme => $wrapper) {
90
    if (empty($wrapper['remote'])) {
91
      unset($wrappers[$scheme]);
92
    }
93
  }
94
  //$wrappers = array_diff_key($wrappers, file_get_stream_wrappers(STREAM_WRAPPERS_LOCAL_NORMAL));
95
  return $wrappers;
96
}
97

    
98
/**
99
 * Check if a stream wrapper scheme is a remote stream wrapper.
100
 */
101
function file_is_scheme_remote($scheme) {
102
  $wrappers = file_get_remote_stream_wrappers();
103
  return isset($wrappers[$scheme]);
104
}
105

    
106
/**
107
 * Implements hook_file_url_alter().
108
 */
109
function remote_stream_wrapper_file_url_alter(&$uri) {
110
  $scheme = file_uri_scheme($uri);
111
  $wrappers = file_get_remote_stream_wrappers();
112
  if ($scheme && isset($wrappers[$scheme]) && strpos($uri, "$scheme://styles/") === 0) {
113
    $uri = file_default_scheme() . '://' . file_uri_target($uri);
114
  }
115
}
116

    
117
/**
118
 * Copy of image_style_path() for use with remote images.
119
 *
120
 * When image_style_path() is give a file like 'http://example.com/image.png'
121
 * it is converted into 'http://styles/stylename/http/example.com/image.png'
122
 * which will fail image_style_deliver().
123
 */
124
function remote_stream_wrapper_image_style_path($style_name, $uri) {
125
  // Force the image style to be returned with the default file scheme, but
126
  // with the file's original scheme in the path.
127
  return file_default_scheme() . '://styles/' . $style_name . '/' . file_uri_scheme($uri) . '/' . file_uri_target($uri);
128
}
129

    
130
/**
131
 * Implements hook_form_FORM_ID_alter().
132
 *
133
 * Manually add support for the remote stream wrapper in file fields since
134
 * it is read-only.
135
 */
136
function remote_stream_wrapper_form_field_ui_field_edit_form_alter(&$form, &$form_state) {
137
  if ($form['#field']['type'] == 'file' || $form['#field']['type'] == 'image') {
138
    $form['field']['settings']['uri_scheme']['#description'] .= ' ' . t('This only applies to uploaded files and not remote files.');
139
  }
140
}
141

    
142
/**
143
 * Validation callback for remote URLs.
144
 */
145
function remote_stream_wrapper_validate_url($element, &$form_state) {
146
  $value = $element['#value'];
147
  if ($value != '' && !valid_url($value, TRUE)) {
148
    form_error($element, t('Invalid URL %url.', array('%url' => $value)));
149
  }
150
  elseif (!file_stream_wrapper_valid_scheme(file_uri_scheme($value))) {
151
    // Check that the scheme is supported.
152
    form_error($element, t('Remote URLs with the %scheme scheme are not supported.', array('%scheme' => file_uri_scheme($value))));
153
  }
154
  else {
155
    // Check that the file exists.
156
    $request = drupal_http_request($value, array('method' => 'HEAD'));
157
    if (!empty($request->error)) {
158
      form_error($element, t('Unable to fetch file from URL %url (error @code: %error).', array('%url' => $value, '@code' => $request->code, '%error' => $request->error)));
159
    }
160
  }
161
}
162

    
163
/**
164
 * Load a file object by URI.
165
 *
166
 * @param string $uri
167
 *   A string containing the URI, path, or filename.
168
 *
169
 * @return
170
 *   A file object, or FALSE if not found.
171
 */
172
function remote_stream_wrapper_file_load_by_uri($uri) {
173
  $uri = file_stream_wrapper_uri_normalize($uri);
174
  $files = entity_load('file', FALSE, array('uri' => $uri));
175
  return !empty($files) ? reset($files) : FALSE;
176
}
177

    
178
/**
179
 * Helper functon to assemble a new file entity object by URI.
180
 *
181
 * @param string $uri
182
 *   A string containing the URI, path, or filename.
183
 */
184
function remote_stream_wrapper_file_create_by_uri($uri) {
185
  $uri = file_stream_wrapper_uri_normalize($uri);
186

    
187
  $file = new stdClass();
188
  $file->fid = NULL;
189
  $file->uri = $uri;
190
  $file->filename = basename($file->uri);
191
  $file->filemime = file_get_mimetype($file->uri);
192
  $file->uid = $GLOBALS['user']->uid;
193
  $file->status = FILE_STATUS_PERMANENT;
194
  return $file;
195
}
196

    
197
/**
198
 * Implements hook_media_browser_plugin_info().
199
 */
200
function remote_stream_wrapper_media_browser_plugin_info() {
201
  $plugins['remote_file'] = array(
202
    'title' => t('Remote URL'),
203
    'class' => 'RemoteStreamWrapperMediaBrowser',
204
    // Support for Media 1.x browser plugin API.
205
    '#title' => t('Remote URL'),
206
    'access callback' => 'remote_stream_wrapper_media_browser_plugin_access',
207
  );
208
  return $plugins;
209
}
210

    
211
/**
212
 * Media 1.x browser plugin access callback.
213
 *
214
 * Duplicate of RemoteStreamWrapperMediaBrowser::access().
215
 */
216
function remote_stream_wrapper_media_browser_plugin_access() {
217
  return user_access('administer files') || user_access('add media from remote urls');
218
}
219

    
220
/**
221
 * Implements hook_media_browser_plugin_view().
222
 */
223
function remote_stream_wrapper_media_browser_plugin_view($plugin_name, $params) {
224
  if ($plugin_name == 'remote_file') {
225
    if (remote_stream_wrapper_media_browser_plugin_access()) {
226
      $params += array('types' => array());
227
      $form = drupal_get_form('remote_stream_wrapper_file_add_form', $params);
228
      return array(
229
        '#title' => t('Remote URL'),
230
        'form' => array($form),
231
      );
232
    }
233
  }
234
}
235

    
236
/**
237
 * Provides a form for adding media items from remote URLs.
238
 *
239
 * @see remote_stream_wrapper_media_browser_form_submit()
240
 */
241
function remote_stream_wrapper_file_add_form($form, &$form_state, array $options = array()) {
242
  $form['url'] = array(
243
    '#type' => 'textfield',
244
    '#title' => 'URL',
245
    '#attributes' => array('class' => array('media-add-from-remote-url')),
246
    '#maxlength' => 255, // Maximum length of {file_managed}.uri
247
    '#element_validate' => array('remote_stream_wrapper_validate_url'),
248
    '#required' => TRUE,
249
  );
250

    
251
  // @todo Validate against file field allowed types.
252
  $form['#validators'] = array();
253

    
254
  $form['actions'] = array('#type' => 'actions');
255
  $form['actions']['submit'] = array(
256
    '#type' => 'submit',
257
    '#value' => t('Submit'),
258
  );
259
  return $form;
260
}
261

    
262
/**
263
 * Save a file record based on a remote URL.
264
 *
265
 * @see remote_stream_wrapper_media_browser_form()
266
 * @see file_save()
267
 * @see DrupalRemoteStreamWrapper
268
 */
269
function remote_stream_wrapper_file_add_form_submit($form, &$form_state) {
270
  $uri = $url = $form_state['values']['url'];
271

    
272
  try {
273
    $file = remote_stream_wrapper_file_load_by_uri($uri);
274
    if (!$file) {
275
      $file = remote_stream_wrapper_file_create_by_uri($uri);
276
      file_save($file);
277
    }
278
  }
279
  catch (Exception $e) {
280
    form_set_error('url', $e->getMessage());
281
    return;
282
  }
283

    
284
  if (empty($file->fid)) {
285
    form_set_error('url', t('Unable to add file from URL %file.', array('%file' => $url)));
286
    return;
287
  }
288
  else {
289
    $form_state['file'] = $file;
290
  }
291

    
292
  if (drupal_valid_path('file/' . $file->fid . '/edit')) {
293
    $destination = array('destination' => 'admin/content/file');
294
    if (isset($_GET['destination'])) {
295
      $destination = drupal_get_destination();
296
      unset($_GET['destination']);
297
    }
298
    $form_state['redirect'] = array('file/' . $file->fid . '/edit', array('query' => $destination));
299
  }
300
  else {
301
    $form_state['redirect'] = 'admin/content/file';
302
  }
303
}