Home

Your premium source for custom modification services for phpBB
  logo

HomeForumsBlogMOD ManagerFAQSearchRegisterLogin

Comments April 10, 2008

Designing the Forum Auth by Post Count MOD Part III: Using Functions

Filed under: phpBB — Dave Rathbun @ 8:41 pm CommentsComments (0) 

This is the last post in a series that started with a few observations about the concept of “Auto Group” MODs and why I think they are bad, followed by some posts about the design for the “Forum Auth by Post Count” MOD that I have released for phpBB2. All of the related posts are linked at the end of the post in case you want to go back and read the full story. In this post I will talk about a basic strategy in programming that should be followed: putting reusable code into a function or procedure.


Authorization Check

The basic premise of the Forum Auth by Post Count MOD is that a board owner can set a minimum (and maximum) post count required to view a forum. Here is what that portion of the code looks like… it’s really quite basic. First there is some exception handling to work with negative post counts and the defaults provided by the application. Then the code simply checks to see if the user passes the authorization test. Oh, and there’s a note that excludes board administrators from any restrictions right at the top of the code.

   // Let's add some special handling for negative
        // user post counts, just to keep them from screwing
        // everything up....
        $user_posts = ($userdata['user_posts'] >= 0 ? $userdata['user_posts'] : 0);

        if ($userdata['user_level'] == ADMIN)
        {
                return TRUE;
        }

        if ($max_posts == NOLIMIT_POST_COUNT_AUTH)
        {
                $max_posts = $user_posts;
        }

        if ($min_posts == NOLIMIT_POST_COUNT_AUTH)
        {
                $min_posts = $user_posts;
        }

        if ( ($user_posts >= $min_posts) && ($user_posts <= $max_posts) )
        {
                return TRUE;
        }

Now here is the fun part. I need to put this code everywhere I check for forum access, or build a list of forums. That includes:

  • index.php
  • posting.php
  • search.php
  • viewforum.php
  • viewtopic.php
  • includes/functions.php (for building the forum jumpbox)

That's quite a few places to put the code! As I was developing the MOD originally I put the code into each of those files as a fast and easy way to start testing. However as the list of affected files grew longer, and as I added new features to the MOD, it became a major bother to go back and apply the same edits to each of those files.

So I created a function.

Creating a Function

If you have been coding for a while this probably seems very basic to you. And it should be, this is a coding technique that you should be used to using. When you have code that is reusable (as the authorization check is in this case) you should place the code in one place and call it. The obvious advantage is that when you need to alter the code there is only one place to go. So the last bit of code looks like this:

function phpbbdoctor_post_count_auth($min_posts, $max_posts)
{
        global $userdata;

        ... every thing else I posted earlier

        return FALSE;
}

The function takes two arguments: the minimum and maximum post value. The $userdata array is used globally so that it doesn't have to be passed. If the user passes (or fails) any of the tests the boolean value TRUE or FALSE is returned. If the user falls all the way to the bottom of the function I return a FALSE result to avoid granting permission just because a user didn't match any of the requirements.

The function is then added to the includes/functions.php code and is available to be called from any of the pages I listed earlier. As a result the code changes to those files are smaller (a minor benefit) and they are all guaranteed to be consistent (a major benefit) and if there are changes required to the function logic I only have one place to go (another major benefit).

Here's what the code for index.php looks like after defining the authorization check as a function... this row:

           $forum_data[] = $row;

is replaced by this block of code:

           if (phpbbdoctor_post_count_auth($row['min_posts_to_view'], $row['max_posts_to_view']))
                {
                        $forum_data[] = $row;
                }

What this will do is query every forum but only add the forum to the array called $forum_data if the user has authorization to view it because of their post count. It's very easy.

Conclusion

So the basic design of this MOD includes adding four fields to the phpbb_forums table, and then creating a function that applies our special rules based on the user's post count. These rules are applied on the index page, the posting process, the search page, and even when a user attempts to view a topic or forum. These rules do not grant access where none was available previously; a user will not be able to see a private forum just because of their post count, they must still be a member of the group assigned access to that forum.

I like this MOD quite a bit, even though I do not use it myself. I think this MOD in combination with the Page Permissions MOD gives board owners quite a few access control options that are not present in the basic phpBB2 package.

Related Links

No Comments »

No comments yet.

RSS feed for comments on this post.

Leave a comment

Tags allowed in comments:
<a href="" title=""> <acronym title=""> <blockquote cite=""> <code> <strong> <em> <u> <sup> <sub> <strike>

Confirm submission by clicking only the marked checkbox:

             *

Powered by WordPress