filesAlias/_public.php

90 lines
2.6 KiB
PHP
Raw Normal View History

2022-12-04 15:32:11 +00:00
<?php
/**
* @brief filesAlias, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Osku and contributors
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('DC_RC_PATH')) {
return null;
}
dcCore::app()->tpl->setPath(dcCore::app()->tpl->getPath(), __DIR__ . '/default-templates');
dcCore::app()->tpl->addValue('fileAliasURL', ['templateAlias','fileAliasURL']);
2022-12-04 15:32:11 +00:00
class templateAlias
{
public static function fileAliasURL($attr)
{
$f = dcCore::app()->tpl->getFilters($attr);
return '<?php echo ' . sprintf($f, 'dcCore::app()->blog->url.dcCore::app()->url->getBase("filesalias")."/".dcCore::app()->ctx->filealias->filesalias_url') . '; ?>';
}
2022-12-04 15:32:11 +00:00
}
class urlFilesAlias extends dcUrlHandlers
{
public static function alias($args)
{
$delete = false;
2022-12-21 23:09:55 +00:00
dcCore::app()->ctx->__set('filealias', dcCore::app()->__get('filealias')->getAlias($args));
2022-12-21 23:09:55 +00:00
if (dcCore::app()->ctx->__get('filealias')->isEmpty()) {
self::p404();
}
2022-12-21 23:09:55 +00:00
if (dcCore::app()->ctx->__get('filealias')->filesalias_disposable) {
$delete = true;
}
2022-12-21 23:09:55 +00:00
if (dcCore::app()->ctx->__get('filealias')->filesalias_password) {
# Check for match
2022-12-21 23:09:55 +00:00
if (!empty($_POST['filepassword']) && $_POST['filepassword'] == dcCore::app()->ctx->__get('filealias')->filesalias_password) {
self::servefile(dcCore::app()->ctx->__get('filealias')->filesalias_destination, $args, $delete);
} else {
self::serveDocument('file-password-form.html', 'text/html', false);
return;
}
} else {
2022-12-21 23:09:55 +00:00
self::servefile(dcCore::app()->ctx->__get('filealias')->filesalias_destination, $args, $delete);
}
}
public static function servefile($target, $alias, $delete = false)
{
$a = new aliasMedia();
$media = $a->getMediaId($target);
if (empty($media)) {
self::p404();
}
$file = dcCore::app()->media->getFile($media);
if (empty($file->file)) {
self::p404();
}
header('Content-type: ' . $file->type);
header('Content-Length: ' . $file->size);
header('Content-Disposition: attachment; filename="' . $file->basename . '"');
if (ob_get_length() > 0) {
ob_end_clean();
}
flush();
readfile($file->file);
if ($delete) {
2022-12-21 23:09:55 +00:00
dcCore::app()->__get('filealias')->deleteAlias($alias);
}
}
}