<?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);
// if (!is_object($vbulletin->db))
// {
// 	exit;
// }

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

// SELECT announcements that are active, will be active in the future or were active in the last ten days
$timenow = vB::getRequest()->getTimeNow();
$anns = vB::getDbAssertor()->getRows('announcement',
		array(vB_dB_Query::CONDITIONS_KEY=> array(
				array('field'=>'enddate', 'value' => $timenow -  864000, vB_Db_Query::OPERATOR_KEY => vB_Db_Query::OPERATOR_LTE)
		)),
		false,
		'announcementid'
);

// Delete all read markers for announcements expired > 10 days
if (!empty($anns))
{
	vB::getDbAssertor()->delete('announcementread',
		array(
				array('field'=>'announcementid', 'value' => array_keys($anns), vB_Db_Query::OPERATOR_KEY => vB_Db_Query::OPERATOR_NE)
		)
	);
}

if (vB::getDatastore()->getOption('tagcloud_searchhistory'))
{
	vB::getDbAssertor()->delete('tagsearch', array(
				array(
						'field'=>'dateline',
						'value' => $timenow - (vB::getDatastore()->getOption('tagcloud_searchhistory') * 60 * 60 * 24),
						vB_Db_Query::OPERATOR_KEY => vB_Db_Query::OPERATOR_LT
				)
		)
	);
}
$profilemaxvisitors = vB::getDatastore()->getOption('profilemaxvisitors');
// ensure this setting makes sense
if ($profilemaxvisitors < 2)
{
	$profilemaxvisitors = 2;
}

// remove profile visits beyond the first $vbulletin->options['profilemaxvisitors']
$rebuild_db = vB::getDbAssertor()->assertQuery('removeProfileVisits', array('profilemaxvisitors' => $profilemaxvisitors));

foreach ($rebuild_db as $user)
{
	$entry = vB::getDbAssertor()->getRow(
			'profilevisitor',
			array(
					'userid' => $user['userid'],
					'visible' => 1,
					vB_dB_Query::PARAM_LIMITSTART => $profilemaxvisitors, vB_dB_Query::PARAM_LIMIT => 1
				),
			array('field' => 'dateline', 'direction' => vB_dB_Query::SORT_DESC));

	if ($entry)
	{
		vB::getDbAssertor()->delete('profilevisitor',
				array(
						array('field'=>'userid', 'value' => $entry['userid'], vB_Db_Query::OPERATOR_KEY => vB_Db_Query::OPERATOR_EQ),
						array('field'=>'visible', 'value' => array(0,1), vB_Db_Query::OPERATOR_KEY => vB_Db_Query::OPERATOR_EQ),
						array('field'=>'dateline', 'value' => $entry['dateline'], vB_Db_Query::OPERATOR_KEY => vB_Db_Query::OPERATOR_LT)
				)
		);
	}
}

// Legacy Hook 'cron_script_cleanup_daily' Removed //

log_cron_action('', $nextitem, 1);

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