Log in

View Full Version : BBCode that calls PHP


mudpyr8
03-25-2004, 04:18 PM
I'm going to convert my dice hack over from phpbb to vB. It's a very comprehensive dice mod that has been very useful to me (and well tested).

Is there any way to use the BBCode Manager in the admincp to introduce a new code that calls some php function introduced in an include file?

I have the phpfunction for the dice roller that returns BB Code formatted text. If I could just put that function in an include file and then create a custom code in the BB Code Manager that calls it I would be done.

If this isn't possible, I think it would be an excellent feature.

Could I possibly put the function in a template of some kind and then reference the template somehow?

Thanks.

Velocd
03-25-2004, 05:03 PM
What's the syntax for this dice bbcode?

Would it be [dice] ?

The quickest way to achieving just the above would be adding a str_replace() in the parse_bbcode function, like:


function parse_bbcode($bbcode, $forumid = 0, $allowsmilie = 1, $isimgcheck = 0, $parsedtext = '', $parsedhasimages = 0, $iswysiwyg = 0)
{
// $parsedtext contains text that has already been turned into HTML and just needs images checking
// $parsedhasimages specifies if the text has images in that need parsing

global $vboptions, $parsed_postcache;

$donl2br = 1;

$bbcode = str_replace('[dice]', dice_function_that_returns_string(), $bbcode); // look for dice

mudpyr8
03-25-2004, 05:10 PM
That definitely looks to be the simplest.

What would I pass to dice_function_that_returns_string() so that the {option} and {param} data gets sent?

Also, would I then just put my code in a file named 'functions_diceroller.php' in the includes directory?

Playing around I just created the following in BB Code Manager:

tag: roll
replacement: <? dieroller({param},{option}); ?>

Velocd
03-25-2004, 05:15 PM
How does the dice bbcode look?

My above code only works if it's just [dice], and that function would simply return a random value. No AdminCP implementation required.

It's not meant for parameters, but it seems your dice tag might look like {some value?}

mudpyr8
03-25-2004, 05:25 PM
Correct. Examples:

5d6

Returns (1 STUN/pt, 0 BODY for 1s, 1 BODY for 2-5, 2 BODY for 6):

5 + 1 + 2 + 6 + 6
STUN = 20
BODY = 6


5d6

Returns (open ended and pick the highest single result)

5 + 1 + 2 + 10 (6 + 4) + 22 (6 + 6 + 6 + 4) = 22

But

5d6

Returns:

5 + 1 + 2 + 6 + 6 = 20


There are of course limits so someone can't write 100000d10000000.

Velocd
03-25-2004, 05:55 PM
Give me some time, and I'll code up a hack for your original request in post #1. (probably tonight, or tomorrow night)

An AdminCP integration with custom function support in bbcode tags would be easier to use for your hack then hard-coding the bbcodeparse file.

mudpyr8
03-25-2004, 06:39 PM
That would rock on toast and be very useful for many other things.

Thanks. I look forward to the hack, and I am in no rush.

Velocd
03-26-2004, 04:10 AM
This hack is underway, and works in the following.

In the ACP, there is a new category called BB Code Functions in the left navbar, underneath Custom BB Codes.

The administrator can add functions to be used in bbcode that are user-defined or internal (built in PHP functions, like rand(), str_replace(), and hundreds of others).

Functions are added by specifying a title for the function, the handle of the function (which is how the function is called), a description, and optional file path to a PHP file containing a user defined function.

If no path is specified to a custom PHP file containing your function, the script will check to see if the handle name is an internal function. If it isn't, an error is outputted.

If the path specified points to an empty file, OR non PHP file, OR a correct PHP file but it doesn't contain the function by the handle name, OR the correct function is present but incorrect number of arguments (there must be AT LEAST 1 argument), then an error will be outputted.

Once a function is added, you can edit a bbcode in the bbcode manager, and there is a drop-down list for selection a function the bbcode applies to. There can be a max of 2 user-defined arguments passed in any function, and those are the normal bbcode arguments: {param} and {option}. The bbcode tag itself doesn't have to have the same name as the function handle.

An example of the whole script:

1. User creates a bbcode function that points to the internal function rand()
2. User create a bbcode that is applied to the function rand, one that takes 2 paramters: {param} and {option}.

3.

Usage: 200
Masked: {param}
PHP: rand({option}, {param});

This whole tag when used inside a post would return a random number between 100-200, and uses the PHP function rand() to perform the operation.

4. If by chance you create a tag for the function rand, but with one argument, which would have to be {param}, then you may or may not be lucky about getting a PHP error.

By default the hack substitutes the value 0 for any empty parameter of a function not specified by the user.

In the case with rand(), one is lucky, because {option} which is the left out parameter is replaced with 0 and still functions.

There are some other loop holes with this hack I've yet to address, but will give more thought into solutions as I work on the hack tomorrow (hopefully finishing by saturday). ;)

PlenoJure
03-26-2004, 10:07 AM
Very interesting idea, if the security issues can be properly addressed then this could be a very powerfull feature. Issues that come to mind are things things like intentially calling a function with invalid paramaters to cause a parse error & the ability to by inject PHP code. I don't know how you are handling the details, but security will be a very important issue with this. If this can be done securly it's something I'd use, it's a great idea.

mudpyr8
03-26-2004, 02:08 PM
Thank you VeloCD. This is a hack I have been thinking about for a while to facilitate a number of other hacks I have in mind (besides dice rollers). I was always frustrated with the amount of effort I needed to exert to put a hack in for custom bbcodes. This solves it in spades, and is a boon for the entire community, not just my dice rolling hack.

BIG THUMBS UP :up: :banana: :up:

Velocd
03-27-2004, 11:18 PM
Hmm, a problem I've come in contact with this hack is whether to make the functions return a value relative to the first time they're called, or upon every page refresh.

For example, I'll use the code from my last post:

Usage: 200
Masked: {param}
PHP: rand({option}, {param});

Because vBulletin retains BBCode tags in posts, and doesn't substitute them with hard-coded values (which absolutely makes sense), this would mean upon page refresh a function is called everytime.

In the case of functions that return non-static values, like rand(), you're going to get a new random number everytime you refresh the page.

I'm not sure if your hack will be affected by this mudpyr8 in a negative way, but let me know your thoughts.

I could hard-code the values for specific functions, making the first generated return value replaced with its tag, and upon page refresh it'll never update.

This would also be a tad more efficient since you aren't calling custom functions over and over again upon page refresh, but the downside is if you were to ever change the bbcode tag or bbcode function from the AdminCP, the previous instances that have already been posted wouldn't be updated. Although, whether that is a huge concern.. I wouldn't think so.

I'll probably add an option for the administrator to decide how they want it.

intentially calling a function with invalid paramaters to cause a parse error & the ability to by inject PHP code

A user can only pass a string as a argument (either {param} or {option}) and nothing more. This solves one from passing PHP code to screw stuff up. You cannot pass functions or variables as arguments either.

As for passing an incorrect amount of arguments, or the wrong datatype of a argument, normally this would result in a parse error.

But, just apply the @ in front of a function, and you suppress its error messages. ;)

mudpyr8
03-28-2004, 11:17 AM
For the dice roller I definitely would not want it to refresh every time, only generate a result the first time. Having an option would be great, even if it was part of the function definition and not an option in the admincp.

Ensuring only strings are passed is fine. Does that mean if they are numerical values they have to be retyped, say using 'int()'?

Thanks again.

Velocd
03-29-2004, 03:28 AM
PHP is very lenient on data type handling (unlike, say, c++).

A numerical value can be a string, as long as it is all numbers. A numerical string, however, isn't a true integer value. However it can easily be so by casting: (int) to the string.

Still, in many cases a numerical string will work in place of an argument in a function, even if it calls for an int.

For example, PHP function rand() only takes integers, yet in this hack the numerical string arguments passed work fine.

By the way, I've finished this hack (except for 1 minor bug, although probably major in your case :ermm: ) and will be releasing it shortly.

edit:
https://vborg.vbsupport.ru/showthread.php?s=&threadid=63113

mudpyr8
03-31-2004, 12:59 AM
Well, here is the die roller: http://www.tekhed.com/dice/

I won't keep it up forever, but you can try it. The output in the table is what I would return from the function.

Thanks for all of your help.

http://www.tekhed.com/dice/

R1chardYoung
11-17-2004, 10:59 AM
Theres a phpBB script that does exactly what your after:

http://www.phpbb.com/phpBB/viewtopic.php?t=124404

If only someone would convert it to vBulletin :D

mudpyr8
11-17-2004, 12:02 PM
Thanks. My dice code was already working under phpbb and I've ported it over using the BBCode that calls PHP mod for VB.

The dice roller you linked to doesn't handle, as far as I can tell, open ended dice, Savage World dice, and Hero dice, which mine does.

Thanks for the link though.