Selaa lähdekoodia

#1489 Not absolute URLs aren't parsed correctly in bx_get_site_info function

Alex Trofimov 8 vuotta sitten
vanhempi
sitoutus
12cc61bb33
1 muutettua tiedostoa jossa 23 lisäystä ja 0 poistoa
  1. 23 0
      inc/utils.inc.php

+ 23 - 0
inc/utils.inc.php

@@ -940,6 +940,10 @@ function bx_get_site_info($sSourceUrl, $aProcessAdditionalTags = array())
                     isset($a['content_attr']) ? $a['content_attr'] : 'content', 
                     $sCharset,
                     isset($a['specialchars_decode']) ? $a['specialchars_decode'] : true); 
+
+                if ((isset($a['name']) && 'og:image' == $a['name']) || (isset($a['tag']) && 'link' == $a['tag'])) {
+                    $aResult[$k] = bx_get_site_info_fix_relative_url ($sSourceUrl, $aResult[$k]);
+                }
             }
 
         }
@@ -948,6 +952,25 @@ function bx_get_site_info($sSourceUrl, $aProcessAdditionalTags = array())
     return $aResult;
 }
 
+/**
+ * Fix relative URL to make it absolute
+ * @param $sSourceUrl main URL
+ * @param $s URL to fix
+ * @return absolute URL or URL wothout changes
+ */ 
+function bx_get_site_info_fix_relative_url ($sSourceUrl, $s)
+{
+    if (0 === stripos($s, 'http://') || 0 === stripos($s, 'https://') || 0 === stripos($s, 'ftp://') || !$s)
+        return $s;
+
+    if ('/' == $s[0]) {
+        $a = parse_url($sSourceUrl);
+        return $a['scheme'] . '://' . $a['host'] . (empty($a['port']) || 80 == $a['port'] || 443 == $a['port'] ? '' : ':' . $a['port']) . $s;
+    }
+
+    return $sSourceUrl . ('/' === substr($sSourceUrl, -1) ? $s : '/' . $s);
+}
+
 function bx_parse_html_tag ($sContent, $sTag, $sAttrNameName, $sAttrNameValue, $sAttrContentName, $sCharset = false, $bSpecialCharsDecode = true)
 {
     if (!preg_match("/<{$sTag}\s+{$sAttrNameName}[='\" ]+{$sAttrNameValue}['\"]\s+{$sAttrContentName}[='\" ]+([^'>\"]*)['\"][^>]*>/i", $sContent, $aMatch) || !isset($aMatch[1]))