Forum Threads Limiter Infusion v0.21 (alpha_24052007)
PHP-Fusion: v6.01.10
original author: muscapaul
email: muscapaul@gmail.com
web: www.muscapaul.com
date: 24 May 2007
--------------------------------------------------
Copyright: Released under the terms and conditions
of the GNU General Public License (Version 2)
--------------------------------------------------

FEATURES
--------
* This infusion provides the possibility set a maximum number of new threads that can be created within a certain forum during a certain period.
* Any number of threads can be chosen.
* The period is given in whole days and is always determined in multiples of 24 hours from the present backwards.
So: The limit is set for a maximum of 'x' per 'y' times 24 hours. As soon as the the oldest of x threads falls outside the period, a new thread can be created.

* This infusion provides the possibility set a maximum number of new threads that a user can create within a certain forum during a certain period.
* The period is given in whole days and is always determined in multiples of 24 hours from the present backwards.
So: The limit is set so that a user can a create maximum of 'x' threads per 'y' times 24 hours. As soon as the the oldest of x threads falls outside the period, the user can create a new thread, provided that the maximum number set above is not reached.

CHANGED FILES
-------------
../forum/postnewthread.php
../forum/viewforum.php
../forum/viewthread.php

AUTOMATIC INSTALL
-----------------

You can choose Automatic Installation if you have not yet modified the three above mentioned files in the forum folder. It is recommended that you make backups of these files before installation.

1. Copy the contents of the php-files and changed-files folder to the corresponding folders on your server.

2. Go via Admin Panel -> System Admin to Infusions and install the Forum Threads Limiter infusion.

3. Go via Admin Panel -> Infusions to Forum Threads Limiter to enter settings for forums for which you want to limit the number of new threads.

MANUAL INSTALLATION
-------------------

You must choose Manual Installation if you have already modified one or more of the three above mentioned files in the forum folder. If one or more of these files were not modified, you can choose to upload the version from the changed-files folder. It is strongly recommended that you make backups of the files before installation.

1. Copy the contents of the php-files and changed-files folder to the corresponding folders on your server.

2. Open the file ../forum/postnewthread.php in a editor of your choice and find the code below (approx. lines 86-89):
            if (!$flood) {
                $result = dbquery("UPDATE ".$db_prefix."forums SET forum_lastpost='".time()."', forum_lastuser='".$userdata['user_id']."' WHERE forum_id='$forum_id'");
                $result = dbquery("INSERT INTO ".$db_prefix."threads (forum_id, thread_subject, thread_author, thread_views, thread_lastpost, thread_lastuser, thread_sticky, thread_locked) VALUES('$forum_id', '$subject', '".$userdata['user_id']."', '0', '".time()."', '".$userdata['user_id']."', '$sticky', '0')");
                $thread_id = mysql_insert_id();

Replace this code with:
            if (!$flood) {
                $forum_data = dbarray(dbquery("SELECT * FROM ".$db_prefix."forum_limit_settings WHERE forum_no='$forum_id'"));
                $allowed_threads = $forum_data['max_threads'];
                $allowed_user = $forum_data['max_per_user'];
                $thread_time = $forum_data['limit_period']*86400;
                $user_time = $forum_data['user_period']*86400;
                $earliest_time_thread=time()-$thread_time;
                $earliest_time_user=time()-$user_time;
                $thread_check = dbquery("SELECT * FROM ".$db_prefix."forum_thread_creation WHERE forum_no='$forum_id' AND creation_datestamp>='$earliest_time_thread'");
                $user_check = dbquery("SELECT * FROM ".$db_prefix."forum_thread_creation WHERE forum_no='$forum_id' AND thread_author='".$userdata['user_id']."' AND creation_datestamp>='$earliest_time_user'");
                $present_threads=dbrows($thread_check);
                $user_threads=dbrows($user_check);
                if (($present_threads<$allowed_threads && $user_threads<$allowed_user) || $allowed_threads=='' || $allowed_user=='')  {  
                $result = dbquery("UPDATE ".$db_prefix."forums SET forum_lastpost='".time()."', forum_lastuser='".$userdata['user_id']."' WHERE forum_id='$forum_id'");
                $result = dbquery("INSERT INTO ".$db_prefix."threads (forum_id, thread_subject, thread_author, thread_views, thread_lastpost, thread_lastuser, thread_sticky, thread_locked) VALUES('$forum_id', '$subject', '".$userdata['user_id']."', '0', '".time()."', '".$userdata['user_id']."', '$sticky', '0')");
                $thread_id = mysql_insert_id();
                $result1 = dbquery("INSERT INTO ".$db_prefix."forum_thread_creation (forum_no, thread_no, creation_datestamp, thread_author) VALUES ('$forum_id', '$thread_id', '".time()."', '".$userdata['user_id']."')"); // new line froum threads limiter

In the same file finde the following code (aprrox. lines 116-120):
                            @unlink($attach['tmp_name']);
                            $error = 2;
                        }
                    }
                }

Immediately below add the following code:
                else {fallback("viewforum.php?forum_id=$forum_id"); }
                }

Save and close the file.

3. Open the file ../forum/viewforum.php in a editor of your choice and find the code below (approx. line 15):
include LOCALE.LOCALESET."forum/main.php";

Immediately below add the following code:
if (file_exists(INFUSIONS."forum_limit/locale/".$settings['locale'].".php")) {
    include INFUSIONS."forum_limit/locale/".$settings['locale'].".php";
} else { include INFUSIONS."forum_limit/locale/English.php"; }

In the same file, find the code below (approx. lines 44-46):
    echo "<td align='right'>
<a href='post.php?action=newthread&amp;forum_id=$forum_id'><img src='".THEME."forum/newthread.gif' alt='".$locale['566']."' style='border:0px;'></a>
</td>\n";

Replace this code with:
$forumlimit=dbarray(dbquery("SELECT * FROM ".$db_prefix."forum_limit_settings WHERE forum_no='$forum_id'"));
$limitperiod = $forumlimit['limit_period']*86400;
$userperiod = $forumlimit['user_period']*86400;
$forum_timelimit = time()-$limitperiod;
$forum_userlimit = time()-$userperiod;
$forum_limit=dbquery("SELECT * FROM ".$db_prefix."forum_thread_creation WHERE forum_no='$forum_id' AND creation_datestamp>'".$forum_timelimit."'");
$forum_limit_user=dbquery("SELECT * FROM ".$db_prefix."forum_thread_creation WHERE forum_no='$forum_id' AND thread_author='".$userdata['user_id']."' AND creation_datestamp>'".$forum_userlimit."'");
$rows_f=dbrows($forum_limit);
$rows_u=dbrows($forum_limit_user);
if (($rows_f<$forumlimit['max_threads'] && $rows_u<$forumlimit['max_per_user']) || ($forumlimit['max_threads']=='' || $forumlimit['max_threads']=='0') || ($forumlimit['max_per_user']=='' || $forumlimit['max_per_user']=='0')) {
    echo "<td align='right'><a href='post.php?action=newthread&amp;forum_id=$forum_id'><img src='".THEME."forum/newthread.gif' alt='".$locale['566']."' style='border:0px;'></a></td>\n";
}
else {
    echo "<td align='right'><b>".$locale['flim401']."</b></td>\n";
}

In the same file finde the code below (approx. lines 196-197):
    echo "<td align='right'>
<a href='post.php?action=newthread&amp;forum_id=$forum_id'><img src='".THEME."forum/newthread.gif' alt='".$locale['566']."' style='border:0px;'></a>
</td>\n";
   
Replace this code with:
if (($rows_f<$forumlimit['max_threads'] && $rows_u<$forumlimit['max_per_user']) || ($forumlimit['max_threads']=='' || $forumlimit['max_threads']=='0') || ($forumlimit['max_per_user']=='' || $forumlimit['max_per_user']=='0')) {
    echo "<td align='right'><a href='post.php?action=newthread&amp;forum_id=$forum_id'><img src='".THEME."forum/newthread.gif' alt='".$locale['566']."' style='border:0px;'></a></td>\n";
}
else {
    echo "<td align='right'><b>".$locale['flim401']."</b></td>\n";
}
  
Save and close the file.

4. Open the file ../forum/viewthread.php in a editor of your choice and find the code below (approx. line 16):
include LOCALE.LOCALESET."forum/main.php";

Immediately below add the following code:
if (file_exists(INFUSIONS."forum_limit/locale/".$settings['locale'].".php")) {
    include INFUSIONS."forum_limit/locale/".$settings['locale'].".php";
} else { include INFUSIONS."forum_limit/locale/English.php"; }

In the same file, find the code below (approx. line 84):
    echo "<a href='post.php?action=newthread&amp;forum_id=$forum_id'><img src='".THEME."forum/newthread.gif' alt='".$locale['566']."' style='border:0px;'></a></td>\n";

Replace this code with:
$forumlimit=dbarray(dbquery("SELECT * FROM ".$db_prefix."forum_limit_settings WHERE forum_no='$forum_id'"));
$limitperiod = $forumlimit['limit_period']*86400;
$userperiod = $forumlimit['user_period']*86400;
$forum_timelimit = time()-$limitperiod;
$forum_userlimit = time()-$userperiod;
$forum_limit=dbquery("SELECT * FROM ".$db_prefix."forum_thread_creation WHERE forum_no='$forum_id' AND creation_datestamp>'".$forum_timelimit."'");
$forum_limit_user=dbquery("SELECT * FROM ".$db_prefix."forum_thread_creation WHERE forum_no='$forum_id' AND thread_author='".$userdata['user_id']."' AND creation_datestamp>'".$forum_userlimit."'");
$rows_f=dbrows($forum_limit);
$rows_u=dbrows($forum_limit_user);
if (($rows_f<$forumlimit['max_threads'] && $rows_u<$forumlimit['max_per_user']) || ($forumlimit['max_threads']=='' || $forumlimit['max_threads']=='0') || ($forumlimit['max_per_user']=='' || $forumlimit['max_per_user']=='0')) {
    echo "<a href='post.php?action=newthread&amp;forum_id=$forum_id'><img src='".THEME."forum/newthread.gif' alt='".$locale['566']."' style='border:0px;'></a></td>\n";
}
else {
    echo "<td align='right'><b>".$locale['flim401']."</b></td>\n";
}

In the same file find the code below (approx. lines 281-282):
        echo "<a href='post.php?action=newthread&amp;forum_id=$forum_id'><img src='".THEME."forum/newthread.gif' alt='".$locale['566']."' style='border:0px;'></a>\n";
        echo "</td>\n";
   
Replace this code with:
if (($rows_f<$forumlimit['max_threads'] && $rows_u<$forumlimit['max_per_user']) || ($forumlimit['max_threads']=='' || $forumlimit['max_threads']=='0') || ($forumlimit['max_per_user']=='' || $forumlimit['max_per_user']=='0')) {
    echo "<a href='post.php?action=newthread&amp;forum_id=$forum_id'><img src='".THEME."forum/newthread.gif' alt='".$locale['566']."' style='border:0px;'></a></td>\n";
}
else {
    echo "<td align='right'><b>".$locale['flim401']."</b></td>\n";
}
  
Save and close the file.

5. Upload the modified files to the ../forum/ folder on your server.

6. Go via Admin Panel -> System Admin to Infusions and install the Forum Threads Limiter infusion.

7. Go via Admin Panel -> Infusions to Forum Threads Limiter to enter settings for forums for which you want to limit the number of new threads.

CHANGELOG
---------
v0.21 (alpha_24052007)
* Bug fixed: Corrected an error in the check whether a new thread could be posted or not.

v0.20 (alpha_10052007)
* Introduction of feature to limit the number of threads a single user can post.
* Check built in to prevent users from using the 'New Thread' button they may reach by going back in the browsers history or by opening multiple new threads in new windows. Before the thread is saved a check is done in the database to prevent that the limit is crossed. If the limit was already reached the user is lead back to the specific forum's page.
* Bug fixed: No threads could be created in forums for which no settings were chosen.

v0.10 (alpha_08052007)
* None (first release)

TO DO
-----
* Introduce button to dump some of the forum_threads_creation table as the is no need to save data that extend back far beyond any of the set limit periods.
* Possibly introduce an automatic deletion of settings for a forum when it is detected that a forum does not exist anymore.