From 0fd597cd348e458de1a9e5ea99c7fc6728cbd243 Mon Sep 17 00:00:00 2001 From: Grischa Brockhaus Date: Sun, 18 Dec 2011 01:55:33 +0100 Subject: [PATCH] Oembed Plugin * More CURL magic (encapsulated in seperate class) * Another wikipedia oembed service as the other didn't was right always. * No php functions in smarty template anymore --- serendipity_event_oembed/CurlFetcher.php | 79 +++++++++++++++++++ serendipity_event_oembed/oembed.tpl | 12 +-- .../oembed/OEmbedProvider.class.php | 28 ++----- serendipity_event_oembed/oembed/providers.xml | 10 +++ 4 files changed, 104 insertions(+), 25 deletions(-) create mode 100644 serendipity_event_oembed/CurlFetcher.php diff --git a/serendipity_event_oembed/CurlFetcher.php b/serendipity_event_oembed/CurlFetcher.php new file mode 100644 index 00000000..9792cf5c --- /dev/null +++ b/serendipity_event_oembed/CurlFetcher.php @@ -0,0 +1,79 @@ + array ( 'method' => 'GET', 'max_redirects' => $max_redirects, ),); + $file_content = file_get_contents($fileurl, null, stream_context_create($context)); + } + return $file_content; + } + + /** + * Handling redirections with curl if safe_mode or open_basedir is enabled. The function working transparent, no problem with header and returntransfer options. You can handle the max redirection with the optional second argument (the function is set the variable to zero if max redirection exceeded). + * Second parameter values: + * - maxredirect is null or not set: redirect maximum five time, after raise PHP warning + * - maxredirect is greather then zero: no raiser error, but parameter variable set to zero + * - maxredirect is less or equal zero: no follow redirections + * (see: http://php.net/manual/en/function.curl-setopt.php) + */ + private function curl_exec_follow(/*resource*/ $ch, /*int*/ &$maxredirect = null) { + $mr = $maxredirect === null ? 5 : intval($maxredirect); + if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) { + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $mr > 0); + curl_setopt($ch, CURLOPT_MAXREDIRS, $mr); + } else { + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); + if ($mr > 0) { + $newurl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); + + $rch = curl_copy_handle($ch); + curl_setopt($rch, CURLOPT_HEADER, true); + curl_setopt($rch, CURLOPT_NOBODY, true); + curl_setopt($rch, CURLOPT_FORBID_REUSE, false); + curl_setopt($rch, CURLOPT_RETURNTRANSFER, true); + do { + curl_setopt($rch, CURLOPT_URL, $newurl); + $header = curl_exec($rch); + if (curl_errno($rch)) { + $code = 0; + } else { + $code = curl_getinfo($rch, CURLINFO_HTTP_CODE); + if ($code == 301 || $code == 302) { + preg_match('/Location:(.*?)\n/', $header, $matches); + $newurl = trim(array_pop($matches)); + } else { + $code = 0; + } + } + } while ($code && --$mr); + curl_close($rch); + if (!$mr) { + if ($maxredirect === null) { + trigger_error('Too many redirects. When following redirects, libcurl hit the maximum amount.', E_USER_WARNING); + } else { + $maxredirect = 0; + } + return false; + } + curl_setopt($ch, CURLOPT_URL, $newurl); + } + } + return curl_exec($ch); + } +} \ No newline at end of file diff --git a/serendipity_event_oembed/oembed.tpl b/serendipity_event_oembed/oembed.tpl index 15705980..2d574e58 100644 --- a/serendipity_event_oembed/oembed.tpl +++ b/serendipity_event_oembed/oembed.tpl @@ -1,11 +1,11 @@ {* oembed.tpl last modified 2011-12-01 *} {if $oembed.type=='rich'} {* =================================================== RICH *} -{if $oembed.provider_name|stripos:"Wikipedia" !== false} +{if $oembed.provider_name=="Wikipedia"}
{$oembed.html}
-{elseif $oembed.provider_name|stripos:"IMDB" !== false} {* beautify IMDB content *} +{elseif $oembed.provider_name=="IMDB"} {* beautify IMDB content *}
{$oembed.html|replace:"

":""|replace:"

":""|replace:" -{elseif $oembed.provider-name|stripos:"Soundcloud" !== false} {* beautify SoundCloud *} +{elseif $oembed.provider-name=="Soundcloud"} {* beautify SoundCloud *} {$oembed.html|replace:"":"
"} {else} {$oembed.html} @@ -17,13 +17,15 @@ {elseif $oembed.type=='photo'} {* =================================================== PHOTO *} {$oembed.title} {elseif $oembed.type=='link'} {* =================================================== LINK *} +{if $oembed.provider_name=="Wikipedia"}
{/if} {if $oembed.description} {if $oembed.title}{$oembed.title}
{/if} -

{if $oembed.thumbnail_url}{/if}{$oembed.description}

+

{if $oembed.thumbnail_url}{/if}{$oembed.description}{if $oembed.url}
[link]{/if}

{else} {$oembed.author_name} {/if} -{else} +{if $oembed.provider_name=="Wikipedia"}
{/if} +{else} {* Link type finishes *} {if $oembed.error}{$oembed.error}{else}{$oembedurl}{/if} {/if} \ No newline at end of file diff --git a/serendipity_event_oembed/oembed/OEmbedProvider.class.php b/serendipity_event_oembed/oembed/OEmbedProvider.class.php index d6ab375d..f0205c97 100644 --- a/serendipity_event_oembed/oembed/OEmbedProvider.class.php +++ b/serendipity_event_oembed/oembed/OEmbedProvider.class.php @@ -1,12 +1,14 @@ onlyJson = $onlyJson; @@ -24,8 +26,8 @@ class OEmbedProvider extends EmbedProvider{ } if ($this->dimensionsSupported) { if (!empty($this->maxwidth)) { - $this->jsonEndpoint.= '&maxwidth=' . $this->maxwidth; - $this->xmlEndpoint.= '&maxwidth=' . $this->maxwidth; + $this->jsonEndpoint.= '&maxwidth=' . $this->maxwidth; + $this->xmlEndpoint.= '&maxwidth=' . $this->maxwidth; } if (!empty($this->maxheight)) { $this->jsonEndpoint.= '&maxwidth=' . $this->maxheight; @@ -42,22 +44,8 @@ class OEmbedProvider extends EmbedProvider{ 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; + $allow_curl = defined('OEMBED_USE_CURL') && OEMBED_USE_CURL && defined('CURLOPT_URL'); + return CurlFetcher::file_get_contents($fileurl, $allow_curl); } private function provideXML($url){ return $this->file_get_contents(preg_replace("/\{url\}/",urlencode($url),$this->xmlEndpoint)); diff --git a/serendipity_event_oembed/oembed/providers.xml b/serendipity_event_oembed/oembed/providers.xml index c1d8f217..2f8a73cf 100644 --- a/serendipity_event_oembed/oembed/providers.xml +++ b/serendipity_event_oembed/oembed/providers.xml @@ -128,12 +128,15 @@ + + Twitpic (via noembed.com) @@ -192,4 +195,11 @@ http://((www.)?audio)?boo.fm/boos/* http://oohembed.com/oohembed/ + + Wikipedia (via oohembed.com) + + https?://*.wikipedia.org/wiki/* + http://oohembed.com/oohembed/ + +