summaryrefslogtreecommitdiff
path: root/index.php
blob: dcf9a392309ebc1f4a68efb30387f652cb932cf5 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
# Copyright © 2014, 2016 Luke Shumaker <lukeshu@sbcglobal.net>
# This work is free. You can redistribute it and/or modify it under the
# terms of the Do What The Fuck You Want To Public License, Version 2,
# as published by Sam Hocevar. See the ./COPYING file for more details.

// 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
$asseturl = dirname($_SERVER["SCRIPT_NAME"]);
$assetdir = dirname($_SERVER["SCRIPT_FILENAME"]);
$root = $_SERVER["DOCUMENT_ROOT"];
$dirname = explode("?", $_SERVER["REQUEST_URI"], 2)[0];

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

function normalizeN($filename) {
	$parts = preg_split("|/+|", $filename, -1, PREG_SPLIT_NO_EMPTY);
	$abs = $filename[0] === '/';
	return [ ($abs?'/':'').implode('/', $parts), count($parts) ];
}

function normalize($filename) {
	list ($new_filename, $n) = normalizeN($filename);
	return $new_filename;
}

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

function s_is_type($mode, $mask) { return ($mode & 0170000/*<sys/stat.h>:S_IFMT*/) === $mask; } // <sys/stat.h>:__S_ISTYPE
function stat_is_dir( $stat) { return s_is_type($stat['mode'], 0040000/*<sys/stat.h>:S_IFDIR*/); }
function stat_is_file($stat) { return s_is_type($stat['mode'], 0100000/*<sys/stat.h>:S_IFREG*/); }
function stat_is_link($stat) { return s_is_type($stat['mode'], 0120000/*<sys/stat.h>:S_IFLNK*/); }

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

	list ($dirname, $parts) = normalizeN($dirname);
	$filepath = $root.'/'.$dirname.'/'.$filename;
	$stat = lstat($filepath);

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

	$classes = array();

	if (stat_is_link($stat)) {
		$classes[] = 'link';
		$lstat = $stat;
		$stat = stat($filepath);
	}

	if ($parts == 0) {
		// in the top-level
		if (stat_is_dir($stat) && is_dir($filepath.'/os')) {
			array_push($classes, 'repo');
			if (in_array($filename, $repos_arch_project  )) { array_push($classes, 'arch'); }
			if (in_array($filename, $repos_para_project  )) { array_push($classes, 'para'); }
			if (in_array($filename, $repos_arch_community)) { array_push($classes, 'arch', 'community'); }
			if (in_array($filename, $repos_para_community)) { array_push($classes, 'para', 'community'); }
			if ($filename[0] === '~'                      ) { array_push($classes, '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 (stat_is_dir( $stat)) { $classes[] = 'dir';  }
	if (stat_is_file($stat)) { $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);

	if (isset($lstat)) {
		$stat = $lstat;
	}
	return [ $classes, $stat ];
}

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

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 {
		require $assetdir.'/404.php';
	}
	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="<?php echo $asseturl."/style.css"; ?>" />
	<link rel="icon" type="image/x-icon" href="https://www.parabola.nu/static/favicon.72ab042ac877.ico" />
	<link rel="shortcut icon" type="image/x-icon" href="https://www.parabola.nu/static/favicon.72ab042ac877.ico" />
	<link rel="apple-touch-icon" href="https://www.parabola.nu/static/logos/apple-touch-icon-57x57.6484c8e17e2f.png" />
	<link rel="apple-touch-icon" sizes="72x72" href="https://www.parabola.nu/static/logos/apple-touch-icon-72x72.f60f1458fe1c.png" />
	<link rel="apple-touch-icon" sizes="114x114" href="https://www.parabola.nu/static/logos/apple-touch-icon-114x114.e7845a2e4bab.png" />
	<link rel="apple-touch-icon" sizes="144x144" href="https://www.parabola.nu/static/logos/apple-touch-icon-144x144.97a164b5f85c.png" />
	<link rel="search" type="application/opensearchdescription+xml" href="https://www.parabola.nu/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://www.parabola.nu/" title="Parabola news, packages, projects and more">Home</a></li>
				<li id="anb-packages"><a href="https://www.parabola.nu/packages/" title="Parabola Package Database">Packages</a></li>
				<li id="anb-wiki"><a href="https://wiki.parabola.nu/" 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.parabola.nu/" title="Our Code">Projects</a></li>
				<li id="anb-download"><a href="https://www.parabola.nu/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; }
							list($classes, $stat) = classify($root, $dirname, $filename);
							echo "\t\t\t\t<tr class=\"".implode(' ', $classes)."\">";

							echo "<td>";
							echo "<a href=\"".htmlentities($filename)."\">".htmlentities($filename)."</a>";
							if (stat_is_link($stat)) {
								$target = readlink($root.'/'.$dirname.'/'.$filename);
								echo " -&gt; <a href=\"".htmlentities($target)."\">".htmlentities($target)."</a>";
							}
							echo "</td>";

							if ($filename === '..') {
								echo '<td></td><td></td>';
							} else {
								echo "<td>".date("Y-m-d H:i", $stat['mtime'])."</td>";
								if (stat_is_dir($stat)) {
									echo "<td class=number>-</td>";
								} else {
									echo "<td class=number>".$stat['size']."</td>";
								}
							}
							echo "</tr>\n";
						}
					}
?>
			</tbody></table>
		</div>

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