PDA

View Full Version : Process post text replacements, abbreviations, fake BBCODE's


CarCdr
08-14-2004, 10:00 PM
Generalized Word or Text Replacer For User Inputs (Posts, etc.)
Version .2: Replace, remove, define abbreviations, define fake bbcodes.
Requires 1 small mod and adds 1 new function. No queries.

(Please, do not suggest that replacement variables can do this. See below.)

Allows an administrator to define the following sorts of replacements for posts, titles, and all other user input fields:

conservative => montague
Replace. Transform any word match, preserving case in the replacement. as in Conservative=>Montague, CONSERVATIVE=>MONTAGUE.

uglytext =>
Removal. Completely remove any occurrence. Like all others you define, can be specified to match word only or any occurrence.

.us => http://our.domain.com
Abbreviation. If defined as a word match, it would work for " .us " or " .us/subdir", but not for "him.us" or ".use".

=> [ size=4][ color=navy][ i]
=> [ /i][ /color][ /size]
Abbreviation. New BBCODES :cheeky:: Abbreviation for something more complicated. (Do not add the spaces after the open ['s. I used just to show these codes in this post.) Usage for these is as you'd expect:
Some Heading

Modifications Required
This hack requires 2 changes to one file, functions.php:
* 1 function modification (a few new lines)
* 1 new function

How It Works
The Admin adds replacement definitions using the same field as for censorship: AdminCP>vBulletin Options>Censorship Options. No change is require on the AdminCP side.

On the user code side, the function that now does censorship is modified to support these new replacements. Replacement will take place anywhere that normal censorship takes place.

Replacements should be defined in the AdminCP censorship field BEFORE plain censorship. See small attached image, where the first 5 lines are replacements and the rest of plain censorship.

Defining Replacements
Instead of simple censorship words or strings, rules are added to the censorship field in AdminCP in the same place that you currently add words to be censored. You add rules are of the form:

FIND_SPEC=REPLACEMENT

where REPLACEMENT is optionable.

There are two types of rules:
(1) string match
(2) word match.

his=her STRING MATCH, CASE INSENSITIVE. Find any occurrence of the characters "his"
and replace it with "her". The case of letters in the replacement ("her")
is used exactly as specified. This would match 3 times in in this fragment:
"His history was this."
{oldword}=newword WORD MATCH, CASE INSENSITIVE. Replace any occurrences of "oldword" with
"newword", using the replacement exactly as specified (it does not change
the case of any letters).
Preserving Upper Or Lower Case (using "=="):

Either type of match supports the feature of case preservation: matching the
case of the found text in the replacement text. Use "==" instead of "=" to
specify that case should be preserved. Example:

{oldword}==newwordSearch is case insensitive, but replacement preserves case. This would match
all occurrences of the word "oldword", regardless of case, but it would attempt to
preserve case in three instances: initial capital, all uppercase, and all
lowercase. Examples translations from the the above rule would be: Oldword => Newword
OLDWORD => NEWWORD
oldword => newword
A nuance here is that if the found text is not all lowercase, all uppercase,
or a single initial capital (e.g., OldWord), the the replacement text is
used exactly as specified in the rule. For example, the rule "{old}==New"
would use "New" as the replacement if "oLd" were matched.
oldword==newwordSame as above, but matches are string matches rather than word matches.
Removing Words or Text:
If no replacement text is specified, as in "uglychars= ", then the match is simply removed from the user's input. This is an alternative to the censorship replacement by *'s.

Abbreviations:
Another possible use is for abbreviations. For example, one could define the word ".us" as a shorthand for the site url by using the following rule:{.us}=http://www.our-domain.com which would match ".us" only as a word, as in ".us/forum" or "go to .us site.", but not "foo.us" or ".use".

Fake BBCODE: You can define an abbreviation that looks like a bbcode. This allows an administrator to define pseudo-bbcode's. You can also use bbcode's in the replacement. As you might expect, "{[red]}=REPLACEMENT" will match [red] as if it were a word and "[red]=REPLACEMENT" will match any occurrence.

Disallowed characters in replacements:
The following characters are not permitted in replacement text:
& ampersand, % percent, < less than, > greater than, ` backquote, \ backslash

Installation Instructions

1. You should include the text of the attached file (a new PHP function) in functions.php, just before the fetch_censored_text would be appropriate.

2. In the function fetch_censored_text in functions.php, find this line:
if (substr($censorword, 0, 2) == '\\{')
and add this BEFORE it (just the non-red text):
if (strpos($censorword, '=') !== false)
{
$text = fetch_censored_text_replacer($text, $censorword);
}
else if (substr($censorword, 0, 2) == '\\{')
Restricting Access/Testing:
You can set "$TEST_USERIDS" in the attached function to your own USERID for testing, so that this mod will not take affect for other users. Then, when you are satisfied and you want to release it, you can set this value back to empty by removing the userid that you added. I have set it to userid "1" (one) in the attachment.

This mod is beta as I have have only tested it on my board, and while I am satisfied with the results, I would not call the testing extensive.

Cautionary Notes

The vB implementation of censored words will match "com" when it is surrounded by any non-letter. Therefore, the censored word input "{com}" will match twice in this phrase, producing the result shown:

Well, 3com is not exactly a .com company.
translates to:
Well, 3*** is not exactly a .*** company.

This same matching implmentation is used by this mod in order to be compatible with vB.

Note that Admin's need to be careful about using translations that will match common words or strings. Remember that translation will take place everywhere that censorship takes place, including of course user profiles and signatures.

Editpost Bug -- Bad Preview

editpost.php does not display previews properly when bbcode abbreviations are used. In editpost.php, censorship is done after bbcode parsing, unlike newthread.php and newreply.php, where previewing works fine for bbcode abbreviations. BTW, the same bug is why URL's are not processed in the preview when you edit a post. I entered vB bugid: 3301 (http://www.vbulletin.com/forum/bugs.php?do=view&bugid=3301).

A note concerning replacement variables
Replacement variables do not have the same flexibility as this mod. Also, I believe they are style specific. More importantly, replacement variables do replacements on ALL output sent to the user, which is often very dangerous. If you defined a replacement variable for "shat", it would replace it everywhere, including in www.shattered.com and HTML and javascript and style sheets... well you get the idea.

Cheers, C.C

???`S?LV?R???`
08-15-2004, 09:25 PM
interesting addition

CarCdr
08-16-2004, 11:01 AM
Something struck me last night that I forgot to mention. Since the things in the censorship field are processed in the order they are defined, all replacements should be defined before all plain censorship.

This ensures that the final version of the post, title, etc. is subjected to ordinary censorship. Attachment shows
AdminCP>vBulletin Options>Censorship Options field with censorship defined AFTER replacements (the first 5 lines). Cheers

JohnBee
11-24-2004, 04:48 PM
*** RESOLVED

I missplaced one of the replacement variables.

im not 100% on the userid thing, will it it work?

I added the following to the censorship {++++}==sh*t
but it doesnt do anything, or did I miss something.

Bison
11-27-2004, 05:07 AM
Very nice!
I've been looking for someone to create this hack!

Bison
12-06-2004, 02:42 AM
Time to move this one out of beta ... it workd great!

Trillian
02-26-2005, 11:52 PM
Thanks! This is just what I needed!

Brains
03-11-2005, 05:28 PM
Very nice.. any thoughts on really hacking it up to allow spaces in both the search and replace text? It'd be nice to replace on phrases as well.

pimpery
03-12-2005, 12:00 AM
this is the worst hack, in the history of mankind. ever hear of preg_match... lol

eva01_
03-12-2005, 12:41 AM
wow why insult someone elses hack like that?

pimpery
03-12-2005, 01:45 AM
wow why insult someone elses hack like that?
because a simple preg_match would do this whole hack, and so much more.

MrToasty
03-14-2005, 10:14 AM
less words, more code... preg_match me...

granpa yum
05-04-2005, 03:31 AM
this wont work for me, when i do it i get the error "
Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING in /public_html/forums/includes/functions.php on line 2119"

any ideas

psoexplorer
05-04-2005, 02:10 PM
Nice little hack! How do I change whole phrases though?

CarCdr
06-17-2005, 10:50 AM
this is the worst hack, in the history of mankind. ever hear of preg_match... lol
LOL.

husain
07-18-2005, 03:36 PM
This is exactly what I was looking for. Is it still in beta?

Bison
07-23-2006, 05:18 PM
It would be nice is one of you Smart Guys could update this hack to work with vB 3.5

jeepinator
03-02-2007, 09:28 PM
^ Yes please.

tokosan
03-14-2007, 01:50 AM
cool man, thanks for makin this much needed mod.

i'm going to get my friend, who knows what a computer is, to install this when he gets on.

vgevolution
10-29-2007, 04:18 AM
Just applied this hack in 3.6.8. So far, it seems to work fine. It would be nice to be able to replace using spaces as well, but that could be a tough one. Perhaps the built-in filter will be upgraded someday?