Projet

Général

Profil

Paste
Télécharger (17,7 ko) Statistiques
| Branche: | Révision:

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

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, $filter = NULL, $format = NULL, $langcode = NULL, $cache = NULL, $cache_id = NULL) {
16
  $replacements = array();
17
  $patterns = array();
18
  $rendered_text = $text;
19
  $count = 1;
20
  preg_match_all(MEDIA_WYSIWYG_TOKEN_REGEX, $text, $matches);
21
  if (!empty($matches[0])) {
22
    foreach ($matches[0] as $match) {
23
      $replacement = media_wysiwyg_token_to_markup(array($match), FALSE, $langcode);
24
      $rendered_text = str_replace($match, $replacement, $rendered_text, $count);
25
    }
26
  }
27
  return $rendered_text;
28
}
29

    
30
/**
31
 * Filter callback to remove paragraph tags surrounding embedded media.
32
 */
33
function media_wysiwyg_filter_paragraph_fix($text) {
34
  $html_dom = filter_dom_load($text);
35
  foreach ($html_dom->getElementsByTagName('p') as $paragraph) {
36
    if (preg_match(MEDIA_WYSIWYG_TOKEN_REGEX, $paragraph->nodeValue)) {
37
      $sibling = $paragraph->firstChild;
38
      do {
39
        $next = $sibling->nextSibling;
40
        $paragraph->parentNode->insertBefore($sibling, $paragraph);
41
      } while ($sibling = $next);
42
      $paragraph->parentNode->removeChild($paragraph);
43
    }
44
  }
45
  $text = filter_dom_serialize($html_dom);
46
  return $text;
47
}
48

    
49
/**
50
 * Parses the contents of a CSS declaration block.
51
 *
52
 * @param string $declarations
53
 *   One or more CSS declarations delimited by a semicolon. The same as a CSS
54
 *   declaration block (see http://www.w3.org/TR/CSS21/syndata.html#rule-sets),
55
 *   but without the opening and closing curly braces. Also the same as the
56
 *   value of an inline HTML style attribute.
57
 *
58
 * @return array
59
 *   A keyed array. The keys are CSS property names, and the values are CSS
60
 *   property values.
61
 */
62
function media_wysiwyg_parse_css_declarations($declarations) {
63
  $properties = array();
64
  foreach (array_map('trim', explode(";", $declarations)) as $declaration) {
65
    if ($declaration != '') {
66
      list($name, $value) = array_map('trim', explode(':', $declaration, 2));
67
      $properties[strtolower($name)] = $value;
68
    }
69
  }
70
  return $properties;
71
}
72

    
73
/**
74
 * Replace callback to convert a media file tag into HTML markup.
75
 *
76
 * @param string $match
77
 *   Takes a match of tag code
78
 * @param bool $wysiwyg
79
 *   Set to TRUE if called from within the WYSIWYG text area editor.
80
 *
81
 * @return string
82
 *   The HTML markup representation of the tag, or an empty string on failure.
83
 *
84
 * @see media_wysiwyg_get_file_without_label()
85
 * @see hook_media_wysiwyg_token_to_markup_alter()
86
 */
87
function media_wysiwyg_token_to_markup($match, $wysiwyg = FALSE, $langcode = NULL) {
88
  static $recursion_stop;
89
  $settings = array();
90
  $match = str_replace("[[", "", $match);
91
  $match = str_replace("]]", "", $match);
92
  $tag = $match[0];
93

    
94
  try {
95
    if (!is_string($tag)) {
96
      throw new Exception('Unable to find matching tag');
97
    }
98

    
99
    $tag_info = drupal_json_decode($tag);
100

    
101
    if (!isset($tag_info['fid'])) {
102
      throw new Exception('No file Id');
103
    }
104

    
105
    // Ensure the 'link_text' key is always defined.
106
    if (!isset($tag_info['link_text'])) {
107
      $tag_info['link_text'] = NULL;
108
    }
109

    
110
    // Ensure a valid view mode is being requested.
111
    if (!isset($tag_info['view_mode'])) {
112
      $tag_info['view_mode'] = variable_get('media_wysiwyg_wysiwyg_default_view_mode', 'full');
113
    }
114
    elseif ($tag_info['view_mode'] != 'default') {
115
      $file_entity_info = entity_get_info('file');
116
      if (!in_array($tag_info['view_mode'], array_keys($file_entity_info['view modes']))) {
117
        // Media 1.x defined some old view modes that have been superseded by
118
        // more semantically named ones in File Entity. The media_update_7203()
119
        // function updates field settings that reference the old view modes,
120
        // but it's impractical to update all text content, so adjust
121
        // accordingly here.
122
        static $view_mode_updates = array(
123
          'media_preview' => 'preview',
124
          'media_small' => 'teaser',
125
          'media_large' => 'full',
126
        );
127
        if (isset($view_mode_updates[$tag_info['view_mode']])) {
128
          $tag_info['view_mode'] = $view_mode_updates[$tag_info['view_mode']];
129
        }
130
        else {
131
          throw new Exception('Invalid view mode');
132
        }
133
      }
134
    }
135

    
136
    $file = file_load($tag_info['fid']);
137
    if (!$file) {
138
      throw new Exception('Could not load media object');
139
    }
140
    // Check if we've got a recursion. Happens because a file_load() may
141
    // triggers file_entity_is_page() which then again triggers a file load.
142
    if (isset($recursion_stop[$file->fid])) {
143
      return '';
144
    }
145
    $recursion_stop[$file->fid] = TRUE;
146

    
147
    $tag_info['file'] = $file;
148

    
149
    // The class attributes is a string, but drupal requires it to be
150
    // an array, so we fix it here.
151
    if (!empty($tag_info['attributes']['class'])) {
152
      $tag_info['attributes']['class'] = explode(" ", $tag_info['attributes']['class']);
153
    }
154

    
155
    // Grab the potentially overridden fields from the file.
156
    $fields = media_wysiwyg_filter_field_parser($tag_info);
157
    foreach ($fields as $key => $value) {
158
      $file->{$key} = $value;
159
    }
160

    
161
    $attributes = is_array($tag_info['attributes']) ? $tag_info['attributes'] : array();
162
    $attribute_whitelist = variable_get('media_wysiwyg_wysiwyg_allowed_attributes', _media_wysiwyg_wysiwyg_allowed_attributes_default());
163
    $settings['attributes'] = array_intersect_key($attributes, array_flip($attribute_whitelist));
164
    $settings['fields'] = $fields;
165

    
166
    if (!empty($tag_info['attributes']) && is_array($tag_info['attributes'])) {
167
      $settings['attributes'] = array_intersect_key($tag_info['attributes'], array_flip($attribute_whitelist));
168

    
169
      // Many media formatters will want to apply width and height independently
170
      // of the style attribute or the corresponding HTML attributes, so pull
171
      // these two out into top-level settings. Different WYSIWYG editors have
172
      // different behavior with respect to whether they store user-specified
173
      // dimensions in the HTML attributes or the style attribute - check both.
174
      // Per http://www.w3.org/TR/html5/the-map-element.html#attr-dim-width, the
175
      // HTML attributes are merely hints: CSS takes precedence.
176
      if (isset($settings['attributes']['style'])) {
177
        $css_properties = media_wysiwyg_parse_css_declarations($settings['attributes']['style']);
178
        foreach (array('width', 'height') as $dimension) {
179
          if (isset($css_properties[$dimension]) && substr($css_properties[$dimension], -2) == 'px') {
180
            $settings[$dimension] = substr($css_properties[$dimension], 0, -2);
181
          }
182
          elseif (isset($settings['attributes'][$dimension])) {
183
            $settings[$dimension] = $settings['attributes'][$dimension];
184
          }
185
        }
186
      }
187
      foreach (array('title', 'alt') as $field_type) {
188
        if (isset($settings['attributes'][$field_type])) {
189
          $settings['attributes'][$field_type] = decode_entities($settings['attributes'][$field_type]);
190
        }
191
      }
192
    }
193
    // Update file metadata from the potentially overridden tag info.
194
    foreach (array('width', 'height') as $dimension) {
195
      if (isset($settings['attributes'][$dimension])) {
196
        $file->metadata[$dimension] = $settings['attributes'][$dimension];
197
      }
198
    }
199
  }
200
  catch (Exception $e) {
201
    watchdog('media', 'Unable to render media from %tag. Error: %error', array('%tag' => $tag, '%error' => $e->getMessage()));
202
    return '';
203
  }
204

    
205
  // If the tag has link text stored with it, override the filename with it for
206
  // the rest of this function, so that if the file is themed as a link, the
207
  // desired text will be used (see, for example, theme_file_link()).
208
  // @todo: Try to find a less hacky way to do this.
209
  if (isset($tag_info['link_text'])) {
210
    // The link text will have characters such as "&" encoded for HTML, but the
211
    // filename itself needs the raw value when it is used to build the link,
212
    // in order to avoid double encoding.
213
    $file->filename = decode_entities($tag_info['link_text']);
214
  }
215

    
216
  if ($wysiwyg) {
217
    $settings['wysiwyg'] = $wysiwyg;
218

    
219
    // Render file in WYSIWYG using appropriate view mode.
220
    $view_mode = db_query('SELECT view_mode FROM {media_view_mode_wysiwyg} WHERE type = :type', array(
221
      ':type' => $file->type,
222
    ))
223
      ->fetchField();
224
    if (empty($view_mode)) {
225
      $view_mode = $tag_info['view_mode'];
226
    }
227

    
228
    // If sending markup to a WYSIWYG, we need to pass the file information so
229
    // that an inline macro can be generated when the WYSIWYG is detached.
230
    // The WYSIWYG plugin is expecting this information in the
231
    // Drupal.settings.mediaDataMap variable.
232
    $element = media_wysiwyg_get_file_without_label($file, $view_mode, $settings, $langcode);
233
    $data = array(
234
      'type' => 'media',
235
      'fid'  => $file->fid,
236
      'view_mode' => $tag_info['view_mode'],
237
      'link_text' => $tag_info['link_text'],
238
    );
239
    drupal_add_js(array('mediaDataMap' => array($file->fid => $data)), 'setting');
240
    $element['#attributes']['data-fid'] = $file->fid;
241
    $element['#attributes']['data-media-element'] = '1';
242
    $element['#attributes']['class'][] = 'media-element';
243
  }
244
  else {
245
    // Display the field elements.
246
    $element = array();
247
    // Render the file entity, for sites using the file_entity rendering method.
248
    if (variable_get('media_wysiwyg_default_render', 'file_entity') == 'file_entity') {
249
      $element['content'] = file_view($file, $tag_info['view_mode']);
250
    }
251
    $element['content']['file'] = media_wysiwyg_get_file_without_label($file, $tag_info['view_mode'], $settings, $langcode);
252
    // Overwrite or set the file #alt attribute if it has been set in this
253
    // instance.
254
    if (!empty($element['content']['file']['#attributes']['alt'])) {
255
      $element['content']['file']['#alt'] = $element['content']['file']['#attributes']['alt'];
256
    }
257
    // Overwrite or set the file #title attribute if it has been set in this
258
    // instance.
259
    if (!empty($element['content']['file']['#attributes']['title'])) {
260
      $element['content']['file']['#title'] = $element['content']['file']['#attributes']['title'];
261
    }
262
    // For sites using the legacy field_attach rendering method, attach fields.
263
    if (variable_get('media_wysiwyg_default_render', 'file_entity') == 'field_attach') {
264
      field_attach_prepare_view('file', array($file->fid => $file), $tag_info['view_mode'], $langcode);
265
      entity_prepare_view('file', array($file->fid => $file), $langcode);
266
      $element['content'] += field_attach_view('file', $file, $tag_info['view_mode'], $langcode);
267
    }
268
    if (count(element_children($element['content'])) > 1) {
269
      // Add surrounding divs to group them together.
270
      // We don't want divs when there are no additional fields to allow files
271
      // to display inline with text, without breaking p tags.
272
      $element['content']['#type'] = 'container';
273
      $element['content']['#attributes']['class'] = array(
274
        'media',
275
        'media-element-container',
276
        'media-' . $element['content']['file']['#view_mode'],
277
      );
278
    }
279

    
280
    // Conditionally add a pre-render if the media filter output is be cached.
281
    $filters = filter_get_filters();
282
    if (!isset($filters['media_filter']['cache']) || $filters['media_filter']['cache']) {
283
      $element['#pre_render'][] = 'media_wysiwyg_pre_render_cached_filter';
284
    }
285
  }
286
  drupal_alter('media_wysiwyg_token_to_markup', $element, $tag_info, $settings, $langcode);
287
  $output = drupal_render($element);
288
  unset($recursion_stop[$file->fid]);
289
  return $output;
290
}
291

    
292
/**
293
 * Parse the field array from the collapsed AJAX string.
294
 */
295
function media_wysiwyg_filter_field_parser($tag_info) {
296
  $fields = array();
297
  if (isset($tag_info['fields'])) {
298
    foreach($tag_info['fields'] as $field_name => $field_value) {
299
      if (strpos($field_name, 'field_') === 0) {
300
        $parsed_field = explode('[', str_replace(']', '', $field_name));
301
        $ref = &$fields;
302

    
303
        // Certain types of fields, because of differences in markup, end up
304
        // here with incomplete arrays. Make a best effort to support as many
305
        // types of fields as possible.
306
        // Single-value select lists show up here with only 2 array items.
307
        if (count($parsed_field) == 2) {
308
          $info = field_info_field($parsed_field[0]);
309
          if ($info && !empty($info['columns'])) {
310
            // Assume single-value.
311
            $parsed_field[] = 0;
312
            // Next tack on the column for this field.
313
            $parsed_field[] = key($info['columns']);
314
          }
315
        }
316
        // Multi-value select lists show up here with 3 array items.
317
        elseif (count($parsed_field) == 3 && is_numeric($parsed_field[2])) {
318
          $info = field_info_field($parsed_field[0]);
319
          // They just need the value column.
320
          $parsed_field[3] = key($info['columns']);
321
        }
322

    
323
        // Each key of the field needs to be the child of the previous key.
324
        foreach ($parsed_field as $key) {
325
          if (!isset($ref[$key])) {
326
            $ref[$key] = array();
327
          }
328
          $ref = &$ref[$key];
329
        }
330

    
331
        // The value should be set at the deepest level.
332
        // Fields that use rich-text markup will be urlencoded.
333
        $ref = decode_entities($field_value);
334
      }
335
    }
336
  }
337
  return $fields;
338
}
339

    
340
/**
341
 * Creates map of inline media tags.
342
 *
343
 * Generates an array of [inline tags] => <html> to be used in filter
344
 * replacement and to add the mapping to JS.
345
 *
346
 * @param string $text
347
 *   The String containing text and html markup of textarea
348
 *
349
 * @return array
350
 *   An associative array with tag code as key and html markup as the value.
351
 *
352
 * @see media_process_form()
353
 * @see media_token_to_markup()
354
 */
355
function _media_wysiwyg_generate_tagMap($text) {
356
  // Making $tagmap static as this function is called many times and
357
  // adds duplicate markup for each tag code in Drupal.settings JS,
358
  // so in media_process_form it adds something like tagCode:<markup>,
359
  // <markup> and when we replace in attach see two duplicate images
360
  // for one tagCode. Making static would make function remember value
361
  // between function calls. Since media_process_form is multiple times
362
  // with same form, this function is also called multiple times.
363
  static $tagmap = array();
364
  preg_match_all("/\[\[.*?\]\]/s", $text, $matches, PREG_SET_ORDER);
365
  foreach ($matches as $match) {
366
    // We see if tagContent is already in $tagMap, if not we add it
367
    // to $tagmap.  If we return an empty array, we break embeddings of the same
368
    // media multiple times.
369
    if (empty($tagmap[$match[0]])) {
370
      // @TODO: Total HACK, but better than nothing.
371
      // We should find a better way of cleaning this up.
372
      if ($markup_for_media = media_wysiwyg_token_to_markup($match, TRUE)) {
373
        $tagmap[$match[0]] = $markup_for_media;
374
      }
375
      else {
376
        $missing = file_create_url(drupal_get_path('module', 'media') . '/images/icons/default/image-x-generic.png');
377
        $tagmap[$match[0]] = '<div><img src="' . $missing . '" width="100px" height="100px"/></div>';
378
      }
379
    }
380
  }
381
  return $tagmap;
382
}
383

    
384
/**
385
 * Return a list of view modes allowed for a file embedded in the WYSIWYG.
386
 *
387
 * @param object $file
388
 *   A file entity.
389
 *
390
 * @return array
391
 *   An array of view modes that can be used on the file when embedded in the
392
 *   WYSIWYG.
393
 *
394
 * @deprecated
395
 */
396
function media_wysiwyg_get_wysiwyg_allowed_view_modes($file) {
397
  $enabled_view_modes = &drupal_static(__FUNCTION__, array());
398

    
399
  // @todo Add more caching for this.
400
  if (!isset($enabled_view_modes[$file->type])) {
401
    $enabled_view_modes[$file->type] = array();
402

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

    
406
    $entity_info = entity_get_info('file');
407
    $view_mode_settings = field_view_mode_settings('file', $file->type);
408
    foreach ($entity_info['view modes'] as $view_mode => $view_mode_info) {
409
      // Do not show view modes that don't have their own settings and will
410
      // only fall back to the default view mode.
411
      if (empty($view_mode_settings[$view_mode]['custom_settings'])) {
412
        continue;
413
      }
414

    
415
      // Don't present the user with an option to choose a view mode in which
416
      // the file is hidden.
417
      $extra_fields = field_extra_fields_get_display('file', $file->type, $view_mode);
418
      if (empty($extra_fields['file']['visible'])) {
419
        continue;
420
      }
421

    
422
      // Add the view mode to the list of enabled view modes.
423
      $enabled_view_modes[$file->type][$view_mode] = $view_mode_info;
424
    }
425
  }
426

    
427
  $view_modes = $enabled_view_modes[$file->type];
428
  media_wysiwyg_wysiwyg_allowed_view_modes_restrict($view_modes, $file);
429
  drupal_alter('media_wysiwyg_allowed_view_modes', $view_modes, $file);
430
  // Invoke the deprecated/misspelled alter hook as well.
431
  drupal_alter('media_wysiwyg_wysiwyg_allowed_view_modes', $view_modes, $file);
432
  return $view_modes;
433
}
434
/**
435
 * Do not show restricted view modes.
436
 */
437
function media_wysiwyg_wysiwyg_allowed_view_modes_restrict(&$view_modes, &$file) {
438
  $restricted_view_modes = db_query('SELECT display FROM {media_restrict_wysiwyg} WHERE type = :type', array(':type' => $file->type))->fetchCol();
439
  foreach ($restricted_view_modes as $restricted_view_mode) {
440
    if (array_key_exists($restricted_view_mode, $view_modes)) {
441
      unset($view_modes[$restricted_view_mode]);
442
    }
443
  }
444
}
445
/**
446
 * #pre_render callback: Modify the element if the render cache is filtered.
447
 */
448
function media_wysiwyg_pre_render_cached_filter($element) {
449
  // Remove contextual links since they are not compatible with cached filtered
450
  // text.
451
  if (isset($element['content']['#contextual_links'])) {
452
    unset($element['content']['#contextual_links']);
453
  }
454

    
455
  return $element;
456
}