Skip to content

Commit ed4af28

Browse files
committed
Initial commit
0 parents  commit ed4af28

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+4137
-0
lines changed

CHANGELOG.txt

Whitespace-only changes.

README.txt

Whitespace-only changes.

images/next.png

560 Bytes
Loading

images/next_press.png

632 Bytes
Loading

images/play.png

1.26 KB
Loading

images/prev.png

551 Bytes
Loading

images/prev_press.png

618 Bytes
Loading

images/providers/Blank.png

379 Bytes
Loading

images/providers/Flickr.png

467 Bytes
Loading

images/providers/FoodPop.png

3.32 KB
Loading

images/providers/Hulu.png

3.32 KB
Loading

images/providers/Metacafe.png

3.4 KB
Loading

images/providers/Rss.png

723 Bytes
Loading

images/providers/SlideShare.png

782 Bytes
Loading

images/providers/TwitPic.png

719 Bytes
Loading

images/providers/Vimeo.png

613 Bytes
Loading

images/providers/YouTube.png

663 Bytes
Loading

images/providers/yFrog.png

880 Bytes
Loading

includes/micropublisher.common.inc

+181
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Common functions
6+
*/
7+
8+
/**
9+
* Returns the provider for a url.
10+
*
11+
* @param string $url
12+
* Teh url to get the provider for.
13+
* @return mixed
14+
* A valid callback or FALSE
15+
*/
16+
function micropublisher_oembed_get_provider($url, &$matches) {
17+
$host = _micropublisher_oembed_get_host($url);
18+
if ($host) {
19+
$providers = micropublisher_oembed_providers($host);
20+
foreach ($providers as $regex => $info) {
21+
if (preg_match($regex, $url, $matches)) {
22+
return $info;
23+
}
24+
}
25+
}
26+
return FALSE;
27+
}
28+
29+
/**
30+
* A utility function to get the base domain from a url.
31+
*
32+
* @param string $uri
33+
* The uri to get the domain form
34+
* @return string
35+
* The domain or NULL
36+
*/
37+
function _micropublisher_oembed_get_host($uri) {
38+
$matches = array();
39+
if (preg_match('/^https?\:\/\/([^\/]+)/', $uri, $matches)) {
40+
$matches = explode('.', $matches[1]);
41+
$match_count = count($matches);
42+
if ($match_count > 1) {
43+
return $matches[$match_count - 2] . '.' . $matches[$match_count - 1];
44+
}
45+
else {
46+
return $matches[0];
47+
}
48+
}
49+
return NULL;
50+
}
51+
52+
/**
53+
* Returns all the registered providers, or the providers for a specific host.
54+
*
55+
* @param string $host
56+
* Optional. Supply a hostname if you only want the provider patterns for a specific host.
57+
* @return array
58+
*/
59+
function micropublisher_oembed_providers($url_host = NULL) {
60+
static $providers;
61+
62+
if (!$providers) {
63+
$cache_key = 'oembed:micropublisher:providers';
64+
65+
if (!($cache = cache_get($cache_key)) && isset($cache->data)) {
66+
$providers = $cache->data;
67+
}
68+
else {
69+
$providers = array();
70+
$modules = module_implements('oembedprovider');
71+
foreach ($modules as $module) {
72+
$ps = call_user_func($module . '_oembedprovider');
73+
foreach ($ps as $pattern => $info) {
74+
$host = _micropublisher_oembed_get_host($info->scheme);
75+
$regex_pattern = '/' . str_replace('\*', '(.+)', preg_quote($pattern, '/')) . '/';
76+
$providers[$host][$regex_pattern] = $info;
77+
}
78+
}
79+
drupal_alter('oembedprovider', $providers);
80+
foreach ($providers as $host => &$patterns) {
81+
uksort($patterns, '_micropublisher_oembed_specificity_compare');
82+
}
83+
cache_set($cache_key, $providers);
84+
}
85+
}
86+
87+
if ($url_host) {
88+
return isset($providers[$url_host]) ? $providers[$url_host] : array();
89+
}
90+
return $providers;
91+
}
92+
93+
/**
94+
* Fetch data for an embeddable URL.
95+
*
96+
* @param string $url
97+
* An external URL for the content to embed.
98+
* @param array $attributes
99+
* An associative array of attributes, with the following keys:
100+
* - 'maxwidth'
101+
* The maximum width of the embed, in pixels.
102+
* - 'maxheight'
103+
* The maximum height of the embed, in pixels.
104+
* @return
105+
* False or an object representing the embeddable data of the URL.
106+
*/
107+
function micropublisher_oembed_data($url, $attributes = array()) {
108+
$matches = array();
109+
if ($provider = (array) micropublisher_oembed_get_provider($url, $matches)) {
110+
return micropublisher_oembed_fetch($provider, $url, $matches, $attributes);
111+
}
112+
return FALSE;
113+
}
114+
115+
/**
116+
* Fetch data for an embeddable URL.
117+
*/
118+
function micropublisher_oembed_fetch($provider, $url, $matches, $attributes = array()) {
119+
global $_oembed_default_attributes;
120+
121+
$embed = FALSE;
122+
123+
if (!empty($_oembed_default_attributes)) {
124+
$attributes = array_merge($_oembed_default_attributes, $attributes);
125+
}
126+
$attributes['url'] = $url;
127+
$query = http_build_query($attributes, NULL, '&');
128+
129+
$source = isset($provider['callback']) ? $provider['callback'] : $provider['endpoint'];
130+
$cache_key = 'oembed:micropublisher:embed:' . md5($source . $url . $query);
131+
$cache = cache_get($cache_key);
132+
133+
if ($cache && isset($cache->data)) {
134+
$embed = $cache->data;
135+
}
136+
else {
137+
if (!empty($provider['callback'])) {
138+
$embed = call_user_func($provider['callback'], $provider, $url, $matches, $attributes);
139+
if ($embed) {
140+
$embed = (object) $embed;
141+
}
142+
}
143+
else {
144+
$fetch_url = $provider['endpoint'] . '?' . $query;
145+
146+
//TODO: Add alternative ways of fetching the content - like http client?
147+
$response = drupal_http_request($fetch_url);
148+
if (!$response->error) {
149+
$embed = json_decode($response->data);
150+
if (!is_object($embed)) {
151+
try {
152+
$embed = @new SimpleXMLElement($response->data);
153+
$embed = (object) get_object_vars($embed);
154+
if (!is_string($embed->title)) {
155+
$embed->title = '';
156+
}
157+
} catch (Exception $e) {
158+
watchdog('micropublisher', 'Could not parse response from %url.', array('%url' => $fetch_url), WATCHDOG_ERROR);
159+
}
160+
}
161+
162+
if (empty($embed->version) || empty($embed->type) || intval($embed->version) != 1) {
163+
$embed = FALSE;
164+
watchdog('micropublisher', 'Response from %url not a valid oEmbed response.', array('%url' => $fetch_url), WATCHDOG_ERROR);
165+
}
166+
}
167+
else {
168+
watchdog('micropublisher', 'Error fetching data from %url.', array('%url' => $fetch_url), WATCHDOG_ERROR);
169+
}
170+
}
171+
172+
if ($embed) {
173+
$embed->original_url = $url;
174+
}
175+
176+
$max_age = isset($embed->cache_age) ? intval($embed->cache_age) : empty($provider['callback']) ? 600 : 60;
177+
cache_set($cache_key, $embed, 'cache', time() + $max_age);
178+
}
179+
180+
return $embed;
181+
}

0 commit comments

Comments
 (0)