media_internet.module

Tracking 7.x-2.x branch
  1. drupal
    1. 7 contributions/media/modules/media_internet/media_internet.module

Classes

NameDescription
MediaInternetBaseHandler
MediaInternetFileHandler
MediaInternetNoHandlerException
MediaInternetValidationException

Functions & methods

NameDescription
media_internet_accessAccess callback for the media_internet media browser plugin.
media_internet_addProvides a form for adding media items from 3rd party sources.
media_internet_add_submitUpload a file from a URL.
media_internet_add_validateAllow stream wrappers to have their chance at validation.
media_internet_get_providerFinds the appropriate provider for a given URL or embed_string
media_internet_get_providersGets the list of providers.
media_internet_hook_infoImplements hook_hook_info().
media_internet_menuImplements hook_menu().
media_internet_permissionImplement hook_permission().

File

View source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<?php

/**
 * Implements hook_hook_info().
 */
function media_internet_hook_info() {
  $hooks = array(
    'media_internet_providers',
  );

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

/**
 * Implements hook_menu().
 */
function media_internet_menu() {
  $items['file/add/web'] = array(
    'title' => 'Web',
    'description' => 'Add internet files to your media library.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('media_internet_add'),
    'access callback' => 'media_internet_access',
    'type' => MENU_LOCAL_TASK,
  );

  return $items;
}

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

/**
 * Implement hook_permission().
 */
function media_internet_permission() {
  return array(
    'add media from remote sources' => array(
      'title' => t('Add media from remote services'),
      'description' => t('Add media from remote sources such as other websites, YouTube, etc'),
    ),
  );
}

/**
 * Provides a form for adding media items from 3rd party sources.
 *
 * @todo Convert the form arguments to just one array of options/parameters.
 */
function media_internet_add($form, &$form_state = array(), $types = NULL) {
  $form['embed_code'] = array(
    '#type' => 'textfield',
    '#title' => t('URL or Embed code'),
    '#description' => t('Input a url or embed code from one of the listed providers.'),
    '#attributes' => array('class' => array('media-add-from-url')),
    // There is no standard specifying a maximum length for a URL. Internet
    // Explorer supports upto 2083 (http://support.microsoft.com/kb/208427), so
    // we assume publicly available media URLs are within this limit.
    '#maxlength' => 2083,
    '#required' => TRUE,
  );


  // @todo:
  // Add live previews back (currently broken)

  //$form['preview'] = array(
  //  '#type' => 'item',
  //  '#title' => t('Preview'),
  //  '#markup' => '<div id="media-add-from-url-preview"></div>'
  //);

  $form['#validators'] = array();
  if ($types) {
    $form['#validators']['media_file_validate_types'] = array($types);
  }

  $providers = array();
  foreach (media_internet_get_providers() as $key => $provider) {
    if (empty($provider['hidden']) || $provider['hidden'] != TRUE) {
      // @todo Convert this to show provider images in a nice format.
      $class = drupal_clean_css_identifier(drupal_strtolower($provider['title']));
      $providers[] = array(
        'data' => check_plain($provider['title']),
        'class' => array($class),
      );
    }
  }
  $form['providers'] = array(
    '#theme' => 'item_list',
    '#title' => t('Supported providers'),
    '#items' => $providers,
    '#attributes' => array(
      'class' => array('media-internet-providers'),
    ),
  );

  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
  return $form;
}

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

  try {
    $provider = media_internet_get_provider($embed_code);
    $provider->validate();
  } catch (MediaInternetNoHandlerException $e) {
    form_set_error('url', $e->getMessage());
    return;
  } catch (MediaInternetValidationException $e) {
    form_set_error('url', $e->getMessage());
    return;
  }

  $validators = $form['#validators'];
  $file = $provider->getFileObject();
  if ($validators) {
    try {
      $file = $provider->getFileObject();
    } catch (Exception $e) {
      form_set_error('url', $e->getMessage());
      return;
    }

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

    if (!empty($errors)) {
      $message = t('%url could not be added.', array('%url' => $embed_code));
      if (count($errors) > 1) {
        $message .= theme('item_list', array('items' => $errors));
      }
      else {
        $message .= ' ' . array_pop($errors);
      }
      form_set_error('url', $message);
      return FALSE;
    }
  }
  // @TODO: Validate that if we have no $uri that this is a valid file to
  // save. For instance, we may only be interested in images, and it would
  // be helpful to let the user know they passed the HTML page containing
  // the image accidentally. That would also save us from saving the file
  // in the submit step.

  // This is kinda a hack of the same.

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

/**
 * Upload a file from a URL.
 *
 * This will copy a file from a remote location and store it locally.
 *
 * @see media_parse_to_uri()
 * @see media_parse_to_file()
 */
function media_internet_add_submit($form, &$form_state) {
  $embed_code = $form_state['values']['embed_code'];
  try {
    // Save the remote file
    $provider = media_internet_get_provider($embed_code);
    // Providers decide if they need to save locally or somewhere else.
    // This method returns a file object
    $file = $provider->save();
  }
  catch (Exception $e) {
    form_set_error('url', $e->getMessage());
    return;
  }

  if (!$file->fid) {
    form_set_error('url', t('The file %file could not be saved. An unknown error has occurred.', array('%file' => $embed_code)));
    return;
  }
  else {
    $form_state['file'] = $file;
  }

  // Redirect to the file edit page after submission.
  if (media_access('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';
  }
}

/**
 * Gets the list of providers.
 *
 * A "Provider" is a bit of meta-data like a title and a logo and a class which
 * can handle saving remote files.  Each provider is able to parse an embed code or URL
 * and store it as a file object in file_managed.
 */
function media_internet_get_providers() {
  $providers = &drupal_static(__FUNCTION__);

  if (!isset($providers)) {
    $cid = 'media:internet:providers';
    if ($cache = cache_get($cid)) {
      $providers = $cache->data;
    }
    else {
      $providers = array();
      foreach (module_implements('media_internet_providers') as $module) {
        foreach (module_invoke($module, 'media_internet_providers') as $key => $provider) {
          // Store the module here too for convinience.
          $providers[$key] = $provider;
          $providers[$key]['module'] = $module;
          if (!isset($providers[$key]['weight'])) {
            $providers[$key]['weight'] = 0;
          }
        }
      }
      uasort($providers, 'drupal_sort_weight');
      cache_set($cid, $providers);
    }
  }

  return $providers;
}

/**
 * Finds the appropriate provider for a given URL or embed_string
 *
 * Each provider has a claim() method which it uses to tell media_internet
 * that it should handle this input.  We cycle through all providers to find
 * the right one.
 *
 * @todo: Make this into a normal hook or something because we have to instantiate
 * each class to test and that's not right.
 */
function media_internet_get_provider($embed_string) {
  foreach (media_internet_get_providers() as $class_name => $nothing) {
    $p = new $class_name($embed_string);
    if ($p->claim($embed_string)) {
      return $p;
    }
  }
  throw new MediaInternetNoHandlerException(t('Unable to handle the provided embed string or URL.'));
}

class MediaInternetFileHandler extends MediaInternetBaseHandler {

  public $fileObject;

  public function preSave(&$file_obj) {
    // Coppies the remote file locally.
    $remote_uri = $file_obj->uri;
    //@TODO: we should follow redirection here an save the final filename, not just the basename.
    $local_filename = basename($remote_uri);
    $local_filename = file_munge_filename($local_filename, media_variable_get('file_extensions'), FALSE);
    $local_uri = file_stream_wrapper_uri_normalize('temporary://' . $local_filename);
    if (!@copy($remote_uri, $local_uri)) {
      throw new Exception('Unable to add file ' . $remote_uri);
      return;
    }
    // Make the current fileObject point to the local_uri, not the remote one.
    $file_obj = file_uri_to_object($local_uri);
  }

  public function postSave(&$file_obj) {
    $scheme = variable_get('file_default_scheme', 'public') . '://';
    $uri = file_stream_wrapper_uri_normalize($scheme . $file_obj->filename);
    // Now to its new home.
    $file_obj = file_move($file_obj, $uri, FILE_EXISTS_RENAME);
  }

  public function getFileObject() {
    if (!$this->fileObject) {
      $this->fileObject = file_uri_to_object($this->embedCode);
    }
    return $this->fileObject;
  }

  public function claim($embedCode) {
    // Claim only valid URLs using a supported scheme.
    if (!valid_url($embedCode, TRUE) || !in_array(file_uri_scheme($embedCode), media_variable_get('fromurl_supported_schemes'))) {
      return FALSE;
    }

    // This handler is intended for regular files, so don't claim URLs
    // containing query strings or fragments.
    if (preg_match('/[\?\#]/', $embedCode)) {
      return FALSE;
    }

    // Since this handler copies the remote file to the local web server, do not
    // claim a URL with an extension disallowed for media uploads.
    $regex = '/\.(' . preg_replace('/ +/', '|', preg_quote(media_variable_get('file_extensions'))) . ')$/i';
    if (!preg_match($regex, basename($embedCode))) {
      return FALSE;
    }

    return TRUE;
  }
}

abstract class MediaInternetBaseHandler {

  public function __construct($embedCode) {
    $this->embedCode = $embedCode;
  }

  /**
   * If required, implementors can validate the embedCode.
   */
  public function validate() {}

  /**
   * Returns a file object which can be used for validation
   *
   * @return StdClass
   */
  abstract public function getFileObject();

  /**
   * Saves a file to the file_managed table (with file_save)
   *
   * @return StdClass
   */
  public function save() {
    $file_obj = $this->getFileObject();
    $this->preSave($file_obj);
    file_save($file_obj);
    $this->postSave($file_obj);
    return $file_obj;
  }

  /**
   * After the file has been saved, implementors may do additional operations.
   *
   * @param $file_obj;
   */
  public function postSave(&$file_obj) {

  }

  /**
   * Before the file has been saved, implementors may do additional operations.
   */
  public function preSave(&$file_obj) {

  }

  /**
   * Recognize if this handler should take the the item with the embed
   * coded passed as argument.
   *
   * @param string $embed_code
   *
   * @return boolean
   *   Whether or not this handler is resposible for the give embed code.
   */
  abstract public function claim($embed_code);
}

class MediaInternetValidationException extends Exception {

}

class MediaInternetNoHandlerException extends Exception {

}