Skip to content

Instantly share code, notes, and snippets.

@tablatronix
Created December 29, 2017 15:01
Show Gist options
  • Save tablatronix/d6217a67ca4e685e0113172cf749d734 to your computer and use it in GitHub Desktop.
Save tablatronix/d6217a67ca4e685e0113172cf749d734 to your computer and use it in GitHub Desktop.
mybb nofollow plugin fixed
<?php
// This plugin is licensed under the GNU GPLv3 license
if(!defined("IN_MYBB"))
{
die("Direct initialization of this file is not allowed.");
}
$plugins->add_hook("parse_message_end", "AddNoFollow_hook");
function AddNoFollow_info()
{
// Inspired by http://mods.mybb.com/view/nofollowexternal
return array(
"name" => "AddNoFollow",
"description" => "This is a simple plugin that puts rel=\"nofollow\" in external links",
"website" => "",
"author" => "Nixtren",
"authorsite" => "https://github.com/Nixtren",
"version" => "0.0.1",
"guid" => "",
"compatibility" => "18*"
);
}
function AddNoFollow_hook($message)
{
global $mybb;
// Thanks to https://stackoverflow.com/questions/14573553/php-file-get-contents-replace-all-urls-in-all-a-href-links
$match = array();
$links = array();
preg_match_all('!https?://[\S]+!', $message, $match);
foreach ($match as $key => $value) foreach ($value as $key2 => $TheUrl) $links[] = $TheUrl;
for ($i = 0; $i < count($links); $i++)
{
$linksurl = parse_url($links[$i]);
$bburl = $mybb->settings['bburl'];
if($linksurl['host'] != parse_url($bburl['host']))
{
$message = str_replace("<a href=\"{$links[$i]}", "<a rel=\"nofollow\" href=\"{$links[$i]}", $message);
}
}
return $message;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment