Topic: Limit users access to forums based on post count
I'm setting up a new forum and after looking at several solutions, I decided on pubBB. It only had one thing that I was really looking for, and that was the ability to limit users access to forums by post count. I hacked a way to do it together this afternoon. I may look at putting it into a module, but I didn't have time to research that today.
Anyway - here's a diff against 1.2.16 of what I did. If you wanted to include it (or something to accomplish the same thing) in the mainline project, that would be groovy. It's nice to be able to reserve certain sections of a forum to only members that are contributing something to the community.
diff -Naur upload.orig/include/functions.php upload/include/functions.php
--- upload.orig/include/functions.php 2007-12-25 14:46:35.000000000 -0600
+++ upload/include/functions.php 2007-12-25 15:03:08.000000000 -0600
@@ -1112,3 +1112,50 @@
echo '</pre>';
exit;
}
+
+//
+// Function to check user permissions to a forum, topic or post
+//
+function check_post_count($db)
+{
+ global $lang_common;
+
+ $id = $_GET['id'];
+
+ if (preg_match("/viewforum\.php/i", $_SERVER['PHP_SELF']))
+ {
+ // The user is trying to view a forum. $id will be the forums id
+ $result = $db->query('SELECT min_post_count FROM '.$db->prefix.'forums WHERE id='.$id) or error('Unable to fetch forum info', __FILE__, __LINE__, $db->error());
+ }
+ elseif (preg_match("/viewtopic\.php/i", $_SERVER['PHP_SELF']))
+ {
+ $pid = $_GET['pid'];
+
+ if (isset($id))
+ {
+ $result = $db->query('SELECT t.forum_id, f.min_post_count FROM '.$db->prefix.'topics t, '.$db->prefix.'forums f where f.id='.$id) or error('Unable to fetch forum info', __FILE__, __LINE__, $db->error());
+ }
+
+ if (isset($pid))
+ {
+ $result = $db->query('SELECT p.topic_id, t.forum_id, f.min_post_count FROM '.$db->prefix.'topics t, '.$db->prefix.'forums f, '.$db->prefix.'posts p where p.id='.$pid.' AND p.topic_id=t.id AND f.id=t.forum_id') or error('Unable to fetch forum info', __FILE__, __LINE__, $db->error());
+ }
+ }
+ elseif (preg_match("/post\.php/i", $_SERVER['PHP_SELF']))
+ {
+ // In post.php $fid is the forum ID and $tid is the topic
+ $id = $_GET['fid'];
+ $result = $db->query('SELECT t.forum_id, f.min_post_count FROM '.$db->prefix.'topics t, '.$db->prefix.'forums f where f.id='.$id) or error('Unable to fetch forum info', __FILE__, __LINE__, $db->error());
+ }
+
+ if (!$db->num_rows($result))
+ message($lang_common['Bad request']);
+
+ $cur_forum = $db->fetch_assoc($result);
+
+ // Does the user have access to this forum?
+ if ($cur_forum['min_post_count'] > 0)
+ if ($pun_user['num_posts'] < $cur_forum['min_post_count'])
+ message($lang_common['Post count']);
+
+}
diff -Naur upload.orig/lang/English/common.php upload/lang/English/common.php
--- upload.orig/lang/English/common.php 2007-12-25 15:04:44.000000000 -0600
+++ upload/lang/English/common.php 2007-12-25 15:00:00.000000000 -0600
@@ -35,6 +35,7 @@
// Notices
'Bad request' => 'Bad request. The link you followed is incorrect or outdated.',
'No view' => 'You do not have permission to view these forums.',
+'Post count' => 'You do not have the required post count to view these forums.',
'No permission' => 'You do not have permission to access this page.',
'Bad referrer' => 'Bad HTTP_REFERER. You were referred to this page from an unauthorized source. If the problem persists please make sure that \'Base URL\' is correctly set in Admin/Options and that you are visiting the forum by navigating to that URL. More information regarding the referrer check can be found in the PunBB documentation.',
diff -Naur upload.orig/post.php upload/post.php
--- upload.orig/post.php 2007-12-25 15:06:38.000000000 -0600
+++ upload/post.php 2007-12-25 15:07:38.000000000 -0600
@@ -36,6 +36,9 @@
if ($tid < 1 && $fid < 1 || $tid > 0 && $fid > 0)
message($lang_common['Bad request']);
+// Check that the user has access to this forum
+check_post_count($db);
+
// Fetch some info about the topic and/or the forum
if ($tid)
$result = $db->query('SELECT f.id, f.forum_name, f.moderators, f.redirect_url, fp.post_replies, fp.post_topics, t.subject, t.closed FROM '.$db->prefix.'topics AS t INNER JOIN '.$db->prefix.'forums AS f ON f.id=t.forum_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND t.id='.$tid) or error('Unable to fetch forum info', __FILE__, __LINE__, $db->error());
diff -Naur upload.orig/viewforum.php upload/viewforum.php
--- upload.orig/viewforum.php 2007-12-25 14:46:35.000000000 -0600
+++ upload/viewforum.php 2007-12-25 14:57:25.000000000 -0600
@@ -38,6 +38,9 @@
// Load the viewforum.php language file
require PUN_ROOT.'lang/'.$pun_user['language'].'/forum.php';
+// Check that the user has access to this forum
+check_post_count($db);
+
// Fetch some info about the forum
$result = $db->query('SELECT f.forum_name, f.redirect_url, f.moderators, f.num_topics, f.sort_by, fp.post_topics FROM '.$db->prefix.'forums AS f LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND f.id='.$id) or error('Unable to fetch forum info', __FILE__, __LINE__, $db->error());
if (!$db->num_rows($result))
diff -Naur upload.orig/viewtopic.php upload/viewtopic.php
--- upload.orig/viewtopic.php 2007-12-25 14:46:35.000000000 -0600
+++ upload/viewtopic.php 2007-12-25 14:57:36.000000000 -0600
@@ -37,6 +37,9 @@
if ($id < 1 && $pid < 1)
message($lang_common['Bad request']);
+// Check that the user has access to this forum
+check_post_count($db);
+
// Load the viewtopic.php language file
require PUN_ROOT.'lang/'.$pun_user['language'].'/topic.php';
This requires an extra column in $db->prefix.forums to allow you to set the post count.
mysql> describe punbbforums;
+----------------+-----------------------+------+-----+-----------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-----------------------+------+-----+-----------+----------------+
| id | int(10) unsigned | | PRI | NULL | auto_increment |
| forum_name | varchar(80) | | | New forum | |
| forum_desc | text | YES | | NULL | |
| redirect_url | varchar(100) | YES | | NULL | |
| moderators | text | YES | | NULL | |
| num_topics | mediumint(8) unsigned | | | 0 | |
| num_posts | mediumint(8) unsigned | | | 0 | |
| last_post | int(10) unsigned | YES | | NULL | |
| last_post_id | int(10) unsigned | YES | | NULL | |
| last_poster | varchar(200) | YES | | NULL | |
| sort_by | tinyint(1) | | | 0 | |
| disp_position | int(10) | | | 0 | |
| cat_id | int(10) unsigned | | | 0 | |
| min_post_count | int(10) unsigned | | | 0 | |
+----------------+-----------------------+------+-----+-----------+----------------+
14 rows in set (0.00 sec)
So there you have it. Feel free to use this code if you like it. A credit would be nice though.