<?php
/**
 * Copyright (c) 2013, 2019, 2021 Eclipse Foundation and others.
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 */

  # If we are requesting a file and it is on archive.eclipse.org, redirect and exit.
  $dir = $_SERVER['DOCUMENT_ROOT'] . urldecode(strtok($_SERVER['REQUEST_URI'], "?"));
  $archive_dir = str_replace("download.eclipse.org", "archive.eclipse.org", $dir);
  if(is_file($archive_dir)) {
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: https://archive.eclipse.org" . $_SERVER['REQUEST_URI']);
    exit;
  }

  # Send short 404 for non-human browsers
  $browser = $_SERVER['HTTP_USER_AGENT'];
        if(
    strpos($browser, "Jakarta") !== FALSE
    || strpos($browser, "Java/") !== FALSE
    || strpos($browser, "Slurp") !== FALSE
    || stripos($browser, "bot") !== FALSE
    || strpos($browser, "bing") !== FALSE
    || strpos($browser, "p2/") !== FALSE
    || strpos($browser, "Wget") !== FALSE
    || strpos($browser, "Googlebot/") !== FALSE
    || strpos($browser, "apacheHttpClient") !== FALSE
    || strpos($browser, "Apache-HttpClient/") !== FALSE
    || stripos($browser, "spider") !== FALSE
    || strpos($browser, "Apache-Maven/") !== FALSE
    || strpos($browser, "Apache Ivy/") !== FALSE
    || strpos($browser, "HTTP-Tiny/") !== FALSE
    || strpos($browser, "Archiva") !== FALSE
    || strpos($browser, "Artifactory/") !== FALSE
    || strpos($browser, "Gradle/") !== FALSE
    || strpos($browser, "Finjan") !== FALSE
    || strpos($browser, "Aether") !== FALSE
    || strpos($browser, "m2e/") !== FALSE
    || strpos($browser, "Debian APT/") !== FALSE
    || strpos($browser, "developer fusion") !== FALSE
    || strpos($browser, "Genuitec") !== FALSE
    || strpos($browser, "netBeans") !== FALSE
    || strpos($browser, "Nexus") !== FALSE
    || strpos($browser, "okhttp") !== FALSE
    ) {
     echo "404 Not Found";
  }
  else {
    require_once($_SERVER['DOCUMENT_ROOT'] . "/eclipse.org-common/classes/downloads/downloadDirectory.class.php");
    $DownloadDirectory = new DownloadDirectory();
    $dir = $DownloadDirectory->getCurrentDirectory();
    $dir_html = "";
    $is_p2 = false;

    if(is_dir($dir)) {
      $files = array();
      $dirs = array();
      if ($dh = opendir($dir)) {
        $dir_html = "<h2>Directory Contents</h2><div id='dirlist'>";
        while (($file = readdir($dh)) !== false) {
          if (filetype($dir . $file) == "dir") {
            $dirs[] = $file;
          }
          else {
            $files[] = $file;

            if(strpos($file, "artifacts.jar") !== FALSE
            || strpos($file, "artifacts.xml") !== FALSE
            || strpos($file, "compositeArtifacts.jar") !== FALSE
            || strpos($file, "compositeArtifacts.xml") !== FALSE) {
              # p2 repo
              $is_p2 = true;
            }
          }
        }
        closedir($dh);
        asort($dirs);
        asort($files);

        $dir_html .= $DownloadDirectory->getFormOutput($files, $dirs);
        $dir_html .= "</div>";
      }
      if($is_p2) {
        # p2
        $html = file_get_contents("404_p2.html");
      }
      else {
        $html = file_get_contents("404.html");
      }
      $html = str_replace("<!-- DNR:FILE -->", $dir_html, $html);
    }
    else {
      # Check archives
      $html = file_get_contents("404_nodoc.html");
      $message_html = "<h1>Not Found</h1>";
      $message_html .= "<p>We're sorry, the page or file cannot be found. Here are some reasons why:</p>";
      $message_html .= "<ul><li>A file may have moved to the archives. Please contact the project members on their user <a href='//eclipse.org/forums'>forum</a>.<br /></li>";
      $message_html .= "<li>Your file was part of a nightly or integration build which is no longer there. Simply download the latest version.</li><li>The project is uploading a new build, and this file is not there yet. Try again later.</li></ul>";
      $archive_dir = str_replace("download.eclipse.org", "archive.eclipse.org", $dir);
      if(is_dir($archive_dir)) {
        $message_html = "<h1>Moved to archives</h1>";
                          $message_html .= "<p>It appears this content was moved to the archives. You can try accessing this URL instead:<br /><br />";
        $message_html .= "&#160; &#160; <a href=\"//archive.eclipse.org" . $_SERVER['REQUEST_URI'] . "\">https://archive.eclipse.org" . $_SERVER['REQUEST_URI'] . "</a></p>";

        # doc title
        $html = str_replace("<title>Not Found</title>", "<title>Moved</title>", $html);

        # breadcrumb
        $html = str_replace("<li class=\"active\">Not Found</li>", "<li class=\"active\">Moved</li>", $html);
      }
      $html = str_replace("<!-- DNR:FILE -->", $message_html, $html);
    }
    echo $html;
  }