From 638589d254bdd3f2f6cd639ff47a6ca4fe03631d Mon Sep 17 00:00:00 2001 From: Savetheinternet Date: Wed, 6 Apr 2011 19:18:36 +1000 Subject: [PATCH] Wordfilters --- inc/config.php | 11 +++++- inc/functions.php | 87 ++++++++++++++++++++++++++++++++++------------- post.php | 2 ++ 3 files changed, 76 insertions(+), 24 deletions(-) diff --git a/inc/config.php b/inc/config.php index 6a9b8fd7..b67ddb10 100644 --- a/inc/config.php +++ b/inc/config.php @@ -20,7 +20,8 @@ 'dir' => Array(), 'mod' => Array(), 'spam' => Array(), - 'flood_filters' => Array() + 'flood_filters' => Array(), + 'wordfilters' => Array() ); // Database stuff @@ -447,6 +448,14 @@ // 'message' => 'Your post has been rejected on the suspicion of a flood attack on this board (too many new threads); post a reply instead.' //); + // Wordfilters are used to automatically replace certain words/phrases with something else. + + // For a normal string replacement: + // $config['wordfilters'][] = Array('cat', 'dog'); + + // Advanced raplcement (regular expressions): + // $config['wordfilters'][] = Array('/cat/', 'dog', true); // 'true' means it's a regular expression + // A small file in the main directory indicating that the script has been ran and the board(s) have been generated. // This keeps the script from querying the database and causing strain when not needed. $config['has_installed'] = '.installed'; diff --git a/inc/functions.php b/inc/functions.php index ed8908cf..f54352e9 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -906,7 +906,19 @@ $ipoc = explode('.', $ip); return $ipoc[3] . '.' . $ipoc[2] . '.' . $ipoc[1] . '.' . $ipoc[0]; } - + + function wordfilters(&$body) { + global $config; + + foreach($config['wordfilters'] as $filter) { + if(isset($filter[2]) && $filter[2]) { + $body = preg_replace($filter[0], $filter[1], $body); + } else { + $body = str_replace($filter[0], $filter[1], $body); + } + } + } + function markup(&$body) { global $board, $config; @@ -938,43 +950,72 @@ } // Cites - if(isset($board) && preg_match_all('/(^|\s)>>([0-9]+?)(\s|$)/', $body, $cites)) { - $previousPosition = 0; - $temp = ''; + if(isset($board) && preg_match_all('/(^|\s)>>(\d+?)(\s|$)/', $body, $cites)) { + if(count($cites[0]) > $config['max_cites']) { + error($config['error']['toomanycites']); + } sql_open(); for($index=0;$indexbindValue(':id', $cite); $query->execute() or error(db_error($query)); if($post = $query->fetch()) { - $replacement = '>>' . $cite . ''; - } else { - $replacement = ">>{$cite}"; + $replacement = '' . + '>>' . $cite . + ''; + $body = str_replace($cites[0][$index], $cites[1][$index] . $replacement . $cites[3][$index], $body); } - - // Find the position of the cite - $position = strpos($body, $cites[0][$index]); + } + } + + // Cross-board linking + if(preg_match_all('/(^|\s)>>>\/(\w+?)\/(\d+)?(\s|$)/', $body, $cites)) { + if(count($cites[0]) > $config['max_cites']) { + error($config['error']['toomanycross']); + } + sql_open(); + for($index=0;$indexbindValue(':id', $cite); + $query->execute() or error(db_error($query)); + + if($post = $query->fetch()) { + $replacement = '' . + '>>>/' . $_board . '/' . $cite . + ''; + $body = str_replace($cites[0][$index], $cites[1][$index] . $replacement . $cites[4][$index], $body); + } + } else { + $replacement = '' . + '>>>/' . $_board . '/' . + ''; + $body = str_replace($cites[0][$index], $cites[1][$index] . $replacement . $cites[4][$index], $body); + } + var_dump($post); + } - // Replace the found string with "xxxx[...]". (allows duplicate tags). Keeps whitespace. - $body = substr_replace($body, str_repeat('x', strlen($cites[0][$index]) - $whitespace[0] - $whitespace[1]), $position + $whitespace[0], strlen($cites[0][$index]) - $whitespace[0] - $whitespace[1]); + //var_dump($cite); + // exit; - $temp .= substr($body, $previousPosition, $position-$previousPosition) . $cites[1][$index] . $replacement . $cites[3][$index]; - $previousPosition = $position+strlen($cites[0][$index]); + // Restore main board settings + openBoard($tmp_board); } - - // The rest - $temp .= substr($body, $previousPosition); - - $body = $temp; } + $body = str_replace("\r", '', $body); diff --git a/post.php b/post.php index 88bdf3ae..6e971ff1 100644 --- a/post.php +++ b/post.php @@ -294,6 +294,8 @@ if($post['mod_tag']) $post['trip'] .= ' ## ' . $post['mod_tag'] . ''; + wordfilters($post['body']); + $post['body_nomarkup'] = $post['body']; if(!($mod && $post['raw']))