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
|
static $recursion_stop;
|
60
|
$settings = array();
|
61
|
$match = str_replace("[[", "", $match);
|
62
|
$match = str_replace("]]", "", $match);
|
63
|
$tag = $match[0];
|
64
|
|
65
|
try {
|
66
|
if (!is_string($tag)) {
|
67
|
throw new Exception('Unable to find matching tag');
|
68
|
}
|
69
|
|
70
|
$tag_info = drupal_json_decode($tag);
|
71
|
|
72
|
if (!isset($tag_info['fid'])) {
|
73
|
throw new Exception('No file Id');
|
74
|
}
|
75
|
|
76
|
// Ensure the 'link_text' key is always defined.
|
77
|
if (!isset($tag_info['link_text'])) {
|
78
|
$tag_info['link_text'] = NULL;
|
79
|
}
|
80
|
|
81
|
// Ensure a valid view mode is being requested.
|
82
|
if (!isset($tag_info['view_mode'])) {
|
83
|
$tag_info['view_mode'] = variable_get('media_wysiwyg_wysiwyg_default_view_mode', 'full');
|
84
|
}
|
85
|
elseif ($tag_info['view_mode'] != 'default') {
|
86
|
$file_entity_info = entity_get_info('file');
|
87
|
if (!in_array($tag_info['view_mode'], array_keys($file_entity_info['view modes']))) {
|
88
|
// Media 1.x defined some old view modes that have been superseded by
|
89
|
// more semantically named ones in File Entity. The media_update_7203()
|
90
|
// function updates field settings that reference the old view modes,
|
91
|
// but it's impractical to update all text content, so adjust
|
92
|
// accordingly here.
|
93
|
static $view_mode_updates = array(
|
94
|
'media_preview' => 'preview',
|
95
|
'media_small' => 'teaser',
|
96
|
'media_large' => 'full',
|
97
|
);
|
98
|
if (isset($view_mode_updates[$tag_info['view_mode']])) {
|
99
|
$tag_info['view_mode'] = $view_mode_updates[$tag_info['view_mode']];
|
100
|
}
|
101
|
else {
|
102
|
throw new Exception('Invalid view mode');
|
103
|
}
|
104
|
}
|
105
|
}
|
106
|
|
107
|
$file = file_load($tag_info['fid']);
|
108
|
if (!$file) {
|
109
|
throw new Exception('Could not load media object');
|
110
|
}
|
111
|
// Check if we've got a recursion. Happens because a file_load() may
|
112
|
// triggers file_entity_is_page() which then again triggers a file load.
|
113
|
if (isset($recursion_stop[$file->fid])) {
|
114
|
return '';
|
115
|
}
|
116
|
$recursion_stop[$file->fid] = TRUE;
|
117
|
|
118
|
$tag_info['file'] = $file;
|
119
|
|
120
|
// The class attributes is a string, but drupal requires it to be
|
121
|
// an array, so we fix it here.
|
122
|
if (!empty($tag_info['attributes']['class'])) {
|
123
|
$tag_info['attributes']['class'] = explode(" ", $tag_info['attributes']['class']);
|
124
|
}
|
125
|
|
126
|
// Grab the potentially overrided fields from the file.
|
127
|
$fields = media_wysiwyg_filter_field_parser($tag_info);
|
128
|
foreach ($fields as $key => $value) {
|
129
|
$file->{$key} = $value;
|
130
|
}
|
131
|
|
132
|
$attributes = is_array($tag_info['attributes']) ? $tag_info['attributes'] : array();
|
133
|
$attribute_whitelist = variable_get('media_wysiwyg_wysiwyg_allowed_attributes', _media_wysiwyg_wysiwyg_allowed_attributes_default());
|
134
|
$settings['attributes'] = array_intersect_key($attributes, array_flip($attribute_whitelist));
|
135
|
$settings['fields'] = $fields;
|
136
|
|
137
|
if (!empty($tag_info['attributes']) && is_array($tag_info['attributes'])) {
|
138
|
$settings['attributes'] = array_intersect_key($tag_info['attributes'], array_flip($attribute_whitelist));
|
139
|
$settings['fields'] = $fields;
|
140
|
|
141
|
// Many media formatters will want to apply width and height independently
|
142
|
// of the style attribute or the corresponding HTML attributes, so pull
|
143
|
// these two out into top-level settings. Different WYSIWYG editors have
|
144
|
// different behavior with respect to whether they store user-specified
|
145
|
// dimensions in the HTML attributes or the style attribute - check both.
|
146
|
// Per http://www.w3.org/TR/html5/the-map-element.html#attr-dim-width, the
|
147
|
// HTML attributes are merely hints: CSS takes precedence.
|
148
|
if (isset($settings['attributes']['style'])) {
|
149
|
$css_properties = media_wysiwyg_parse_css_declarations($settings['attributes']['style']);
|
150
|
foreach (array('width', 'height') as $dimension) {
|
151
|
if (isset($css_properties[$dimension]) && substr($css_properties[$dimension], -2) == 'px') {
|
152
|
$settings[$dimension] = substr($css_properties[$dimension], 0, -2);
|
153
|
}
|
154
|
elseif (isset($settings['attributes'][$dimension])) {
|
155
|
$settings[$dimension] = $settings['attributes'][$dimension];
|
156
|
}
|
157
|
}
|
158
|
}
|
159
|
}
|
160
|
}
|
161
|
catch (Exception $e) {
|
162
|
watchdog('media', 'Unable to render media from %tag. Error: %error', array('%tag' => $tag, '%error' => $e->getMessage()));
|
163
|
return '';
|
164
|
}
|
165
|
|
166
|
// If the tag has link text stored with it, override the filename with it for
|
167
|
// the rest of this function, so that if the file is themed as a link, the
|
168
|
// desired text will be used (see, for example, theme_file_link()).
|
169
|
// @todo: Try to find a less hacky way to do this.
|
170
|
if (isset($tag_info['link_text'])) {
|
171
|
// The link text will have characters such as "&" encoded for HTML, but the
|
172
|
// filename itself needs the raw value when it is used to build the link,
|
173
|
// in order to avoid double encoding.
|
174
|
$file->filename = decode_entities($tag_info['link_text']);
|
175
|
}
|
176
|
|
177
|
if ($wysiwyg) {
|
178
|
$settings['wysiwyg'] = $wysiwyg;
|
179
|
// If sending markup to a WYSIWYG, we need to pass the file information so
|
180
|
// that an inline macro can be generated when the WYSIWYG is detached.
|
181
|
// The WYSIWYG plugin is expecting this information in the
|
182
|
// Drupal.settings.mediaDataMap variable.
|
183
|
$element = media_wysiwyg_get_file_without_label($file, $tag_info['view_mode'], $settings);
|
184
|
$data = array(
|
185
|
'type' => 'media',
|
186
|
'fid' => $file->fid,
|
187
|
'view_mode' => $tag_info['view_mode'],
|
188
|
'link_text' => $tag_info['link_text'],
|
189
|
);
|
190
|
drupal_add_js(array('mediaDataMap' => array($file->fid => $data)), 'setting');
|
191
|
$element['#attributes']['data-fid'] = $file->fid;
|
192
|
$element['#attributes']['data-media-element'] = '1';
|
193
|
$element['#attributes']['class'][] = 'media-element';
|
194
|
}
|
195
|
else {
|
196
|
// Display the field elements.
|
197
|
$element = array();
|
198
|
// Render the file entity, for sites using the file_entity rendering method.
|
199
|
if (variable_get('media_wysiwyg_default_render', 'file_entity') == 'file_entity') {
|
200
|
$element['content'] = file_view($file, $tag_info['view_mode']);
|
201
|
}
|
202
|
$element['content']['file'] = media_wysiwyg_get_file_without_label($file, $tag_info['view_mode'], $settings);
|
203
|
// Overwrite or set the file #alt attribute if it has been set in this
|
204
|
// instance.
|
205
|
if (!empty($element['content']['file']['#attributes']['alt'])) {
|
206
|
$element['content']['file']['#alt'] = $element['content']['file']['#attributes']['alt'];
|
207
|
}
|
208
|
// Overwrite or set the file #title attribute if it has been set in this
|
209
|
// instance.
|
210
|
if (!empty($element['content']['file']['#attributes']['title'])) {
|
211
|
$element['content']['file']['#title'] = $element['content']['file']['#attributes']['title'];
|
212
|
}
|
213
|
// For sites using the legacy field_attach rendering method, attach fields.
|
214
|
if (variable_get('media_wysiwyg_default_render', 'file_entity') == 'field_attach') {
|
215
|
field_attach_prepare_view('file', array($file->fid => $file), $tag_info['view_mode']);
|
216
|
entity_prepare_view('file', array($file->fid => $file));
|
217
|
$element['content'] += field_attach_view('file', $file, $tag_info['view_mode']);
|
218
|
}
|
219
|
if (count(element_children($element['content'])) > 1) {
|
220
|
// Add surrounding divs to group them together.
|
221
|
// We dont want divs when there are no additional fields to allow files
|
222
|
// to display inline with text, without breaking p tags.
|
223
|
$element['content']['#type'] = 'container';
|
224
|
$element['content']['#attributes']['class'] = array(
|
225
|
'media',
|
226
|
'media-element-container',
|
227
|
'media-' . $element['content']['file']['#view_mode']
|
228
|
);
|
229
|
}
|
230
|
|
231
|
// Conditionally add a pre-render if the media filter output is be cached.
|
232
|
$filters = filter_get_filters();
|
233
|
if (!isset($filters['media_filter']['cache']) || $filters['media_filter']['cache']) {
|
234
|
$element['#pre_render'][] = 'media_wysiwyg_pre_render_cached_filter';
|
235
|
}
|
236
|
}
|
237
|
drupal_alter('media_wysiwyg_token_to_markup', $element, $tag_info, $settings);
|
238
|
$output = drupal_render($element);
|
239
|
unset($recursion_stop[$file->fid]);
|
240
|
return $output;
|
241
|
}
|
242
|
|
243
|
/**
|
244
|
* Parse the field array from the collapsed AJAX string.
|
245
|
*/
|
246
|
function media_wysiwyg_filter_field_parser($tag_info) {
|
247
|
$fields = array();
|
248
|
if (isset($tag_info['fields'])) {
|
249
|
foreach($tag_info['fields'] as $field_name => $field_value) {
|
250
|
if (strpos($field_name, 'field_') === 0) {
|
251
|
$parsed_field = explode('[', str_replace(']', '', $field_name));
|
252
|
$ref = &$fields;
|
253
|
|
254
|
// Each key of the field needs to be the child of the previous key.
|
255
|
foreach ($parsed_field as $key) {
|
256
|
if (!isset($ref[$key])) {
|
257
|
$ref[$key] = array();
|
258
|
}
|
259
|
$ref = &$ref[$key];
|
260
|
}
|
261
|
|
262
|
// The value should be set at the deepest level.
|
263
|
// Fields that use rich-text markup will be urlencoded.
|
264
|
$ref = urldecode($field_value);
|
265
|
}
|
266
|
}
|
267
|
}
|
268
|
return $fields;
|
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
|
}
|
360
|
|
361
|
/**
|
362
|
* #pre_render callback: Modify the element if the render cache is filtered.
|
363
|
*/
|
364
|
function media_wysiwyg_pre_render_cached_filter($element) {
|
365
|
// Remove contextual links since they are not compatible with cached filtered
|
366
|
// text.
|
367
|
if (isset($element['content']['#contextual_links'])) {
|
368
|
unset($element['content']['#contextual_links']);
|
369
|
}
|
370
|
|
371
|
return $element;
|
372
|
}
|