PDA

View Full Version : How do you script BB Code?


Jaxel
01-05-2009, 02:47 PM
Okay... lets say I want to make a bbcode... 536

I want to parse some PHP code on this code. How would I handle this?


function fetch_bbcode_video($videoid)
{
require_once(DIR . '/includes/class_videosharingservice.php');
global $vbphrase, $vbulletin, $show, $stylevar;
if (!$videoinfo = $vbulletin->db->query_first("
SELECT video.*
FROM " . TABLE_PREFIX . "video AS video
WHERE video.videoid = '" . $videoid . "'")
)
{
return false;
}
$classname = "vB_VideoSharingService_$video[videoservice]";
$obj = new $classname($vbulletin);
$embedcode = $obj->fetch_embedcode($videoinfo['videoidservice']);
$video['cattitle'] =& $vbulletin->videocats["$video[videocategoryid]"]['title'];
$video['caturl'] = construct_category_url($vbulletin->videocats["$video[videocategoryid]"]);
$video['url'] = construct_video_url($video);
$video['userurl'] = construct_user_url($video);
if ($video['timelength'] == 0)
{
$video['timelength'] = "???";
}
else
{
$duration = $video['timelength'];
$minutes = floor($duration / 60);
$seconds = $duration % 60;
$seconds = str_pad($seconds, 2, "0", STR_PAD_LEFT);
$video['timelength'] = "$minutes:$seconds";
}
eval('$html = "' . fetch_template('bbcode_video') . '";');
return $html;
}

Jaxel
01-07-2009, 10:04 PM
Okay... since no one knows how to script BB code... how about something different...

How would I make a script that examines a new post and preg_matches a thread and replaces it with content? So if someone made a post and it had:

536

I would want it to preg_match it with:

\[(VIDEO|video)](\d*)\[/(VIDEO|video)]

Then of course, I would run some PHP code on the preg_match[2] returned as 536.

Would this be possible?

Bellardia
01-07-2009, 10:14 PM
Okay... since no one knows how to script BB code... how about something different...

How would I make a script that examines a new post and preg_matches a thread and replaces it with content? So if someone made a post and it had:

536

I would want it to preg_match it with:

\[(VIDEO|video)]([\d]*)\[/(VIDEO|video)]

Then of course, I would run some PHP code on the preg_match[2] returned as 536.

Would this be possible?

I posted this in another thread a bit earlier, it should help you out

You'll require two plugins for this. I posted their source as xml just because it included everything. Just pay particular attention to the hook location and the basic functions I included.

This one will take the text to be posted, and submit it to a function so can it can be run through a preg_replace or similar function.
<plugin active="1" executionorder="5">

<title>Title Of Mod</title>

<hookname>postbit_display_complete</hookname>

<phpcode><![CDATA[global $vbulletin;

$this->post['message'] = Function_Name($this->post['message']);

]]></phpcode>

</plugin>


Another code in the global_start hook can be used to hold the source of the link replacement, modify the post and return it however you want.

<title>Plugin Title</title>
<hookname>global_start</hookname>
<phpcode><![CDATA[
function Function_Name($post) {
global $vbulletin;
//Preg_replace + other Functions
return $post;
}
]]></phpcode>

Dismounted
01-08-2009, 02:12 AM
That is not adding another BB code. You can add a proper BB code (using PHP processing) by adding your tag to the processing array and setting the callback as the external callback function. Look at the bottom of class_bbcode.php.

Jaxel
01-10-2009, 12:36 PM
Huh? I'm confused... is there a guide somewhere?

All I want to do is have a BB Code, with a PHP function run on it.

Dismounted
01-11-2009, 04:15 AM
That is not adding another BB code. You can add a proper BB code (using PHP processing) by adding your tag to the processing array and setting the callback as the external callback function. Look at the bottom of class_bbcode.php.
All the information you need is in that post.

Jaxel
01-12-2009, 05:09 PM
Is there any way of doing this WITHOUT editing VB files? And I dont know what I am supposed to be looking at in class_bbcode.php.

Dismounted
01-13-2009, 04:41 AM
Is there any way of doing this WITHOUT editing VB files?
Yes, and that is what I'm telling you.
And I dont know what I am supposed to be looking at in class_bbcode.php.
You will see how tags are defined. Define your own tag and callback.

Jaxel
01-14-2009, 04:35 PM
Do you mind actually explaining what needs to be done? Because I still dont see what I'm supposed to be looking at.

*EDIT* ah... I think I get it now, you wanted me to see: ($hook = vBulletinHook::fetch_hook('bbcode_fetch_tags')) ? eval($hook) : false;

I tried doing this...

// [VIDEO]
require_once(DIR . '/includes/functions_videodirectory.php');

$tag_list['no_option']['video'] = array(
'callback' => 'handle_external',
'external_callback' => 'handle_bbcode_video',
'strip_empty' => true
);
But I get an error:
Fatal error: Call to undefined method vB_BbCodeParser::handle_bbcode_video() in /home/eightway/public_html/includes/class_bbcode.php on line 1143

How do I get it to call handle_bbcode_video in functions_videodirectory.php?

--------------- Added 1231967882 at 1231967882 ---------------

Well I figured to just use the function in the plugin itself, instead of calling a file... But I'm having trouble passing the parameter data...

// [VIDEO]
$tag_list['no_option']['video'] = array(
'callback' => 'handle_external',
'external_callback' => 'handle_bbcode_video',
'strip_empty' => true
);

function handle_bbcode_video($videoid)
{
global $vbulletin;

if (!$videoinfo = $vbulletin->db->query_first("
SELECT video.*
FROM " . TABLE_PREFIX . "video AS video
WHERE video.videoid = '" . $videoid . "'")
)
{
return 'This video does not exist';
}

require_once(DIR . '/includes/class_videosharingservice.php');
require_once(DIR . '/includes/videoserviceapi/class_' . strtolower($videoinfo['videoservice']) . '.php');

$classname = 'vB_VideoSharingService_' . $videoinfo['videoservice'];
$obj = new $classname($vbulletin);

$embedhtml = $obj->fetch_embedcode($videoinfo['videoidservice']);

return $embedhtml;
}

For some reason, $videoid is always empty. Why is this? It ALWAYS returns 'This video does not exist'.

--------------- Added 1231974120 at 1231974120 ---------------

Is there a guide or something that explains what all the array options in $tag_list do?

Dismounted
01-15-2009, 04:23 AM
Do you mind actually explaining what needs to be done?
You've done a lot of work - and it shows, because you're nearly there. I'm going to give you the rest of it, because unlike a lot of people, you've actually tried doing it yourself. :)

Hook: bbcode_fetch_tags
// [VIDEO]
$tag_list['no_option']['video'] = array(
'callback' => 'handle_external',
'external_callback' => 'handle_bbcode_video',
'strip_empty' => true
);

Hook: bbcode_create
// include our custom functions
require_once(DIR . '/includes/functions_videodirectory.php');
require_once(DIR . '/includes/class_videosharingservice.php');

Function: handle_bbcode_video (/includes/functions_videodirectory.php)
function handle_bbcode_video(&$parser, $value, $option)
{
global $vbulletin;

// clean video id
$videoid = intval($value);

// fetch video information -- this is costly, it is run every time the bb code tag is used
$videoinfo = $vbulletin->db->query_first("
SELECT *
FROM " . TABLE_PREFIX . "video
WHERE video.videoid = " . $videoid . "
LIMIT 1
");

// check video exists
if (empty($videoinfo))
{
// this will show up on posts -- make sure this is what you want
return 'This video does not exist.';
}

// instantiate our video class
require_once(DIR . '/includes/videoserviceapi/class_' . strtolower($videoinfo['videoservice']) . '.php');
$classname = 'vB_VideoSharingService_' . $videoinfo['videoservice'];
$obj = new $classname($vbulletin);

// return embed code
return $obj->fetch_embedcode($videoinfo['videoidservice']);
}
I've cleaned up and commented the function for you - you should read some of the comments.

Jaxel
01-20-2009, 02:24 AM
Hmm... Dismounted, thanks for the help... I didn't see your new post till now...

I got this working a few days ago, and this is the code I am using:

if ($vbulletin->options['videodirectory_bbcode'])
{
// [VIDEO] [VIDEO=XXX]
$tag_list['no_option']['video'] = array(
'callback' => 'handle_external',
'strip_empty' => true,
'stop_parse' => true,
'disable_smilies' => true,
'disable_wordwrap' => true,
'strip_space_after' => 0,
'external_callback' => 'handle_bbcode_video'
);
$tag_list['option']['video'] = array(
'callback' => 'handle_external',
'strip_empty' => true,
'stop_parse' => true,
'disable_smilies' => true,
'disable_wordwrap' => true,
'strip_space_after' => 0,
'external_callback' => 'handle_bbcode_video'
);
}

function handle_bbcode_video(&$parser, $videoid, $videotitle)
{
global $vbulletin, $stylevar;

if (!$video = $vbulletin->db->query_first("SELECT video.*
FROM " . TABLE_PREFIX . "video AS video WHERE videoid = '" . $videoid . "'"))
{
$video['error'] = 'THIS VIDEO DOES NOT EXIST';
eval('$HTMLembed = "' . fetch_template('video_bbcode') . '";');
return $HTMLembed;
}

require_once(DIR . '/includes/functions_videodirectory.php');
require_once(DIR . '/includes/class_videosharingservice.php');
require_once(DIR . '/includes/videoserviceapi/class_' . strtolower($video['videoservice']) . '.php');

$classname = "vB_VideoSharingService_$video[videoservice]";
$obj = new $classname($vbulletin);

$showful = $vbulletin->options['videodirectory_showful'];
$showrel = $vbulletin->options['videodirectory_showrel'];
$showsta = $vbulletin->options['videodirectory_showsta'];

$video['embed'] = $obj->fetch_embedcode($video['videoidservice'], false, $showful, $showrel, $showsta);
$video['url'] = construct_video_url($video);
if (empty($videotitle)) { $videotitle = $video['title']; }

eval('$HTMLembed = "' . fetch_template('video_bbcode') . '";');
return $HTMLembed;
}

Do you mind looking it over so see if I am doing anything wrong? I'm doing this all in a SINGLE plugin; is that wrong?

Dismounted
01-20-2009, 03:22 AM
Do you mind looking it over so see if I am doing anything wrong?
I guess it will work, but look out for phrases in templates you're evaluating (they won't currently work with the code you're using).
I'm doing this all in a SINGLE plugin; is that wrong?
Ideally, you should have most/all your plugin code in files, but this is only for performance reasons.