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
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
class Download extends HTMLController
|
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
// Ensure we're logged in at this point.
|
|
|
|
$user = Registry::get('user');
|
|
|
|
if (!$user->isLoggedIn())
|
|
|
|
throw new NotAllowedException();
|
|
|
|
|
|
|
|
if(!isset($_GET['tag']))
|
|
|
|
throw new UnexpectedValueException('Must specify an album to download');
|
|
|
|
|
|
|
|
$tag = (int)$_GET['tag'];
|
|
|
|
$album = Tag::fromId($tag);
|
|
|
|
|
|
|
|
if($album->kind !== 'Album')
|
|
|
|
throw new UnexpectedValueException('Specified tag does not correspond to an album');
|
|
|
|
|
|
|
|
//Yes TOCTOU but it does not need to be perfect.
|
2019-09-29 14:47:56 +02:00
|
|
|
$lock_file = join('/', [sys_get_temp_dir(), 'pics-export.lock']);
|
2018-12-26 10:31:57 +01:00
|
|
|
if(!file_exists($lock_file))
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$fp = fopen($lock_file, 'x');
|
|
|
|
|
|
|
|
if(!$fp)
|
|
|
|
throw new UnexpectedValueException('Could not open lock-file');
|
2019-09-29 14:47:56 +02:00
|
|
|
|
2018-12-26 10:31:57 +01:00
|
|
|
$this->exportAlbum($album);
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
2019-09-29 14:47:56 +02:00
|
|
|
fclose($fp);
|
2018-12-26 10:31:57 +01:00
|
|
|
unlink($lock_file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw new UnexpectedValueException('Another export is busy, please try again later');
|
|
|
|
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function exportAlbum($album)
|
|
|
|
{
|
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));
|
2018-12-26 10:31:57 +01:00
|
|
|
foreach($album_ids as $album_id)
|
|
|
|
{
|
|
|
|
$iterator = AssetIterator::getByOptions(
|
2019-09-29 14:47:56 +02:00
|
|
|
[
|
2018-12-26 10:31:57 +01:00
|
|
|
'id_tag' => $album_id
|
2019-09-29 14:47:56 +02:00
|
|
|
]
|
2018-12-26 10:31:57 +01:00
|
|
|
);
|
|
|
|
while($asset = $iterator->Next())
|
|
|
|
{
|
2019-09-29 14:47:56 +02:00
|
|
|
$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 = [
|
|
|
|
0 => ['pipe', 'r'],
|
|
|
|
1 => ['pipe', 'w'],
|
|
|
|
];
|
2018-12-26 10:31:57 +01:00
|
|
|
|
|
|
|
$command = 'tar --null -cf - -T -';
|
|
|
|
|
|
|
|
$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');
|
|
|
|
|
|
|
|
$album_name = $album->tag;
|
|
|
|
|
|
|
|
header('Pragma: no-cache');
|
|
|
|
header('Content-Description: File Download');
|
|
|
|
header('Content-disposition: attachment; filename="' . $album_name . '.tar"');
|
|
|
|
header('Content-Type: application/octet-stream');
|
|
|
|
header('Content-Transfer-Encoding: binary');
|
|
|
|
|
|
|
|
foreach($files as $file)
|
|
|
|
fwrite($pipes[0], $file . "\0");
|
|
|
|
|
|
|
|
fclose($pipes[0]);
|
|
|
|
|
|
|
|
while($chunk = stream_get_contents($pipes[1], 4096))
|
|
|
|
echo $chunk;
|
|
|
|
|
|
|
|
fclose($pipes[1]);
|
|
|
|
|
|
|
|
proc_close($proc);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getChildAlbumIds($parent_id)
|
|
|
|
{
|
2019-09-29 14:47:56 +02:00
|
|
|
$ids = [];
|
2018-12-26 10:31:57 +01:00
|
|
|
|
|
|
|
$albums = Tag::getAlbums($parent_id, 0, PHP_INT_MAX, 'object');
|
|
|
|
foreach($albums as $album)
|
|
|
|
{
|
|
|
|
$ids[] = $album->id_tag;
|
|
|
|
$ids = array_merge($ids, $this->getChildAlbumIds($album->id_tag));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ids;
|
|
|
|
}
|
|
|
|
}
|