From a9fb8e4d6052dbb5db95939128d491897b4f6a0c Mon Sep 17 00:00:00 2001 From: Fabio Comuni Date: Mon, 24 Oct 2011 14:30:11 +0200 Subject: [PATCH] Use oembed for bookmark, audio, video, youtube --- include/bbcode.php | 41 ++++++++++++++++++++++++++++++++++++----- mod/item.php | 1 - 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/include/bbcode.php b/include/bbcode.php index b33be686e4..81f37ca047 100644 --- a/include/bbcode.php +++ b/include/bbcode.php @@ -9,6 +9,22 @@ function stripcode_br_cb($s) { return '[code]' . str_replace('
', '', $s[1]) . '[/code]'; } +function tryoembed($match){ + $url = ((count($match)==2)?$match[1]:$match[2]); + + $o = oembed_fetch_url($url); + + //echo "
"; var_dump($match, $url, $o); killme();
+
+	if ($o->type=="error") return $match[0];
+	
+	$html = oembed_format_object($o);
+	
+	return $html;
+	
+}
+
+
 
 	// BBcode 2 HTML was written by WAY2WEB.net
 	// extended to work with Mistpark/Friendika - Mike Macgirvin
@@ -40,11 +56,14 @@ function bbcode($Text,$preserve_nl = false) {
 	// Set up the parameters for a MAIL search string
 	$MAILSearchString = $URLSearchString;
 
+
 	// Perform URL Search
-
-
 	$Text = preg_replace("/([^\]\=]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1$2', $Text);
+	
+	$Text = preg_replace_callback("/\[bookmark\=([^\]]*)\].*?\[\/bookmark\]/ism",'tryoembed',$Text);
+	$Text = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",'[url=$1]$2[/url]',$Text);
 
+	$Text = preg_replace_callback("/\[url\]([$URLSearchString]*)\[\/url\]/ism",'tryoembed',$Text);
 	$Text = preg_replace("/\[url\]([$URLSearchString]*)\[\/url\]/ism", '$1', $Text);
 	$Text = preg_replace("/\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$2', $Text);
 	//$Text = preg_replace("/\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[\/url\]/ism", '$2', $Text);
@@ -120,6 +139,13 @@ function bbcode($Text,$preserve_nl = false) {
 	// [img]pathtoimage[/img]
 	$Text = preg_replace("/\[img\](.*?)\[\/img\]/ism", '' . t('Image/photo') . '', $Text);
 
+
+
+	// Try to Oembed
+	$Text = preg_replace_callback("/\[video\](.*?)\[\/video\]/ism", 'tryoembed', $Text);
+	$Text = preg_replace_callback("/\[audio\](.*?)\[\/audio\]/ism", 'tryoembed', $Text);
+
+
 	// html5 video and audio
 
 	$Text = preg_replace("/\[video\](.*?)\[\/video\]/ism", '', $Text);
@@ -129,17 +155,22 @@ function bbcode($Text,$preserve_nl = false) {
 	$Text = preg_replace("/\[iframe\](.*?)\[\/iframe\]/ism", '', $Text);
          
 
-	if (get_pconfig(local_user(), 'oembed', 'use_for_youtube' )==1){
+	/*if (get_pconfig(local_user(), 'oembed', 'use_for_youtube' )==1){
 		// use oembed for youtube links
 		$Text = preg_replace("/\[youtube\]/",'[embed]',$Text); 
 		$Text = preg_replace("/\[\/youtube\]/",'[/embed]',$Text); 
-	} else {
+	} else {*/
 		// Youtube extensions
+        $Text = preg_replace_callback("/\[youtube\](https?:\/\/www.youtube.com\/watch\?v\=.*?)\[\/youtube\]/ism", 'tryoembed', $Text);        
+        $Text = preg_replace_callback("/\[youtube\](https?:\/\/youtu.be\/.*?)\[\/youtube\]/ism",'tryoembed',$Text); 
+        
         $Text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/watch\?v\=(.*?)\[\/youtube\]/ism",'[youtube]$1[/youtube]',$Text); 
         $Text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/embed\/(.*?)\[\/youtube\]/ism",'[youtube]$1[/youtube]',$Text); 
         $Text = preg_replace("/\[youtube\]https?:\/\/youtu.be\/(.*?)\[\/youtube\]/ism",'[youtube]$1[/youtube]',$Text); 
+		
+        
 		$Text = preg_replace("/\[youtube\]([A-Za-z0-9\-_=]+)(.*?)\[\/youtube\]/ism", '', $Text);
-	}
+	//}
 
 	$Text = preg_replace("/\[vimeo\]https?:\/\/player.vimeo.com\/video\/([0-9]+)(.*?)\[\/vimeo\]/ism",'[vimeo]$1[/vimeo]',$Text); 
 	$Text = preg_replace("/\[vimeo\]https?:\/\/vimeo.com\/([0-9]+)(.*?)\[\/vimeo\]/ism",'[vimeo]$1[/vimeo]',$Text); 
diff --git a/mod/item.php b/mod/item.php
index ab104735b9..0e855d54d9 100644
--- a/mod/item.php
+++ b/mod/item.php
@@ -338,7 +338,6 @@ function item_post(&$a) {
 	$bookmark = 0;
 	if(preg_match_all("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",$body,$match)) {
 		$bookmark = 1;
-		$body = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",'[url=$1]$2[/url]',$body);
 	}