Projet

Général

Profil

Paste
Télécharger (10 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / media / modules / media_internet / media_internet.module @ e4215af7

1
<?php
2

    
3
/**
4
 * Implements hook_hook_info().
5
 */
6
function media_internet_hook_info() {
7
  $hooks = array(
8
    'media_internet_providers',
9
  );
10

    
11
  return array_fill_keys($hooks, array('group' => 'media'));
12
}
13

    
14
/**
15
 * Implements hook_menu().
16
 */
17
function media_internet_menu() {
18
  $items['file/add/web'] = array(
19
    'title' => 'Web',
20
    'description' => 'Add internet files to your media library.',
21
    'page callback' => 'drupal_get_form',
22
    'page arguments' => array('media_internet_add_upload'),
23
    'access callback' => 'media_internet_access',
24
    'type' => MENU_LOCAL_TASK,
25
    'file' => 'file_entity.pages.inc',
26
    'file path' => drupal_get_path('module', 'file_entity'),
27
  );
28

    
29
  return $items;
30
}
31

    
32
/**
33
 * Access callback for the media_internet media browser plugin.
34
 */
35
function media_internet_access($account = NULL) {
36
  return user_access('administer files', $account) || user_access('add media from remote sources', $account);
37
}
38

    
39
/**
40
 * Implement hook_permission().
41
 */
42
function media_internet_permission() {
43
  return array(
44
    'add media from remote sources' => array(
45
      'title' => t('Add media from remote services'),
46
      'description' => t('Add media from remote sources such as other websites, YouTube, etc'),
47
    ),
48
  );
49
}
50

    
51
/**
52
 * Implements hook_theme().
53
 */
54
function media_internet_theme() {
55
  return array(
56
    // media_internet.pages.inc.
57
    'media_internet_embed_help' => array(
58
      'variables' => array('description' => NULL, 'supported_providers' => NULL),
59
    ),
60
  );
61
}
62

    
63
/**
64
 * Gets the list of Internet media providers.
65
 *
66
 * Each 'Provider' has a title and a class which can handle saving remote files.
67
 * Providers are each given a turn at parsing a user-submitted URL or embed code
68
 * and, if they recognize that it belongs to a service or protocol they support,
69
 * they store a representation of it as a file object in file_managed.
70
 *
71
 * @return array
72
 *   An associative array of provider information keyed by provider name.
73
 */
74
function media_internet_get_providers() {
75
  $providers = &drupal_static(__FUNCTION__);
76

    
77
  if (!isset($providers)) {
78
    foreach (module_implements('media_internet_providers') as $module) {
79
      foreach (module_invoke($module, 'media_internet_providers') as $class => $info) {
80
        $providers[$class] = $info;
81

    
82
        // Store the name of the module which declared the provider.
83
        $providers[$class]['module'] = $module;
84

    
85
        // Assign a default value to providers which don't specify a weight.
86
        if (!isset($providers[$class]['weight'])) {
87
          $providers[$class]['weight'] = 0;
88
        }
89
      }
90
    }
91

    
92
    // Allow modules to alter the list of providers.
93
    drupal_alter('media_internet_providers', $providers);
94

    
95
    // Sort the providers by weight.
96
    uasort($providers, 'drupal_sort_weight');
97
  }
98

    
99
  return $providers;
100
}
101

    
102
/**
103
 * Finds the appropriate provider for a given URL or embed_string
104
 *
105
 * Each provider has a claim() method which it uses to tell media_internet
106
 * that it should handle this input.  We cycle through all providers to find
107
 * the right one.
108
 *
109
 * @todo: Make this into a normal hook or something because we have to instantiate
110
 * each class to test and that's not right.
111
 */
112
function media_internet_get_provider($embed_string) {
113
  foreach (media_internet_get_providers() as $class_name => $nothing) {
114
    module_load_include('inc', $nothing['module'], 'includes/' . $class_name);
115
    $p = new $class_name($embed_string);
116
    if ($p->claim($embed_string)) {
117
      return $p;
118
    }
119
  }
120
  throw new MediaInternetNoHandlerException(t('Unable to handle the provided embed string or URL.'));
121
}
122

    
123
/**
124
 * Returns HTML for help text based on supported internet media providers.
125
 *
126
 * @param $variables
127
 *   An associative array containing:
128
 *   - description: The normal description for this field, specified by the
129
 *     user.
130
 *   - supported_providers: A string of supported providers.
131
 *
132
 * @ingroup themeable
133
 */
134
function theme_media_internet_embed_help($variables) {
135
  $description = $variables['description'];
136
  $supported_providers = $variables['supported_providers'];
137

    
138
  $descriptions = array();
139

    
140
  if (strlen($description)) {
141
    $descriptions[] = $description;
142
  }
143
  if (!empty($supported_providers)) {
144
    $descriptions[] = t('Supported internet media providers: !providers.', array('!providers' => '<strong>' . $supported_providers . '</strong>'));
145
  }
146

    
147
  return implode('<br />', $descriptions);
148
}
149

    
150
/**
151
 * Implements hook_forms().
152
 */
153
function media_internet_forms($form_id, $args) {
154
  $forms = array();
155

    
156
  // Create a copy of the upload wizard form for internet media.
157
  if ($form_id == 'media_internet_add_upload') {
158
    $forms[$form_id] = array(
159
      'callback' => 'file_entity_add_upload',
160
    );
161
  }
162

    
163
  return $forms;
164
}
165

    
166
/**
167
 * Implements hook_form_FORM_ID_alter().
168
 */
169
function media_internet_form_file_entity_add_upload_alter(&$form, &$form_state, $form_id) {
170
  $step = $form['#step'];
171
  $options = $form['#options'];
172

    
173
  // Swap the upload field for an embed field when on the first step of the web
174
  // tab.
175
  if ($form_id == 'media_internet_add_upload' && $step == 1) {
176
    unset($form['upload']);
177

    
178
    $form['embed_code'] = array(
179
      '#type' => 'textfield',
180
      '#title' => t('File URL'),
181
      '#description' => t('Enter a URL to a file.'),
182
      '#attributes' => array('class' => array('media-add-from-url')),
183
      // There is no standard specifying a maximum length for a URL. Internet
184
      // Explorer supports up to 2083 (http://support.microsoft.com/kb/208427)
185
      // so we assume publicly available media URLs are within this limit.
186
      '#maxlength' => 2083,
187
      '#required' => TRUE,
188
      '#default_value' => isset($form_state['storage']['embed_code']) ? $form_state['storage']['embed_code'] : NULL,
189
    );
190

    
191
    // Create an array to hold potential Internet media providers.
192
    $providers = array();
193

    
194
    // Determine if there are any visible providers.
195
    foreach (media_internet_get_providers() as $key => $provider) {
196
      if (empty($provider['hidden']) || $provider['hidden'] != TRUE) {
197
        $providers[] = check_plain($provider['title']);
198
      }
199
    }
200

    
201
    $form['#providers'] = $providers;
202

    
203
    // Notify the user of any available providers.
204
    if ($providers) {
205
      // If any providers are enabled it is assumed that some kind of embed is supported.
206
      $form['embed_code']['#title'] = t('File URL or media resource');
207
      $form['embed_code']['#description'] = t('Enter a URL to a file or media resource. Many media providers also support identifying media via the embed code used to embed the media into external websites.');
208

    
209
      $form['embed_code']['#description'] = theme('media_internet_embed_help', array('description' => $form['embed_code']['#description'], 'supported_providers' => implode(', ', $providers)));
210
    }
211

    
212
    $form['#validators'] = array();
213

    
214
    if (!empty($options['types'])) {
215
      $form['#validators']['media_file_validate_types'] = array($options['types']);
216
    }
217

    
218
    // Add validation and submission handlers to the form and ensure that they
219
    // run first.
220
    array_unshift($form['#validate'], 'media_internet_add_validate');
221
    array_unshift($form['#submit'], 'media_internet_add_submit');
222
  }
223
}
224

    
225
/**
226
 * Allow stream wrappers to have their chance at validation.
227
 *
228
 * Any module that implements hook_media_parse will have an
229
 * opportunity to validate this.
230
 *
231
 * @see media_parse_to_uri()
232
 */
233
function media_internet_add_validate($form, &$form_state) {
234
  // Supporting providers can now claim this input. It might be a URL, but it
235
  // might be an embed code as well.
236
  $embed_code = $form_state['values']['embed_code'];
237

    
238
  try {
239
    $provider = media_internet_get_provider($embed_code);
240
    $provider->validate();
241
  }
242
  catch (MediaInternetNoHandlerException $e) {
243
    form_set_error('embed_code', $e->getMessage());
244
    return;
245
  }
246
  catch (MediaInternetValidationException $e) {
247
    form_set_error('embed_code', $e->getMessage());
248
    return;
249
  }
250

    
251
  $validators = $form['#validators'];
252
  $file = $provider->getFileObject();
253

    
254
  if ($validators) {
255
    try {
256
      $file = $provider->getFileObject();
257
    }
258
    catch (Exception $e) {
259
      form_set_error('embed_code', $e->getMessage());
260
      return;
261
    }
262

    
263
    // Check for errors. @see media_add_upload_validate calls file_save_upload().
264
    // this code is ripped from file_save_upload because we just want the validation part.
265
    // Call the validation functions specified by this function's caller.
266
    $errors = file_validate($file, $validators);
267

    
268
    if (!empty($errors)) {
269
      $message = t('%url could not be added.', array('%url' => $embed_code));
270
      if (count($errors) > 1) {
271
        $message .= theme('item_list', array('items' => $errors));
272
      }
273
      else {
274
        $message .= ' ' . array_pop($errors);
275
      }
276
      form_set_error('embed_code', $message);
277
      return FALSE;
278
    }
279
  }
280

    
281
  // @TODO: Validate that if we have no $uri that this is a valid file to
282
  // save. For instance, we may only be interested in images, and it would
283
  // be helpful to let the user know they passed the HTML page containing
284
  // the image accidentally. That would also save us from saving the file
285
  // in the submit step.
286

    
287
  // This is kinda a hack of the same.
288

    
289
  // This should use the file_validate routines that the upload form users.
290
  // We need to fix the media_parse_to_file routine to allow for a validation.
291
}
292

    
293
/**
294
 * Upload a file from a URL.
295
 *
296
 * This will copy a file from a remote location and store it locally.
297
 *
298
 * @see media_parse_to_uri()
299
 * @see media_parse_to_file()
300
 */
301
function media_internet_add_submit($form, &$form_state) {
302
  $embed_code = $form_state['values']['embed_code'];
303

    
304
  try {
305
    // Save the remote file
306
    $provider = media_internet_get_provider($embed_code);
307
    // Providers decide if they need to save locally or somewhere else.
308
    // This method returns a file object
309
    $file = $provider->save();
310
  }
311
  catch (Exception $e) {
312
    form_set_error('embed_code', $e->getMessage());
313
    return;
314
  }
315

    
316
  if (!$file->fid) {
317
    form_set_error('embed_code', t('The file %file could not be saved. An unknown error has occurred.', array('%file' => $embed_code)));
318
    return;
319
  }
320
  else {
321
    $form_state['storage']['upload'] = $file->fid;
322
  }
323
}