lib.php 2.43 KB
Newer Older
Camil Staps's avatar
Camil Staps committed
1 2
<?php
define('CLEANHOME', '/opt/clean');
3
define('CLEANLIB', CLEANHOME . '/lib');
Camil Staps's avatar
Camil Staps committed
4

5 6
$lib = isset($_GET['lib']) ? $_GET['lib'] : 'StdEnv';
$lib = preg_replace('/[^\\w\\/\\-]/', '', $lib);
Camil Staps's avatar
Camil Staps committed
7

8 9 10 11 12 13 14
$ignored_files = [
	'_startup',
	'_library',
	'_startupProfile',
	'_startupTrace',
	'_system'];

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
function containsModules($dir) {
	global $ignored_files;
	try {
		$d = new DirectoryIterator($dir);
		foreach ($d as $f) {
			if ($f->isDot())
				continue;
			if ($f->isDir() && containsModules($dir . '/' . $f->getFilename()))
				return true;
			else if ($f->isFile() && $f->getExtension() == 'dcl' &&
					!in_array($f->getBasename('.dcl'), $ignored_files))
				return true;
		}
	} catch (Exception $e) { }
	return false;
}
Camil Staps's avatar
Camil Staps committed
31 32

function getDirsAndModules($dir) {
33 34
	global $ignored_files;
	global $ignored_dirs;
Camil Staps's avatar
Camil Staps committed
35 36 37 38 39 40 41 42
	$ds = [];
	$ms = [];

	try {
		$d = new DirectoryIterator($dir);
		foreach ($d as $f) {
			if ($f->isDot())
				continue;
43
			if ($f->isDir() && containsModules($dir . '/' . $f->getFilename()))
Camil Staps's avatar
Camil Staps committed
44
				$ds[] = $f->getFilename();
45 46
			else if ($f->getExtension() == 'dcl' &&
					!in_array($f->getBasename('.dcl'), $ignored_files))
Camil Staps's avatar
Camil Staps committed
47 48 49
				$ms[] = $f->getBasename('.dcl');
		}

50 51
		natcasesort($ds);
		natcasesort($ms);
Camil Staps's avatar
Camil Staps committed
52 53 54 55 56 57 58 59 60 61 62 63
	} catch (Exception $e) {
		echo 'Failed to get directory ' . $dir;
	}

	return ['dirs' => $ds, 'modules' => $ms];
}

function makeBrowser($dir, $basemodule) {
	echo '<div class="browser togglee">';
	$elems = getDirsAndModules($dir);

	foreach ($elems['dirs'] as $d) {
64 65
		echo '<div class="browser-item directory toggle-container" data-name="' . $d . '">' .
				'<span class="toggler">' .
Camil Staps's avatar
Camil Staps committed
66 67
					'<span class="toggle-icon">&#x229e</span>' .
					'<span class="title">' . $d . '</span></span>';
Camil Staps's avatar
Camil Staps committed
68 69 70 71 72 73
		makeBrowser($dir . '/' . $d, $basemodule . $d . '.');
		echo '</div>';
	}

	foreach ($elems['modules'] as $m) {
		$fullm = $basemodule . $m;
74
		echo '<div class="browser-item module" data-name="' . $m . '">' . $m . '</div>';
Camil Staps's avatar
Camil Staps committed
75 76 77 78 79
	}

	echo '</div>';
}

80
$groups = json_decode(file_get_contents('/var/libs.json'), true);
Camil Staps's avatar
Camil Staps committed
81

82
echo '<div class="browser">';
83
foreach ($groups as $group => $libs) {
84 85
	echo '<h4 class="browser-header">' . $group . '</h4>';
	foreach ($libs as $lib) {
86
		echo '<div class="browser-item directory toggle-container" data-name="' . $lib['name'] . '">' .
87 88
				'<span class="toggler">' .
					'<span class="toggle-icon">&#x229e</span>' .
89 90
					'<span class="title">' . $lib['name'] . '</span></span>';
		makeBrowser(CLEANLIB . '/' . $lib['name'], '');
91 92 93 94
		echo '</div>';
	}
}
echo '</div>';