Projet

Général

Profil

Paste
Télécharger (9,07 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / remote_stream_wrapper / remote_stream_wrapper.module @ 503b3f7b

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_preprocess_image().
108
 */
109
function remote_stream_wrapper_preprocess_image(&$variables) {
110
  static $regex;
111

    
112
  if (!empty($variables['style_name'])) {
113
    if (!isset($regex)) {
114
      $wrappers = file_get_remote_stream_wrappers();
115
      $schemes = implode('|', array_keys($wrappers));
116
      $regex = "#^($schemes)://styles/#";
117
    }
118
    $variables['path'] = preg_replace($regex, file_default_scheme() . '://styles/', $variables['path'], 1);
119
  }
120
}
121

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

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

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

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

    
183
/**
184
 * Helper functon to assemble a new file entity object by URI.
185
 *
186
 * @param string $uri
187
 *   A string containing the URI, path, or filename.
188
 */
189
function remote_stream_wrapper_file_create_by_uri($uri) {
190
  $uri = file_stream_wrapper_uri_normalize($uri);
191

    
192
  $file = new stdClass();
193
  $file->fid = NULL;
194
  $file->uri = $uri;
195
  $file->filename = basename($file->uri);
196
  $file->filemime = file_get_mimetype($file->uri);
197
  $file->uid = $GLOBALS['user']->uid;
198
  $file->status = FILE_STATUS_PERMANENT;
199
  return $file;
200
}
201

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

    
216
/**
217
 * Media 1.x browser plugin access callback.
218
 *
219
 * Duplicate of RemoteStreamWrapperMediaBrowser::access().
220
 */
221
function remote_stream_wrapper_media_browser_plugin_access() {
222
  return user_access('administer files') || user_access('add media from remote urls');
223
}
224

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

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

    
256
  // @todo Validate against file field allowed types.
257
  $form['#validators'] = array();
258

    
259
  $form['actions'] = array('#type' => 'actions');
260
  $form['actions']['submit'] = array(
261
    '#type' => 'submit',
262
    '#value' => t('Submit'),
263
  );
264
  return $form;
265
}
266

    
267
/**
268
 * Save a file record based on a remote URL.
269
 *
270
 * @see remote_stream_wrapper_media_browser_form()
271
 * @see file_save()
272
 * @see DrupalRemoteStreamWrapper
273
 */
274
function remote_stream_wrapper_file_add_form_submit($form, &$form_state) {
275
  $uri = $url = $form_state['values']['url'];
276

    
277
  try {
278
    $file = remote_stream_wrapper_file_load_by_uri($uri);
279
    if (!$file) {
280
      $file = remote_stream_wrapper_file_create_by_uri($uri);
281
      file_save($file);
282
    }
283
  }
284
  catch (Exception $e) {
285
    form_set_error('url', $e->getMessage());
286
    return;
287
  }
288

    
289
  if (empty($file->fid)) {
290
    form_set_error('url', t('Unable to add file from URL %file.', array('%file' => $url)));
291
    return;
292
  }
293
  else {
294
    $form_state['file'] = $file;
295
  }
296

    
297
  if (drupal_valid_path('file/' . $file->fid . '/edit')) {
298
    $destination = array('destination' => 'admin/content/file');
299
    if (isset($_GET['destination'])) {
300
      $destination = drupal_get_destination();
301
      unset($_GET['destination']);
302
    }
303
    $form_state['redirect'] = array('file/' . $file->fid . '/edit', array('query' => $destination));
304
  }
305
  else {
306
    $form_state['redirect'] = 'admin/content/file';
307
  }
308
}