]> git.mxchange.org Git - friendica.git/commitdiff
Move tryoembed to Content\OEmbed
authorHypolite Petovan <mrpetovan@gmail.com>
Sun, 7 Jan 2018 05:44:23 +0000 (00:44 -0500)
committerHypolite Petovan <mrpetovan@gmail.com>
Sun, 7 Jan 2018 05:44:23 +0000 (00:44 -0500)
- Add bbcode functions documentation
- Add Exception handling to OEmbed::getHTML
- Fix formatting

include/bbcode.php
src/Content/OEmbed.php

index ab599e7c919f6aff122806c2101b996d655653a5..f20942dbad674b12ad4ba6456f98e14da9a427aa 100644 (file)
@@ -40,8 +40,19 @@ function bb_map_location($match) {
        return str_replace($match[0], '<div class="map"  >' . Map::byLocation($match[1]) . '</div>', $match[0]);
 }
 
-function bb_attachment($Text, $simplehtml = false, $tryoembed = true) {
-
+/**
+ * Processes [attachment] tags
+ *
+ * Note: Can produce a [bookmark] tag in the returned string
+ *
+ * @brief Processes [attachment] tags
+ * @param string $Text
+ * @param bool|int $simplehtml
+ * @param bool $tryoembed
+ * @return string
+ */
+function bb_attachment($Text, $simplehtml = false, $tryoembed = true)
+{
        $data = get_attachment_data($Text);
        if (!$data) {
                return $Text;
@@ -52,10 +63,7 @@ function bb_attachment($Text, $simplehtml = false, $tryoembed = true) {
                $data["title"] = str_replace(array("http://", "https://"), "", $data["title"]);
        }
 
-       if (((strpos($data["text"], "[img=") !== false)
-               || (strpos($data["text"], "[img]") !== false)
-               || Config::get('system', 'always_show_preview'))
-               && ($data["image"] != "")) {
+       if (((strpos($data["text"], "[img=") !== false) || (strpos($data["text"], "[img]") !== false) || Config::get('system', 'always_show_preview')) && ($data["image"] != "")) {
                $data["preview"] = $data["image"];
                $data["image"] = "";
        }
@@ -69,11 +77,13 @@ function bb_attachment($Text, $simplehtml = false, $tryoembed = true) {
                        $text = sprintf('<span class="type-%s">', $data["type"]);
                }
 
-               $bookmark = array(sprintf('[bookmark=%s]%s[/bookmark]', $data["url"], $data["title"]), $data["url"], $data["title"]);
+               $oembed = sprintf('[bookmark=%s]%s[/bookmark]', $data['url'], $data['title']);
                if ($tryoembed) {
-                       $oembed = tryoembed($bookmark);
-               } else {
-                       $oembed = $bookmark[0];
+                       try {
+                               $oembed = OEmbed::getHTML($data['url'], $data['title']);
+                       } catch (Exception $e) {
+                               // $oembed isn't modified
+                       }
                }
 
                if (stripos($oembed, "<iframe ") !== false) {
@@ -100,7 +110,7 @@ function bb_attachment($Text, $simplehtml = false, $tryoembed = true) {
                        $text .= '</span>';
                }
        }
-       return trim($data["text"].' '.$text.' '.$data["after"]);
+       return trim($data["text"] . ' ' . $text . ' ' . $data["after"]);
 }
 
 function bb_remove_share_information($Text, $plaintext = false, $nolink = false) {
@@ -223,32 +233,6 @@ function stripcode_br_cb($s) {
        return '[code]' . str_replace('<br />', '', $s[1]) . '[/code]';
 }
 
-function tryoembed($match) {
-       $url = $match[1];
-
-       // Always embed the SSL version
-       $url = str_replace(array("http://www.youtube.com/", "http://player.vimeo.com/"),
-                               array("https://www.youtube.com/", "https://player.vimeo.com/"), $url);
-
-       $o = OEmbed::fetchURL($url);
-
-       if (!is_object($o)) {
-               return $match[0];
-       }
-
-       if (isset($match[2])) {
-               $o->title = $match[2];
-       }
-
-       if ($o->type == "error") {
-               return $match[0];
-       }
-
-       $html = OEmbed::formatObject($o);
-
-       return $html;
-}
-
 /*
  * [noparse][i]italic[/i][/noparse] turns into
  * [noparse][ i ]italic[ /i ][/noparse],
@@ -432,6 +416,16 @@ function bb_replace_images($body, $images) {
        return $newbody;
 }
 
+/**
+ * Processes [share] tags
+ *
+ * Note: Can produce a [bookmark] tag in the output
+ *
+ * @brief Processes [share] tags
+ * @param array    $share      preg_match_callback result array
+ * @param bool|int $simplehtml
+ * @return string
+ */
 function bb_ShareAttributes($share, $simplehtml)
 {
        $attributes = $share[2];
@@ -520,7 +514,6 @@ function bb_ShareAttributes($share, $simplehtml)
        }
 
        $preshare = trim($share[1]);
-
        if ($preshare != "") {
                $preshare .= "<br /><br />";
        }
@@ -588,8 +581,13 @@ function bb_ShareAttributes($share, $simplehtml)
                default:
                        // Transforms quoted tweets in rich attachments to avoid nested tweets
                        if (stripos(normalise_link($link), 'http://twitter.com/') === 0 && OEmbed::isAllowedURL($link)) {
-                               $bookmark = array(sprintf('[bookmark=%s]%s[/bookmark]', $link, $preshare), $link, $preshare);
-                               $text = $preshare . tryoembed($bookmark);
+                               try {
+                                       $oembed = OEmbed::getHTML($link, $preshare);
+                               } catch (Exception $e) {
+                                       $oembed = sprintf('[bookmark=%s]%s[/bookmark]', $link, $preshare);
+                               }
+
+                               $text = $preshare . $oembed;
                        } else {
                                $text = trim($share[1]) . "\n";
 
@@ -597,14 +595,13 @@ function bb_ShareAttributes($share, $simplehtml)
 
                                $tpl = get_markup_template('shared_content.tpl');
                                $text .= replace_macros($tpl, array(
-                                               '$profile' => $profile,
-                                               '$avatar' => $avatar,
-                                               '$author' => $author,
-                                               '$link' => $link,
-                                               '$posted' => $posted,
-                                               '$content' => trim($share[3])
-                                       )
-                               );
+                                       '$profile' => $profile,
+                                       '$avatar' => $avatar,
+                                       '$author' => $author,
+                                       '$link' => $link,
+                                       '$posted' => $posted,
+                                       '$content' => trim($share[3])
+                               ));
                        }
                        break;
        }
index 4aaae4d5dca37effc185eeee2c6cb5b0117e7d92..50b8bb4e3545718493fd8d436fc8630868eb41b7 100644 (file)
@@ -15,6 +15,7 @@ use dba;
 use DOMDocument;\r
 use DOMXPath;\r
 use DOMNode;\r
+use Exception;\r
 \r
 require_once 'include/dba.php';\r
 require_once 'mod/proxy.php';\r
@@ -305,6 +306,27 @@ class OEmbed
                return allowed_domain($domain, $allowed, true);\r
        }\r
 \r
+       public static function getHTML($url, $title = null)\r
+       {\r
+               // Always embed the SSL version\r
+               $url = str_replace(array("http://www.youtube.com/", "http://player.vimeo.com/"),\r
+                                       array("https://www.youtube.com/", "https://player.vimeo.com/"), $url);\r
+\r
+               $o = OEmbed::fetchURL($url);\r
+\r
+               if (!is_object($o) || $o->type == 'error') {\r
+                       throw new Exception('OEmbed failed for URL: ' . $url);\r
+               }\r
+\r
+               if (x($title)) {\r
+                       $o->title = $title;\r
+               }\r
+\r
+               $html = OEmbed::formatObject($o);\r
+\r
+               return $html;\r
+       }\r
+\r
        /**\r
         * @brief Generates the iframe HTML for an oembed attachment.\r
         *\r