Welcome Guest, Not a member yet? Register   Sign In
Custom conditionals in html content?
#1

[eluser]CroNiX[/eluser]
On a project the client creates page content using the fckeditor. He is now wanting to go above and beyond traditional straight html and use some sort of a custom parser for processing conditional statements within the html. I know vBulletin allows this sort of thing in their templates but I have no idea how to incorporate this.

Basically I want to use if/else statements within the html without having to run a dangerous eval(), so I can run the whole text string through a parser and protect certain content.

Something like:

case 1: show text block only if member of listed groups
Quote:<p><strong>This is text everyone can see.<strong></p>

[protected=administrator;moderator]
<p>This would only be seen if the member is an administrator or moderator.</p>
[/protected]
Case 2: show text block only if member of listed groups. If not, show alternative.
Quote:<p><strong>This is text everyone can see.<strong></p>
[protected=administrator;moderator]
This would only be seen if the member is an administrator or moderator.
[protected_else]
This would be the alternative text shown if not an admin or moderator
[/protected]

Does this seem like the best way to accomplish this? If so I would really appreciate some help coming up with the regex to capture the above 2 cases. Regex is still something I have to master and this seems like it would be fairly complex.

Thanks for any help or suggestions!
#2

[eluser]CroNiX[/eluser]
I got this figured out and thought I'd post in case anyone else could use this.
This is useful for a client who has an html editor, or area in their application where they create html markup for dynamic page content, to be able to easily have areas within a page only shown to certain usergroups. This is great for cases where you don't want to restrict the whole page.

For instance, the client recently used it on a payment page in a regular website (not shopping cart enabled), where members of paid usergroups get shown certain discounted paypal buttons while unregistered users get shown different buttons.

It is to be used in the following ways and bolded text is required.
The variables are the usergroups who are allowed to see the html contained.

To only display a section of html who is a member of group1.
Quote:blah blah html everyone can see...
[protected=group1]This is the text/html displayed to people of "group1".[/protected]
blah blah html everyone can see...

To only display a section of html to multiple groups.
Just chain the names of the multiple group names with a semi colon for a separator.
Quote:blah blah html everyone can see...
[protected=group1;group2]This is the text/html displayed to people of "group1" OR "group2".[/protected]
blah blah html everyone can see...

If you want to have a section that contains alternate text which is displayed to users who are NOT in the defined groups, use [alt][/alt] tags nested within the [protected][/protected] text area.
Quote:blah blah html everyone can see...
[protected=group1;group2]This is the text/html displayed to people of "group1" or "group2".
[alt]This is the html/text displayed to people who are NOT in usergroup "group1" or "group2".[/alt]
[/protected]
blah blah html everyone can see...

The only thing you really need to do is figure out how to populate the current users usergroups in the $users_groups array in the beginning of the code.

Heres the code. To use it (assuming its in a controller) just:
Code:
$parsed_text = $this->parse_protected($html_text_to_be_parsed);

I hope someone finds it useful.

Code:
//note: the array users_groups would contain the users current usergroups,
//which is how the permissions are determined.  You will need to populate
//this your own way to suit your case.

function parse_protected($text)
{
    //array of current users' usergroups
    $users_groups = array('group1');
    //find protected text
    $exp = '/(\[protected=)(.+)(\])(.+)(\[\/protected\])/iU';
    preg_match_all($exp, $text, $matches, PREG_SET_ORDER);
    //if no matches were found, just return original text
    if(count($matches) < 1)
    {
        return $text;
    }
    else
    {
        //parse
        foreach($matches as $match)
        {
            $original     = $match[0];             //full original text of matched item
            $groups     = split(';', $match[2]);//the usergroups (to right of equals)
            $content     = $match[4];            //the content string

            //check for alternative text: [alt]alternative text[/alt]
            $alt_exp = '/\[alt\](.+?)\[\/alt\]/i';
            preg_match_all($alt_exp, $original, $alt_matches, PREG_SET_ORDER);

            //user is allowed to view, get protected content
            if(count(array_intersect($groups, $users_groups)) > 0)
            {
                //replace the original match with its content
                $new_text = str_replace($original, $content, $original);

                //check to see if there was alt text and remove it
                if(count($alt_matches) > 0)
                {
                    $alt = $alt_matches[0][0];
                    $new_text = str_replace($alt, '', $new_text);
                }
                $text = str_replace($original, $new_text, $text);
            }
            else
            {
                //not allowed, check if there is alternative content.
                //If so, use that else remove entire match
                if(count($alt_matches) > 0)
                {
                    $alt = $alt_matches[0][1];
                    $new_text = str_replace($original, $alt, $original);
                }
                else
                {
                    $new_text = str_replace($original, '', $original);
                }
                $text = str_replace($original, $new_text, $text);
            }
        }
    }
    return $text;
}




Theme © iAndrew 2016 - Forum software by © MyBB