<?php
/*******************************************************************************
 * Copyright (c) 2013 Eclipse Foundation, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eclipse Foundation - initial API and implementation
 *******************************************************************************/


	$browser = $_SERVER['HTTP_USER_AGENT'];

	# Send short 404 for non-human browsers
        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
		) {
                echo "404 Not Found";
        }
	else {
		$_SERVER['REQUEST_URI'] = str_replace("?d", "", $_SERVER['REQUEST_URI']);
		$dir = $_SERVER['DOCUMENT_ROOT'] . urldecode($_SERVER['REQUEST_URI']);
		$dir_html = "";
		$is_p2 = false;

		$initial_state_string = "Show";
		$initial_state_style  = "none";
		if($_SERVER['QUERY_STRING'] == "d") {
			$initial_state_string = "Hide";
			$initial_state_style  = "inline";
		}
		if(is_dir($dir)) {
			$files = array();
			$dirs = array();
			if ($dh = opendir($dir)) {
				$dir_html = "<p><a id='togglelabel' href='#' onclick='fnToggleDirList();return false;'>$initial_state_string Directory Contents.</a></p><div id='dirlist' style='display:$initial_state_style;'>";
				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);

				foreach ($dirs as $directory) {
					if($directory != "." ) {
						$dir_html .= "<img src='http://dev.eclipse.org/small_icons/places/folder.png'><a href='" . $_SERVER['REQUEST_URI'] . "$directory/?d'> " . $directory . "</a><br />\n";
					}
				}
				foreach ($files as $file) {
					$dir_html .= "<img src='http://dev.eclipse.org/small_icons/actions/edit-copy.png'><a href='" . $_SERVER['REQUEST_URI'] . "$file'> " . $file . "</a><br />\n";
				}

				$dir_html .= "</div><script language='javascript' src='/errors/js.js'></script>";
			}
			if($is_p2) {
				# p2
				$html = file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/errors/404_p2.html");
			}
			else {
				$html = file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/errors/404.html");
			}
			$html = str_replace("<!-- DNR:FILE -->", $dir_html, $html);
		}
		else {
			# Check archives
			$html = file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/errors/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=\"http://archive.eclipse.org" . $_SERVER['REQUEST_URI'] . "\">http://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;

	}
?>
