Explorar o código

#1 - Add "nofollow" tag.

AntonLV %!s(int64=12) %!d(string=hai) anos
pai
achega
47989fcd8f
Modificáronse 1 ficheiros con 18 adicións e 5 borrados
  1. 18 5
      inc/classes/BxDolCmts.php

+ 18 - 5
inc/classes/BxDolCmts.php

@@ -840,14 +840,27 @@ class BxDolCmts extends BxDol implements iBxDolReplaceable
 
     protected function _prepareTextForOutput ($s)
     {
-        $sHttp = '';
+    	bx_import('BxDolTemplate');
+    	$oTemplate = BxDolTemplate::getInstance();
+
         $sPattern = "/((https?|ftp|news):\/\/)?([a-z]([a-z0-9\-]*\.)+(aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel|[a-z]{2})|(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))(\/[a-z0-9_\-\.~]+)*(\/([a-z0-9_\-\.]*)(\?[a-z0-9+_\-\.%=&]*)?)?(#[a-z][a-z0-9_]*)?/";
-        $aMatches = array();
-        if(preg_match($sPattern, $s, $aMatches) && empty($aMatches[1]))
-            $sHttp = 'http://';
+        $bAddNofollow = $this->_oQuery->getParam('sys_add_nofollow') == 'on';
 
         $s = bx_process_output($s, BX_DATA_HTML);
-        $s = preg_replace($sPattern, '<a href="' . $sHttp . '$0" target="_blank">$0</a>', $s);
+        $s = preg_replace_callback($sPattern, function($aMatches) use($oTemplate, $bAddNofollow) {
+        	$aLinkAttrs = array(
+        		array('key' => 'target', 'value' => '_blank')
+        	);
+	        if($bAddNofollow && strncmp(BX_DOL_URL_ROOT, $aMatches[0], strlen(BX_DOL_URL_ROOT)) != 0)
+	        	$aLinkAttrs[] = array('key' => 'rel', 'value' => 'nofollow');
+
+        	return $oTemplate->parsePageByName('bx_a.html', array(
+        		'href' => (empty($aMatches[1]) ? 'http://' : '') . $aMatches[0],
+        		'title' => $aMatches[0],
+        		'bx_repeat:attrs' => $aLinkAttrs,
+        		'content' => $aMatches[0],
+        	));
+        }, $s);
 
         return $s;
     }