<?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);
@set_time_limit(0);
ignore_user_abort(1);

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

// #################### PRE-CACHE TEMPLATES AND DATA ######################
global $phrasegroups, $specialtemplates, $vbphrase, $vbulletin;
$phrasegroups = array('maintenance');
if ($_POST['do'] == 'rebuildstyles')
{
	$phrasegroups[] = 'style';
}
$specialtemplates = array('ranks');


// ########################## REQUIRE BACK-END ############################
require_once(dirname(__FILE__) . '/global.php');
require_once(DIR . '/includes/functions_databuild.php');

require_once (DIR ."/vb/vb.php");
vB::init();
// ######################## CHECK ADMIN PERMISSIONS #######################
if (!can_administer('canadminmaintain'))
{
	print_cp_no_permission();
}

// ############################# LOG ACTION ###############################
log_admin_action();

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

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

$vbulletin->input->clean_array_gpc('r', array(
	'perpage' => vB_Cleaner::TYPE_UINT,
	'startat' => vB_Cleaner::TYPE_UINT
));

// ###################### Start clear cache ########################
if ($_REQUEST['do'] == 'clear_cache')
{
	print_cp_header($vbphrase['clear_system_cache']);
	vB_Cache::resetCache();
	print_cp_message($vbphrase['cache_cleared']);
}
else
{
	print_cp_header($vbphrase['maintenance']);
}

// ###################### Rebuild all style info #######################
if ($_POST['do'] == 'rebuildstyles')
{
	require_once(DIR . '/includes/adminfunctions_template.php');

	$vbulletin->input->clean_array_gpc('p', array(
		'renumber' => vB_Cleaner::TYPE_BOOL,
		'install'  => vB_Cleaner::TYPE_BOOL
	));

	build_all_styles($vbulletin->GPC['renumber'], $vbulletin->GPC['install'], 'misc.php?' . vB::getCurrentSession()->get('sessionurl') . 'do=chooser#style');

	print_stop_message2('updated_styles_successfully');
}

// ###################### Start emptying the index #######################
if ($_REQUEST['do'] == 'emptyindex')
{
	print_form_header('misc', 'doemptyindex');
	print_table_header($vbphrase['confirm_deletion_gcpglobal']);
	print_description_row($vbphrase['are_you_sure_empty_index']);
	print_submit_row($vbphrase['yes'], '', 2, $vbphrase['no']);
}

// ###################### Start emptying the index #######################
if ($_POST['do'] == 'doemptyindex')
{
	vB_Api::instanceInternal('search')->emptyIndex();
	$args = array();
	parse_str(vB::getCurrentSession()->get('sessionurl'),$args);
	$args['do'] = 'chooser';
	print_stop_message2('emptied_search_index_successfully', 'misc', $args);

}

// ###################### Start build search index #######################
if ($_REQUEST['do'] == 'doindextypes')
{
	require_once(DIR . '/includes/functions_misc.php');
	//require_once(DIR . '/vb/search/core.php');

	$vbulletin->input->clean_array_gpc('r', array(
		//'doprocess'    => vB_Cleaner::TYPE_UINT,
		'autoredirect' => vB_Cleaner::TYPE_BOOL,
		'totalitems'   => vB_Cleaner::TYPE_UINT,
		'indextypes'   => vB_Cleaner::TYPE_NOHTML
	));
	//vB_Api::instanceInternal('search')->indexAll();
	$starttime = microtime();
	$end = false;
	//	Init Search & get the enabled types to be re-indexed
	//$types = vB_Search_Core::get_instance();
	//$indexed_types = $types->get_indexed_types();
	$filters = false;
	if (!empty($vbulletin->GPC['indextypes']))
	{
		$filters = array('type' => $vbulletin->GPC['indextypes']);
	}

	$startat = 0;
	$perpage = false;
	if (!empty($vbulletin->GPC['startat']) OR !empty($vbulletin->GPC['perpage']))
	{
		$startat = empty($vbulletin->GPC['startat']) ? 0 : $vbulletin->GPC['startat'];
		if (!empty($vbulletin->GPC['perpage']))
		{
			$options = vB::getDatastore()->get_value('options');
			$perpage = $vbulletin->GPC['perpage'];
			if (!empty($options['maxresults']))
			{
				$perpage = min($vbulletin->GPC['perpage'], $options['maxresults']);
			}
		}
	}
	echo '<p>' . $vbphrase['building_search_index'] . ' ' .
				 (empty($vbulletin->GPC['indextypes']) ? '' : (vB_Types::instance()->getContentTypeClass($vbulletin->GPC['indextypes']) . ' ' )) .
				 $startat  . ' :: ' .
				 ($startat + $perpage) . '</p>';
	vbflush();
	$hasmore = vB_Api::instanceInternal('search')->indexRange($startat, $perpage, $filters);
	$pagetime = vb_number_format(fetch_microtime_difference($starttime), 2);

	echo '</p><p><b>' . construct_phrase($vbphrase['processing_time_x'], $pagetime) . '<br />' . construct_phrase($vbphrase['total_items_processed_x'], $startat + $perpage) . '</b></p>';
	vbflush();

	// There is more to do of that type
	if ($hasmore)
	{
		if ($vbulletin->GPC['autoredirect'] == 1)
		{
			$args = array();
			parse_str(vB::getCurrentSession()->get('sessionurl'),$args);
			$args['do'] = 'doindextypes';
			$args['startat'] = $startat + $perpage;
			$args['pp'] = $perpage;
			$args['autoredirect'] = $vbulletin->GPC['autoredirect'];
			//$args['doprocess'] = $vbulletin->GPC['doprocess'];
			$args['totalitems'] = $vbulletin->GPC['totalitems'];
			$args['indextypes'] = $vbulletin->GPC['indextypes'];
			print_cp_redirect2('misc', $args);
		}

		echo "<p><a href=\"misc.php?" . vB::getCurrentSession()->get('sessionurl') .
			"do=doindextypes&amp;startat=" . ($startat + $perpage) . "&amp;pp=$perpage" .
			"&amp;autoredirect=" . $vbulletin->GPC['autoredirect'] .
			//"&amp;doprocess=" . $vbulletin->GPC['doprocess'] .
			"&amp;totalitems=" . $vbulletin->GPC['totalitems'] .
			"&amp;indextypes=" . $vbulletin->GPC['indextypes'] . "\">" .
			$vbphrase['click_here_to_continue_processing'] . "</a></p>";
	}
	else
	{
		$args = array();
		parse_str(vB::getCurrentSession()->get('sessionurl'),$args);
		$args['do'] = 'chooser';

		print_stop_message2('rebuilt_search_index_successfully', 'misc', $args);
	}
}

// ###################### Start update post counts ################
if ($_REQUEST['do'] == 'updateposts')
{
	if (empty($vbulletin->GPC['perpage']))
	{
		$vbulletin->GPC['perpage'] = 1000;
	}

	$finishat = $vbulletin->GPC['startat'] + $vbulletin->GPC['perpage'];
	
	$topChannels = vB_Api::instanceInternal('content_channel')->fetchTopLevelChannelIds();
	$forumChannel = $topChannels['forum'];
	$channelContentType = vB_Types::instance()->getContentTypeID('vBForum_Channel');

	echo '<p>' . $vbphrase['updating_post_counts'] . '</p>';
	
	$forums = $vbulletin->db->query_read("
		SELECT node.nodeid
		FROM vb_node AS node
		INNER JOIN vb_closure AS cl ON cl.parent = $forumChannel AND cl.child = node.nodeid
		WHERE node.contenttypeid = $channelContentType
		AND node.nodeid != $forumChannel
	");
	$gotforums = '';
	while ($forum = $vbulletin->db->fetch_array($forums))
	{
		$gotforums .= ',' . $forum['nodeid'];
	}

	$users = $vbulletin->db->query_read("
		SELECT *
		FROM " . TABLE_PREFIX . "user
		WHERE userid >= " . $vbulletin->GPC['startat'] . "
		ORDER BY userid
		LIMIT " . $vbulletin->GPC['perpage']
	);

	$finishat = $vbulletin->GPC['startat'];

	while ($user = $vbulletin->db->fetch_array($users))
	{
		$totalposts = $vbulletin->db->query_first("
			SELECT COUNT(*) AS posts
			FROM vb_node AS post
			INNER JOIN vb_node AS thread ON (thread.nodeid = post.parentid)
			WHERE post.userid = " . $user['userid'] . "
			AND thread.parentid IN (0$gotforums)
			AND thread.publishdate IS NOT NULL AND thread.approved = 1
			AND post.publishdate IS NOT NULL AND post.approved = 1
		");

		$userdm = new vB_Datamanager_User($vbulletin, vB_DataManager_Constants::ERRTYPE_CP);
		$userdm->set_existing($user);
		$userdm->set('posts', $totalposts['posts']);
		$userdm->set_ladder_usertitle($totalposts['posts']);
		$userdm->save();
		unset($userdm);

		echo construct_phrase($vbphrase['processing_x'], $user['userid']) . "<br />\n";
		vbflush();

		$finishat = ($user['userid'] > $finishat ? $user['userid'] : $finishat);
	}

	$finishat++;

	if ($checkmore = $vbulletin->db->query_first("SELECT userid FROM " . TABLE_PREFIX . "user WHERE userid >= $finishat LIMIT 1"))
	{
		$args = array();
		parse_str(vB::getCurrentSession()->get('sessionurl'),$args);
		$args['do'] = 'updateposts';
		$args['startat'] = $finishat;
		$args['pp'] = $vbulletin->GPC['perpage'];
		print_cp_redirect2('misc', $args);
		echo "<p><a href=\"misc.php?" . vB::getCurrentSession()->get('sessionurl') . "do=updateposts&amp;startat=$finishat&amp;pp=" . $vbulletin->GPC['perpage'] . "\">" . $vbphrase['click_here_to_continue_processing'] . "</a></p>";
	}
	else
	{
		print_stop_message2('updated_post_counts_successfully', 'misc');
	}
}

// ###################### Start update user #######################
if ($_REQUEST['do'] == 'updateuser')
{
	require_once(DIR . '/includes/functions_infractions.php');

	if (empty($vbulletin->GPC['perpage']))
	{
		$vbulletin->GPC['perpage'] = 1000;
	}

	echo '<p>' . $vbphrase['updating_user_info'] . '</p>';
	$tmp_usergroup_cache = array();

	$infractiongroups = array();
	$groups = $vbulletin->db->query_read("
		SELECT usergroupid, orusergroupid, pointlevel, override
		FROM " . TABLE_PREFIX . "infractiongroup
		ORDER BY pointlevel
	");
	while ($group = $vbulletin->db->fetch_array($groups))
	{
		$infractiongroups["$group[usergroupid]"]["$group[pointlevel]"][] = array(
			'orusergroupid' => $group['orusergroupid'],
			'override'      => $group['override'],
		);
	}

	$users = $vbulletin->db->query_read("
		SELECT user.*, usertextfield.rank,
		IF(user.displaygroupid=0, user.usergroupid, user.displaygroupid) AS displaygroupid
		FROM " . TABLE_PREFIX . "user AS user
		LEFT JOIN " . TABLE_PREFIX . "usertextfield AS usertextfield USING (userid)
		WHERE user.userid >= " . $vbulletin->GPC['startat'] . "
		ORDER BY user.userid
		LIMIT " . $vbulletin->GPC['perpage']
	);

	$finishat = $vbulletin->GPC['startat'];

	while ($user = $vbulletin->db->fetch_array($users))
	{
		$userdm = new vB_Datamanager_User($vbulletin, vB_DataManager_Constants::ERRTYPE_CP);
		$userdm->set_existing($user);
		cache_permissions($user, false);

		$userdm->set_usertitle(
			($user['customtitle'] ? $user['usertitle'] : ''),
			false,
			$vbulletin->usergroupcache["$user[displaygroupid]"],
			($user['customtitle'] == 1 OR $user['permissions']['genericpermissions'] & $vbulletin->bf_ugp_genericpermissions['canusecustomtitle']) ? true : false,
			($user['customtitle'] == 1) ? true : false
		);

		if ($lastpost = $vbulletin->db->query_first("SELECT MAX(publishdate) AS dateline FROM " . TABLE_PREFIX . "node WHERE userid = $user[userid]"))
		{
			$lastpost['dateline'] = intval($lastpost['dateline']);
		}
		else
		{
			$lastpost['dateline'] = 0;
		}

		$infractioninfo = fetch_infraction_groups($infractiongroups, $user['userid'], $user['ipoints'], $user['usergroupid']);
		$userdm->set('infractiongroupids', $infractioninfo['infractiongroupids']);
		$userdm->set('infractiongroupid', $infractioninfo['infractiongroupid']);

		$userdm->set('posts', $user['posts']); // This will activate the rank update
		$userdm->set('lastpost', $lastpost['dateline']);
		$userdm->save();
		unset($userdm);

		echo construct_phrase($vbphrase['processing_x'], $user['userid']) . "<br />\n";
		vbflush();

		$finishat = ($user['userid'] > $finishat ? $user['userid'] : $finishat);
	}

	$finishat++;

	if ($checkmore = $vbulletin->db->query_first("SELECT userid FROM " . TABLE_PREFIX . "user WHERE userid >= $finishat LIMIT 1"))
	{
		$args = array();
		parse_str(vB::getCurrentSession()->get('sessionurl'),$args);
		$args['do'] = 'updateuser';
		$args['startat'] = $finishat;
		$args['pp'] = $vbulletin->GPC['perpage'];
		print_cp_redirect2('misc', $args);
		echo "<p><a href=\"misc.php?" . vB::getCurrentSession()->get('sessionurl') . "do=updateuser&amp;startat=$finishat&amp;pp=" . $vbulletin->GPC['perpage'] . "\">" . $vbphrase['click_here_to_continue_processing'] . "</a></p>";
	}
	else
	{
		print_stop_message2('updated_user_titles_successfully', 'misc');
	}
}

// ###################### Start update usernames #######################
if ($_REQUEST['do'] == 'updateusernames')
{
	if (empty($vbulletin->GPC['perpage']))
	{
		$vbulletin->GPC['perpage'] = 1000;
	}

	echo '<p>' . $vbphrase['updating_usernames'] . '</p>';
	$users = $vbulletin->db->query_read("
		SELECT *
		FROM " . TABLE_PREFIX . "user
		WHERE userid >= " . $vbulletin->GPC['startat'] . "
		ORDER BY userid
		LIMIT " . $vbulletin->GPC['perpage']
	);

	$finishat = $vbulletin->GPC['startat'];
	while ($user = $vbulletin->db->fetch_array($users))
	{
		$userman = new vB_Datamanager_User($vbulletin, vB_DataManager_Constants::ERRTYPE_SILENT);
		$userman->set_existing($user);
		$userman->update_username($user['userid'], $user['username']);
		unset($userman);

		echo construct_phrase($vbphrase['processing_x'], $user['userid']) . "<br />\n";
		vbflush();

		$finishat = ($user['userid'] > $finishat ? $user['userid'] : $finishat);
	}

	$finishat++; // move past the last processed user

	if ($checkmore = $vbulletin->db->query_first("SELECT userid FROM " . TABLE_PREFIX . "user WHERE userid >= $finishat LIMIT 1"))
	{
		$args = array();
		parse_str(vB::getCurrentSession()->get('sessionurl'),$args);
		$args['do'] = 'updateusernames';
		$args['startat'] = $finishat;
		$args['pp'] = $vbulletin->GPC['perpage'];
		print_cp_redirect2('misc', $args);
		echo "<p><a href=\"misc.php?" . vB::getCurrentSession()->get('sessionurl') . "do=updateusernames&amp;startat=$finishat&amp;pp=" . $vbulletin->GPC['perpage'] . "\">" . $vbphrase['click_here_to_continue_processing'] . "</a></p>";
	}
	else
	{
		print_stop_message2('updated_usernames_successfully', 'misc');
	}
}


// ###################### Start update forum #######################
if ($_REQUEST['do'] == 'updateforum' OR $_REQUEST['do'] == 'updatecategorychannel')
{
	if (empty($vbulletin->GPC['perpage']))
	{
		$vbulletin->GPC['perpage'] = 100;
	}

	echo '<p>' . $vbphrase['updating_forums'] . '</p>';
	
	$channelTypeid = vB_Types::instance()->getContentTypeID('vBForum_Channel');
	$assertor = vB::getDbAssertor();

	$maxNodeid = $assertor->getRow('vBInstall:getMaxNodeid', array());
	$maxNodeid = intval($maxNodeid['maxid']);

	if ($maxNodeid < $vbulletin->GPC['startat'])
	{
		if ($_REQUEST['do'] == 'updateforum')
		{
			//redirect to updatecategorychannel
			$args = array();
			$startat = 0;
			parse_str(vB::getCurrentSession()->get('sessionurl'),$args);
			$args['do'] = 'updatecategorychannel';
			$args['startat'] = $startat;
			$args['pp'] = $vbulletin->GPC['perpage'];
			print_cp_redirect2('misc', $args);
			echo "<p><a href=\"misc.php?" . vB::getCurrentSession()->get('sessionurl') . "do=updatecategorychannel&amp;startat=$startat&amp;pp=" . $vbulletin->GPC['perpage'] . "\">" . $vbphrase['click_here_to_continue_processing'] . "</a></p>";
		}
		else
		{
			//redirect to updatechannelcounts
			$args = array();
			$startat = 0;
			parse_str(vB::getCurrentSession()->get('sessionurl'),$args);
			$args['do'] = 'updatechannelcounts';
			$args['startat'] = $startat;
			print_cp_redirect2('misc', $args);
			echo "<p><a href=\"misc.php?" . vB::getCurrentSession()->get('sessionurl') . "do=updatecategorychannel&amp;startat=$startat\">" . $vbphrase['click_here_to_continue_processing'] . "</a></p>";
		}
	}
	else
	{
		$args = array();
		parse_str(vB::getCurrentSession()->get('sessionurl'),$args);
		$args['pp'] = $vbulletin->GPC['perpage'];

		if ($_REQUEST['do'] == 'updatecategorychannel')
		{
			$assertor->assertQuery('vBInstall:updateCategoryLast',
				array('channeltypeid' =>  $channelTypeid)
			);
			$args['do'] = 'updatechannelcounts';
			$args['startat'] = $startat = 0;
			print_cp_redirect2('misc', $args);
			echo "<p><a href=\"misc.php?" . vB::getCurrentSession()->get('sessionurl') . "do=updatechannelcounts&amp;startat=$startat&amp;pp=" . $vbulletin->GPC['perpage'] . "\">" . $vbphrase['click_here_to_continue_processing'] . "</a></p>";
		}
		elseif ($_REQUEST['do'] == 'updateforum')
		{
			$assertor->assertQuery('vBInstall:updateChannelLast',
				array('channeltypeid' =>  $channelTypeid,
					'startat' => $vbulletin->GPC['startat'], 'batchsize' => $vbulletin->GPC['perpage']));

			$startat = $vbulletin->GPC['startat'] + $vbulletin->GPC['perpage'];
			$args['do'] = 'updateforum';
			$args['startat'] = $startat;
			print_cp_redirect2('misc', $args);
			echo "<p><a href=\"misc.php?" . vB::getCurrentSession()->get('sessionurl') . "do=updateforum&amp;startat=$startat&amp;pp=" . $vbulletin->GPC['perpage'] . "\">" . $vbphrase['click_here_to_continue_processing'] . "</a></p>";
		}	
	}
}

if ($_REQUEST['do'] == 'updatechannelcounts')
{
	if (empty($vbulletin->GPC['startat']))
	{
		$vbulletin->GPC['startat'] = 0;
	}
	$channelTypeid = vB_Types::instance()->getContentTypeID('vBForum_Channel');
	$assertor = vB::getDbAssertor();

	if ($vbulletin->GPC['startat'] > 10)
	{
		print_stop_message2('updated_forum_successfully', 'misc');
	}
	else
	{
		$assertor->assertQuery('vBInstall:updateChannelCounts',
			array('channeltypeid' =>  $channelTypeid));
		$processed = $assertor->getRow('vBInstall:getProcessedCount', array());
		$processed = $processed['recs'];
		if (empty($processed))
		{
			print_stop_message2('updated_forum_successfully', 'misc');
		}
		else
		{
			$args = array();
			$startat = $vbulletin->GPC['startat']++;
			parse_str(vB::getCurrentSession()->get('sessionurl'),$args);
			$args['do'] = 'updatechannelcounts';
			$args['startat'] = $startat;
			print_cp_redirect2('misc', $args);
			echo "<p><a href=\"misc.php?" . vB::getCurrentSession()->get('sessionurl') . "do=updatechannelcounts&amp;startat=$startat\">" . $vbphrase['click_here_to_continue_processing'] . "</a></p>";
		}
	}
}

// ###################### Start update threads #######################
if ($_REQUEST['do'] == 'updatethread')
{
	if (empty($vbulletin->GPC['perpage']))
	{
		$vbulletin->GPC['perpage'] = 2000;
	}

	echo '<p>' . $vbphrase['updating_threads'] . '</p>';
	
	$excludeTypes = array(
		vB_Types::instance()->getContentTypeID('vBForum_Channel'),
		vB_Types::instance()->getContentTypeID('vBForum_Photo'),
		vB_Types::instance()->getContentTypeID('vBForum_Attach'),
	);


	$starters = $vbulletin->db->query_read("
		SELECT DISTINCT(node.nodeid)
		FROM " . TABLE_PREFIX . "vb_node as node
		INNER JOIN " . TABLE_PREFIX ."vb_closure AS cl ON cl.parent = node.nodeid AND cl.child <> node.nodeid 
		WHERE nodeid = starter AND contenttypeid not in (" . implode(',', $excludeTypes) . ")
		LIMIT " . $vbulletin->GPC['startat'] . ", " . $vbulletin->GPC['perpage'] . "
	");
	
	$finishat = 0;

	while ($starter = $vbulletin->db->fetch_array($starters))
	{
		vB_Node::fixNodeCount($starter['nodeid']);
		echo construct_phrase($vbphrase['processing_x'], $starter['nodeid'])."<br />\n";
		vbflush();

		$finishat++;
	}
	
	if ($finishat < $vbulletin->GPC['perpage'])
	{
		print_stop_message2('updated_threads_successfully', 'misc');
	}
	else
	{
		$args = array();
		$startat = $vbulletin->GPC['startat'] + $vbulletin->GPC['perpage'];
		parse_str(vB::getCurrentSession()->get('sessionurl'),$args);
		$args['do'] = 'updatethread';
		$args['startat'] = $startat;
		$args['pp'] = $vbulletin->GPC['perpage'];
		print_cp_redirect2('misc', $args);
		echo "<p><a href=\"misc.php?" . vB::getCurrentSession()->get('sessionurl') . "do=updatethread&amp;startat=$startat&amp;pp=" . $vbulletin->GPC['perpage'] . "\">" . $vbphrase['click_here_to_continue_processing'] . "</a></p>";
	}
}

// ###################### Start update similar threads #######################
/*
if ($_REQUEST['do'] == 'updatesimilar')
{
	require_once(DIR . '/includes/functions_search.php');
	if (empty($vbulletin->GPC['perpage']))
	{
		$vbulletin->GPC['perpage'] = 100;
	}


	echo '<p>' . $vbphrase['updating_similar_threads'] . '</p>';

	$threads = $vbulletin->db->query_read("
		SELECT title, threadid
		FROM " . TABLE_PREFIX . "thread
		WHERE threadid >= " . $vbulletin->GPC['startat'] . "
		ORDER BY threadid
		LIMIT " . $vbulletin->GPC['perpage']
	);

	$finishat = $vbulletin->GPC['startat'];

	while ($thread = $vbulletin->db->fetch_array($threads))
	{
		//require_once(DIR . '/vb/search/core.php');
		$searchcontroller = vB_Search_Core::get_instance()->get_search_controller();
		$similarthreads = $searchcontroller->get_similar_threads($thread['title'], $thread['threadid']);

		$threadman =& datamanager_init('Thread', $vbulletin, vB_DataManager_Constants::ERRTYPE_CP, 'threadpost');
		$threadman->set_existing($thread);
		$threadman->set('similar', implode(',', $similarthreads));
		$threadman->save();

		echo construct_phrase($vbphrase['processing_x'], $thread['threadid']) . "<br />\n";
		vbflush();

		$finishat = ($thread['threadid'] > $finishat ? $thread['threadid'] : $finishat);
	}

	$finishat++;

	if ($checkmore = $vbulletin->db->query_first("SELECT threadid FROM " . TABLE_PREFIX . "thread WHERE threadid >= $finishat LIMIT 1"))
	{
		$args = array();
		parse_str(vB::getCurrentSession()->get('sessionurl'),$args);
		$args['do'] = 'updatesimilar';
		$args['startat'] = $finishat;
		$args['pp'] = $vbulletin->GPC['perpage'];
		print_cp_redirect2('misc', $args);
		echo "<p><a href=\"misc.php?" . vB::getCurrentSession()->get('sessionurl') . "do=updatesimilar&amp;startat=$finishat&amp;pp=" . $vbulletin->GPC['perpage'] . "\">" . $vbphrase['click_here_to_continue_processing'] . "</a></p>";
	}
	else
	{
		print_stop_message2('updated_similar_threads_successfully', 'misc');
	}
}
*/
// ################## Start rebuilding user reputation ######################
if ($_POST['do'] == 'rebuildreputation')
{
	$vbulletin->input->clean_array_gpc('p', array(
		'reputation_base' => vB_Cleaner::TYPE_INT,
	));

	$users = $vbulletin->db->query_read("
		SELECT reputation.userid, SUM(reputation.reputation) AS totalrep
		FROM " . TABLE_PREFIX . "reputation AS reputation
		GROUP BY reputation.userid
	");

	$userrep = array();
	while ($user = $vbulletin->db->fetch_array($users))
	{
		$user['totalrep'] += $vbulletin->GPC['reputation_base'];
		$userrep["$user[totalrep]"] .= ",$user[userid]";
	}

	if (!empty($userrep))
	{
		foreach ($userrep AS $reputation => $ids)
		{
			$usercasesql .= " WHEN userid IN (0$ids) THEN $reputation";
		}
	}

	if ($usercasesql)
	{
		$vbulletin->db->query_write("
			UPDATE " . TABLE_PREFIX . "user
			SET reputation =
				CASE
					$usercasesql
					ELSE " . $vbulletin->GPC['reputation_base'] . "
				END
		");
	}
	else // there is no reputation
	{
		$vbulletin->db->query_write("
			UPDATE " . TABLE_PREFIX . "user
			SET reputation = " . $vbulletin->GPC['reputation_base'] . "
		");
	}

	require_once(DIR . '/includes/adminfunctions_reputation.php');
	build_reputationids();

	print_stop_message2('rebuilt_user_reputation_successfully', 'misc');

}

// ################## Start rebuilding attachment thumbnails ################
if ($_REQUEST['do'] == 'rebuildthumbs')
{
	$vbulletin->input->clean_array_gpc('r', array(
		'quality'      => vB_Cleaner::TYPE_UINT,
		'autoredirect' => vB_Cleaner::TYPE_BOOL,
	));

	if (($memory_limit = ini_size_to_bytes(@ini_get('memory_limit'))) < 128 * 1024 * 1024 AND $memory_limit > 0)
	{
		@ini_set('memory_limit', 128 * 1024 * 1024);
	}
        
	$image =& vB_Image::instance();

	$validtypes =& $image->thumb_extensions;
	$extensions = array();
	foreach ($vbulletin->attachmentcache AS $key => $value)
	{
		$key = strtolower($key);
		if ($key != 'extensions' AND !empty($validtypes["$key"]))
		{
			$extensions[] = "'$key'";
		}
	}
	$extensions = implode(',', $extensions);

	if (!$extensions)
	{
		print_stop_message2('you_have_no_attachments_set_to_thumb');
	}

	if ($vbulletin->options['imagetype'] != 'Magick' AND !function_exists('imagetypes'))
	{
		//print_stop_message2('your_version_no_image_support', 'misc');
	}

	if (empty($vbulletin->GPC['perpage']))
	{
		$vbulletin->GPC['perpage'] = 20;
	}

	if (!$vbulletin->GPC['startat'])
	{
		$firstattach = $vbulletin->db->query_first("
			SELECT MIN(filedataid) AS min
			FROM " . TABLE_PREFIX . "filedata"
		);
		$vbulletin->GPC['startat'] = intval($firstattach['min']);
	}

	echo '<p>' . construct_phrase($vbphrase['building_attachment_thumbnails'], "misc.php?" . vB::getCurrentSession()->get('sessionurl') . "do=rebuildthumbs&startat=" . $vbulletin->GPC['startat'] . "&pp=" . $vbulletin->GPC['perpage'] . "&autoredirect=" . $vbulletin->GPC['autoredirect'] . "&quality=" . $vbulletin->GPC['quality']) . '</p>';

	if ($vbulletin->options['attachfile'])
	{
		require_once(DIR . '/includes/functions_file.php');
	}

	$attachments = $vbulletin->db->query_read("
		SELECT
			filedataid, filedata, userid, extension, dateline, CONCAT('file.', extension) AS filename
		FROM " . TABLE_PREFIX . "filedata
		WHERE filedataid >= " . $vbulletin->GPC['startat'] . "
			AND	extension IN ($extensions)
		ORDER BY filedataid
		LIMIT " . $vbulletin->GPC['perpage']
	);

	$finishat = $vbulletin->GPC['startat'];

	while ($attachment = $vbulletin->db->fetch_array($attachments))
	{
		if (!$vbulletin->options['attachfile']) // attachments are in the database
		{
			if ($vbulletin->options['safeupload'])
			{
				$filename = $vbulletin->options['tmppath'] . '/' . md5(uniqid(microtime()) . $vbulletin->userinfo['userid']);
			}
			else
			{
				$filename = tempnam(ini_get('upload_tmp_dir'), 'vbthumb');
			}
			$filenum = fopen($filename, 'wb');
			fwrite($filenum, $attachment['filedata']);
			fclose($filenum);
		}
		else
		{
			$filename = fetch_attachment_path($attachment['userid'], $attachment['filedataid']);
		}

		echo construct_phrase($vbphrase['processing_x'], "$vbphrase[attachment] : $attachment[filedataid] ($attachment[extension])");

		if (!is_readable($filename) OR !@filesize($filename))
		{
			echo '<b>' . $vbphrase['error_attachment_missing'] . '</b><br />';
			continue;
		}

		$labelimage = ($vbulletin->options['attachthumbs'] == 3 OR $vbulletin->options['attachthumbs'] == 4);
		$drawborder = ($vbulletin->options['attachthumbs'] == 2 OR $vbulletin->options['attachthumbs'] == 4);
		$thumbnail = $image->fetchThumbnail($attachment['filename'], $filename, $vbulletin->options['attachthumbssize'], $vbulletin->options['attachthumbssize'], $vbulletin->GPC['quality'], $labelimage, $drawborder);

		// Remove temporary file we used to generate thumbnail
		if (!$vbulletin->options['attachfile'])
		{
			@unlink($filename);
		}

		$attachdata =& datamanager_init('Filedata', $vbulletin, vB_DataManager_Constants::ERRTYPE_SILENT, 'attachment');
		$attachdata->set_existing($attachment);
		$attachdata->set('width', $thumbnail['source_width']);
		$attachdata->set('height', $thumbnail['source_height']);
		if (!empty($thumbnail['filedata']))
		{
			$attachdata->setr('thumbnail', $thumbnail['filedata']);
			$attachdata->set('thumbnail_dateline', TIMENOW);
			$attachdata->set('thumbnail_width', $thumbnail['width']);
			$attachdata->set('thumbnail_height', $thumbnail['height']);
		}
		if (!($result = $attachdata->save()))
		{
			if (!empty($attachdata->errors[0]))
			{
				echo $attacherror =& $attachdata->errors[0];
			}
		}
		unset($attachdata);

		if (!empty($thumbnail['imageerror']))
		{
			echo ' <b>' . $vbphrase["error_$thumbnail[imageerror]"] . '</b>';
		}
		else if (empty($thumbnail['filedata']))
		{
			echo ' <b>' . $vbphrase['error'] . '</b>';
		}
		echo '<br />';
		vbflush();

		$finishat = ($attachment['filedataid'] > $finishat ? $attachment['filedataid'] : $finishat);
	}

	$finishat++;

	if ($checkmore = $vbulletin->db->query_first("SELECT filedataid FROM " . TABLE_PREFIX . "filedata WHERE filedataid >= $finishat AND extension IN ($extensions) ORDER BY filedataid LIMIT 1"))
	{
		if ($vbulletin->GPC['autoredirect'] == 1)
		{
			$args = array();
			parse_str(vB::getCurrentSession()->get('sessionurl'),$args);
			$args['do'] = 'rebuildthumbs';
			$args['startat'] = $finishat;
			$args['pp'] = $vbulletin->GPC['perpage'];
			$args['quality'] = $vbulletin->GPC['quality'];
			$args['autoredirect'] = 1;
			print_cp_redirect2('misc', $args);
		}
		echo "<p><a href=\"misc.php?" . vB::getCurrentSession()->get('sessionurl') . "do=rebuildthumbs&amp;startat=$finishat&amp;pp=" . $vbulletin->GPC['perpage'] . "&amp;quality=" . $vbulletin->GPC['quality'] . '">' . $vbphrase['click_here_to_continue_processing'] . "</a></p>";
	}
	else
	{
		print_stop_message2('rebuilt_attachment_thumbnails_successfully', 'misc');
	}
}

// ################## Start rebuilding avatar thumbnails ################
if ($_REQUEST['do'] == 'rebuildavatars')
{
	$vbulletin->input->clean_array_gpc('r', array(
		'autoredirect' => vB_Cleaner::TYPE_BOOL,
	));

	if (($memory_limit = ini_size_to_bytes(@ini_get('memory_limit'))) < 128 * 1024 * 1024 AND $memory_limit > 0)
	{
		@ini_set('memory_limit', 128 * 1024 * 1024);
	}

	if ($vbulletin->options['imagetype'] != 'Magick' AND !function_exists('imagetypes'))
	{
		//print_stop_message2('your_version_no_image_support', 'misc');
	}

	if (empty($vbulletin->GPC['perpage']))
	{
		$vbulletin->GPC['perpage'] = 20;
	}

	if (!$vbulletin->GPC['startat'])
	{
		$firstattach = $vbulletin->db->query_first("SELECT MIN(userid) AS min FROM " . TABLE_PREFIX . "customavatar");
		$vbulletin->GPC['startat'] = intval($firstattach['min']);
	}

	echo '<p>' . construct_phrase($vbphrase['building_avatar_thumbnails'], "misc.php?" . vB::getCurrentSession()->get('sessionurl') . "do=rebuildavatars&startat=" . $vbulletin->GPC['startat'] . "&pp=" . $vbulletin->GPC['perpage'] . "&autoredirect=" . $vbulletin->GPC['autoredirect']) . '</p>';

	$avatars = $vbulletin->db->query_read("
		SELECT user.userid, user.avatarrevision, customavatar.filedata, customavatar.filename, customavatar.dateline, customavatar.width, customavatar.height
		FROM " . TABLE_PREFIX . "customavatar AS customavatar
		INNER JOIN " . TABLE_PREFIX . "user AS user ON(user.userid=customavatar.userid)
		WHERE customavatar.userid >= " . $vbulletin->GPC['startat'] . "
		ORDER BY customavatar.userid
		LIMIT " . $vbulletin->GPC['perpage']
	);

	$finishat = $vbulletin->GPC['startat'];

	while ($avatar = $vbulletin->db->fetch_array($avatars))
	{
		echo construct_phrase($vbphrase['processing_x'], "$vbphrase[avatar] : $avatar[userid] (" . file_extension($avatar['filename']) . ') ');

		if ($vbulletin->options['usefileavatar'])
		{
			$avatarurl = $vbulletin->options['avatarurl'] . "/avatar$avatar[userid]_$avatar[avatarrevision].gif";
			$avatar['filedata'] = @file_get_contents($avatarurl);
		}

		if (!empty($avatar['filedata']))
		{
			$dataman = new vB_Datamanager_Userpic_Avatar($vbulletin, vB_DataManager_Constants::ERRTYPE_STANDARD, 'userpic');
			$dataman->set_existing($avatar);
			$dataman->save();
			unset($dataman);
		}

		echo '<br />';
		vbflush();

		$finishat = ($avatar['userid'] > $finishat ? $avatar['userid'] : $finishat);
	}

	$finishat++;

	if ($checkmore = $vbulletin->db->query_first("SELECT userid FROM " . TABLE_PREFIX . "customavatar WHERE userid >= $finishat LIMIT 1"))
	{
		if ($vbulletin->GPC['autoredirect'] == 1)
		{
			$args = array();
			parse_str(vB::getCurrentSession()->get('sessionurl'),$args);
			$args['do'] = 'rebuildavatars';
			$args['startat'] = $finishat;
			$args['pp'] = $vbulletin->GPC['perpage'];
			$args['autoredirect'] = 1;
			print_cp_redirect2('misc', $args);
		}
		echo "<p><a href=\"misc.php?" . vB::getCurrentSession()->get('sessionurl') . "do=rebuildavatars&amp;startat=$finishat&amp;pp=" . $vbulletin->GPC['perpage'] . '">' . $vbphrase['click_here_to_continue_processing'] . "</a></p>";
	}
	else
	{
		print_stop_message2('rebuilt_avatar_thumbnails_successfully', 'misc');
	}
}

// ################## Start rebuilding admin avatar thumbnails ################
if ($_REQUEST['do'] == 'rebuildadminavatars')
{
	$vbulletin->input->clean_array_gpc('r', array(
		'autoredirect' => vB_Cleaner::TYPE_BOOL,
	));

	if (($current_memory_limit = ini_size_to_bytes(@ini_get('memory_limit'))) < 128 * 1024 * 1024 AND $current_memory_limit > 0)
	{
		@ini_set('memory_limit', 128 * 1024 * 1024);
	}

	if ($vbulletin->options['imagetype'] != 'Magick' AND !function_exists('imagetypes'))
	{
		//print_stop_message2('your_version_no_image_support', 'misc');
	}

	$avatarpath = DIR . '/images/avatars/thumbs';

	if (!is_writable($avatarpath))
	{
		print_stop_message2('avatarpath_not_writable');
	}

	if (empty($vbulletin->GPC['perpage']))
	{
		$vbulletin->GPC['perpage'] = 20;
	}

	if (!$vbulletin->GPC['startat'])
	{
		$firstavatar = $vbulletin->db->query_first("SELECT MIN(avatarid) AS min FROM " . TABLE_PREFIX . "avatar");
		$vbulletin->GPC['startat'] = intval($firstavatar['min']);
	}

	echo '<p>' . construct_phrase($vbphrase['building_avatar_thumbnails'], "misc.php?" . vB::getCurrentSession()->get('sessionurl') . "do=rebuildadminavatars&startat=" . $vbulletin->GPC['startat'] . "&pp=" . $vbulletin->GPC['perpage'] . "&autoredirect=" . $vbulletin->GPC['autoredirect']) . '</p>';

	$avatars = $vbulletin->db->query_read("
		SELECT avatarid, avatarpath, title
		FROM " . TABLE_PREFIX . "avatar
		WHERE avatarid >= " . $vbulletin->GPC['startat'] . "
		ORDER BY avatarid
		LIMIT " . $vbulletin->GPC['perpage']
	);

	$finishat = $vbulletin->GPC['startat'];

	while ($avatar = $vbulletin->db->fetch_array($avatars))
	{
		$finishat = ($avatar['avatarid'] > $finishat ? $avatar['avatarid'] : $finishat);

		echo construct_phrase($vbphrase['processing_x'], "$vbphrase[avatar] : $avatar[avatarid] ($avatar[title])");
		$imagepath = $avatar['avatarpath'];
		$destination = $avatarpath . '/' . $avatar['avatarid'] . '.gif';
		$remotefile = false;

		if (is_file($avatar['avatarpath']))
		{
			$imagepath = $avatar['avatarpath'];
		}
		else
		{
			$location = dirname(__FILE__) . '/../' . $avatar['avatarpath'] ;
			if (is_file($location))
			{
				$imagepath = $location;
			}
			else if ($avatar['avatarpath'][0] == '/')
			{
				// absolute web path -- needs to be translated into a full path and handled that way
				$avatar['avatarpath'] = create_full_url($avatar['avatarpath']);
			}
		}

		if (substr($avatar['avatarpath'], 0, 7) == 'http://')
		{
			if ($vbulletin->options['safeupload'])
			{
				$imagepath = $vbulletin->options['tmppath'] . '/' . md5(uniqid(microtime()) . $avatar['avatarid']);
			}
			else
			{
				$imagepath = tempnam(ini_get('upload_tmp_dir'), 'vbthumb');
			}
			if ($filenum = @fopen($imagepath, 'wb'))
			{
				require_once(DIR . '/includes/class_vurl.php');
				$vurl = new vB_vURL($vbulletin);
				$vurl->set_option(VURL_URL, $avatar['avatarpath']);
				$vurl->set_option(VURL_HEADER, true);
				$vurl->set_option(VURL_RETURNTRANSFER, true);
				if ($result = $vurl->exec())
				{
					@fwrite($filenum, $result['body']);
				}
				unset($vurl);
				@fclose($filenum);
				$remotefile = true;
			}
		}

		if (!file_exists($imagepath))
		{
			echo " ... <span class=\"modsincethirtydays\">$vbphrase[unable_to_read_avatar]</span><br />\n";
			vbflush();
			continue;
		}

		$image =& vB_Image::instance();
		$imageinfo = $image->fetchImageInfo($imagepath);
		if ($imageinfo[0] > FIXED_SIZE_AVATAR_WIDTH OR $imageinfo[1] > FIXED_SIZE_AVATAR_HEIGHT)
		{
			$file = 'file.' . ($imageinfo[2] == 'JPEG' ? 'jpg' : strtolower($imageinfo[2]));
			$thumbnail = $image->fetchThumbnail($file, $imagepath, FIXED_SIZE_AVATAR_WIDTH, FIXED_SIZE_AVATAR_HEIGHT);
			if ($thumbnail['filedata'] AND $filenum = @fopen($destination, 'wb'))
			{
				@fwrite($filenum, $thumbnail['filedata']);
				@fclose($filenum);
			}
			unset($thumbnail);
		}
		else if ($filenum = fopen($destination, 'wb'))
		{
			@fwrite($filenum, file_get_contents($imagepath));
			fclose($filenum);
		}

		if ($remotefile)
		{
			@unlink($imagepath);
		}

		echo "<br />\n";
		vbflush();
	}

	$finishat++;

	if ($checkmore = $vbulletin->db->query_first("SELECT avatarid FROM " . TABLE_PREFIX . "avatar WHERE avatarid >= $finishat LIMIT 1"))
	{
		if ($vbulletin->GPC['autoredirect'] == 1)
		{
			$args = array();
			parse_str(vB::getCurrentSession()->get('sessionurl'),$args);
			$args['do'] = 'rebuildadminavatars';
			$args['startat'] = $finishat;
			$args['pp'] = $vbulletin->GPC['perpage'];
			$args['autoredirect'] = 1;
			print_cp_redirect2('misc', $args);
		}
		echo "<p><a href=\"misc.php?" . vB::getCurrentSession()->get('sessionurl') . "do=rebuildadminavatars&amp;startat=$finishat&amp;pp=" . $vbulletin->GPC['perpage'] . '">' . $vbphrase['click_here_to_continue_processing'] . "</a></p>";
	}
	else
	{
		print_stop_message2('rebuilt_avatar_thumbnails_successfully', 'misc');
	}

}


// ################## Start rebuilding sgicon thumbnails ################
/*
if ($_REQUEST['do'] == 'rebuildsgicons')
{
	$vbulletin->input->clean_array_gpc('r', array(
		'quality'      => vB_Cleaner::TYPE_UINT,
		'autoredirect' => vB_Cleaner::TYPE_BOOL,
		'perpage'      => vB_Cleaner::TYPE_UINT,
		'startat'      => vB_Cleaner::TYPE_UINT
	));

	// Increase memlimit
	if (($memory_limit = ini_size_to_bytes(@ini_get('memory_limit'))) < 128 * 1024 * 1024 AND $memory_limit > 0)
	{
		@ini_set('memory_limit', 128 * 1024 * 1024);
	}

	// Get dimension constants
	require_once(DIR . '/includes/functions_socialgroup.php');

	// Get image handler
	$image = vB_Image::instance();

	// Check if image manip is supported
	if ($vbulletin->options['imagetype'] != 'Magick' AND !function_exists('imagetypes'))
	{
		print_stop_message2('your_version_no_image_support');
	}

	$vbulletin->GPC['perpage'] = max($vbulletin->GPC['perpage'], 20);

	echo '<p>' . construct_phrase($vbphrase['building_sgicon_thumbnails'], "misc.php?" . vB::getCurrentSession()->get('sessionurl') . "do=rebuildsgicons&startat=" . $vbulletin->GPC['startat'] . "&pp=" . $vbulletin->GPC['perpage'] . "&autoredirect=" . $vbulletin->GPC['autoredirect'] . "&quality=" . $vbulletin->GPC['quality']) . '</p>';

	// Get group info
	$result = $vbulletin->db->query_read("
		SELECT socialgroupicon.dateline, socialgroupicon.userid, socialgroupicon.filedata, socialgroupicon.extension,
				socialgroupicon.width, socialgroupicon.height, socialgroupicon.groupid
		FROM " . TABLE_PREFIX . "socialgroupicon AS socialgroupicon
		LIMIT " . intval($vbulletin->GPC['startat']) . ', ' . intval($vbulletin->GPC['perpage'])
	);

	$checkmore = ($vbulletin->db->num_rows($result) >= $vbulletin->GPC['perpage']);

	// Create dm for icon and ensure icon filedata is set but thumbdata is empty, so that thumbs are rebuilt by the dm
	while ($icon = $vbulletin->db->fetch_array($result))
	{
		// some transaltion for group info
		$icon['icondateline'] = $icon['dateline'];

		echo construct_phrase($vbphrase['processing_x'], "$vbphrase[socialgroup_icon] $icon[groupid]<br />\n");
		vbflush();

		$filedata = false;

		if ($vbulletin->options['usefilegroupicon'])
		{
			$iconpath = fetch_socialgroupicon_url($icon, false, true, true);
			$thumbpath = fetch_socialgroupicon_url($group, true, true, true);
			$filedata = @file_get_contents($iconpath);
		}
		else
		{
			$filedata = $icon['filedata'];
		}

		if ($filedata)
		{
			$icondm = datamanager_init('SocialGroupIcon', $vbulletin, vB_DataManager_Constants::ERRTYPE_CP);
			$icondm->set_existing($icon);
			$icondm->set('thumbnail_filedata', false);
			$icondm->set('filedata', $filedata);
			$icondm->set_info('thumbnail_quality', $vbulletin->GPC['quality']);

			if (!$icondm->save())
			{
				echo ('<b>' . (!empty($icondm->errors[0]) ? $icondm->errors[0] : $vbphrase['error']) . '</b>');
			}
		}
	}
	$vbulletin->db->free_result($result);

	echo '<br />';
	vbflush();

	$startat = $vbulletin->GPC['startat'] + $vbulletin->GPC['perpage'];

	if ($checkmore)
	{
		if ($vbulletin->GPC['autoredirect'] == 1)
		{
			$args = array();
			parse_str(vB::getCurrentSession()->get('sessionurl'),$args);
			$args['do'] = 'rebuildsgicons';
			$args['startat'] = $finishat;
			$args['pp'] = $vbulletin->GPC['perpage'];
			$args['quality'] = $vbulletin->GPC['quality'];
			$args['autoredirect'] = 1;
			print_cp_redirect2('misc', $args);
		}
		echo "<p><a href=\"misc.php?" . vB::getCurrentSession()->get('sessionurl') . "do=rebuildsgicons&amp;startat=$startat&amp;pp=" . $vbulletin->GPC['perpage'] . '&amp;quality=' . $vbulletin->GPC['quality'] . '>' . $vbphrase['click_here_to_continue_processing'] . '</a></p>';
	}
	else
	{
		// rebuild newest groups cache
		fetch_socialgroup_newest_groups(true, false, !$vbulletin->options['sg_enablesocialgroupicons']);

		print_stop_message2('rebuilt_sgicon_thumbnails_successfully', 'misc');
	}
}
*/

// ###################### Start rebuilding post cache #######################
/*
if ($_POST['do'] == 'rebuildalbumupdates')
{
	if (!$vbulletin->options['album_recentalbumdays'])
	{
		print_stop_message2('recent_album_updates_disabled', 'misc');
	}

	require_once(DIR . '/includes/functions_album.php');

	exec_rebuild_album_updates();

	print_stop_message2('recent_album_updates_rebuilt', 'misc');
}
*/

// ###################### Start rebuilding post cache #######################
/*
if ($_REQUEST['do'] == 'buildpostcache')
{
	$bbcodelist = array();
	$bbcodes = $vbulletin->db->query_read("
		SELECT templateid,  template
		FROM " . TABLE_PREFIX . "template
		WHERE title IN ('bbcode_quote', 'bbcode_php', 'bbcode_code', 'bbcode_html', 'bbcode_video')
	");
	while ($bbcode = $vbulletin->db->fetch_array($bbcodes))
	{
		$bbcodelist["$bbcode[templateid]"] = $bbcode['template'];
	}

	$stylelist = array();
	//$uniquelist = array();
	$styles = $vbulletin->db->query_read("
		SELECT * FROM " . TABLE_PREFIX . "style
	");

	while ($style = $vbulletin->db->fetch_array($styles))
	{
		$tlist = unserialize($style['templatelist']);
		$stylelist["$style[styleid]"]['templatelist'] = array(
			'bbcode_code'  =>& $bbcodelist["$tlist[bbcode_code]"],
			'bbcode_quote' =>& $bbcodelist["$tlist[bbcode_quote]"],
			'bbcode_php'   =>& $bbcodelist["$tlist[bbcode_php]"],
			'bbcode_html'  =>& $bbcodelist["$tlist[bbcode_html]"],
			'bbcode_video' =>& $bbcodelist["$tlist[bbcode_video]"],
		);

		$stylelist["$style[styleid]"]['idlist'] = array(
			'bbcode_code'  => intval($tlist['bbcode_code_styleid']),
			'bbcode_quote' => intval($tlist['bbcode_quote_styleid']),
			'bbcode_php'   => intval($tlist['bbcode_php_styleid']),
			'bbcode_html'  => intval($tlist['bbcode_html_styleid']),
			'bbcode_video' => intval($tlist['bbcode_video_styleid']),
		);

		$stylelist["$style[styleid]"]['newstylevars'] = unserialize($style['newstylevars']);
	}
	$stylelist["0"] =& $stylelist["{$vbulletin->options['styleid']}"];

	require_once(DIR . '/includes/class_bbcode.php');
	$bbcode_parser = new vB_BbCodeParser($vbulletin, fetch_tag_list());

	if (empty($vbulletin->GPC['perpage']))
	{
		$vbulletin->GPC['perpage'] = 1000;
	}

	if ($vbulletin->GPC['startat'] == 0)
	{
		$vbulletin->db->query_write("TRUNCATE TABLE " . TABLE_PREFIX . "postparsed");
		$firstpost = $vbulletin->db->query_first("SELECT MIN(postid) AS min FROM " . TABLE_PREFIX . "post");
		$vbulletin->GPC['startat'] = intval($firstpost['min']);
	}

	echo '<p>' . $vbphrase['building_post_cache'] . '</p>';

	$posts = $vbulletin->db->query_read("
		SELECT postid, forumid, pagetext, allowsmilie, thread.lastpost
		FROM " . TABLE_PREFIX . "post AS post, " . TABLE_PREFIX . "thread AS thread
		WHERE post.threadid = thread.threadid AND
			postid >= " . $vbulletin->GPC['startat'] . " AND
			thread.lastpost >= " . (TIMENOW - ($vbulletin->options['cachemaxage'] * 60 * 60 * 24)) . "
		ORDER BY postid
		LIMIT " . $vbulletin->GPC['perpage']
	);

	$finishat = $vbulletin->GPC['startat'];

	$saveparsed = '';
	while ($post = $vbulletin->db->fetch_array($posts))
	{
		# Only cache posts for the chosen style if this post belongs to a forum with a styleoverride
		if ($vbulletin->forumcache["$post[forumid]"]['options'] & $vbulletin->bf_misc_forumoptions['styleoverride'])
		{
			$styleid = $vbulletin->forumcache["$post[forumid]"]['styleid'];
			$vbulletin->templatecache =& $stylelist["$styleid"]['templatelist'];

			// The fact that we use $userinfo here means that if you were to use any language specific stylevars in these templates (which we don't do by default), they would be of this user's language
			// The only remedy for this is to create even more scenarios in the post parsed table with left -> right, right -> left and the imageoverride folder :eek:
			$vbulletin->stylevars = $stylelist["$styleid"]['newstylevars'];
			fetch_stylevars($stylelist["$styleid"], $vbulletin->userinfo);

			$parsedtext = $bbcode_parser->parse($post['pagetext'], $post['forumid'], $post['allowsmilie'], false, '', false, true);

			$saveparsed .= ", ($post[postid],
				" . intval($post['lastpost']) . ",
				" . intval($bbcode_parser->cached['has_images']) . ",
				'" . $vbulletin->db->escape_string($bbcode_parser->cached['text']) . "',
					" . intval($styleid) . ",
					" . intval(LANGUAGEID) . "
			)";
			echo construct_phrase($vbphrase['processing_x'], $post['postid']) . "<br />\n";
		}
		else
		{
			echo construct_phrase($vbphrase['processing_x'], $post['postid']) . "\n";
			$count = 0;

			foreach (array_keys($stylelist) AS $styleid)
			{
				if ($styleid == 0)
				{
					continue;
				}
				$count++;
				if ($count > 1)
				{
					echo ',';
				}
				echo " $count";
				$vbulletin->templatecache =& $stylelist["$styleid"]['templatelist'];

				// The fact that we use $userinfo here means that if you were to use any language specific stylevars in these templates (which we don't do by default), they would be of this user's language
				// The only remedy for this is to create even more scenarios in the post parsed table with left -> right, right -> left and the imageoverride folder :eek:
				$vbulletin->stylevars = $stylelist["$styleid"]['newstylevars'];
				fetch_stylevars($stylelist["$styleid"], $vbulletin->userinfo);

				$parsedtext = $bbcode_parser->parse($post['pagetext'], $post['forumid'], $post['allowsmilie'], false, '', false, true);
				$saveparsed .= ", ($post[postid],
					" . intval($post['lastpost']) . ",
					" . intval($bbcode_parser->cached['has_images']) . ",
					'" . $vbulletin->db->escape_string($bbcode_parser->cached['text']) . "',
					" . intval($styleid) . ",
					" . intval(LANGUAGEID) . "
				)";
			}
			echo "<br />\n";
		}

		if (strlen($saveparsed) > 500000)
		{
			// break the query every 500k
			$saveparsed = substr($saveparsed, 1);

			// insert query //
			$vbulletin->db->query_write("
				REPLACE INTO " . TABLE_PREFIX . "postparsed
				(postid, dateline, hasimages, pagetext_html , styleid, languageid)
				VALUES
				$saveparsed
			");

			$saveparsed = '';
		}

		$finishat = ($post['postid'] > $finishat ? $post['postid'] : $finishat);
	}
	if ($saveparsed)
	{
		$saveparsed = substr($saveparsed, 1);
		//insert query//
		$vbulletin->db->query_write("
			REPLACE INTO " . TABLE_PREFIX . "postparsed
			(postid, dateline, hasimages, pagetext_html , styleid, languageid)
			VALUES
			$saveparsed
		");
	}

	vbflush();

	$finishat++;

	if ($checkmore = $vbulletin->db->query_first("SELECT postid FROM " . TABLE_PREFIX . "post WHERE postid >= $finishat LIMIT 1"))
	{
		$args = array();
		parse_str(vB::getCurrentSession()->get('sessionurl'),$args);
		$args['do'] = 'buildpostcache';
		$args['startat'] = $finishat;
		$args['pp'] = $vbulletin->GPC['perpage'];
		print_cp_redirect2('misc', $args);
		echo "<p><a href=\"misc.php?" . vB::getCurrentSession()->get('sessionurl') . "do=buildpostcache&amp;startat=$finishat&amp;pp=" . $vbulletin->GPC['perpage'] . "\">" . $vbphrase['click_here_to_continue_processing'] . "</a></p>";
	}
	else
	{
		print_stop_message2('updated_post_cache_successfully', 'misc');
	}
}
*/

if ($_POST['do'] == 'truncatesigcache')
{
	$vbulletin->db->query_write("TRUNCATE TABLE " . TABLE_PREFIX . "sigparsed");

	print_stop_message2('updated_signature_cache_successfully', 'misc');
}

// ###################### Start remove dupe #######################
if ($_REQUEST['do'] == 'removedupe')
{
	if (empty($vbulletin->GPC['perpage']))
	{
		$vbulletin->GPC['perpage'] = 500;
	}


	echo '<p>' . $vbphrase['removing_duplicate_threads'] . '</p>';
	
	$channelContentType = vB_Types::instance()->getContentTypeID('vBForum_Channel');

	$threads = $vbulletin->db->query_read("
		SELECT nodeid, title, parentid, authorname, publishdate
		FROM " . TABLE_PREFIX . "node WHERE nodeid >= " . $vbulletin->GPC['startat'] . " AND contenttypeid != " . $channelContentType . "
		ORDER BY nodeid
		LIMIT " . $vbulletin->GPC['perpage']
	);

	$finishat = $vbulletin->GPC['startat'];

	while ($thread = $vbulletin->db->fetch_array($threads))
	{
		$deletethreads = $vbulletin->db->query_read("
			SELECT *
			FROM " . TABLE_PREFIX . "node
			WHERE title = '" . $vbulletin->db->escape_string($thread['title']) . "' AND
				parentid = $thread[parentid] AND
				authorname = '" . $vbulletin->db->escape_string($thread['authorname']) . "' AND
				publishdate = $thread[publishdate] AND
				nodeid > $thread[nodeid] AND
				contenttypeid != " . $channelContentType . "
		");
		while ($deletethread = $vbulletin->db->fetch_array($deletethreads))
		{
			vB_Api::instanceInternal('node')->deleteNodes($deletethread['nodeid']);
			echo "&nbsp;&nbsp;&nbsp; ".construct_phrase($vbphrase['delete_x'], $deletethread['nodeid'])."<br />";
		}
		echo construct_phrase($vbphrase['processing_x'], $thread['nodeid'])."<br />\n";
		vbflush();

		$finishat = ($thread['nodeid'] > $finishat ? $thread['nodeid'] : $finishat);
	}

	$finishat++;

	if ($checkmore = $vbulletin->db->query_first("SELECT nodeid FROM " . TABLE_PREFIX . "node WHERE nodeid >= $finishat LIMIT 1"))
	{
		$args = array();
		parse_str(vB::getCurrentSession()->get('sessionurl'),$args);
		$args['do'] = 'removedupe';
		$args['startat'] = $finishat;
		$args['pp'] = $vbulletin->GPC['perpage'];
		print_cp_redirect2('misc', $args);
		echo "<p><a href=\"misc.php?" . vB::getCurrentSession()->get('sessionurl') . "do=removedupe&amp;startat=$finishat&amp;pp=" . $vbulletin->GPC['perpage'] . "\">" . $vbphrase['click_here_to_continue_processing'] . "</a></p>";
	}
	else
	{
		print_stop_message2('deleted_duplicate_threads_successfully', 'misc');
	}

}

// ###################### Start find lost users #######################
if ($_POST['do'] == 'lostusers')
{
	$users = $vbulletin->db->query_read("
		SELECT user.userid
		FROM " . TABLE_PREFIX . "user AS user
		LEFT JOIN " . TABLE_PREFIX . "userfield AS userfield USING(userid)
		WHERE userfield.userid IS NULL
	");

	$userids = array();
	while ($user = $vbulletin->db->fetch_array($users))
	{
		$userids[] = $user['userid'];
	}

	if (!empty($userids))
	{
		/*insert query*/
		$vbulletin->db->query_write("INSERT INTO " . TABLE_PREFIX . "userfield (userid) VALUES (" . implode('),(', $userids) . ")");
	}

	$users = $vbulletin->db->query_read("
		SELECT user.userid
		FROM " . TABLE_PREFIX . "user AS user
		LEFT JOIN " . TABLE_PREFIX . "usertextfield AS usertextfield USING(userid)
		WHERE usertextfield.userid IS NULL
	");

	$userids = array();
	while ($user = $vbulletin->db->fetch_array($users))
	{
		$userids[] = $user['userid'];
	}

	if (!empty($userids))
	{
		/*insert query*/
		$vbulletin->db->query_write("INSERT INTO " . TABLE_PREFIX . "usertextfield (userid) VALUES (" . implode('),(', $userids) . ")");
	}

	print_stop_message2('user_records_repaired', 'misc');
}

// ###################### Start add missing keywords #######################
if ($_REQUEST['do'] == 'addmissingkeywords')
{
	require_once(DIR . '/includes/functions_newpost.php');

	if (empty($vbulletin->GPC['perpage']))
	{
		$vbulletin->GPC['perpage'] = 50;
	}

	$finishat = intval($vbulletin->GPC['startat']);
	
	$excludeTypes =vB_Types::instance()->getContentTypeID('vBForum_Channel') . ', ' 
		. vB_Types::instance()->getContentTypeID('vBForum_Photo') . ', '
		. vB_Types::instance()->getContentTypeID('vBForum_Attach') . ', '
		. vB_Types::instance()->getContentTypeID('vBForum_PrivateMessage');

	$threads = $vbulletin->db->query_read($query = "
		SELECT thread.nodeid, thread.taglist, thread.prefixid, thread.title, post.rawtext AS firstpost
		FROM " . TABLE_PREFIX . "node AS thread
		LEFT JOIN " . TABLE_PREFIX . "text AS post ON(post.nodeid = thread.nodeid)
		WHERE thread.keywords IS NULL
		AND thread.contenttypeid NOT IN ($excludeTypes)
		AND thread.starter = thread.nodeid
		ORDER BY thread.nodeid ASC
		LIMIT " . $vbulletin->GPC['startat'] . ", " . $vbulletin->GPC['perpage'] . "
	");
	while ($thread = $vbulletin->db->fetch_array($threads))
	{
		$gotsome = true;
		$threadinfo = vB_Api::instanceInternal('node')->getNode($thread['nodeid']);
		if (!$threadinfo)
		{
			$finishat++;
			continue;
		}

		$keywords = fetch_keywords_list($threadinfo, $thread['firstpost']);
		
		vB::getDbAssertor()->assertQuery('node', array(
			vB_dB_Query::TYPE_KEY =>vB_dB_Query::QUERY_UPDATE,
			'keywords' => $keywords,
			vB_dB_Query::CONDITIONS_KEY => array(
				'nodeid' => intval($thread['nodeid']),
			)
		));

		echo construct_phrase($vbphrase['processing_x'], $thread['threadid'])."<br />\n";
		vbflush();
	}

	if ($gotsome)
	{
		$args = array();
		parse_str(vB::getCurrentSession()->get('sessionurl'),$args);
		$args['do'] = 'addmissingkeywords';
		$args['startat'] = $finishat;
		$args['pp'] = $vbulletin->GPC['perpage'];
		print_cp_redirect2('misc', $args);
		echo "<p><a href=\"misc.php?" . vB::getCurrentSession()->get('sessionurl') . "do=addmissingkeywords&amp;pp=" . $vbulletin->GPC['perpage'] . "&amp;startat=$finishat\">" . $vbphrase['click_here_to_continue_processing'] . "</a></p>";
	}
	else
	{
		print_stop_message2('added_missing_keywords_successfully', 'misc');
	}

}

// ###################### Start build statistics #######################
if ($_REQUEST['do'] == 'buildstats')
{
	$timestamp =& $vbulletin->GPC['startat'];
	$vbulletin->GPC['perpage'] = 10 * 86400;

	if (empty($timestamp))
	{
		// this is the first page of a stat rebuild
		// so let's clear out the old stats
		$vbulletin->db->query_write("TRUNCATE TABLE " . TABLE_PREFIX . "stats");

		// and select a suitable start time
		$timestamp = $vbulletin->db->query_first("SELECT MIN(joindate) AS start FROM " . TABLE_PREFIX . "user WHERE joindate > 0");
		if ($timestamp['start'] == 0 OR $timestamp['start'] < 915166800)
		{ // no value found or its before 1999 lets just make it the year 2000
			$timestamp['start'] = 946684800;
		}
		$month = date('n', $timestamp['start']);
		$day = date('j', $timestamp['start']);
		$year = date('Y', $timestamp['start']);

		$timestamp = mktime(0, 0, 0, $month, $day, $year);
	}

	if ($timestamp + $vbulletin->GPC['perpage'] >= TIMENOW)
	{
		$endstamp = TIMENOW;
	}
	else
	{
		$endstamp = $timestamp + $vbulletin->GPC['perpage'];
	}
	
	$topChannels = vB_Api::instanceInternal('content_channel')->fetchTopLevelChannelIds();
	$forumChannel = $topChannels['forum'];
	$channelContentType = vB_Types::instance()->getContentTypeID('vBForum_Channel');

	while ($timestamp <= $endstamp)
	{
		// new users
		$newusers = $vbulletin->db->query_first('SELECT COUNT(userid) AS total FROM ' . TABLE_PREFIX . 'user WHERE joindate >= ' . $timestamp . ' AND joindate < ' . ($timestamp + 86400));

		// new threads
		$newthreads = $vbulletin->db->query_first('SELECT COUNT(nodeid) AS total FROM ' . TABLE_PREFIX . 'node AS node INNER JOIN ' . TABLE_PREFIX . 'closure AS cl ON cl.parent = ' . $forumChannel . ' WHERE node.nodeid = node.starter AND cl.child = node.nodeid AND node.publishdate >= ' . $timestamp . ' AND node.publishdate < ' . ($timestamp + 86400));

		// new posts
		$newposts = $vbulletin->db->query_first('SELECT COUNT(nodeid) AS total FROM ' . TABLE_PREFIX . 'node AS node INNER JOIN vb_closure as cl ON cl.parent = ' . $forumChannel . ' WHERE node.nodeid != node.starter AND cl.child = node.nodeid AND node.contenttypeid != ' . $channelContentType . ' AND node.publishdate >= ' . $timestamp . ' AND node.publishdate < ' . ($timestamp + 86400));

		// active users
		$activeusers = $vbulletin->db->query_first('SELECT COUNT(userid) AS total FROM ' . TABLE_PREFIX . 'user WHERE lastactivity >= ' . $timestamp . ' AND lastactivity < ' . ($timestamp + 86400));

		$inserts[] = "($timestamp, $newusers[total], $newthreads[total], $newposts[total], $activeusers[total])";

		echo $vbphrase['done'] . " $timestamp <br />\n";
		vbflush();

		$timestamp += 3600 * 24;

	}

	if (!empty($inserts))
	{
		/*insert query*/
		$vbulletin->db->query_write("
			REPLACE INTO " . TABLE_PREFIX . "stats
				(dateline, nuser, nthread, npost, ausers)
			VALUES
				" . implode(',', $inserts) . "
		");
		$args = array();
		parse_str(vB::getCurrentSession()->get('sessionurl'),$args);
		$args['do'] = 'buildstats';
		$args['startat'] = $timestamp;
		print_cp_redirect2('misc', $args);

	}
	else
	{
		print_stop_message2('rebuilt_statistics_successfully', 'misc');
	}
}

// ###################### Start remove dupe threads #######################
if ($_REQUEST['do'] == 'removeorphanthreads')
{
	if (empty($vbulletin->GPC['perpage']))
	{
		$vbulletin->GPC['perpage'] = 50;
	}

	$result = fetch_adminutil_text('orphanthread');

	if ($result == 'done')
	{
		build_adminutil_text('orphanthread');
		print_stop_message2('deleted_orphan_threads_successfully_gmaintenance', 'misc');
	}
	else if ($result != '')
	{
		$threadarray = unserialize($result);
	}
	else
	{
		$excludeTypes = array(
			vB_Types::instance()->getContentTypeID('vBForum_Channel'),
			vB_Types::instance()->getContentTypeID('vBForum_Photo'),
			vB_Types::instance()->getContentTypeID('vBForum_Attach'),
			vB_Types::instance()->getContentTypeID('vBForum_PrivateMessage')
		);
		$channelContentType = vB_Types::instance()->getContentTypeID('vBForum_Channel');

		$threadarray = array();
		// Fetch IDS
		$threads = $vbulletin->db->query_read("
			SELECT thread.nodeid
			FROM " . TABLE_PREFIX . "node AS thread
			LEFT JOIN " . TABLE_PREFIX . "node AS forum ON forum.nodeid = thread.parentid AND forum.contenttypeid = $channelContentType
			WHERE forum.nodeid IS NULL
			AND thread.contenttypeid NOT IN (" . implode(',', $excludeTypes) . ")
			AND thread.starter = thread.nodeid
		");
		while ($thread = $vbulletin->db->fetch_array($threads))
		{
			$threadarray[] = $thread['threadid'];
			$count++;
		}
	}

	echo '<p>' . $vbphrase['removing_orphan_threads'] . '</p>';

	while ($threadid = array_pop($threadarray) AND $count < $vbulletin->GPC['perpage'])
	{
		vB_Api::instanceInternal('node')->deleteNodes($threadid);
		echo construct_phrase($vbphrase['processing_x'], $threadid)."<br />\n";
		vbflush();
		$count++;
	}

	if (empty($threadarray))
	{
		build_adminutil_text('orphanthread', 'done');
	}
	else
	{
		build_adminutil_text('orphanthread', serialize($threadarray));
	}

	$args = array();
	parse_str(vB::getCurrentSession()->get('sessionurl'),$args);
	$args['do'] = 'removeorphanthreads';
	$args['pp'] = $vbulletin->GPC['perpage'];
	print_cp_redirect2('misc', $args);

	echo "<p><a href=\"misc.php?" . vB::getCurrentSession()->get('sessionurl') . "do=removeorphanthreads&amp;pp=" . $vbulletin->GPC['perpage'] . "\">" . $vbphrase['click_here_to_continue_processing'] . "</a></p>";

}

// ###################### Start remove posts #######################
if ($_REQUEST['do'] == 'removeorphanposts')
{
	if (empty($vbulletin->GPC['perpage']))
	{
		$vbulletin->GPC['perpage'] = 50;
	}

	$finishat = $vbulletin->GPC['startat'] + $vbulletin->GPC['perpage'];
	
	$excludeTypes = array(
		vB_Types::instance()->getContentTypeID('vBForum_Channel'),
		vB_Types::instance()->getContentTypeID('vBForum_Photo'),
		vB_Types::instance()->getContentTypeID('vBForum_Attach'),
		vB_Types::instance()->getContentTypeID('vBForum_PrivateMessage')
	);

	$posts = $vbulletin->db->query_read("
		SELECT post.nodeid
		FROM " . TABLE_PREFIX . "node AS post
		LEFT JOIN " . TABLE_PREFIX . "node AS thread ON post.parentid = thread.nodeid AND thread.nodeid = thread.starter
		WHERE thread.nodeid IS NULL
		AND post.contenttypeid NOT IN (" . implode(',', $excludeTypes) . ")
		LIMIT " . $vbulletin->GPC['startat'] . ", " . $vbulletin->GPC['perpage'] . "
	");
	while ($post = $vbulletin->db->fetch_array($posts))
	{
		vB_Api::instanceInternal('node')->deleteNodes($post['nodeid']);
		echo construct_phrase($vbphrase['processing_x'], $post['postid'])."<br />\n";
		vbflush();
		$gotsome = true;
	}

	if($gotsome)
	{
		$args = array();
		parse_str(vB::getCurrentSession()->get('sessionurl'),$args);
		$args['do'] = 'removeorphanposts';
		$args['startat'] = $finishat;
		$args['pp'] = $vbulletin->GPC['perpage'];
		print_cp_redirect2('misc', $args);
		echo "<p><a href=\"misc.php?" . vB::getCurrentSession()->get('sessionurl') . "do=removeorphanposts&amp;pp=" . $vbulletin->GPC['perpage'] . "&amp;startat=$finishat\">" . $vbphrase['click_here_to_continue_processing'] . "</a></p>";
	}
	else
	{
		print_stop_message2('deleted_orphan_posts_successfully', 'misc');
	}
}

// ###################### Anonymous Survey Code #######################
if ($_REQUEST['do'] == 'survey')
{
	// first we'd like extra phrase groups from the cphome
	fetch_phrase_group('cphome');

	/*
	All the functions are prefixed with @ to supress errors, this allows us to get feedback from hosts which have almost everything
	useful disabled
	*/

	// What operating system is the webserver running
	$os = @php_uname('s');

	// Using 32bit or 64bit
	$architecture = @php_uname('m');//php_uname('r') . ' ' . php_uname('v') . ' ' . //;

	// Webserver Signature
	$web_server = $_SERVER['SERVER_SOFTWARE'];

	// PHP Web Server Interface
	$sapi_name = @php_sapi_name();

	// If Apache is used, what sort of modules, mod_security?
	if (function_exists('apache_get_modules'))
	{
		$apache_modules = @apache_get_modules();
	}
	else
	{
		$apache_modules = null;
	}

	// Check to see if a recent version is being used
	$php = PHP_VERSION;

	// Check for common PHP Extensions
	$php_extensions = @get_loaded_extensions();

	// Various configuration options regarding PHP
	$php_safe_mode = SAFEMODE ? $vbphrase['on'] : $vbphrase['off'];
	$php_open_basedir = ((($bd = @ini_get('open_basedir')) AND $bd != '/') ? $vbphrase['on'] : $vbphrase['off']);
	$php_memory_limit = ((function_exists('memory_get_usage') AND ($limit = @ini_get('memory_limit'))) ? htmlspecialchars($limit) : $vbphrase['off']);

	// what version of MySQL
	$mysql = $vbulletin->db->query_first("SELECT VERSION() AS version");
	$mysql = $mysql['version'];

	// Post count
	$posts = $vbulletin->db->query_first("SELECT COUNT(*) AS total FROM " . TABLE_PREFIX . "post");
	$posts = $posts['total'];

	// User Count
	$users = $vbulletin->db->query_first("SELECT COUNT(*) AS total FROM " . TABLE_PREFIX . "user");
	$users = $users['total'];

	// Forum Count
	$forums = $vbulletin->db->query_first("SELECT COUNT(*) AS total FROM " . TABLE_PREFIX . "forum");
	$forums = $forums['total'];

	// Usergroup Count
	$usergroups = $vbulletin->db->query_first("SELECT COUNT(*) AS total FROM " . TABLE_PREFIX . "usergroup");
	$usergroups = $usergroups['total'];

	// First Forum Post
	$firstpost = $vbulletin->db->query_first("SELECT MIN(dateline) AS firstpost FROM " . TABLE_PREFIX . "post");
	$firstpost = $firstpost['firstpost'];

	// Last upgrade performed
	$lastupgrade = $vbulletin->db->query_first("SELECT MAX(dateline) AS lastdate FROM " . TABLE_PREFIX . "upgradelog");
	$lastupgrade = $lastupgrade['lastdate'];

	// percentage of users not using linear mode
	$nonlinear = $vbulletin->db->query_first("SELECT COUNT(*) AS total FROM " . TABLE_PREFIX . "user WHERE threadedmode <> 0");
	$nonlinear = number_format(100 * ($nonlinear['total'] / $users), 2, '.', '');

	// character sets in use within all languages
	$charsets_result = $vbulletin->db->query_read("SELECT DISTINCT charset AS charset FROM " . TABLE_PREFIX . "language");
	$charsets = array();
	while ($charset = $vbulletin->db->fetch_array($charsets_result))
	{
		$charset_name = trim(htmlspecialchars($charset['charset']));
		if ($charset_name != '')
		{
			$charsets["$charset_name"] = $charset_name;
		}
	}
	$vbulletin->db->free_result($charsets_result);

	?>
	<style type="text/css">
	.infotable td { font-size: smaller; }
	.infotable tr { vertical-align: top; }
	.hcell { font-weight: bold; white-space: nowrap; width: 200px; }
	</style>
	<form action="" method="post">
	<?php

	$apache_modules_html = '';
	if (is_array($apache_modules))
	{
		$apache_modules = array_map('htmlspecialchars', $apache_modules);

		foreach ($apache_modules AS $apache_module)
		{
			$apache_modules_html .= "<input type=\"hidden\" name=\"apache_module[]\" value=\"$apache_module\" />";
		}
	}

	$php_extensions_html = '';
	if (is_array($php_extensions))
	{
		$php_extensions = array_map('htmlspecialchars', $php_extensions);

		foreach ($php_extensions AS $php_extension)
		{
			$php_extensions_html .= "<input type=\"hidden\" name=\"php_extension[]\" value=\"$php_extension\" />";
		}
	}

	$charsets_html = '';
	if (is_array($charsets))
	{
		$charsets = array_map('htmlspecialchars', $charsets);

		foreach ($charsets AS $charset)
		{
			$charsets_html .= "<input type=\"hidden\" name=\"charset[]\" value=\"$charset\" />";
		}
	}

	print_table_start();
	print_table_header($vbphrase['anon_server_survey']);
	print_description_row($vbphrase['anon_server_survey_desc']);
	print_table_header('<img src="../' . $vbulletin->options['cleargifurl'] . '" width="1" height="1" alt="" />');
	print_description_row("
		<table cellpadding=\"0\" cellspacing=\"6\" border=\"0\" class=\"infotable\">
		<tr><td class=\"hcell\">$vbphrase[vbulletin_version_gmaintenance]</td><td>" . $vbulletin->options['templateversion'] . "</td></tr>
		<tr><td class=\"hcell\">$vbphrase[server_type]</td><td>$os</td></tr>
		<tr><td class=\"hcell\">$vbphrase[system_architecture]</td><td>$architecture</td></tr>
		<tr><td class=\"hcell\">$vbphrase[mysql_version]</td><td>$mysql</td></tr>
		<tr><td class=\"hcell\">$vbphrase[web_server]</td><td>$web_server</td></tr>
		<tr><td class=\"hcell\">SAPI</td><td>$sapi_name</td></tr>" . (is_array($apache_modules) ? "
		<tr><td class=\"hcell\">$vbphrase[apache_modules]</td><td>" . implode(', ', $apache_modules) . "</td></tr>" : '') . "
		<tr><td class=\"hcell\">PHP</td><td>$php</td></tr>
		<tr><td class=\"hcell\">$vbphrase[php_extensions]</td><td>" . implode(', ', $php_extensions) . "</td></tr>
		<tr><td class=\"hcell\">$vbphrase[php_memory_limit]</td><td>$php_memory_limit</td></tr>
		<tr><td class=\"hcell\">$vbphrase[php_safe_mode]</td><td>$php_safe_mode</td></tr>
		<tr><td class=\"hcell\">$vbphrase[php_openbase_dir]</td><td>$php_open_basedir</td></tr>
		<tr><td class=\"hcell\">$vbphrase[character_sets_usage]</td><td>" . implode(', ', $charsets) . "</td></tr>
		</table>");

	print_table_header($vbphrase['optional_info']);

	print_description_row("
		<table cellpadding=\"0\" cellspacing=\"6\" border=\"0\" class=\"infotable\">
		<tr><td class=\"hcell\">$vbphrase[total_posts_gmaintenance]</td><td>
			<label for=\"cb_posts\"><input type=\"checkbox\" name=\"posts\" id=\"cb_posts\" value=\"$posts\" checked=\"checked\" />" . vb_number_format($posts) . "</label></td></tr>
		<tr><td class=\"hcell\">$vbphrase[total_users]</td><td>
			<label for=\"cb_users\"><input type=\"checkbox\" name=\"users\" id=\"cb_users\" value=\"$users\" checked=\"checked\" />" . vb_number_format($users) . "</label></td></tr>
		<tr><td class=\"hcell\">$vbphrase[threaded_mode_usage]</td><td>
			<label for=\"cb_nonlinear\"><input type=\"checkbox\" name=\"nonlinear\" id=\"cb_nonlinear\" value=\"$nonlinear\" checked=\"checked\" />" . vb_number_format($nonlinear, 2) . "%</label></td></tr>
		<tr><td class=\"hcell\">$vbphrase[total_forums]</td><td>
			<label for=\"cb_forums\"><input type=\"checkbox\" name=\"forums\" id=\"cb_forums\" value=\"$forums\" checked=\"checked\" />" . vb_number_format($forums) . "</label></td></tr>
		<tr><td class=\"hcell\">$vbphrase[total_usergroups]</td><td>
			<label for=\"cb_usergroups\"><input type=\"checkbox\" name=\"usergroups\" id=\"cb_usergroups\" value=\"$usergroups\" checked=\"checked\" />" . vb_number_format($usergroups) . "</label></td></tr>
		" . ($firstpost > 0 ? "<tr><td class=\"hcell\">$vbphrase[first_post_date]</td><td>
			<label for=\"cb_firstpost\"><input type=\"checkbox\" name=\"firstpost\" id=\"cb_firstpost\" value=\"$firstpost\" checked=\"checked\" />" . vbdate($vbulletin->options['dateformat'], $firstpost) . "</label></td></tr>" : '') .
		 	($lastupgrade > 0 ? "<tr><td class=\"hcell\">$vbphrase[last_upgrade_date]</td><td>
			<label for=\"cb_lastupgrade\"><input type=\"checkbox\" name=\"lastupgrade\" id=\"cb_lastupgrade\" value=\"$lastupgrade\" checked=\"checked\" />" . vbdate($vbulletin->options['dateformat'], $lastupgrade) . "</label></td></tr>" : '') . "
		</table>
		<input type=\"hidden\" name=\"vbversion\" value=\"" . SIMPLE_VERSION . "\" />
		<input type=\"hidden\" name=\"os\" value=\"$os\" />
		<input type=\"hidden\" name=\"architecture\" value=\"$architecture\" />
		<input type=\"hidden\" name=\"mysql\" value=\"$mysql\" />
		<input type=\"hidden\" name=\"web_server\" value=\"$web_server\" />
		<input type=\"hidden\" name=\"sapi_name\" value=\"$sapi_name\" />
			$apache_modules_html
		<input type=\"hidden\" name=\"php\" value=\"$php\" />
			$php_extensions_html
		<input type=\"hidden\" name=\"php_memory_limit\" value=\"$php_memory_limit\" />
		<input type=\"hidden\" name=\"php_safe_mode\" value=\"$php_safe_mode\" />
		<input type=\"hidden\" name=\"php_open_basedir\" value=\"$php_open_basedir\" />
			$charsets_html
	");
	print_submit_row($vbphrase['send_info'], '');
	print_table_footer();
}

// ###################### Start user choices #######################
if ($_REQUEST['do'] == 'chooser')
{
	print_form_header('misc', 'updateuser');
	print_table_header($vbphrase['update_user_titles'], 2, 0);
	print_input_row($vbphrase['number_of_users_to_process_per_cycle_gmaintenance'], 'perpage', 1000);
	print_submit_row($vbphrase['update_user_titles']);

	print_form_header('misc', 'updatethread');
	print_table_header($vbphrase['rebuild_thread_information'], 2, 0);
	print_input_row($vbphrase['number_of_threads_to_process_per_cycle'], 'perpage', 2000);
	print_submit_row($vbphrase['rebuild_thread_information']);

	print_form_header('misc', 'updateforum');
	print_table_header($vbphrase['rebuild_forum_information'], 2, 0);
	print_input_row($vbphrase['number_of_forums_to_process_per_cycle'], 'perpage', 100);
	print_submit_row($vbphrase['rebuild_forum_information']);

	print_form_header('misc', 'addmissingkeywords');
	print_table_header($vbphrase['add_missing_thread_keywords']);
	print_input_row($vbphrase['number_of_threads_to_process_per_cycle'], 'perpage', 1000);
	print_submit_row($vbphrase['add_keywords']);

	print_form_header('misc', 'lostusers');
	print_table_header($vbphrase['fix_broken_user_profiles']);
	print_description_row($vbphrase['finds_users_without_complete_entries']);
	print_submit_row($vbphrase['fix_broken_user_profiles'],NULL);

	print_form_header('misc', 'doindextypes');
	print_table_header($vbphrase['rebuild_search_index'], 2, 0);
	print_description_row(construct_phrase($vbphrase['note_reindexing_empty_indexes_x'], vB::getCurrentSession()->get('sessionurl')));

	//don't use array_merge, it will (incorrectly) assume that the keys are index values
	//instead of meaningful numeric keys and renumber them.
	$contenttypes = vB_Types::instance()->getSearchableContentTypes();
	$types = array ( 0 => $vbphrase['all']);
	foreach ($contenttypes as $key => $type) {
		$types[$key] = $type['class'];
	}
	print_select_row($vbphrase['search_content_type_to_index'], 'indextypes', $types);
	print_input_row($vbphrase['search_items_batch'], 'perpage', 250);
	print_input_row($vbphrase['search_start_item_id'], 'startat', 0);
	//print_input_row($vbphrase['search_items_to_process'], 'doprocess', 0);
	print_yes_no_row($vbphrase['include_automatic_javascript_redirect'], 'autoredirect', 1);
	print_description_row($vbphrase['note_server_intensive']);
	print_submit_row($vbphrase['rebuild_search_index']);
/*
	if ($vbulletin->options['cachemaxage'] > 0)
	{
		print_form_header('misc', 'buildpostcache');
		print_table_header($vbphrase['rebuild_post_cache']);
		print_input_row($vbphrase['number_of_posts_to_process_per_cycle'], 'perpage', 1000);
		print_submit_row($vbphrase['rebuild_post_cache']);
	}
*/
	print_form_header('misc', 'truncatesigcache');
	print_table_header($vbphrase['empty_signature_cache']);
	print_description_row($vbphrase['change_output_signatures_empty_cache']);
	print_submit_row($vbphrase['empty_signature_cache'],NULL);

	print_form_header('misc', 'buildstats');
	print_table_header($vbphrase['rebuild_statistics'], 2, 0);
	print_description_row($vbphrase['rebuild_statistics_warning']);
	print_submit_row($vbphrase['rebuild_statistics'],NULL);
/*
	print_form_header('misc', 'updatesimilar');
	print_table_header($vbphrase['rebuild_similar_threads']);
	print_description_row($vbphrase['note_rebuild_similar_thread_list']);
	print_input_row($vbphrase['number_of_threads_to_process_per_cycle'], 'perpage', 100);
	print_submit_row($vbphrase['rebuild_similar_threads']);
*/
	print_form_header('misc', 'removedupe');
	print_table_header($vbphrase['delete_duplicate_threads'], 2, 0);
	print_description_row($vbphrase['note_duplicate_threads_have_same']);
	print_input_row($vbphrase['number_of_threads_to_process_per_cycle'], 'perpage', 500);
	print_submit_row($vbphrase['delete_duplicate_threads']);

	print_form_header('misc', 'rebuildthumbs');
	print_table_header($vbphrase['rebuild_attachment_thumbnails'], 2, 0);
	print_description_row($vbphrase['function_rebuilds_thumbnails']);
	print_input_row($vbphrase['number_of_attachments_to_process_per_cycle_gmaintenance'], 'perpage', 25);
	$quality = intval($vbulletin->options['thumbquality']);
	if ($quality <= 0 OR $quality > 100)
	{
		$quality = 75;
	}
	print_input_row($vbphrase['thumbnail_quality_gmaintenance'], 'quality', $quality);
	print_yes_no_row($vbphrase['include_automatic_javascript_redirect'], 'autoredirect', 1);
	print_submit_row($vbphrase['rebuild_attachment_thumbnails']);

	print_form_header('misc', 'rebuildavatars');
	print_table_header($vbphrase['rebuild_custom_avatar_thumbnails'], 2, 0);
	#print_description_row($vbphrase['function_rebuilds_avatars']);
	print_input_row($vbphrase['number_of_avatars_to_process_per_cycle'], 'perpage', 25);
	print_yes_no_row($vbphrase['include_automatic_javascript_redirect'], 'autoredirect', 1);
	print_submit_row($vbphrase['rebuild_custom_avatar_thumbnails']);

	print_form_header('misc', 'rebuildadminavatars');
	print_table_header($vbphrase['rebuild_avatar_thumbnails'], 2, 0);
	#print_description_row($vbphrase['function_rebuilds_avatars']);
	print_input_row($vbphrase['number_of_avatars_to_process_per_cycle'], 'perpage', 25);
	print_yes_no_row($vbphrase['include_automatic_javascript_redirect'], 'autoredirect', 1);
	print_submit_row($vbphrase['rebuild_avatar_thumbnails']);
/*
	print_form_header('misc', 'rebuildsgicons');
	print_table_header($vbphrase['rebuild_sgicon_thumbnails'], 2, 0);
	print_input_row($vbphrase['number_of_icons_to_process_per_cycle'], 'perpage', 25);
	$quality = intval($vbulletin->options['thumbquality']);
	if ($quality <= 0 OR $quality > 100)
	{
		$quality = 75;
	}
	print_input_row($vbphrase['thumbnail_quality_gmaintenance'], 'quality', $quality);
	print_yes_no_row($vbphrase['include_automatic_javascript_redirect'], 'autoredirect', 1);
	print_submit_row($vbphrase['rebuild_sgicon_thumbnails']);
*/
/*
	print_form_header('misc', 'rebuildalbumupdates');
	print_table_header($vbphrase['rebuild_recently_updated_albums_list'], 1, 0);
	print_description_row($vbphrase['rebuild_recently_updated_albums_description']);
	print_submit_row($vbphrase['rebuild_album_updates'],NULL);
*/
	print_form_header('misc', 'rebuildreputation');
	print_table_header($vbphrase['rebuild_user_reputation'], 2, 0);
	print_description_row($vbphrase['function_rebuilds_reputation']);
	print_input_row($vbphrase['reputation_base'], 'reputation_base', $vbulletin->options['reputationdefault']);
	print_submit_row($vbphrase['rebuild_user_reputation']);

	print_form_header('misc', 'updateusernames');
	print_table_header($vbphrase['update_usernames']);
	print_input_row($vbphrase['number_of_users_to_process_per_cycle_gmaintenance'], 'perpage', 1000);
	print_submit_row($vbphrase['update_usernames']);

	print_form_header('misc', 'updateposts');
	print_table_header($vbphrase['update_post_counts'], 2, 0);
	print_description_row($vbphrase['recalculate_users_post_counts_warning']);
	print_input_row($vbphrase['number_of_users_to_process_per_cycle_gmaintenance'], 'perpage', 1000);
	print_submit_row($vbphrase['update_post_counts']);

	print_form_header('misc', 'rebuildstyles');
	print_table_header($vbphrase['rebuild_styles'], 2, 0, 'style');
	print_description_row($vbphrase['function_allows_rebuild_all_style_info']);
	print_yes_no_row($vbphrase['check_styles_no_parent'], 'install', 1);
	print_yes_no_row($vbphrase['renumber_all_templates_from_one'], 'renumber', 0);
	print_submit_row($vbphrase['rebuild_styles'], 0);

	build_adminutil_text('orphanthread');
	print_form_header('misc', 'removeorphanthreads');
	print_table_header($vbphrase['remove_orphan_threads']);
	print_description_row($vbphrase['function_removes_orphan_threads']);
	print_input_row($vbphrase['number_of_threads_to_process_per_cycle'], 'perpage', 50);
	print_submit_row($vbphrase['remove_orphan_threads']);

	print_form_header('misc', 'removeorphanposts');
	print_table_header($vbphrase['remove_orphan_posts']);
	print_description_row($vbphrase['function_removes_orphan_posts']);
	print_input_row($vbphrase['number_of_posts_to_process_per_cycle'], 'perpage', 50);
	print_submit_row($vbphrase['remove_orphan_posts']);
}

// Legacy Hook 'admin_maintenance' Removed //

print_cp_footer();

/*======================================================================*\
|| ####################################################################
|| # 
|| # CVS: $RCSfile$ - $Revision: 66517 $
|| ####################################################################
\*======================================================================*/
