Alex Trofimov 11 лет назад
Родитель
Сommit
ea2f2201de
2 измененных файлов с 55 добавлено и 9 удалено
  1. 50 0
      inc/utils.inc.php
  2. 5 9
      modules/boonex/timeline/classes/BxTimelineModule.php

+ 50 - 0
inc/utils.inc.php

@@ -780,6 +780,56 @@ function bx_file_get_contents($sFileUrl, $aParams = array(), $bChangeTimeout = f
     return $sResult;
 }
 
+function bx_get_site_info($sSourceUrl, $aProcessAdditionalTags = array())
+{
+    $aResult = array();
+    $sContent = bx_file_get_contents($sSourceUrl);
+
+    if ($sContent) {
+        $sCharset = '';
+        preg_match("/<meta.+charset=([A-Za-z0-9-]+).+>/i", $sContent, $aMatch);
+        if (isset($aMatch[1]))
+            $sCharset = $aMatch[1];
+
+        if (preg_match("/<title.*>(.*)<\/title>/i", $sContent, $aMatch))
+            $aResult['title'] = $aMatch[1];
+        else
+            $aResult['title'] = parse_url($sSourceUrl, PHP_URL_HOST);
+
+        $aResult['description'] = bx_parse_html_tag($sContent, 'meta', 'name', 'description', 'content', $sCharset);
+        $aResult['keywords'] = bx_parse_html_tag($sContent, 'meta', 'name', 'keywords', 'content', $sCharset);
+
+        if ($aProcessAdditionalTags) {
+
+            foreach ($aProcessAdditionalTags as $k => $a) {
+                $aResult[$k] = bx_parse_html_tag(
+                    $sContent, 
+                    isset($a['tag']) ? $a['tag'] : 'meta', 
+                    isset($a['name_attr']) ? $a['name_attr'] : 'itemprop', 
+                    isset($a['name']) ? $a['name'] : $k, 
+                    isset($a['content_attr']) ? $a['content_attr'] : 'content', 
+                    $sCharset); 
+            }
+
+        }
+    }
+
+    return $aResult;
+}
+
+function bx_parse_html_tag ($sContent, $sTag, $sAttrNameName, $sAttrNameValue, $sAttrContentName, $sCharset = false)
+{
+    if (!preg_match("/<{$sTag}\s+{$sAttrNameName}[='\" ]+{$sAttrNameValue}['\"]\s+{$sAttrContentName}[='\" ]+([^<]*)['\"][\/\s]*>/i", $sContent, $aMatch) || !isset($aMatch[1]))
+        preg_match("/<{$sTag}\s+{$sAttrContentName}[='\" ]+([^<]*)['\"]\s+{$sAttrNameName}[='\" ]+{$sAttrNameValue}['\"][\/\s]*>/i", $sContent, $aMatch);
+
+    $s = isset($aMatch[1]) ? $aMatch[1] : '';
+
+    if ($s && $sCharset)
+        $s = mb_convert_encoding($s, 'UTF-8', $sCharset);
+
+    return $s;
+}
+
 // calculation ini_get('upload_max_filesize') in bytes as example
 function return_bytes($val)
 {

+ 5 - 9
modules/boonex/timeline/classes/BxTimelineModule.php

@@ -447,21 +447,17 @@ class BxTimelineModule extends BxBaseModNotificationsModule
         $oForm->initChecker();
         if($oForm->isSubmittedAndValid()) {
             $sLink = $oForm->getCleanValue('url');
-            $sLinkContent = bx_file_get_contents($sLink);
 
             $aMatches = array();
             preg_match($this->_oConfig->getPregPattern('url'), $sLink, $aMatches);
             $sLink = (empty($aMatches[2]) ? 'http://' : '') . $aMatches[0];
 
-            $aMatches = array();
-            preg_match($this->_oConfig->getPregPattern('meta_title'), $sLinkContent, $aMatches);
-            $sLinkTitle = !empty($aMatches[1]) ? $aMatches[1] : $sLink;
-
-            $aMatches = array();
-            preg_match($this->_oConfig->getPregPattern('meta_description'), $sLinkContent, $aMatches);
-            $sLinkDescription = !empty($aMatches[1]) ? $aMatches[1] : '';
+            $aSiteInfo = bx_get_site_info($sLink, array(
+                'thumbnailUrl' => array('tag' => 'link', 'content_attr' => 'href'),
+                'OGImage' => array('name_attr' => 'property', 'name' => 'og:image'),
+            ));
 
-            $iId = (int)$oForm->insert(array('profile_id' => $iUserId, 'url' => $sLink, 'title' => $sLinkTitle, 'text' => $sLinkDescription, 'added' => time()));
+            $iId = (int)$oForm->insert(array('profile_id' => $iUserId, 'url' => $sLink, 'title' => $aSiteInfo['title'], 'text' => $aSiteInfo['description'], 'added' => time()));
             if(!empty($iId))
                 return array('item' => $this->_oTemplate->getAttachLinkItem($iUserId, $iId));