<?php
/*======================================================================*\
|| #################################################################### ||
|| # vBulletin 5.0.0 Beta 10
|| # ---------------------------------------------------------------- # ||
|| # Copyright �2000-2012 vBulletin Solutions Inc. All Rights Reserved. ||
|| # This file may not be redistributed in whole or significant part. # ||
|| # ---------------- VBULLETIN IS NOT FREE SOFTWARE ---------------- # ||
|| # http://www.vbulletin.com | http://www.vbulletin.com/license.html # ||
|| #################################################################### ||
\*======================================================================*/

// ######################## SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);

// ##################### DEFINE IMPORTANT CONSTANTS #######################
define('CVS_REVISION', '$RCSfile$ - $Revision: 62889 $');

// #################### PRE-CACHE TEMPLATES AND DATA ######################
global $phrasegroups, $specialtemplates, $vbphrase, $reputation, $vbulletin;
$phrasegroups = array('reputation', 'user', 'reputationlevel');
$specialtemplates = array();

// ########################## REQUIRE BACK-END ############################
require_once(dirname(__FILE__) . '/global.php');
require_once(DIR . '/includes/adminfunctions_reputation.php');
$assertor = vB::getDbAssertor();

// ######################## CHECK ADMIN PERMISSIONS #######################
if (!can_administer('canadminusers'))
{
	print_cp_no_permission();
}

$vbulletin->input->clean_array_gpc('r', array(
	'reputationlevelid' => vB_Cleaner::TYPE_INT,
	'minimumreputation' => vB_Cleaner::TYPE_INT,
));

// ############################# LOG ACTION ###############################
log_admin_action(iif($vbulletin->GPC['reputationlevelid'] != 0, " reputationlevel id = " . $vbulletin->GPC['reputationlevelid'], iif($vbulletin->GPC['minimumreputation'] != 0, "minimum reputation = " . $vbulletin->GPC['minimumreputation'], '')));

// ########################################################################
// ######################### START MAIN SCRIPT ############################
// ########################################################################

print_cp_header($vbphrase['user_reputation_manager_greputation']);

// *************************************************************************************************

if (empty($_REQUEST['do']))
{
	$_REQUEST['do'] = 'modify';
}

// *************************************************************************************************

if ($_REQUEST['do'] == 'add' OR $_REQUEST['do'] == 'edit')
{
	$vbulletin->input->clean_array_gpc('r', array(
		'reputationlevelid' => vB_Cleaner::TYPE_INT
	));

	print_form_header('adminreputation', 'update');
	if ($vbulletin->GPC['reputationlevelid'])
	{
		$reputationlevel = $assertor->getRow('vBForum:reputationlevel', array('reputationlevelid' => $vbulletin->GPC['reputationlevelid']));

		$level = 'reputation' . $reputationlevel['reputationlevelid'];

		$phrase = $assertor->getRow('vBForum:phrase', array('languageid' => 0, 'fieldname' => 'reputationlevel', 'varname' => $level));
		if (is_array($phrase) AND !isset($phrase['errors']))
		{
			$reputationlevel['level'] = $phrase['text'];
			$reputationlevel['levelvarname'] = 'reputation' . $reputationlevel['reputationlevelid'];
		}

		construct_hidden_code('reputationlevelid', $vbulletin->GPC['reputationlevelid']);
		construct_hidden_code('oldminimum', $reputation['minimumreputation']);
		print_table_header(construct_phrase($vbphrase['x_y_id_z'], $vbphrase['reputation_level_guser'], '<i>' . htmlspecialchars_uni($reputationlevel['level']) . '</i>', $reputationlevel['minimumreputation']));
	}
	else
	{
		print_table_header($vbphrase['add_new_reputation_level']);
	}

	if ($reputationlevel['level'])
	{
		print_input_row($vbphrase['description_gcpglobal'] . '<dfn>' . construct_link_code($vbphrase['translations'], "phrase.php?" . vB::getCurrentSession()->get('sessionurl') . "do=edit&fieldname=reputationlevel&varname=$reputationlevel[levelvarname]&t=1", 1)  . '</dfn>', 'level', $reputationlevel['level']);
	}
	else
	{
		print_input_row($vbphrase['description_gcpglobal'], 'level');
	}
	print_input_row($vbphrase['minimum_reputation_level'], 'reputationlevel[minimumreputation]', $reputationlevel['minimumreputation']);
	print_submit_row(iif($vbulletin->GPC['reputationlevelid'], $vbphrase['update'], $vbphrase['save']));
}

// *************************************************************************************************

if ($_POST['do'] == 'update')
{
	$vbulletin->input->clean_array_gpc('p', array(
		'reputationlevelid' => vB_Cleaner::TYPE_INT,
		'oldminimum'        => vB_Cleaner::TYPE_INT,
		'reputationlevel'   => vB_Cleaner::TYPE_ARRAY,
		'level'             => vB_Cleaner::TYPE_STR,
	));

	$vbulletin->GPC['reputationlevel']['minimumreputation'] = intval($vbulletin->GPC['reputationlevel']['minimumreputation']);
	
	$queryParams = array(vB_dB_Query::CONDITIONS_KEY => array(
		array('field' => 'minimumreputation', 'value' => $vbulletin->GPC['reputationlevel']['minimumreputation'], vB_dB_Query::OPERATOR_KEY => vB_dB_Query::OPERATOR_EQ)
	));
	if ($vbulletin->GPC['reputationlevelid'])
	{ 
		// edit
		$queryParams[vB_dB_Query::CONDITIONS_KEY][] = array('field' => 'reputationlevelid', 'value' => $vbulletin->GPC['reputationlevelid'], vB_dB_Query::OPERATOR_KEY => vB_dB_Query::OPERATOR_NE);
	}

	$existing = $assertor->getRow('vBForum:reputationlevel', $queryParams);
	if (!$existing)
	{
		if ($vbulletin->GPC['reputationlevelid'])
		{ 
			// edit
			$updated = $assertor->update('vBForum:reputationlevel', array('minimumreputation' => $vbulletin->GPC['reputationlevel']['minimumreputation']), array('reputationlevelid' => $vbulletin->GPC['reputationlevelid']));

			if ($vbulletin->GPC['oldminimum'] != $vbulletin->GPC['reputationlevel']['minimumreputation'])
			{
				// need to update user table
				build_reputationids();
			}
		}
		else
		{
			$rLevelId = $assertor->insert('vBForum:reputationlevel', array('minimumreputation' => $vbulletin->GPC['reputationlevel']['minimumreputation']));
			if ($rLevelId AND !isset($rLevelId['errors']))
			{
				$vbulletin->GPC['reputationlevelid'] = array_pop($rLevelId);
				build_reputationids();
			}
			else
			{
				print_stop_message2('could_not_create_reputationlevel', 'adminreputation', array('do'=>'modify'));			
			}
		}

		$options = vB::getDatastore()->getValue('options');
		try 
		{
			$userInfo = vB_Api::instanceInternal('user')->fetchUserInfo();
		}
		catch (vB_Exception_Api $ex)
		{
			print_stop_message2($ex->getMessage(), 'adminreputation', array('do'=>'modify'));
		}
		
		$assertor->assertQuery('vBForum:reputationLevelPhraseReplace', array(
			'languageid' => 0,
			'fieldname' => 'reputationlevel',
			'varname' => ('reputation' . $vbulletin->GPC['reputationlevelid']),
			'text' => $vbulletin->GPC['level'],
			'product' => 'vbulletin',
			'username' => $userInfo['username'],
			'dateline' => vB::getRequest()->getTimeNow(),
			'version' => $options['templateversion']
		));

		require_once(DIR . '/includes/adminfunctions_language.php');
		build_language();

		print_stop_message2(array('saved_reputation_level_x_successfully',  htmlspecialchars_uni($vbulletin->GPC['level'])), 'adminreputation', array('do'=>'modify'));
	}
	else
	{
		print_stop_message2('no_permission_duplicate_reputation', 'adminreputation', array('do'=>'modify'));
	}
}

// *************************************************************************************************

if ($_REQUEST['do'] == 'remove')
{
	$vbulletin->input->clean_array_gpc('r', array(
		'minimumreputation'	=> vB_Cleaner::TYPE_INT
	));

	print_form_header('adminreputation', 'kill');
	construct_hidden_code('minimumreputation', $vbulletin->GPC['minimumreputation']);
	print_table_header($vbphrase['confirm_deletion_gcpglobal']);
	print_description_row(construct_phrase($vbphrase['are_you_sure_you_want_to_delete_the_reputation_level_x'], '<i>' . $vbulletin->GPC['minimumreputation'] . '</i>'));
	print_submit_row($vbphrase['yes'], '', 2, $vbphrase['no']);
}

// *************************************************************************************************

if ($_POST['do'] == 'kill')
{
	$vbulletin->input->clean_array_gpc('p', array(
		'minimumreputation'	=> vB_Cleaner::TYPE_INT
	));

	$reputationlevel = $assertor->getRow('vBForum:reputationlevel', array('minimumreputation' => $vbulletin->GPC['minimumreputation']));
	$assertor->delete('vBForum:phrase', array(
		'fieldname' => 'reputationlevel', 
		'varname' => ('reputation' . $reputationlevel['reputationlevelid'])
	));

	require_once(DIR . '/includes/adminfunctions_language.php');
	build_language();

	$assertor->delete('vBForum:reputationlevel', array(
		'minimumreputation' => $vbulletin->GPC['minimumreputation'] 
	));

	build_reputationids();

	print_stop_message2('deleted_reputation_level_successfully', 'adminreputation', array('do'=>'modify'));
}

// *************************************************************************************************

if ($_POST['do'] == 'updateminimums')
{
	$vbulletin->input->clean_array_gpc('p', array(
		'reputation' 	=> vB_Cleaner::TYPE_ARRAY
	));

	if (is_array($vbulletin->GPC['reputation']))
	{
		$found = array();
		foreach($vbulletin->GPC['reputation'] AS $index => $value)
		{
			if (isset($found["$value"]))
			{
				print_stop_message2('no_permission_duplicate_reputation');
			}
			else
			{
				$found["$value"] = 1;
			}
		}

		foreach ($vbulletin->GPC['reputation'] AS $index => $value)
		{
			$assertor->update('vBForum:reputationlevel', 
				array('minimumreputation' => intval($value)), 
				array('reputationlevelid' => intval($index))
			);
		}

		build_reputationids();
	}

	print_stop_message2(array('saved_reputation_level_x_successfully', ''), 'adminreputation', array('do'=>'modify'));
}

// *************************************************************************************************

if ($_REQUEST['do'] == 'list' OR $_REQUEST['do'] == 'dolist')
{
	$vbulletin->input->clean_array_gpc('r', array(
		'leftby'     => vB_Cleaner::TYPE_NOHTML,
		'leftfor'    => vB_Cleaner::TYPE_NOHTML,
		'userid'     => vB_Cleaner::TYPE_UINT,
		'whoadded'   => vB_Cleaner::TYPE_UINT,
		'pagenumber' => vB_Cleaner::TYPE_UINT,
		'perpage'    => vB_Cleaner::TYPE_UINT,
		'orderby'    => vB_Cleaner::TYPE_STR,
		'start'      => vB_Cleaner::TYPE_ARRAY_UINT,
		'end'        => vB_Cleaner::TYPE_ARRAY_UINT,
		'startstamp' => vB_Cleaner::TYPE_UINT,
		'endstamp'   => vB_Cleaner::TYPE_UINT
	));

	$vbulletin->GPC['start'] 	= iif($vbulletin->GPC['startstamp'], $vbulletin->GPC['startstamp'], $vbulletin->GPC['start']);
	$vbulletin->GPC['end'] 		= iif($vbulletin->GPC['endstamp'], $vbulletin->GPC['endstamp'], $vbulletin->GPC['end']);

	if ($whoaddedinfo = verify_id('user', $vbulletin->GPC['whoadded'], 0, 1))
	{
		$vbulletin->GPC['leftby'] = $whoaddedinfo['username'];
	}
	else
	{
		$vbulletin->GPC['whoadded'] = 0;
	}

	if ($userinfo = verify_id('user', $vbulletin->GPC['userid'], 0, 1))
	{
		$vbulletin->GPC['leftfor'] = $userinfo['username'];
	}
	else
	{
		$vbulletin->GPC['userid'] = 0;
	}

	// Default View Values

	if (!$vbulletin->GPC['start'])
	{
		$vbulletin->GPC['start'] = TIMENOW - 3600 * 24 * 30;
	}

	if (!$vbulletin->GPC['end'])
	{
		$vbulletin->GPC['end'] = TIMENOW;
	}


	print_form_header('adminreputation', 'dolist');
	print_table_header($vbphrase['view_reputation_comments']);
	print_input_row($vbphrase['leftfor'], 'leftfor', $vbulletin->GPC['leftfor'], 0);
	print_input_row($vbphrase['leftby'], 'leftby', $vbulletin->GPC['leftby'], 0);
	print_time_row($vbphrase['start_date'], 'start', $vbulletin->GPC['start'], false);
	print_time_row($vbphrase['end_date'], 'end', $vbulletin->GPC['end'], false);
	print_submit_row($vbphrase['go']);
}

// *************************************************************************************************

if ($_REQUEST['do'] == 'dolist')
{
	require_once(DIR . '/includes/functions_misc.php');
	if ($vbulletin->GPC['startstamp'])
	{
		$vbulletin->GPC['start'] = $vbulletin->GPC['startstamp'];
	}
	else
	{
		$vbulletin->GPC['start'] = vbmktime(0, 0, 0, $vbulletin->GPC['start']['month'], $vbulletin->GPC['start']['day'], $vbulletin->GPC['start']['year']);
	}

	if ($vbulletin->GPC['endstamp'])
	{
		$vbulletin->GPC['end'] = $vbulletin->GPC['endstamp'];
	}
	else
	{
		$vbulletin->GPC['end'] = vbmktime(23, 59, 59, $vbulletin->GPC['end']['month'], $vbulletin->GPC['end']['day'], $vbulletin->GPC['end']['year']);
	}

	if ($vbulletin->GPC['start'] >= $vbulletin->GPC['end'])
	{
		print_stop_message2('start_date_after_end_gerror');
	}

	if ($vbulletin->GPC['leftby'])
	{
		if (!$assertor->getRow('user', array('username' => $vbulletin->GPC['leftby'])))
		{
			print_stop_message2(array('could_not_find_user_x',  $vbulletin->GPC['leftby']));
		}
		$vbulletin->GPC['whoadded'] = $leftby_user['userid'];
	}

	if ($vbulletin->GPC['leftfor'])
	{
		if (!$assertor->getRow('user', array('username' => $vbulletin->GPC['leftfor'])))
		{
			print_stop_message2(array('could_not_find_user_x',  $vbulletin->GPC['leftfor']));
		}
		$vbulletin->GPC['userid'] = $leftfor_user['userid'];
	}

	$queryParams = array(vB_dB_Query::TYPE_KEY => vB_dB_Query::QUERY_COUNT);
	$params = array();
	if ($vbulletin->GPC['whoadded'])
	{
		$queryParams[vB_dB_Query::CONDITIONS_KEY][] = array('field' => 'whoadded', 'value' => $vbulletin->GPC['whoadded'], vB_dB_Query::OPERATOR_KEY => vB_dB_Query::OPERATOR_EQ);
		$params['whoadded'] = $vbulletin->GPC['whoadded'];
	}
	if ($vbulletin->GPC['userid'])
	{
		$queryParams[vB_dB_Query::CONDITIONS_KEY][] = array('field' => 'userid', 'value' => $vbulletin->GPC['userid'], vB_dB_Query::OPERATOR_KEY => vB_dB_Query::OPERATOR_EQ);
		$params['userid'] = $vbulletin->GPC['userid'];
	}
	if ($vbulletin->GPC['start'])
	{
		$queryParams[vB_dB_Query::CONDITIONS_KEY][] = array('field' => 'dateline', 'value' => $vbulletin->GPC['start'], vB_dB_Query::OPERATOR_KEY => vB_dB_Query::OPERATOR_GTE);
		$params['start'] = $vbulletin->GPC['start'];
	}
	if ($vbulletin->GPC['end'])
	{
		$queryParams[vB_dB_Query::CONDITIONS_KEY][] = array('field' => 'dateline', 'value' => $vbulletin->GPC['end'], vB_dB_Query::OPERATOR_KEY => vB_dB_Query::OPERATOR_LTE);
		$params['end'] = $vbulletin->GPC['end'];
	}

	$count = $assertor->getRow('vBForum:reputation', $queryParams);

	$totalrep = $count['count'];

	if (!$totalrep)
	{
		print_stop_message2('no_matches_found_gerror');
	}

	switch($vbulletin->GPC['orderby'])
	{
		case 'leftbyuser':
			$params['orderby'] = 'leftby';
			break;
		case 'leftforuser':
			$params['orderby'] = 'leftfor';
			break;
		default:
			$params['orderby'] = 'dateline';
	}

	sanitize_pageresults($totalrep, $vbulletin->GPC['pagenumber'], $vbulletin->GPC['perpage']);
	$params[vB_Db_Query::PARAM_LIMIT] = $vbulletin->GPC['perpage'];
	$params[vB_Db_Query::PARAM_LIMITSTART] = $startat = ($vbulletin->GPC['pagenumber'] - 1) * $vbulletin->GPC['perpage'];
	$totalpages = ceil($totalrep / $vbulletin->GPC['perpage']);

	$comments = $assertor->getRows('vBForum:listReputationComments', $params);

	if ($vbulletin->GPC['pagenumber'] != 1)
	{
		$prv = $vbulletin->GPC['pagenumber'] - 1;
		$firstpage = "<input type=\"button\" class=\"button\" value=\"&laquo; " . $vbphrase['first_page'] . "\" tabindex=\"1\" onclick=\"window.location='adminreputation.php?" . vB::getCurrentSession()->get('sessionurl') .	"do=dolist" .
			"&u=" 			. $vbulletin->GPC['userid'] .
			"&whoadded="	. $vbulletin->GPC['whoadded'] .
			"&pp="			. $vbulletin->GPC['perpage'] .
			"&page=1" .
			"&startstamp=" 	. $vbulletin->GPC['start'] .
			"&endstamp=" 	. $vbulletin->GPC['end'] .
			"&orderby=" . $vbulletin->GPC['orderby'] . "'\">";

		$prevpage = "<input type=\"button\" class=\"button\" value=\"&lt; " . $vbphrase['prev_page'] . "\" tabindex=\"1\" onclick=\"window.location='adminreputation.php?" . vB::getCurrentSession()->get('sessionurl') . "do=dolist" .
			"&u="			. $vbulletin->GPC['userid'] .
			"&whoadded="	. $vbulletin->GPC['whoadded'] .
			"&pp="		 	. $vbulletin->GPC['perpage'] .
			"&page="		. $prv .
			"&startstamp="	. $vbulletin->GPC['start'] .
			"&endstamp="	. $vbulletin->GPC['end'] .
			"&orderby=" . $vbulletin->GPC['orderby'] . "'\">";
	}

	if ($vbulletin->GPC['pagenumber'] != $totalpages)
	{
		$nxt = $vbulletin->GPC['pagenumber'] + 1;
		$nextpage = "<input type=\"button\" class=\"button\" value=\"" . $vbphrase['next_page'] . " &gt;\" tabindex=\"1\" onclick=\"window.location='adminreputation.php?" . vB::getCurrentSession()->get('sessionurl') . "do=dolist" .
			"&u="			. $vbulletin->GPC['userid'] .
			"&whoadded="	. $vbulletin->GPC['whoadded'] .
			"&pp="			. $vbulletin->GPC['perpage'] .
			"&page="		. $nxt .
			"&startstamp="	. $vbulletin->GPC['start'] .
			"&endstamp="	. $vbulletin->GPC['end'] .
			"&orderby=" . $vbulletin->GPC['orderby'] . "'\">";

		$lastpage = "<input type=\"button\" class=\"button\" value=\"" . $vbphrase['last_page'] . " &raquo;\" tabindex=\"1\" onclick=\"window.location='adminreputation.php?" . vB::getCurrentSession()->get('sessionurl') . "do=dolist" .
			"&u="			. $vbulletin->GPC['userid'] .
			"&whoadded="	. $vbulletin->GPC['whoadded'] .
			"&pp="			. $vbulletin->GPC['perpage'] .
			"&page="		. $totalpages .
			"&startstamp="	. $vbulletin->GPC['start'] .
			"&endstamp=" 	. $vbulletin->GPC['end'] .
			"&orderby=" . $vbulletin->GPC['orderby'] . "'\">";
	}

	print_form_header('adminreputation', 'dolist');
	print_table_header(construct_phrase($vbphrase['x_reputation_comments_page_y_z'], vb_number_format($totalrep), $vbulletin->GPC['pagenumber'], vb_number_format($totalpages)), 7);

	$headings = array();
	$headings[] = "<a href='adminreputation.php?" . vB::getCurrentSession()->get('sessionurl') . "do=dolist" .
		"&amp;u=" 			. $vbulletin->GPC['userid'] .
		"&amp;whoadded="	. $vbulletin->GPC['whoadded'] .
		"&amp;pp="			. $vbulletin->GPC['perpage'] .
		"&amp;orderby=leftbyuser" .
		"&amp;page=" 		. $vbulletin->GPC['pagenumber'] .
		"&amp;startstamp="	. $vbulletin->GPC['start'] .
		"&amp;endstamp="	. $vbulletin->GPC['end'] . "' title='" . $vbphrase['order_by_username'] . "'>" . $vbphrase['leftby'] . "</a>";

	$headings[] = "<a href='adminreputation.php?" . vB::getCurrentSession()->get('sessionurl') . "do=dolist" .
		"&amp;u="			. $vbulletin->GPC['userid'] .
		"&amp;whoadded="	. $vbulletin->GPC['whoadded'] .
		"&amp;pp="			. $vbulletin->GPC['perpage'] .
		"&amp;orderby=leftforuser" .
		"&amp;page=" 		. $vbulletin->GPC['pagenumber'] .
		"&amp;startstamp="	. $vbulletin->GPC['start'] .
		"&amp;endstamp="	. $vbulletin->GPC['end'] . "' title='" . $vbphrase['order_by_username'] . "'>" . $vbphrase['leftfor'] . "</a>";

	$headings[] = "<a href='adminreputation.php?" . vB::getCurrentSession()->get('sessionurl') . "do=dolist" .
		"&amp;u="			. $vbulletin->GPC['userid'] .
		"&amp;whoadded="	. $vbulletin->GPC['whoadded'] .
		"&amp;pp="			. $vbulletin->GPC['perpage'] .
		"&amp;orderby=date" .
		"&amp;page="		. $vbulletin->GPC['pagenumber'] .
		"&amp;startstamp="	. $vbulletin->GPC['start'] .
		"&amp;endstamp="	. $vbulletin->GPC['end'] . "' title='" . $vbphrase['order_by_date'] . "'>" . $vbphrase['date'] . "</a>";

	$headings[] = $vbphrase['reputation'];
	$headings[] = $vbphrase['reason'];
	$headings[] = $vbphrase['post'];
	$headings[] = $vbphrase['controls'];
	print_cells_row($headings, 1);

	foreach ($comments AS $comment)
	{

		$postlink = '';
		if (!empty($comment['nodeid']))
		{
			//deliberately don't use the title.  We don't have it in our result set (or
			//in any of the tables in our result set) and we'll catch it on redirect.  
			//Plus the admincp isn't a big SEO issue -- we just want to get the links
			//on the classes so that they work and centralize logic for future changes.
			$postlink = fetch_seo_url('thread|bburl', $comment, array('p' => $comment['nodeid'])) . "#post$comment[postid]";
		}

		$cell = array();
		$cell[] = "<a href=\"user.php?" . vB::getCurrentSession()->get('sessionurl') . "do=edit&amp;u=$comment[whoadded]\"><b>$comment[leftby_username]</b></a>";
		$cell[] = "<a href=\"user.php?" . vB::getCurrentSession()->get('sessionurl') . "do=edit&amp;u=$comment[userid]\"><b>$comment[leftfor_username]</b></a>";
		$cell[] = '<span class="smallfont">' . vbdate($vbulletin->options['logdateformat'], $comment['dateline']) . '</span>';
		$cell[] = $comment['reputation'];
		$cell[] = !empty($comment['reason']) ? '<span class="smallfont">' . htmlspecialchars_uni($comment['reason']) . '</span>' : '';
		$cell[] = $postlink ? construct_link_code(htmlspecialchars_uni($vbphrase['post']), $postlink, true, '', true) : '&nbsp;';
		$cell[] = construct_link_code($vbphrase['edit'], "adminreputation.php?" . vB::getCurrentSession()->get('sessionurl') . "do=editreputation&reputationid=$comment[reputationid]", false, '', true) .
			' ' . construct_link_code($vbphrase['delete'], "adminreputation.php?" . vB::getCurrentSession()->get('sessionurl') . "do=deletereputation&reputationid=$comment[reputationid]", false, '', true);
		print_cells_row($cell);
	}

	print_table_footer(7, "$firstpage $prevpage &nbsp; $nextpage $lastpage");
}

// *************************************************************************************************

if ($_REQUEST['do'] == 'editreputation')
{
	$vbulletin->input->clean_array_gpc('r', array(
		'reputationid' => vB_Cleaner::TYPE_INT
	));
	
	if ($repinfo = $assertor->getRow('vBForum:editReputationInfo', array('reputationid' => $vbulletin->GPC['reputationid'])))
	{
		print_form_header('adminreputation', 'doeditreputation');
		print_table_header($vbphrase['edit_reputation_greputation']);
		print_label_row($vbphrase['thread'], 
			$repinfo['title'] ? "<a href=\"" . fetch_seo_url('thread|bburl', $repinfo, 
				array('p' => $repinfo['postid'])) . "#post$repinfo[postid]" . 
				"\">$repinfo[title]</a>" : '');
		print_label_row($vbphrase['leftby'], $repinfo['whoadded_username']);
		print_label_row($vbphrase['leftfor'], $repinfo['username']);
		print_input_row($vbphrase['comment_guser'], 'reputation[reason]', $repinfo['reason']);
		print_input_row($vbphrase['reputation'], 'reputation[reputation]', $repinfo['reputation'], 0, 5);
		construct_hidden_code('reputationid', $vbulletin->GPC['reputationid']);
		construct_hidden_code('oldreputation', $repinfo[reputation]);
		construct_hidden_code('userid', $repinfo['userid']);
		print_submit_row();
	}
	else
	{
		print_stop_message2('no_matches_found_gerror');
	}
}

// *************************************************************************************************

if ($_POST['do'] == 'doeditreputation')
{
	$vbulletin->input->clean_array_gpc('p', array(
		'reputation'	=> vB_Cleaner::TYPE_ARRAY,
		'reputationid'	=> vB_Cleaner::TYPE_INT,
		'oldreputation'	=> vB_Cleaner::TYPE_INT,
		'userid'		=> vB_Cleaner::TYPE_INT
	));

	$insertValues = array();
	$structure = $assertor->fetchTableStructure('vBForum:reputation');
	foreach ($vbulletin->GPC['reputation'] AS $field => $value)
	{
		if (in_array($field, $structure['structure']))
		{
			$insertValues[$field] = $value;
		}
	}
	
	if (!empty($insertValues))
	{
		$assertor->update('vBForum:reputation', $insertValues, array('reputationid' => $vbulletin->GPC['reputationid']));
		if ($vbulletin->GPC['oldreputation'] != $vbulletin->GPC['reputation']['reputation'])
		{
			$diff = $vbulletin->GPC['oldreputation'] - $vbulletin->GPC['reputation']['reputation'];
	
			$user = fetch_userinfo($vbulletin->GPC['userid']);
			if ($user)
			{
				$userdm = new vB_Datamanager_User($vbulletin, vB_DataManager_Constants::ERRTYPE_SILENT);
				$userdm->set_existing($user);
				$userdm->set('reputation', "reputation - $diff", false);
				$userdm->save();
				unset($userdm);
			}
		}
	}

	print_stop_message2('saved_reputation_successfully', 'adminreputation', array('do'=>'list', 'u'=> $vbulletin->GPC['userid']));
}

// *************************************************************************************************

if ($_POST['do'] == 'killreputation')
{
	$vbulletin->input->clean_array_gpc('p', array(
		'reputationid'	=> vB_Cleaner::TYPE_INT
	));

	$repinfo = verify_id('reputation', $vbulletin->GPC['reputationid'], 0, 1);

	$user = fetch_userinfo($repinfo['userid']);
	if ($user)
	{
		$userdm = new vB_Datamanager_user($vbulletin, vB_DataManager_Constants::ERRTYPE_CP);
		$userdm->set_existing($user);
		$userdm->set('reputation', $user['reputation'] - $repinfo['reputation']);
		$userdm->save();
		unset($userdm);
	}

	$assertor->delete('vBForum:reputation', array('reputationid' => $vbulletin->GPC['reputationid']));
	
	print_stop_message2('deleted_reputation_successfully', 'adminreputation', array('do'=>'list','u'=>$repinfo['userid']));
}

// *************************************************************************************************

if ($_REQUEST['do'] == 'deletereputation')
{
	$vbulletin->input->clean_array_gpc('r', array(
		'reputationid'	=> vB_Cleaner::TYPE_INT
	));

	print_delete_confirmation('reputation', $vbulletin->GPC['reputationid'], 'adminreputation', 'killreputation');
}

if ($_REQUEST['do'] == 'modify')
{
	$reputationlevels = $assertor->getRows('vBForum:reputationlevel', array(), array('minimumreputation'));

	print_form_header('adminreputation', 'updateminimums');
	print_table_header($vbphrase['user_reputation_manager_greputation'], 3);
	print_cells_row(array($vbphrase['reputation_level_guser'], $vbphrase['minimum_reputation_level'], $vbphrase['controls']), 1);

	foreach ($reputationlevels AS $reputationlevel)
	{
		$reputationlevel['level'] = htmlspecialchars_uni($vbphrase['reputation' . $reputationlevel['reputationlevelid']]);
		$cell = array();
		$cell[] = "$vbphrase[user] <b>$reputationlevel[level]</b>";
		$cell[] = "<input type=\"text\" class=\"bginput\" tabindex=\"1\" name=\"reputation[$reputationlevel[reputationlevelid]]\" value=\"$reputationlevel[minimumreputation]\" size=\"5\" />";
		$cell[] = construct_link_code($vbphrase['edit'], "adminreputation.php?" . vB::getCurrentSession()->get('sessionurl') . "do=edit&reputationlevelid=$reputationlevel[reputationlevelid]") . construct_link_code($vbphrase['delete'], "adminreputation.php?" . vB::getCurrentSession()->get('sessionurl') . "do=remove&minimumreputation=$reputationlevel[minimumreputation]");
		print_cells_row($cell);
	}

	print_submit_row($vbphrase['update'], $vbphrase['reset'], 3);
}

print_cp_footer();

/*======================================================================*\
|| ####################################################################
|| # 
|| # CVS: $RCSfile$ - $Revision: 62889 $
|| ####################################################################
\*======================================================================*/
?>
