summaryrefslogtreecommitdiff
path: root/index.php
blob: 2c06ca3554f785323ec2e59ed0ea399488b2a20a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
// Configuration
$repos_arch_project = [ 'core', 'extra', 'staging', 'testing', 'gnome-unstable', 'kde-unstable' ];
$repos_arch_community = [ 'community', 'community-staging', 'community-testing',
                          'multilib',  'multilib-staging',  'multilib-testing' ];
$repos_para_project = [ 'libre', 'libre-testing',
                        'libre-multilib', 'libre-multilib-testing' ];
$repos_para_community = [ 'cross', 'java', 'kernels', 'nonprism', 'pcr' ];

// Automatic configuration
$assetdir = dirname($_SERVER["SCRIPT_NAME"]);
$root = $_SERVER["DOCUMENT_ROOT"];
$dirname = explode("?", $_SERVER["REQUEST_URI"], 2)[0];

////////////////////////////////////////////////////////////////////////////////

function show_404() {
	// This is the template used by Nginx internally;
	// if you want something else, have Nginx intercept it.
	// Separation of concerns.
	?><html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center><?php echo $_SERVER["SERVER_SOFTWARE"]; ?></center>
</body>
</html>
<?php
}

function is_root($root, $dirname) {
	$path = $root.'/'.$dirname;

	$parts = preg_split("|/+|", $root, -1, PREG_SPLIT_NO_EMPTY);
	$root = implode('/', $parts);

	$parts = preg_split("|/+|", $path, -1, PREG_SPLIT_NO_EMPTY);
	$path = implode('/', $parts);

	return ($path === $root);
}

function classify($root, $dirname, $filename) {
	global $repos_arch_project, $repos_arch_community, $repos_para_project, $repos_para_community;

	if ($filename === '.') { return [ 'dir' ]; }
	if ($filename === '..') { return [ 'dir', 'parent' ]; }

	$parts = preg_split("|/+|", $dirname, -1, PREG_SPLIT_NO_EMPTY);
	$dirname = implode('/', $parts);

	$classes = array();
	if (count($parts) == 0) {
		// in the top-level
		if (in_array($filename, $repos_arch_project  )) { array_push($classes, 'repo', 'arch'); }
		if (in_array($filename, $repos_para_project  )) { array_push($classes, 'repo', 'para'); }
		if (in_array($filename, $repos_arch_community)) { array_push($classes, 'repo', 'arch', 'community'); }
		if (in_array($filename, $repos_para_community)) { array_push($classes, 'repo', 'para', 'community'); }
		if ($filename[0] === '~'                      ) { array_push($classes, 'repo', 'para', 'community'); }
	} elseif ($dirname == "pool" || $dirname == "sources") {
		switch($filename) {
		case "packages":  array_push($classes, 'pool', 'arch'             ); break;
		case "community": array_push($classes, 'pool', 'arch', 'community'); break;
		case "parabola":  array_push($classes, 'pool', 'para'             ); break;
		}
	}
	if ($filename[0] === '.' || substr($filename, -1) === '~') { $classes[] = 'hidden'; }
	if (is_link($root.'/'.$dirname.'/'.$filename)) { $classes[] = 'link'; }
	if (is_dir( $root.'/'.$dirname.'/'.$filename)) { $classes[] = 'dir';  }
	if (is_file($root.'/'.$dirname.'/'.$filename)) { $classes[] = 'file'; }

	if (preg_match("/\.pkg\.tar(\..z)?$/" , $filename) == 1) { $classes[] = 'pkg'; }
	if (preg_match("/\.tar(\..z|\.bz2)?$/", $filename) == 1) { $classes[] = 'tar'; }
	$classes[] = pathinfo($filename, PATHINFO_EXTENSION);

	return $classes;
}

////////////////////////////////////////////////////////////////////////////////

if (!is_dir($root.'/'.$dirname)) {
	if (is_file($root.'/'.$dirname)) {
		header('Content-Type: text/plain'); // gross, but if Nginx is "properly" configured, this only happens when serving itself, which is text
		readfile($root.'/'.$dirname);
	} else {
		header("HTTP/1.0 404 Not Found");
		show_404();
	}
	exit(0);
}

////////////////////////////////////////////////////////////////////////////////
?>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8" />
	<title>Parabola GNU/Linux-libre - Index of <?php echo htmlentities($dirname); ?></title>
	<link rel="stylesheet" type="text/css" href="https://parabolagnulinux.org/static/archweb.3b36d2301f70.css" media="screen, projection" />
	<link rel="stylesheet" type="text/css" href="<?php echo $assetdir."/style.css"; ?>" />
	<link rel="icon" type="image/x-icon" href="https://parabolagnulinux.org/static/favicon.72ab042ac877.ico" />
	<link rel="shortcut icon" type="image/x-icon" href="https://parabolagnulinux.org/static/favicon.72ab042ac877.ico" />
	<link rel="apple-touch-icon" href="https://parabolagnulinux.org/static/logos/apple-touch-icon-57x57.6484c8e17e2f.png" />
	<link rel="apple-touch-icon" sizes="72x72" href="https://parabolagnulinux.org/static/logos/apple-touch-icon-72x72.f60f1458fe1c.png" />
	<link rel="apple-touch-icon" sizes="114x114" href="https://parabolagnulinux.org/static/logos/apple-touch-icon-114x114.e7845a2e4bab.png" />
	<link rel="apple-touch-icon" sizes="144x144" href="https://parabolagnulinux.org/static/logos/apple-touch-icon-144x144.97a164b5f85c.png" />
	<link rel="search" type="application/opensearchdescription+xml" href="https://parabolagnulinux.org/opensearch/packages/" title="Parabola GNU/Linux-libre Packages" />
</head>
<body>

	<div id="archnavbar" class="anb-repo">
		<div id="archnavbarlogo"><h1><a href="/" title="Return to the main page">Parabola</a></h1></div>
		<div id="archnavbarmenu">
			<ul id="archnavbarlist">
				<li id="anb-home"><a href="https://parabolagnulinux.org" title="Parabola news, packages, projects and more">Home</a></li>
				<li id="anb-packages"><a href="https://parabolagnulinux.org/packages/" title="Parabola Package Database">Packages</a></li>
				<li id="anb-wiki"><a href="https://wiki.parabolagnulinux.org" title="Community documentation">Wiki</a></li>
				<li id="anb-bugs"><a href="https://labs.parabola.nu" title="Issue Tracker">Bugs</a></li>
				<li id="anb-projects"><a href="https://projects.parabolagnulinux.org" title="Our Code">Projects</a></li>
				<li id="anb-download"><a href="https://parabolagnulinux.org/download" title="Get Parabola">Download</a></li>
			</ul>
		</div>
	</div><!-- #archnavbar -->

	<div id="content">

		<div id="archdev-navbar"> </div>

		<div class="box">
			<h2>Index of <?php echo htmlentities($dirname); ?></h2>
			<table><tbody>
<?php
					if ($dh = opendir($root.'/'.$dirname)) {
						$filenames = array();
						while (($filename = readdir($dh)) !== false) { $filenames[] = $filename; }
						closedir($dh);
						sort($filenames);
						foreach ($filenames as $filename) {
							if ($filename === '.') { continue; }
							if ($filename === '..' && is_root($root,$dirname)) { continue; }
							$classes = classify($root, $dirname, $filename);
							echo "\t\t\t\t<tr class=\"".implode(' ', $classes)."\">";
							if (is_link($root.'/'.$dirname.'/'.$filename)) {
								echo "<td><a href=\"".htmlentities($filename)."\">".htmlentities($filename)."</a> -> ".htmlentities(readlink($root.'/'.$dirname.'/'.$filename))."</td>";
							} else {
								echo "<td><a href=\"".htmlentities($filename)."\">".htmlentities($filename)."</a></td>";
							}
							
							if ($filename == '..') {
								echo '<td></td><td></td>';
							} else {
								echo "<td>".date("Y-m-d H:i", filemtime($root.'/'.$dirname.'/'.$filename))."</td>";
								if (is_dir($root.'/'.$dirname.'/'.$filename)) {
									echo "<td class=number>-</td>";
								} else {
									echo "<td class=number>".filesize($root.'/'.$dirname.'/'.$filename)."</td>";
								}
								echo "</tr>\n";
							}
						}
					}
?>
			</tbody></table>
		</div>

	</div>
</body>
</html>