Projet

Général

Profil

Paste
Télécharger (2,62 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / media / modules / media_internet / includes / MediaInternetFileHandler.inc @ ca0757b9

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of MediaInternetFileHandler.
6
 */
7

    
8
/**
9
 * A class for managing the addition of Internet files.
10
 */
11
class MediaInternetFileHandler extends MediaInternetBaseHandler {
12

    
13
  public $fileObject;
14

    
15
  public function preSave(&$file_obj) {
16
    // Coppies the remote file locally.
17
    $remote_uri = $file_obj->uri;
18
    //@TODO: we should follow redirection here an save the final filename, not just the basename.
19
    $local_filename = basename($remote_uri);
20
    $local_filename = file_munge_filename($local_filename, variable_get('file_entity_default_allowed_extensions', 'jpg jpeg gif png txt doc docx xls xlsx pdf ppt pptx pps ppsx odt ods odp mp3 mov mp4 m4a m4v mpeg avi ogg oga ogv weba webp webm'), FALSE);
21
    $local_uri = file_stream_wrapper_uri_normalize('temporary://' . $local_filename);
22
    if (!@copy($remote_uri, $local_uri)) {
23
      throw new Exception('Unable to add file ' . $remote_uri);
24
      return;
25
    }
26
    // Make the current fileObject point to the local_uri, not the remote one.
27
    $file_obj = file_uri_to_object($local_uri);
28
  }
29

    
30
  public function postSave(&$file_obj) {
31
    $scheme = variable_get('file_default_scheme', 'public') . '://';
32
    module_load_include('inc', 'file_entity', 'file_entity.pages');
33
    $destination_uri = file_entity_upload_destination_uri(array());
34
    $uri = file_stream_wrapper_uri_normalize($destination_uri . '/' . $file_obj->filename);
35
    // Now to its new home.
36
    $file_obj = file_move($file_obj, $uri, FILE_EXISTS_RENAME);
37
  }
38

    
39
  public function getFileObject() {
40
    if (!$this->fileObject) {
41
      $this->fileObject = file_uri_to_object($this->embedCode);
42
    }
43
    return $this->fileObject;
44
  }
45

    
46
  public function claim($embedCode) {
47
    // Claim only valid URLs using a supported scheme.
48
    if (!valid_url($embedCode, TRUE) || !in_array(file_uri_scheme($embedCode), variable_get('media_fromurl_supported_schemes', array('http', 'https', 'ftp', 'smb', 'ftps')))) {
49
      return FALSE;
50
    }
51

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

    
58
    // Since this handler copies the remote file to the local web server, do not
59
    // claim a URL with an extension disallowed for media uploads.
60
    $regex = '/\.(' . preg_replace('/ +/', '|', preg_quote(variable_get('file_entity_default_allowed_extensions', 'jpg jpeg gif png txt doc docx xls xlsx pdf ppt pptx pps ppsx odt ods odp mp3 mov mp4 m4a m4v mpeg avi ogg oga ogv weba webp webm'))) . ')$/i';
61
    if (!preg_match($regex, basename($embedCode))) {
62
      return FALSE;
63
    }
64

    
65
    return TRUE;
66
  }
67
}