Code: Alles auswählen
<?php
* Make sure this file has not already been included.
*/
if (!defined('INCLUDES_PRIME_LINKS'))
{
define('INCLUDES_PRIME_LINKS', true);
// Options
define('ALL_SUBDOMAINS_INTERNAL', true);
define('ANONYMIZE_EXTERNAL_LINKS', 'http://anonym.to?');
// Link relationships
define('INTERNAL_LINK_REL', '');
define('EXTERNAL_LINK_REL', 'nofollow');
// Link targets
define('INTERNAL_LINK_TARGET', '');
define('EXTERNAL_LINK_TARGET', '_blank');
// Link classes
define('INTERNAL_LINK_CLASS', 'postlink-local');
define('EXTERNAL_LINK_CLASS', '');
/**
* Decodes all HTML entities (html_entity_decode() doesn't decode numberical entities).
*/
function decode_entities($text)
{
$text = html_entity_decode($text, ENT_QUOTES, 'ISO-8859-1'); //UTF-8 does not work!
$text = preg_replace('/&#(\d+);/me', 'chr($1)', $text); //decimal notation
$text = preg_replace('/&#x([a-f0-9]+);/mei', 'chr(0x$1)', $text); //hex notation
return($text);
}
/**
* Removes subdomains from a URL.
*/
function remove_subdomains($url)
{
$url = preg_replace('#^(http|https)://[^/]+?\.((?:[a-z0-9-]+\.[a-z]+)|localhost/)#i', '$1://$2', $url);
return($url);
}
/**
* Determines if a URL is local or external.
*/
function is_link_local($url, $board_url = false)
{
if ($board_url === false)
{
$board_url = generate_board_url(true);
$board_url = ALL_SUBDOMAINS_INTERNAL ? remove_subdomains($board_url) : $board_url;
}
$is_local = (strpos($url, $board_url) === 0);
if (!$is_local)
{
$protocol = substr($url, 0, strpos($url, ':'));
$is_local = !$protocol || ($protocol && !in_array($protocol, array('http', 'https', 'mailto', 'ftp', 'gopher')));
}
return($is_local);
}
/**
*/
function prime_links($message)
{
// A quick check before we start using regular expressions
if (strpos($message, '<a ') === false)
{
return($message);
}
$board_url = generate_board_url(true);
$board_url = ALL_SUBDOMAINS_INTERNAL ? remove_subdomains($board_url) : $board_url;
preg_match_all('/(<a[^>]+?>)/i', $message, $matches, PREG_SET_ORDER);
foreach ($matches as $links)
{
$is_local = false;
$link = $new_link = $links[0];
$href = preg_replace('/^.*href="([^"]*)".*$/i', '$1', $link);
if ($href == $link) //no link was found
{
continue;
}
$href = decode_entities($href);
$href = ALL_SUBDOMAINS_INTERNAL ? remove_subdomains($href) : $href;
$is_local = is_link_local($href, $board_url);
$new_class = $is_local ? INTERNAL_LINK_CLASS : EXTERNAL_LINK_CLASS;
$new_target = $is_local ? INTERNAL_LINK_TARGET : EXTERNAL_LINK_TARGET;
$new_rel = $is_local ? INTERNAL_LINK_REL : EXTERNAL_LINK_REL;
if ($new_class)
{
$class = preg_replace('/^.*class="([^"]*)".*$/i', '$1', $link);
$class = $class == $link ? '' : $class; // Was class found?
$old_class = $class == 'postlink' ? '' : $class;
$new_class = trim(implode(' ', array_unique(array_merge(explode(' ', $old_class), (array)$new_class)))); // This will append the new class instead of replacing the existing one.
$new_link = $class ? str_replace("class=\"$class\"", "class=\"$new_class\"", $new_link) : str_replace('>', " class=\"$new_class\">", $new_link);
}
if ($new_target)
{
$click = preg_replace('/^.*onclick="([^"]*)".*$/i', '$1', $link);
$click = $click == $link ? '' : $click; // Was onclick found?
$new_click = "this.target='$new_target';";
$new_link = $click ? str_replace("onclick=\"$click\"", "onclick=\"$new_click\"", $new_link) : str_replace('>', " onclick=\"$new_click\">", $new_link);
// The target attribute is not allowed in STRICT doctypes. If you want to use it anyway, just uncomment the following lines.
// $target = preg_replace('/^.*target="([^"]*)".*$/i', '$1', $link);
// $target = $target == $link ? '' : $target; // Was target found?
// $new_link = $target ? str_replace("target=\"$target\"", "target=\"$new_target\"", $new_link) : str_replace('>', " target=\"$new_target\">", $new_link);
}
if ($new_rel)
{
$rel = preg_replace('/^.*rel="([^"]*)".*$/i', '$1', $link);
$rel = $rel == $link ? '' : $rel; // Was rel found?
$new_rel = trim(implode(' ', array_unique(array_merge(explode(' ', $rel), (array)$new_rel)))); // This will append the new rel instead of replacing the existing one.
$new_link = $rel ? str_replace("rel=\"$rel\"", "rel=\"$new_rel\"", $new_link) : str_replace('>', " rel=\"$new_rel\">", $new_link);
}
if (!$is_local && ANONYMIZE_EXTERNAL_LINKS)
{
$new_link = str_replace('href="', 'href="' . ANONYMIZE_EXTERNAL_LINKS, $new_link);
}
$searches[] = $link;
$replacements[] = $new_link;
}
$message = str_replace($searches, $replacements, $message);
return($message);
}
}
?>