From 5056eaa83cd6ab48ea7808777843a7fe51b84378 Mon Sep 17 00:00:00 2001 From: Grischa Brockhaus Date: Wed, 14 Dec 2011 11:51:24 +0100 Subject: [PATCH 01/14] Fixed some broken HTML / XML --- serendipity_event_findmore/plugin_findmore.tpl | 2 +- serendipity_event_microformats/UTF-8/documentation_cs.html | 2 +- serendipity_event_microformats/UTF-8/documentation_cz.html | 2 +- serendipity_event_microformats/documentation_cs.html | 2 +- serendipity_event_microformats/documentation_cz.html | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/serendipity_event_findmore/plugin_findmore.tpl b/serendipity_event_findmore/plugin_findmore.tpl index e05cb5eb..30aa6b51 100644 --- a/serendipity_event_findmore/plugin_findmore.tpl +++ b/serendipity_event_findmore/plugin_findmore.tpl @@ -123,7 +123,7 @@ {if NOT $findmore_disabled_services.plusone}
- +
{/if} diff --git a/serendipity_event_microformats/UTF-8/documentation_cs.html b/serendipity_event_microformats/UTF-8/documentation_cs.html index 9c477a28..9c499efa 100644 --- a/serendipity_event_microformats/UTF-8/documentation_cs.html +++ b/serendipity_event_microformats/UTF-8/documentation_cs.html @@ -65,4 +65,4 @@ - diff --git a/serendipity_event_microformats/UTF-8/documentation_cz.html b/serendipity_event_microformats/UTF-8/documentation_cz.html index 9c477a28..9c499efa 100644 --- a/serendipity_event_microformats/UTF-8/documentation_cz.html +++ b/serendipity_event_microformats/UTF-8/documentation_cz.html @@ -65,4 +65,4 @@ - diff --git a/serendipity_event_microformats/documentation_cs.html b/serendipity_event_microformats/documentation_cs.html index b098fd01..c4bc7de5 100644 --- a/serendipity_event_microformats/documentation_cs.html +++ b/serendipity_event_microformats/documentation_cs.html @@ -65,4 +65,4 @@ - diff --git a/serendipity_event_microformats/documentation_cz.html b/serendipity_event_microformats/documentation_cz.html index a9185a88..d3c56b1f 100644 --- a/serendipity_event_microformats/documentation_cz.html +++ b/serendipity_event_microformats/documentation_cz.html @@ -65,4 +65,4 @@ - From 009b8e1caacd76fc131c26061cd2c85ee015f859 Mon Sep 17 00:00:00 2001 From: Grischa Brockhaus Date: Wed, 14 Dec 2011 11:52:02 +0100 Subject: [PATCH 02/14] Ignoring some stuff --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..8957054e --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/.settings +/.project +/.buildpath +/zipplugin.sh From 37caa6260ae69fcc3935e9bbdfbadd2d84b8e6b7 Mon Sep 17 00:00:00 2001 From: Grischa Brockhaus Date: Wed, 14 Dec 2011 12:18:36 +0100 Subject: [PATCH 03/14] OEmbed Checkin Into Branch --- serendipity_event_oembed/lang_de.inc.php | 9 +++ serendipity_event_oembed/lang_en.inc.php | 10 +++ .../oembed/EmbedProvider.class.php | 12 +++ .../oembed/Exception404.class.php | 3 + .../oembed/LinkEmbed.class.php | 4 + .../oembed/OEmbed.class.php | 23 ++++++ .../oembed/OEmbedProvider.class.php | 74 +++++++++++++++++ .../oembed/PhotoEmbed.class.php | 6 ++ .../oembed/ProviderManager.class.php | 45 +++++++++++ .../oembed/RichEmbed.class.php | 6 ++ .../oembed/VideoEmbed.class.php | 7 ++ .../oembed/YouTubeProvider.class.php | 80 +++++++++++++++++++ serendipity_event_oembed/oembed/config.php | 10 +++ serendipity_event_oembed/oembed/providers.xml | 24 ++++++ .../oembed/tests/OEmbedTest.php | 24 ++++++ .../oembed/tests/ProviderManagerTest.php | 24 ++++++ .../oembed/tests/ProviderXmlTest.php | 8 ++ .../oembed/tests/YouTubeTest.php | 12 +++ .../serendipity_event_oembed.php | 66 +++++++++++++++ 19 files changed, 447 insertions(+) create mode 100755 serendipity_event_oembed/lang_de.inc.php create mode 100755 serendipity_event_oembed/lang_en.inc.php create mode 100644 serendipity_event_oembed/oembed/EmbedProvider.class.php create mode 100644 serendipity_event_oembed/oembed/Exception404.class.php create mode 100644 serendipity_event_oembed/oembed/LinkEmbed.class.php create mode 100644 serendipity_event_oembed/oembed/OEmbed.class.php create mode 100644 serendipity_event_oembed/oembed/OEmbedProvider.class.php create mode 100644 serendipity_event_oembed/oembed/PhotoEmbed.class.php create mode 100644 serendipity_event_oembed/oembed/ProviderManager.class.php create mode 100644 serendipity_event_oembed/oembed/RichEmbed.class.php create mode 100644 serendipity_event_oembed/oembed/VideoEmbed.class.php create mode 100644 serendipity_event_oembed/oembed/YouTubeProvider.class.php create mode 100644 serendipity_event_oembed/oembed/config.php create mode 100644 serendipity_event_oembed/oembed/providers.xml create mode 100644 serendipity_event_oembed/oembed/tests/OEmbedTest.php create mode 100644 serendipity_event_oembed/oembed/tests/ProviderManagerTest.php create mode 100644 serendipity_event_oembed/oembed/tests/ProviderXmlTest.php create mode 100644 serendipity_event_oembed/oembed/tests/YouTubeTest.php create mode 100644 serendipity_event_oembed/serendipity_event_oembed.php diff --git a/serendipity_event_oembed/lang_de.inc.php b/serendipity_event_oembed/lang_de.inc.php new file mode 100755 index 00000000..3896913c --- /dev/null +++ b/serendipity_event_oembed/lang_de.inc.php @@ -0,0 +1,9 @@ + + * EN-Revision: Revision of lang_en.inc.php + */ + +@define('PLUGIN_EVENT_OEMBED_NAME', 'oEmbed Plugin'); diff --git a/serendipity_event_oembed/lang_en.inc.php b/serendipity_event_oembed/lang_en.inc.php new file mode 100755 index 00000000..e76e12a8 --- /dev/null +++ b/serendipity_event_oembed/lang_en.inc.php @@ -0,0 +1,10 @@ + + * EN-Revision: Revision of lang_en.inc.php + */ + +@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.'); diff --git a/serendipity_event_oembed/oembed/EmbedProvider.class.php b/serendipity_event_oembed/oembed/EmbedProvider.class.php new file mode 100644 index 00000000..ad94760c --- /dev/null +++ b/serendipity_event_oembed/oembed/EmbedProvider.class.php @@ -0,0 +1,12 @@ +url = $url; + $this->endpoint = $endpoint; + } +} diff --git a/serendipity_event_oembed/oembed/Exception404.class.php b/serendipity_event_oembed/oembed/Exception404.class.php new file mode 100644 index 00000000..4e93adf4 --- /dev/null +++ b/serendipity_event_oembed/oembed/Exception404.class.php @@ -0,0 +1,3 @@ +$value){ + $this->$key=(string)$value; + } + } +} + diff --git a/serendipity_event_oembed/oembed/OEmbedProvider.class.php b/serendipity_event_oembed/oembed/OEmbedProvider.class.php new file mode 100644 index 00000000..94345e82 --- /dev/null +++ b/serendipity_event_oembed/oembed/OEmbedProvider.class.php @@ -0,0 +1,74 @@ +urlRegExp=preg_replace(array("/\*/","/\//","/\.\*\./"),array(".*","\/",".*"),$url); + $this->urlRegExp="/".$this->urlRegExp."/"; + if (preg_match("/\{format\}/",$endpoint)){ + $this->jsonEndpoint=preg_replace("/\{format\}/","json",$endpoint); + $this->jsonEndpoint.="?url={url}"; + $this->xmlEndpoint=preg_replace("/\{format\}/","xml",$endpoint); + $this->xmlEndpoint.="?url={url}"; + } else { + $this->jsonEndpoint=$endpoint."?url={url}&format=json"; + $this->xmlEndpoint=$endpoint."?url={url}&format=xml"; + } + } + + public function getUrlRegExp(){ return $this->urlRegExp; } + public function getJsonEndpoint(){ return $this->jsonEndpoint; } + public function getXmlEndpoint(){ return $this->xmlEndpoint; } + + public function match($url){ + return preg_match($this->urlRegExp,$url); + } + private function provideXML($url){ + return file_get_contents(preg_replace("/\{url\}/",urlencode($url),$this->xmlEndpoint)); + } + private function getTypeObj($type){ + switch($type){ + case "photo": + return new PhotoEmbed(); + break; + case "video": + return new VideoEmbed(); + break; + case "link": + return new LinkEmbed(); + break; + case "rich": + return new RichEmbed(); + break; + default: + return new OEmbed(); + } + } + private function provideObject($url){ + $xml=simplexml_load_string($this->provideXML($url)); + //TODO $xml->type alapjan assigner + $obj = $this->getTypeObj((string)$xml->type); + $obj->cloneObj($xml); + $obj->resource_url=$url; + return $obj; + } + private function provideSerialized($url){ + $serialized=serialize($this->provideObject($url)); + return $serialized; + } + public function provide($url,$format="json"){ + if($format=="xml"){ + return $this->provideXML($url); + } else if ($format=="object"){ + return $this->provideObject($url); + } else if ($format=="serialized"){ + return $this->provideSerialized($url); + } else { + return file_get_contents(preg_replace("/\{url\}/",urlencode($url),$this->jsonEndpoint)); + } + } + public function register(){} +} + diff --git a/serendipity_event_oembed/oembed/PhotoEmbed.class.php b/serendipity_event_oembed/oembed/PhotoEmbed.class.php new file mode 100644 index 00000000..da482ff5 --- /dev/null +++ b/serendipity_event_oembed/oembed/PhotoEmbed.class.php @@ -0,0 +1,6 @@ +providers=array(); + $xml = simplexml_load_file(PROVIDER_XML);// PROVIDER_XML comes from config.php + foreach($xml->provider as $provider){ + if(!isset($provider->class) && isset($provider->endpoint)){ + $this->register(new OEmbedProvider($provider->url,$provider->endpoint)); + } else { + $classname="".$provider->class; // force to be string :) + $reflection = new ReflectionClass($classname); + $this->register($reflection->newInstance($provider));//so we could pass config vars + } + } + } + static function getInstance(){ + if(!isset($_instance) || $_instance==null){ + $_instance = new ProviderManager(); + } + return $_instance; + } + public function register($provider){ + $this->providers[]=$provider; + } + public function provide($url,$format){ + foreach ($this->providers as $provider){ + if ($provider->match($url)){ + return $provider->provide($url,$format); + } + } + return null; + } +} diff --git a/serendipity_event_oembed/oembed/RichEmbed.class.php b/serendipity_event_oembed/oembed/RichEmbed.class.php new file mode 100644 index 00000000..18274118 --- /dev/null +++ b/serendipity_event_oembed/oembed/RichEmbed.class.php @@ -0,0 +1,6 @@ +type='video'; + $myEmbed->version='1.0'; + $myEmbed->provider_name="Youtube"; + $myEmbed->provider_url="http://youtube.com"; + $myEmbed->resource_url=$url; + $xml = new DOMDocument; + if(@($xml->load('http://gdata.youtube.com/feeds/api/videos/'.$video_id))) { + @$guid = $xml->getElementsByTagName("guid")->item(0)->nodeValue; + $link = str_replace("http://www.youtube.com/watch?v=","http://bergengocia.net/indavideobombyoutubemashup/view.php?id=",$guid); + $myEmbed->title =$xml->getElementsByTagName("title")->item(0)->nodeValue; + $myEmbed->description =$xml->getElementsByTagNameNS("*","description")->item(0)->nodeValue; + $myEmbed->author_name =$xml->getElementsByTagName("author")->item(0)->getElementsByTagName("name")->item(0)->nodeValue; + $myEmbed->author_url =$xml->getElementsByTagName("author")->item(0)->getElementsByTagName("uri")->item(0)->nodeValue; + $myEmbed->thumbnail_url =$xml->getElementsByTagNameNS("*","thumbnail")->item(0)->getAttribute("url"); + $myEmbed->thumbnail_width =$xml->getElementsByTagNameNS("*","thumbnail")->item(0)->getAttribute("width"); + $myEmbed->thumbnail_height =$xml->getElementsByTagNameNS("*","thumbnail")->item(0)->getAttribute("height"); + $med_content_url=$xml->getElementsByTagNameNS("http://search.yahoo.com/mrss/","content")->item(0)->getAttribute("url"); + $myEmbed->html= + ''."\n". + ' '."\n". + ' '."\n". + ' '."\n". + ''; // according to http://code.google.com/apis/youtube/developers_guide_protocol.html#Displaying_information_about_a_video + $myEmbed->width="425"; + $myEmbed->height="350"; // same as in the html + //$myEmbed->duration=$xml->getElementsByTagNameNS($xml->lookupNamespaceURI("*"),"content")->item(0)->getAttribute("duration"); + //$time = floor($duration / 60) . ":" . $duration % 60; + return $myEmbed; + } else throw new Exception404("xxx"); + } + private function provideXML($url){ + $string=""; + foreach($this->getEmbed($url) as $key=>$value){ + if(isset($value)&& $value!="") $string.=" <".$key.">".$value."\n"; + } + $string="\n".$string.""; + return $string; + } + private function provideObject($url){ + return $this->getEmbed($url); + } + private function provideJSON($url){ + return json_encode($this->getEmbed($url)); + } + private function provideSerialized($url){ + return serialize($this->getEmbed($url)); + } + public function provide($url,$format="json"){ + if($format=="xml"){ + return $this->provideXML($url); + } else if ($format=="object"){ + return $this->provideObject($url); + } else if ($format=="serialized"){ + return $this->provideSerialized($url); + } else { + return $this->provideJSON($url);; + } + } + + + public function __construct($config){ + parent::__construct("http://youtube.com",""); + } +} diff --git a/serendipity_event_oembed/oembed/config.php b/serendipity_event_oembed/oembed/config.php new file mode 100644 index 00000000..0912723e --- /dev/null +++ b/serendipity_event_oembed/oembed/config.php @@ -0,0 +1,10 @@ + diff --git a/serendipity_event_oembed/oembed/providers.xml b/serendipity_event_oembed/oembed/providers.xml new file mode 100644 index 00000000..f4ccfcca --- /dev/null +++ b/serendipity_event_oembed/oembed/providers.xml @@ -0,0 +1,24 @@ + + + + http://*.flickr.com/* + http://www.flickr.com/services/oembed/ + + + http://*.pownce.com/* + http://api.pownce.com/2.1/oembed.{format} + + + https://*.twitter.com/* + https://api.twitter.com/1/statuses/oembed.{format} + + + http://*.vimeo.com/* + http://vimeo.com/api/oembed.{format} + + + http://*.youtube.*/* + YouTubeProvider + + + diff --git a/serendipity_event_oembed/oembed/tests/OEmbedTest.php b/serendipity_event_oembed/oembed/tests/OEmbedTest.php new file mode 100644 index 00000000..40f0ab61 --- /dev/null +++ b/serendipity_event_oembed/oembed/tests/OEmbedTest.php @@ -0,0 +1,24 @@ +provider as $provider){ + // $x = new OEmbedProvider("http://*.flickr.com/*","http://www.flickr.com/services/oembed/"); + $x = new OEmbedProvider($provider->url,$provider->endpoint); + echo $x->url.":\n"; + if ($x->match("http://www.flickr.com/photos/bees/2341623661/")){ + print_r($x->provide("http://www.flickr.com/photos/bees/2341623661/","object")); + } + echo " ".$x->getUrlRegExp()."\n"; + echo " ".$x->getJsonEndpoint()."\n"; + echo " ".$x->getXmlEndpoint()."\n"; +} diff --git a/serendipity_event_oembed/oembed/tests/ProviderManagerTest.php b/serendipity_event_oembed/oembed/tests/ProviderManagerTest.php new file mode 100644 index 00000000..9ad199cd --- /dev/null +++ b/serendipity_event_oembed/oembed/tests/ProviderManagerTest.php @@ -0,0 +1,24 @@ +provide($url,"object"); + if (!empty($obj)) print_r($obj); +} + +$manager = ProviderManager::getInstance(); +// Youtube long link +test($manager,"http://www.youtube.com/watch?v=8UVNT4wvIGY"); + // Youtube Kurze URL +test($manager,"http://youtu.be/8UVNT4wvIGY"); +// Twitter +test($manager,"https://twitter.com/#!/tagesschau/status/146562892454572032"); +// flickr +test($manager,"http://www.flickr.com/photos/gbrockhaus/2052855443/in/set-72157603214268227/"); +// vimeo +test($manager,"http://vimeo.com/33510073"); + diff --git a/serendipity_event_oembed/oembed/tests/ProviderXmlTest.php b/serendipity_event_oembed/oembed/tests/ProviderXmlTest.php new file mode 100644 index 00000000..64aaaffc --- /dev/null +++ b/serendipity_event_oembed/oembed/tests/ProviderXmlTest.php @@ -0,0 +1,8 @@ +provider as $provider){ + echo $provider->url; + echo $provider->endpoint; +} + diff --git a/serendipity_event_oembed/oembed/tests/YouTubeTest.php b/serendipity_event_oembed/oembed/tests/YouTubeTest.php new file mode 100644 index 00000000..654c8315 --- /dev/null +++ b/serendipity_event_oembed/oembed/tests/YouTubeTest.php @@ -0,0 +1,12 @@ +provide("","object"); + //print_r($obj); + print_r($obj->html); +} + +testYoutubeProvider(); + diff --git a/serendipity_event_oembed/serendipity_event_oembed.php b/serendipity_event_oembed/serendipity_event_oembed.php new file mode 100644 index 00000000..ac8b8c24 --- /dev/null +++ b/serendipity_event_oembed/serendipity_event_oembed.php @@ -0,0 +1,66 @@ +add('name', PLUGIN_EVENT_OEMBED_NAME); + $propbag->add('description', PLUGIN_EVENT_OEMBED_DESC); + $propbag->add('stackable', false); + $propbag->add('author', 'Grischa Brockhaus'); + $propbag->add('version', '0.01'); + $propbag->add('requirements', array( + 'serendipity' => '0.8', + 'smarty' => '2.6.7', + 'php' => '5.1.0' + )); + $propbag->add('groups', array('FRONTEND_EXTERNAL_SERVICES')); + $propbag->add('event_hooks', array( + 'backend_publish' => true, // An entry was puplished (was draft before or saved from the scratch). + )); + + //$propbag->add('configuration', array('max_items','ext_vis_stat','stat_all','banned_bots')); + } + + function introspect_config_item($name, &$propbag) + { + } + + function event_hook($event, &$bag, &$eventData) { + global $serendipity; + + $hooks = &$bag->get('event_hooks'); + + if (isset($hooks[$event])) { + switch($event) { + case 'backend_publish': + $this->update_entry($eventData); + return true; + } + } + + return true; + + } + + function update_entry($eventData) { + + } + +} \ No newline at end of file From 27d0fadcf1b28fdef2701c690d548051ebef85b2 Mon Sep 17 00:00:00 2001 From: Grischa Brockhaus Date: Wed, 14 Dec 2011 15:29:47 +0100 Subject: [PATCH 04/14] Twitter embed with (tweet link) is working. --- .../oembed/LinkEmbed.class.php | 2 +- .../oembed/PhotoEmbed.class.php | 2 +- .../oembed/ProviderManager.class.php | 12 +--- .../oembed/RichEmbed.class.php | 2 +- .../oembed/VideoEmbed.class.php | 2 +- serendipity_event_oembed/oembed/config.php | 33 ++++++++--- .../oembed/tests/ProviderManagerTest.php | 2 +- .../serendipity_event_oembed.php | 58 +++++++++++++++++-- 8 files changed, 83 insertions(+), 30 deletions(-) diff --git a/serendipity_event_oembed/oembed/LinkEmbed.class.php b/serendipity_event_oembed/oembed/LinkEmbed.class.php index ddb7a2d6..6a2ca33b 100644 --- a/serendipity_event_oembed/oembed/LinkEmbed.class.php +++ b/serendipity_event_oembed/oembed/LinkEmbed.class.php @@ -1,4 +1,4 @@ -providers=array(); - $xml = simplexml_load_file(PROVIDER_XML);// PROVIDER_XML comes from config.php + $xml = simplexml_load_file(PLUGIN_OEMBED_PROVIDER_XML_FILE);// PROVIDER_XML comes from config.php foreach($xml->provider as $provider){ if(!isset($provider->class) && isset($provider->endpoint)){ $this->register(new OEmbedProvider($provider->url,$provider->endpoint)); diff --git a/serendipity_event_oembed/oembed/RichEmbed.class.php b/serendipity_event_oembed/oembed/RichEmbed.class.php index 18274118..cf49705b 100644 --- a/serendipity_event_oembed/oembed/RichEmbed.class.php +++ b/serendipity_event_oembed/oembed/RichEmbed.class.php @@ -1,4 +1,4 @@ - + +// Include all class files +/* +$oembed_config_class_wildcard = dirname(__FILE__) . "/*class.php"; +foreach (glob($oembed_config_class_wildcard) as $filename) +{ + //echo "$filename
\n"; + @include $filename; +} +*/ +require_once dirname(__FILE__) . '/' . 'Exception404.class.php'; +require_once dirname(__FILE__) . '/' . 'OEmbed.class.php'; +require_once dirname(__FILE__) . '/' . 'LinkEmbed.class.php'; +require_once dirname(__FILE__) . '/' . 'PhotoEmbed.class.php'; +require_once dirname(__FILE__) . '/' . 'RichEmbed.class.php'; +require_once dirname(__FILE__) . '/' . 'VideoEmbed.class.php'; + +require_once dirname(__FILE__) . '/' . 'EmbedProvider.class.php'; +require_once dirname(__FILE__) . '/' . 'OEmbedProvider.class.php'; +require_once dirname(__FILE__) . '/' . 'YouTubeProvider.class.php'; + +require_once dirname(__FILE__) . '/' . 'ProviderManager.class.php'; + diff --git a/serendipity_event_oembed/oembed/tests/ProviderManagerTest.php b/serendipity_event_oembed/oembed/tests/ProviderManagerTest.php index 9ad199cd..e9001780 100644 --- a/serendipity_event_oembed/oembed/tests/ProviderManagerTest.php +++ b/serendipity_event_oembed/oembed/tests/ProviderManagerTest.php @@ -1,5 +1,5 @@ add('groups', array('FRONTEND_EXTERNAL_SERVICES')); $propbag->add('event_hooks', array( - 'backend_publish' => true, // An entry was puplished (was draft before or saved from the scratch). +// 'backend_publish' => true, // An entry was puplished (was draft before or saved from the scratch). +// 'backend_save' => true, // An entry was saved. + 'frontend_display' => true, )); //$propbag->add('configuration', array('max_items','ext_vis_stat','stat_all','banned_bots')); @@ -44,13 +48,29 @@ class serendipity_event_oembed extends serendipity_event function event_hook($event, &$bag, &$eventData) { global $serendipity; - + + static $simplePatterns = null; + + if ($simplePatterns==null) { + $simplePatterns = array( + //'simpleTweet' => '@\(tweet\s+(\S*)\)@Usi', + 'simpleTweet' => '@\(tweet\s+(.*)\)@Usi', + ); + } + $hooks = &$bag->get('event_hooks'); if (isset($hooks[$event])) { switch($event) { + case 'frontend_display': case 'backend_publish': - $this->update_entry($eventData); + case 'backend_save': + if (!isset($eventData['body']) && !isset($eventData['extended'])) { + // Do not use for user comments, html nuggets, static pages etc. + return false; + break; + } + $this->update_entry($eventData, $simplePatterns); return true; } } @@ -59,8 +79,34 @@ class serendipity_event_oembed extends serendipity_event } - function update_entry($eventData) { - + function update_entry(&$eventData, &$patterns) { + if (!empty($eventData['body'])) { + $eventData['body'] = preg_replace_callback( + $patterns['simpleTweet'], + array( $this, "oembedRewriteCallback"), + $eventData['body']); + } + /* + if (!empty($eventData['extended'])) { + $eventData['extended'] = preg_replace_callback( + $patterns['simpleTweet'], + array( $this, "oembedRewriteCallback"), + $eventData['extended']); + } + */ + } + + function oembedRewriteCallback($match) { + $url = $match[1]; + //$url = "http://www.flickr.com/photos/gbrockhaus/2052855443/in/set-72157603214268227/"; + //print "
callback
"; + print_r($match[1]); + $manager = ProviderManager::getInstance(); + $obj=$manager->provide($url,"object"); + if (!empty($obj)) { + if ($obj->type == 'rich') return $obj->html; + } + return $match[0]; } } \ No newline at end of file From f11e8b95316aff39d7cbe407400f79fa66d4c233 Mon Sep 17 00:00:00 2001 From: Grischa Brockhaus Date: Thu, 15 Dec 2011 01:42:11 +0100 Subject: [PATCH 05/14] =?UTF-8?q?Erstes=20wirklich=20funktionsf=C3=A4hige?= =?UTF-8?q?=20OEmbed=20Plugin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- serendipity_event_oembed/OEmbedDatabase.php | 80 +++++++++++++++++++ .../oembed/OEmbedProvider.class.php | 4 + serendipity_event_oembed/oembed/config.php | 4 +- serendipity_event_oembed/oembed/providers.xml | 22 +++++ .../oembed/tests/ProviderManagerTest.php | 8 +- .../serendipity_event_oembed.php | 61 ++++++++++---- 6 files changed, 159 insertions(+), 20 deletions(-) create mode 100644 serendipity_event_oembed/OEmbedDatabase.php diff --git a/serendipity_event_oembed/OEmbedDatabase.php b/serendipity_event_oembed/OEmbedDatabase.php new file mode 100644 index 00000000..685e05ed --- /dev/null +++ b/serendipity_event_oembed/OEmbedDatabase.php @@ -0,0 +1,80 @@ +type; + $oeobj = serialize($oembed); + $query = "insert into {$serendipity['dbPrefix']}" . PLUGIN_OEMBED_DATABASEVNAME . " (urlmd5,url,oetype,oeobj) VALUES ('$urlmd5','$url','$oetype','$oeobj')"; + return serendipity_db_query($query); + } + + function load_oembed($url) { + global $serendipity; + if (empty($url)) return null; + + $urlmd5 = md5($url); + $query = "select oeobj from {$serendipity['dbPrefix']}" . PLUGIN_OEMBED_DATABASEVNAME . " where urlmd5='$urlmd5'"; + + $rows = serendipity_db_query($query); + if (!is_array($rows)) { // fresh search + return null; + } + else { + $oeobj = null; + foreach ($rows as $row) { + $oeobj = $row['oeobj']; + if (!empty($oeobj)) break; + } + if (!empty($oeobj)) { + return unserialize($oeobj); + } + + } + return null; + } + + function install(&$obj) { + global $serendipity; + + if (!OEmbedDatabase::table_created(PLUGIN_OEMBED_DATABASEVNAME)) { + $md5test = md5("test"); + $md5len = strlen($md5test); + $q = "create table {$serendipity['dbPrefix']}" . PLUGIN_OEMBED_DATABASEVNAME. " (" . + "urlmd5 char($md5len) not null, " . + "url varchar(3000) not null, " . + "oetype varchar(20) not null, " . + "oeobj text not null, " . + "primary key (urlmd5)" . + ")"; + + $result = serendipity_db_schema_import($q); + + if ($result !== true) { + return; + } + } + } + + + function table_created($table = PLUGIN_OEMBED_DATABASEVNAME) { + global $serendipity; + + $q = "select count(*) from {$serendipity['dbPrefix']}" . $table; + $row = serendipity_db_query($q, true, 'num'); + + if (!is_numeric($row[0])) { // if the response we got back was an SQL error.. :P + return false; + } else { + return true; + } + } + +} \ 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 94345e82..9942c250 100644 --- a/serendipity_event_oembed/oembed/OEmbedProvider.class.php +++ b/serendipity_event_oembed/oembed/OEmbedProvider.class.php @@ -48,6 +48,10 @@ class OEmbedProvider extends EmbedProvider{ } private function provideObject($url){ $xml=simplexml_load_string($this->provideXML($url)); + if (empty($xml)) { + $data=$this->provide($url); + if (!empty($$data)) $xml = json_decode($data); + } //TODO $xml->type alapjan assigner $obj = $this->getTypeObj((string)$xml->type); $obj->cloneObj($xml); diff --git a/serendipity_event_oembed/oembed/config.php b/serendipity_event_oembed/oembed/config.php index 8046154d..7ddc4bf6 100644 --- a/serendipity_event_oembed/oembed/config.php +++ b/serendipity_event_oembed/oembed/config.php @@ -21,7 +21,9 @@ require_once dirname(__FILE__) . '/' . 'VideoEmbed.class.php'; require_once dirname(__FILE__) . '/' . 'EmbedProvider.class.php'; require_once dirname(__FILE__) . '/' . 'OEmbedProvider.class.php'; -require_once dirname(__FILE__) . '/' . 'YouTubeProvider.class.php'; + +// Will be loaded via generic oembedprovider! +//require_once dirname(__FILE__) . '/' . 'YouTubeProvider.class.php'; require_once dirname(__FILE__) . '/' . 'ProviderManager.class.php'; diff --git a/serendipity_event_oembed/oembed/providers.xml b/serendipity_event_oembed/oembed/providers.xml index f4ccfcca..d2e6af9e 100644 --- a/serendipity_event_oembed/oembed/providers.xml +++ b/serendipity_event_oembed/oembed/providers.xml @@ -4,10 +4,12 @@ http://*.flickr.com/* http://www.flickr.com/services/oembed/ + https://*.twitter.com/* https://api.twitter.com/1/statuses/oembed.{format} @@ -16,9 +18,29 @@ http://*.vimeo.com/* http://vimeo.com/api/oembed.{format} + + + http://*.youtube.com/watch?v=* + http://www.youtube.com/oembed + + + http://*.youtu.be/* + http://www.youtube.com/oembed + + + + http://official.fm/tracks/* + http://official.fm/services/oembed.{format} + + + + http://blip.tv/* + http://blip.tv/oembed + diff --git a/serendipity_event_oembed/oembed/tests/ProviderManagerTest.php b/serendipity_event_oembed/oembed/tests/ProviderManagerTest.php index e9001780..0ee1ae52 100644 --- a/serendipity_event_oembed/oembed/tests/ProviderManagerTest.php +++ b/serendipity_event_oembed/oembed/tests/ProviderManagerTest.php @@ -12,13 +12,13 @@ function test($manager, $url) { $manager = ProviderManager::getInstance(); // Youtube long link -test($manager,"http://www.youtube.com/watch?v=8UVNT4wvIGY"); +//test($manager,"http://www.youtube.com/watch?v=8UVNT4wvIGY"); // Youtube Kurze URL -test($manager,"http://youtu.be/8UVNT4wvIGY"); +//test($manager,"http://youtu.be/8UVNT4wvIGY"); // Twitter -test($manager,"https://twitter.com/#!/tagesschau/status/146562892454572032"); +//test($manager,"https://twitter.com/#!/tagesschau/status/146562892454572032"); // flickr test($manager,"http://www.flickr.com/photos/gbrockhaus/2052855443/in/set-72157603214268227/"); // vimeo -test($manager,"http://vimeo.com/33510073"); +//test($manager,"http://vimeo.com/33510073"); diff --git a/serendipity_event_oembed/serendipity_event_oembed.php b/serendipity_event_oembed/serendipity_event_oembed.php index f104c8c8..443b1f4f 100644 --- a/serendipity_event_oembed/serendipity_event_oembed.php +++ b/serendipity_event_oembed/serendipity_event_oembed.php @@ -11,8 +11,9 @@ if (file_exists($probelang)) { } include dirname(__FILE__) . '/lang_en.inc.php'; -include_once dirname(__FILE__) . '/oembed/config.php'; // autoload oembed classes - +require_once dirname(__FILE__) . '/oembed/config.php'; // autoload oembed classes +require_once dirname(__FILE__) . '/OEmbedDatabase.php'; + class serendipity_event_oembed extends serendipity_event { @@ -39,11 +40,17 @@ class serendipity_event_oembed extends serendipity_event 'frontend_display' => true, )); - //$propbag->add('configuration', array('max_items','ext_vis_stat','stat_all','banned_bots')); + $propbag->add('configuration', array('info')); } function introspect_config_item($name, &$propbag) { + switch($name) { + case 'info': + $propbag->add('type', 'content'); + $propbag->add('default', "Info"); + break; + } } function event_hook($event, &$bag, &$eventData) { @@ -54,7 +61,7 @@ class serendipity_event_oembed extends serendipity_event if ($simplePatterns==null) { $simplePatterns = array( //'simpleTweet' => '@\(tweet\s+(\S*)\)@Usi', - 'simpleTweet' => '@\(tweet\s+(.*)\)@Usi', + 'simpleTweet' => '@\[(?:embed|tweet)\s+(.*)\]@Usi', ); } @@ -86,27 +93,51 @@ class serendipity_event_oembed extends serendipity_event array( $this, "oembedRewriteCallback"), $eventData['body']); } - /* if (!empty($eventData['extended'])) { - $eventData['extended'] = preg_replace_callback( + $eventData['body'] = preg_replace_callback( $patterns['simpleTweet'], array( $this, "oembedRewriteCallback"), $eventData['extended']); } - */ } function oembedRewriteCallback($match) { $url = $match[1]; - //$url = "http://www.flickr.com/photos/gbrockhaus/2052855443/in/set-72157603214268227/"; - //print "
callback
"; - print_r($match[1]); - $manager = ProviderManager::getInstance(); - $obj=$manager->provide($url,"object"); + $obj = OEmbedDatabase::load_oembed($url); + $html = ''; + if (empty($obj)) { + $manager = ProviderManager::getInstance(); + try { + $obj=$manager->provide($url,"object"); + if (!empty($obj)) { + OEmbedDatabase::save_oembed($url,$obj); + } + } + catch (ErrorException $e) { + // Timeout in most cases + //return $e; + } + } if (!empty($obj)) { - if ($obj->type == 'rich') return $obj->html; + if ($obj->type == 'rich') + $html = $obj->html; + elseif ($obj->type == 'video') + $html = $obj->html; + elseif ($obj->type == 'photo') { + $html = '' .$obj->title . ''; + } } - return $match[0]; + else { + $html = '' . $match[1] . ''; + } + return '' . $html . ''; } - + + function cleanup() { + OEmbedDatabase::install($this); + } + function install() { + OEmbedDatabase::install($this); + } + } \ No newline at end of file From 56854a6a5310daefa060e302eb855402b3ac27cc Mon Sep 17 00:00:00 2001 From: Grischa Brockhaus Date: Thu, 15 Dec 2011 03:13:15 +0100 Subject: [PATCH 06/14] OEmbed: DB save corrected, JSON Only Providers supported. --- serendipity_event_oembed/OEmbedDatabase.php | 15 ++++--- .../oembed/OEmbed.class.php | 40 +++++++++---------- .../oembed/OEmbedProvider.class.php | 14 +++++-- .../oembed/ProviderManager.class.php | 3 +- serendipity_event_oembed/oembed/providers.xml | 5 ++- .../serendipity_event_oembed.php | 7 ++-- 6 files changed, 46 insertions(+), 38 deletions(-) diff --git a/serendipity_event_oembed/OEmbedDatabase.php b/serendipity_event_oembed/OEmbedDatabase.php index 685e05ed..98e86571 100644 --- a/serendipity_event_oembed/OEmbedDatabase.php +++ b/serendipity_event_oembed/OEmbedDatabase.php @@ -5,15 +5,14 @@ class OEmbedDatabase { function save_oembed($url, $oembed) { - global $serendipity; + if (empty($url) || !isset($oembed)) return false; + $save = array(); + $save['urlmd5'] = md5($url); + $save['url'] = $url; + $save['oetype'] = $oembed->type; + $save['oeobj'] = serialize($oembed); + return serendipity_db_insert( PLUGIN_OEMBED_DATABASEVNAME, $save ); - if (empty($url) || empty($oembed)) return false; - - $urlmd5 = md5($url); - $oetype = $oembed->type; - $oeobj = serialize($oembed); - $query = "insert into {$serendipity['dbPrefix']}" . PLUGIN_OEMBED_DATABASEVNAME . " (urlmd5,url,oetype,oeobj) VALUES ('$urlmd5','$url','$oetype','$oeobj')"; - return serendipity_db_query($query); } function load_oembed($url) { diff --git a/serendipity_event_oembed/oembed/OEmbed.class.php b/serendipity_event_oembed/oembed/OEmbed.class.php index d0ffcdf9..4abfdeef 100644 --- a/serendipity_event_oembed/oembed/OEmbed.class.php +++ b/serendipity_event_oembed/oembed/OEmbed.class.php @@ -1,23 +1,23 @@ $value){ - $this->$key=(string)$value; - } - } +class OEmbed{ + public $type; + public $version; + public $title; + public $author_name; + public $author_url; + public $provider_name; + public $provider_url; + public $cache_age; + public $description; // added by me, not part of OEmbed + public $resource_url; // added by me, not part of OEmbed + public $thumbnail_url; + public $thumbnail_width; + public $thumbnail_height; + + public function cloneObj($object){ + foreach($object as $key=>$value){ + $this->$key=(string)$value; + } + } } diff --git a/serendipity_event_oembed/oembed/OEmbedProvider.class.php b/serendipity_event_oembed/oembed/OEmbedProvider.class.php index 9942c250..3bef5ed6 100644 --- a/serendipity_event_oembed/oembed/OEmbedProvider.class.php +++ b/serendipity_event_oembed/oembed/OEmbedProvider.class.php @@ -3,8 +3,12 @@ class OEmbedProvider extends EmbedProvider{ private $urlRegExp; private $jsonEndpoint; private $xmlEndpoint; - public function __construct($url,$endpoint){ + + private $onlyJson = false; + + public function __construct($url,$endpoint, $onlyJson=false){ parent::__construct($url,$endpoint); + $this->onlyJson = $onlyJson; $this->urlRegExp=preg_replace(array("/\*/","/\//","/\.\*\./"),array(".*","\/",".*"),$url); $this->urlRegExp="/".$this->urlRegExp."/"; if (preg_match("/\{format\}/",$endpoint)){ @@ -47,10 +51,12 @@ class OEmbedProvider extends EmbedProvider{ } } private function provideObject($url){ - $xml=simplexml_load_string($this->provideXML($url)); - if (empty($xml)) { + if (!$this->onlyJson) { + $xml=simplexml_load_string($this->provideXML($url)); + } + else { $data=$this->provide($url); - if (!empty($$data)) $xml = json_decode($data); + if (!empty($data)) $xml = json_decode($data); } //TODO $xml->type alapjan assigner $obj = $this->getTypeObj((string)$xml->type); diff --git a/serendipity_event_oembed/oembed/ProviderManager.class.php b/serendipity_event_oembed/oembed/ProviderManager.class.php index 34350be9..a2df7618 100644 --- a/serendipity_event_oembed/oembed/ProviderManager.class.php +++ b/serendipity_event_oembed/oembed/ProviderManager.class.php @@ -7,7 +7,8 @@ class ProviderManager{ $xml = simplexml_load_file(PLUGIN_OEMBED_PROVIDER_XML_FILE);// PROVIDER_XML comes from config.php foreach($xml->provider as $provider){ if(!isset($provider->class) && isset($provider->endpoint)){ - $this->register(new OEmbedProvider($provider->url,$provider->endpoint)); + $onlyJson = isset($provider->jsononly); + $this->register(new OEmbedProvider($provider->url,$provider->endpoint, $onlyJson)); } else { $classname="".$provider->class; // force to be string :) $reflection = new ReflectionClass($classname); diff --git a/serendipity_event_oembed/oembed/providers.xml b/serendipity_event_oembed/oembed/providers.xml index d2e6af9e..7ad3b0b5 100644 --- a/serendipity_event_oembed/oembed/providers.xml +++ b/serendipity_event_oembed/oembed/providers.xml @@ -25,7 +25,7 @@ --> - http://*.youtube.com/watch?v=* + http://*.youtube.com/* http://www.youtube.com/oembed @@ -34,12 +34,13 @@ - http://official.fm/tracks/* + http://official.fm/(playlists|tracks)/* http://official.fm/services/oembed.{format} http://blip.tv/* + http://blip.tv/oembed diff --git a/serendipity_event_oembed/serendipity_event_oembed.php b/serendipity_event_oembed/serendipity_event_oembed.php index 443b1f4f..4af6a958 100644 --- a/serendipity_event_oembed/serendipity_event_oembed.php +++ b/serendipity_event_oembed/serendipity_event_oembed.php @@ -61,7 +61,7 @@ class serendipity_event_oembed extends serendipity_event if ($simplePatterns==null) { $simplePatterns = array( //'simpleTweet' => '@\(tweet\s+(\S*)\)@Usi', - 'simpleTweet' => '@\[(?:embed|tweet)\s+(.*)\]@Usi', + 'simpleTweet' => '@\[(?:e|embed|tweet)\s+(.*)\]@Usi', ); } @@ -109,13 +109,14 @@ class serendipity_event_oembed extends serendipity_event $manager = ProviderManager::getInstance(); try { $obj=$manager->provide($url,"object"); - if (!empty($obj)) { + if (isset($obj)) { OEmbedDatabase::save_oembed($url,$obj); } } catch (ErrorException $e) { + print "Loading online url ex $e ..\n"; // Timeout in most cases - //return $e; + return $e; } } if (!empty($obj)) { From 46fda7303d48fca5ea5b3f79e11391fd0c439521 Mon Sep 17 00:00:00 2001 From: Grischa Brockhaus Date: Thu, 15 Dec 2011 16:48:50 +0100 Subject: [PATCH 07/14] OEmbed entities templatable --- serendipity_event_oembed/OEmbedDatabase.php | 18 +++++- serendipity_event_oembed/OEmbedTemplater.php | 21 +++++++ serendipity_event_oembed/lang_en.inc.php | 9 +++ serendipity_event_oembed/oembed.tpl | 16 ++++++ .../oembed/OEmbedProvider.class.php | 1 + .../oembed/ProviderList.php | 18 ++++++ serendipity_event_oembed/oembed/providers.xml | 57 ++++++++++++++++++- .../serendipity_event_oembed.php | 38 ++++++------- 8 files changed, 155 insertions(+), 23 deletions(-) create mode 100644 serendipity_event_oembed/OEmbedTemplater.php create mode 100644 serendipity_event_oembed/oembed.tpl create mode 100644 serendipity_event_oembed/oembed/ProviderList.php diff --git a/serendipity_event_oembed/OEmbedDatabase.php b/serendipity_event_oembed/OEmbedDatabase.php index 98e86571..858dc444 100644 --- a/serendipity_event_oembed/OEmbedDatabase.php +++ b/serendipity_event_oembed/OEmbedDatabase.php @@ -6,13 +6,16 @@ class OEmbedDatabase { function save_oembed($url, $oembed) { if (empty($url) || !isset($oembed)) return false; + if (isset($oembed->html)) { + $oembed->html = OEmbedDatabase::cleanup_html($oembed->html); + } $save = array(); $save['urlmd5'] = md5($url); $save['url'] = $url; $save['oetype'] = $oembed->type; $save['oeobj'] = serialize($oembed); - return serendipity_db_insert( PLUGIN_OEMBED_DATABASEVNAME, $save ); - + serendipity_db_insert( PLUGIN_OEMBED_DATABASEVNAME, $save ); + return $oembed; } function load_oembed($url) { @@ -76,4 +79,15 @@ class OEmbedDatabase { } } + function cleanup_html( $str ) { + // Clear unicode stuff + $str=str_ireplace("\u003C","<",$str); + $str=str_ireplace("\u003E",">",$str); + // Clear CDATA Trash. + $str = preg_replace("@^$@", '$1', $str); + $str = preg_replace("@^$@", '$1', $str); + return $str; + } + } \ No newline at end of file diff --git a/serendipity_event_oembed/OEmbedTemplater.php b/serendipity_event_oembed/OEmbedTemplater.php new file mode 100644 index 00000000..f163c2df --- /dev/null +++ b/serendipity_event_oembed/OEmbedTemplater.php @@ -0,0 +1,21 @@ +assign('oembedurl',$url); + $serendipity['smarty']->assign('oembed',(array)$oembed); + $tfile = serendipity_getTemplateFile($filename, 'serendipityPath'); + if (!$tfile || $filename == $tfile) { + $tfile = dirname(__FILE__) . '/' . $filename; + } + $inclusion = $serendipity['smarty']->security_settings[@INCLUDE_ANY]; + $serendipity['smarty']->security_settings[@INCLUDE_ANY] = true; + // 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 + $serendipity['smarty']->security_settings[@INCLUDE_ANY] = $inclusion; + return $content; + } +} \ No newline at end of file diff --git a/serendipity_event_oembed/lang_en.inc.php b/serendipity_event_oembed/lang_en.inc.php index e76e12a8..1f787309 100755 --- a/serendipity_event_oembed/lang_en.inc.php +++ b/serendipity_event_oembed/lang_en.inc.php @@ -8,3 +8,12 @@ @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_INFO', '

oEmbed Plugin

' . +'

'. +'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.
' . +'The syntax for this plugin is [embed link]. If the link is not supported by the plugin at the moment, it will replace the URL by a link pointing to that URL.
'. +'

'. +'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.)'. +'

'. +'The plugin supports representations of the following link types:%s'. +'

'); diff --git a/serendipity_event_oembed/oembed.tpl b/serendipity_event_oembed/oembed.tpl new file mode 100644 index 00000000..41a1b167 --- /dev/null +++ b/serendipity_event_oembed/oembed.tpl @@ -0,0 +1,16 @@ +{* oembed.tpl last modified 2011-12-01 *} + +{if $oembed.type=='rich'} +{$oembed.html} +{elseif $oembed.type=='video'} +{$oembed.html} +{elseif $oembed.type=='image'} + +{elseif $oembed.type=='photo'} + +{elseif $oembed.type=='link'} +{$oembed.author_name} +{else} +{$oembedurl} +{/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 3bef5ed6..af7ce783 100644 --- a/serendipity_event_oembed/oembed/OEmbedProvider.class.php +++ b/serendipity_event_oembed/oembed/OEmbedProvider.class.php @@ -34,6 +34,7 @@ class OEmbedProvider extends EmbedProvider{ } private function getTypeObj($type){ switch($type){ + case "image": case "photo": return new PhotoEmbed(); break; diff --git a/serendipity_event_oembed/oembed/ProviderList.php b/serendipity_event_oembed/oembed/ProviderList.php new file mode 100644 index 00000000..b8c4f5ac --- /dev/null +++ b/serendipity_event_oembed/oembed/ProviderList.php @@ -0,0 +1,18 @@ +provider as $provider){ + if (isset($provider->name)) { + $pentry = $provider->name; + if ($with_url && isset($provider->url)) { + $pentry = "$pentry
(" . $provider->url . ")"; + } + $provider_names[] = $pentry; + } + } + natsort($provider_names); + return "
  1. " . implode("
  2. ", $provider_names) . "
"; + } +} \ No newline at end of file diff --git a/serendipity_event_oembed/oembed/providers.xml b/serendipity_event_oembed/oembed/providers.xml index 7ad3b0b5..629a871a 100644 --- a/serendipity_event_oembed/oembed/providers.xml +++ b/serendipity_event_oembed/oembed/providers.xml @@ -1,6 +1,7 @@ + Flickr (image and video) http://*.flickr.com/* http://www.flickr.com/services/oembed/ @@ -11,7 +12,8 @@ --> - https://*.twitter.com/* + Twitter Status + https://*.twitter.com/*/status/* https://api.twitter.com/1/statuses/oembed.{format} @@ -25,23 +27,76 @@ --> + Youtube http://*.youtube.com/* http://www.youtube.com/oembed + Youtube short link http://*.youtu.be/* http://www.youtube.com/oembed + Official.fm (tracks and playlist) http://official.fm/(playlists|tracks)/* http://official.fm/services/oembed.{format} + Blib TV http://blip.tv/* http://blip.tv/oembed + + + My Opera + http://my.opera.com/* + http://my.opera.com/service/oembed + + + + Hulu + http://www.hulu.com/* + http://www.hulu.com/api/oembed.{format} + + + Revision 3 + http://*.revision3.com/* + http://revision3.com/api/oembed/ + + + Viddler + http://*.viddler.com/* + http://lab.viddler.com/services/oembed/ + + + + YFrog + + http://*.yfrog.com/* + http://www.yfrog.com/api/oembed + + + + DailyMotion + http://*.dailymotion.com/* + http://www.dailymotion.com/services/oembed + + + + PicPlz + + http://*.picplz.com/* + http://picplz.com/oembed + + + + SoundCloud + http://*.soundcloud.com/* + http://soundcloud.com/oembed + + diff --git a/serendipity_event_oembed/serendipity_event_oembed.php b/serendipity_event_oembed/serendipity_event_oembed.php index 4af6a958..b98e42fa 100644 --- a/serendipity_event_oembed/serendipity_event_oembed.php +++ b/serendipity_event_oembed/serendipity_event_oembed.php @@ -11,8 +11,10 @@ if (file_exists($probelang)) { } include dirname(__FILE__) . '/lang_en.inc.php'; -require_once dirname(__FILE__) . '/oembed/config.php'; // autoload oembed classes +require_once dirname(__FILE__) . '/oembed/config.php'; // autoload oembed classes and config require_once dirname(__FILE__) . '/OEmbedDatabase.php'; +require_once dirname(__FILE__) . '/OEmbedTemplater.php'; +require_once dirname(__FILE__) . '/oembed/ProviderList.php'; class serendipity_event_oembed extends serendipity_event @@ -48,7 +50,7 @@ class serendipity_event_oembed extends serendipity_event switch($name) { case 'info': $propbag->add('type', 'content'); - $propbag->add('default', "Info"); + $propbag->add('default', sprintf(PLUGIN_EVENT_OEMBED_INFO, ProviderList::ul_providernames(true))); break; } } @@ -94,7 +96,7 @@ class serendipity_event_oembed extends serendipity_event $eventData['body']); } if (!empty($eventData['extended'])) { - $eventData['body'] = preg_replace_callback( + $eventData['extended'] = preg_replace_callback( $patterns['simpleTweet'], array( $this, "oembedRewriteCallback"), $eventData['extended']); @@ -110,30 +112,26 @@ class serendipity_event_oembed extends serendipity_event try { $obj=$manager->provide($url,"object"); if (isset($obj)) { - OEmbedDatabase::save_oembed($url,$obj); + $obj = OEmbedDatabase::save_oembed($url,$obj); } } catch (ErrorException $e) { - print "Loading online url ex $e ..\n"; // Timeout in most cases - return $e; + //return $e; } } - if (!empty($obj)) { - if ($obj->type == 'rich') - $html = $obj->html; - elseif ($obj->type == 'video') - $html = $obj->html; - elseif ($obj->type == 'photo') { - $html = '' .$obj->title . ''; - } - } - else { - $html = '' . $match[1] . ''; - } - return '' . $html . ''; + return OEmbedTemplater::fetchTemplate('oembed.tpl',$obj, $url); + } + function cleanup_html( $str ) { + // Clear unicode stuff + $str=str_ireplace("\u003C","<",$str); + $str=str_ireplace("\u003E",">",$str); + // Clear CDATA Trash. + $str = preg_replace("@^$@", '$1', $str); + $str = preg_replace("@^$@", '$1', $str); + return $str; } - function cleanup() { OEmbedDatabase::install($this); } From b1ea56b43024dd804ae3a2e5a2fa4eb168fc7483 Mon Sep 17 00:00:00 2001 From: Grischa Brockhaus Date: Fri, 16 Dec 2011 11:44:55 +0100 Subject: [PATCH 08/14] OEmbed: * Instagr.am added * Global maxwidth/maxheight setting *Microblogging: * Minimal optical changes --- serendipity_event_oembed/OEmbedDatabase.php | 6 ++ serendipity_event_oembed/lang_en.inc.php | 4 ++ serendipity_event_oembed/oembed.tpl | 6 +- .../oembed/EmbedProvider.class.php | 6 +- .../oembed/OEmbedProvider.class.php | 16 ++++- .../oembed/ProviderManager.class.php | 9 +-- .../oembed/YouTubeProvider.class.php | 4 +- serendipity_event_oembed/oembed/providers.xml | 10 ++- .../serendipity_event_oembed.php | 65 ++++++++++++------- .../serendipity_event_twitter.php | 4 +- 10 files changed, 91 insertions(+), 39 deletions(-) diff --git a/serendipity_event_oembed/OEmbedDatabase.php b/serendipity_event_oembed/OEmbedDatabase.php index 858dc444..d5eda746 100644 --- a/serendipity_event_oembed/OEmbedDatabase.php +++ b/serendipity_event_oembed/OEmbedDatabase.php @@ -42,6 +42,11 @@ class OEmbedDatabase { } return null; } + function clear_cache() { + global $serendipity; + $q = "delete from {$serendipity['dbPrefix']}" . PLUGIN_OEMBED_DATABASEVNAME; + serendipity_db_schema_import($q); + } function install(&$obj) { global $serendipity; @@ -80,6 +85,7 @@ class OEmbedDatabase { } function cleanup_html( $str ) { + $str = trim($str); // Clear unicode stuff $str=str_ireplace("\u003C","<",$str); $str=str_ireplace("\u003E",">",$str); diff --git a/serendipity_event_oembed/lang_en.inc.php b/serendipity_event_oembed/lang_en.inc.php index 1f787309..04f0f325 100755 --- a/serendipity_event_oembed/lang_en.inc.php +++ b/serendipity_event_oembed/lang_en.inc.php @@ -8,6 +8,10 @@ @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_INFO', '

oEmbed Plugin

' . '

'. '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.
' . diff --git a/serendipity_event_oembed/oembed.tpl b/serendipity_event_oembed/oembed.tpl index 41a1b167..b40818bd 100644 --- a/serendipity_event_oembed/oembed.tpl +++ b/serendipity_event_oembed/oembed.tpl @@ -5,12 +5,12 @@ {elseif $oembed.type=='video'} {$oembed.html} {elseif $oembed.type=='image'} - +{$oembed.title} {elseif $oembed.type=='photo'} - +{$oembed.title} {elseif $oembed.type=='link'} {$oembed.author_name} {else} -{$oembedurl} +{$oembedurl} {/if} \ No newline at end of file diff --git a/serendipity_event_oembed/oembed/EmbedProvider.class.php b/serendipity_event_oembed/oembed/EmbedProvider.class.php index ad94760c..5187d849 100644 --- a/serendipity_event_oembed/oembed/EmbedProvider.class.php +++ b/serendipity_event_oembed/oembed/EmbedProvider.class.php @@ -2,11 +2,15 @@ abstract class EmbedProvider { public $url; public $endpoint; + public $maxwidth; + public $maxheight; public abstract function match($url); public abstract function provide($url,$format="json"); // public abstract function register(); - public function __construct($url,$endpoint){ + public function __construct($url,$endpoint, $maxwidth=null, $maxheight=null){ $this->url = $url; $this->endpoint = $endpoint; + $this->maxwidth = $maxwidth; + $this->maxheight = $maxheight; } } diff --git a/serendipity_event_oembed/oembed/OEmbedProvider.class.php b/serendipity_event_oembed/oembed/OEmbedProvider.class.php index af7ce783..b78125b7 100644 --- a/serendipity_event_oembed/oembed/OEmbedProvider.class.php +++ b/serendipity_event_oembed/oembed/OEmbedProvider.class.php @@ -3,12 +3,14 @@ class OEmbedProvider extends EmbedProvider{ private $urlRegExp; private $jsonEndpoint; private $xmlEndpoint; + private $dimensionsSupported = true; private $onlyJson = false; - public function __construct($url,$endpoint, $onlyJson=false){ - parent::__construct($url,$endpoint); + public function __construct($url,$endpoint, $onlyJson=false, $maxwidth=null, $maxheight=null, $dimensionsSupported=true){ + parent::__construct($url,$endpoint,$maxwidth,$maxheight); $this->onlyJson = $onlyJson; + $this->dimensionsSupported = $dimensionsSupported; $this->urlRegExp=preg_replace(array("/\*/","/\//","/\.\*\./"),array(".*","\/",".*"),$url); $this->urlRegExp="/".$this->urlRegExp."/"; if (preg_match("/\{format\}/",$endpoint)){ @@ -20,6 +22,16 @@ class OEmbedProvider extends EmbedProvider{ $this->jsonEndpoint=$endpoint."?url={url}&format=json"; $this->xmlEndpoint=$endpoint."?url={url}&format=xml"; } + if ($this->dimensionsSupported) { + if (!empty($this->maxwidth)) { + $this->jsonEndpoint.= '&maxwidth=' . $this->maxwidth; + $this->xmlEndpoint.= '&maxwidth=' . $this->maxwidth; + } + if (!empty($this->maxheight)) { + $this->jsonEndpoint.= '&maxwidth=' . $this->maxheight; + $this->xmlEndpoint.= '&maxwidth=' . $this->maxheight; + } + } } public function getUrlRegExp(){ return $this->urlRegExp; } diff --git a/serendipity_event_oembed/oembed/ProviderManager.class.php b/serendipity_event_oembed/oembed/ProviderManager.class.php index a2df7618..92691a08 100644 --- a/serendipity_event_oembed/oembed/ProviderManager.class.php +++ b/serendipity_event_oembed/oembed/ProviderManager.class.php @@ -2,13 +2,14 @@ class ProviderManager{ private $providers; private static $_instance; - private function __construct(){ + private function __construct($maxwidth=null, $maxheight=null){ $this->providers=array(); $xml = simplexml_load_file(PLUGIN_OEMBED_PROVIDER_XML_FILE);// PROVIDER_XML comes from config.php foreach($xml->provider as $provider){ if(!isset($provider->class) && isset($provider->endpoint)){ $onlyJson = isset($provider->jsononly); - $this->register(new OEmbedProvider($provider->url,$provider->endpoint, $onlyJson)); + $dimensionsSupported = !isset($provider->nodimensionsupport); + $this->register(new OEmbedProvider($provider->url,$provider->endpoint, $onlyJson, $maxwidth, $maxheight, $dimensionsSupported)); } else { $classname="".$provider->class; // force to be string :) $reflection = new ReflectionClass($classname); @@ -16,9 +17,9 @@ class ProviderManager{ } } } - static function getInstance(){ + static function getInstance($maxwidth=null, $maxheight=null){ if(!isset($_instance) || $_instance==null){ - $_instance = new ProviderManager(); + $_instance = new ProviderManager($maxwidth, $maxheight); } return $_instance; } diff --git a/serendipity_event_oembed/oembed/YouTubeProvider.class.php b/serendipity_event_oembed/oembed/YouTubeProvider.class.php index 2bf03ec4..d7797a0b 100644 --- a/serendipity_event_oembed/oembed/YouTubeProvider.class.php +++ b/serendipity_event_oembed/oembed/YouTubeProvider.class.php @@ -74,7 +74,7 @@ class YouTubeProvider extends EmbedProvider { } - public function __construct($config){ - parent::__construct("http://youtube.com",""); + public function __construct($config,$maxwidth=null, $maxheight=null){ + parent::__construct("http://youtube.com","", $maxwidth, $maxheight); } } diff --git a/serendipity_event_oembed/oembed/providers.xml b/serendipity_event_oembed/oembed/providers.xml index 629a871a..04e8eb4e 100644 --- a/serendipity_event_oembed/oembed/providers.xml +++ b/serendipity_event_oembed/oembed/providers.xml @@ -13,7 +13,7 @@ --> Twitter Status - https://*.twitter.com/*/status/* + https?://*.twitter.com/*/status(es)?/* https://api.twitter.com/1/statuses/oembed.{format} @@ -52,6 +52,7 @@ My Opera + http://my.opera.com/* http://my.opera.com/service/oembed @@ -79,6 +80,13 @@ http://www.yfrog.com/api/oembed + + Instragr.am + + http://*.instagr.am/* + http://api.instagram.com/api/v1/oembed/ + + DailyMotion http://*.dailymotion.com/* diff --git a/serendipity_event_oembed/serendipity_event_oembed.php b/serendipity_event_oembed/serendipity_event_oembed.php index b98e42fa..0d115374 100644 --- a/serendipity_event_oembed/serendipity_event_oembed.php +++ b/serendipity_event_oembed/serendipity_event_oembed.php @@ -37,12 +37,10 @@ class serendipity_event_oembed extends serendipity_event )); $propbag->add('groups', array('FRONTEND_EXTERNAL_SERVICES')); $propbag->add('event_hooks', array( -// 'backend_publish' => true, // An entry was puplished (was draft before or saved from the scratch). -// 'backend_save' => true, // An entry was saved. 'frontend_display' => true, )); - - $propbag->add('configuration', array('info')); + $configuration = $configuration = array('info','maxwidth','maxheight'); + $propbag->add('configuration', $configuration); } function introspect_config_item($name, &$propbag) @@ -52,7 +50,20 @@ class serendipity_event_oembed extends serendipity_event $propbag->add('type', 'content'); $propbag->add('default', sprintf(PLUGIN_EVENT_OEMBED_INFO, ProviderList::ul_providernames(true))); break; + case 'maxwidth': + $propbag->add('type', 'string'); + $propbag->add('name', PLUGIN_EVENT_OEMBED_MAXWIDTH); + $propbag->add('description', PLUGIN_EVENT_OEMBED_MAXWIDTH_DESC); + $propbag->add('default', ''); + break; + case 'maxheight': + $propbag->add('type', 'string'); + $propbag->add('name', PLUGIN_EVENT_OEMBED_MAXHEIGHT); + $propbag->add('description', PLUGIN_EVENT_OEMBED_MAXHEIGHT_DESC); + $propbag->add('default', ''); + break; } + return true; } function event_hook($event, &$bag, &$eventData) { @@ -72,14 +83,10 @@ class serendipity_event_oembed extends serendipity_event if (isset($hooks[$event])) { switch($event) { case 'frontend_display': - case 'backend_publish': - case 'backend_save': - if (!isset($eventData['body']) && !isset($eventData['extended'])) { - // Do not use for user comments, html nuggets, static pages etc. - return false; - break; + if (isset($eventData['body']) && isset($eventData['extended'])) { + $this->update_entry($eventData, $simplePatterns, 'body'); + $this->update_entry($eventData, $simplePatterns, 'extended'); } - $this->update_entry($eventData, $simplePatterns); return true; } } @@ -88,27 +95,34 @@ class serendipity_event_oembed extends serendipity_event } - function update_entry(&$eventData, &$patterns) { - if (!empty($eventData['body'])) { - $eventData['body'] = preg_replace_callback( + function update_entry(&$eventData, &$patterns, $dateType) { + if (!empty($eventData[$dateType])) { + $eventData[$dateType] = preg_replace_callback( $patterns['simpleTweet'], array( $this, "oembedRewriteCallback"), - $eventData['body']); - } - if (!empty($eventData['extended'])) { - $eventData['extended'] = preg_replace_callback( - $patterns['simpleTweet'], - array( $this, "oembedRewriteCallback"), - $eventData['extended']); + $eventData[$dateType]); } } function oembedRewriteCallback($match) { $url = $match[1]; + $maxwidth = $this->get_config('maxwidth',''); + $maxheight = $this->get_config('maxheight',''); + $obj = $this->expand($url, $maxwidth, $maxheight); + return OEmbedTemplater::fetchTemplate('oembed.tpl',$obj, $url); + } + + /** + * This method can be used by other plugins. It will expand an URL to an oembed object (or null if not supported). + * @param string $url The url to be expanded + * @param string $maxwidth Maximum width of returned object (if service supports this). May be left empty + * @param string $maxheight Maximum height of returned object (if service supports this). May be left empty + * @return OEmbed or null + */ + function expand($url, $maxwidth=null, $maxheight=null) { $obj = OEmbedDatabase::load_oembed($url); - $html = ''; if (empty($obj)) { - $manager = ProviderManager::getInstance(); + $manager = ProviderManager::getInstance($maxwidth,$maxheight); try { $obj=$manager->provide($url,"object"); if (isset($obj)) { @@ -120,8 +134,9 @@ class serendipity_event_oembed extends serendipity_event //return $e; } } - return OEmbedTemplater::fetchTemplate('oembed.tpl',$obj, $url); + return $obj; } + function cleanup_html( $str ) { // Clear unicode stuff $str=str_ireplace("\u003C","<",$str); @@ -134,6 +149,8 @@ class serendipity_event_oembed extends serendipity_event } function cleanup() { OEmbedDatabase::install($this); + OEmbedDatabase::clear_cache(); + echo '

Cleared oembed cache.
'; } function install() { OEmbedDatabase::install($this); diff --git a/serendipity_plugin_twitter/serendipity_event_twitter.php b/serendipity_plugin_twitter/serendipity_event_twitter.php index 667a875a..7fb6dbf1 100644 --- a/serendipity_plugin_twitter/serendipity_event_twitter.php +++ b/serendipity_plugin_twitter/serendipity_event_twitter.php @@ -625,7 +625,7 @@ class serendipity_event_twitter extends serendipity_plugin { $propbag->add('name', PLUGIN_EVENT_TWITTER_CONSUMER_KEY); $propbag->add('description', PLUGIN_EVENT_TWITTER_CONSUMER_KEY_DESC); $propbag->add('default', ''); - break; + break; case 'general_oa_consumersecret': $propbag->add('type', 'string'); $propbag->add('name', PLUGIN_EVENT_TWITTER_CONSUMER_SECRET); @@ -1245,7 +1245,7 @@ a.twitter_update_time { } if (empty($highest_ids[$article_id]['last_info']) || empty($highest_ids[$article_id]['last_info']['lasttweetid']) || "{$entry[TWITTER_SEARCHRESULT_ID]}">$highest_ids[$article_id]['last_info']['lasttweetid']) { if ($complete) { // This is called from admin interface - echo "Found new tweetback for article $article_id: tweetid: {$entry[TWITTER_SEARCHRESULT_ID]}
"; + echo "
Found new tweetback for article $article_id: tweetid: {$entry[TWITTER_SEARCHRESULT_ID]}

"; } $this->check_tweetbacks_save_comment($article_id, $entry, $comment_type, true); $comment_saved = true; From b0c245e434f8bbb571791d93a5ac35eca91f4f29 Mon Sep 17 00:00:00 2001 From: Grischa Brockhaus Date: Sun, 18 Dec 2011 00:44:57 +0100 Subject: [PATCH 09/14] 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. --- serendipity_event_oembed/OEmbedDatabase.php | 6 +- serendipity_event_oembed/OEmbedTemplater.php | 18 +++- serendipity_event_oembed/lang_en.inc.php | 13 +-- serendipity_event_oembed/oembed.tpl | 35 +++++--- .../oembed/OEmbedProvider.class.php | 22 ++++- serendipity_event_oembed/oembed/providers.xml | 89 ++++++++++++++++++- .../serendipity_event_oembed.php | 3 +- 7 files changed, 160 insertions(+), 26 deletions(-) diff --git a/serendipity_event_oembed/OEmbedDatabase.php b/serendipity_event_oembed/OEmbedDatabase.php index d5eda746..e994584c 100644 --- a/serendipity_event_oembed/OEmbedDatabase.php +++ b/serendipity_event_oembed/OEmbedDatabase.php @@ -90,9 +90,9 @@ class OEmbedDatabase { $str=str_ireplace("\u003C","<",$str); $str=str_ireplace("\u003E",">",$str); // Clear CDATA Trash. - $str = preg_replace("@^$@", '$1', $str); - $str = preg_replace("@^$@", '$1', $str); + $str = preg_replace("@^$@", '$1', $str); + $str = preg_replace("@^$@", '$1', $str); return $str; } diff --git a/serendipity_event_oembed/OEmbedTemplater.php b/serendipity_event_oembed/OEmbedTemplater.php index f163c2df..2e35757c 100644 --- a/serendipity_event_oembed/OEmbedTemplater.php +++ b/serendipity_event_oembed/OEmbedTemplater.php @@ -1,9 +1,12 @@ 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; } } \ No newline at end of file diff --git a/serendipity_event_oembed/lang_en.inc.php b/serendipity_event_oembed/lang_en.inc.php index 04f0f325..4452f636 100755 --- a/serendipity_event_oembed/lang_en.inc.php +++ b/serendipity_event_oembed/lang_en.inc.php @@ -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', '

oEmbed Plugin

' . '

'. '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.
' . -'The syntax for this plugin is [embed link]. If the link is not supported by the plugin at the moment, it will replace the URL by a link pointing to that URL.
'. +'The syntax of this plugin is [embed link] (or [e link] if you like it shorter).
'. +'If the link is not supported by the plugin at the moment, it will replace the URL by a link pointing to that URL.
'. '

'. '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.)'. '

'. diff --git a/serendipity_event_oembed/oembed.tpl b/serendipity_event_oembed/oembed.tpl index b40818bd..15705980 100644 --- a/serendipity_event_oembed/oembed.tpl +++ b/serendipity_event_oembed/oembed.tpl @@ -1,16 +1,29 @@ {* oembed.tpl last modified 2011-12-01 *} -{if $oembed.type=='rich'} -{$oembed.html} -{elseif $oembed.type=='video'} -{$oembed.html} -{elseif $oembed.type=='image'} -{$oembed.title} -{elseif $oembed.type=='photo'} -{$oembed.title} -{elseif $oembed.type=='link'} -{$oembed.author_name} +{if $oembed.type=='rich'} {* =================================================== RICH *} +{if $oembed.provider_name|stripos:"Wikipedia" !== false} +

{$oembed.html}
+{elseif $oembed.provider_name|stripos:"IMDB" !== false} {* beautify IMDB content *} +
{$oembed.html|replace:"

":""|replace:"

":""|replace:" +{elseif $oembed.provider-name|stripos:"Soundcloud" !== false} {* beautify SoundCloud *} +{$oembed.html|replace:"":"
"} {else} -{$oembedurl} +{$oembed.html} +{/if} +{elseif $oembed.type=='video'} {* =================================================== VIDEO *} +{$oembed.html} +{elseif $oembed.type=='image'} {* =================================================== IMAGE *} +{$oembed.title} +{elseif $oembed.type=='photo'} {* =================================================== PHOTO *} +{$oembed.title} +{elseif $oembed.type=='link'} {* =================================================== LINK *} +{if $oembed.description} +{if $oembed.title}{$oembed.title}
{/if} +

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

+{else} +{$oembed.author_name} +{/if} +{else} +{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 b78125b7..d6ab375d 100644 --- a/serendipity_event_oembed/oembed/OEmbedProvider.class.php +++ b/serendipity_event_oembed/oembed/OEmbedProvider.class.php @@ -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(){} diff --git a/serendipity_event_oembed/oembed/providers.xml b/serendipity_event_oembed/oembed/providers.xml index 04e8eb4e..c1d8f217 100644 --- a/serendipity_event_oembed/oembed/providers.xml +++ b/serendipity_event_oembed/oembed/providers.xml @@ -36,7 +36,17 @@ http://*.youtu.be/* http://www.youtube.com/oembed - + + SmugMug + http://*.smugmug.com/* + http://api.smugmug.com/services/oembed/ + + + Photobucket + http://*.photobucket.com/(albums|groups)/* + http://photobucket.com/oembed + + Official.fm (tracks and playlist) http://official.fm/(playlists|tracks)/* @@ -49,6 +59,12 @@ http://blip.tv/oembed + + + Qik Mobile Videos + http://qik.com/video/* + http://qik.com/api/oembed.{format} + My Opera @@ -105,6 +121,75 @@ http://*.soundcloud.com/* http://soundcloud.com/oembed + + Skitch + http://(www.)?skitch.com/* + http://skitch.com/oembed/ + + + + Wikipedia (via noembed.com) + + https?://*.wikipedia.org/wiki/* + http://noembed.com/embed + + + Twitpic (via noembed.com) + + http://(www.)?twitpic.com/* + http://noembed.com/embed + + + Imgur.com (via noembed.com) + + http://imgur.com/* + http://noembed.com/embed + + + Imdb.com (via noembed.com) + + http://(www.)?imdb.com/title/* + http://noembed.com/embed + + + CloudApp (via noembed.com) + + http://cl.ly/* + http://noembed.com/embed + + + ASCII Art Farts (via noembed.com) + + http://www.asciiartfarts.com/*.html + http://noembed.com/embed + + + GiantBomb (via noembed.com) + + http://www.giantbomb.com/* + http://noembed.com/embed + + + Spotify (via noembed.com) + + https?://open.spotify.com/(track|album)/* + http://noembed.com/embed + + + + + + Audioboo (via oohembed.com) + + http://((www.)?audio)?boo.fm/boos/* + http://oohembed.com/oohembed/ + - diff --git a/serendipity_event_oembed/serendipity_event_oembed.php b/serendipity_event_oembed/serendipity_event_oembed.php index 0d115374..b43f1d9a 100644 --- a/serendipity_event_oembed/serendipity_event_oembed.php +++ b/serendipity_event_oembed/serendipity_event_oembed.php @@ -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', ); } From 0fd597cd348e458de1a9e5ea99c7fc6728cbd243 Mon Sep 17 00:00:00 2001 From: Grischa Brockhaus Date: Sun, 18 Dec 2011 01:55:33 +0100 Subject: [PATCH 10/14] 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/ + + From ed9b492b623c65cadd59fa590f1098eb971ed4c9 Mon Sep 17 00:00:00 2001 From: Grischa Brockhaus Date: Sun, 18 Dec 2011 02:40:27 +0100 Subject: [PATCH 11/14] OEmbed * Prepared first Spartacus release --- serendipity_event_oembed/ChangeLog | 3 +++ serendipity_event_oembed/serendipity_event_oembed.php | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 serendipity_event_oembed/ChangeLog diff --git a/serendipity_event_oembed/ChangeLog b/serendipity_event_oembed/ChangeLog new file mode 100644 index 00000000..e6f161c3 --- /dev/null +++ b/serendipity_event_oembed/ChangeLog @@ -0,0 +1,3 @@ +Version 1.00 (brockhaus) +----------------------- +* Initial Release, have fun! :) \ No newline at end of file diff --git a/serendipity_event_oembed/serendipity_event_oembed.php b/serendipity_event_oembed/serendipity_event_oembed.php index b43f1d9a..3d4b6ae6 100644 --- a/serendipity_event_oembed/serendipity_event_oembed.php +++ b/serendipity_event_oembed/serendipity_event_oembed.php @@ -30,7 +30,7 @@ class serendipity_event_oembed extends serendipity_event $propbag->add('description', PLUGIN_EVENT_OEMBED_DESC); $propbag->add('stackable', false); $propbag->add('author', 'Grischa Brockhaus'); - $propbag->add('version', '0.01'); + $propbag->add('version', '1.00'); $propbag->add('requirements', array( 'serendipity' => '0.8', 'smarty' => '2.6.7', From 9fd2be9ff629cf9cd00d91460d933ce9d07bd175 Mon Sep 17 00:00:00 2001 From: Grischa Brockhaus Date: Sun, 18 Dec 2011 03:41:20 +0100 Subject: [PATCH 12/14] OEmbed: Typo --- serendipity_event_oembed/oembed/providers.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/serendipity_event_oembed/oembed/providers.xml b/serendipity_event_oembed/oembed/providers.xml index 2f8a73cf..9819bf66 100644 --- a/serendipity_event_oembed/oembed/providers.xml +++ b/serendipity_event_oembed/oembed/providers.xml @@ -97,7 +97,7 @@ - Instragr.am + Instagr.am http://*.instagr.am/* http://api.instagram.com/api/v1/oembed/ From bd492d76c7033e974fde74c5624e11e9d7a0cd77 Mon Sep 17 00:00:00 2001 From: Grischa Brockhaus Date: Sun, 18 Dec 2011 04:44:52 +0100 Subject: [PATCH 13/14] OEmbed: More specific cssclass files for different oembed types --- serendipity_event_oembed/oembed.tpl | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/serendipity_event_oembed/oembed.tpl b/serendipity_event_oembed/oembed.tpl index 2d574e58..d483ec34 100644 --- a/serendipity_event_oembed/oembed.tpl +++ b/serendipity_event_oembed/oembed.tpl @@ -1,6 +1,6 @@ {* oembed.tpl last modified 2011-12-01 *} - {if $oembed.type=='rich'} {* =================================================== RICH *} + {if $oembed.provider_name=="Wikipedia"}
{$oembed.html}
{elseif $oembed.provider_name=="IMDB"} {* beautify IMDB content *} @@ -10,13 +10,21 @@ {else} {$oembed.html} {/if} +
{elseif $oembed.type=='video'} {* =================================================== VIDEO *} + {$oembed.html} + {elseif $oembed.type=='image'} {* =================================================== IMAGE *} + {$oembed.title} + {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} @@ -25,7 +33,9 @@ {$oembed.author_name} {/if} {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 From 5f77678e27a57f8b31999617fd9b337543ca146f Mon Sep 17 00:00:00 2001 From: Grischa Brockhaus Date: Sun, 18 Dec 2011 14:21:38 +0100 Subject: [PATCH 14/14] OEmbed: Minimal performance change. --- serendipity_event_oembed/OEmbedTemplater.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/serendipity_event_oembed/OEmbedTemplater.php b/serendipity_event_oembed/OEmbedTemplater.php index 2e35757c..e2bc2264 100644 --- a/serendipity_event_oembed/OEmbedTemplater.php +++ b/serendipity_event_oembed/OEmbedTemplater.php @@ -17,8 +17,8 @@ class OEmbedTemplater { $inclusion = $serendipity['smarty']->security_settings[@INCLUDE_ANY]; $serendipity['smarty']->security_settings[@INCLUDE_ANY] = true; - - if (version_compare($serendipity['version'], '1.7-alpha1','>=')) { + + if (version_compare($serendipity['version'], '1.7-alpha1')>=0) { $serendipity['smarty']->disableSecurity(); } else {