<?PHP 

// do I need to do additional security checks to make sure the script is being called appropriately?

include("config.php");

if(is_logged_in($user)){
  $username = base64_decode($_SESSION['user']);
  if($username != "Slowman" && $username != "Rappstar" && $username != "Herbert") { 
    header('Location:https://slowtwitch.com/calendar/');
    exit();
  }
} else { 
  header('Location:https://slowtwitch.com/calendar/');
  exit(); 
}

// set the page title
$pagetitle = "Admin: Duplicate Check";

// set meta tags
$meta_keywords = "";
$meta_description = "";
?>

<? include("include_common_head.php"); ?>
<? include("include_common_content.php"); ?>

<!-- Start outer -->
<div id="outer">

<!-- BODY -->
<div class="content">
<!-- LEFT COLUMN -->

<!-- End left column -->
<!-- Content col -->

<h1 class="content-title"><strong>Checking For Duplicates&hellip;</strong></h1>
Duplicate check includes a name similarity check, points similarity check (within +/- 5% total), type (triathlon/duathlon) check, and date match check.<br />
<div class="indent">
<?
// select any races that we haven't cleared that are past due.
$sql = "SELECT uid, name, points, UNIX_TIMESTAMP(date) AS date, type FROM gforum_Triathlons WHERE date > '2009-01-01'";
			  
$results = mysql_query($sql) or die(mysql_error());

echo "Processing races&hellip;<br />";

$arr_Races = array("");
$arr_Duplicates = array("");
$match_count = 0;
$i = 0;
while($row = mysql_fetch_array($results)) {
	// build an array that we can loop through. We will need to then duplicate this
	$arr_Races[$i] = $row;
	$i++;
}

$arr_Races_Match = array("");
$arr_Races_Match = $arr_Races;

foreach ($arr_Races AS $race) {
    // DEBUG: which UIDs are we processing
    //echo ("RACE_UID: <a href=\"https://slowtwitch.com/calendar/individual.php?uid=".$race[uid]."\" target=\"_blank\">".$race[uid].". ".$race[name]."</a><br />");
    
    $j = 0;
	foreach ($arr_Races_Match AS $match) {
		if ($race[uid] != $match[uid]) {	
			similar_text($race[name], $match[name], $percent);
			if ($percent >= 90 && ($race[points]/$match[points]*100) >= 95 && ($race[points]/$match[points]*100) <= 105 && (date("Ymd", $race[date]) == date("Ymd", $match[date])) && (!in_array($match[uid], $arr_Duplicates)) && ($race[type] == $match[type])) {
				if ($j == 0) {
				   echo ("RACE_UID: <a href=\"https://slowtwitch.com/calendar/individual.php?uid=".$race[uid]."\" target=\"_blank\">".$race[uid].". ".$race[name]." [".$race['points']." Points]</a><br />");
				   $j = 1;
				   array_push($arr_Duplicates, $race[uid]);
				}
				// DEBUG: which USERUID are we processing
				echo ("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;POTENTIAL MATCH: <a href=\"https://slowtwitch.com/calendar/individual.php?uid=".$match[uid]."\" target=\"_blank\">".$match[uid].". ".$match[name]." [".$match['points']." Points] (".round($percent,0)."%)</a><br />");
				$match_count++;
			}
		}
    }
}

echo "Done processing races.<br />";
echo $match_count." potential duplicates found.";

//header('Location: https://slowtwitch.com/calendar/?tags_cleared='.$tags_cleared.'.php');

?>

</div>
<!-- End content col -->

</div>
<!-- End BODY -->
</div>
<!-- End Outer -->

<? include("include_common_footer.php"); ?>