oEmbed Plugin

* Nearly 30 services supported
* Some output of them fixed using smarty templating
* s9y 1.6 and 1.7 compatibility (different smarty versions)
* oEmbed result fixing while saving to DB.
This commit is contained in:
Grischa Brockhaus 2011-12-18 00:44:57 +01:00
parent 12502f22d3
commit b0c245e434
7 changed files with 160 additions and 26 deletions

View file

@ -90,9 +90,9 @@ class OEmbedDatabase {
$str=str_ireplace("\u003C","<",$str);
$str=str_ireplace("\u003E",">",$str);
// Clear CDATA Trash.
$str = preg_replace("@^<!\[CDATA\[(.*)]]>$@", '$1', $str);
$str = preg_replace("@^<!\[CDATA\[(.*)$@", '$1', $str);
$str = preg_replace("@(.*)]]>$@", '$1', $str);
$str = preg_replace("@^<!\[CDATA\[(.*)\]\]>$@", '$1', $str);
$str = preg_replace("@^<!\[CDATA\[(.*)@", '$1', $str);
$str = preg_replace("@(.*)\]\]>$@", '$1', $str);
return $str;
}

View file

@ -1,9 +1,12 @@
<?php
class OEmbedTemplater {
/* get the right template path - as default in template folder or the fallback plugin folder */
/* get the right template (s9y template path, then plugin path) and expand it */
function fetchTemplate($filename, $oembed, $url) {
global $serendipity;
if (!is_object($serendipity['smarty']))
serendipity_smarty_init();
// Declare the oembed to smarty
$serendipity['smarty']->assign('oembedurl',$url);
$serendipity['smarty']->assign('oembed',(array)$oembed);
@ -11,11 +14,22 @@ class OEmbedTemplater {
if (!$tfile || $filename == $tfile) {
$tfile = dirname(__FILE__) . '/' . $filename;
}
$inclusion = $serendipity['smarty']->security_settings[@INCLUDE_ANY];
$serendipity['smarty']->security_settings[@INCLUDE_ANY] = true;
if (version_compare($serendipity['version'], '1.7-alpha1','>=')) {
$serendipity['smarty']->disableSecurity();
}
else {
$serendipity['smarty']->security = false;
}
// be smarty 3 compat including the serendipity_smarty class wrappers ->fetch and ->display methods and remove changed parameter number 4
$content = $serendipity['smarty']->fetch('file:'. $tfile);//, false
$content = @$serendipity['smarty']->fetch('file:'. $tfile);//, false
$serendipity['smarty']->security_settings[@INCLUDE_ANY] = $inclusion;
return $content;
}
}

View file

@ -8,14 +8,17 @@
@define('PLUGIN_EVENT_OEMBED_NAME', 'oEmbed Plugin');
@define('PLUGIN_EVENT_OEMBED_DESC', 'oEmbed is a format for allowing an embedded representation of a URL on your blog. It allows blog articles to display embedded content (such as tweets, photos or videos) when a user posts a link to that resource, without having to parse the resource directly.');
/*
@define('PLUGIN_EVENT_OEMBED_EXPAND_TWEETBACKS', 'Expand tweetbacks');
@define('PLUGIN_EVENT_OEMBED_EXPAND_TWEETBACKS_DESC', 'If enabled the plugin will detect tweetbacks and set their content to the oEmbed content delivered from twitter.');
*/
@define('PLUGIN_EVENT_OEMBED_MAXWIDTH', 'Max width of replacements');
@define('PLUGIN_EVENT_OEMBED_MAXWIDTH_DESC', 'This is the max width the service should produce when providing a replacement. Not all services supports this but most.');
@define('PLUGIN_EVENT_OEMBED_MAXHEIGHT', 'Max height of replacements');
@define('PLUGIN_EVENT_OEMBED_MAXHEIGHT_DESC','This is the max height the service should produce when providing a replacement. Not all services supports this but most.');
@define('PLUGIN_EVENT_OEMBED_INFO', '<h3>oEmbed Plugin</h3>' .
'<p>'.
'This plugin expands URLs to pages of known services to a representation of that URL. It shows i.e. the video for a youtube URL or the image instead of a flickr URL.<br/>' .
'The syntax for this plugin is <b>[embed <i>link</i>]</b>. If the link is not supported by the plugin at the moment, it will replace the URL by a link pointing to that URL.<br/>'.
'The syntax of this plugin is <b>[embed <i>link</i>]</b> (or <b>[e <i>link</i>]</b> if you like it shorter).<br/>'.
'If the link is not supported by the plugin at the moment, it will replace the URL by a link pointing to that URL.<br/>'.
'</p><p>'.
'Please put this plugin at the top of your plugins list, so no other plugin can change this syntax (by adding a href i.e.)'.
'</p><p>'.

View file

@ -1,16 +1,29 @@
{* oembed.tpl last modified 2011-12-01 *}
<span class="serendipity_oembed">
{if $oembed.type=='rich'}
{$oembed.html}
{elseif $oembed.type=='video'}
{$oembed.html}
{elseif $oembed.type=='image'}
<a href="{$oembed.url}"><img src="{$oembed.thumbnail_url}""{if $oembed.title} title="{$oembed.title}" alt="{$oembed.title}"{/if}/></a>
{elseif $oembed.type=='photo'}
<img src="{$oembed.url}"{if $oembed.title} title="{$oembed.title}" alt="{$oembed.title}"{/if}/>
{elseif $oembed.type=='link'}
<a href="{$oembedurl}" title="{$oembed.title}">{$oembed.author_name}</a>
{if $oembed.type=='rich'} {* =================================================== RICH *}
{if $oembed.provider_name|stripos:"Wikipedia" !== false}
<blockquote>{$oembed.html}</blockquote>
{elseif $oembed.provider_name|stripos:"IMDB" !== false} {* beautify IMDB content *}
<blockquote>{$oembed.html|replace:"<h2>":"<strong>"|replace:"</h2>":"</strong>"|replace:"<img":"<img align='right'"}</blockquote>
{elseif $oembed.provider-name|stripos:"Soundcloud" !== false} {* beautify SoundCloud *}
{$oembed.html|replace:"</object>":"</object><br/>"}
{else}
<a href="{$oembedurl}" target="_blank">{$oembedurl}</a>
{$oembed.html}
{/if}
{elseif $oembed.type=='video'} {* =================================================== VIDEO *}
{$oembed.html}
{elseif $oembed.type=='image'} {* =================================================== IMAGE *}
<a href="{$oembed.url}"><img src="{$oembed.thumbnail_url}""{if $oembed.title} title="{$oembed.title}" alt="{$oembed.title}"{/if}/></a>
{elseif $oembed.type=='photo'} {* =================================================== PHOTO *}
<img src="{$oembed.url}"{if $oembed.title} title="{$oembed.title}" alt="{$oembed.title}"{/if}/>
{elseif $oembed.type=='link'} {* =================================================== LINK *}
{if $oembed.description}
{if $oembed.title}<strong>{$oembed.title}</strong><br/>{/if}
<p>{if $oembed.thumbnail_url}<img src="{$oembed.thumbnail_url}" align="left">{/if}{$oembed.description}</p>
{else}
<a href="{$oembedurl}" title="{$oembed.title}">{$oembed.author_name}</a>
{/if}
{else}
<a href="{$oembedurl}" target="_blank">{if $oembed.error}{$oembed.error}{else}{$oembedurl}{/if}</a>
{/if}
</span>

View file

@ -41,8 +41,26 @@ class OEmbedProvider extends EmbedProvider{
public function match($url){
return preg_match($this->urlRegExp,$url);
}
private function file_get_contents($fileurl) {
if (defined('OEMBED_USE_CURL') && OEMBED_USE_CURL && defined('CURLOPT_URL')) {
$ch = curl_init();
$timeout = 5; // 0 wenn kein Timeout
curl_setopt($ch, CURLOPT_URL, $fileurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_MAXREDIRS, 3 );
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_content = curl_exec($ch);
curl_close($ch);
}
else {
$context = array ( 'http' => array ( 'method' => 'GET', 'max_redirects' => 3, ),);
$file_content = file_get_contents($fileurl, null, stream_context_create($context));
}
return $file_content;
}
private function provideXML($url){
return file_get_contents(preg_replace("/\{url\}/",urlencode($url),$this->xmlEndpoint));
return $this->file_get_contents(preg_replace("/\{url\}/",urlencode($url),$this->xmlEndpoint));
}
private function getTypeObj($type){
switch($type){
@ -89,7 +107,7 @@ class OEmbedProvider extends EmbedProvider{
} else if ($format=="serialized"){
return $this->provideSerialized($url);
} else {
return file_get_contents(preg_replace("/\{url\}/",urlencode($url),$this->jsonEndpoint));
return $this->file_get_contents(preg_replace("/\{url\}/",urlencode($url),$this->jsonEndpoint));
}
}
public function register(){}

View file

@ -36,7 +36,17 @@
<url>http://*.youtu.be/*</url>
<endpoint>http://www.youtube.com/oembed</endpoint>
</provider>
<provider>
<name>SmugMug</name>
<url>http://*.smugmug.com/*</url>
<endpoint>http://api.smugmug.com/services/oembed/</endpoint>
</provider>
<provider>
<name>Photobucket</name>
<url>http://*.photobucket.com/(albums|groups)/*</url>
<endpoint>http://photobucket.com/oembed</endpoint>
</provider>
<provider>
<name>Official.fm (tracks and playlist)</name>
<url>http://official.fm/(playlists|tracks)/*</url>
@ -49,6 +59,12 @@
<jsononly/>
<endpoint>http://blip.tv/oembed</endpoint>
</provider>
<provider>
<name>Qik Mobile Videos</name>
<url>http://qik.com/video/*</url>
<endpoint>http://qik.com/api/oembed.{format}</endpoint>
</provider>
<provider>
<name>My Opera</name>
@ -105,6 +121,75 @@
<url>http://*.soundcloud.com/*</url>
<endpoint>http://soundcloud.com/oembed</endpoint>
</provider>
<provider>
<name>Skitch</name>
<url>http://(www.)?skitch.com/*</url>
<endpoint>http://skitch.com/oembed/</endpoint>
</provider>
<!-- noembed providers -->
<provider>
<name>Wikipedia (via noembed.com)</name>
<jsononly/>
<url>https?://*.wikipedia.org/wiki/*</url>
<endpoint>http://noembed.com/embed</endpoint>
</provider>
<provider>
<name>Twitpic (via noembed.com)</name>
<jsononly/>
<url>http://(www.)?twitpic.com/*</url>
<endpoint>http://noembed.com/embed</endpoint>
</provider>
<provider>
<name>Imgur.com (via noembed.com)</name>
<jsononly/>
<url>http://imgur.com/*</url>
<endpoint>http://noembed.com/embed</endpoint>
</provider>
<provider>
<name>Imdb.com (via noembed.com)</name>
<jsononly/>
<url>http://(www.)?imdb.com/title/*</url>
<endpoint>http://noembed.com/embed</endpoint>
</provider>
<provider>
<name>CloudApp (via noembed.com)</name>
<jsononly/>
<url>http://cl.ly/*</url>
<endpoint>http://noembed.com/embed</endpoint>
</provider>
<provider>
<name>ASCII Art Farts (via noembed.com)</name>
<jsononly/>
<url>http://www.asciiartfarts.com/*.html</url>
<endpoint>http://noembed.com/embed</endpoint>
</provider>
<provider>
<name>GiantBomb (via noembed.com)</name>
<jsononly/>
<url>http://www.giantbomb.com/*</url>
<endpoint>http://noembed.com/embed</endpoint>
</provider>
<provider>
<name>Spotify (via noembed.com)</name>
<jsononly/>
<url>https?://open.spotify.com/(track|album)/*</url>
<endpoint>http://noembed.com/embed</endpoint>
</provider>
<!-- Too big for blogs
<provider>
<name>Amazon.com (via noembed.com)</name>
<jsononly/>
<url>https?://www.(amazon|amzn).com/*</url>
<endpoint>http://noembed.com/embed</endpoint>
</provider>
-->
<!-- oohembed.com providers -->
<provider>
<name>Audioboo (via oohembed.com)</name>
<jsononly/>
<url>http://((www.)?audio)?boo.fm/boos/*</url>
<endpoint>http://oohembed.com/oohembed/</endpoint>
</provider>
</providers>

View file

@ -16,6 +16,7 @@ require_once dirname(__FILE__) . '/OEmbedDatabase.php';
require_once dirname(__FILE__) . '/OEmbedTemplater.php';
require_once dirname(__FILE__) . '/oembed/ProviderList.php';
@define('OEMBED_USE_CURL',TRUE);
class serendipity_event_oembed extends serendipity_event
{
@ -74,7 +75,7 @@ class serendipity_event_oembed extends serendipity_event
if ($simplePatterns==null) {
$simplePatterns = array(
//'simpleTweet' => '@\(tweet\s+(\S*)\)@Usi',
'simpleTweet' => '@\[(?:e|embed|tweet)\s+(.*)\]@Usi',
'simpleTweet' => '@\[(?:e|embed)\s+(.*)\]@Usi',
);
}