Projet

Général

Profil

Paste
Télécharger (4,82 ko) Statistiques
| Branche: | Révision:

root / htmltest / sites / all / modules / media / media.media.inc @ a5572547

1
<?php
2

    
3
/**
4
 * @file
5
 * Media module integration for the Media module.
6
 */
7

    
8
/**
9
 * Implements hook_media_browser_plugin_info().
10
 */
11
function media_media_browser_plugin_info() {
12
  $info['upload'] = array(
13
    'title' => t('Upload'),
14
    'weight' => -10,
15
    'class' => 'MediaBrowserUpload',
16
  );
17

    
18
  // Add a plugin for each View display using the 'media_browser' display type.
19
  $view_weight = 10;
20
  foreach (views_get_enabled_views() as $view) {
21
    foreach ($view->display as $display) {
22
      if ($display->display_plugin == 'media_browser') {
23
        $title = $display->display_title;
24
        if (!empty($display->display_options['title'])) {
25
          $title = $display->display_options['title'];
26
        }
27
        $info["{$view->name}--{$display->id}"] = array(
28
          'title' => $title,
29
          // @TODO make this configurable.
30
          'weight' => $view_weight++,
31
          'class' => 'MediaBrowserView',
32
          'view_name' => $view->name,
33
          'view_display_id' => $display->id,
34
        );
35
      }
36
    }
37
  }
38

    
39
  return $info;
40
}
41

    
42
/**
43
 * Implements hook_query_media_browser_alter().
44
 */
45
function media_query_media_browser_alter($query) {
46
  // Ensure that the query is against the file_managed table.
47
  $tables = $query->getTables();
48
  if (empty($tables['file_managed'])) {
49
    throw new Exception(t('Media browser being queried without the file_managed table.'));
50
  }
51
  $alias = $tables['file_managed']['alias'];
52

    
53
  $params = drupal_get_query_parameters();
54
  // How do we validate these?  I don't know.
55
  // I think PDO should protect them, but I'm not 100% certain.
56
  array_walk_recursive($params, 'media_recursive_check_plain');
57

    
58
  $types = !empty($params['types']) ? $params['types'] : NULL;
59
  $url_include_patterns = !empty($params['url_include_patterns']) ? $params['url_include_patterns'] : NULL;
60
  $url_exclude_patterns = !empty($params['url_exclude_patterns']) ? $params['url_exclude_patterns'] : NULL;
61
  $allowed_schemes = !empty($params['schemes']) ? array_filter($params['schemes']) : array();
62
  $extensions = !empty($params['file_extensions']) ? array_filter(explode(' ', $params['file_extensions'])) : array();
63

    
64
  $or_condition = db_or();
65

    
66
  if (!empty($allowed_schemes)) {
67
    // Include local files with the allowed extensions and types.
68
    $local_wrappers = array_intersect_key(media_get_local_stream_wrappers(), $allowed_schemes);
69
    if (!empty($extensions) && !empty($local_wrappers)) {
70
      // Extension filtering.
71
      $local_condition = db_or();
72
      foreach (array_keys($local_wrappers) as $scheme) {
73
        foreach ($extensions as $extension) {
74
          $local_condition->condition($alias . '.uri', db_like($scheme . '://') . '%' . db_like('.' . $extension), 'LIKE');
75
        }
76
      }
77
      $or_condition->condition($local_condition);
78
    }
79
    if (!empty($types) && !empty($local_wrappers)) {
80
      // Type filtering.
81
      $local_condition = db_or();
82
      foreach (array_keys($local_wrappers) as $scheme) {
83
        $local_condition->condition($alias . '.type', $types, 'IN');
84
      }
85
      $or_condition->condition($local_condition);
86
    }
87

    
88
    // Include remote files with the allowed file types.
89
    // We cant filter extensions here, because remote file filenames usually
90
    // are a url or a parameter of a query.
91
    $remote_wrappers = array_intersect_key(media_get_remote_stream_wrappers(), $allowed_schemes);
92
    if (!empty($types) && !empty($remote_wrappers)) {
93
      $remote_condition = db_and();
94
      $wrapper_condition = db_or();
95
      foreach (array_keys($remote_wrappers) as $scheme) {
96
        $wrapper_condition->condition($alias . '.uri', db_like($scheme . '://') . '%', 'LIKE');
97
      }
98
      $remote_condition->condition($wrapper_condition);
99
      $remote_condition->condition($alias . '.type', $types, 'IN');
100
      $or_condition->condition($remote_condition);
101
    }
102
  }
103
  else {
104
    if (!empty($types)) {
105
      $query->condition($alias . '.type', $types, 'IN');
106
    }
107
    if (!empty($extensions)) {
108
      foreach ($extensions as $extension) {
109
        $or_condition->condition($alias . '.uri', db_like('.' . $extension), 'LIKE');
110
      }
111
    }
112
  }
113

    
114
  if ($or_condition->count()) {
115
    $query->condition($or_condition);
116
  }
117

    
118
  if ($url_include_patterns) {
119
    $query->condition($alias . '.uri', '%' . db_like($url_include_patterns) . '%', 'LIKE');
120
    // Insert stream related restrictions here.
121
  }
122
  if ($url_exclude_patterns) {
123
    $query->condition($alias . '.uri', '%' . db_like($url_exclude_patterns) . '%', 'NOT LIKE');
124
  }
125

    
126
  if (!user_access('administer files')) {
127
    $query->condition($alias . '.uri', db_like('private://') . '%', 'NOT LIKE');
128
  }
129

    
130
  // @todo This is possibly redundant since it's already filtered in the view.
131
  $query->condition($alias . '.status', FILE_STATUS_PERMANENT);
132

    
133
  foreach (array_keys(file_entity_get_hidden_stream_wrappers()) as $name) {
134
    $query->condition($alias . '.uri', db_like($name . '://') . '%', 'NOT LIKE');
135
  }
136
}