";
$query = "UPDATE games SET system=0 WHERE system=".$system;
$result = mysqli_query($link, $query);
if (gettype($result) == "boolean" && $result)
{
echo "Files altered successfully! No further action required.
";
}
else
{
echo "Something has gone wrong, please manually alter all files with system '".$system."' to have value 0
";
}
}
else
{
echo "System could not be deleted. Try again later.
";
}
}
// If a source is being removed (can't remove default)
elseif ($remove == "source" && $source != "" && $source != "0")
{
$query = "DELETE FROM source where id=".$system;
$result = mysqli_query($link, $query);
if (gettype($result) == "boolean" && $result)
{
echo "Source deleted successfully! Altering all related files.
";
$query = "UPDATE games SET source=0 WHERE source=".$source;
$result = mysqli_query($link, $query);
if (gettype($result) == "boolean" && $result)
{
echo "Files altered successfully! No further action required.
";
}
else
{
echo "Something has gone wrong, please manually alter all files with source '".$source."' to have value 0
";
}
}
else
{
echo "System could not be deleted. Try again later.
";
}
}
// If a game is being removed
elseif ($remove == "game" && $game != "")
{
$query = "DELETE FROM games
JOIN files
ON games.id=files.setid
JOIN checksums
ON files.id=checksums.file
WHERE games.id=".$game;
$result = mysqli_query($link, $query);
if (gettype($result) == "boolean" && $result)
{
echo "Game and dependent files deleted successfully!
";
}
else
{
echo "Could not delete game or dependent files. Please try again
";;
}
}
// If a file is being removed
elseif ($remove == "file" && $file != "")
{
$query = "DELETE FROM files
JOIN checksums
ON files.id=checksums.file
WHERE files.id=".$file;
$result = mysqli_query($link, $query);
if (gettype($result) == "boolean" && $result)
{
echo "File deleted successfully!
";
}
else
{
echo "Could not delete file. Please try again
";;
}
}
}
// Handle the POST cases. They do not have to be mutually exclusive
else
{
// If a system is being edited or added via POST
if ($system != "" && $manufacturer != "" && $systemname != "")
{
// If the system is new and being added
if ($system == "-1")
{
// Always check if this exact combination is already there. This might have been in error
$query = "SELECT * FROM systems WHERE manufacturer='".$manufacturer." AND system='".$systemname."'";
$result = mysqli_query($link, $query);
// If we find this system, tell the user and don't move forward
if (gettype($result) != "boolean" && mysqli_num_rows($result) > 0)
{
echo "This system has been found. No further action is required
";
}
// If the system is not found, add it
else
{
$query = "INSERT INTO systems (manufacturer, system) VALUES ('".$manufacturer."', '".$systemname."')";
$result = mysqli_query($link, $query);
if (gettype($result) == "boolean" && $result)
{
echo "The system '".$manufacturer." - ".$systemname."' has been added successfully!
";
}
else
{
echo "The system '".$manufacturer." - ".$systemname."' could not be added, try again later
";
}
}
}
// If the system is being edited
else
{
// Check if the system exists first
$query = "SELECT * FROM systems WHERE id=".$system;
$result = mysqli_query($link, $query);
// If we don't find the system, don't add it. This might have been in error
if (gettype($result) == "boolean" && !$result)
{
echo "The system with id '".$system."' could not be found, try again later
";
}
// If we find the system, update with the new information, if not a duplicate of another
else
{
$query = "SELECT * FROM systems WHERE manufacturer='".$manufacturer." AND system='".$systemname."'";
$result = mysqli_query($link, $query);
// If the system is found or unchanged, don't readd it
if (gettype($result) != "boolean" && mysqli_num_rows($result) > 0)
{
echo "This system has been found. No further action is required
";
}
// If the system really has changed or is not a duplicate, add it
else
{
$query = "UPDATE systems SET manufacturer='".$manufacturer."', system='".$systemname."' WHERE id=".$system;
$result = mysqli_query($link, $query);
if (gettype($result) == "boolean" && $result)
{
echo "The system '".$manufacturer." - ".$systemname."' has been updated successfully!
";
}
else
{
echo "The system '".$manufacturer." - ".$systemname."' could not be updated, try again later
";
}
}
}
}
}
// If a source is being edited or added via POST
if ($source != "" && $sourcename != "" && $url != "")
{
// If the source is new and being added
if ($source == "-1")
{
// Always check if this exact combination is already there. This might have been in error
$query = "SELECT * FROM sources WHERE name='".$sourcename."'";
$result = mysqli_query($link, $query);
// If we find this source, tell the user and don't move forward
if (gettype($result) != "boolean" && mysqli_num_rows($result) > 0)
{
echo "This source has been found. No further action is required
";
}
// If the source is not found, add it
else
{
$query = "INSERT INTO sources (name, url) VALUES ('".$sourcename."', '".$url."')";
$result = mysqli_query($link, $query);
if (gettype($result) == "boolean" && $result)
{
echo "The source '".$sourcename."' has been added successfully!
";
}
else
{
echo "The source '".$sourcename."' could not be added, try again later
";
}
}
}
// If the source is being edited
else
{
// Check if the source exists first
$query = "SELECT * FROM sources WHERE id=".$system;
$result = mysqli_query($link, $query);
// If we don't find the source, don't add it. This might have been in error
if (gettype($result) == "boolean" && !$result)
{
echo "The source with id '".$source."' could not be found, try again later
";
}
// If we find the source, update with the new information, if not a duplicate of another
else
{
$query = "SELECT * FROM sources WHERE name='".$sourcename."'";
$result = mysqli_query($link, $query);
// If the source is found or unchanged, don't readd it
if (gettype($result) != "boolean" && mysqli_num_rows($result) > 0)
{
echo "This source has been found. No further action is required
";
}
// If the source really has changed or is not a duplicate, add it
else
{
$query = "UPDATE sources SET name='".$sourcename."', url='".$url."' WHERE id=".$source;
$result = mysqli_query($link, $query);
if (gettype($result) == "boolean" && $result)
{
echo "The source '".$sourcename."' has been updated successfully!
";
}
else
{
echo "The source '".$sourcename."' could not be updated, try again later
";
}
}
}
}
}
// If a game is being edited or added via POST
if ($game != "" && $gamename != "" && $system != "" && $source != "")
{
// If the game is new and being added
if ($game == "-1")
{
// Always check if this exact combination is already there. This might have been in error
$query = "SELECT * FROM games WHERE name='".$gamename."' AND system=".$system." AND source=".$source;
$result = mysqli_query($link, $query);
// If we find this game, tell the user and don't move forward
if (gettype($result) != "boolean" && mysqli_num_rows($result) > 0)
{
echo "This game has been found. No further action is required
";
}
// If the game is not found, add it
else
{
$query = "INSERT INTO games (name, system, source) VALUES ('".$gamename."', ".$system.", ".$source.")";
$result = mysqli_query($link, $query);
if (gettype($result) == "boolean" && $result)
{
echo "The game '".$gamename."' has been added successfully!
";
}
else
{
echo "The game '".$gamename."' could not be added, try again later
";
}
}
}
// If the game is being edited
else
{
// Check if the game exists first
$query = "SELECT * FROM games WHERE id=".$game;
$result = mysqli_query($link, $query);
// If we don't find the game, don't add it. This might have been in error
if (gettype($result) == "boolean" && !$result)
{
echo "The game with id '".$game."' could not be found, try again later
";
}
// If we find the game, update with the new information, if not a duplicate of another
else
{
$query = "SELECT * FROM games WHERE name='".$gamename."' AND system=".$system." AND source=".$source;
$result = mysqli_query($link, $query);
// If the game is found elsewhere or unchanged, don't readd it
if (gettype($result) != "boolean" && mysqli_num_rows($result) > 0)
{
echo "This source has been found. No further action is required
";
}
// If the game really has changed or is not a duplicate, add it
else
{
$query = "UPDATE game SET name='".$gamename."', system=".$system.", source=".$source." WHERE id=".$game;
$result = mysqli_query($link, $query);
if (gettype($result) == "boolean" && $result)
{
echo "The game '".$gamename."' has been updated successfully!
";
}
else
{
echo "The game '".$gamename."' could not be updated, try again later
";
}
}
}
}
}
// If a file is being edited or added via POST
if ($file != "" && $filename != "" && $game != "" && $type != "" &&
(($type == "rom" && $size != "" && ($crc != "" || $md5 != "" || $sha1 != "")) ||
($type == "disk" && ($md5 != "" || $sha1 != ""))))
{
// If the file is new and being added
if ($file == "-1")
{
// Always check if this exact combination is already there. This might have been in error
$query = "SELECT * FROM files
JOIN checksums ON files.id=checksums.file
WHERE files.name='".$filename.
"' AND files.setid=".$game.
" AND files.type='".$type.
"' AND checksums.size=".$size.
"' AND checksums.crc='".$crc.
"' AND checksums.md5='".$md5.
"' AND checksums.sha1='".$sha1;
$result = mysqli_query($link, $query);
// If we find this file, tell the user and don't move forward
if (gettype($result) != "boolean" && mysqli_num_rows($result) > 0)
{
echo "This file has been found. No further action is required
";
}
// If the file is not found, add it
else
{
$query = "INSERT INTO files (name, game, type)
VALUES ('".$filename."', ".$game.", ".$type.")";
$result = mysqli_query($link, $query);
if (gettype($result) == "boolean" && $result)
{
echo "The file '".$filename."' has been added successfully! Adding checksums.
";
$file = mysqli_insert_id($link);
$query = "INSERT INTO checksums (game, size, crc, md5, sha1)
VALUES (".$game.", ".$size.", '".$crc."', '".$md5."', '".$sha1."')";
$result = mysqli_query($link, $query);
if (gettype($result) == "boolean" && $result)
{
echo "The checksums have been added!
";
}
else
{
echo "The checksums could not be added, please try again later
";
}
}
else
{
echo "The file '".$filename."' could not be added, try again later
";
}
}
}
// If the file is being edited
else
{
// Check if the file exists first
$query = "SELECT * FROM files WHERE id=".$file;
$result = mysqli_query($link, $query);
// If we don't find the file, don't add it. This might have been in error
if (gettype($result) == "boolean" && !$result)
{
echo "The game with id '".$file."' could not be found, try again later
";
}
// If we find the file, update with the new information, if not a duplicate of another
else
{
$query = "SELECT * FROM files
JOIN checksums ON files.id=checksums.file
WHERE files.name='".$filename.
"' AND files.setid=".$game.
" AND files.type='".$type.
"' AND checksums.size=".$size.
"' AND checksums.crc='".$crc.
"' AND checksums.md5='".$md5.
"' AND checksums.sha1='".$sha1;
$result = mysqli_query($link, $query);
// If the file is found elsewhere or unchanged, don't readd it
if (gettype($result) != "boolean" && mysqli_num_rows($result) > 0)
{
echo "This file has been found. No further action is required
";
}
// If the source really has changed or is not a duplicate, add it
else
{
$query = "UPDATE file
JOIN checksums ON files.id=checksums.file
SET files.name='".$filename.
"' AND files.setid=".$game.
" AND files.type='".$type.
"' AND checksums.size=".$size.
"' AND checksums.crc='".$crc.
"' AND checksums.md5='".$md5.
"' AND checksums.sha1='".$sha1;
$result = mysqli_query($link, $query);
if (gettype($result) == "boolean" && $result)
{
echo "The file '".$filename."' has been updated successfully!
";
}
else
{
echo "The file '".$filename."' could not be updated, try again later
";
}
}
}
}
}
}
// Assuming there are no relevent params (or processing POST is done), show the basic page
if ($system == "" && $source == "" && $game == "" && $file == "")
{
show_default($link);
}
// First and foremost, capture file edit mode. It's exclusive above all others.
elseif ($file != "")
{
// Retrieve the file info
$query = "SELECT files.id AS file,
files.name AS name,
files.type AS type,
checksums.size AS size,
checksums.crc AS crc,
checksums.md5 AS md5,
checksums.sha1 AS sha1
FROM files
JOIN checksums
ON files.id=checksums.file
WHERE files.id=".$file;
$result = mysqli_query($link, $query);
$rom = mysqli_fetch_assoc($result);
// Now output the editable information
echo "