Forum Threads Limiter Infusion v1.00
PHP-Fusion: v7.00.+
original author: muscapaul
email: muscapaul@gmail.com
web: www.muscapaul.com
date: 19 August 2008
--------------------------------------------------
This program is released as free software under the
Affero GPL license. You can redistribute it and/or
modify it under the terms of this license which you
can read by viewing the included agpl.txt or online
at www.gnu.org/licenses/agpl.html. Removal of this
copyright header is strictly prohibited without
written permission from the original author(s).
--------------------------------------------------

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.

* This infusion provides the possibility to set a maximum number of posts a user can do 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 for a maximum of 'x' per 'y' times 24 hours. As soon as the the oldest of x posts falls outside the period, a new message can be posted.

* The strictest limit is always used. 


UPGRADING FROM PHP-FUSION v6 TO PHP-FUSION v7
---------------------------------------------
Please note, if you want to upgrade your Forum Threads Limiter Infusion during a PHP-Fusion version upgrade from v6 to v7, there is no need to defuse the infusion. There will be no error messages as long as you upload the infusion files at the same time as the PHP-Fusion files. When you upload the infusion files, you can simply overwrite the existing ones.
PHP-Fusion v7 utilises a somewhat different system for administering admin rights for Infusions. In v6 it used to be a single setting that gave rights to all Infusions. Under the new version every infusion will be given its own code for admin rights. As a result it is necessary to run an upgrade script (../infusions/forum_limits/v6-7upgrade.php) that will at least add the rights for the primary Superadministrator. The file has checkboxes to indicate whether all superadmins should be given access rights to the Forum Threads Limiter Infusion and/or whether all admins with Infusion Administration rights should be given those rights.
This script only needs to be run if a site is upgraded from PHP-Fusion v6 to v7. Even though a check has been built in to prevent the script from being run after the upgrade has already been performed, it is advised to delete the script from your server after upgrading.



CHANGED FILES
-------------
../forum/options.php
../forum/postify.php
../forum/postnewthread.php
../forum/postreply.php
../forum/viewforum.php
../forum/viewthread.php


AUTOMATIC INSTALLATION
----------------------

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 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 files folder to the corresponding folders on your server.

2. Open the file ../forum/options.php in a editor of your choice and find the code below (approx. line 20 of the original file):
include LOCALE.LOCALESET."forum/options.php";

Immediately AFTER add the following code:
include INFUSIONS."forum_limit/infusion_db.php";


In the same file finde the following code (aprrox. line 85):
		$result = dbquery("SELECT * FROM ".DB_FORUM_ATTACHMENTS." WHERE thread_id='".$_GET['thread_id']."'");

Immediately AFTER add the following code:
		$result = dbquery("DELETE FROM ".DB_FORUM_THREAD_CREATION." WHERE thread_no='$thread_id'");


		In the same file finde the following code (aprrox. line 144):
 		$result = dbquery("UPDATE ".DB_THREADS." SET forum_id='".$_POST['new_forum_id']."' WHERE thread_id='".$_GET['thread_id']."'");

Immediately AFTER add the following code:
		$result = dbquery("UPDATE ".DB_FORUM_THREAD_CREATION." SET forum_no='$new_forum_id' WHERE thread_no='$thread_id'");

Save and close the file.

3. Open the file ../forum/postify.php in a editor of your choice and find the code below (approx. lines 79-80 of the original file):
	closetable();
} else if ($_GET['post'] == "reply") {

Immediately AFTER add the following code:

	include INFUSIONS."forum_limit/includes/forum_limit_include.php";
	if ($post_check >= $global_data['global_max_posts'] && $global_data['global_setting'] == '2') { redirect(FORUM); }

Save and close the file.

4. Open the file ../forum/postnewthread.php in a editor of your choice and find the code below (approx. line 18 of the original file):

if (!defined("IN_FUSION")) { die("Access Denied"); }

Immediately AFTER add the following code:

include INFUSIONS."forum_limit/infusion_db.php";
include INFUSIONS."forum_limit/includes/forum_limit_include.php";


In the same file finde the following code (aprrox. lines 111-112):

				$result = dbquery("INSERT INTO ".DB_THREADS." (forum_id, thread_subject, thread_author, thread_views, thread_lastpost, thread_lastpostid, thread_lastuser, thread_postcount, thread_poll, thread_sticky, thread_locked) VALUES('".$_GET['forum_id']."', '$subject', '".$userdata['user_id']."', '0', '".time()."', '0', '".$userdata['user_id']."', '1', '".$thread_poll."', '".$sticky_thread."', '".$lock_thread."')");
				$thread_id = mysql_insert_id();

REPLACE this code by the following code:

				if ($post_check<$global_data['global_max_posts'] && (($present_threads<$allowed_threads && $user_threads<$allowed_user) || $allowed_threads=='' || $allowed_user=='')) {
				$result = dbquery("INSERT INTO ".DB_THREADS." (forum_id, thread_subject, thread_author, thread_views, thread_lastpost, thread_lastpostid, thread_lastuser, thread_postcount, thread_poll, thread_sticky, thread_locked) VALUES('".$_GET['forum_id']."', '$subject', '".$userdata['user_id']."', '0', '".time()."', '0', '".$userdata['user_id']."', '1', '".$thread_poll."', '".$sticky_thread."', '".$lock_thread."')");
				$thread_id = mysql_insert_id();
				$result1 = dbquery("INSERT INTO ".DB_FORUM_THREAD_CREATION." (forum_no, thread_no, creation_datestamp, thread_author) VALUES ('$forum_no', '$thread_id', '".time()."', '".$userdata['user_id']."')");

				
In the same file finde the following code (aprrox. lines 159-161):

			} else {
					redirect("viewforum.php?forum_id=".$_GET['forum_id']);
			}

Immediately BEFORE add the following code:
			} else {
					redirect("viewforum.php?forum_id=".$_GET['forum_id']);
			}
(This is indeed the same code that is repeated.)

Save and close the file.

5. Open the file ../forum/postreply.php in a editor of your choice and find the code below (approx. line 18 of the original file):

if (!defined("IN_FUSION")) { die("Access Denied"); }

Immediately AFTER add the following code:

include INFUSIONS."forum_limit/infusion_db.php";
include INFUSIONS."forum_limit/includes/forum_limit_include.php";


In the same file finde the following code (aprrox. line 61):

			if (!flood_control("post_datestamp", DB_POSTS, "post_author='".$userdata['user_id']."'")) {

Immediately AFTER add the following code:

			if ($post_check < $global_data['global_max_posts'] && (($present_threads < $allowed_threads && $user_threads < $allowed_user) || $allowed_threads == '' || $allowed_user == '')) {

				
In the same file finde the following code (aprrox. lines 112-114):

			} else {
					redirect("viewforum.php?forum_id=".$_GET['forum_id']);
			}

Immediately BEFORE add the following code:
			} else {
					redirect("viewforum.php?forum_id=".$_GET['forum_id']);
			}
(This is indeed the same code that is repeated.)

Save and close the file.

6. Open the file ../forum/viewforum.php in a editor of your choice and find the code below (approx. line 20 of the original file):

include LOCALE.LOCALESET."forum/main.php";

Immediately AFTER add the following code:

if (iMEMBER) {
	include INFUSIONS."forum_limit/infusion_db.php";
	include INFUSIONS."forum_limit/includes/forum_limit_include.php";

	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"; }
}


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 107):):

		$result = dbquery("DELETE FROM ".DB_FORUM_POLLS." WHERE thread_id IN (".$thread_ids.")");

Immediatelt AFTER add the following code:

		$result = dbquery("DELETE FROM ".DB_FORUM_THREAD_CREATION." WHERE thread_no IN (".$thread_ids.")");


In the same file, find the code below (approx. line 132):):

	if (iMEMBER && $can_post) { echo "<td align='right' style='padding:4px 0px 4px 0px'><a href='post.php?action=newthread&amp;forum_id=".$_GET['forum_id']."'><img src='".get_image("newthread")."' alt='".$locale['566']."' style='border:0px;' /></a></td>\n"; }

REPLACE this code with the following:

	if (iMEMBER && $can_post) {
		$forumlimit = dbarray(dbquery("SELECT * FROM ".DB_FORUM_LIMIT_SETTINGS." WHERE forum_no='$forum_no'"));
		$limitperiod = $forumlimit['limit_period']*86400;
		$userperiod = $forumlimit['user_period']*86400;
		$forum_timelimit = time()-$limitperiod;
		$forum_userlimit = time()-$userperiod;
		$forum_limit = dbquery("SELECT * FROM ".DB_FORUM_THREAD_CREATION." WHERE forum_no='$forum_no' AND creation_datestamp>'".$forum_timelimit."'");
		$forum_limit_user = dbquery("SELECT * FROM ".DB_FORUM_THREAD_CREATION." WHERE forum_no='$forum_no' AND thread_author='".$userdata['user_id']."' AND creation_datestamp>'".$forum_userlimit."'");
		$rows_f = dbrows($forum_limit);
		$rows_u = dbrows($forum_limit_user);
		if ((($post_check < $global_data['global_max_posts'] && $global_data['global_setting'] != '0') || $global_data['global_setting'] == '0') && (($rows_f < $forumlimit['max_threads'] && $rows_u < $forumlimit['max_per_user']) || $forumlimit['max_threads'] == '' || $forumlimit['max_per_user'] == '')) {
			echo "<td align='right' style='padding:4px 0px 4px 0px'>";
			echo "<a href='post.php?action=newthread&amp;forum_id=".$_GET['forum_id']."'><img src='".get_image("newthread")."' alt='".$locale['566']."' style='border:0px;' /></a></td>\n";
		}
		else {
			if ($post_check >= $global_data['global_max_posts'] && $global_data['global_setting'] == '2') {
				echo "<td align='right'><span class='button' style='padding:4px 0px 2px 0px'>&nbsp;".$locale['FLIM402']."&nbsp;</span></td>\n"; // no new threads warning
			}
			else {
				echo "<td align='right'><span class='button' style='padding:4px 0px 2px 0px'>&nbsp;".$locale['FLIM401']."&nbsp;</span></td>\n"; // no new threads warning
			}
		}
	}


In the same file finde the code below (approx. line 271):

	if (iMEMBER && $can_post) { echo "<td align='right' style='padding-top:5px'><a href='post.php?action=newthread&amp;forum_id=".$_GET['forum_id']."'><img src='".get_image("newthread")."' alt='".$locale['566']."' style='border:0px;' /></a></td>\n"; }

REPLACE this code with the following code:

	if (iMEMBER && $can_post) {
		if ((($post_check < $global_data['global_max_posts'] && $global_data['global_setting'] != '0') || $global_data['global_setting'] == '0') && (($rows_f < $forumlimit['max_threads'] && $rows_u < $forumlimit['max_per_user']) || $forumlimit['max_threads'] == '' || $forumlimit['max_per_user'] == '')) {
		echo "<td align='right' style='padding-top:5px'><a href='post.php?action=newthread&amp;forum_id=".$_GET['forum_id']."'><img src='".get_image("newthread")."' alt='".$locale['566']."' style='border:0px;' /></a></td>\n";
		}
		else {
			if ($post_check >= $global_data['global_max_posts'] && $global_data['global_setting'] == '2') {
				echo "<td align='right'<span class='button' style='padding:4px 0px 2px 0px'>&nbsp;".$locale['FLIM402']."&nbsp;</span></td>\n"; //no new posts warning
			}
			else {
				echo "<td align='right'><span class='button' style='padding:4px 0px 2px 0px'>&nbsp;".$locale['FLIM401']."&nbsp;</span></td>\n"; // no new threads warning
			}
		}
	}

	
Save and close the file.

7. Open the file ../forum/viewthread.php in a editor of your choice and find the code below (approx. line 21 of the original file):

include LOCALE.LOCALESET."forum/main.php";

Immediately below add the following code:

// seven new lines for FLIM, added code block to prevent posting if limit for all posts is reached is transfered to include
if (iMEMBER) {
	include INFUSIONS."forum_limit/infusion_db.php";
	include INFUSIONS."forum_limit/includes/forum_limit_include.php";

	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 166):

		if (!flood_control("post_datestamp", DB_POSTS, "post_author='".$userdata['user_id']."'")) {

Immediately AFTER add this code:

			if ($post_check < $global_data['global_max_posts'] && $global_data['global_setting'] == '2') {

			
In the same file find the code below (approx. line 174):

			redirect("postify.php?post=reply&error=0&forum_id=".$fdata['forum_id']."&thread_id=".$_GET['thread_id']."&post_id=$newpost_id");

Immediately AFTER add this code:

		}

In the same file find the code below (approx. lines 194-200):

			echo "<a href='post.php?action=reply&amp;forum_id=".$fdata['forum_id']."&amp;thread_id=".$_GET['thread_id']."'><img src='".get_image("reply")."' alt='".$locale['565']."' style='border:0px' /></a>\n";
		}
		if ($can_post) {
			echo "<a href='post.php?action=newthread&amp;forum_id=".$fdata['forum_id']."'><img src='".get_image("newthread")."' alt='".$locale['566']."' style='border:0px' /></a>\n</td>\n";
		}
	}
	echo "</tr>\n</table>\n";
   
REPLACE this code with the following code:

			if (($post_check<$global_data['global_max_posts'] && $global_data['global_setting'] == '2') || $global_data['global_setting'] != '2') {
				echo "<a href='post.php?action=reply&amp;forum_id=".$fdata['forum_id']."&amp;thread_id=".$_GET['thread_id']."'><img src='".get_image("reply")."' alt='".$locale['565']."' style='border:0px' /></a>\n";
			}
		}
		$forumlimit=dbarray(dbquery("SELECT * FROM ".DB_FORUM_LIMIT_SETTINGS." WHERE forum_no='".$fdata['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_FORUM_THREAD_CREATION." WHERE forum_no='".$fdata['forum_id']."' AND creation_datestamp>'".$forum_timelimit."'");
		$forum_limit_user = dbquery("SELECT * FROM ".DB_FORUM_THREAD_CREATION." WHERE forum_no='".$fdata['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 ((($post_check < $global_data['global_max_posts'] && $global_data['global_setting'] != '0') || $global_data['global_setting'] == '0') && (($rows_f < $forumlimit['max_threads'] && $rows_u < $forumlimit['max_per_user']) || $forumlimit['max_threads'] == '' || $forumlimit['max_per_user'] == '')) {
			if ($can_post) {
				echo "<a href='post.php?action=newthread&amp;forum_id=".$fdata['forum_id']."'><img src='".get_image("newthread")."' alt='".$locale['566']."' style='border:0px' /></a>\n</td>\n";
			}
		}
		else {
			if ($post_check >= $global_data['global_max_posts'] && $global_data['global_setting'] == '2') {
				echo "<span class='button' style='padding:4px 0px 2px 0px'>&nbsp;".$locale['FLIM402']."&nbsp;</span>\n<div style='margin:5px'></div>\n"; // no new post warning
			}
			else {
				echo "<span class='button' style='padding:4px 0px 2px 0px'>&nbsp;".$locale['FLIM401']."&nbsp;</span>\n<div style='margin:5px'></div>\n"; // no new threads warning
			}
		}
	}
	echo "</tr>\n</table>\n";


In the same file find the code below (approx. line 343):

				echo "<a href='post.php?action=reply&amp;forum_id=".$data['forum_id']."&amp;thread_id=".$data['thread_id']."&amp;post_id=".$data['post_id']."&amp;quote=".$data['post_id']."'><img src='".get_image("quote")."' alt='".$locale['569']."' style='border:0px;vertical-align:middle' /></a>\n";
   
REPLACE this code with the following code:

if (($post_check < $global_data['global_max_posts'] && $global_data['global_setting'] == '2') || $global_data['global_setting'] != '2') {
				echo "<a href='post.php?action=reply&amp;forum_id=".$data['forum_id']."&amp;thread_id=".$data['thread_id']."&amp;post_id=".$data['post_id']."&amp;quote=".$data['post_id']."'><img src='".get_image("quote")."' alt='".$locale['569']."' style='border:0px;vertical-align:middle' /></a>\n";
}

In the same file find the code below (approx. lines 215-224):

		echo "<a href='post.php?action=reply&amp;forum_id=".$fdata['forum_id']."&amp;thread_id=".$_GET['thread_id']."'><img src='".get_image("reply")."' alt='".$locale['565']."' style='border:0px' /></a>\n";
	}
	if ($can_post) {
		echo "<a href='post.php?action=newthread&amp;forum_id=".$fdata['forum_id']."'><img src='".get_image("newthread")."' alt='".$locale['566']."' style='border:0px' /></a>\n";
	}
	echo "</td>\n</tr>\n</table>\n";
}
closetable();

if (iMEMBER && $can_reply && !$fdata['thread_locked']) {

REPLACE this code with the following code:

// one new line for FLIM
		if (($post_check < $global_data['global_max_posts'] && $global_data['global_setting'] == '2') || $global_data['global_setting'] != '2') {

			echo "<a href='post.php?action=reply&amp;forum_id=".$fdata['forum_id']."&amp;thread_id=".$_GET['thread_id']."'><img src='".get_image("reply")."' alt='".$locale['565']."' style='border:0px' /></a>\n";

// one new accolade for FLIM
		}
	}
// next line added for forum limit
	if ((($post_check < $global_data['global_max_posts'] && $global_data['global_setting'] != '0') || $global_data['global_setting'] == '0') && (($rows_f < $forumlimit['max_threads'] && $rows_u < $forumlimit['max_per_user']) || $forumlimit['max_threads'] == '' || $forumlimit['max_per_user'] == '')) {

		if ($can_post) {
			echo "<a href='post.php?action=newthread&amp;forum_id=".$fdata['forum_id']."'><img src='".get_image("newthread")."' alt='".$locale['566']."' style='border:0px' /></a>\n";
		}

// next block added for forum limit
	}
	else {
		if ($post_check >= $global_data['global_max_posts'] && $global_data['global_setting'] == '2') {
			echo "<span class='button' style='padding:4px 0px 2px 0px'>&nbsp;".$locale['FLIM402']."&nbsp;</span>\n<div style='margin:5px'></div>\n"; // no new posts warning
		}
		else {
			echo "<span class='button' style='padding:4px 0px 2px 0px'>&nbsp;".$locale['FLIM401']."&nbsp;</span>\n<div style='margin:5px'></div>\n"; // no new threads warning
		}
	} // end block

	echo "</td>\n</tr>\n</table>\n";
}
closetable();

//one line edited for forum_coc
if (iMEMBER && $can_reply && !$fdata['thread_locked'] && ($data1['fcoc_on'] == '0' OR ($data['coc_status'] == '1' && $present-$data['coc_datestamp']<=$data1['flushcoc_time']))) {
  
Save and close the file.


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

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

10. 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
---------
v1.00
* Code completely updated for compatibility with PHP-Fusion v7.
* Few minor code corrections for calculations.

v0.32
* Modded version of ../forum/viewthread.php updated after core update of PHP-Fusion.

v0.31 (alpha_12102007)
* Correction of several conditional errors that resulted in a situation that members could not post when they should be able to or that the quick reply box did not work.
* Bug in storing of user settings for a particular forum corrected.
* Infused link to admin panel corrected.

v0.30 (alpha_24072007)
* Introduction to set a global limit on the number of posts a user can do within a certain period.
* Several improvements to admin system (saving settings, deletion/moving threads being taken into account).
* Thread limitation working better with 0 settings.

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.