Projet

Général

Profil

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

root / drupal7 / sites / all / modules / media / modules / media_wysiwyg / includes / media_wysiwyg.filter.inc @ ca0757b9

1
<?php
2

    
3
/**
4
 * @file
5
 * Functions related to the WYSIWYG editor and the media input filter.
6
 */
7

    
8
define('MEDIA_WYSIWYG_TOKEN_REGEX', '/\[\[.*?\]\]/s');
9

    
10
/**
11
 * Filter callback for media markup filter.
12
 *
13
 * @TODO check for security probably pass text through filter_xss
14
 */
15
function media_wysiwyg_filter($text) {
16
  $text = preg_replace_callback(MEDIA_WYSIWYG_TOKEN_REGEX, 'media_wysiwyg_token_to_markup', $text);
17
  return $text;
18
}
19

    
20
/**
21
 * Parses the contents of a CSS declaration block.
22
 *
23
 * @param string $declarations
24
 *   One or more CSS declarations delimited by a semicolon. The same as a CSS
25
 *   declaration block (see http://www.w3.org/TR/CSS21/syndata.html#rule-sets),
26
 *   but without the opening and closing curly braces. Also the same as the
27
 *   value of an inline HTML style attribute.
28
 *
29
 * @return array
30
 *   A keyed array. The keys are CSS property names, and the values are CSS
31
 *   property values.
32
 */
33
function media_wysiwyg_parse_css_declarations($declarations) {
34
  $properties = array();
35
  foreach (array_map('trim', explode(";", $declarations)) as $declaration) {
36
    if ($declaration != '') {
37
      list($name, $value) = array_map('trim', explode(':', $declaration, 2));
38
      $properties[strtolower($name)] = $value;
39
    }
40
  }
41
  return $properties;
42
}
43

    
44
/**
45
 * Replace callback to convert a media file tag into HTML markup.
46
 *
47
 * @param string $match
48
 *   Takes a match of tag code
49
 * @param bool $wysiwyg
50
 *   Set to TRUE if called from within the WYSIWYG text area editor.
51
 *
52
 * @return string
53
 *   The HTML markup representation of the tag, or an empty string on failure.
54
 *
55
 * @see media_wysiwyg_get_file_without_label()
56
 * @see hook_media_wysiwyg_token_to_markup_alter()
57
 */
58
function media_wysiwyg_token_to_markup($match, $wysiwyg = FALSE) {
59
  $settings = array();
60
  $match = str_replace("[[", "", $match);
61
  $match = str_replace("]]", "", $match);
62
  $tag = $match[0];
63

    
64
  try {
65
    if (!is_string($tag)) {
66
      throw new Exception('Unable to find matching tag');
67
    }
68

    
69
    $tag_info = drupal_json_decode($tag);
70

    
71
    if (!isset($tag_info['fid'])) {
72
      throw new Exception('No file Id');
73
    }
74

    
75
    // Ensure the 'link_text' key is always defined.
76
    if (!isset($tag_info['link_text'])) {
77
      $tag_info['link_text'] = NULL;
78
    }
79

    
80
    // Ensure a valid view mode is being requested.
81
    if (!isset($tag_info['view_mode'])) {
82
      $tag_info['view_mode'] = variable_get('media_wysiwyg_wysiwyg_default_view_mode', 'full');
83
    }
84
    elseif ($tag_info['view_mode'] != 'default') {
85
      $file_entity_info = entity_get_info('file');
86
      if (!in_array($tag_info['view_mode'], array_keys($file_entity_info['view modes']))) {
87
        // Media 1.x defined some old view modes that have been superseded by
88
        // more semantically named ones in File Entity. The media_update_7203()
89
        // function updates field settings that reference the old view modes,
90
        // but it's impractical to update all text content, so adjust
91
        // accordingly here.
92
        static $view_mode_updates = array(
93
          'media_preview' => 'preview',
94
          'media_small' => 'teaser',
95
          'media_large' => 'full',
96
        );
97
        if (isset($view_mode_updates[$tag_info['view_mode']])) {
98
          $tag_info['view_mode'] = $view_mode_updates[$tag_info['view_mode']];
99
        }
100
        else {
101
          throw new Exception('Invalid view mode');
102
        }
103
      }
104
    }
105

    
106
    $file = file_load($tag_info['fid']);
107
    if (!$file) {
108
      throw new Exception('Could not load media object');
109
    }
110
    $tag_info['file'] = $file;
111

    
112
    // The class attributes is a string, but drupal requires it to be
113
    // an array, so we fix it here.
114
    if (!empty($tag_info['attributes']['class'])) {
115
      $tag_info['attributes']['class'] = explode(" ", $tag_info['attributes']['class']);
116
    }
117

    
118
    // Grab the potentially overrided fields from the file.
119
    $fields = media_wysiwyg_filter_field_parser($tag_info);
120

    
121
    $attributes = is_array($tag_info['attributes']) ? $tag_info['attributes'] : array();
122
    $attribute_whitelist = variable_get('media_wysiwyg_wysiwyg_allowed_attributes', _media_wysiwyg_wysiwyg_allowed_attributes_default());
123
    $settings['attributes'] = array_intersect_key($attributes, array_flip($attribute_whitelist));
124
    $settings['fields'] = $fields;
125

    
126
    if (!empty($tag_info['attributes']) && is_array($tag_info['attributes'])) {
127
      $settings['attributes'] = array_intersect_key($tag_info['attributes'], array_flip($attribute_whitelist));
128
      $settings['fields'] = $fields;
129

    
130
      // Many media formatters will want to apply width and height independently
131
      // of the style attribute or the corresponding HTML attributes, so pull
132
      // these two out into top-level settings. Different WYSIWYG editors have
133
      // different behavior with respect to whether they store user-specified
134
      // dimensions in the HTML attributes or the style attribute - check both.
135
      // Per http://www.w3.org/TR/html5/the-map-element.html#attr-dim-width, the
136
      // HTML attributes are merely hints: CSS takes precedence.
137
      if (isset($settings['attributes']['style'])) {
138
        $css_properties = media_wysiwyg_parse_css_declarations($settings['attributes']['style']);
139
        foreach (array('width', 'height') as $dimension) {
140
          if (isset($css_properties[$dimension]) && substr($css_properties[$dimension], -2) == 'px') {
141
            $settings[$dimension] = substr($css_properties[$dimension], 0, -2);
142
          }
143
          elseif (isset($settings['attributes'][$dimension])) {
144
            $settings[$dimension] = $settings['attributes'][$dimension];
145
          }
146
        }
147
      }
148
    }
149
  }
150
  catch (Exception $e) {
151
    watchdog('media', 'Unable to render media from %tag. Error: %error', array('%tag' => $tag, '%error' => $e->getMessage()));
152
    return '';
153
  }
154

    
155
  // If the tag has link text stored with it, override the filename with it for
156
  // the rest of this function, so that if the file is themed as a link, the
157
  // desired text will be used (see, for example, theme_file_link()).
158
  // @todo: Try to find a less hacky way to do this.
159
  if (isset($tag_info['link_text'])) {
160
    // The link text will have characters such as "&" encoded for HTML, but the
161
    // filename itself needs the raw value when it is used to build the link,
162
    // in order to avoid double encoding.
163
    $file->filename = decode_entities($tag_info['link_text']);
164
  }
165

    
166
  if ($wysiwyg) {
167
    $settings['wysiwyg'] = $wysiwyg;
168
    // If sending markup to a WYSIWYG, we need to pass the file infomation so
169
    // that an inline macro can be generated when the WYSIWYG is detached.
170
    // The WYSIWYG plugin is expecting this information in the
171
    // Drupal.settings.mediaDataMap variable.
172
    $element = media_wysiwyg_get_file_without_label($file, $tag_info['view_mode'], $settings);
173
    $data = array(
174
      'type' => 'media',
175
      'fid'  => $file->fid,
176
      'view_mode' => $tag_info['view_mode'],
177
      'link_text' => $tag_info['link_text'],
178
    );
179
    drupal_add_js(array('mediaDataMap' => array($file->fid => $data)), 'setting');
180
    $element['#attributes']['data-fid'] = $file->fid;
181
    $element['#attributes']['class'][] = 'media-element';
182
  }
183
  else {
184
    // Display the field elements.
185
    $element = array();
186
    $element['content']['file'] = media_wysiwyg_get_file_without_label($file, $tag_info['view_mode'], $settings);
187
    // Overwrite or set the file #alt attribute if it has been set in this
188
    // instance.
189
    if (!empty($element['content']['file']['#attributes']['alt'])) {
190
      $element['content']['file']['#alt'] = $element['content']['file']['#attributes']['alt'];
191
    }
192
    // Overwrite or set the file #title attribute if it has been set in this
193
    // instance.
194
    if (!empty($element['content']['file']['#attributes']['title'])) {
195
      $element['content']['file']['#title'] = $element['content']['file']['#attributes']['title'];
196
    }
197
    field_attach_prepare_view('file', array($file->fid => $file), $tag_info['view_mode']);
198
    entity_prepare_view('file', array($file->fid => $file));
199
    $element['content'] += field_attach_view('file', $file, $tag_info['view_mode']);
200
    if (count(element_children($element['content'])) > 1) {
201
      // Add surrounding divs to group them together.
202
      // We dont want divs when there are no additional fields to allow files
203
      // to display inline with text, without breaking p tags.
204
      $element['content']['#type'] = 'container';
205
      $element['content']['#attributes']['class'] = array(
206
        'media',
207
        'media-element-container',
208
        'media-' . $element['content']['file']['#view_mode']
209
      );
210
    }
211
  }
212
  drupal_alter('media_wysiwyg_token_to_markup', $element, $tag_info, $settings);
213
  return drupal_render($element);
214
}
215

    
216
/**
217
 * Parse the field array from the collapsed AJAX string.
218
 */
219
function media_wysiwyg_filter_field_parser($tag_info) {
220
  $fields = array();
221
  if (isset($tag_info['fields'])) {
222
    foreach($tag_info['fields'] as $field_name => $field_value) {
223
      if (strpos($field_name, 'field_') === 0) {
224
        $parsed_field = explode('[', str_replace(']', '', $field_name));
225
        $ref = &$fields;
226

    
227
        // Each key of the field needs to be the child of the previous key.
228
        foreach ($parsed_field as $key) {
229
          if (!isset($ref[$key])) {
230
            $ref[$key] = array();
231
          }
232
          $ref = &$ref[$key];
233
        }
234

    
235
        // The value should be set at the deepest level.
236
        // Fields that use rich-text markup will be urlencoded.
237
        $ref = urldecode($field_value);
238
      }
239
    }
240
  }
241
  return $fields;
242
}
243

    
244
/**
245
 * Builds a map of media tags in the element.
246
 *
247
 * Builds a map of the media tags in an element that are being rendered to their
248
 * rendered HTML. The map is stored in JS, so we can transform them when the
249
 * editor is being displayed.
250
 */
251
function media_wysiwyg_pre_render_text_format($element) {
252
  // filter_process_format() copies properties to the expanded 'value' child
253
  // element.
254
  if (!isset($element['format'])) {
255
    return $element;
256
  }
257

    
258
  $field = &$element['value'];
259
  $settings = array(
260
    'field' => $field['#id'],
261
  );
262

    
263
  $tagmap = _media_wysiwyg_generate_tagMap($field['#value']);
264

    
265
  if (isset($tagmap)) {
266
    drupal_add_js(array('tagmap' => $tagmap), 'setting');
267
  }
268
  return $element;
269
}
270

    
271
/**
272
 * Creates map of inline media tags.
273
 *
274
 * Generates an array of [inline tags] => <html> to be used in filter
275
 * replacement and to add the mapping to JS.
276
 *
277
 * @param string $text
278
 *   The String containing text and html markup of textarea
279
 *
280
 * @return array
281
 *   An associative array with tag code as key and html markup as the value.
282
 *
283
 * @see media_process_form()
284
 * @see media_token_to_markup()
285
 */
286
function _media_wysiwyg_generate_tagMap($text) {
287
  // Making $tagmap static as this function is called many times and
288
  // adds duplicate markup for each tag code in Drupal.settings JS,
289
  // so in media_process_form it adds something like tagCode:<markup>,
290
  // <markup> and when we replace in attach see two duplicate images
291
  // for one tagCode. Making static would make function remember value
292
  // between function calls. Since media_process_form is multiple times
293
  // with same form, this function is also called multiple times.
294
  static $tagmap = array();
295
  preg_match_all("/\[\[.*?\]\]/s", $text, $matches, PREG_SET_ORDER);
296
  foreach ($matches as $match) {
297
    // We see if tagContent is already in $tagMap, if not we add it
298
    // to $tagmap.  If we return an empty array, we break embeddings of the same
299
    // media multiple times.
300
    if (empty($tagmap[$match[0]])) {
301
      // @TODO: Total HACK, but better than nothing.
302
      // We should find a better way of cleaning this up.
303
      if ($markup_for_media = media_wysiwyg_token_to_markup($match, TRUE)) {
304
        $tagmap[$match[0]] = $markup_for_media;
305
      }
306
      else {
307
        $missing = file_create_url(drupal_get_path('module', 'media') . '/images/icons/default/image-x-generic.png');
308
        $tagmap[$match[0]] = '<div><img src="' . $missing . '" width="100px" height="100px"/></div>';
309
      }
310
    }
311
  }
312
  return $tagmap;
313
}
314

    
315
/**
316
 * Return a list of view modes allowed for a file embedded in the WYSIWYG.
317
 *
318
 * @param object $file
319
 *   A file entity.
320
 *
321
 * @return array
322
 *   An array of view modes that can be used on the file when embedded in the
323
 *   WYSIWYG.
324
 */
325
function media_wysiwyg_get_wysiwyg_allowed_view_modes($file) {
326
  $enabled_view_modes = &drupal_static(__FUNCTION__, array());
327

    
328
  // @todo Add more caching for this.
329
  if (!isset($enabled_view_modes[$file->type])) {
330
    $enabled_view_modes[$file->type] = array();
331

    
332
    // Add the default view mode by default.
333
    $enabled_view_modes[$file->type]['default'] = array('label' => t('Default'), 'custom settings' => TRUE);
334

    
335
    $entity_info = entity_get_info('file');
336
    $view_mode_settings = field_view_mode_settings('file', $file->type);
337
    foreach ($entity_info['view modes'] as $view_mode => $view_mode_info) {
338
      // Do not show view modes that don't have their own settings and will
339
      // only fall back to the default view mode.
340
      if (empty($view_mode_settings[$view_mode]['custom_settings'])) {
341
        continue;
342
      }
343

    
344
      // Don't present the user with an option to choose a view mode in which
345
      // the file is hidden.
346
      $extra_fields = field_extra_fields_get_display('file', $file->type, $view_mode);
347
      if (empty($extra_fields['file']['visible'])) {
348
        continue;
349
      }
350

    
351
      // Add the view mode to the list of enabled view modes.
352
      $enabled_view_modes[$file->type][$view_mode] = $view_mode_info;
353
    }
354
  }
355

    
356
  $view_modes = $enabled_view_modes[$file->type];
357
  drupal_alter('media_wysiwyg_wysiwyg_allowed_view_modes', $view_modes, $file);
358
  return $view_modes;
359
}