<?php
// TODO:
//	* fix sitemap=y output when using not existant s/c/a (suite/comp/arch) as option
//	* Add changelog view option
//	  - extract changelog for binary something like 
//          dpkg-deb --fsys-tarfile wine_1.1.16+20090310git-1_i386.deb |\
//           tar tf - --wildcards "*/changelog.Debian.gz" 2>/dev/null; echo $? (0=ok, 2=not found)
//        - extract changelog from deb file with apt-listchanges like
//          apt-listchanges -f text --save_seen=none -a --which=changelogs 
//          /tmp/build-dir/foo/bar.deb 2>&1 | grep -v "^mode: debian-changelog$\|
//          ^Local variables:$\|^add-log-mailing-address \|^End:$"
//        - extract changelog for source from .diff.gz
//	* Print field names (in detail box) bold or something other than the value
//	* Beautify packet/source search
//	* Check more wrong htmlspecialchars(*) places (see 0.1.5 comment on that,
//	  but be aware of switch from htmlenteties to htmlspecialchars)
//	* Check function print_source_detail if going through source_binary array
//	  is really the right way to display the "Binary (arch):" output.
//	  Like for alsa-lib, there are Packages which get only build on specific
//        arches! For now, do not display NOT found bin-packages
//	* Remove 410 (GONE) header response from fullindex=y
//
// History:
// 0.1.6.5: (2019-12-05)
//	* Fix calling of get_opt_str, no need to use ",&$variable", just use ",$variable" and within
//        get_opt_str use in function definition ",&$"
// 0.1.6.4: (2015-04-20)
//	* Optimize to be more mobile-device friendly
//	  (viewport-meta, inline-stylesheet)
// 0.1.6.3: (2012-08-17)
//	* Add 'linux' to $meta_keywords
//      * Add '(GNU/Linux)' string to function print_general_body_info output
//	* Add non-Debian affiliated warning because of:
//        http://lists.alioth.debian.org/pipermail/pkg-multimedia-maintainers/2012-May/026787.html
// 0.1.6.2: (2012-01-19)
//      * in print_general_meta_tags and print_html_start output the defined charset and not hardcoded utf-8
//	* in print_binary_detail don't output version in show/hide details link (just the suite)
// 0.1.6.1: (2012-01-16)
//	* in print_source_detail don't output version in show/hide details link (just the suite)
// 0.1.6: (2012-01-13)
//	* in search_find_package also print Package/Source suite info (after Architecture)
//	* in print_source_list_box_start don't output version in url-link (just the suite)
//	* add listing of package content for binary packages (dpkg -c)
//	* add version info to show/hide detailed package info link
//	* print_source_detail: only print 'Binary (<arch>): packages' when there is
//	  at least one found package
//	* in print_binary_detail add version info to source-link
//	* in print_source_list_box_start output version-link via urlencode ('+' sign fix)
//	* add GET/POST option 'v' (package version)
//	* in search_find_package add 'Architecture: ' info to binary package details
//	* fix link of 'Binary (<arch>): <filename>' in print_source_detail (wrong arch used)
//	* add listing of full changelog of binary files
//	* move handling of repository listing to new function repo_listing
//	* Add ability to generate a google sitemap (?sitemap=y)
//	* Fix Packages index pharsing. There can be a "Homepage:" field after the
//	  long description (or other fields... :)).
//	* use PHP_SELF as PRGFILE (needed for link creation) instead of hardcoded
//	  "list_repository.php"
//	* Implement Package/Source search functionality (?search=y)
//	* Disable meta_tag robot NOFOLLOW for non index pages
//	* Switch from htmlenteties to htmlspecialchars and use variable $CODEPAGE
//	* Add sitemap_index=y option to generate a ... sitemap index
//	  Also update function sitemap to handle sitemap=mainindex and normal
//	  sitemap=y (and also additional s,c,a options)
//	* fix human_readable_size for GB size (missing divide trough $nk)
// 0.1.5: (2009-03-02)
//      * Add more Fields to source-dsc-view
//      * remove wrong (not needed) htmlentities(*) in new detailed source view
//        as function get_opt_str already does this!
//	* Fix bug: In Source-package listing, the "Binary (arch):" listing outputs
//	  a list of build packages to switch to. The check if this binary package
//	  exists (and therefor a link should be generated) was wrong, due to bin_found
//        had not been reset to -1 within the loop, but outside. Therefor after a package
//        was found, all other were also displayed as if the exist.
//	* Do not output binary packages in print_source_detail/"Binary (arch):" which can
//        not be found. (See TODO about print_source_detail)
//	* Add link to hide details in source-package view
//	* Add current package (if selected) to meta_keywords
//	* Add "detailed" (if source-details on) to meta_keywords
// 0.1.4: (2009-01-19)
//	* sort Packages/Sources listing by name
//	* read file size for binary/source files
//	* add human_readable_size for size conversion
//	* add function read_source_description to read a dsc file
//	  This is needed for fields like "Homepage" or "Vcs-Browser"
//	* enhance print_source_details to:
//	  - Output html link to enhanced-details source listing
//	  - When called as enhanced, execute read_source_description
//          and output these new fields
//	* Revert logic for not allowing SearchBots to follow links on source-detail-page
//	  Without this, the detailed source-info pages wouldn't get indexed by google+co
// 0.1.3:
//	* Fix Source-filelisting
//	* Add variable PRGFILE
// 0.1.2:
//	* Add HTTP-Error Code 410 (GONE)/404 (Not Found) if something could not be found
//	* use ob_start and ob_end_flush
//	  (needed for HTTP-Error being print before other strings)
$PRGNAME = "ListRepository";
$PRGVERSION = "0.1.6.5";
$PRGAUTHOR = "Jürgen Tretthahn";
$PRGDATE = "2008-2019";
$PRGFILE = basename($_SERVER['PHP_SELF']);
$FULLSCRIPTURL = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'];
// $CODEPAGE='ISO-8859-15';
// $SOURCECODEPAGE='UTF-8';
$CODEPAGE='UTF-8';

// ======================== START OF CONFIGURATION SECTION ========================
// Specify how the "dists" directory can be accessed through webbrowser
$html_baserepdir="/repositories/daily/";
// Specify where the "dists" directory can be found on the filesystem
$baserepdir = "/opt/debian-www/repositories/daily/";
// Specify the "dists" directory
$baserepdists = $baserepdir."dists/";
// Specify which suites should be available
$defined_suites = array("testing","unstable","experimental");

// meta-tags settings
$meta_keywords = htmlspecialchars("linux, debian, repository, packages", ENT_QUOTES, $CODEPAGE);
$meta_description = htmlspecialchars("several Debian Packages (.deb) from " . $PRGAUTHOR . " for " . implode($defined_suites,"/"), ENT_QUOTES, $CODEPAGE);
$meta_author = htmlspecialchars("Jürgen Tretthahn", ENT_QUOTES, $CODEPAGE);

// On page header info
$html_author = "<span style=\"color: #229955\">".$meta_author."</span> alias <span style=\"color: #BB4444\">Orson</span>";

// Page title
$global_title = "OTP's Debian repository listing";

// ======================== END OF CONFIGURATION SECTION ========================

// Security
function remove_slashes($string)
{
	return str_replace("/", " ", $string);
}

function abnorm_end()
{
	//header("HTTP/1.0 404 Not Found");
	header("HTTP/1.0 410 Gone");
	print_html_end();
	ob_end_flush();
	exit ("");
}

// other functions
function human_readable_size($size)
{
	$nk=100;
	$base=1024;
	if ($size >= $base*$base*$base*$base) { $h_size=round(($size*$nk)/($base*$base*$base*$base))/$nk." TB"; } else {
	if ($size >= $base*$base*$base) { $h_size=round(($size*$nk)/($base*$base*$base))/$nk." GB"; } else {
	if ($size >= $base*$base) { $h_size=round(($size*$nk)/($base*$base))/$nk." MB"; } else {
	if ($size >= $base) { $h_size=round(($size*$nk)/$base)/$nk." KB"; } else {
		$h_size=$size." B";
	}}}}
	return $h_size;
}

function sort_by_binary($a,$b)
{
	$c=$a['binary'];
	$d=$b['binary'];
	if ($c == $d) return 0;
	return ($c < $d) ? -1 : 1;
}

function sort_by_source($a,$b)
{
	$c=$a['source'];
	$d=$b['binary'];
	if ($c == $d) return 0;
	return ($c < $d) ? -1 : 1;
}

// Printing functions
function print_general_meta_tags($add_keywords,$robot_follow)
{
	global $CODEPAGE;
	global $meta_keywords;
	global $meta_description;
	global $meta_author;
	echo '<meta http-equiv="Content-Type" content="text/html; charset=' . $CODEPAGE . '">'; echo "\n";
	echo '<meta name="description" content="' . $meta_description . '">'; echo "\n";
	echo '<meta name="author" content="' . $meta_author . '">'; echo "\n";
	//echo '<meta http-equiv="expires" content="1800"> <!-- 30 Minuten (Angabe in Sekunden) -->'; echo "\n";
	echo '<meta name="keywords" content="' . $meta_keywords . '">'; echo "\n";
        if ($robot_follow == "Y") {
		echo '<meta name="robots" content="INDEX, FOLLOW">'; echo "\n";
	} else {
		echo '<meta name="robots" content="INDEX, NOFOLLOW">'; echo "\n";
	}
	echo '<meta name="revisit-after" content="4 days">'; echo "\n";
	echo '<meta name=viewport content="width=device-width, initial-scale=1">'; echo "\n";
}

function print_general_body_info()
{
	global $html_author;
	global $defined_suites;
	echo "<div style=\"font-size:large\">This website is not affiliated with the Debian Project, use it in combination with Debian at your own risk!</div></br>\n";
	echo "<div style=\"font-size:large\">Debian (GNU/Linux) like repositories for " . implode($defined_suites,"/") ." suites from " . $html_author . "</div>\n";
}

function print_html_start($title,$robot_follow)
{
	global $CODEPAGE;
	global $global_title;
	header( 'Content-type: text/html; charset=' . $CODEPAGE);
	echo "<HTML>\n";
	echo "<HEAD>\n";
	print_general_meta_tags("",$robot_follow);
//	echo " <link rel=\"stylesheet\" type=\"text/css\" href=\"styles/site.css\">\n";

	echo " <style type=\"text/css\" media=\"all\">\n";
	echo file_get_contents("styles/site.css");
	echo " </style>\n";
	
//	echo " <style type=\"text/css\" media=\"all\">\n";
//	echo "  @import url(\"styles/site.css\");\n";
//	//echo "  @import url(\"styles/mirror.css\");\n";
//	echo " </style>\n";
//	echo " <!--[if IE]><![if gte IE 6]><![endif]-->\n";
//? >
//  <style type="text/css" media="print">";
//   @import url("styles/print.css");
//  </style>
// <!--[if IE]><![endif]><![endif]-->
//<?php
	echo "<TITLE>\n";
	if ($title == "") {echo "$global_title\n";} else {echo "$global_title - $title\n";}
	echo "</TITLE\n";
	echo "</HEAD>\n";
	echo "<BODY>\n";
	print_general_body_info();
}

function print_html_end()
{
	global $PRGNAME;
	global $PRGVERSION;
	global $PRGAUTHOR;
	global $PRGDATE;
	echo "<HR/>\n";
	echo "<div style=\"font-size:small\" align=\"center\"><a href=\"?search=y\">Repository search page</a></div>\n";
	echo "<div style=\"font-size:x-small\" align=\"center\">$PRGNAME ($PRGVERSION), &#169; $PRGAUTHOR ($PRGDATE)</div>\n";
	echo "</BODY>\n";
	echo "</HTML>\n";
}

function print_suite_box_start($base,$suite,$component,$architecture,$package)
{
	global $PRGFILE;
	echo "<p class=\"para\">\n";
	echo "<div class=\"example\" style=\"background-color: #88FF88\">\n";
	echo "<div class=\"info\">\n";

	// Base Repo link
	if ($base == "N") echo "<p><b>Repository: <a href=\"$PRGFILE\">Orsons debian stuff</a></b></p>\n";

	// Suite info
	//echo "<p><b>Suite: <a href=\"$PRGFILE?s=$suite\">$suite</a></b></p>\n";
	echo "<p><b>Suite: ";
	if ($base != "N" || $component != "" ) echo "<a href=\"$PRGFILE?s=$suite\">";
	echo "$suite";
	if ($base != "N" || $component != "" ) echo "</a>";
	echo "</b></p>\n";
	

	// Component info
	//if ($component != "") echo "<p><b>Component: <a href=\"$PRGFILE?s=$suite&c=$component\">$component</a></b></p>\n";
	if ($component != "") echo "<p><b>Component: $component</b></p>\n";

	// Architecutre info
	if ($architecture != "") {
		if ($package != "") {
			echo "<p><b>Architecture: <a href=\"$PRGFILE?s=$suite&c=$component&a=$architecture\">$architecture</a></b></p>\n";
		} else {
			echo "<p><b>Architecture: $architecture</b></p>\n";
		}
	}

	echo "</div>\n";
	echo "<div class=\"example-contents\">\n";
}
function print_suite_box_end()
{
	echo "</div>\n";
	echo "</div>\n";
	echo "</p>\n";
}

function print_component_box_start($component)
{
	echo "<p class=\"para\">\n";
	echo "<div class=\"example\" style=\"background-color: #AAAAAA\">\n";
	echo "<div class=\"info\"><p><b>Component: $component</b></p></div>\n";
	echo "<div class=\"example-contents\">\n";
}
function print_component_box_end()
{
	echo "</div>\n";
	echo "</div>\n";
	echo "</p>\n";
}

function print_binary_list_box_start($suite,$component,$architecture,$package,$version)
{
	global $PRGFILE;
	global $CODEPAGE;
	echo "<p class=\"para\">\n";
	echo "<div class=\"example\" style=\"background-color: #C0C0FF\">\n";
	echo "<div class=\"info\"><p><b>Package: <a href=\"$PRGFILE?s=$suite&c=$component&a=$architecture&p=$package\">".htmlspecialchars($package, ENT_QUOTES, $CODEPAGE)."</a> (".htmlspecialchars($version, ENT_QUOTES, $CODEPAGE).")</b></p></div>\n";
	echo "<div class=\"example-contents\">\n";
}
function print_binary_list_box_end()
{
	echo "</div>\n";
	echo "</div>\n";
	echo "</p>\n";
}

function print_binary_detail_box_start($package,$add_string)
{
	echo "<p class=\"para\">\n";
	echo "<div class=\"example\" style=\"background-color: #C0C0FF\">\n";
	echo "<div class=\"info\"><p><b>Package".$add_string.": $package</b></p></div>\n";
	echo "<div class=\"example-contents\">\n";
}
function print_binary_detail_box_end()
{
	echo "</div>\n";
	echo "</div>\n";
	echo "</p>\n";
}

function print_source_list_box_start($suite,$component,$architecture,$source,$version)
{
	global $PRGFILE;
	global $CODEPAGE;
	echo "<p class=\"para\">\n";
	echo "<div class=\"example\" style=\"background-color: #CCCCAA\">\n";
	// echo "<div class=\"info\"><p><b>Source: <a href=\"$PRGFILE?s=$suite&c=$component&a=$architecture&p=$source&v=".urlencode($version)."\">".htmlspecialchars($source, ENT_QUOTES, $CODEPAGE)."</a> (".htmlspecialchars($version, ENT_QUOTES, $CODEPAGE).")</b></p></div>\n";
	echo "<div class=\"info\"><p><b>Source: <a href=\"$PRGFILE?s=$suite&c=$component&a=$architecture&p=$source\">".htmlspecialchars($source, ENT_QUOTES, $CODEPAGE)."</a> (".htmlspecialchars($version, ENT_QUOTES, $CODEPAGE).")</b></p></div>\n";
	echo "<div class=\"example-contents\">\n";
}
function print_source_list_box_end()
{
	echo "</div>\n";
	echo "</div>\n";
	echo "</p>\n";
}

function print_source_detail_box_start($source)
{
	echo "<p class=\"para\">\n";
	echo "<div class=\"example\" style=\"background-color: #CCCCAA\">\n";
	echo "<div class=\"info\"><p><b>Source: $source</b></p></div>\n";
	echo "<div class=\"example-contents\">\n";
}
function print_source_detail_box_end()
{
	echo "</div>\n";
	echo "</div>\n";
	echo "</p>\n";
}

// Get option
function get_opt_str($line,$check,&$erg)
{
	global $CODEPAGE;
	if (strncmp($line, $check, strlen($check)) == 0) { $erg = htmlspecialchars(rtrim(substr($line,strlen($check))), ENT_QUOTES, $CODEPAGE);}
}

// Read functions
function read_suite_release_file($suite)
{
	global $baserepdists;
	global $rel_architectures;
	global $rel_components;
	$suite_info = array();

	$release_file_name = "$baserepdists" . "$suite/" . "Release";
	// Check if correct/existing
	$res=file_exists($release_file_name);
	if ($res != 1) {
		echo "<H1>Sorry, something bad happened!</H1>\n";
		abnorm_end();
	}
	// Open file for reading
	$release_file=fopen($release_file_name,"r") or exit("Unable to open file!");

	// Begin processing Release file
	while(!feof($release_file)) {
		$line = fgets($release_file);

		get_opt_str($line,"Suite: ",$suite_info['suite']);
		get_opt_str($line,"Codename: ",$suite_info['codename']);
		$tmp_str = "";get_opt_str($line,"Architectures: ",$tmp_str);
		if ($tmp_str !== "") {
			$expl = explode(" ",$tmp_str);
			$c = 0;
			foreach ($expl AS $e) { $suite_info['architectures'][$c] = "$e"; $c++;}
		}
		$tmp_str = "";get_opt_str($line,"Components: ",$tmp_str);
		if ($tmp_str !== "") {
			$expl = explode(" ",$tmp_str);
			$c = 0;
			foreach ($expl AS $e) { $suite_info['components'][$c] = "$e"; $c++;}
		}
		get_opt_str($line,"Date: ",$suite_info['date']);
		get_opt_str($line,"Description: ",$suite_info['description']);
		get_opt_str($line,"Label: ",$suite_info['label']);
		get_opt_str($line,"Origin: ",$suite_info['origin']);
	}

	// Close file
	fclose($release_file);
	
	return $suite_info;
}

function process_suite_release($base,$suite,$component,$architecture,$package)
{
	$suite_info = read_suite_release_file($suite);

	print_suite_box_start($base,$suite_info['suite'],$component,$architecture,$package);
	echo "Suite: ".$suite_info['suite']."<br/>\n";
	echo "Codename: ".$suite_info['codename']."<br/>\n";
	echo "Components: ".implode($suite_info['components']," ")."<br/>\n";
	echo "Architectures: ".implode($suite_info['architectures']," ")."<br/>\n";
	echo "Date: ".$suite_info['date']."<br/>\n";
	echo "Description: ".$suite_info['description']."<br/>\n";
	echo "Label: ".$suite_info['label']."<br/>\n";
	echo "Origin: ".$suite_info['origin']."<br/>\n";
	print_suite_box_end();
	return $suite_info;
}

function read_packages_info($suite,$component,$arch)
{
	global $baserepdists;
	$binary_info = array();
	$bin_count = 0;
	$bin_binary = "";
	$bin_source = "";
	$bin_version = "";
	$bin_section = "";
	$bin_priority = "";
	$bin_maintainer = "";
	$bin_filename = "";
	$bin_filesize = "";
	$bin_description = "";
	$bin_description_long = "";
	$bin_homepage = "";
	$desc_mode = "";

	$package_file_name = "$baserepdists" . "$suite/" . $component . "/binary-" . $arch . "/Packages";
	// Check if correct/existing
	$res=file_exists($package_file_name);
	if ($res != 1) {
		echo "<H1>Sorry, something bad happened!</H1>\n";
		abnorm_end();
	}
	// Open file for reading
	$package_file=fopen($package_file_name,"r") or exit("Unable to open file!");

	// Begin processing Package file
	while(!feof($package_file)) {
		
		$line = fgets($package_file);

		if ($line == "\n") {
			$package_info[$bin_count]['binary'] = $bin_binary;
			if ($bin_source == "")
				{$package_info[$bin_count]['source'] = $bin_binary;} else 
				{$package_info[$bin_count]['source'] = $bin_source;}
			$package_info[$bin_count]['version'] = $bin_version;
			$package_info[$bin_count]['section'] = $bin_section;
			$package_info[$bin_count]['priority'] = $bin_priority;
			$package_info[$bin_count]['maintainer'] = $bin_maintainer;
			$package_info[$bin_count]['filename'] = $bin_filename;
			$package_info[$bin_count]['filesize'] = human_readable_size($bin_filesize);
			$package_info[$bin_count]['description'] = $bin_description;
			$package_info[$bin_count]['description_long'] = $bin_description_long;
			$package_info[$bin_count]['homepage'] = $bin_homepage;
			$bin_binary = "";
			$bin_source = "";
			$bin_version = "";
			$bin_section = "";
			$bin_priority = "";
			$bin_maintainer = "";
			$bin_filename = "";
			$bin_filesize = "";
			$bin_description = "";
			$bin_description_long = "";
			$bin_homepage = "";
			$desc_mode = "";
			$bin_count++;
		} else
		{
			if (($desc_mode == "") || (substr($line,0,1) != " ")) {
				$desc_mode = "";
				get_opt_str($line,"Package: ",$bin_binary);
				get_opt_str($line,"Source: ",$bin_source);
				get_opt_str($line,"Version: ",$bin_version);
				get_opt_str($line,"Section: ",$bin_section);
				get_opt_str($line,"Priority: ",$bin_priority);
				get_opt_str($line,"Maintainer: ",$bin_maintainer);
				get_opt_str($line,"Filename: ",$bin_filename);
				get_opt_str($line,"Size: ",$bin_filesize);
				get_opt_str($line,"Description: ",$bin_description);
				if (strncmp($line,"Description:",strlen("Description:")) == 0) $desc_mode = "Y";
				get_opt_str($line,"Homepage: ",$bin_homepage);
			} else {
					if ($line == " .\n")
					{ $bin_description_long = "$bin_description_long\n";} else
					{ $bin_description_long = "$bin_description_long".substr($line,1); }
			}
		}

	}

	// Close file
	fclose($package_file);

	return $package_info;
}

function read_sources_info($suite,$component,$arch)
{
	global $baserepdists;
	$source_info = array();
	$src_count = 0;
	$src_source = "";
	$src_binary = "";
	$src_architecture = "";
	$src_version = "";
	$src_maintainer = "";
	$src_directory = "";
	$src_files = array();
	$file_mode = "";
	$file_count = 0;

	$source_file_name = "$baserepdists" . "$suite/" . $component . "/" . $arch . "/Sources";
	// Check if correct/existing
	$res=file_exists($source_file_name);
	if ($res != 1) {
		echo "<H1>Sorry, something bad happened!</H1>\n";
		abnorm_end();
	}
	// Open file for reading
	$source_file=fopen($source_file_name,"r") or exit("Unable to open file!");

	// Begin processing Sources file
	while(!feof($source_file)) {
		
		$line = fgets($source_file);

		if ($line == "\n") {
			$source_info[$src_count]['source'] = $src_source;
			if ($src_binary == "")
				{$source_info[$src_count]['binary'] = $src_source;} else 
				{$source_info[$src_count]['binary'] = $src_binary;}
			$source_info[$src_count]['version'] = $src_version;
			$source_info[$src_count]['architecture'] = $src_architecture;
			$source_info[$src_count]['maintainer'] = $src_maintainer;
			$source_info[$src_count]['directory'] = $src_directory;
			$source_info[$src_count]['files'] = $src_files;
			$source_info[$src_count]['homepage'] = $src_homepage;
			$src_source = "";
			$src_binary = "";
			$src_version = "";
			$src_architecture = "";
			$src_maintainer = "";
			$src_directory = "";
			$src_files = array();
			$file_mode = "";
			$file_count = 0;
			$src_count++;
		} else
		{
			if ($file_mode == "") {
				get_opt_str($line,"Package: ",$src_source);
				get_opt_str($line,"Binary: ",$src_binary);
				get_opt_str($line,"Version: ",$src_version);
				get_opt_str($line,"Architecture: ",$src_architecture);
				get_opt_str($line,"Maintainer: ",$src_maintainer);
				get_opt_str($line,"Directory: ",$src_directory);
				if (strncmp($line,"Files:",strlen("Files:")) == 0) $file_mode = "Y";
			} else {
				if ($file_mode == "Y") {
					$chkfile=substr($line,0,1);
					if ($chkfile == " ") {
						$file=substr(strrchr($line," "),1);
						$size=substr(strrchr($line," "),1);
						$file_str=str_word_count($line,1,"0123456789_-+~.");
						$src_files[$file_count]['name']=rtrim($file);
						$src_files[$file_count]['size']=human_readable_size($file_str[1]);
						$file_count++;
					} else {
						$file_mode = "E";
					}
				}
			}
		}
	}

	// Close file
	fclose($source_file);

	return $source_info;
}

function read_source_description($suite,$component,$arch,$source_info)
{
	global $baserepdir;
	global $baserepdists;
	$source_dsc_info = array();
	$src_source = "";
	$src_binary = "";
	$src_architecture = "";
	$src_version = "";
	$src_maintainer = "";
	$src_files = array();
	$src_homepage = "";
	$src_build_depends = "";
	$src_build_depends_indep = "";
	$src_build_conflicts = "";
	$src_vcs_browser = "";
	$src_vcs_svn = "";
	$file_mode = "";
	$file_count = 0;
	$source_description_file = "";

	foreach ($source_info['files'] AS $file_info) {
		if (preg_match('/^.*(\.dsc)$/',$file_info['name'])) $source_description_file=$file_info['name'];
	}

	$source_dsc_file_name = "$baserepdir" . $source_info['directory'] . "/" . "$source_description_file";
	// Check if correct/existing
	$res=file_exists($source_dsc_file_name);
	if ($res != 1) {
		echo "<H1>Sorry, something bad happened!</H1>\n";
		abnorm_end();
	}
	// Open file for reading
	$source_dsc_file=fopen($source_dsc_file_name,"r") or exit("Unable to open file!");

	// Begin processing Sources file
	while(!feof($source_dsc_file)) {
		
		$line = fgets($source_dsc_file);

		if ($line !== "\n") {
			if ($file_mode == "") {
				get_opt_str($line,"Source: ",$src_source);
				get_opt_str($line,"Binary: ",$src_binary);
				get_opt_str($line,"Version: ",$src_version);
				get_opt_str($line,"Architecture: ",$src_architecture);
				get_opt_str($line,"Maintainer: ",$src_maintainer);
				get_opt_str($line,"Homepage: ",$src_homepage);
				get_opt_str($line,"Build-Depends: ",$src_build_depends);
				get_opt_str($line,"Build-Depends-Indep: ",$src_build_depends_indep);
				get_opt_str($line,"Build-Conflicts: ",$src_build_conflicts);
				get_opt_str($line,"Vcs-Browser: ",$src_vcs_browser);
				get_opt_str($line,"Vcs-Svn: ",$src_vcs_svn);
				if (strncmp($line,"Files:",strlen("Files:")) == 0) $file_mode = "Y";
				// FIXME: Homepage field exists in *.dsc file, but not in Sources
			} else {
				if ($file_mode == "Y") {
					$chkfile=substr($line,0,1);
					if ($chkfile == " ") {
						$file=substr(strrchr($line," "),1);
						$size=substr(strrchr($line," "),1);
						$file_str=str_word_count($line,1,"0123456789_-+~.");
						$src_files[$file_count]['name']=rtrim($file);
						$src_files[$file_count]['size']=human_readable_size($file_str[1]);
						$file_count++;
					} else {
						$file_mode = "E";
					}
				}
			}
		}
	}
	$source_dsc_info['source'] = $source_info['source'];
	if ($src_binary == "")
		{$source_dsc_info['binary'] = $src_source;} else 
		{$source_dsc_info['binary'] = $src_binary;}
	$source_dsc_info['version'] = $src_version;
	$source_dsc_info['architecture'] = $src_architecture;
	$source_dsc_info['maintainer'] = $src_maintainer;
	$source_dsc_info['files'] = $src_files;
	$source_dsc_info['homepage'] = $src_homepage;
	$source_dsc_info['build-depends'] = $src_build_depends;
	$source_dsc_info['build-depends-indep'] = $src_build_depends_indep;
	$source_dsc_info['build-conflicts'] = $src_build_conflicts;
	$source_dsc_info['vcs-browser'] = $src_vcs_browser;
	$source_dsc_info['vcs-svn'] = $src_vcs_svn;

	// Close file
	fclose($source_dsc_file);

	return $source_dsc_info;
}

function print_binary_list($suite,$component,$architecture,$binary_info_list)
{
	if (count($binary_info_list,1) == 0) {
		echo "<H3>No packages found!</H3>\n";
	} else {
		uasort($binary_info_list,"sort_by_binary");
		foreach ($binary_info_list AS $binary) {
			print_binary_list_box_start($suite,$component,$architecture,$binary['binary'],$binary['version']);
			echo "Description: ".$binary['description']."<br/>\n";
			print_binary_list_box_end();
		}
	}
}

function print_source_list($suite,$component,$architecture,$source_info_list)
{
	if (count($source_info_list,1) == 0) {
		echo "<H3>No sources found!</H3>\n";
	} else {
		uasort($source_info_list,"sort_by_source");
		foreach ($source_info_list AS $source) {
			print_source_list_box_start($suite,$component,$architecture,$source['source'],$source['version']);
			echo "Architecture: ".$source['architecture']."<br/>\n";
			print_source_list_box_end();
		}
	}
}

function print_binary_detail($suite,$component,$architecture,$binary,$bmode)
{
	global $PRGFILE;
	global $html_baserepdir;
	global $baserepdir;
	if ($bmode == "fcl") {
		print_binary_detail_box_start("<a href=\"$PRGFILE?s=".$suite."&c=".$component."&a=".$architecture."&p=".$binary['binary']."\">".$binary['binary']."</a>"," Changelog");
		$cmd = "apt-listchanges -f text --save_seen=none -a --which=changelogs ".$baserepdir.$binary['filename']." 2>&1 | grep -v \"^Reading changelogs\\.\\.\\.\\|^mode: debian-changelog$\\|^Local variables:$\\|^add-log-mailing-address \\|^End:$\"";
		$output = shell_exec("$cmd");
		echo "<pre>$output</pre>\n";
	} elseif ($bmode == "fpc") {
		print_binary_detail_box_start("<a href=\"$PRGFILE?s=".$suite."&c=".$component."&a=".$architecture."&p=".$binary['binary']."\">".$binary['binary']."</a>"," content");
		$cmd = "dpkg -c ".$baserepdir.$binary['filename']." 2>&1";
		$output = shell_exec("$cmd");
		echo "<pre>$output</pre>\n";
	} else {
		print_binary_detail_box_start($binary['binary'],"");
		echo "Version: ".$binary['version']."<br/>\n";
		// echo "Source: <a href=\"$PRGFILE?s=$suite&c=$component&a=source&p=".$binary['source']."&v=".urlencode($binary['version'])."\">".$binary['source']."</a><br/>\n";
		echo "Source: <a href=\"$PRGFILE?s=$suite&c=$component&a=source&p=".$binary['source']."\">".$binary['source']."</a><br/>\n";
		echo "Section: ".$binary['section']."<br/>\n";
		echo "Priority: ".$binary['priority']."<br/>\n";
		echo "Maintainer: ".$binary['maintainer']."<br/>\n";
		echo "Description: ".$binary['description']."<br/>\n";
		echo "<a href=\"$PRGFILE?s=".$suite."&c=".$component."&a=".$architecture."&p=".$binary['binary']."&bmode=fcl\">Show full debian changelog</a><br/>\n";
		if ($binary['description_long'] != "") echo "Long description:<br/>".nl2br($binary['description_long'])."\n";
		echo "<hr>";
		echo "File: <a href=\"$html_baserepdir".$binary['filename']."\">".substr(strrchr($binary['filename'],"/"),1)."</a>&nbsp;(".$binary['filesize'].") [<a href=\"$PRGFILE?s=".$suite."&c=".$component."&a=".$architecture."&p=".$binary['binary']."&bmode=fpc\">package contents</a>]<br/>\n";
	}
	print_binary_detail_box_end();
}

function print_source_detail($suite,$component,$architecture,$source,$detailed_source_info)
{
	global $PRGFILE;
	global $html_baserepdir;
	$arch_count = 0;
	$comp_count = 0;
	$bin_count = 0;
	$s_arch_count = 0;
	$s_comp_count = 0;
	$s_bin_count = 0;
	$arch_comp_list = array();
	$bin_list = array();
	$bin_found = 0;

	// -------------- READ INFO FOR BINARY-PACKAGES LISTING -----------------
	// read suite info for existing architectures
	$suite_info = read_suite_release_file($suite);

	// Go trough components/architectures and read the package list
	foreach ($suite_info['architectures'] AS $s_architecture) {
		if ($s_architecture != "source") {
			foreach ($suite_info['components'] AS $s_component) {
				$arch_comp_list[$arch_count]['architecture'] = $s_architecture;
				$arch_comp_list[$arch_count]['component'][$comp_count]['name'] = $s_component;
				$arch_comp_list[$arch_count]['component'][$comp_count]['bin_list'] = read_packages_info($suite,$s_component,$s_architecture);
				$comp_count++;
			}
			$arch_count++;
		}
	}

	for ($s_arch_count = 0; $s_arch_count < $arch_count; $s_arch_count++) {
		for ($s_comp_count = 0; $s_comp_count < $comp_count; $s_comp_count++) {
			foreach(explode(", ",$source['binary']) AS $package){
				$binary_info = find_binary_by_name($package,$arch_comp_list[$s_arch_count]['component'][$s_comp_count]['bin_list']);
				if ($binary_info != "") {
					$bin_list[$bin_count]['architecture'] = $arch_comp_list[$s_arch_count]['architecture'];
					$bin_list[$bin_count]['component'] = $arch_comp_list[$s_arch_count]['component'][$s_comp_count]['name'];
					$bin_list[$bin_count]['name'] = $binary_info['binary'];
					$bin_list[$bin_count]['version'] = $binary_info['version'];
					$bin_count++;
				}
			}
		}
	
	}
	// -------------- END: READ INFO FOR BINARY-PACKAGES LISTING ------------

	print_source_detail_box_start($source['source']);
	echo "Version: ".$source['version']."<br/>\n";
	//echo "Binary: ".$source['binary']."<br/>\n";
	for ($s_arch_count = 0; $s_arch_count < $arch_count; $s_arch_count++) {
		$bin_found_printed = 0;
		//echo "Binary: ".$source['binary']."<br/>\n";
		//echo "Binary (".$arch_comp_list[$s_arch_count]['architecture']."): ";
		foreach(explode(", ",$source['binary']) AS $package){
			$bin_found = -1;
			for ($s_bin_count = 0; ($s_bin_count < $bin_count) && ($bin_found == -1); $s_bin_count++) {
				if (($bin_list[$s_bin_count]['name'] == $package) && ($bin_list[$s_bin_count]['version'] == $source['version']) && ($bin_list[$s_bin_count]['architecture'] == $arch_comp_list[$s_arch_count]['architecture'])) { $bin_found = $s_bin_count; }
			}
			if ($bin_found != -1) {
				if ($bin_found_printed == 0) {
					$bin_found_printed = 1;
					echo "Binary (".$arch_comp_list[$s_arch_count]['architecture']."): ";
				}
				echo "<a href=\"$PRGFILE?s=$suite&c=".$bin_list[$bin_found]['component']."&a=".$bin_list[$bin_found]['architecture']."&p=$package\">".$package."</a>&nbsp;";
			}
			/* Disabled for now, see TODO for print_source_detail and Rel.Notes for 1.1.5
			 else
			{
				echo "$package ";
			} */
		}
		if ($bin_found_printed != 0) {
			echo "<br/>\n";
		}
	}
	echo "Architecture: ".$source['architecture']."<br/>\n";
	echo "Maintainer: ".$source['maintainer']."<br/>\n";
	if ($detailed_source_info == "y") {
		// echo "<a href=\"$PRGFILE?s=".$suite."&c=".$component."&a=".$architecture."&p=".$source['source']."&v=".urlencode($source['version'])."\">Hide detailed source-package info</a><br/>\n";
		echo "<a href=\"$PRGFILE?s=".$suite."&c=".$component."&a=".$architecture."&p=".$source['source']."\">Hide detailed source-package info</a><br/>\n";
		$source_dsc = read_source_description($suite,$component,$architecture,$source);
		if ($source_dsc['homepage'] != "") echo "Homepage: <a href=\"".$source_dsc['homepage']."\">".$source_dsc['homepage']."</a><br/>\n";
		if ($source_dsc['vcs-browser'] != "") echo "Vcs-Browser: <a href=\"".$source_dsc['vcs-browser']."\">".$source_dsc['vcs-browser']."</a><br/>\n";
		if ($source_dsc['vcs-svn'] != "") echo "Vcs-Svn: <a href=\"".$source_dsc['vcs-svn']."\">".$source_dsc['vcs-svn']."</a><br/>\n";
		if ($source_dsc['build-depends'] != "") echo "Build-Depends: ".$source_dsc['build-depends']."<br/>\n";
		if ($source_dsc['build-depends-indep'] != "") echo "Build-Depends-Indep: ".$source_dsc['build-depends-indep']."<br/>\n";
		if ($source_dsc['build-conflicts'] != "") echo "Build-Conflicts: ".$source_dsc['build-conflicts']."<br/>\n";
	} else {
		// echo "<a href=\"$PRGFILE?s=".$suite."&c=".$component."&a=".$architecture."&p=".$source['source']."&v=".urlencode($source['version'])."&d=y\">Show detailed source-package info</a><br/>\n";
		echo "<a href=\"$PRGFILE?s=".$suite."&c=".$component."&a=".$architecture."&p=".$source['source']."&d=y\">Show detailed source-package info</a><br/>\n";
	}
	echo "<hr>\n";
	echo "Files:<br/>\n";
	foreach ($source['files'] AS $file_info) {
		echo "<a href=\"$html_baserepdir".$source['directory']."/".$file_info['name']."\">".$file_info['name']."</a>&nbsp;(".$file_info['size'].")<br/>\n";
	}
	print_source_detail_box_end();
}

function process_suite_components($suite_info)
{
	global $PRGFILE;
	foreach ($suite_info['components'] AS $component) {
		print_component_box_start($component);
		foreach ($suite_info['architectures'] AS $architecture) {
			echo "Architecture: <a href=\"$PRGFILE?s=".$suite_info['suite']."&c=$component&a=$architecture\">$architecture</a><br/>\n";
		}
		print_component_box_end();
	}
}

function print_suites()
{
	global $defined_suites;
	echo "<div style=\"font-size:large\"><p>Following Suites are available:<p></div>\n";
	foreach ($defined_suites AS $suite) {
		process_suite_release("J",$suite,"","","");
	}
}

function print_package_find_error($err_s_index)
{
	//header("HTTP/1.0 404 Not Found");
	header("HTTP/1.0 410 Gone");
	echo "<H3>Sorry, the package could not be found within the $err_s_index index!</H3><br/>\n";
}

function find_binary_by_name($search,$binary_info_list)
{
	$key = "";
	$acount = count($binary_info_list);
	for ($c = 0; ($c < $acount) && ($key == ""); $c++) if ($binary_info_list[$c]['binary'] == $search) $key=$c;
	if ($key !== "") return $binary_info_list[$key];
	return "";
}

function find_source_by_name($search,$version,$source_info_list)
{
	$key = "";
	$acount = count($source_info_list);
	for ($c = 0; ($c < $acount) && ($key == ""); $c++) if ($source_info_list[$c]['source'] == $search && (($version == "") || ($source_info_list[$c]['version'] == "$version"))) $key=$c;
	if ($key !== "") return $source_info_list[$key];
	return "";
}

function repo_listing($suite,$component,$architecture,$package,$version,$detailed_source_info,$bmode,$robot_follow)
{
	if ($suite!="" && $component!="" && $architecture!="" && $package!="" && $detailed_source_info=="y") {
		print_html_start("$package - detailed source view",$robot_follow);
	} else {
		print_html_start("$package",$robot_follow);
	}
	
	if ($suite == "" ) {
		print_suites();
	} else
	{
		if ($component == "" || $architecture == "") {
			$suite_info = process_suite_release("N",$suite,"","","");
			process_suite_components($suite_info);
		} else
		{
			$suite_info = process_suite_release("N",$suite,$component,$architecture,$package);
			if ($package == "") {
				if ($architecture == "source") {
					$source_info_list = read_sources_info($suite,$component,$architecture);
					print_source_list($suite,$component,$architecture,$source_info_list);
				} else
				{
					$binary_info_list = read_packages_info($suite,$component,$architecture);
					print_binary_list($suite,$component,$architecture,$binary_info_list);
				}
			} else
			{
				if ($architecture == "source") {
					$source_info_list = read_sources_info($suite,$component,$architecture);
					$source_info = find_source_by_name($package,$version,$source_info_list);
					if ($source_info == "") {
						print_package_find_error("source");
					} else {
						print_source_detail($suite,$component,$architecture,$source_info,$detailed_source_info);
					}
				} else
				{
					$binary_info_list = read_packages_info($suite,$component,$architecture);
					$binary_info = find_binary_by_name($package,$binary_info_list);
					if ($binary_info == "") {
						print_package_find_error("package");
					} else {
						print_binary_detail($suite,$component,$architecture,$binary_info,$bmode);
					}
				}
			}
		}
	}
	
	print_html_end();
}

function generate_sitemap($i_suite,$i_component,$i_architecture)
{
	global $defined_suites;
	global $baserepdir;
	global $baserepdists;
	global $FULLSCRIPTURL;
	global $CODEPAGE;

	// Determine if complete package index should be generated
	if (($i_suite == "") && ($i_component == "") && ($i_architecture == "")) {
		$fullindex = "y";
		header("HTTP/1.0 410 Gone");
	} else {
		$fullindex = "n";
	}

	// if specific suite/component/arch is requested
	if ($i_suite == "mainindex") { 
		$use_suites = $defined_suites;
		$use_comp = array();
		$use_arch = array();
	} else {
		if ($i_suite != "") { 
			$use_suites = array("$i_suite");
		} else {
			$use_suites = $defined_suites;
		}
		if ($i_component != "") { 
			$use_comp = array("$i_component");
		} else {
			$use_comp = array();
		}
		if ($i_architecture != "") { 
			$use_arch = array("$i_architecture");
		} else {
			$use_arch = array();
		}
	}

	// output XML header content type
	header('Content-type: text/xml; charset=utf-8');

	// output XML start
	echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
	//echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
	echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"' . "\n";
	echo ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' . "\n";
	echo ' xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9' . "\n";
	echo ' http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">' . "\n";

	if (($fullindex == "y") || ($i_suite == "mainindex")) {
		// output search package page
		echo "<url>\n";
		echo '<loc>' . htmlspecialchars($FULLSCRIPTURL . '?search=y', ENT_QUOTES, $CODEPAGE) . "</loc>\n";
		echo "<changefreq>weekly</changefreq>\n";
		echo "<priority>0.5</priority>\n";
		echo "</url>\n";
		echo "\n";

		// output main suite select page
		echo "<url>\n";
		echo '<loc>' . htmlspecialchars($FULLSCRIPTURL, ENT_QUOTES, $CODEPAGE) . "</loc>\n";
		echo "<changefreq>weekly</changefreq>\n";
		echo "<priority>0.7</priority>\n";
		echo "</url>\n";
		echo "\n";
	}

	// go through suites
	foreach ($use_suites AS $suite) {
		if (($fullindex == "y") || ($i_suite == "mainindex")) {
			// output suite index page
			echo "<url>\n";
			echo '<loc>' . htmlspecialchars($FULLSCRIPTURL . '?s=' . $suite , ENT_QUOTES, $CODEPAGE) . "</loc>\n";
			echo "<changefreq>weekly</changefreq>\n";
			echo "<priority>0.6</priority>\n";
			echo "</url>\n";
			echo "\n";
		}

		// if output of components+arches detail end
		if ($i_suite != "mainindex") {
			// read suite release file
			$suite_info = read_suite_release_file($suite);
			$cngdate = date(DATE_W3C,strtotime($suite_info['date']));

			if (count($use_comp) == 0) {$use_comp = $suite_info['components'];}
			if (count($use_arch) == 0) {$use_arch = $suite_info['architectures'];}

			// go through components+arches in suite
			foreach ($use_comp AS $component) {
				foreach ($use_arch AS $architecture) {
					$locurl = htmlspecialchars($FULLSCRIPTURL . "?s=".$suite_info['suite']."&c=$component&a=$architecture", ENT_QUOTES, $CODEPAGE);
					// output component/arch filelist index page
					echo "<url>\n";
					echo '<loc>' . $locurl . "</loc>\n";
					echo '<lastmod>' . $cngdate . "</lastmod>\n";
					echo "<changefreq>daily</changefreq>\n";
					// Only output Priority in fullindex mode. In mainindex mode there could be
					// only one link (the page itself without any packages), and google would
					// print a warning, that all links have the same none-standard priority
					if ($fullindex == "y") {echo "<priority>0.5</priority>\n";}
					echo "</url>\n";
					echo "\n";

					// output packages/sources
					if ($architecture == "source") {
						$source_info_list = read_sources_info($suite,$component,$architecture);
						if (count($source_info_list,1) != 0) {
							//uasort($source_info_list,"sort_by_source");
							foreach ($source_info_list AS $source_info) {
								$package = $source_info['source'];
								foreach ($source_info['files'] AS $file_info) {
									if (preg_match('/^.*(\.dsc)$/',$file_info['name'])) $source_description_file=$file_info['name'];
								}
							
								$source_dsc_file_name = "$baserepdir" . $source_info['directory'] . "/" . "$source_description_file";
								// Check if correct/existing
								$res=file_exists($source_dsc_file_name);
								if ($res == 1) {
									$locurl = htmlspecialchars($FULLSCRIPTURL . "?s=".$suite_info['suite']."&c=$component&a=$architecture&p=$package", ENT_QUOTES, $CODEPAGE);
									$locurld = htmlspecialchars($FULLSCRIPTURL . "?s=".$suite_info['suite']."&c=$component&a=$architecture&p=$package&d=y", ENT_QUOTES, $CODEPAGE);
									// get last modified time
									$lmtime = date(DATE_W3C,filemtime($source_dsc_file_name));

									// output source package page
									echo "<url>\n";
									echo '<loc>' . $locurl . "</loc>\n";
									echo '<lastmod>' . $lmtime . "</lastmod>\n";
									echo "<changefreq>weekly</changefreq>\n";
									echo "<priority>0.4</priority>\n";
									echo "</url>\n";
									echo "\n";
									// output source detailed package page
									echo "<url>\n";
									echo '<loc>' . $locurld . "</loc>\n";
									echo '<lastmod>' . $lmtime . "</lastmod>\n";
									echo "<changefreq>weekly</changefreq>\n";
									echo "<priority>0.4</priority>\n";
									echo "</url>\n";
									echo "\n";
								}
							}
						}
					} else
					{
						$binary_info_list = read_packages_info($suite,$component,$architecture);
						if (count($binary_info_list,1) != 0) {
							//uasort($source_info_list,"sort_by_source");
							foreach ($binary_info_list AS $binary_info) {
								$package = $binary_info['binary'];
							
								$binary_file_name = "$baserepdir" . $binary_info['directory'] . "/" . $binary_info['filename'];
								// Check if correct/existing
								$res=file_exists($binary_file_name);
								if ($res == 1) {
									$locurl = htmlspecialchars($FULLSCRIPTURL . "?s=".$suite_info['suite']."&c=$component&a=$architecture&p=$package", ENT_QUOTES, $CODEPAGE);
									// get last modified time
									$lmtime = date(DATE_W3C,filemtime($binary_file_name));

									// output binary package page
									echo "<url>\n";
									echo '<loc>' . $locurl . "</loc>\n";
									echo '<lastmod>' . $lmtime . "</lastmod>\n";
									echo "<changefreq>weekly</changefreq>\n";
									echo "<priority>0.4</priority>\n";
									echo "</url>\n";
									echo "\n";
								}
							}
						}
					}
				}
			} // go through components+arches in suite end
		} // if output of components+arches detail end

	} // go through suites end

	// output XML end
	echo "</urlset>\n";
}

function generate_sitemap_index()
{
	global $defined_suites;
	global $baserepdir;
	global $baserepdists;
	global $FULLSCRIPTURL;
	global $CODEPAGE;

	// output XML header content type
	header('Content-type: text/xml; charset=utf-8');

	// output XML start
	echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
	echo '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"' . "\n";
	echo 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' . "\n";
	echo 'xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9' . "\n";
	echo 'http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd">' . "\n";


	// output Main Index (containing the Low-Level Pages)
	echo '<sitemap>' . "\n";
	echo '<loc>' . htmlspecialchars($FULLSCRIPTURL . '?sitemap=y&s=mainindex', ENT_QUOTES, $CODEPAGE) . "</loc>\n"; 
	echo "<lastmod>" . date(DATE_W3C) . "</lastmod>\n";
	echo '</sitemap>' . "\n";
	
	
	// go through suites
	foreach ($defined_suites AS $suite) {
		// read suite release file
		$suite_info = read_suite_release_file($suite);
		$cngdate = date(DATE_W3C,strtotime($suite_info['date']));

		// go through components+arches in suite
		foreach ($suite_info['components'] AS $component) {
			foreach ($suite_info['architectures'] AS $architecture) {
				$locurl = htmlspecialchars($FULLSCRIPTURL.'?sitemap=y'."&s=".$suite_info['suite']."&c=$component&a=$architecture", ENT_QUOTES, $CODEPAGE);
				// output component/arch filelist index page
				echo "<sitemap>\n";
				echo '<loc>' . $locurl . "</loc>\n";
				echo '<lastmod>' . $cngdate . "</lastmod>\n";
				echo "</sitemap>\n";
			}
		}
	}
	
	// output XML end
	echo "</sitemapindex>\n";
}

function search_find_package($search_pkg,$search_suite,$search_comp,$search_arch,$search_desc)
{
	global $defined_suites;
	global $baserepdir;
	global $baserepdists;
	global $PRGFILE;
	global $FULLSCRIPTURL;

	// go through suites
	foreach ($defined_suites AS $suite) {
		// check if suite matches
		if (($search_suite=="") || (preg_match("/$search_suite/i",$suite)))
		{
			// read suite release file
			$suite_info = read_suite_release_file($suite);

			// go through components+arches in suite
			foreach ($suite_info['components'] AS $component) {
				if (($search_comp=="") || (preg_match("/$search_comp/i",$component)))
				foreach ($suite_info['architectures'] AS $architecture) {
					if (($search_arch=="") || (preg_match("/$search_arch/i",$architecture)))
					{
						// check packages (source NEEDS package name)
						if ($architecture == "source") {
							if ($search_pkg!="") {
								$source_info_list = read_sources_info($suite,$component,$architecture);
								if (count($source_info_list,1) != 0) {
									uasort($source_info_list,"sort_by_source");
									foreach ($source_info_list AS $source_info) {
										$package = $source_info['source'];
										if (preg_match("/$search_pkg/i",$package)) {
											// output source package page
											print_source_list_box_start($suite,$component,$architecture,$source_info['source'],$source_info['version']);
											echo "Architecture: ".$source_info['architecture']."<br/>\n";
											echo "Suite: ".$suite."<br/>\n";
											print_source_list_box_end();
										}
									}
								}
							}
						} else
						{
							$binary_info_list = read_packages_info($suite,$component,$architecture);
							if (count($binary_info_list,1) != 0) {
								uasort($binary_info_list,"sort_by_binary");
								foreach ($binary_info_list AS $binary_info) {
									$package = $binary_info['binary'];
								
									$binary_file_name = "$baserepdir" . $binary_info['directory'] . "/" . $binary_info['filename'];
									if (($search_pkg=="") || (preg_match("/$search_pkg/i",$package))) {
										if (($search_desc=="") || (preg_match("/$search_desc/i",$binary_info['description'])) || (preg_match("/$search_desc/i",$binary_info['description']))) {
											// output binary package page
											print_binary_list_box_start($suite,$component,$architecture,$package,$binary_info['version']);
											echo "Architecture: ".$architecture."<br/>\n";
											echo "Suite: ".$suite."<br/>\n";
											echo "Description: ".$binary_info['description']."<br/>\n";
											print_binary_list_box_end();
										}
									}
								}
							}
						}
					}
				}
			}
		} // suite search end

	// go through suites end
	}
}

function print_search_page($search_pkg,$search_suite,$search_comp,$search_arch,$search_desc)
{
	global $meta_keywords;
	global $PRGFILE;

	$meta_keywords = $meta_keywords . ', search';
	print_html_start("Package search","N");
?>
	<p><div style="font-size:medium" align="left"><a href="<?php echo "$PRGFILE"; ?>">Back to repository index</a></div></p>
	<form method="GET">
		<table>
			<input type="hidden" name="search" value="y">
			<tr>
				<td align="right">Package:</td>
				<td><input name="spkg" type="text" size="15" maxlength="30" value="<?php echo "$search_pkg" ?>"></td>
				<td align="left">Hint: Source-Package search _must_ have at least a package name!</td>
			</tr>
			<tr>
				<td align="right">Suite:</td>
				<td><input name="ssuite" type="text" size="15" maxlength="30" value="<?php echo "$search_suite" ?>"></td>
			</tr>
			<tr>
				<td align="right">Component:</td>
				<td><input name="scomp" type="text" size="15" maxlength="30" value="<?php echo "$search_comp" ?>"></td>
			</tr>
			<tr>
				<td align="right">Architecture:</td>
				<td><input name="sarch" type="text" size="15" maxlength="30" value="<?php echo "$search_arch" ?>"></td>
				<td align="left">Hint: to search for source+amd64 packages use 'source|amd64'</td>
			</tr>
			<tr>
				<td align="right">Description:</td>
				<td><input name="sdesc" type="text" size="15" maxlength="30" value="<?php echo "$search_desc" ?>"></td>
				<td align="left">Hint: Applies only on non source architectures</td>
			</tr>
			<tr>
				<td colspan=2 align=center><input type="submit" value="Begin search"></td>
			</tr>
		</table>
	</form>
<?php

	if ($search_pkg!="" || $search_suite!="" || $search_comp!="" || $search_arch!="" || $search_desc!="") {search_find_package($search_pkg,$search_suite,$search_comp,$search_arch,$search_desc);}

	print_html_end();
}

// Parse GET/POST data
// Repository Listing
$suite = filter_var($_GET['s'],FILTER_CALLBACK,array("options"=>"remove_slashes"));
$component = filter_var($_GET['c'],FILTER_CALLBACK,array("options"=>"remove_slashes"));
$architecture = filter_var($_GET['a'],FILTER_CALLBACK,array("options"=>"remove_slashes"));
$package = filter_var($_GET['p'],FILTER_CALLBACK,array("options"=>"remove_slashes"));
$version = filter_var($_GET['v'],FILTER_CALLBACK,array("options"=>"remove_slashes"));
$detailed_source_info = filter_var($_GET['d'],FILTER_CALLBACK,array("options"=>"remove_slashes"));
$bmode = filter_var($_GET['bmode'],FILTER_CALLBACK,array("options"=>"remove_slashes"));
// Sitemap
$sitemap = filter_var($_GET['sitemap'],FILTER_CALLBACK,array("options"=>"remove_slashes"));
$sitemap_index = filter_var($_GET['sitemap_index'],FILTER_CALLBACK,array("options"=>"remove_slashes"));
// Search
$search = filter_var($_GET['search'],FILTER_CALLBACK,array("options"=>"remove_slashes"));
$search_pkg = filter_var($_GET['spkg'],FILTER_CALLBACK,array("options"=>"remove_slashes"));
$search_suite = filter_var($_GET['ssuite'],FILTER_CALLBACK,array("options"=>"remove_slashes"));
$search_comp = filter_var($_GET['scomp'],FILTER_CALLBACK,array("options"=>"remove_slashes"));
$search_arch = filter_var($_GET['sarch'],FILTER_CALLBACK,array("options"=>"remove_slashes"));
$search_desc = filter_var($_GET['sdesc'],FILTER_CALLBACK,array("options"=>"remove_slashes"));

//if ($suite == "" || $component == "" || $architecture == "" || $package == "") {
	$robot_follow = "Y";
//} else {
//	$robot_follow = "N";
//}

// Start main
ob_start();

// add info to meta_keywords tag
if ($package != "" ) { $meta_keywords = $meta_keywords . ', ' . $package; }
if ($detailed_source_info != "" ) { $meta_keywords = $meta_keywords . ', detailed'; }

if ($sitemap == "y")	{generate_sitemap($suite,$component,$architecture);}
if ($sitemap_index == "y")	{generate_sitemap_index();}
if ($search == "y")	{print_search_page($search_pkg,$search_suite,$search_comp,$search_arch,$search_desc);}
if (($sitemap != "y") && ($sitemap_index != "y") && ($search != "y"))
{
	repo_listing($suite,$component,$architecture,$package,$version,$detailed_source_info,$bmode,$robot_follow);
}

ob_end_flush();
?>
