2018-12-26 10:31:57 +01:00
|
|
|
<?php
|
|
|
|
/*****************************************************************************
|
|
|
|
* Download.php
|
|
|
|
* Contains the code to download an album.
|
|
|
|
*
|
|
|
|
* Kabuki CMS (C) 2013-2019, Aaron van Geffen
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2020-03-01 17:00:18 +01:00
|
|
|
class Download
|
2018-12-26 10:31:57 +01:00
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
// Ensure we're logged in at this point.
|
|
|
|
$user = Registry::get('user');
|
|
|
|
if (!$user->isLoggedIn())
|
|
|
|
throw new NotAllowedException();
|
|
|
|
|
2020-03-01 17:00:18 +01:00
|
|
|
if (!isset($_GET['tag']))
|
|
|
|
throw new UserFacingException('No album or tag has been specified for download.');
|
2018-12-26 10:31:57 +01:00
|
|
|
|
|
|
|
$tag = (int)$_GET['tag'];
|
|
|
|
$album = Tag::fromId($tag);
|
|
|
|
|
2020-03-01 17:00:18 +01:00
|
|
|
if (isset($_SESSION['current_export']))
|
2020-03-11 18:57:31 +01:00
|
|
|
throw new UserFacingException('You can only export one album at the same time. Please wait until the other download finishes, or try again later.');
|
2018-12-26 10:31:57 +01:00
|
|
|
|
2020-03-01 17:00:18 +01:00
|
|
|
// So far so good?
|
|
|
|
$this->exportAlbum($album);
|
|
|
|
exit;
|
2018-12-26 10:31:57 +01:00
|
|
|
}
|
|
|
|
|
2020-03-01 17:07:10 +01:00
|
|
|
private function exportAlbum(Tag $album)
|
2018-12-26 10:31:57 +01:00
|
|
|
{
|
2019-09-29 14:47:56 +02:00
|
|
|
$files = [];
|
2018-12-26 10:31:57 +01:00
|
|
|
|
2019-09-29 14:47:56 +02:00
|
|
|
$album_ids = array_merge([$album->id_tag], $this->getChildAlbumIds($album->id_tag));
|
2020-03-11 12:13:04 +01:00
|
|
|
foreach ($album_ids as $album_id)
|
2018-12-26 10:31:57 +01:00
|
|
|
{
|
2020-03-01 17:07:10 +01:00
|
|
|
$iterator = AssetIterator::getByOptions(['id_tag' => $album_id]);
|
|
|
|
while ($asset = $iterator->next())
|
|
|
|
$files[] = join(DIRECTORY_SEPARATOR, [$asset->getSubdir(), $asset->getFilename()]);
|
2018-12-26 10:31:57 +01:00
|
|
|
}
|
|
|
|
|
2019-09-29 14:47:56 +02:00
|
|
|
$descriptorspec = [
|
2020-03-11 12:13:04 +01:00
|
|
|
0 => ['pipe', 'r'], // STDIN
|
|
|
|
1 => ['pipe', 'w'], // STDOUT
|
2019-09-29 14:47:56 +02:00
|
|
|
];
|
2018-12-26 10:31:57 +01:00
|
|
|
|
2020-03-11 18:57:31 +01:00
|
|
|
// Prevent simultaneous exports.
|
|
|
|
$_SESSION['current_export'] = $album->id_tag;
|
|
|
|
|
|
|
|
// Allow new exports if the connection is terminated unexpectedly (e.g. when a user aborts a download).
|
|
|
|
register_shutdown_function(function() {
|
|
|
|
if (isset($_SESSION['current_export']))
|
|
|
|
unset($_SESSION['current_export']);
|
|
|
|
});
|
|
|
|
|
2020-03-01 17:07:10 +01:00
|
|
|
$command = 'tar -cf - -C ' . escapeshellarg(ASSETSDIR) . ' --null -T -';
|
2018-12-26 10:31:57 +01:00
|
|
|
|
|
|
|
$proc = proc_open($command, $descriptorspec, $pipes, ASSETSDIR);
|
|
|
|
|
|
|
|
if(!$proc)
|
|
|
|
throw new UnexpectedValueException('Could not execute TAR command');
|
|
|
|
|
|
|
|
if(!$pipes[0])
|
|
|
|
throw new UnexpectedValueException('Could not open pipe for STDIN');
|
|
|
|
|
|
|
|
if(!$pipes[1])
|
|
|
|
throw new UnexpectedValueException('Could not open pipe for STDOUT');
|
|
|
|
|
2020-03-11 12:13:04 +01:00
|
|
|
// STDOUT should not block.
|
|
|
|
stream_set_blocking($pipes[1], 0);
|
2018-12-26 10:31:57 +01:00
|
|
|
|
|
|
|
header('Pragma: no-cache');
|
|
|
|
header('Content-Description: File Download');
|
2020-03-11 12:13:04 +01:00
|
|
|
header('Content-disposition: attachment; filename="' . $album->tag . '.tar"');
|
2018-12-26 10:31:57 +01:00
|
|
|
header('Content-Type: application/octet-stream');
|
|
|
|
header('Content-Transfer-Encoding: binary');
|
|
|
|
|
2020-03-11 12:13:04 +01:00
|
|
|
// Write filenames to include to STDIN, separated by null bytes.
|
|
|
|
foreach ($files as $file)
|
2018-12-26 10:31:57 +01:00
|
|
|
fwrite($pipes[0], $file . "\0");
|
|
|
|
|
2020-03-11 12:13:04 +01:00
|
|
|
// Close STDIN pipe to start archiving.
|
2018-12-26 10:31:57 +01:00
|
|
|
fclose($pipes[0]);
|
|
|
|
|
2020-03-11 12:31:31 +01:00
|
|
|
// At this point, end output buffering so we can enjoy more than ~62MB of photos.
|
|
|
|
ob_end_flush();
|
|
|
|
|
2020-03-11 12:13:04 +01:00
|
|
|
do
|
|
|
|
{
|
|
|
|
// Read STDOUT as `tar` is doing its work.
|
|
|
|
echo stream_get_contents($pipes[1], 4096);
|
|
|
|
|
|
|
|
// Are we still running?
|
|
|
|
$status = proc_get_status($proc);
|
|
|
|
}
|
|
|
|
while (!empty($status) && $status['running']);
|
2018-12-26 10:31:57 +01:00
|
|
|
|
2020-03-11 12:13:04 +01:00
|
|
|
// Close STDOUT pipe and clean up process.
|
2018-12-26 10:31:57 +01:00
|
|
|
fclose($pipes[1]);
|
|
|
|
|
|
|
|
proc_close($proc);
|
2020-03-11 18:57:31 +01:00
|
|
|
|
|
|
|
// Allow new exports from this point onward.
|
|
|
|
unset($_SESSION['current_export']);
|
2018-12-26 10:31:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getChildAlbumIds($parent_id)
|
|
|
|
{
|
2019-09-29 14:47:56 +02:00
|
|
|
$ids = [];
|
2018-12-26 10:31:57 +01:00
|
|
|
|
2020-03-01 17:09:58 +01:00
|
|
|
$albums = Tag::getAlbums($parent_id, 0, PHP_INT_MAX);
|
|
|
|
foreach ($albums as $album)
|
2018-12-26 10:31:57 +01:00
|
|
|
{
|
2020-03-01 17:09:58 +01:00
|
|
|
$ids[] = $album['id_tag'];
|
|
|
|
$ids = array_merge($ids, $this->getChildAlbumIds($album['id_tag']));
|
2018-12-26 10:31:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $ids;
|
|
|
|
}
|
|
|
|
}
|