<?
$extensies = Array();

function ParseDir ($rootName = ".", $dirName = "/") {
	global $extensies, $globaldir, $XPIndexDir, $UserDirs, $ApacheUserDir;
	$globaldir = rawurldecode($rootName . $dirName);

	$d = @dir($globaldir);

	if ($d === false) {
		$requested = substr($dirName, 2);
		$user = substr($requested, 0, strpos($requested, '/'));
		$requested = substr($requested, strpos($requested, '/'));

		$globaldir = rawurldecode($UserDirs . '/' . $user . '/' . $ApacheUserDir . $requested);

		$d = dir($globaldir);
	}

	while($entry = $d->read()) {
		if ((($entry[0] != '.') || ($entry == '..')) && ('/' . $entry != $XPIndexDir)) {
			if (is_dir($globaldir."/".$entry)) {
				if ($entry == '..') {
					$result_array[$entry] = '000000000';
					$extensies['000000000'] = '';
				} else {
					$result_array[$entry] = '000000001';
					$extensies['000000001'] = '';
				}
			} else {
				$filename = split('\.', $entry);
				$filename = array_reverse($filename);
				$filename[0] = strtolower($filename[0]);
				$result_array[$entry] = $filename[0];
				$extensies[$filename[0]] = '';
			}
		}
	}

	$d->close();

	ksort($extensies);
	asort($result_array);

	array_walk ($result_array, 'groupdir');
	array_walk ($extensies, 'printgroup');
}

function display_perms($mode)
   {
      /* Determine Type */
      if( $mode & 0x1000 )
         $type='p'; /* FIFO pipe */
      else if( $mode & 0x2000 )
         $type='c'; /* Character special */
      else if( $mode & 0x4000 )
         $type='d'; /* Directory */
      else if( $mode & 0x6000 )
         $type='b'; /* Block special */
      else if( $mode & 0x8000 )
         $type='-'; /* Regular */
      else if( $mode & 0xA000 )
         $type='l'; /* Symbolic Link */
      else if( $mode & 0xC000 )
         $type='s'; /* Socket */
      else
         $type='u'; /* UNKNOWN */

      /* Determine permissions */
      $owner["read"]    = ($mode & 00400) ? 'r' : '-';
      $owner["write"]   = ($mode & 00200) ? 'w' : '-';
      $owner["execute"] = ($mode & 00100) ? 'x' : '-';
      $group["read"]    = ($mode & 00040) ? 'r' : '-';
      $group["write"]   = ($mode & 00020) ? 'w' : '-';
      $group["execute"] = ($mode & 00010) ? 'x' : '-';
      $world["read"]    = ($mode & 00004) ? 'r' : '-';
      $world["write"]   = ($mode & 00002) ? 'w' : '-';
      $world["execute"] = ($mode & 00001) ? 'x' : '-';

      /* Adjust for SUID, SGID and sticky bit */
      if( $mode & 0x800 ) $owner["execute"] = ($owner[execute]=='x') ? 's' : 'S';
      if( $mode & 0x400 ) $group["execute"] = ($group[execute]=='x') ? 's' : 'S';
      if( $mode & 0x200 ) $world["execute"] = ($world[execute]=='x') ? 't' : 'T';

      printf("%1s", $type);
      printf("%1s%1s%1s", $owner[read], $owner[write], $owner[execute]);
      printf("%1s%1s%1s", $group[read], $group[write], $group[execute]);
      printf("%1s%1s%1s", $world[read], $world[write], $world[execute]);
   }

function groupdir($extensie, $file) {
	global $extensies;
	$extensies[$extensie][$file] = '';
}

function printgroup($files, $group) {
	global $XPIndexLanguage, $knownfiletypes, $captions;

	if (array_key_exists($group, $knownfiletypes)) {
		$nicegroup = $knownfiletypes[$group][$XPIndexLanguage];
	} else {
		$nicegroup = strtoupper($group) . "-" . $captions['file'][$XPIndexLanguage];
	}
?>				<tr>
					<td colspan="7" class="groupheader"><?=$nicegroup?></td>
				</tr>
<?
	ksort($files);
	array_walk ($files, 'printfile', $group);
?>				<tr>
					<td colspan="7">&nbsp;</td>
				</tr>
<?
}

function printfile($empty, $file, $group) {
	global $XPIndexDir, $XPIndexLanguage, $icons, $knownfiletypes, $globaldir, $captions;

	if (array_key_exists($group, $knownfiletypes)) {
		$nicegroup = $knownfiletypes[$group][$XPIndexLanguage];
	} else {
		$nicegroup = strtoupper($group) . "-" . $captions['file'][$XPIndexLanguage];
	}

	if (array_key_exists($group, $icons)) {
		$icon = $XPIndexDir . '/icons/' . $icons[$group];
	} else {
		$icon = $XPIndexDir . '/icons/none.png';
	}

	$fileowner = posix_getpwuid(fileowner($globaldir . $file));
	$filegroup = posix_getgrgid(filegroup($globaldir . $file));
	$fileperms = fileperms($globaldir . $file);
?>				<tr>
					<td class="left">
						<a href="./<?=$file?>" title="<?=$file?>" class="icon"><img src="<?=$icon?>" class="icon" alt="<?=$file?>" /></a><a href="./<?=$file?>" title="<?=$file?>"><?=$file?></a>
					</td>
					<td class="right"><?=number_format(filesize($globaldir . $file)/1024, 0, ',', '.')?> kB</td>
					<td class="left"><?=$nicegroup?></td>
					<td class="left"><?=date("d-m-Y H:m", filemtime($globaldir . $file))?></td>
					<td class="left"><?=$fileowner['name']?></td>
					<td class="left"><?=$filegroup['name']?></td>
					<td class="left"><?=display_perms($fileperms)?></td>
				</tr>
<?
}
?>
