]> git.mxchange.org Git - friendica.git/blobdiff - src/Content/OEmbed.php
Merge pull request #5435 from annando/htaccess
[friendica.git] / src / Content / OEmbed.php
index 9f1df5174566bdca08e5be7493ad5d70fcb35c22..fe59567b2eec6409219266ed3f59c22c0ce48e6a 100644 (file)
@@ -1,21 +1,26 @@
 <?php
+
 /**
  * @file src/Content/OEmbed.php
  */
 namespace Friendica\Content;
 
+use DOMDocument;
+use DOMNode;
+use DOMText;
+use DOMXPath;
+use Exception;
 use Friendica\Core\Addon;
 use Friendica\Core\Cache;
+use Friendica\Core\Config;
 use Friendica\Core\L10n;
 use Friendica\Core\System;
-use Friendica\Core\Config;
+use Friendica\Database\DBA;
 use Friendica\Database\DBM;
+use Friendica\Util\DateTimeFormat;
+use Friendica\Util\Network;
 use Friendica\Util\ParseUrl;
-use dba;
-use DOMDocument;
-use DOMXPath;
-use DOMNode;
-use Exception;
+use stdClass;
 
 require_once 'include/dba.php';
 require_once 'mod/proxy.php';
@@ -58,7 +63,7 @@ class OEmbed
                $a = get_app();
 
                $condition = ['url' => normalise_link($embedurl), 'maxwidth' => $a->videowidth];
-               $oembed = dba::selectFirst('oembed', ['content'], $condition);
+               $oembed = DBA::selectFirst('oembed', ['content'], $condition);
                if (DBM::is_result($oembed)) {
                        $txt = $oembed["content"];
                } else {
@@ -77,7 +82,7 @@ class OEmbed
                        if (!in_array($ext, $noexts)) {
                                // try oembed autodiscovery
                                $redirects = 0;
-                               $html_text = fetch_url($embedurl, false, $redirects, 15, "text/*");
+                               $html_text = Network::fetchUrl($embedurl, false, $redirects, 15, "text/*");
                                if ($html_text) {
                                        $dom = @DOMDocument::loadHTML($html_text);
                                        if ($dom) {
@@ -85,13 +90,13 @@ class OEmbed
                                                $entries = $xpath->query("//link[@type='application/json+oembed']");
                                                foreach ($entries as $e) {
                                                        $href = $e->getAttributeNode("href")->nodeValue;
-                                                       $txt = fetch_url($href . '&maxwidth=' . $a->videowidth);
+                                                       $txt = Network::fetchUrl($href . '&maxwidth=' . $a->videowidth);
                                                        break;
                                                }
                                                $entries = $xpath->query("//link[@type='text/json+oembed']");
                                                foreach ($entries as $e) {
                                                        $href = $e->getAttributeNode("href")->nodeValue;
-                                                       $txt = fetch_url($href . '&maxwidth=' . $a->videowidth);
+                                                       $txt = Network::fetchUrl($href . '&maxwidth=' . $a->videowidth);
                                                        break;
                                                }
                                        }
@@ -104,12 +109,12 @@ class OEmbed
                                $txt = '{"type":"error"}';
                        } else { //save in cache
                                $j = json_decode($txt);
-                               if ($j->type != "error") {
-                                       dba::insert('oembed', [
+                               if (!empty($j->type) && $j->type != "error") {
+                                       DBA::insert('oembed', [
                                                'url' => normalise_link($embedurl),
                                                'maxwidth' => $a->videowidth,
                                                'content' => $txt,
-                                               'created' => datetime_convert()
+                                               'created' => DateTimeFormat::utcNow()
                                        ], true);
                                }
 
@@ -159,11 +164,12 @@ class OEmbed
                return $j;
        }
 
-       private static function formatObject($j)
+       private static function formatObject(stdClass $j)
        {
                $embedurl = $j->embedurl;
                $jhtml = $j->html;
                $ret = '<div class="oembed ' . $j->type . '">';
+
                switch ($j->type) {
                        case "video":
                                if (isset($j->thumbnail_url)) {
@@ -311,7 +317,7 @@ class OEmbed
 
                $allowed = explode(',', $str_allowed);
 
-               return allowed_domain($domain, $allowed);
+               return Network::isDomainAllowed($domain, $allowed);
        }
 
        public static function getHTML($url, $title = null)
@@ -322,7 +328,7 @@ class OEmbed
 
                $o = self::fetchURL($url, !self::isAllowedURL($url));
 
-               if (!is_object($o) || $o->type == 'error') {
+               if (!is_object($o) || property_exists($o, 'type') && $o->type == 'error') {
                        throw new Exception('OEmbed failed for URL: ' . $url);
                }