Import From Datfile"; ini_set('max_execution_time', 0); // Set the execution time to infinite. This is a bad idea in production. // Verify GET variables $auto = isset($_GET["auto"]) && $_GET["auto"] == "1"; $size = isset($_GET["size"]) && $_GET["size"] == "1"; $type = isset($_GET["type"]) && in_array($_GET["type"], $type) ? $_GET["type"] : ""; // Set import path $importroot = "../temp/import/"; // If there's a type defined, set the root accordingly if ($type != "") { $importroot .= $type."/"; } if (!isset($_GET["filename"])) { // List all files, auto-generate links to proper pages echo "

Automatically add all DATs
\n". "Sort list by name
\n". "Sort list by size

\n"; $files = scandir($importroot); if (sizeof($files) != 0) { if ($size) { usort($files, function ($a, $b) { global $importroot; $size_a = filesize($importroot.$a); $size_b = filesize($importroot.$b); return $size_a - $size_b; }); } foreach ($files as $file) { if (preg_match("/^.*\.dat$/", $file) || preg_match("/^.*\.xml$/", $file)) { // If we want to import everything in the folder... if ($auto) { import_dat($file); sleep(1); echo ""; } else { echo "".htmlspecialchars($file)." (".filesize($importroot.$file)." bytes)
\n"; } } } } } else { import_dat($_GET["filename"]); sleep(1); echo ""; } function import_dat ($filename) { global $link, $normalize_chars, $search_pattern, $importroot, $type; // First, get the pattern of the file name. This is required for organization. switch ($type) { case "mame": $datpattern = "/^(.*)\.xml$/"; break; case "nointro": $datpattern = "/^(.*?) \((\d{8}-\d{6})_CM\)\.dat$/"; break; case "redump": $datpattern = "/^(.*?) \((\d{8} \d{2}-\d{2}-\d{2})\)\.dat$/"; break; case "tosec": $datpattern = "/^(.*?) - .* \(TOSEC-v(\d{4}-\d{2}-\d{2})_CM\)\.dat$/"; break; case "trurip": $datpattern = "/^(.*?) - .* \(trurip_XML\)\.dat$/"; break; default: $datpattern = "/^(.+?) - (.+?) \((.*) (.*)\)\.dat$/"; break; } // Check the file is valid if (!file_exists($importroot.$filename)) { echo "The file you supply must be in ".$importroot."
"; echo "Go back to import page"; return; } elseif (!preg_match($datpattern, $filename, $fileinfo)) { echo "DAT not in the proper pattern! (Manufacturer - SystemName (Source .*)\.dat)
\n"; echo "Go back to import page"; return; } echo "

The file ".$filename." has a proper pattern!

\n"; // Next, get information from the database on the current machine switch ($type) { case "mame": preg_match("/^(.*) - (.*)$/", $mapping_mame[$fileinfo[1]], $name); $manufacturer = $name[1]; $system = $name[2]; $source = "MAME"; $datestring = filemtime($importroot.$filename); $date = date("Y-m-d G:i:s", $datestring); break; case "nointro": preg_match("/^(.*) - (.*)$/", $mapping_nointro[$fileinfo[1]], $name); $manufacturer = $name[1]; $system = $name[2]; $source = "no-Intro"; $datestring = $fileinfo[2]; preg_match("/(\d{4})(\d{2})(\d{2})-(\d{2})(\d{2})(\d{2})/", $datestring, $date); $date = $date[1]."-".$date[2]."-".$date[3]." ".$date[4].":".$date[5].":".$date[6]; break; case "redump": preg_match("/^(.*) - (.*)$/", $mapping_redump[$fileinfo[1]], $name); $manufacturer = $name[1]; $system = $name[2]; $source = "Redump"; $datestring = $fileinfo[2]; preg_match("/(\d{4})(\d{2})(\d{2}) (\d{2})-(\d{2})-(\d{2})/", $datestring, $date); $date = $date[1]."-".$date[2]."-".$date[3]." ".$date[4].":".$date[5].":".$date[6]; break; case "tosec": preg_match("/^(.*) - (.*)$/", $mapping_tosec[$fileinfo[1]], $name); $manufacturer = $name[1]; $system = $name[2]; $source = "TOSEC"; $datestring = $fileinfo[2]; preg_match("/(\d{4})-(\d{2})-(\d{2}))/", $datestring, $date); $date = $date[1]."-".$date[2]."-".$date[3]." 00:00:00"; break; case "trurip": preg_match("/^(.*) - (.*)$/", $mapping_trurip[$fileinfo[1]], $name); $manufacturer = $name[1]; $system = $name[2]; $source = "trurip"; $datestring = filemtime($importroot.$filename); $date = date("Y-m-d G:i:s", $datestring); break; default: $manufacturer = $fileinfo[1]; $system = $fileinfo[2]; $source = $fileinfo[3]; $datestring = $fileinfo[4]; preg_match("/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/", $datestring, $date); $date = $date[1]."-".$date[2]."-".$date[3]." ".$date[4].":".$date[5].":".$date[6]; break; } $query = "SELECT id FROM systems WHERE manufacturer='$manufacturer' AND system='$system'"; $result = mysqli_query($link, $query); if (!gettype($result) == "boolean" || mysqli_num_rows($result) == 0) { echo('Error: No suitable system found! Please add the system and then try again
'); return; } $sysid = mysqli_fetch_assoc($result); $sysid = $sysid["id"]; if ($sourceid == "") { $query = "SELECT id FROM sources WHERE name='".$source."'"; $result = mysqli_query($link, $query); if (!gettype($result) == "boolean" || mysqli_num_rows($result) == 0) { echo('Error: No suitable source found! Please add the source and then try again
'); return; } $sourceid = mysqli_fetch_assoc($result); $sourceid = $sourceid["id"]; } // Then, parse the file and read in the information. Echo it out for safekeeping for now. $handle = fopen($importroot.$filename, "r"); if ($handle) { $format = ""; $machinefound = false; $machinename = ""; $description = ""; $gameid = 0; $comment = false; echo "

Roms Added:

\n"; while (($line = fgets($handle)) !== false) { // If a machine or game tag is found, check to see if it's in the database // If it's not, add it to the database and then save the gameID // Normalize the whole line, just in case $line = strtr($line, $normalize_chars); // If the input style hasn't been set, set it according to the header if ($format == "") { if (strpos($line, "") !== false) { $comment = false; } elseif (strpos($line, "
MachineRomSizeCRC32MD5SHA1