MyBB Community Forums

Full Version: parsing messages from outside myBB
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,

I'm trying to call postParser from my main website code, and it's working by-and-large.  I'm initializing like this:

    if (!defined('MYBB_ROOT')) { define('MYBB_ROOT', $forum_path); }

    global $mybb;
    define('IN_MYBB', NULL);
    require_once($forum_path.'inc/init.php');
    require_once($forum_path.'inc/class_parser.php');


and then calling like this:
    $parser_options = [];
    $parser_options['allow_html'] = 1;
//    $parser_options['allow_mycode'] = 1;
    $parser_options['allow_smilies'] = 1;
    $parser_options['allow_imgcode'] = 1;
    $parser_options['allow_videocode'] = 1;
    $parser_options['filter_badwords'] = 1;
    $parser = new postParser;

    // select message from mybb_gtgg_posts
    $parser->parse_message($message, $parser_options);


... and this works, except of course for translating "mycode" segments. If I uncomment the 'allow_mycode' line, then the parser returns an empty string.

edited:

 It looks like that is happening because mybb settings aren't initialized, e.g. in function cache_mycode:
if($mybb->settings['allowbasicmycode'] == 1)

I added:
    global $mybb;

... and then printed out $mybb.  It looks like $mybb is being initialized correctly, and allowbasicmycode is set to 1.

So, the question is, am I missing some initialization required to make parse_message work for mycode?

Thanks,
--Dave
Try this code:

define('IN_MYBB', 1);
require_once('./global.php');

require_once(MYBB_ROOT.'inc/class_parser.php');

$parser_options = array(
	'allow_html' => 1,
	'allow_mycode' => 1,
	'allow_smilies' => 1,
	'allow_imgcode' => 1,
	'allow_videocode' => 1,
	'filter_badwords' => 1,
);
$parser = new postParser;

$message = 'This [b]is[/b] a [i]message[/i] with [color=red]some[/color] MyCode.';

echo($parser->parse_message($message, $parser_options));
(2018-10-23, 10:14 PM)Wildcard Wrote: [ -> ]Try this code:

define('IN_MYBB', 1);
require_once('./global.php');

require_once(MYBB_ROOT.'inc/class_parser.php');

$parser_options = [];
$parser_options['allow_html'] = 1;
$parser_options['allow_mycode'] = 1;
$parser_options['allow_smilies'] = 1;
$parser_options['allow_imgcode'] = 1;
$parser_options['allow_videocode'] = 1;
$parser_options['filter_badwords'] = 1;
$parser = new postParser;

$message = 'This [b]is[/b] a [i]message[/i] with [color=red]some[/color] MyCode.';

echo($parser->parse_message($message, $parser_options));

Thanks @Wildcard,

Good idea for testing, and thanks for the simplified initialization code!

The test message is indeed parsed correctly, although real posts still return the empty string...

I'll look into it more... if you have any ideas on what could cause blank messages, I'm all ears!

Thanks,
--Dave
(2018-10-23, 10:27 PM)dhc1 Wrote: [ -> ]The test message is indeed parsed correctly, although real posts still return the empty string...
...
if you have any ideas on what could cause blank messages, I'm all ears!

No, I honestly can't think of anything. I'm assuming you are running this code as a separate page in your forum root, given the relative location to global.php so where is the code failing to parse a "real post"?

If you give me more information about what you are trying to do, I will try to help you figure it out. More information is the key.
Yes, it's a separate page. I basically "select * from TABLE_PREFIX.posts where tid=$tid order by dateline" and then call parse_message for each.

If I'm pretty much on the right track, I think I'll have to delve a bit deeper into the parse_message function to see what's happening...
Post your code here. place it in tags:

[php]
echo('Hello, world.');
[/php]
update:

The problem is with \xa0 characters (html   characters) in the message. If I filter these out using:

$message = preg_replace('/\xa0/',' ',$message);

...then the parser works correctly.

Any ideas on how \xa0 characters get into the message in the first place?  It would seem   characters must get translated somewhere on posting a message.

If so, how and where are they supposed to be re-translated during formatting?
If not, how are they getting there?

Thanks again,
--Dave