/** * Format and hyperlink mentions * * @param string $p_text The text to process. * @param bool $p_html true for html, false otherwise. * @return string The processed text. */ function mention_format_text($p_text, $p_html = true) { $t_mentioned_users = mention_get_users($p_text); if (empty($t_mentioned_users)) { return $p_text; } $t_mentions_tag = mentions_tag(); $t_formatted_mentions = array(); foreach ($t_mentioned_users as $t_username => $t_user_id) { $t_mention = $t_mentions_tag . $t_username; $t_mention_formatted = $t_mention; if ($p_html) { $t_mention_formatted = string_display_line($t_mention_formatted); $t_mention_formatted = '<a class="user" href="' . string_sanitize_url('view_user_page.php?id=' . $t_user_id, true) . '">' . $t_mention_formatted . '</a>'; if (!user_is_enabled($t_user_id)) { $t_mention_formatted = '<s>' . $t_mention_formatted . '</s>'; } $t_mention_formatted = '<span class="mention">' . $t_mention_formatted . '</span>'; } $t_formatted_mentions[$t_mention] = $t_mention_formatted; } # Replace the mentions, ignoring existing anchor tags (otherwise # previously set mailto links would be processed as mentions, # corrupting the output $t_text = string_process_exclude_anchors($p_text, function ($p_string) use($t_formatted_mentions) { return str_replace(array_keys($t_formatted_mentions), array_values($t_formatted_mentions), $p_string); }); return $t_text; }
/** * Search email addresses and URLs for a few common protocols in the given * string, and replace occurences with href anchors. * @param string $p_string String to be processed. * @return string */ function string_insert_hrefs($p_string) { static $s_url_regex = null; static $s_email_regex = null; if (!config_get('html_make_links')) { return $p_string; } # Initialize static variables if (is_null($s_url_regex)) { # URL protocol. The regex accepts a small subset from the list of valid # IANA permanent and provisional schemes defined in # http://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml $t_url_protocol = '(?:https?|s?ftp|file|irc[6s]?|ssh|telnet|nntp|git|svn(?:\\+ssh)?|cvs):\\/\\/'; # %2A notation in url's $t_url_hex = '%[[:digit:]A-Fa-f]{2}'; # valid set of characters that may occur in url scheme. Note: - should be first (A-F != -AF). $t_url_valid_chars = '-_.,!~*\';\\/?%^\\\\:@&={\\|}+$#[:alnum:]\\pL'; $t_url_chars = "(?:{$t_url_hex}|[{$t_url_valid_chars}\\(\\)\\[\\]])"; $t_url_chars2 = "(?:{$t_url_hex}|[{$t_url_valid_chars}])"; $t_url_chars_in_brackets = "(?:{$t_url_hex}|[{$t_url_valid_chars}\\(\\)])"; $t_url_chars_in_parens = "(?:{$t_url_hex}|[{$t_url_valid_chars}\\[\\]])"; $t_url_part1 = $t_url_chars; $t_url_part2 = "(?:\\({$t_url_chars_in_parens}*\\)|\\[{$t_url_chars_in_brackets}*\\]|{$t_url_chars2})"; $s_url_regex = "/({$t_url_protocol}({$t_url_part1}*?{$t_url_part2}+))/su"; # e-mail regex $s_email_regex = substr_replace(email_regex_simple(), '(?:mailto:)?', 1, 0); } # Find any URL in a string and replace it with a clickable link $p_string = preg_replace_callback($s_url_regex, function ($p_match) { $t_url_href = 'href="' . rtrim($p_match[1], '.') . '"'; if (config_get('html_make_links') == LINKS_NEW_WINDOW) { $t_url_target = ' target="_blank"'; } else { $t_url_target = ''; } return "<a {$t_url_href}{$t_url_target}>{$p_match[1]}</a>"; }, $p_string); # Find any email addresses in the string and replace them with a clickable # mailto: link, making sure that we skip processing of any existing anchor # tags, to avoid parts of URLs such as https://user@example.com/ or # http://user:password@example.com/ to be not treated as an email. $p_string = string_process_exclude_anchors($p_string, function ($p_string) use($s_email_regex) { return preg_replace($s_email_regex, '<a href="mailto:\\0">\\0</a>', $p_string); }); return $p_string; }