<?

class BBcodesCallback {
	static function LinksCheckUSER($arr)
	{
		$us = user::data($arr[1]);
		if ($us['id']) {
			return user::nick($arr[1],7);
		}
		return '<span class="user_nick_err">@'.$arr[1].'</span>';		
	}
	static function LinksCheck($arr)
	{
		$arr[1] = self::Links($arr[1]);	
		if ($arr[1] == 'ErrorLINK' || $arr[1] == 'ErrorLINKtWO'){
			return __('Ссылка повреждена '.$arr[1].'');
		}
		$LinkRe = str_replace(array('http://','www.','https://'),null, $arr[2]);
		$title = $arr[2];	
		$LinkRe = FileSystem::TextCut($LinkRe ,30);
		$lchec = FileSystem::TextStrlen($arr[1]) > 1 ? $arr[1] : $arr[2];
		$pos = mb_substr($lchec, 0, 1);
		
		if (preg_match('#^http://'.preg_quote($_SERVER['HTTP_HOST']).'#',$lchec) || $pos == '/') {
			if (FileSystem::TextStrlen($arr[1]) > 1) {
					$title = $arr[1];	
					return ' <a class="bb_link_2" href="'.$arr[1].'" title="'.$title.'">'. $LinkRe .'</a> ';
				} else {
					return $arr[1].' <a class="bb_link_2" title="'.$title.'"  href="'.$arr[2].'">'. $LinkRe .'</a>'. $arr[4];
				}
			} else 	{
			return ' <a target="_blank" class="bb_link" title="'.$title.'" href="//'. $_SERVER['HTTP_HOST'] .'/main/link/'.base64_encode(html_entity_decode($arr[2])).'">'. $LinkRe .'</a> ';
		}
	}
	static function Links($link)
	{
		$out = $link;
		$link = str_replace(PHP_EOL, null, $link);
		if (preg_match('~^ *(javascript|data)~i', $link)) {
			$out = 'ErrorLINK';
		}
		$out = filter_var($out, FILTER_SANITIZE_URL);
		return $out;		
	}	
}