voxpelli / drupal-oembed

An oEmbed client and provider for Drupal

This URL has Read+Write access

drupal-oembed / oembedprovider.module
100644 131 lines (113 sloc) 2.96 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
// $Id$
 
/**
 * @file
 * Module for providing content as defined in the oEmbed specification
 */
 
/**
 * Implementation of hook_menu().
 */
function oembedprovider_menu() {
  $menu = array();
 
  $handler = array(
    'type' => MENU_CALLBACK,
    'file' => 'oembedprovider.inc',
    'page callback' => '_oembedprovider_handle_request',
    'page arguments' => array(2),
    'access arguments' => array('access content'),
  );
 
  $menu['oembed/endpoint'] = $menu['oembed/endpoint/%'] = $handler;
 
  return $menu;
}
 
/**
 * Implementation of hook_theme().
 */
function oembedprovider_theme() {
  $themes = array();
  $themes['oembed_node'] = array(
    'arguments' => array(
      'node' => NULL,
    ),
  );
  return $themes;
}
 
/**
 * The default provider to handle nodes
 *
 * @param string $url
 * @param array $matches
 */
function oembedprovider_node_provider($provider, $url, $matches) {
  module_load_include('inc', 'oembedprovider');
  return _oembedprovider_node_provider($provider, $url, $matches);
}
 
/**
 * Default theme implementation for oembed_node. We just mirror the node_view function
 * and pass everything on to the node template.
 *
 * @param object $node
 * @return string
 * The html representation of the node
 */
function theme_oembed_node($node) {
  $node = node_build_content($node, TRUE, FALSE);
 
  // Set the proper node part, then unset unused $node part so that a bad
  // theme can not open a security hole.
  $content = drupal_render($node->content);
  $node->teaser = $content;
  unset($node->body);
 
  // Allow modules to modify the fully-built node.
  node_invoke_nodeapi($node, 'alter', TRUE, FALSE);
 
  return theme('node', $node, TRUE, FALSE);
}
 
/**
 * Implementation of hook_oembedprovider().
 *
 * @return array
 */
function oembedprovider_oembedprovider() {
  $base_url = url('', array('absolute' => TRUE)) . 'node/*';
  return array(
    $base_url => array(
      'callback' => 'oembedprovider_node_provider',
    ),
  );
}
 
/**
 * Returns all the registered response formats
 *
 * @return array
 */
function oembedprovider_formats() {
  static $formats;
 
  if (!$formats) {
    $cache_key = 'oembedprovider:formats';
 
    if (!$reset && ($cache = cache_get($cache_key)) && isset($cache->data)) {
      $formats = $cache->data;
    }
    else {
      $formats = array(
        'json' => array(
          'mime' => 'text/javascript',
          'callback' => '_oembedprovider_formats_json',
        ),
        'xml' => array(
          'mime' => 'text/xml',
          'callback' => '_oembedprovider_formats_xml',
        ),
      );
      drupal_alter('oembedprovider_formats', $formats);
 
      cache_set($cache_key, $formats);
    }
  }
 
  return $formats;
}
 
/**
 * Implementation of hook_oembedprovider_formats_alter().
 */
function oembedprovider_oembedprovider_formats_alter(&$formats) {
  $formats['jsonp'] = array(
    'mime' => 'text/javascript',
    'callback' => '_oembedprovider_formats_jsonp',
  );
}