diff --git a/inc/toolbox.php b/inc/toolbox.php index 70f45e9..d00ba37 100644 --- a/inc/toolbox.php +++ b/inc/toolbox.php @@ -273,11 +273,14 @@ class ToolBox //TODO implement link pattern public static function findReplaceLinks($sText, $sLinkPattern='') { + //Parameters + $sPreChar = '(^|\ |\*|\'|\()'; + $sPostChar = '(\ |\.|\:|\)|$)'; + $sServerPath = substr(str_replace(array('http://', 'https://'), '', $_GET['serv_name']), 0, -1); + //Fix Carriage Return / Line Feed OS issue $sText = self::fixEOL($sText); - $sServerPath = substr(str_replace(array('http://', 'https://'), '', $_GET['serv_name']), 0, -1); - //building all replacements possibilities foreach(Databap::$TYPES as $sType=>$asTypeInfo) { @@ -292,22 +295,25 @@ class ToolBox foreach($asReplacements as $sHash=>$sPage) { //Type + id - $asPatterns['`(^|\ )(https?://|)'.$sServerPath.'/?\#'.$sHash.'\-([\d]+)`mui'] = '$1$4'.$sPage.' $3'; + $asPatterns['`'.$sPreChar.'(https?://|)'.$sServerPath.'/?\#'.$sHash.'\-([\d]+)'.$sPostChar.'`mui'] = '$1$4'.$sPage.' $3'; //Type + phrase - if($sPage=='table') $asPatterns['`(^|\ )(https?://|)'.$sServerPath.'/?\#(?i)'.$sHash.'(?-i)\-([_A-Z0-9]+)`mu'] = '$1$4'.$sPage.' $3'; + if($sPage=='table') $asPatterns['`'.$sPreChar.'(https?://|)'.$sServerPath.'/?\#(?i)'.$sHash.'(?-i)\-([_A-Z0-9]+)'.$sPostChar.'`mu'] = '$1$4'.$sPage.' $3'; } $sText = preg_replace(array_keys($asPatterns), array_values($asPatterns), $sText); - //Phase 2: Identify remaining links (external) - $asPatterns = array('`((?:https?|ftp)://\S+[[:alnum:]]/?)`mui', '`((?$1 ', '$1'); - $sText = preg_replace($asPatterns, $asLinks, $sText); + //Pahse 2: Replacing directly all remaining links + //Phase 2a: Internal hashes + $asPatterns = array(); + foreach(Databap::$HASH_TO_PAGE as $sHash=>$sPage) $asPatterns['`'.$sPreChar.'(https?://|)'.$sServerPath.'/?\#(?i)'.$sHash.'(?-i)'.$sPostChar.'`mui'] = '$1'.Databap::$PAGE_TITLES[$sPage].'$3'; + + //Phase 2b: External links + $asPatterns['`((?:https?|ftp)://\S+[[:alnum:]]/?)`mui'] = '$1 '; + $asPatterns['`((?$1'; + $sText = preg_replace(array_keys($asPatterns), array_values($asPatterns), $sText); //Phase 3: Rebuild link $asPatterns = array(); - $sPreChar = '(^|\ |\*|\'|\()'; - $sPostChar = '(\ |\.|\:|\)|$)'; foreach($asReplacements as $sHash=>$sPage) { if(mb_strlen($sHash)>1)