Skip to content

Commit

Permalink
Cleaned up PR DirectoryLister#96
Browse files Browse the repository at this point in the history
  • Loading branch information
PHLAK committed Aug 20, 2015
1 parent 6378834 commit fb857b0
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 64 deletions.
44 changes: 22 additions & 22 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,33 @@
die($data);

}

if (isset($_GET['zip'])) {

$dirArray = $lister->zipDirectory($_GET['zip']);
} else {



// Initialize the directory array
if (isset($_GET['dir'])) {
$dirArray = $lister->listDirectory($_GET['dir']);
} else {
$dirArray = $lister->listDirectory('.');
}

// Define theme path
if (!defined('THEMEPATH')) {
define('THEMEPATH', $lister->getThemePath());
}
// Initialize the directory array
if (isset($_GET['dir'])) {
$dirArray = $lister->listDirectory($_GET['dir']);
} else {
$dirArray = $lister->listDirectory('.');
}

// Set path to theme index
$themeIndex = $lister->getThemePath(true) . '/index.php';
// Define theme path
if (!defined('THEMEPATH')) {
define('THEMEPATH', $lister->getThemePath());
}

// Set path to theme index
$themeIndex = $lister->getThemePath(true) . '/index.php';

// Initialize the theme
if (file_exists($themeIndex)) {
include($themeIndex);
} else {
die('ERROR: Failed to initialize theme');
}

// Initialize the theme
if (file_exists($themeIndex)) {
include($themeIndex);
} else {
die('ERROR: Failed to initialize theme');
}

}
66 changes: 38 additions & 28 deletions resources/DirectoryLister.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function __construct() {
$this->_themeName = $this->_config['theme_name'];

}

/**
* If it is allowed to zip whole directories
*
Expand All @@ -77,64 +77,75 @@ public function isZipEnabled() {
}
return $this->_config['zip_dirs'];
}

/**
* Creates zipfile of directory
*
* @param string $directory Relative path of directory to list
* @access public
*/
public function zipDirectory($directory) {
if ($this->_config['zip_dirs'])
{

if ($this->_config['zip_dirs']) {

// Cleanup directory path
$directory = $this->setDirectoryPath($directory);
if ($directory != '.' && $this->_isHidden($directory)){

if ($directory != '.' && $this->_isHidden($directory)) {
echo "Access denied.";
}
$filename_no_ext = basename("$directory");
if ( $directory == '.' )
{
$filename_no_ext = "Home";

$filename_no_ext = basename($directory);

if ($directory == '.') {
$filename_no_ext = 'Home';
}

// We deliver a zip file
header("Content-Type: archive/zip");
header('Content-Type: archive/zip');

// Filename for the browser to save the zip file
header("Content-Disposition: attachment; filename=\"$filename_no_ext".".zip\"");
header("Content-Disposition: attachment; filename=\"$filename_no_ext.zip\"");

//change directory so the zip file doesnt have a tree structure in it.
chdir($directory);

// TODO: Probably we have to parse exclude list more carefully
$exclude_list = implode(" ", array_merge($this->_config['hidden_files'],array('index.php')));
$exclude_list = implode(' ', array_merge($this->_config['hidden_files'], array('index.php')));
$exclude_list = str_replace("*", "\*", $exclude_list);
if ($this->_config['zip_stream'])
{

if ($this->_config['zip_stream']) {

// zip the stuff (dir and all in there) into the streamed zip file
$stream = popen( "/usr/bin/zip -".$this->_config['zip_compression_level']." -r -q - * -x ".$exclude_list, "r" );
if( $stream )
{
fpassthru( $stream );
fclose( $stream );
$stream = popen('/usr/bin/zip -' . $this->_config['zip_compression_level'] . ' -r -q - * -x ' . $exclude_list, 'r');

if ($stream) {
fpassthru($stream);
fclose($stream);
}

} else {

// zip the stuff (dir and all in there) into the tmp_zip file
exec('zip -'.$this->_config['zip_compression_level'].' -r '.$tmp_zip.' * -x '.$exclude_list);
exec('zip -' . $this->_config['zip_compression_level'] . ' -r ' . $tmp_zip . ' * -x ' . $exclude_list);

// get a tmp name for the .zip
$tmp_zip = tempnam ("tmp", "tempzip") . ".zip";
$tmp_zip = tempnam('tmp', 'tempzip') . '.zip';

// calc the length of the zip. it is needed for the progress bar of the browser
$filesize = filesize($tmp_zip);
header("Content-Length: $filesize");

// deliver the zip file
$fp = fopen("$tmp_zip","r");
$fp = fopen($tmp_zip, 'r');
echo fpassthru($fp);

// clean up the tmp zip file
unlink($tmp_zip);

}
}

}


Expand Down Expand Up @@ -416,7 +427,7 @@ public function setDirectoryPath($path = null) {
return $this->_directory;

}

/**
* Get directory path variable
*
Expand All @@ -425,7 +436,6 @@ public function setDirectoryPath($path = null) {
*/
public function getDirectoryPath() {
return $this->_directory;

}


Expand Down
19 changes: 11 additions & 8 deletions resources/default.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
'footer.php'
),

// Files that, if present in a directory, make the directory a direct link
// rather than a browse link.
// Files that, if present in a directory, make the directory
// a direct link rather than a browse link.
'index_files' => array(
'index.htm',
'index.html',
Expand All @@ -35,15 +35,18 @@
'reverse_sort' => array(
// 'path/to/folder'
),

// Allow to download directories as zip files
'zip_dirs' => false,
// Stream zip file content directly to the client, without any temporary file
'zip_stream' => true,
'zip_compression_level' => 0,
'zip_dirs' => false,

// Stream zip file content directly to the client,
// without any temporary file
'zip_stream' => true,

'zip_compression_level' => 0,

// Disable zip downloads for particular directories
'zip_disable' => array(
'.' // - disable for root directory by default
// 'path/to/folder'
),

Expand Down
23 changes: 17 additions & 6 deletions resources/themes/bootstrap/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,25 @@
</p>

<div class="navbar-right">
<?php if ($lister->isZipEnabled()): ?>
<ul id="page-top-download-all" class="nav navbar-nav">
<li><a href="?zip=<?php echo $lister->getDirectoryPath() ?>" id="download-all-link"><i class="fa fa-download fa-lg"></i></a></li>
</ul>
<?php endif; ?>

<ul id="page-top-nav" class="nav navbar-nav">
<li><a href="javascript:void(0)" id="page-top-link"><i class="fa fa-arrow-circle-up fa-lg"></i></a></li>
<li>
<a href="javascript:void(0)" id="page-top-link">
<i class="fa fa-arrow-circle-up fa-lg"></i>
</a>
</li>
</ul>

<?php if ($lister->isZipEnabled()): ?>
<ul id="page-top-download-all" class="nav navbar-nav">
<li>
<a href="?zip=<?php echo $lister->getDirectoryPath(); ?>" id="download-all-link">
<i class="fa fa-download fa-lg"></i>
</a>
</li>
</ul>
<?php endif; ?>

</div>

</div>
Expand Down

0 comments on commit fb857b0

Please sign in to comment.