<?php
  
FUNCTION LINKS_FILTER($url){
  
  $re = '/["*|><){},+!`(^%]/';
  $subst = NULL;

  RETURN PREG_REPLACE($re, $subst, $url);  
  
}
  
FUNCTION LINKS2($arr) {
  
  IF (PREG_MATCH('#^'.SCHEME.PREG_QUOTE(HTTP_HOST).'#', $arr[2]) || !PREG_MATCH('#://#', $arr[2])){
    
    RETURN '<a href="'.LINKS_FILTER($arr[1]).'">'.LINKS_FILTER($arr[2]).'</a>';
    
  }ELSE{
    
    RETURN '<a href="/?get=url&data='.BASE64_ENCODE(HTML_ENTITY_DECODE(LINKS_FILTER($arr[1]))).'">'.LINKS_FILTER($arr[2]).'</a>';
    
  }
  
}

FUNCTION LINKS3($arr)
{
  
  IF (PREG_MATCH('#^'.SCHEME.PREG_QUOTE(HTTP_HOST).'#', $arr[1])){
    
    RETURN '<a href="'.LINKS_FILTER($arr[1]).'">'.LINKS_FILTER($arr[1]).'</a>';
    
  }ELSE{
    
    RETURN '<a href="/?get=url&data='.BASE64_ENCODE(HTML_ENTITY_DECODE(LINKS_FILTER($arr[1]))).'">'.LINKS_FILTER($arr[1]).'</a>';
    
  }
  
}
  
FUNCTION LINKS($msg) {
  
  $msg = PREG_REPLACE_CALLBACK('/\[a href=((?!javascript:|data:|document.cookie).+)\](.+)\[\/a\]/isU', 'LINKS2', $msg);
  $msg = PREG_REPLACE_CALLBACK('/((?:http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(?:\/\S*)?)/', 'LINKS3', $msg);
  
  RETURN $msg;

}