<?php
/*****************************************************************************
 *   Remove Restrictions (/inc/plugins/myplaza/rmrestrict.php)
 *     - MyPlaza for MyBB 1.2
 *    By ZiNgA BuRgA, 2007
 * 
 * Allows users to pay to remove/lessen board/usergroup restrictions.
 *****************************************************************************/

if(!defined("IN_MYBB"))
	die("This file cannot be accessed directly.");

function rmrestrict_lang()
{
	global $mybb, $lang;
	switch($mybb->settings['bblanguage'])
	{
		default:
			$lang->rmrestrict_pmquota_htmlextra = 'This will increase your PM quota by %1$s PM(s).';
			$lang->rmrestrict_attachquota_htmlextra = 'This will increase your Attachment quota by %1$s.';
			$lang->rmrestrict_postflood_htmlextra = 'This will decrease post flooding times by %1$s second(s).';
			$lang->rmrestrict_sigsize_htmlextra = 'This will increase the maximum number of allowable characters in your signature by %1$s.';
			$lang->rmrestrict_sigimg_htmlextra = 'This will allow you to add up to %1$s more images to your signature.';
			$lang->rmrestrict_avatarsize_htmlextra = 'This will increase your maximum avatar filesize by %1$s.';
			$lang->rmrestrict_avatardim_htmlextra = 'This will increase your maximum avatar dimensions by %1$sx%1$s.';
			$lang->rmrestrict_minpostlen_htmlextra = 'This will lower the minimum length of posts by %1$s character(s).';
			$lang->rmrestrict_maxpostlen_htmlextra = 'This will increase the maximum length of posts by %1$s character(s).';
			$lang->rmrestrict_maxpostimg_htmlextra = 'This will increase the maximum number of images you can place per post by %1$s.';
			$lang->rmrestrict_maxpostattach_htmlextra = 'This will increase the maximum number of attachments allowable per posts by %1$s.';
			$lang->rmrestrict_maxthumbdim_htmlextra = 'This will increase the maximum dimensions for thumbnails in attached images by %1$sx%1$s.';
			$lang->rmrestrict_minedittime_htmlextra = 'This will decrease the minimum time between post edits by %1$s minute(s).';
			$lang->rmrestrict_maxpolllen_htmlextra = 'This will increase the maximum number of allowable characters in poll options by %1$s.';
			$lang->rmrestrict_maxpollopt_htmlextra = 'This will increase the maximum number of allowable poll options by %1$s.';
			$lang->rmrestrict_maxrep_htmlextra = 'This will increase your maximum reputation power by %1$s.';
			$lang->rmrestrict_searchflood_htmlextra = 'This will decrease the search flood time by %1$s second(s).';
			
			
	}
}

function rmrestrict_lang_admin()
{
	global $mybb, $lang;
	switch($mybb->settings['bblanguage'])
	{
		default:
			$lang->rmrestrict_name = 'Remove/Lessen Restrictions';
			$lang->rmrestrict_description = 'Allows users to pay to remove/lessen board/usergroup restrictions.';
			
			$lang->item_rmrestrict_pmquota_name = 'Extra PM Quota';
			$lang->item_rmrestrict_pmquota_desc = 'Increase your total PM quota.';
			$lang->item_rmrestrict_attachquota_name = 'Extra Attachment Space';
			$lang->item_rmrestrict_attachquota_desc = 'Increase your attachment quota.';
			$lang->item_rmrestrict_postflood_name = 'Reduce Flood Time';
			$lang->item_rmrestrict_postflood_desc = 'Reduces the flood time between making posts.';
			$lang->item_rmrestrict_sigsize_name = 'Increase signature size';
			$lang->item_rmrestrict_sigsize_desc = 'Allows you to put more characters into your signature.';
			$lang->item_rmrestrict_sigimg_name = 'Extra images in signature';
			$lang->item_rmrestrict_sigimg_desc = 'Increases the number of images you may put into your signature.';
			$lang->item_rmrestrict_avatarsize_name = 'Increase max avatar size';
			$lang->item_rmrestrict_avatarsize_desc = 'Allows you to upload larger (in filesize) avatars.';
			$lang->item_rmrestrict_avatardim_name = 'Increase max avatar dimensions';
			$lang->item_rmrestrict_avatardim_desc = 'Increases the maximum allowable avatar dimensions.';
			$lang->item_rmrestrict_minpostlen_name = 'Decrease minimum post length';
			$lang->item_rmrestrict_minpostlen_desc = 'Decreases the minimum number of characters allowed in a post.';
			$lang->item_rmrestrict_maxpostlen_name = 'Increase maximum post length';
			$lang->item_rmrestrict_maxpostlen_desc = 'Increases the maximum number of characters allowed in a post.';
			$lang->item_rmrestrict_maxpostimg_name = 'Increase maximum images per posts';
			$lang->item_rmrestrict_maxpostimg_desc = 'Increases the maximum number of allowable images in a post.';
			$lang->item_rmrestrict_maxpostattach_name = 'Increase maximum attachments per posts';
			$lang->item_rmrestrict_maxpostattach_desc = 'Increases the maximum number of allowable attachments in a post.';
			$lang->item_rmrestrict_maxthumbdim_name = 'Increase attachment thumbnail size';
			$lang->item_rmrestrict_maxthumbdim_desc = 'Increases the maximum dimensions of thumbnails in an attached image.';
			$lang->item_rmrestrict_minedittime_name = 'Decrease edit time limit';
			$lang->item_rmrestrict_minedittime_desc = 'Decreases the minimum time between post edits.';
			$lang->item_rmrestrict_maxpolllen_name = 'Increase maximum poll length';
			$lang->item_rmrestrict_maxpolllen_desc = 'Increases the maximum number of characters allowed in poll options.';
			$lang->item_rmrestrict_maxpollopt_name = 'Increase maximum poll options';
			$lang->item_rmrestrict_maxpollopt_desc = 'Allows you to add more options to a poll.';
			$lang->item_rmrestrict_maxrep_name = 'Increase maximum reputation power';
			$lang->item_rmrestrict_maxrep_desc = 'Increases the maximum reputation power you have.';
			$lang->item_rmrestrict_searchflood_name = 'Decrease search flooding';
			$lang->item_rmrestrict_searchflood_desc = 'Decreases the search flood time.';
			$lang->item_rmrestrict_cancustomtitle_name = 'Enable changing Usertitle';
			$lang->item_rmrestrict_cancustomtitle_desc = 'Allows you to change your usertitle at any time.';
			$lang->item_rmrestrict_canchangename_name = 'Enable changing Username';
			$lang->item_rmrestrict_canchangename_desc = 'Allows you to change your username at any time.';
			$lang->item_rmrestrict_canviewwolinvis_name = 'Enable viewing hidden members';
			$lang->item_rmrestrict_canviewwolinvis_desc = 'Allows you to see invisible members in the Who\'s Online list.';
			$lang->item_rmrestrict_canviewonline_name = 'Enable viewing Who\'s Online';
			$lang->item_rmrestrict_canviewonline_desc = 'Allows you to see the Who\'s Online list.';
			$lang->item_rmrestrict_canaddpublicevents_name = 'Enable adding Public Events to Calendar';
			$lang->item_rmrestrict_canaddpublicevents_desc = 'Allows you to add Public Events to the Calendar.';
			$lang->item_rmrestrict_canviewmemberlist_name = 'Enable viewing Member List';
			$lang->item_rmrestrict_canviewmemberlist_desc = 'Allows you to see the Member List.';
			$lang->item_rmrestrict_cansendemail_name = 'Enable Email sending';
			$lang->item_rmrestrict_cansendemail_desc = 'Allows you to send Emails to other members.';
			$lang->item_rmrestrict_cansendpms_name = 'Enable PM sending';
			$lang->item_rmrestrict_cansendpms_desc = 'Allows you to send Private Messages to other members.';
			$lang->item_rmrestrict_cantrackpms_name = 'Enable PM Trakcing';
			$lang->item_rmrestrict_cantrackpms_desc = 'Allows you to request Read Receipts to PMs you send.';
			$lang->item_rmrestrict_candenypmreceipts_name = 'Enable PM Receipt Denial';
			$lang->item_rmrestrict_candenypmreceipts_desc = 'Allows you to deny PM read receipts.';
			$lang->item_rmrestrict_canpostpolls_name = 'Enable Poll posting';
			$lang->item_rmrestrict_canpostpolls_desc = 'Allows you to post Polls.';
			$lang->item_rmrestrict_candeleteposts_name = 'Enable post deleting';
			$lang->item_rmrestrict_candeleteposts_desc = 'Allows you to delete posts.';
			$lang->item_rmrestrict_candeletethreads_name = 'Enable thread deleting';
			$lang->item_rmrestrict_candeletethreads_desc = 'Allows you to delete threads.';
			$lang->item_rmrestrict_caneditposts_name = 'Enable post editing';
			$lang->item_rmrestrict_caneditposts_desc = 'Allows you to edit posts.';
			$lang->item_rmrestrict_canratethreads_name = 'Enable thread rating';
			$lang->item_rmrestrict_canratethreads_desc = 'Allows you to rate threads.';
			$lang->item_rmrestrict_canpostattachments_name = 'Enable attachment posting';
			$lang->item_rmrestrict_canpostattachments_desc = 'Allows you to add/post attachments.';
			$lang->item_rmrestrict_canpostthreads_name = 'Enable thread posting';
			$lang->item_rmrestrict_canpostthreads_desc = 'Allows you to start new threads.';
			$lang->item_rmrestrict_candlattachments_name = 'Enable attachment downloading';
			$lang->item_rmrestrict_candlattachments_desc = 'Allows you to download attachments.';
			$lang->item_rmrestrict_canviewprofiles_name = 'Enable Profile viewing';
			$lang->item_rmrestrict_canviewprofiles_desc = 'Allows you to view the Profiles of other members.';
			
			$lang->setting_rmrestrict_pmquota = 'Increase PM Quota by (PMs)';
			$lang->setting_rmrestrict_pmquota_desc = 'The amount to increase a user\'s PM Quota by, when they buy the related item.';
			$lang->setting_rmrestrict_attachquota = 'Increase Attachment Quota by (bytes)';
			$lang->setting_rmrestrict_attachquota_desc = 'The amount to increase a user\'s Attachment Quota by, when they buy the related item.';
			$lang->setting_rmrestrict_postflood = 'Decrease Post Flood Time by (seconds)';
			$lang->setting_rmrestrict_postflood_desc = 'The amount to decrease a user\'s Post Flood time by, when they buy the related item.';
			$lang->setting_rmrestrict_sigsize = 'Increase Max Signature Length by (characters)';
			$lang->setting_rmrestrict_sigsize_desc = 'The amount to increase a user\'s Maximum Signature Length by, when they buy the related item.';
			$lang->setting_rmrestrict_sigimg = 'Increase Max Signature Images by (images)';
			$lang->setting_rmrestrict_sigimg_desc = 'The amount to increase the number of images a user may place in his/her signature, when they buy the related item.';
			$lang->setting_rmrestrict_avatarsize = 'Increase Max Avatar Filesize by (bytes)';
			$lang->setting_rmrestrict_avatarsize_desc = 'The amount to increase the maximum acceptable filesize for an avatar for a user, when they buy the related item.';
			$lang->setting_rmrestrict_avatardim = 'Increase Max Avatar Dimensions by (pixels)';
			$lang->setting_rmrestrict_avatardim_desc = 'The amount to increase the maximum acceptable dimensions for an avatar for a user, when they buy the related item.  Note that this value applies to both height and width.';
			$lang->setting_rmrestrict_minpostlen = 'Decrease Minimum Message Length by (characters)';
			$lang->setting_rmrestrict_minpostlen_desc = 'The amount to decrease the minimum acceptable length for a message, when the user buys the related item.';
			$lang->setting_rmrestrict_maxpostlen = 'Increase Maximum Message Length by (characters)';
			$lang->setting_rmrestrict_maxpostlen_desc = 'The amount to increase the maximum acceptable length for a message, when the user buys the related item.';
			$lang->setting_rmrestrict_maxpostimg = 'Increase Maximum Post Images by (images)';
			$lang->setting_rmrestrict_maxpostimg_desc = 'The amount to increase the maximum number of images acceptable for a post, when the user buys the related item.';
			$lang->setting_rmrestrict_maxpostattach = 'Increase Maximum Post Attachments by (attachments)';
			$lang->setting_rmrestrict_maxpostattach_desc = 'The amount to increase the maximum number of attachments acceptable for a post, when the user buys the related item.';
			$lang->setting_rmrestrict_maxthumbdim = 'Increase Maximum Thumbnail Dimensions by (pixels)';
			$lang->setting_rmrestrict_maxthumbdim_desc = 'The amount to increase the maximum attachment thumbnail dimensions, when the user buys the related item.';
			$lang->setting_rmrestrict_minedittime = 'Decrease Minimum Edit Time by (minutes)';
			$lang->setting_rmrestrict_minedittime_desc = 'The amount to decrease the minimum edit time, when the user buys the related item.';
			$lang->setting_rmrestrict_maxpolllen = 'Increase Maximum Poll Option Length by (characters)';
			$lang->setting_rmrestrict_maxpolllen_desc = 'The amount to increase the maximum length of poll options by, when the user buys the related item.';
			$lang->setting_rmrestrict_maxpollopt = 'Increase Maximum No. Poll Options by (options)';
			$lang->setting_rmrestrict_maxpollopt_desc = 'The amount to increase the maximum number of poll options allowable in a poll, when the user buys the related item.';
			$lang->setting_rmrestrict_maxrep = 'Increase Reputation Power by';
			$lang->setting_rmrestrict_maxrep_desc = 'The amount to increase the maximum reputation power of a user by, when the user buys the related item.';
			$lang->setting_rmrestrict_searchflood = 'Decrease Search Flood time by (seconds)';
			$lang->setting_rmrestrict_searchflood_desc = 'The amount to decrease the search flooding time for a user by, when they buy the related item.';
			
			$lang->rmrestrict_options = 'Restriction Removing Options';
			$lang->edit_user_perms = 'Edit User\'s Permissions';
			$lang->plaza_edit_user_perms_nav = 'User Permissions';
			$lang->userperms_updated = 'User Permissions Updated.';
			$lang->not_set = 'Not Set (Default)';
			
			$lang->quotas_and_space = 'Quotas and Space';
			$lang->floodcontrol = 'Flood Control';
			$lang->sig_and_ava = 'Signature and Avatar';
			$lang->posting_restrictions = 'Posting Restrictions';
			$lang->reputation = 'Reputation';
			$lang->name_and_title = 'Name and Title';
			$lang->page_viewing = 'Page Viewing';
			$lang->interaction_perms = 'Interaction Permissions';
			$lang->posting_perms = 'Posting Permissions';
			
			
			$lang->added_by_rmrestrict = 'Category inserted by Remove/Lessen restrictions module.';
	}
}

function rmrestrict_info()
{
	return array(
		"website"		=> "http://myplaza.zingaburga.com",
		"author"		=> "ZiNgA BuRgA",
		"authorsite"	=> "http://zingaburga.com/",
		"version"		=> "1.0",
		"compatibility"	=> array(0.5)
	);
}

define('RMRESTRICT_NUMERIC_POS', 1);
define('RMRESTRICT_NUMERIC_NEG', 2);
define('RMRESTRICT_NUMERIC_NEG', 2);
define('RMRESTRICT_YESNO', 3);
function rmrestrict_options()
{
	return array(
		'pmquota' => array(
			'cost' => 500,
			'type' => RMRESTRICT_NUMERIC_POS,
			'datatype' => 'int',
			'zerogreater' => true, 
			'valuein' => 'usergroup',
			'valuename' => 'pmquota',
			'setting' => 25
		),
		'attachquota' => array(
			'cost' => 500,
			'type' => RMRESTRICT_NUMERIC_POS,
			'datatype' => 'int',
			'zerogreater' => true, 
			'valuein' => 'usergroup',
			'valuename' => 'attachquota',
			'setting' => 2048
		),
		'postflood' => array(
			'cost' => 500,
			'type' => RMRESTRICT_NUMERIC_NEG,
			'datatype' => 'smallint',
			'zerogreater' => true, 
			'valuein' => 'settings',
			'valuename' => 'postfloodsecs',
			'setting' => 5
		),
		'sigsize' => array(
			'cost' => 500,
			'type' => RMRESTRICT_NUMERIC_POS,
			'datatype' => 'int',
			'zerogreater' => true, 
			'valuein' => 'settings',
			'valuename' => 'siglength',
			'setting' => 100
		),
		'sigimg' => array(
			'cost' => 500,
			'type' => RMRESTRICT_NUMERIC_POS,
			'datatype' => 'smallint',
			'zerogreater' => true, 
			'valuein' => 'settings',
			'valuename' => 'maxsigimages',
			'setting' => 2
		),
		'avatarsize' => array(
			'cost' => 500,
			'type' => RMRESTRICT_NUMERIC_POS,
			'datatype' => 'int',
			'zerogreater' => true, 
			'valuein' => 'settings',
			'valuename' => 'avatarsize',
			'setting' => 51200
		),
		'avatardim' => array(
			'cost' => 500,
			'type' => RMRESTRICT_NUMERIC_POS,
			'datatype' => 'smallint',
			'zerogreater' => true, 
			'valuein' => 'settings',
			'valuename' => 'maxavatardims',
			'setting' => 20
		),
		'minpostlen' => array(
			'cost' => 100,
			'type' => RMRESTRICT_NUMERIC_NEG,
			'datatype' => 'int',
			'zerogreater' => true, 
			'valuein' => 'settings',
			'valuename' => 'minmessagelength',
			'setting' => 5
		),
		'maxpostlen' => array(
			'cost' => 100,
			'type' => RMRESTRICT_NUMERIC_POS,
			'datatype' => 'int',
			'zerogreater' => true, 
			'valuein' => 'settings',
			'valuename' => 'maxmessagelength',
			'setting' => 1000
		),
		'maxpostimg' => array(
			'cost' => 200,
			'type' => RMRESTRICT_NUMERIC_POS,
			'datatype' => 'smallint',
			'zerogreater' => true, 
			'valuein' => 'settings',
			'valuename' => 'maxpostimages',
			'setting' => 20
		),
		'maxpostattach' => array(
			'cost' => 200,
			'type' => RMRESTRICT_NUMERIC_POS,
			'datatype' => 'smallint',
			'zerogreater' => true, 
			'valuein' => 'settings',
			'valuename' => 'maxattachments',
			'setting' => 3
		),
		'maxthumbdim' => array(
			'cost' => 200,
			'type' => RMRESTRICT_NUMERIC_POS,
			'datatype' => 'smallint',
			'zerogreater' => true, 
			'valuein' => 'settings',
			'valuename' => 'attachthumb',
			'setting' => 20
		),
		'minedittime' => array(
			'cost' => 100,
			'type' => RMRESTRICT_NUMERIC_NEG,
			'datatype' => 'int',
			'zerogreater' => true, 
			'valuein' => 'settings',
			'valuename' => 'edittimelimit',
			'setting' => 2
		),
		'maxpolllen' => array(
			'cost' => 200,
			'type' => RMRESTRICT_NUMERIC_POS,
			'datatype' => 'int',
			'zerogreater' => true, 
			'valuein' => 'settings',
			'valuename' => 'polloptionlimit',
			'setting' => 50
		),
		'maxpollopt' => array(
			'cost' => 200,
			'type' => RMRESTRICT_NUMERIC_POS,
			'datatype' => 'smallint',
			'zerogreater' => true, 
			'valuein' => 'settings',
			'valuename' => 'maxpolloptions',
			'setting' => 5
		),
		'maxrep' => array(
			'cost' => 500,
			'type' => RMRESTRICT_NUMERIC_POS,
			'datatype' => 'smallint',
			'zerogreater' => false, 
			'valuein' => 'usergroup',
			'valuename' => 'reputationpower',
			'setting' => 1
		),
		'searchflood' => array(
			'cost' => 500,
			'type' => RMRESTRICT_NUMERIC_POS,
			'datatype' => 'int',
			'zerogreater' => true, 
			'valuein' => 'settings',
			'valuename' => 'searchfloodtime',
			'setting' => 10
		),
		'cancustomtitle' => array(
			'cost' => 2000,
			'type' => RMRESTRICT_YESNO,
			'valuein' => 'usergroup',
			'valuename' => 'cancustomtitle'
		),
		'canchangename' => array(
			'cost' => 20000,
			'type' => RMRESTRICT_YESNO,
			'valuein' => 'usergroup',
			'valuename' => 'canchangename'
		),
		'canviewwolinvis' => array(
			'cost' => 2000,
			'type' => RMRESTRICT_YESNO,
			'valuein' => 'usergroup',
			'valuename' => 'canviewwolinvis'
		),
		'canviewonline' => array(
			'cost' => 200,
			'type' => RMRESTRICT_YESNO,
			'valuein' => 'usergroup',
			'valuename' => 'canviewonline'
		),
		'canaddpublicevents' => array(
			'cost' => 100,
			'type' => RMRESTRICT_YESNO,
			'valuein' => 'usergroup',
			'valuename' => 'canaddpublicevents'
		),
		'canviewmemberlist' => array(
			'cost' => 50,
			'type' => RMRESTRICT_YESNO,
			'valuein' => 'usergroup',
			'valuename' => 'canviewmemberlist'
		),
		'cansendemail' => array(
			'cost' => 20,
			'type' => RMRESTRICT_YESNO,
			'valuein' => 'usergroup',
			'valuename' => 'cansendemail'
		),
		'cansendpms' => array(
			'cost' => 20,
			'type' => RMRESTRICT_YESNO,
			'valuein' => 'usergroup',
			'valuename' => 'cansendpms'
		),
		'cantrackpms' => array(
			'cost' => 50,
			'type' => RMRESTRICT_YESNO,
			'valuein' => 'usergroup',
			'valuename' => 'cantrackpms'
		),
		'candenypmreceipts' => array(
			'cost' => 50,
			'type' => RMRESTRICT_YESNO,
			'valuein' => 'usergroup',
			'valuename' => 'candenypmreceipts'
		),
		'canpostpolls' => array(
			'cost' => 50,
			'type' => RMRESTRICT_YESNO,
			'valuein' => 'usergroup',
			'valuename' => 'canpostpolls'
		),
		'candeleteposts' => array(
			'cost' => 10,
			'type' => RMRESTRICT_YESNO,
			'valuein' => 'usergroup',
			'valuename' => 'candeleteposts'
		),
		'candeletethreads' => array(
			'cost' => 10,
			'type' => RMRESTRICT_YESNO,
			'valuein' => 'usergroup',
			'valuename' => 'candeletethreads'
		),
		'caneditposts' => array(
			'cost' => 5,
			'type' => RMRESTRICT_YESNO,
			'valuein' => 'usergroup',
			'valuename' => 'caneditposts'
		),
		'canratethreads' => array(
			'cost' => 5,
			'type' => RMRESTRICT_YESNO,
			'valuein' => 'usergroup',
			'valuename' => 'canratethreads'
		),
		'canpostattachments' => array(
			'cost' => 20,
			'type' => RMRESTRICT_YESNO,
			'valuein' => 'usergroup',
			'valuename' => 'canpostattachments'
		),
		'canpostthreads' => array(
			'cost' => 20,
			'type' => RMRESTRICT_YESNO,
			'valuein' => 'usergroup',
			'valuename' => 'canpostthreads'
		),
		'candlattachments' => array(
			'cost' => 20,
			'type' => RMRESTRICT_YESNO,
			'valuein' => 'usergroup',
			'valuename' => 'candlattachments'
		),
		'canviewprofiles' => array(
			'cost' => 20,
			'type' => RMRESTRICT_YESNO,
			'valuein' => 'usergroup',
			'valuename' => 'canviewprofiles'
		),
	);
}

function get_rmcat($name)
{
	switch($name)
	{
		case 'pmquota': case 'attachquota':
			return 'quotas_and_space';
		case 'postflood': case 'minedittime': case 'searchflood':
			return 'floodcontrol';
		case 'sigsize': case 'sigimg': case 'avatarsize': case 'avatardim':
			return 'sig_and_ava';
		case 'minpostlen': case 'maxpostlen': case 'maxpostimg': case 'maxpostattach': case 'maxthumbdim': case 'maxpolllen': case 'maxpollopt':
			return 'posting_restrictions';
		case 'maxrep':
			return 'reputation';
		case 'cancustomtitle': case 'canchangename':
			return 'name_and_title';
		case 'canviewwolinvis': case 'canviewonline': case 'canviewmemberlist': case 'candlattachments': case 'canviewprofiles':
			return 'page_viewing';
		case 'canaddpublicevents': case 'cansendemail': case 'cansendpms': case 'cantrackpms': case 'candenypmreceipts': case 'canratethreads':
			return 'interaction_perms';
		case 'canpostpolls': case 'candeleteposts': case 'candeletethreads': case 'caneditposts': case 'canpostattachments': case 'canpostthreads':
			return 'posting_perms';
	}
}

function rmrestrict_options_typed()
{
	$options = rmrestrict_options();
	$rtn = array();
	foreach($options as $name => $info)
		$rtn[get_rmcat($name)][$name] = $info;
	return $rtn;
}


function rmrestrict_activate()
{
	// stop people trying to activate this module as a MyBB plugin
	if(!defined("IN_MYPLAZA_ADMIN"))
		cperror('This is not a normal MyBB plugin!  Please upload this file to your /inc/plugins/myplaza directory.');
	
	global $db, $lang;
	
	$cats = array('quotas_and_space', 'floodcontrol', 'sig_and_ava', 'posting_restrictions', 'reputation', 'name_and_title', 'page_viewing', 'interaction_perms', 'posting_perms');
	$cids = array();
	foreach($cats as $cat)
	{
		$db->insert_query(MY_TABLE_PREFIX.'plaza_cat', array(
			'name' => $lang->$cat,
			'description' => $lang->added_by_rmrestrict,
			'visiblegroups' => 'all'
		));
		$cids[$cat] = $db->insert_id();
	}
	$options = rmrestrict_options();
	foreach($options as $name => $info)
	{
		$item_add = array(
			'idname' => 'rmrestrict_'.$name,
			'cost' => $info['cost'],
			'visible' => MY_NO, 
			'buylimitamount' => 2,
			'cid' => $cids[get_rmcat($name)]
		);
		
		if($info['type'] != RMRESTRICT_YESNO)
			$item_add['htmlextra'] = '{$lang->rmrestrict_'.$name.'_htmlextra}';
		else
			$item_add['htmlextra'] = '';
		myplaza_add_item($item_add);
	}
	
	// add settings
	$new_settings = array();
	foreach($options as $name => $info)
	{
		if($info['type'] == RMRESTRICT_YESNO) continue;
		$new_settings[] = array(
			'name' => 'rmrestrict_'.$name,
			'optionscode' => 'text',
			'value' => $info['setting']
		);
	}
	add_settings($new_settings);
	unset($new_settings);
	
	// then add columns to user table
	$new_cols = array('uid' => array('type' => 'int', 'size' => 5, 'not_null' => true, 'unsigned' => true));
	foreach($options as $name => $info)
	{
		if($info['type'] == RMRESTRICT_YESNO)
			$new_cols[$name] = array('type' => 'tinyint', 'size' => 3, 'default' => 0, 'not_null' => true);
		else
			$new_cols[$name] = array('type' => $info['datatype'], 'size' => ($info['datatype'] == 'smallint' ? 5 : 10), 'default' => 0, 'not_null' => true);
	}
	db_create_table('plaza_userperms', $new_cols, array(array('fields' => 'uid', 'type' => 'primary')));
	//db_add_columns('users', $new_cols);
}

function rmrestrict_plugin()
{
	global $plugins;
	// due to lack of hooks, we're forced to globally hook this one :(
	if(!defined('IN_ADMINCP'))
	{
		$plugins->add_hook('global_end', 'rmrestrict_execute');
		$plugins->add_hook('private_send_do_send', 'rmrestrict_pmhack');
	}
	else
	{
		$plugins->add_hook('admin_users_edit_userlinks', 'rmrestrict_edituserperms_link', 20);
		$plugins->add_hook('admin_users_start', 'rmrestrict_admin_editperms');
		$plugins->add_hook('admin_users_do_delete', 'rmrestrict_del_userperms');
	}
}

function rmrestrict_deactivate()
{
	$options = rmrestrict_options();
	myplaza_remove_module_items();
	
	global $db, $lang;
	// remove settings
	$settings = array();
	foreach($options as $name => $info)
	{
		if($info['type'] != RMRESTRICT_YESNO)
			$settings[] = $name;
	}
	$db->delete_query(MY_TABLE_PREFIX.'settings', "name IN ('".implode("','", $settings)."')");
	$db->delete_query(MY_TABLE_PREFIX.'plaza_cat', "description='".$db->escape_string($lang->added_by_rmrestrict)."'");
	
	// take out columns in user table
	db_drop_tables(array('plaza_userperms'));
	/*
	$cols = array();
	foreach($options as $name => $info)
		$cols['rmrestrict_'.$name] = 'rmrestrict_'.$name;
	db_remove_columns('users', $cols);
	*/
}


// our PM quota hack
function rmrestrict_pmhack()
{
	global $mybb, $db;
	// we'll have to search for the user
	$uid = $db->fetch_field($db->simple_select(MY_TABLE_PREFIX.'users', 'uid', 'username=\''.$db->escape_string($mybb->input['to']).'\''), 'uid');
	if(!$uid) return;
	
	// get their custom permissions
	$extrapmquota = $db->fetch_field($db->simple_select(MY_TABLE_PREFIX.'plaza_userperms', 'pmquota', 'uid='.$uid), 'pmquota');
	if(!$extrapmquota) return;
	
	// now we'll hack the cache :P
	user_permissions($uid); // first, cache the stuff (if necessary)
	// now modify the cache
	global $user_cache;
	if($user_cache[$uid]['permissions']['pmquota'] != '0')
		$user_cache[$uid]['permissions']['pmquota'] += $extrapmquota;
}

function rmrestrict_execute()
{
	global $mybb, $db;
	if(!$mybb->user['uid']) return;
	$options = rmrestrict_options();
	unset($options['avatardim']); //requires special handling
	unset($options['maxthumbdim']); //requires special handling
	
	$userperms = $db->fetch_array($db->simple_select(MY_TABLE_PREFIX.'plaza_userperms', '*', 'uid='.$mybb->user['uid']));
	if(!$userperms) return; // no custom user permissions for this user
	
	foreach($options as $name => $info)
	{
		$valuein = $info['valuein'];
		$value_array = &$mybb->$valuein; // WTF? PHP doesn't like $mybb->$valuein[] ???
		if($info['type'] == RMRESTRICT_YESNO)
		{
			if($userperms[$name])
				$value_array[$info['valuename']] = ($userperms[$name] == 2 ? MY_YES : MY_NO);
		}
		else
			$value_array[$info['valuename']] = rmrestrict_increase(intval($value_array[$info['valuename']]), $userperms[$name], 0, null, $info['zerogreater']);
	}
	// we  need to do special handling of avatar dimensions
	
	if($mybb->settings['maxavatardims'] && $userperms['avatardim'])
	{
		$dim = explode('x',$mybb->settings['maxavatardims']);
		if(count($dim) == 2)
		{
			$dim[0] = intval($dim[0]) + $userperms['avatardim'];
			$dim[1] = intval($dim[1]) + $userperms['avatardim'];
			if($dim[0] < 1) $dim[0] = 1;
			if($dim[1] < 1) $dim[1] = 1;
			$mybb->settings['maxavatardims'] = $dim[0].'x'.$dim[1];
		}
	}
	if($mybb->settings['attachthumbw'] && $mybb->settings['attachthumbh'] && $userperms['maxthumbdim'])
	{
		$mybb->settings['attachthumbw'] = intval($mybb->settings['attachthumbw']) + $userperms['maxthumbdim'];
		$mybb->settings['attachthumbh'] = intval($mybb->settings['attachthumbh']) + $userperms['maxthumbdim'];
		if($mybb->settings['attachthumbw'] < 1) $mybb->settings['attachthumbw'] = 1;
		if($mybb->settings['attachthumbh'] < 1) $mybb->settings['attachthumbh'] = 1;
	}
}


function rmrestrict_increase($value, $extra, $min = null, $max = null, $zero_greater = false)
{
	if((!$zero_greater || $value > 0) && $extra)
	{
		$value += $extra;
		if($max !== null && $value > $max)
			$value = $max;
		if($min !== null && $value < $min)
			$value = $min;
	}
	return $value;
}


function rmrestrict_admin($process)
{
	global $mybb, $lang, $db;
	
	$opt = rmrestrict_options();
	$options = array();
	if($process)
	{
		// remove yes/no switches
		foreach($opt as $name => $info)
			if($info['type'] != RMRESTRICT_YESNO)
				$options['rmrestrict_'.$name] = 'rmrestrict_'.$name;
		unset($opt);
		
		myplaza_process_settings($options);
	}
	else
	{
		// remove yes/no switches
		foreach($opt as $name => $info)
			if($info['type'] != RMRESTRICT_YESNO)
				$options[get_rmcat($name)]['rmrestrict_'.$name] = 'rmrestrict_'.$name;
		unset($opt);
		
		cpheader_myplaza_module();
		starttable();
		tableheader($lang->rmrestrict_options);
		foreach($options as $sect => $opts)
		{
			tablesubheader($lang->$sect);
			myplaza_generate_settings_code($opts);
		}
		endtable();
		cpfooter_myplaza_module();
	}
}

function rmrestrict_htmlextra()
{
	global $lang, $mybb;
	$options = rmrestrict_options();
	
	// handle special cases here (filesize formatting):
	$lang->rmrestrict_attachquota_htmlextra = sprintf($lang->rmrestrict_attachquota_htmlextra, get_friendly_size(intval($mybb->settings['rmrestrict_attachquota'])));
	$lang->rmrestrict_avatarsize_htmlextra = sprintf($lang->rmrestrict_avatarsize_htmlextra, get_friendly_size(intval($mybb->settings['rmrestrict_avatarsize'])));
	unset($options['attachquota']);
	unset($options['avatarsize']);
	
	// do the rest
	foreach($options as $name => $info)
		if($info['type'] != RMRESTRICT_YESNO)
		{
			$langvar = 'rmrestrict_'.$name.'_htmlextra';
			$valuein = $info['valuein'];
			$value_array = &$mybb->$valuein;
			$lang->$langvar = sprintf($lang->$langvar, intval($value_array['rmrestrict_'.$name]));
		}
	
}

function rmrestrict_run($item)
{
	if(substr($item['idname'], 0, 11) != 'rmrestrict_') return false;
	$options = rmrestrict_options();
	$name = substr($item['idname'], 11);
	$options = $options[$name];
	if(empty($options)) return false;
	
	global $mybb, $buyRtnMsg, $lang, $db;
	$perm = $db->fetch_field($db->simple_select(MY_TABLE_PREFIX.'plaza_userperms', $name, 'uid='.$mybb->user['uid']), $name);
	if($options['type'] == RMRESTRICT_YESNO)
	{
		if(isset($perm))
		{
			if($perm == 2)
			{
				$buyRtnMsg = $lang->already_enabled;
				return false;
			}
			else
				$db->update_query(MY_TABLE_PREFIX.'plaza_userperms', array($name => 2), 'uid='.$mybb->user['uid']);
		}
		else
		{
			$valuein = $options['valuein'];
			$value_array = &$mybb->$valuein;
			if($value_array[$info['valuename']] == MY_YES)
			{
				$buyRtnMsg = $lang->already_enabled;
				return false;
			}
			else
				$db->insert_query(MY_TABLE_PREFIX.'plaza_userperms', array('uid' => $mybb->user['uid'], $name => 2));
		}
	}
	else
	{
		$value = intval($mybb->settings[$item['idname']]);
		if(isset($perm))
			db_update('plaza_userperms', array($name => $name.'+'.$value), 'uid='.$mybb->user['uid']);
		else
			$db->insert_query(MY_TABLE_PREFIX.'plaza_userperms', array('uid' => $mybb->user['uid'], $name => $value));
	}
	return true;
}

function rmrestrict_admin_editperms()
{
	global $mybb, $db, $lang;
	if($mybb->input['action'] == 'editperms' || $mybb->input['action'] == 'do_editperms')
	{
		myplaza_langload('rmrestrict');
		addacpnav($lang->plaza_edit_user_perms_nav);
		
		$uid = intval($mybb->input['uid']);
		$userperms = $db->fetch_array($db->simple_select(MY_TABLE_PREFIX.'plaza_userperms', '*', 'uid='.$uid));
		if(!$userperms) $userperms = array();
	}
	
	if($mybb->input['action'] == 'editperms')
	{
		$lang->load('usergroups');
		$lang->load('myplaza');
		cpheader();
		startform('users.php', "", "do_editperms");
		makehiddencode('uid', $uid);
		starttable();
		tableheader($lang->plaza_edit_user_perms_nav);
		
		$options = rmrestrict_options_typed();
		
		
		// translate our variable names to MyBB's langvars
		$langvars = array(
			'canviewprofiles' => 'can_view_profiles',
			'candlattachments' => 'can_download_attachments',
			'canpostpolls' => 'can_post_polls',
			'canpostthreads' => 'can_post_threads',
			'canratethreads' => 'can_rate_threads',
			'canpostattachments' => 'can_post_attachments',
			'caneditposts' => 'can_edit_posts',
			'candeleteposts' => 'can_delete_posts',
			'candeletethreads' => 'can_delete_threads',
			'cansendpms' => 'can_send_pms',
			'cantrackpms' => 'can_track_pms',
			'candenypmreceipts' => 'can_deny_pms',
			'canaddpublicevents' => 'can_add_public',
			'canviewonline' => 'can_view_wol',
			'canviewwolinvis' => 'can_view_invisible',
			'canchangename' => 'can_change_name',
			'cancustomtitle' => 'can_custom_titles',
			'canviewmemberlist' => 'can_view_mlist',
			'cansendemail' => 'can_send_emails'
		);
		
		foreach($options as $sect => $opt)
		{
			tablesubheader($lang->$sect);
			foreach($opt as $name => $info)
			{
				if($info['type'] == RMRESTRICT_YESNO)
				{
					$langvar = $langvars[$name];
					makeyesnonullcode($lang->$langvar, $name, $userperms[$name]);
				}
				else
				{
					$langvar = 'item_rmrestrict_'.$name.'_name';
					makeinputcode($lang->$langvar, $name, $userperms[$name]);
				}
			}
		}
		
		endtable();
		endform($lang->submit_changes, $lang->reset_button);
		cpfooter();
		exit;
	}
	
	if($mybb->input['action'] == 'do_editperms')
	{
		$update_array = array();
		$options = rmrestrict_options();
		foreach($options as $name => $info)
		{
			if(!$mybb->input[$name]) continue;
			$value = intval($mybb->input[$name]);
			if($info['type'] == RMRESTRICT_YESNO)
			{
				if($value == 1 || $value == 2)
					$update_array[$name] = $value;
				else
					$update_array[$name] = 0;
			}
			else
			{
				$update_array[$name] = $value;
			}
		}
		
		// add or update?
		if(!empty($userperms))
			$db->update_query(MY_TABLE_PREFIX.'plaza_userperms', $update_array, 'uid='.$uid);
		else
		{
			$update_array['uid'] = $uid;
			$db->insert_query(MY_TABLE_PREFIX.'plaza_userperms', $update_array);
		}
		
		cpredirect("users.php?".SID."&lastuid={$mybb->input['uid']}", $lang->userperms_updated);
		exit;
	}
}

function rmrestrict_edituserperms_link()
{
	global $userlinks, $user, $lang;
	myplaza_langload('rmrestrict');
	$userlinks = str_replace('</ul>', '<li><a href="users.php?'.SID.'&amp;action=editperms&amp;uid='.$user['uid'].'">'.$lang->edit_user_perms.'</a></li></ul>', $userlinks);
}

function rmrestrict_del_userperms()
{
	global $mybb, $db;
	$db->delete_query(MY_TABLE_PREFIX.'plaza_userperms', 'uid='.intval($mybb->user['uid']));
}


function makeyesnonullcode($title, $name, $value=0)
{
	global $lang;
	$bgcolor = getaltbg();
	if($value == 1)
		$nocheck = ' checked="checked"';
	elseif($value == 2)
		$yescheck = ' checked="checked"';
	else
		$nullcheck = ' checked="checked"';
	echo "<tr>\n<td class=\"$bgcolor\" valign=\"top\" width=\"40%\">$title</td>\n<td class=\"$bgcolor\" valign=\"top\" width=\"60%\"><label><input type=\"radio\" name=\"$name\" value=\"2\"$yescheck />&nbsp;$lang->yes</label> &nbsp;&nbsp;<label><input type=\"radio\" name=\"$name\" value=\"1\"$nocheck />&nbsp;$lang->no</label> &nbsp;&nbsp;<label><input type=\"radio\" name=\"$name\" value=\"0\"$nullcheck />&nbsp;$lang->not_set</label></td>\n</tr>\n";
}

?>