From d63dea93c52cab59b926e12daa2de36cdb1d4e3c Mon Sep 17 00:00:00 2001 From: 8chan Date: Wed, 24 Dec 2014 06:14:04 -0800 Subject: [PATCH] Remove links from ban list --- inc/bans.php | 11 +++++++++++ tools/hide_bans_links.php | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 tools/hide_bans_links.php diff --git a/inc/bans.php b/inc/bans.php index 215ff279..fed34938 100644 --- a/inc/bans.php +++ b/inc/bans.php @@ -322,6 +322,17 @@ class Bans { if ($post) { $post['board'] = $board['uri']; + $match_urls = '(?xi)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))'; + + $matched = array(); + + preg_match_all("#$match_urls#im", $post['body_nomarkup'], $matched); + + if (isset($matched[0]) && $matched[0]) { + $post['body'] = str_replace($matched[0], '###Link-Removed###', $post['body']); + $post['body_nomarkup'] = str_replace($matched[0], '###Link-Removed###', $post['body_nomarkup']); + } + $query->bindValue(':post', json_encode($post)); } else $query->bindValue(':post', null, PDO::PARAM_NULL); diff --git a/tools/hide_bans_links.php b/tools/hide_bans_links.php new file mode 100644 index 00000000..5a8e02a6 --- /dev/null +++ b/tools/hide_bans_links.php @@ -0,0 +1,34 @@ +execute() or error(db_error($query)); +$num_bans = $query->rowCount(); +$iter = 0; + +while ($ban = $query->fetch(PDO::FETCH_ASSOC)) { + $iter++; + + if (!$ban['post']) + continue; + + $match_urls = '(?xi)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))'; + + $matched = array(); + + $post = json_decode($ban['post']); + + preg_match_all("#$match_urls#im", $post->body_nomarkup, $matched); + + if (!isset($matched[0]) || !$matched[0]) + continue; + + $post->body = str_replace($matched[0], '###Link-Removed###', $post->body); + $post->body_nomarkup = str_replace($matched[0], '###Link-Removed###', $post->body_nomarkup); + + $update = prepare('UPDATE ``bans`` SET `post` = :post WHERE `id` = :id'); + $update->bindValue(':post', json_encode($post)); + $update->bindValue(':id', $ban['id']); + $update->execute() or error(db_error($update)); + echo "Processed $iter/$num_bans\n"; +}