diff --git a/bin/dat_parser.php b/bin/dat_parser.php deleted file mode 100644 index 92cd8485..00000000 --- a/bin/dat_parser.php +++ /dev/null @@ -1,167 +0,0 @@ -"; - // print_r($header); - // print_r($game_data); - // print_r($resources); - // echo ""; - - return array($header, $game_data, $resources, $dat_filepath); -} - -// Process command line args -if ($index = array_search("--upload", $argv)) { - foreach (array_slice($argv, $index + 1) as $filepath) { - if ($filepath == "--match") - continue; - - db_insert(parse_dat($filepath)); - } -} - -if (in_array("--match", $argv)) { - populate_matching_games(); -} - -?> - diff --git a/bin/schema.php b/bin/schema.php deleted file mode 100644 index 108f16dd..00000000 --- a/bin/schema.php +++ /dev/null @@ -1,246 +0,0 @@ -connect_errno) { - die("Connect failed: " . $conn->connect_error); -} - -// Create database -$sql = "CREATE DATABASE IF NOT EXISTS " . $dbname; -if ($conn->query($sql) === TRUE) { - echo "Database created successfully\n"; -} -else { - echo "Error creating database: " . $conn->error; - exit(); -} - -$conn->query("USE " . $dbname); - - -///////////////////////// CREATE TABLES ///////////////////////// - -// Create engine table -$table = "CREATE TABLE IF NOT EXISTS engine ( - id INT AUTO_INCREMENT PRIMARY KEY, - name VARCHAR(200), - engineid VARCHAR(100) NOT NULL -)"; - -if ($conn->query($table) === TRUE) { - echo "Table 'engine' created successfully\n"; -} -else { - echo "Error creating 'engine' table: " . $conn->error; -} - -// Create game table -$table = "CREATE TABLE IF NOT EXISTS game ( - id INT AUTO_INCREMENT PRIMARY KEY, - name VARCHAR(200), - engine INT NOT NULL, - gameid VARCHAR(100) NOT NULL, - extra VARCHAR(200), - platform VARCHAR(30), - language VARCHAR(10), - FOREIGN KEY (engine) REFERENCES engine(id) -)"; - -if ($conn->query($table) === TRUE) { - echo "Table 'game' created successfully\n"; -} -else { - echo "Error creating 'game' table: " . $conn->error; -} - -// Create fileset table -$table = "CREATE TABLE IF NOT EXISTS fileset ( - id INT AUTO_INCREMENT PRIMARY KEY, - game INT, - status VARCHAR(20), - src VARCHAR(20), - `key` VARCHAR(64), - `megakey` VARCHAR(64), - `delete` BOOLEAN DEFAULT FALSE NOT NULL, - `timestamp` TIMESTAMP NOT NULL, - detection_size INT, - FOREIGN KEY (game) REFERENCES game(id) -)"; - -if ($conn->query($table) === TRUE) { - echo "Table 'fileset' created successfully\n"; -} -else { - echo "Error creating 'fileset' table: " . $conn->error; -} - -// Create file table -$table = "CREATE TABLE IF NOT EXISTS file ( - id INT AUTO_INCREMENT PRIMARY KEY, - name VARCHAR(200) NOT NULL, - size BIGINT NOT NULL, - checksum VARCHAR(64) NOT NULL, - fileset INT NOT NULL, - detection BOOLEAN NOT NULL, - FOREIGN KEY (fileset) REFERENCES fileset(id) ON DELETE CASCADE -)"; - -if ($conn->query($table) === TRUE) { - echo "Table 'file' created successfully\n"; -} -else { - echo "Error creating 'file' table: " . $conn->error; -} - -// Create filechecksum table -$table = "CREATE TABLE IF NOT EXISTS filechecksum ( - id INT AUTO_INCREMENT PRIMARY KEY, - file INT NOT NULL, - checksize VARCHAR(10) NOT NULL, - checktype VARCHAR(10) NOT NULL, - checksum VARCHAR(64) NOT NULL, - FOREIGN KEY (file) REFERENCES file(id) ON DELETE CASCADE -)"; - -if ($conn->query($table) === TRUE) { - echo "Table 'filechecksum' created successfully\n"; -} -else { - echo "Error creating 'filechecksum' table: " . $conn->error; -} - -// Create queue table -$table = "CREATE TABLE IF NOT EXISTS queue ( - id INT AUTO_INCREMENT PRIMARY KEY, - time TIMESTAMP NOT NULL, - notes varchar(300), - fileset INT, - userid INT NOT NULL, - commit VARCHAR(64) NOT NULL, - FOREIGN KEY (fileset) REFERENCES fileset(id) -)"; - -if ($conn->query($table) === TRUE) { - echo "Table 'queue' created successfully\n"; -} -else { - echo "Error creating 'queue' table: " . $conn->error; -} - -// Create log table -$table = "CREATE TABLE IF NOT EXISTS log ( - id INT AUTO_INCREMENT PRIMARY KEY, - `timestamp` TIMESTAMP NOT NULL, - category VARCHAR(100) NOT NULL, - user VARCHAR(100) NOT NULL, - `text` varchar(300) -)"; - -if ($conn->query($table) === TRUE) { - echo "Table 'log' created successfully\n"; -} -else { - echo "Error creating 'log' table: " . $conn->error; -} - -// Create history table -$table = "CREATE TABLE IF NOT EXISTS history ( - id INT AUTO_INCREMENT PRIMARY KEY, - `timestamp` TIMESTAMP NOT NULL, - fileset INT NOT NULL, - oldfileset INT NOT NULL, - log INT -)"; - -if ($conn->query($table) === TRUE) { - echo "Table 'history' created successfully\n"; -} -else { - echo "Error creating 'history' table: " . $conn->error; -} - -// Create transactions table -$table = "CREATE TABLE IF NOT EXISTS transactions ( - id INT AUTO_INCREMENT PRIMARY KEY, - `transaction` INT NOT NULL, - fileset INT NOT NULL -)"; - -if ($conn->query($table) === TRUE) { - echo "Table 'transactions' created successfully\n"; -} -else { - echo "Error creating 'transactions' table: " . $conn->error; -} - - -///////////////////////// CREATE INDEX ///////////////////////// - -// Create indices for fast data retrieval -// PK and FK are automatically indexed in InnoDB, so they are not included -$index = "CREATE INDEX detection ON file (detection)"; - -if ($conn->query($index) === TRUE) { - echo "Created index for 'file.detection'\n"; -} -else { - echo "Error creating index for 'file.detection': " . $conn->error; -} - -$index = "CREATE INDEX checksum ON filechecksum (checksum)"; - -if ($conn->query($index) === TRUE) { - echo "Created index for 'filechecksum.checksum'\n"; -} -else { - echo "Error creating index for 'filechecksum.checksum': " . $conn->error; -} - -$index = "CREATE INDEX engineid ON engine (engineid)"; - -if ($conn->query($index) === TRUE) { - echo "Created index for 'engine.engineid'\n"; -} -else { - echo "Error creating index for 'engine.engineid': " . $conn->error; -} - -$index = "CREATE INDEX fileset_key ON fileset (`key`)"; - -if ($conn->query($index) === TRUE) { - echo "Created index for 'fileset.key'\n"; -} -else { - echo "Error creating index for 'fileset.key': " . $conn->error; -} - -$index = "CREATE INDEX status ON fileset (status)"; - -if ($conn->query($index) === TRUE) { - echo "Created index for 'fileset.status'\n"; -} -else { - echo "Error creating index for 'fileset.status': " . $conn->error; -} - -$index = "CREATE INDEX fileset ON history (fileset)"; - -if ($conn->query($index) === TRUE) { - echo "Created index for 'history.fileset'\n"; -} -else { - echo "Error creating index for 'history.fileset': " . $conn->error; -} - -$conn->close(); -?> - diff --git a/bin/seeds.php b/bin/seeds.php deleted file mode 100644 index 2c3d75f3..00000000 --- a/bin/seeds.php +++ /dev/null @@ -1,75 +0,0 @@ -connect_errno) { - die("Connect failed: " . $conn->connect_error); -} - -$conn->query("USE " . $dbname); - - -///////////////////////// INSERT VALUES ///////////////////////// - -$query = "INSERT INTO engine (name, engineid) -VALUES ('Drascula', '1')"; -$conn->query($query); -$conn->query("SET @engine_last = LAST_INSERT_ID()"); - -$query = "INSERT INTO game (name, engine, gameid) -VALUES ('Drascula: The Vampire Strikes Back', @engine_last, '1')"; -$conn->query($query); -$conn->query("SET @game_last = LAST_INSERT_ID()"); - -$query = "INSERT INTO file (name, size, checksum) -VALUES ('Packet.001', '32847563', 'fac946707f07d51696a02c00cc182078')"; -$conn->query($query); -$conn->query("SET @file_last = LAST_INSERT_ID()"); - -$query = "INSERT INTO fileset (game, file, status, `key`) -VALUES (@game_last, @file_last, 0, 'fac946707f07d51696a02c00cc182078')"; -$conn->query($query); -$conn->query("SET @fileset_last = LAST_INSERT_ID()"); - -// Checksize: 0 (full checksum) -$query = "INSERT INTO filechecksum (file, checksize, checktype, checksum) -VALUES (@file_last, '0', 'md5', 'fac946707f07d51696a02c00cc182078')"; -$conn->query($query); -$conn->query("SET @filechecksum_last = LAST_INSERT_ID()"); - -$query = "INSERT INTO fileset_detection (fileset, checksum) -VALUES (@fileset_last, @filechecksum_last)"; -$conn->query($query); - -// Checksize: 5000B -$query = "INSERT INTO filechecksum (file, checksize, checktype, checksum) -VALUES (@file_last, '5000', 'md5', 'c6a8697396e213a18472542d5f547cb4')"; -$conn->query($query); -$conn->query("SET @filechecksum_last = LAST_INSERT_ID()"); - -$query = "INSERT INTO fileset_detection (fileset, checksum) -VALUES (@fileset_last, @filechecksum_last)"; -$conn->query($query); - -// Checksize: 10000B -$query = "INSERT INTO filechecksum (file, checksize, checktype, checksum) -VALUES (@file_last, '10000', 'md5', '695f4152f02b8fa4c1374a0ed04cf996')"; -$conn->query($query); -$conn->query("SET @filechecksum_last = LAST_INSERT_ID()"); - -$query = "INSERT INTO fileset_detection (fileset, checksum) -VALUES (@fileset_last, @filechecksum_last)"; -$conn->query($query); - - -$conn->close(); -?> - diff --git a/endpoints/validate.php b/endpoints/validate.php deleted file mode 100644 index 1963a5ce..00000000 --- a/endpoints/validate.php +++ /dev/null @@ -1,166 +0,0 @@ - -1, - "success" => 0, - "empty" => 2, - "no_metadata" => 3, -); - -$json_string = file_get_contents('php://input'); -$json_object = json_decode($json_string); - -$ip = $_SERVER['REMOTE_ADDR']; -// Take only first 3 bytes, set 4th byte as '.X' -// FIXME: Assumes IPv4 -$ip = implode('.', array_slice(explode('.', $ip), 0, 3)) . '.X'; - -$game_metadata = array(); -foreach ($json_object as $key => $value) { - if ($key == 'files') - continue; - - $game_metadata[$key] = $value; -} - -$json_response = array( - 'error' => $error_codes['success'], - 'files' => array() -); - -if (count($game_metadata) == 0) { - if (count($json_object->files) == 0) { - $json_response['error'] = $error_codes['empty']; - unset($json_response['files']); - $json_response['status'] = 'empty_fileset'; - - - $json_response = json_encode($json_response); - echo $json_response; - return; - } - - $json_response['error'] = $error_codes['no_metadata']; - unset($json_response['files']); - $json_response['status'] = 'no_metadata'; - - $fileset_id = user_insert_fileset($json_object->files, $ip, $conn); - $json_response['fileset'] = $fileset_id; - - $json_response = json_encode($json_response); - echo $json_response; - return; -} - -// Find game(s) that fit the metadata -$query = "SELECT game.id FROM game -JOIN engine ON game.engine = engine.id -WHERE gameid = '{$game_metadata['gameid']}' -AND engineid = '{$game_metadata['engineid']}' -AND platform = '{$game_metadata['platform']}' -AND language = '{$game_metadata['language']}'"; -$games = $conn->query($query); - -if ($games->num_rows == 0) { - $json_response['error'] = $error_codes['unknown']; - unset($json_response['files']); - $json_response['status'] = 'unknown_variant'; - - $fileset_id = user_insert_fileset($json_object->files, $ip, $conn); - $json_response['fileset'] = $fileset_id; -} - -// Check if all files in the (first) fileset are present with user -while ($game = $games->fetch_array()) { - $fileset = $conn->query("SELECT file.id, name, size FROM file - JOIN fileset ON fileset.id = file.fileset - WHERE fileset.game = {$game['id']} AND - (status = 'fullmatch' OR status = 'partialmatch' OR status = 'detection')"); - - if ($fileset->num_rows == 0) - continue; - - // Convert checktype, checksize to checkcode - $fileset = $fileset->fetch_all(MYSQLI_ASSOC); - foreach (array_values($fileset) as $index => $file) { - $spec_checksum_res = $conn->query("SELECT checksum, checksize, checktype - FROM filechecksum WHERE file = {$file['id']}"); - - while ($spec_checksum = $spec_checksum_res->fetch_assoc()) { - $fileset[$index][$spec_checksum['checktype'] . '-' . $spec_checksum['checksize']] = $spec_checksum['checksum']; - } - } - - $file_object = $json_object->files; - - // Sort the filesets by filename - usort($file_object, function ($a, $b) { - return strcmp($a->name, $b->name); - }); - usort($fileset, function ($a, $b) { - return strcmp($a['name'], $b['name']); - }); - - for ($i = 0, $j = 0; $i < count($fileset) && $j < count($file_object); $i++, $j++) { - $status = 'ok'; - $db_file = $fileset[$i]; - $user_file = $file_object[$j]; - $filename = strtolower($user_file->name); - - if (strtolower($db_file['name']) != $filename) { - if (strtolower($db_file['name']) > $filename) { - $status = 'unknown_file'; - $i--; // Retain same db_file for next iteration - } - else { - $status = 'missing'; - $filename = $db_file['name']; - $j--; // Retain same user_file for next iteration - } - } - elseif ($db_file['size'] != $user_file->size && $status == 'ok') { - $status = 'size_mismatch'; - } - - if ($status == 'ok') { - foreach ($user_file->checksums as $checksum_data) { - foreach ($checksum_data as $key => $value) { - $user_checkcode = $checksum_data->type; - // If it's not the full checksum - if (strpos($user_checkcode, '-') !== false) - continue; - - $user_checksum = $checksum_data->checksum; - $user_checkcode .= '-0'; - - if (strcasecmp($db_file[$user_checkcode], $user_checksum) != 0) - $status = 'checksum_mismatch'; - - break; - } - } - } - - if ($status != 'ok') { - $json_response['error'] = 1; - - $fileset_id = user_insert_fileset($json_object->files, $ip, $conn); - $json_response['fileset'] = $fileset_id; - } - - array_push($json_response['files'], array('status' => $status, 'name' => $filename)); - } - - break; -} - -$json_response = json_encode($json_response); -echo $json_response; -?> - diff --git a/fileset.php b/fileset.php deleted file mode 100644 index 73a09e52..00000000 --- a/fileset.php +++ /dev/null @@ -1,215 +0,0 @@ -\n"; -echo "\n"; -echo "\n"; - - -$mysql_cred = json_decode(file_get_contents(__DIR__ . '/mysql_config.json'), true); -$servername = $mysql_cred["servername"]; -$username = $mysql_cred["username"]; -$password = $mysql_cred["password"]; -$dbname = $mysql_cred["dbname"]; - -// Create connection -mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); -$conn = new mysqli($servername, $username, $password); -$conn->set_charset('utf8mb4'); -$conn->autocommit(FALSE); - -// Check connection -if ($conn->connect_errno) { - die("Connect failed: " . $conn->connect_error); -} - -$conn->query("USE " . $dbname); - -$min_id = $conn->query("SELECT MIN(id) FROM fileset")->fetch_array()[0]; -if (!isset($_GET['id'])) { - $id = $min_id; -} -else { - $max_id = $conn->query("SELECT MAX(id) FROM fileset")->fetch_array()[0]; - $id = max($min_id, min($_GET['id'], $max_id)); - if ($conn->query("SELECT id FROM fileset WHERE id = {$id}")->num_rows == 0) - $id = $conn->query("SELECT fileset FROM history WHERE oldfileset = {$id}")->fetch_array()[0]; -} - -$history = $conn->query("SELECT `timestamp`, oldfileset, log -FROM history WHERE fileset = {$id} -ORDER BY `timestamp`"); - - -// Display fileset details -echo "

Fileset: {$id}

"; - -$result = $conn->query("SELECT * FROM fileset WHERE id = {$id}")->fetch_assoc(); - -echo "

Fileset details

"; -echo "\n"; -if ($result['game']) { - $temp = $conn->query("SELECT game.name as 'game name', engineid, gameid, extra, platform, language -FROM fileset JOIN game ON game.id = fileset.game JOIN engine ON engine.id = game.engine -WHERE fileset.id = {$id}"); - $result = array_merge($result, $temp->fetch_assoc()); -} -else { - unset($result['key']); - unset($result['status']); - unset($result['delete']); -} - -foreach (array_keys($result) as $column) { - if ($column == 'id' || $column == 'game') - continue; - - echo "\n"; -} - -echo "\n"; -foreach ($result as $column => $value) { - if ($column == 'id' || $column == 'game') - continue; - - echo ""; -} -echo "\n"; -echo "
{$column}
{$value}
\n"; - -echo "

Files in the fileset

"; -echo "
"; -// Preserve GET variables on form submit -foreach ($_GET as $k => $v) { - if ($k == 'widetable') - continue; - - $k = htmlspecialchars($k); - $v = htmlspecialchars($v); - echo ""; -} - -// Come up with a better solution to set widetable=true on button click -// Currently uses hidden text input -if (isset($_GET['widetable']) && $_GET['widetable'] == 'true') { - echo ""; - echo ""; -} -else { - echo ""; - echo ""; -} - -echo "
"; - -// Table -echo "\n"; - -$result = $conn->query("SELECT file.id, name, size, checksum, detection - FROM file WHERE fileset = {$id}")->fetch_all(MYSQLI_ASSOC); - -if (isset($_GET['widetable']) && $_GET['widetable'] == 'true') { - foreach (array_values($result) as $index => $file) { - $spec_checksum_res = $conn->query("SELECT checksum, checksize, checktype - FROM filechecksum WHERE file = {$file['id']}"); - - while ($spec_checksum = $spec_checksum_res->fetch_assoc()) { - // md5-0 is skipped since it is already shown as file.checksum - if ($spec_checksum['checksize'] == 0) - continue; - - $result[$index][$spec_checksum['checktype'] . '-' . $spec_checksum['checksize']] = $spec_checksum['checksum']; - } - } -} - -$counter = 1; -foreach ($result as $row) { - if ($counter == 1) { - echo "\n"; - } - } - - echo "\n"; - echo "\n"; - foreach ($row as $key => $value) { - if ($key == 'id') - continue; - - echo "\n"; - } - echo "\n"; - - $counter++; -} -echo "
\n"; // Numbering column - foreach (array_keys($row) as $index => $key) { - if ($key == 'id') - continue; - - echo "{$key}
{$counter}.{$value}
\n"; - -// Dev Actions -echo "

Developer Actions

"; -echo ""; -echo ""; - -if (isset($_POST['delete'])) { - $conn->query("UPDATE fileset SET `delete` = TRUE WHERE id = {$_POST['delete']}"); - $conn->commit(); -} -if (isset($_POST['match'])) { - match_and_merge_user_filesets($_POST['match']); - header("Location: {$filename}?id={$_POST['match']}"); -} - -echo ""; // Hidden - - -// Display history and logs -echo "

Fileset history

"; - -echo "\n"; -echo ""; -echo ""; -echo ""; -echo ""; - -$logs = $conn->query("SELECT `timestamp`, category, `text`, id FROM log -WHERE `text` REGEXP 'Fileset:{$id}' -ORDER BY `timestamp` DESC, id DESC"); - -while ($row = $logs->fetch_assoc()) { - echo "\n"; - echo "\n"; - echo "\n"; - echo "\n"; - echo "\n"; - echo "\n"; -} - -while ($history_row = $history->fetch_assoc()) { - $logs = $conn->query("SELECT `timestamp`, category, `text`, id FROM log - WHERE `text` REGEXP 'Fileset:{$history_row['oldfileset']}' - AND `category` NOT REGEXP 'merge' - ORDER BY `timestamp` DESC, id DESC"); - - while ($row = $logs->fetch_assoc()) { - echo "\n"; - echo "\n"; - echo "\n"; - echo "\n"; - echo "\n"; - echo "\n"; - } -} - -echo "
TimestampCategoryDescriptionLog ID
{$row['timestamp']}{$row['category']}{$row['text']}{$row['id']}
{$row['timestamp']}{$row['category']}{$row['text']}{$row['id']}
\n"; - -?> - diff --git a/games_list.php b/games_list.php deleted file mode 100644 index c6950aad..00000000 --- a/games_list.php +++ /dev/null @@ -1,31 +0,0 @@ - table -$filters = array( - "engineid" => "engine", - "gameid" => "game", - "extra" => "game", - "platform" => "game", - "language" => "game", - "name" => "game", - "status" => "fileset" -); - -$mapping = array( - 'engine.id' => 'game.engine', - 'game.id' => 'fileset.game', -); - -create_page($filename, 25, $records_table, $select_query, $order, $filters, $mapping); -?> - diff --git a/include/db_functions.php b/include/db_functions.php deleted file mode 100644 index 81f21ffb..00000000 --- a/include/db_functions.php +++ /dev/null @@ -1,595 +0,0 @@ -set_charset('utf8mb4'); - $conn->autocommit(FALSE); - - // Check connection - if ($conn->connect_errno) { - die("Connect failed: " . $conn->connect_error); - } - - $conn->query("USE " . $dbname); - - return $conn; -} - -/** - * Retrieves the checksum and checktype of a given type + checksum - * eg: md5-5000 t:12345... -> 5000, md5-t, 12345... - */ -function get_checksum_props($checkcode, $checksum) { - $checksize = 0; - $checktype = $checkcode; - - if (strpos($checkcode, '-') !== false) { - $exploded_checkcode = explode('-', $checkcode); - $last = array_pop($exploded_checkcode); - if ($last == '1M' || is_numeric($last)) - $checksize = $last; - - $checktype = implode('-', $exploded_checkcode); - } - - // Detection entries have checktypes as part of the checksum prefix - if (strpos($checksum, ':') !== false) { - $prefix = explode(':', $checksum)[0]; - $checktype .= "-" . $prefix; - - $checksum = explode(':', $checksum)[1]; - } - - return array($checksize, $checktype, $checksum); -} - -/** - * Routine for inserting a game into the database - inserting into engine and - * game tables - */ -function insert_game($engine_name, $engineid, $title, $gameid, $extra, $platform, $lang, $conn) { - // Set @engine_last if engine already present in table - $exists = false; - if ($res = $conn->query(sprintf("SELECT id FROM engine WHERE engineid = '%s'", $engineid))) { - if ($res->num_rows > 0) { - $exists = true; - $conn->query(sprintf("SET @engine_last = '%d'", $res->fetch_array()[0])); - } - } - - // Insert into table if not present - if (!$exists) { - $query = sprintf("INSERT INTO engine (name, engineid) - VALUES ('%s', '%s')", mysqli_real_escape_string($conn, $engine_name), $engineid); - $conn->query($query); - $conn->query("SET @engine_last = LAST_INSERT_ID()"); - } - - // Insert into game - $query = sprintf("INSERT INTO game (name, engine, gameid, extra, platform, language) - VALUES ('%s', @engine_last, '%s', '%s', '%s', '%s')", mysqli_real_escape_string($conn, $title), - $gameid, mysqli_real_escape_string($conn, $extra), $platform, $lang); - $conn->query($query); - $conn->query("SET @game_last = LAST_INSERT_ID()"); -} - -function insert_fileset($src, $detection, $key, $megakey, $transaction, $log_text, $conn, $ip = '') { - $status = $detection ? "detection" : $src; - $game = "NULL"; - $key = $key == "" ? "NULL" : "'{$key}'"; - $megakey = $megakey == "" ? "NULL" : "'{$megakey}'"; - - if ($detection) { - $status = "detection"; - $game = "@game_last"; - } - - // Check if key/megakey already exists, if so, skip insertion (no quotes on purpose) - if ($detection) - $existing_entry = $conn->query("SELECT id FROM fileset WHERE `key` = {$key}"); - else - $existing_entry = $conn->query("SELECT id FROM fileset WHERE megakey = {$megakey}"); - - if ($existing_entry->num_rows > 0) { - $existing_entry = $existing_entry->fetch_array()[0]; - $conn->query("SET @fileset_last = {$existing_entry}"); - - $category_text = "Uploaded from {$src}"; - $log_text = "Duplicate of Fileset:{$existing_entry}, {$log_text}"; - if ($src == 'user') - $log_text = "Duplicate of Fileset:{$existing_entry}, from user IP {$ip}, {$log_text}"; - - $user = 'cli:' . get_current_user(); - create_log(mysqli_real_escape_string($conn, $category_text), $user, mysqli_real_escape_string($conn, $log_text)); - - if (!$detection) - return false; - - $conn->query("UPDATE fileset SET `timestamp` = FROM_UNIXTIME(@fileset_time_last) - WHERE id = {$existing_entry}"); - $conn->query("UPDATE fileset SET status = 'detection' - WHERE id = {$existing_entry} AND status = 'obsolete'"); - $conn->query("DELETE FROM game WHERE id = @game_last"); - return false; - } - - // $game and $key should not be parsed as a mysql string, hence no quotes - $query = "INSERT INTO fileset (game, status, src, `key`, megakey, `timestamp`) - VALUES ({$game}, '{$status}', '{$src}', {$key}, {$megakey}, FROM_UNIXTIME(@fileset_time_last))"; - $conn->query($query); - $conn->query("SET @fileset_last = LAST_INSERT_ID()"); - - $category_text = "Uploaded from {$src}"; - $fileset_last = $conn->query("SELECT @fileset_last")->fetch_array()[0]; - $log_text = "Created Fileset:{$fileset_last}, {$log_text}"; - if ($src == 'user') - $log_text = "Created Fileset:{$fileset_last}, from user IP {$ip}, {$log_text}"; - - $user = 'cli:' . get_current_user(); - create_log(mysqli_real_escape_string($conn, $category_text), $user, mysqli_real_escape_string($conn, $log_text)); - $conn->query("INSERT INTO transactions (`transaction`, fileset) VALUES ({$transaction}, {$fileset_last})"); - - return true; -} - -/** - * Routine for inserting a file into the database, inserting into all - * required tables - * $file is an associated array (the contents of 'rom') - * If checksum of the given checktype doesn't exists, silently fails - */ -function insert_file($file, $detection, $src, $conn) { - // Find full md5, or else use first checksum value - $checksum = ""; - $checksize = 5000; - if (isset($file["md5"])) { - $checksum = $file["md5"]; - } - else { - foreach ($file as $key => $value) { - if (strpos($key, "md5") !== false) { - list($checksize, $checktype, $checksum) = get_checksum_props($key, $value); - break; - } - } - } - - $query = sprintf("INSERT INTO file (name, size, checksum, fileset, detection) - VALUES ('%s', '%s', '%s', @fileset_last, %d)", mysqli_real_escape_string($conn, $file["name"]), - $file["size"], $checksum, $detection); - $conn->query($query); - - if ($detection) - $conn->query("UPDATE fileset SET detection_size = {$checksize} WHERE id = @fileset_last AND detection_size IS NULL"); - $conn->query("SET @file_last = LAST_INSERT_ID()"); -} - -function insert_filechecksum($file, $checktype, $conn) { - if (!array_key_exists($checktype, $file)) - return; - - $checksum = $file[$checktype]; - list($checksize, $checktype, $checksum) = get_checksum_props($checktype, $checksum); - - $query = sprintf("INSERT INTO filechecksum (file, checksize, checktype, checksum) - VALUES (@file_last, '%s', '%s', '%s')", $checksize, $checktype, $checksum); - $conn->query($query); -} - -/** - * Delete filesets marked for deletion - */ -function delete_filesets($conn) { - $query = "DELETE FROM fileset WHERE `delete` == TRUE"; - $conn->query($query); -} - -/** - * Create an entry to the log table on each call of db_insert() or - * populate_matching_games() - */ -function create_log($category, $user, $text) { - $conn = db_connect(); - $conn->query(sprintf("INSERT INTO log (`timestamp`, category, user, `text`) - VALUES (FROM_UNIXTIME(%d), '%s', '%s', '%s')", time(), $category, $user, $text)); - $log_last = $conn->query("SELECT LAST_INSERT_ID()")->fetch_array()[0]; - - if (!$conn->commit()) - echo "Creating log failed\n"; - - return $log_last; -} - -/** - * Calculate `key` value as md5("name:title:...:engine:file1:size:md5:file2:...") - */ -function calc_key($fileset) { - $key_string = ""; - - foreach ($fileset as $key => $value) { - if ($key == 'engineid' || $key == 'gameid' || $key == 'rom') - continue; - - $key_string .= ':' . $value; - } - - $files = $fileset['rom']; - foreach ($files as $file) { - foreach ($file as $key => $value) { - $key_string .= ':' . $value; - } - } - - $key_string = trim($key_string, ':'); - return md5($key_string); -} - -/** - * Calculate `megakey` value as md5("file1:size:md5:file2:...") - */ -function calc_megakey($files) { - $key_string = ""; - foreach ($files as $file) { - foreach ($file as $key => $value) { - $key_string .= ':' . $value; - } - } - $key_string = trim($key_string, ':'); - return md5($key_string); -} - -/** - * Insert values from the associated array into the DB - * They will be inserted under gameid NULL as the game itself is unconfirmed - */ -function db_insert($data_arr) { - $header = $data_arr[0]; - $game_data = $data_arr[1]; - $resources = $data_arr[2]; - $filepath = $data_arr[3]; - - $conn = db_connect(); - - /** - * Author can be: - * scummvm -> Detection Entries - * scanner -> CLI scanner tool in python - * _anything else_ -> DAT file - */ - $author = $header["author"]; - $version = $header["version"]; - - /** - * status can be: - * detection -> Detection entries (source of truth) - * user -> Submitted by users via ScummVM, unmatched (Not used in the parser) - * scan -> Submitted by cli/scanner, unmatched - * dat -> Submitted by DAT, unmatched - * partialmatch -> Submitted by DAT, matched - * fullmatch -> Submitted by cli/scanner, matched - * obsolete -> Detection entries that are no longer part of the detection set - */ - $src = ""; - if ($author == "scan" || $author == "scummvm") - $src = $author; - else - $src = "dat"; - - $detection = ($src == "scummvm"); - $status = $detection ? "detection" : $src; - - // Set timestamp of fileset insertion - $conn->query(sprintf("SET @fileset_time_last = %d", time())); - - // Create start log entry - $transaction_id = $conn->query("SELECT MAX(`transaction`) FROM transactions")->fetch_array()[0] + 1; - - $category_text = "Uploaded from {$src}"; - $log_text = sprintf("Started loading DAT file, size %d, author '%s', version %s. - State '%s'. Transaction: %d", - $filepath, filesize($filepath), $author, $version, $status, $transaction_id); - - $user = 'cli:' . get_current_user(); - create_log(mysqli_real_escape_string($conn, $category_text), $user, mysqli_real_escape_string($conn, $log_text)); - - foreach ($game_data as $fileset) { - if ($detection) { - $engine_name = $fileset["engine"]; - $engineid = $fileset["sourcefile"]; - $gameid = $fileset["name"]; - $title = $fileset["title"]; - $extra = $fileset["extra"]; - $platform = $fileset["platform"]; - $lang = $fileset["language"]; - - insert_game($engine_name, $engineid, $title, $gameid, $extra, $platform, $lang, $conn); - } - elseif ($src == "dat") - if (isset($fileset['romof']) && isset($resources[$fileset['romof']])) - $fileset["rom"] = array_merge($fileset["rom"], $resources[$fileset["romof"]]["rom"]); - - $key = $detection ? calc_key($fileset) : ""; - $megakey = !$detection ? calc_megakey($fileset['rom']) : ""; - $log_text = sprintf("size %d, author '%s', version %s. - State '%s'.", - filesize($filepath), $author, $version, $status); - - if (insert_fileset($src, $detection, $key, $megakey, $transaction_id, $log_text, $conn)) { - foreach ($fileset["rom"] as $file) { - insert_file($file, $detection, $src, $conn); - foreach ($file as $key => $value) { - if ($key != "name" && $key != "size") - insert_filechecksum($file, $key, $conn); - } - } - } - } - - if ($detection) - $conn->query("UPDATE fileset SET status = 'obsolete' - WHERE `timestamp` != FROM_UNIXTIME(@fileset_time_last) - AND status = 'detection'"); - - $fileset_insertion_count = $conn->query("SELECT COUNT(fileset) from transactions WHERE `transaction` = {$transaction_id}")->fetch_array()[0]; - $category_text = "Uploaded from {$src}"; - $log_text = sprintf("Completed loading DAT file, filename '%s', size %d, author '%s', version %s. - State '%s'. Number of filesets: %d. Transaction: %d", - $filepath, filesize($filepath), $author, $version, $status, $fileset_insertion_count, $transaction_id); - - if (!$conn->commit()) - echo "Inserting failed\n"; - else { - $user = 'cli:' . get_current_user(); - create_log(mysqli_real_escape_string($conn, $category_text), $user, mysqli_real_escape_string($conn, $log_text)); - } -} - -/** - * Compare 2 dat filesets to find if they are equivalent or not - */ -function compare_filesets($id1, $id2, $conn) { - $fileset1 = $conn->query("SELECT name, size, checksum - FROM file WHERE fileset = '{$id1}'")->fetch_array(); - $fileset2 = $conn->query("SELECT name, size, checksum - FROM file WHERE fileset = '{$id2}'")->fetch_array(); - - // Sort filesets on checksum - usort($fileset1, function ($a, $b) { - return $a[2] <=> $b[2]; - }); - usort($fileset2, function ($a, $b) { - return $a[2] <=> $b[2]; - }); - - if (count($fileset1) != count($fileset2)) - return false; - - for ($i = 0; $i < count($fileset1); $i++) { - // If checksums do not match - if ($fileset1[2] != $fileset2[2]) - return false; - } - - return True; -} - -/** - * Return fileset statuses that can be merged with set of given status - * eg: scan and dat -> detection - * fullmatch -> partialmatch, detection - */ -function status_to_match($status) { - $order = array("detection", "dat", "scan", "partialmatch", "fullmatch", "user"); - return array_slice($order, 0, array_search($status, $order)); -} - -/** - * Detects games based on the file descriptions in $dat_arr - * Compares the files with those in the detection entries table - * $game_files consists of both the game ( ) and resources ( ) parts - */ -function find_matching_game($game_files) { - $matching_games = array(); // All matching games - $matching_filesets = array(); // All filesets containing one file from $game_files - $matches_count = 0; // Number of files with a matching detection entry - - $conn = db_connect(); - - foreach ($game_files as $file) { - $checksum = $file[1]; - - $query = "SELECT file.fileset as file_fileset - FROM filechecksum - JOIN file ON filechecksum.file = file.id - WHERE filechecksum.checksum = '{$checksum}' AND file.detection = TRUE"; - $records = $conn->query($query)->fetch_all(); - - // If file is not part of detection entries, skip it - if (count($records) == 0) - continue; - - $matches_count++; - foreach ($records as $record) - array_push($matching_filesets, $record[0]); - } // Check if there is a fileset_id that is present in all results - foreach (array_count_values($matching_filesets) as $key => $value) { - $count_files_in_fileset = $conn->query(sprintf("SELECT COUNT(file.id) FROM file - JOIN fileset ON file.fileset = fileset.id - WHERE fileset.id = '%s'", $key))->fetch_array()[0]; - - // We use < instead of != since one file may have more than one entry in the fileset - // We see this in Drascula English version, where one entry is duplicated - if ($value < $matches_count || $value < $count_files_in_fileset) - continue; - - $records = $conn->query(sprintf("SELECT engineid, game.id, gameid, platform, - language, `key`, src, fileset.id as fileset - FROM game - JOIN fileset ON fileset.game = game.id - JOIN engine ON engine.id = game.engine - WHERE fileset.id = '%s'", $key)); - - array_push($matching_games, $records->fetch_array()); - } - - if (count($matching_games) != 1) - return $matching_games; - - // Check the current fileset priority with that of the match - $records = $conn->query(sprintf("SELECT id FROM fileset, ({$query}) AS res - WHERE id = file_fileset AND - status IN ('%s')", implode("', '", status_to_match($game_files[3])))); - - // If priority order is correct - if ($records->num_rows != 0) - return $matching_games; - - if (compare_filesets($matching_games[0]['fileset'], $game_files[0][0], $conn)) { - $conn->query("UPDATE fileset SET `delete` = TRUE WHERE id = {$game_files[0][0]}"); - return array(); - } - - return $matching_games; -} - -/** - * Merge two filesets without duplicating files - * Used after matching an unconfirmed fileset with a detection entry - */ -function merge_filesets($detection_id, $dat_id) { - $conn = db_connect(); - - $detection_files = $conn->query(sprintf("SELECT DISTINCT(filechecksum.checksum), checksize, checktype - FROM filechecksum JOIN file on file.id = filechecksum.file - WHERE fileset = '%d'", $detection_id))->fetch_all(); - - foreach ($detection_files as $file) { - $checksum = $file[0]; - $checksize = $file[1]; - $checktype = $file[2]; - - // Delete original detection entry so newly matched fileset is the only fileset for game - $conn->query(sprintf("DELETE FROM file - WHERE checksum = '%s' AND fileset = %d LIMIT 1", $checksum, $detection_id)); - - // Mark files present in the detection entries - $conn->query(sprintf("UPDATE file - JOIN filechecksum ON filechecksum.file = file.id - SET detection = TRUE, - checksize = %d, - checktype = '%s' - WHERE fileset = '%d' AND filechecksum.checksum = '%s'", - $checksize, $checktype, $dat_id, $checksum)); - } - - // Add fileset pair to history ($dat_id is the new fileset for $detection_id) - $conn->query(sprintf("INSERT INTO history (`timestamp`, fileset, oldfileset) - VALUES (FROM_UNIXTIME(%d), %d, %d)", time(), $dat_id, $detection_id)); - $history_last = $conn->query("SELECT LAST_INSERT_ID()")->fetch_array()[0]; - - $conn->query("UPDATE history SET fileset = {$dat_id} WHERE fileset = {$detection_id}"); - - // Delete original fileset - $conn->query("DELETE FROM fileset WHERE id = {$detection_id}"); - - if (!$conn->commit()) - echo "Error merging filesets\n"; - - return $history_last; -} - -/** - * (Attempt to) match fileset that have fileset.game as NULL - * This will delete the original detection fileset and replace it with the newly - * matched fileset - */ -function populate_matching_games() { - $conn = db_connect(); - - // Getting unmatched filesets - $unmatched_filesets = array(); - - $unmatched_files = $conn->query("SELECT fileset.id, filechecksum.checksum, src, status - FROM fileset - JOIN file ON file.fileset = fileset.id - JOIN filechecksum ON file.id = filechecksum.file - WHERE fileset.game IS NULL AND status != 'user'"); - $unmatched_files = $unmatched_files->fetch_all(); - - // Splitting them into different filesets - for ($i = 0; $i < count($unmatched_files); $i++) { - $cur_fileset = $unmatched_files[$i][0]; - $temp = array(); - while ($i < count($unmatched_files) - 1 && $cur_fileset == $unmatched_files[$i][0]) { - array_push($temp, $unmatched_files[$i]); - $i++; - } - array_push($unmatched_filesets, $temp); - } - - foreach ($unmatched_filesets as $fileset) { - $matching_games = find_matching_game($fileset); - - if (count($matching_games) != 1) // If there is no match/non-unique match - continue; - - $matched_game = $matching_games[0]; - - // Update status depending on $matched_game["src"] (dat -> partialmatch, scan -> fullmatch) - $status = $fileset[0][2]; - if ($fileset[0][2] == "dat") - $status = "partialmatch"; - elseif ($fileset[0][2] == "scan") - $status = "fullmatch"; - - // Convert NULL values to string with value NULL for printing - $matched_game = array_map(function ($val) { - return (is_null($val)) ? "NULL" : $val; - }, $matched_game); - - $category_text = "Matched from {$fileset[0][2]}"; - $log_text = "Matched game {$matched_game['engineid']}: - {$matched_game['gameid']}-{$matched_game['platform']}-{$matched_game['language']} - variant {$matched_game['key']}. State {$status}. Fileset:{$fileset[0][0]}."; - - // Updating the fileset.game value to be $matched_game["id"] - $query = sprintf("UPDATE fileset - SET game = %d, status = '%s', `key` = '%s' - WHERE id = %d", $matched_game["id"], $status, $matched_game["key"], $fileset[0][0]); - - $history_last = merge_filesets($matched_game["fileset"], $fileset[0][0]); - - if ($conn->query($query)) { - $user = 'cli:' . get_current_user(); - - // Merge log - create_log("Fileset merge", $user, - mysqli_real_escape_string($conn, "Merged Fileset:{$matched_game['fileset']} and Fileset:{$fileset[0][0]}")); - - // Matching log - $log_last = create_log(mysqli_real_escape_string($conn, $category_text), $user, - mysqli_real_escape_string($conn, $log_text)); - - // Add log id to the history table - $conn->query("UPDATE history SET log = {$log_last} WHERE id = {$history_last}"); - } - - if (!$conn->commit()) - echo "Updating matched games failed\n"; - } -} - - -?> - diff --git a/include/pagination.php b/include/pagination.php deleted file mode 100644 index 7269137b..00000000 --- a/include/pagination.php +++ /dev/null @@ -1,255 +0,0 @@ -\n"; -echo "\n"; -echo "\n"; - -/** - * Return a string denoting which two columns link two tables - */ -function get_join_columns($table1, $table2, $mapping) { - foreach ($mapping as $primary => $foreign) { - $primary = explode('.', $primary); - $foreign = explode('.', $foreign); - if (($primary[0] == $table1 && $foreign[0] == $table2) || - ($primary[0] == $table2 && $foreign[0] == $table1)) - return "{$primary[0]}.{$primary[1]} = {$foreign[0]}.{$foreign[1]}"; - } - - echo "No primary-foreign key mapping provided. Filter is invalid"; -} - -function create_page($filename, $results_per_page, $records_table, $select_query, $order, $filters = array(), $mapping = array()) { - $mysql_cred = json_decode(file_get_contents(__DIR__ . '/../mysql_config.json'), true); - $servername = $mysql_cred["servername"]; - $username = $mysql_cred["username"]; - $password = $mysql_cred["password"]; - $dbname = $mysql_cred["dbname"]; - - // Create connection - mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); - $conn = new mysqli($servername, $username, $password); - $conn->set_charset('utf8mb4'); - $conn->autocommit(FALSE); - - // Check connection - if ($conn->connect_errno) { - die("Connect failed: " . $conn->connect_error); - } - - $conn->query("USE " . $dbname); - - // If there exist get variables that are for filtering - $_GET = array_filter($_GET); - if (isset($_GET['sort'])) { - $column = $_GET['sort']; - $column = explode('-', $column); - $order = "ORDER BY {$column[0]}"; - - if (strpos($_GET['sort'], 'desc') !== false) - $order .= " DESC"; - } - - if (array_diff(array_keys($_GET), array('page', 'sort'))) { - $condition = "WHERE "; - $tables = array(); - foreach ($_GET as $key => $value) { - if ($key == 'page' || $key == 'sort' || $value == '') - continue; - - array_push($tables, $filters[$key]); - $condition .= $condition != "WHERE " ? " AND {$filters[$key]}.{$key} REGEXP '{$value}'" : "{$filters[$key]}.{$key} REGEXP '{$value}'"; - } - if ($condition == "WHERE ") - $condition = ""; - - // If more than one table is to be searched - $from_query = $records_table; - if (count($tables) > 1 || $tables[0] != $records_table) - for ($i = 0; $i < count($tables); $i++) { - if ($tables[$i] == $records_table) - continue; - - $from_query .= sprintf(" JOIN %s ON %s", $tables[$i], get_join_columns($records_table, $tables[$i], $mapping)); - } - - $num_of_results = $conn->query( - "SELECT COUNT({$records_table}.id) FROM {$from_query} {$condition}")->fetch_array()[0]; - } - // If $records_table has a JOIN (multiple tables) - elseif (preg_match("/JOIN/", $records_table) !== false) { - $first_table = explode(" ", $records_table)[0]; - $num_of_results = $conn->query("SELECT COUNT({$first_table}.id) FROM {$records_table}")->fetch_array()[0]; - } - else { - $num_of_results = $conn->query("SELECT COUNT(id) FROM {$records_table}")->fetch_array()[0]; - } - $num_of_pages = ceil($num_of_results / $results_per_page); - if ($num_of_results == 0) { - echo "No results for given filters"; - return; - } - - if (!isset($_GET['page'])) { - $page = 1; - } - else { - $page = max(1, min($_GET['page'], $num_of_pages)); - } - - $offset = ($page - 1) * $results_per_page; - - // If there exist get variables that are for filtering - if (array_diff(array_keys($_GET), array('page'))) { - $condition = "WHERE "; - foreach ($_GET as $key => $value) { - $value = mysqli_real_escape_string($conn, $value); - if (!isset($filters[$key])) - continue; - - $condition .= $condition != "WHERE " ? "AND {$filters[$key]}.{$key} REGEXP '{$value}'" : "{$filters[$key]}.{$key} REGEXP '{$value}'"; - } - if ($condition == "WHERE ") - $condition = ""; - - $query = "{$select_query} {$condition} {$order} LIMIT {$results_per_page} OFFSET {$offset}"; - } - else { - $query = "{$select_query} {$order} LIMIT {$results_per_page} OFFSET {$offset}"; - } - $result = $conn->query($query); - - - // Table - echo "
"; - echo "\n"; - - $counter = $offset + 1; - while ($row = $result->fetch_assoc()) { - if ($counter == $offset + 1) { // If it is the first run of the loop - if (count($filters) > 0) { - echo ""; - foreach (array_keys($row) as $key) { - if (!isset($filters[$key])) { - echo "\n"; - } - echo ""; - echo ""; - } - - echo "\n"; - else - echo "\n"; - } - } - - if ($filename == 'games_list.php' || $filename == 'user_games_list.php') - echo "\n"; - else - echo "\n"; - echo "\n"; - foreach ($row as $key => $value) { - if ($key == 'fileset') - continue; - - // Add links to fileset in logs table - $matches = array(); - if (preg_match("/Fileset:(\d+)/", $value, $matches, PREG_OFFSET_CAPTURE)) { - $value = substr($value, 0, $matches[0][1]) . - "{$matches[0][0]}" . - substr($value, $matches[0][1] + strlen($matches[0][0])); - } - - echo "\n"; - } - echo "\n"; - - $counter++; - } - - echo "
"; - continue; - } - - // Filter textbox - $filter_value = isset($_GET[$key]) ? $_GET[$key] : ""; - - echo "
\n"; // Numbering column - foreach (array_keys($row) as $key) { - if ($key == 'fileset') - continue; - - // Preserve GET variables - $vars = ""; - foreach ($_GET as $k => $v) { - if ($k == 'sort' && $v == $key) - $vars .= "&{$k}={$v}-desc"; - elseif ($k != 'sort') - $vars .= "&{$k}={$v}"; - } - - if (strpos($vars, "&sort={$key}") === false) - echo "{$key}{$key}
{$counter}.{$value}
\n"; - echo "
\n"; - - // Preserve GET variables - $vars = ""; - foreach ($_GET as $key => $value) { - if ($key == 'page') - continue; - $vars .= "&{$key}={$value}"; - } - - // Navigation elements - if ($num_of_pages > 1) { - echo "
\n"; - - // Preserve GET variables on form submit - foreach ($_GET as $key => $value) { - if ($key == 'page') - continue; - - $key = htmlspecialchars($key); - $value = htmlspecialchars($value); - if ($value != "") - echo ""; - } - - echo "\n"; - - echo "
\n"; - } - -} -?> - diff --git a/include/user_fileset_functions.php b/include/user_fileset_functions.php deleted file mode 100644 index b160436c..00000000 --- a/include/user_fileset_functions.php +++ /dev/null @@ -1,153 +0,0 @@ - $value) { - if ($key != 'checksums') { - $key_string .= ':' . $value; - continue; - } - - foreach ($value as $checksum_pair) - $key_string .= ':' . $checksum_pair->checksum; - } - } - $key_string = trim($key_string, ':'); - - return md5($key_string); -} - -function file_json_to_array($file_json_object) { - $res = array(); - - foreach ($file_json_object as $key => $value) { - if ($key != 'checksums') { - $res[$key] = $value; - continue; - } - - foreach ($value as $checksum_pair) - $res[$checksum_pair->type] = $checksum_pair->checksum; - } - - return $res; -} - -function user_insert_queue($user_fileset, $conn) { - $query = sprintf("INSERT INTO queue (time, notes, fileset, ticketid, userid, commit) - VALUES (%d, NULL, @fileset_last, NULL, NULL, NULL)", time()); - - $conn->query($query); -} - -function user_insert_fileset($user_fileset, $ip, $conn) { - $src = 'user'; - $detection = false; - $key = ''; - $megakey = user_calc_key($user_fileset); - $transaction_id = $conn->query("SELECT MAX(`transaction`) FROM transactions")->fetch_array()[0] + 1; - $log_text = "from user submitted files"; - $conn = db_connect(); - - // Set timestamp of fileset insertion - $conn->query(sprintf("SET @fileset_time_last = %d", time())); - - if (insert_fileset($src, $detection, $key, $megakey, $transaction_id, $log_text, $conn, $ip)) { - foreach ($user_fileset as $file) { - $file = file_json_to_array($file); - - insert_file($file, $detection, $src, $conn); - foreach ($file as $key => $value) { - if ($key != "name" && $key != "size") - insert_filechecksum($file, $key, $conn); - } - } - } - - $fileset_id = $conn->query("SELECT @fileset_last")->fetch_array()[0]; - $conn->commit(); - return $fileset_id; -} - - -/** - * (Attempt to) match fileset that have fileset.game as NULL - * This will delete the original detection fileset and replace it with the newly - * matched fileset - */ -function match_and_merge_user_filesets($id) { - $conn = db_connect(); - - // Getting unmatched filesets - $unmatched_filesets = array(); - - $unmatched_files = $conn->query("SELECT fileset.id, filechecksum.checksum, src, status - FROM fileset - JOIN file ON file.fileset = fileset.id - JOIN filechecksum ON file.id = filechecksum.file - WHERE status = 'user' AND fileset.id = {$id}"); - $unmatched_files = $unmatched_files->fetch_all(); - - // Splitting them into different filesets - for ($i = 0; $i < count($unmatched_files); $i++) { - $cur_fileset = $unmatched_files[$i][0]; - $temp = array(); - while ($i < count($unmatched_files) - 1 && $cur_fileset == $unmatched_files[$i][0]) { - array_push($temp, $unmatched_files[$i]); - $i++; - } - array_push($unmatched_filesets, $temp); - } - - foreach ($unmatched_filesets as $fileset) { - $matching_games = find_matching_game($fileset); - - if (count($matching_games) != 1) // If there is no match/non-unique match - continue; - - $matched_game = $matching_games[0]; - - $status = 'fullmatch'; - - // Convert NULL values to string with value NULL for printing - $matched_game = array_map(function ($val) { - return (is_null($val)) ? "NULL" : $val; - }, $matched_game); - - $category_text = "Matched from {$fileset[0][2]}"; - $log_text = "Matched game {$matched_game['engineid']}: - {$matched_game['gameid']}-{$matched_game['platform']}-{$matched_game['language']} - variant {$matched_game['key']}. State {$status}. Fileset:{$fileset[0][0]}."; - - // Updating the fileset.game value to be $matched_game["id"] - $query = sprintf("UPDATE fileset - SET game = %d, status = '%s', `key` = '%s' - WHERE id = %d", $matched_game["id"], $status, $matched_game["key"], $fileset[0][0]); - - $history_last = merge_filesets($matched_game["fileset"], $fileset[0][0]); - - if ($conn->query($query)) { - $user = 'cli:' . get_current_user(); - - // Merge log - create_log("Fileset merge", $user, - mysqli_real_escape_string($conn, "Merged Fileset:{$matched_game['fileset']} and Fileset:{$fileset[0][0]}")); - - // Matching log - $log_last = create_log(mysqli_real_escape_string($conn, $category_text), $user, - mysqli_real_escape_string($conn, $log_text)); - - // Add log id to the history table - $conn->query("UPDATE history SET log = {$log_last} WHERE id = {$history_last}"); - } - - if (!$conn->commit()) - echo "Updating matched games failed\n"; - } -} - - -?> - diff --git a/index.php b/index.php deleted file mode 100644 index f76a86a1..00000000 --- a/index.php +++ /dev/null @@ -1,15 +0,0 @@ - diff --git a/logs.php b/logs.php deleted file mode 100644 index 53b538ab..00000000 --- a/logs.php +++ /dev/null @@ -1,20 +0,0 @@ - 'log', - 'timestamp' => 'log', - 'category' => 'log', - 'user' => 'log', - 'text' => 'log' -); - -create_page($filename, 25, $records_table, $select_query, $order, $filters); -?> - diff --git a/mod_actions.php b/mod_actions.php deleted file mode 100644 index 845b285b..00000000 --- a/mod_actions.php +++ /dev/null @@ -1,28 +0,0 @@ -\n"; -echo "\n"; -echo "\n"; - - -// Dev Tools -echo "

Developer Moderation Tools

"; -echo ""; -echo "
"; -echo ""; -echo "
"; -echo ""; - -if (isset($_POST['delete'])) { -} -if (isset($_POST['match'])) { - // merge_user_filesets(); -} - -echo ""; // Hidden -?> - diff --git a/user_games_list.php b/user_games_list.php deleted file mode 100644 index e6706b47..00000000 --- a/user_games_list.php +++ /dev/null @@ -1,32 +0,0 @@ - table -$filters = array( - "engineid" => "engine", - "gameid" => "game", - "extra" => "game", - "platform" => "game", - "language" => "game", - "name" => "game", - "status" => "fileset" -); - -$mapping = array( - 'engine.id' => 'game.engine', - 'game.id' => 'fileset.game', -); - -create_page($filename, 200, $records_table, $select_query, $order, $filters, $mapping); -?> -