<?

error_reporting (E_ALL ^ E_NOTICE);

// read the database settings from the file
include_once('./settings.php');

// DB Connection Info read from settings file
Global $DBServer, $DBUser, $DBPass, $DBDatabase;
			
//establish a connection to database (server, database, password)
mysql_connect($DBServer, $DBUser, $DBPass) or die ("Could not connect to database");
	
//select database
mysql_select_db($DBDatabase) or die ("Could not select database");


$Settings['DBPrefix'] = $DBPrefix;



// ========================================================================================
// parse the query string
// ========================================================================================
$QS = $HTTP_SERVER_VARS['REQUEST_URI'];
//$QS = $HTTP_SERVER_VARS["HTTP_HOST"].$HTTP_SERVER_VARS["PHP_SELF"];

// remove any .html from the end of the url
//$QS = str_replace('.html', '', $QS);

// break the query string off from the rest of the address
list ($LocationPath, $QSData) = explode('?', $QS, 2);

// if there's something in the query string
if ($QSData != '') {

	$QSPieces = explode('/', $QSData);		

}

if ($QSPieces[0] != '') {

	$query = "select * from `".$Settings['DBPrefix']."core-Settings` where Name='EditorPreviousColors' limit 1";
	$result = mysql_query($query) or die (mysql_error());
	$row = mysql_fetch_array($result);


	if ($row['Value'] != '') {
	
		$ColorsArray = explode('$$', $row['Value']);
		
		// is this color already in the list?
		for ($i = 0; $i < 20; $i++) {
			if (strtolower($ColorsArray[$i]) == strtolower($QSPieces[0])) {
				
				$ColorAlreadyExists = true;
				$ColorExistsAt = $i;
				
			}
		}
		
		
		if ($ColorAlreadyExists == true) {
		
			// swap places of the previously most recent color and the new one that's
			// already somewhere else in the list, so it's at the start of the list
			
			$ColorsArray[20] = $ColorsArray[$ColorExistsAt];
			$ColorsArray[$ColorExistsAt] = 'swap';
			
			
			for ($i = 0; $i <= 20; $i++) {
	
				if ($ColorsArray[$i] != 'swap') {
					if ($i > 0) {
						$PreviousColorsString .= '$$';
					}
					$PreviousColorsString .= $ColorsArray[$i];
				}

			}
		
		}
		else {

			$ColorsArray[] = $QSPieces[0];
	
			for ($i = 1; $i <= 20; $i++) {
	
				if ($i > 1) {
					$PreviousColorsString .= '$$';
				}
				$PreviousColorsString .= $ColorsArray[$i];

			}
		
		}
		
	}
	else {
	
		$PreviousColorsString = '$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$'.$QSPieces[0];
	
	}	
	

	$query = "update `".$Settings['DBPrefix']."core-Settings` set Value='".$PreviousColorsString."' where Name='EditorPreviousColors' limit 1";
	$result = mysql_query($query) or die (mysql_error());

}


Header('Location: images/admin/spacer.gif');


?>
