<?php
if (isset($_GET["path"])) {
    $path = $_GET["path"];
    if (!preg_match('/^[A-Za-z0-9.:#_$\/+-]+$/', $path)) {
        die("Error: Invalid characters in test path.");
    }
    $path = str_replace("\\", "/", $path);
    $pos = strpos($path, "tests/");
    if ($pos !== false) {
        $path = substr($path, $pos + 6);
    }
    $sumotests = "/srv/git/sumo/tests/";
    $testpath = realpath($sumotests.$path);
// basic sanity checks to prevent obvious attempts to get access to the complete file system
    if (strpos($testpath, $sumotests) !== 0 || !file_exists($testpath)) {
        die("Error: Test '".$path."' not found.");
    }
    $testdir = tempnam("/tmp", "extractTestDir");
    unlink($testdir); // we just need the name
    $app = "";
    if (isset($_GET["app"]) && $_GET["app"] != "") {
        if (!preg_match('/^[a-z.]+$/', $_GET["app"])) {
            die("Error: Invalid app.");
        }
        $app = " -a ".$_GET["app"];
    }
    $extract = escapeshellcmd($sumotests."../tools/extractTest.py ".$testpath." -x -o ".$testdir.$app);
    $o1 = shell_exec($extract." 2>&1");
//echo $o1;
    if (!file_exists($testdir)) {
        die("Error: Extraction failed.\n".$o1);
    }
    $zipname = tempnam("/tmp", "extractTest");
    unlink($zipname); // we just need the name
    $zipname .= ".zip";
    chdir($testdir);
    $o2 = shell_exec(escapeshellcmd("zip -r ".$zipname." .")." 2>&1");
//echo $o2;
    if (file_exists($zipname)) {
        header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
        header("Cache-Control: public"); // needed for internet explorer
        header("Content-Type: application/zip");
        header("Content-Transfer-Encoding: Binary");
        header("Content-Length:".filesize($zipname));
        header("Content-Disposition: attachment; filename=".str_replace("/", "_", $path).".zip");
        readfile($zipname);
        die();
    } else {
        die("Error: Zipfile not found.\n".$o2);
    }
}
?>
<!DOCTYPE html>

<html lang="en">
<head>
  <title>Extracting tests - Eclipse SUMO - Simulation of Urban MObility</title>
  <meta charset="UTF-8">
  <link rel="stylesheet" type="text/css" href="sumo.css">
  <link rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/">
</head>

<body id="bmission">
<div id="container">

 <div id="logo" align="left"><img src="../logo.png" width="auto" height="80" align="left"
     alt="SUMO - Simulation of Urban MObility" style="margin-left:10px;
     margin-right:20px;"/>
      <div id="logo_text1">&nbsp;</div>
      <div id="logo_text2">&nbsp;</div>
 </div>

 <div id="subsMenu"><ul>
  <li class="sub"><a href="/">Home</a></li>
  <li class="sub"><a href="/docs/">Documentation</a></li>
  <li class="sub"><a href="/docs/Downloads.html">Downloads</a></li>
  <li class="sub"><a href="https://github.com/eclipse-sumo/sumo/issues">Issue Tracker</a></li>
  <li class="sub"><a href="https://github.com/eclipse-sumo/sumo">GitHub Repository</a></li>
  <li class="sub"><a href="https://www.eclipse.dev/sumo/contact/">Contact</a></li>
 </ul></div>

 <h2>Extracting tests</h2>
<p>SUMO comes with a large set of tests, just browse them at <a href="https://github.com/eclipse-sumo/sumo/tree/main/tests">sumo/tests/</a>.
If you do not want to download all the tests with the <a href="/docs/Downloads.html#all-inclusive-tarball" title="Downloads">complete sumo package</a>
you can use this form to download a single test. Paste the path to the test you like
(e.g. <a href="https://github.com/eclipse-sumo/sumo/tree/main/tests/sumo/extended/rerouter/use_routing_device">sumo/extended/rerouter/use_routing_device</a>)
into the form and get a zip containing all the files. You can also paste the complete URL from GitHub, you do not need to cut off the prefix.
</p>
<p>You do not need to fill the Application field unless this page complains about a non-unique application. If this is the case you need to choose the variant to extract
(like "sumo" or "meso") and fill it in. You can also use this site as a web service by calling it like https://sumo.dlr.de/extractTest.php?path=path/to/test.
</p>
  <form method="get" action="extractTest.php">
    <label for="path">Test path:</label>
    <input type="text" name="path" id="path" size="50">
    <label for="app">Application:</label>
    <input type="text" name="app" id="app" size="20">
    <input type="submit" name="submit" value="Extract">
  </form>
  <br>
  <button onClick="copyLink();">Copy download link</button>

  <div id="copied_message" style="display:none; color:green;">
    <br>Copied to clipboard!
  </div>

  <div id="footer">
    <div>Copyright (C) 2011-<?php echo date("Y"); ?>, German Aerospace Center (DLR) - Institute of Transportation Systems</div>
    <div>Layout based on <a href="http://www.oswd.org/design/preview/id/3365">"Three Quarters"</a> by "SimplyGold"</div>
  </div>
</div>

<script>
function copyLink() {
    var path = document.getElementById("path");
    path.select();
    navigator.clipboard.writeText("https://sumo.dlr.de/extractTest.php?path=" + path.value.replace(/\\/g, '/'));
    document.activeElement.blur();

    document.getElementById("copied_message").style.display = "block";
    setTimeout(function() {
        document.getElementById("copied_message").style.display = "none";
    }, 2000);
} 
</script>
</body>

</html>
