voxpelli / drupal-oembed

An oEmbed client and provider for Drupal

This URL has Read+Write access

drupal-oembed / oembed.inc
100644 59 lines (53 sloc) 2.081 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
// $Id$
 
/**
 * @file
 * Functions for the oEmbed filter
 */
 
function _oembed_filter_apply($text, $format) {
  global $_oembed_default_attributes;
  $_oembed_default_attributes = array(
    'maxwidth' => intval(variable_get('oembed_maxwidth_' . $format, '')),
    'maxheight' => intval(variable_get('oembed_maxheight_' . $format, '')),
  );
  $text = preg_replace_callback("`(^|<p>|<li>|<br\s*/?>|[ \n\r\t\(])((http://|https://|ftp://|mailto:|smb://|afp://|file://|gopher://|news://|ssl://|sslv2://|sslv3://|tls://|tcp://|udp://)([a-zA-Z0-9@:%_+*~#?&=.,/;-]*[a-zA-Z0-9@:%_+*~#&=/;-]))([.,?!]*?)(?=($|</p>|</li>|<br\s*/?>|[ \n\r\t\)]))`i", '_oembed_preg_parse', $text);
  unset($_oembed_default_attributes);
  return $text;
}
 
function _oembed_filter_settings($format) {
  $form = array();
  $form['oembed'] = array(
    '#type' => 'fieldset',
    '#title' => t('oEmbed'),
    '#collapsible' => TRUE,
  );
  $form['oembed']['oembed_maxwidth_' . $format] = array(
    '#type' => 'textfield',
    '#title' => t('Maximum width of embed'),
    '#default_value' => variable_get('oembed_maxwidth_' . $format, ''),
    '#description' => t('The maximum width of an embed, isn\'t respected by all providers'),
  );
  $form['oembed']['oembed_maxheight_' . $format] = array(
    '#type' => 'textfield',
    '#title' => t('Maximum height of embed'),
    '#default_value' => variable_get('oembed_maxheight_' . $format, ''),
    '#description' => t('The maximum height of an embed, isn\'t respected by all providers'),
  );
  return $form;
}
 
function _oembed_preg_parse($match) {
  return _oembed_resolve_link($match[2], $match[1], $match[5]);
}
 
function _oembed_resolve_link($match, $prefix, $suffix) {
  $return = '';
  $url = decode_entities($match);
  $matches = array();
  if ($provider = oembedcore_get_provider($url, $matches)) {
    $embed = oembedcore_oembed_fetch($provider, $url, $matches);
    $return = oembedcore_oembed_html($embed, $url);
  }
  if (empty($return)) {
    $return = l($match, $url, array('absolute' => TRUE));
  }
 
  return $prefix . $return . $suffix;
}