use namespace

master
Jean-Christian Paul Denis 2023-03-26 15:37:32 +02:00
parent 7880918940
commit 84d2e74487
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951
11 changed files with 564 additions and 319 deletions

View File

@ -16,5 +16,6 @@ if (!defined('DC_RC_PATH')) {
class initFilesAlias class initFilesAlias
{ {
/** @var string This plugin table name */
public const ALIAS_TABLE_NAME = 'filesalias'; public const ALIAS_TABLE_NAME = 'filesalias';
} }

View File

@ -10,27 +10,53 @@
* @copyright Jean-Christian Denis * @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_CONTEXT_ADMIN')) { declare(strict_types=1);
return null;
namespace Dotclear\Plugin\filesAlias;
use dcAdmin;
use dcAuth;
use dcCore;
use dcFavorites;
use dcNsProcess;
use dcPage;
class Backend extends dcNsProcess
{
public static function init(): bool
{
static::$init = defined('DC_CONTEXT_ADMIN');
return static::$init;
}
public static function process(): bool
{
if (!static::$init) {
return false;
}
dcCore::app()->menu[dcAdmin::MENU_BLOG]->addItem(
My::name(),
dcCore::app()->adminurl->get('admin.plugin.' . My::id()),
dcPage::getPF(My::id() . '/icon.svg'),
preg_match('/' . preg_quote(dcCore::app()->adminurl->get('admin.plugin.' . My::id())) . '(&.*)?$/', $_SERVER['REQUEST_URI']),
dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([dcAuth::PERMISSION_CONTENT_ADMIN]), dcCore::app()->blog->id)
);
dcCore::app()->addBehavior('adminDashboardFavoritesV2', function (dcFavorites $favs): void {
$favs->register(My::id(), [
'title' => My::name(),
'url' => dcCore::app()->adminurl->get('admin.plugin.' . My::id()),
'small-icon' => dcPage::getPF(My::id() . '/icon.svg'),
'large-icon' => dcPage::getPF(My::id() . '/icon.svg'),
'permissions' => dcCore::app()->auth->makePermissions([
dcAuth::PERMISSION_USAGE,
dcAuth::PERMISSION_CONTENT_ADMIN,
]),
]);
});
return true;
}
} }
dcCore::app()->menu[dcAdmin::MENU_BLOG]->addItem(
__('Media sharing'),
dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__)),
urldecode(dcPage::getPF(basename(__DIR__) . '/icon.svg')),
preg_match('/' . preg_quote(dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__))) . '(&.*)?$/', $_SERVER['REQUEST_URI']),
dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([dcAuth::PERMISSION_CONTENT_ADMIN]), dcCore::app()->blog->id)
);
dcCore::app()->addBehavior('adminDashboardFavoritesV2', function (dcFavorites $favs) {
$favs->register('filesAlias', [
'title' => __('Media sharing'),
'url' => dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__)),
'small-icon' => dcPage::getPF(basename(__DIR__) . '/icon.svg'),
'large-icon' => dcPage::getPF(basename(__DIR__) . '/icon.svg'),
'permissions' => dcCore::app()->auth->makePermissions([
dcAuth::PERMISSION_USAGE,
dcAuth::PERMISSION_CONTENT_ADMIN,
]),
]);
});

View File

@ -10,80 +10,37 @@
* @copyright Jean-Christian Denis * @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_RC_PATH')) { declare(strict_types=1);
return null;
}
dcCore::app()->tpl->setPath(dcCore::app()->tpl->getPath(), __DIR__ . '/default-templates'); namespace Dotclear\Plugin\filesAlias;
dcCore::app()->tpl->addValue('fileAliasURL', ['templateAlias','fileAliasURL']);
class templateAlias use dcCore;
use dcNsProcess;
class Frontend extends dcNsProcess
{ {
public static function fileAliasURL($attr) public static function init(): bool
{ {
$f = dcCore::app()->tpl->getFilters($attr); static::$init = defined('DC_RC_PATH');
return '<?php echo ' . sprintf($f, 'dcCore::app()->blog->url.dcCore::app()->url->getBase("filesalias")."/".dcCore::app()->ctx->filealias->filesalias_url') . '; ?>'; return static::$init;
} }
}
public static function process(): bool
class urlFilesAlias extends dcUrlHandlers {
{ if (!static::$init) {
public static function alias($args) return false;
{ }
$delete = false;
dcCore::app()->tpl->setPath(
dcCore::app()->ctx->__set('filealias', dcCore::app()->__get('filealias')->getAlias($args)); dcCore::app()->tpl->getPath(),
My::path() . DIRECTORY_SEPARATOR . 'default-templates'
if (dcCore::app()->ctx->__get('filealias')->isEmpty()) { );
self::p404(); dcCore::app()->tpl->addValue(
} 'fileAliasURL',
[FrontendTemplate::class, 'fileAliasURL']
if (dcCore::app()->ctx->__get('filealias')->filesalias_disposable) { );
$delete = true;
} return true;
if (dcCore::app()->ctx->__get('filealias')->filesalias_password) {
# Check for match
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 {
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) {
dcCore::app()->__get('filealias')->deleteAlias($alias);
}
} }
} }

View File

@ -0,0 +1,29 @@
<?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
*/
declare(strict_types=1);
namespace Dotclear\Plugin\filesAlias;
use ArrayObject;
use dcCore;
class FrontendTemplate
{
public static function fileAliasURL(ArrayObject $attr): string
{
return '<?php echo ' . sprintf(
dcCore::app()->tpl->getFilters($attr),
'dcCore::app()->blog->url.dcCore::app()->url->getBase("filesalias")."/".dcCore::app()->ctx->filealias->filesalias_url'
) . '; ?>';
}
}

View File

@ -10,38 +10,53 @@
* @copyright Jean-Christian Denis * @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_CONTEXT_ADMIN')) { declare(strict_types=1);
return null;
}
try { namespace Dotclear\Plugin\filesAlias;
if (!dcCore::app()->newVersion(
basename(__DIR__), use dbStruct;
dcCore::app()->plugins->moduleInfo(basename(__DIR__), 'version') use dcCore;
)) { use dcNsProcess;
return null; use Exception;
class Install extends dcNsProcess
{
public static function init(): bool
{
self::$init = defined('DC_CONTEXT_ADMIN') && dcCore::app()->newVersion(My::id(), dcCore::app()->plugins->moduleInfo(My::id(), 'version'));
return self::$init;
} }
$s = new dbStruct(dcCore::app()->con, dcCore::app()->prefix); public static function process(): bool
{
if (!self::$init) {
return false;
}
$s->{initFilesAlias::ALIAS_TABLE_NAME} try {
->blog_id('varchar', 32, false) $s = new dbStruct(dcCore::app()->con, dcCore::app()->prefix);
->filesalias_url('varchar', 255, false)
->filesalias_destination('varchar', 255, false)
->filesalias_password('varchar', 32, true, null)
->filesalias_disposable('smallint', 0, false, 0)
->primary('pk_filesalias', 'blog_id', 'filesalias_url') $s->{My::ALIAS_TABLE_NAME}
->index('idx_filesalias_blog_id', 'btree', 'blog_id') ->blog_id('varchar', 32, false)
->reference('fk_filesalias_blog', 'blog_id', 'blog', 'blog_id', 'cascade', 'cascade') ->filesalias_url('varchar', 255, false)
; ->filesalias_destination('varchar', 255, false)
->filesalias_password('varchar', 32, true, null)
->filesalias_disposable('smallint', 0, false, 0)
$si = new dbStruct(dcCore::app()->con, dcCore::app()->prefix); ->primary('pk_filesalias', 'blog_id', 'filesalias_url')
$changes = $si->synchronize($s); ->index('idx_filesalias_blog_id', 'btree', 'blog_id')
->reference('fk_filesalias_blog', 'blog_id', 'blog', 'blog_id', 'cascade', 'cascade')
;
return true; $si = new dbStruct(dcCore::app()->con, dcCore::app()->prefix);
} catch (Exception $e) { $changes = $si->synchronize($s);
dcCore::app()->error->add($e->getMessage());
return true;
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
}
return true;
}
} }
return false;

View File

@ -10,150 +10,231 @@
* @copyright Jean-Christian Denis * @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_CONTEXT_ADMIN')) { declare(strict_types=1);
return null;
}
$o = dcCore::app()->__get('filealias'); namespace Dotclear\Plugin\filesAlias;
$aliases = $o->getAliases();
$media = new dcMedia();
$a = new aliasMedia();
$part = $_REQUEST['part'] ?? 'list';
# Update aliases use dcAuth;
if (isset($_POST['a']) && is_array($_POST['a'])) { use dcCore;
try { use dcMedia;
$o->updateAliases($_POST['a']); use dcNsProcess;
dcAdminNotices::addSuccessNotice(__('Aliases successfully updated.')); use dcPage;
dcCore::app()->adminurl->redirect('admin.plugin.' . basename(__DIR__)); use Dotclear\Helper\Html\Html;
} catch (Exception $e) { use Dotclear\Helper\Html\Form\{
dcCore::app()->error->add($e->getMessage()); Checkbox,
Form,
Hidden,
Input,
Label,
Note,
Para,
Submit,
Text
};
use Exception;
class Manage extends dcNsProcess
{
public static function init(): bool
{
static::$init = defined('DC_CONTEXT_ADMIN') && dcCore::app()->auth->check(
dcCore::app()->auth->makePermissions([
dcAuth::PERMISSION_ADMIN,
]),
dcCore::app()->blog->id
);
return static::$init;
} }
}
# New alias public static function process(): bool
if (isset($_POST['filesalias_url'])) { {
$url = empty($_POST['filesalias_url']) ? PallazzoTools::rand_uniqid() : $_POST['filesalias_url']; if (!static::$init) {
return false;
}
$target = $_POST['filesalias_destination']; if (!(dcCore::app()->media instanceof dcMedia)) {
$totrash = isset($_POST['filesalias_disposable']) ? true : false; dcCore::app()->media = new dcMedia();
$password = empty($_POST['filesalias_password']) ? '' : $_POST['filesalias_password']; }
if (preg_match('/^' . preg_quote($media->root_url, '/') . '/', $target)) { # Update aliases
$target = preg_replace('/^' . preg_quote($media->root_url, '/') . '/', '', $target); if (isset($_POST['a']) && is_array($_POST['a'])) {
$found = $a->getMediaId($target);
if (!empty($found)) {
try { try {
$o->createAlias($url, $target, $totrash, $password); Utils::updateAliases($_POST['a']);
dcAdminNotices::addSuccessNotice(__('Alias for this media created.')); dcPage::addSuccessNotice(__('Aliases successfully updated.'));
dcCore::app()->adminurl->redirect('admin.plugin.' . basename(__DIR__)); dcCore::app()->adminurl->redirect('admin.plugin.' . My::id());
} catch (Exception $e) { } catch (Exception $e) {
dcCore::app()->error->add($e->getMessage()); dcCore::app()->error->add($e->getMessage());
} }
} else {
dcCore::app()->error->add(__('Target is not in media manager.'));
} }
} else {
$found = $a->getMediaId($target);
if (!empty($found)) { # New alias
try { if (isset($_POST['filesalias_url'])) {
$o->createAlias($url, $target, $totrash, $password); $url = empty($_POST['filesalias_url']) ? PallazzoTools::rand_uniqid() : $_POST['filesalias_url'];
dcAdminNotices::addSuccessNotice(__('Alias for this media modified.'));
dcCore::app()->adminurl->redirect('admin.plugin.' . basename(__DIR__)); $target = $_POST['filesalias_destination'];
} catch (Exception $e) { $totrash = isset($_POST['filesalias_disposable']) ? true : false;
dcCore::app()->error->add($e->getMessage()); $password = empty($_POST['filesalias_password']) ? '' : $_POST['filesalias_password'];
if (preg_match('/^' . preg_quote(dcCore::app()->media->root_url, '/') . '/', $target)) {
$target = preg_replace('/^' . preg_quote(dcCore::app()->media->root_url, '/') . '/', '', $target);
$found = Utils::getMediaId($target);
if (!empty($found)) {
try {
Utils::createAlias($url, $target, $totrash, $password);
dcPage::addSuccessNotice(__('Alias for this media created.'));
dcCore::app()->adminurl->redirect('admin.plugin.' . My::id());
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
}
} else {
dcCore::app()->error->add(__('Target is not in media manager.'));
}
} else {
$found = Utils::getMediaId($target);
if (!empty($found)) {
try {
Utils::createAlias($url, $target, $totrash, $password);
dcPage::addSuccessNotice(__('Alias for this media modified.'));
dcCore::app()->adminurl->redirect('admin.plugin.' . My::id());
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
}
} else {
dcCore::app()->error->add(__('Target is not in media manager.'));
}
} }
} else {
dcCore::app()->error->add(__('Target is not in media manager.'));
} }
return true;
} }
}
?>
<html>
<head>
<title><?php echo __('Media sharing'); ?></title>
</head>
<body> public static function render(): void
<?php {
if (!self::$init) {
return;
}
if ($part == 'new') { dcPage::openModule(My::name());
echo
dcPage::breadcrumb([
html::escapeHTML(dcCore::app()->blog->name) => '',
__('Media sharing') => dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__)),
__('New alias') => '',
]) .
dcPage::notices() .
'<form action="' . dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__)) . '" method="post">' .
'<h3>' . __('New alias') . '</h3>' .
'<p ><label for="filesalias_destination" class="required">' . __('Destination:') . ' </label>' .
form::field('filesalias_destination', 70, 255) . '</p>' .
'<p class="form-note warn">' . __('Destination file must be in media manager.') . '</p>' .
'<p><label for="filesalias_url">' . __('URL (alias):') . '</label>' .
form::field('filesalias_url', 70, 255) . '</p>' .
'<p class="form-note info">' . __('Leave empty to get a randomize alias.') . '</p>' .
'<p><label for="filesalias_password">' . __('Password:') . '</label> ' .
form::field('filesalias_password', 70, 255) . '</p>' .
'<p>' . form::checkbox('filesalias_disposable', 1) .
'<label for="filesalias_disposable" class="classic">' . __('Disposable') . '</label></p>' .
'<p>' .
dcCore::app()->formNonce() .
form::hidden('part', 'new') .
'<input type="submit" value="' . __('Save') . '" /></p>' .
'<p class="form-note">' . sprintf(__('Do not put blog media URL "%s" in fields or it will be removed.'), $media->root_url) . '</p>' .
'</form>';
} else {
echo
dcPage::breadcrumb([
html::escapeHTML(dcCore::app()->blog->name) => '',
__('Media sharing') => '',
]) .
dcPage::notices() .
'<p class="top-add"><a class="button add" href="' .
dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__), ['part' => 'new']) .
'">' . __('New alias') . '</a></p>';
if (empty($aliases)) { if (($_REQUEST['part'] ?? '') == 'new') {
echo '<p>' . __('No alias') . '</p>'; self::displayAliasForm();
} else { } else {
self::displayAliasList();
}
dcPage::helpBlock('filesAlias');
dcPage::closeModule();
}
private static function displayAliasForm(): void
{
echo echo
'<form action="' . dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__)) . '" method="post">' . dcPage::breadcrumb([
'<div class="table-outer">' . Html::escapeHTML(dcCore::app()->blog->name) => '',
'<table><thead>' . My::name() => dcCore::app()->adminurl->get('admin.plugin.' . My::id()),
'<caption>' . __('Aliases list') . '</caption>' . __('New alias') => '',
'<tr>' . ]) .
'<th class="nowrap" scope="col">' . __('Destination') . ' - <ins>' . html::escapeHTML($media->root_url) . '</ins><code>(-?-)</code></th>' . dcPage::notices() .
'<th class="nowrap" scope="col">' . __('Alias') . ' - <ins>' . dcCore::app()->blog->url . dcCore::app()->url->getBase('filesalias') . '/' . '</ins><code>(-?-)</code></th>' . (new Form('filesalias_new'))->action(dcCore::app()->adminurl->get('admin.plugin.' . My::id()))->method('post')->fields([
'<th class="nowrap" scope="col">' . __('Disposable') . '</th>' . (new Text('h3', Html::escapeHTML(__('New alias')))),
'<th class="nowrap" scope="col">' . __('Password') . '</th>' . (new Note())->text(sprintf(__('Do not put blog media URL "%s" in fields or it will be removed.'), dcCore::app()->media->root_url))->class('form-note'),
'</tr></thead><body>'; // destination
(new Para())->items([
(new Label(__('Destination:')))->for('filesalias_destination')->class('required'),
(new Input('filesalias_destination'))->size(70)->maxlenght(255),
]),
(new Note())->text(__('Destination file must be in media manager.'))->class('form-note'),
// url
(new Para())->items([
(new Label(__('URL (alias):')))->for('filesalias_url')->class('required'),
(new Input('filesalias_url'))->size(70)->maxlenght(255),
]),
(new Note())->text(__('Leave empty to get a randomize alias.'))->class('form-note'),
// password
(new Para())->items([
(new Label(__('Password:')))->for('filesalias_password')->class('required'),
(new Input('filesalias_password'))->size(70)->maxlenght(255),
]),
// disposable
(new Para())->items([
(new Checkbox('filesalias_disposable', false))->value(1),
(new Label(__('Disposable'), Label::OUTSIDE_LABEL_AFTER))->for('filesalias_disposable')->class('classic'),
]),
// submit
(new Para())->items([
(new Submit(['save']))->value(__('Save')),
(new Hidden(['part'], 'new')),
(new Text('', dcCore::app()->formNonce())),
]),
])->render();
}
foreach ($aliases as $k => $v) { private static function displayAliasList(): void
$url = dcCore::app()->blog->url . dcCore::app()->url->getBase('filesalias') . '/' . html::escapeHTML($v['filesalias_url']); {
$aliases = Utils::getAliases();
echo
dcPage::breadcrumb([
Html::escapeHTML(dcCore::app()->blog->name) => '',
My::name() => '',
]) .
dcPage::notices() .
'<p class="top-add"><a class="button add" href="' .
dcCore::app()->adminurl->get('admin.plugin.' . My::id(), ['part' => 'new']) .
'">' . __('New alias') . '</a></p>';
if ($aliases->isEmpty()) {
echo '<p>' . __('No alias') . '</p>';
} else {
$lines = '';
$i = 0;
while ($aliases->fetch()) {
$url = dcCore::app()->blog->url . dcCore::app()->url->getBase('filesalias') . '/' . Html::escapeHTML($aliases->f('filesalias_url'));
$lines .= '<tr class="line" id="l_' . $i . '">' .
'<td>' .
(new Input(['a[' . $i . '][filesalias_destination]']))->size(50)->maxlenght(255)->value(Html::escapeHTML($aliases->f('filesalias_destination')))->render() .
'</td>' .
'<td>' .
(new Input(['a[' . $i . '][filesalias_url]']))->size(50)->maxlenght(255)->value(Html::escapeHTML($aliases->f('filesalias_url')))->render() .
'<a href="' . $url . '">' . __('link') . '</a></td>' .
'<td>' .
(new Input(['a[' . $i . '][filesalias_password]']))->size(50)->maxlenght(255)->value(Html::escapeHTML($aliases->f('filesalias_password')))->render() .
'</td>' .
'<td class="maximal">' .
(new Checkbox(['a[' . $i . '][filesalias_disposable]'], (bool) $aliases->f('filesalias_disposable')))->value(1)->render() .
'</td>' .
'</tr>';
$i++;
}
$link = '<a href="' . $url . '">' . __('link') . '</a>';
$v['filesalias_disposable'] ??= false;
echo echo
'<tr class="line" id="l_' . $k . '">' . (new Form('filesalias_list'))->action(dcCore::app()->adminurl->get('admin.plugin.' . My::id()))->method('post')->fields([
'<td>' . form::field(['a[' . $k . '][filesalias_destination]'], 40, 255, html::escapeHTML($v['filesalias_destination'])) . '</td>' . (new Text(
'<td class="maximal">' . form::field(['a[' . $k . '][filesalias_url]'], 20, 255, html::escapeHTML($v['filesalias_url'])) . '<a href="' . $url . '">' . __('link') . '</a></td>' . '',
'<td class="minimal">' . form::checkbox(['a[' . $k . '][filesalias_disposable]'], 1, $v['filesalias_disposable']) . '</td>' . '<div class="table-outer">' .
'<td class="minimal">' . form::field(['a[' . $k . '][filesalias_password]'], 10, 255, html::escapeHTML($v['filesalias_password'])) . '</td>' . '<table><thead>' .
'</tr>'; '<caption>' . __('Aliases list') . '</caption>' .
'<tr>' .
'<th class="nowrap" scope="col">' . __('Destination') . ' - <ins>' . Html::escapeHTML(dcCore::app()->media->root_url) . '</ins><code>(-?-)</code></th>' .
'<th class="nowrap" scope="col">' . __('Alias') . ' - <ins>' . dcCore::app()->blog->url . dcCore::app()->url->getBase('filesalias') . '/' . '</ins><code>(-?-)</code></th>' .
'<th class="nowrap" scope="col">' . __('Password') . '</th>' .
'<th class="nowrap" scope="col">' . __('Disposable') . '</th>' .
'</tr></thead><body>' .
$lines .
'</tobdy></table></div>'
)),
(new Para())->items([
(new Submit(['save']))->value(__('Update')),
(new Hidden(['part'], 'list')),
(new Text('', dcCore::app()->formNonce())),
]),
(new Note())->text(__('To remove a link, empty its alias or destination.'))->class('form-note'),
])->render();
} }
echo '</tobdy></table></div>' .
'<p class="form-note">' . __('To remove a link, empty its alias or destination.') . '</p>' .
'<p>' . dcCore::app()->formNonce() . form::hidden('part', 'list') .
'<input type="submit" value="' . __('Update') . '" /></p>' .
'</form>';
} }
} }
dcPage::helpBlock('filesAlias');
?>
</body>
</html>

45
src/My.php 100644
View File

@ -0,0 +1,45 @@
<?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
*/
declare(strict_types=1);
namespace Dotclear\Plugin\filesAlias;
/**
* Plugin definitions
*/
class My extends \initFilesAlias
{
/**
* This module id
*/
public static function id(): string
{
return basename(dirname(__DIR__));
}
/**
* This module name
*/
public static function name(): string
{
return __('Media sharing');
}
/**
* This module root path
*/
public static function path(): string
{
return dirname(__DIR__);
}
}

View File

@ -10,9 +10,16 @@
* @copyright Jean-Christian Denis * @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
declare(strict_types=1);
namespace Dotclear\Plugin\filesAlias;
/**
* Adapted from Enrico Pallazzo class
*/
class PallazzoTools class PallazzoTools
{ {
public static function rand_uniqid() public static function rand_uniqid(): string
{ {
$index = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; $index = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$in = time(); $in = time();

View File

@ -10,21 +10,35 @@
* @copyright Jean-Christian Denis * @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_RC_PATH')) { declare(strict_types=1);
return null;
namespace Dotclear\Plugin\filesAlias;
use dcCore;
use dcNsProcess;
class Prepend extends dcNsProcess
{
public static function init(): bool
{
static::$init = defined('DC_RC_PATH');
return static::$init;
}
public static function process(): bool
{
if (!static::$init) {
return false;
}
dcCore::app()->url->register(
'filesalias',
'pub',
'^pub/(.+)$',
[UrlHandler::class, 'alias']
);
return true;
}
} }
Clearbricks::lib()->autoload([
'filesAliases' => __DIR__ . '/inc/class.files.alias.php',
'aliasMedia' => __DIR__ . '/inc/class.files.alias.php',
'PallazzoTools' => __DIR__ . '/inc/lib.files.alias.tools.php',
]);
dcCore::app()->__set('filealias', new filesAliases());
dcCore::app()->url->register(
'filesalias',
'pub',
'^pub/(.+)$',
['urlFilesAlias','alias']
);

83
src/UrlHandler.php 100644
View File

@ -0,0 +1,83 @@
<?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
*/
declare(strict_types=1);
namespace Dotclear\Plugin\filesAlias;
use dcCore;
use dcMedia;
use dcUrlHandlers;
class UrlHandler extends dcUrlHandlers
{
public static function alias(string $args): void
{
$delete = false;
dcCore::app()->ctx->__set('filealias', Utils::getAlias($args));
if (dcCore::app()->ctx->__get('filealias')->isEmpty()) {
self::p404();
}
if (dcCore::app()->ctx->__get('filealias')->f('filesalias_disposable')) {
$delete = true;
}
if (dcCore::app()->ctx->__get('filealias')->f('filesalias_password')) {
# Check for match
if (!empty($_POST['filepassword']) && $_POST['filepassword'] == dcCore::app()->ctx->__get('filealias')->f('filesalias_password')) {
self::servefile(dcCore::app()->ctx->__get('filealias')->f('filesalias_destination'), $args, $delete);
} else {
self::serveDocument('file-password-form.html', 'text/html', false);
return;
}
} else {
self::servefile(dcCore::app()->ctx->__get('filealias')->f('filesalias_destination'), $args, $delete);
}
}
public static function servefile(string $target, string $alias, bool $delete = false): void
{
$media = Utils::getMediaId($target);
if (empty($media)) {
self::p404();
}
if (!(dcCore::app()->media instanceof dcMedia)) {
dcCore::app()->media = new dcMedia();
}
$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) {
Utils::deleteAlias($alias);
}
}
}

View File

@ -10,53 +10,48 @@
* @copyright Jean-Christian Denis * @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
class filesAliases declare(strict_types=1);
namespace Dotclear\Plugin\filesAlias;
use dcCore;
use dcMedia;
use dcRecord;
use Exception;
class Utils
{ {
protected $aliases; public static function getAliases(): dcRecord
public function __construct()
{ {
return new dcRecord(dcCore::app()->con->select(
'SELECT filesalias_url, filesalias_destination, filesalias_password, filesalias_disposable ' .
'FROM ' . dcCore::app()->prefix . My::ALIAS_TABLE_NAME . ' ' .
"WHERE blog_id = '" . dcCore::app()->con->escapeStr(dcCore::app()->blog->id) . "' " .
'ORDER BY filesalias_url ASC '
));
} }
public function getAliases() public static function getAlias(string $url): dcRecord
{ {
if (is_array($this->aliases)) { return new dcRecord(dcCore::app()->con->select(
return $this->aliases; 'SELECT filesalias_url, filesalias_destination, filesalias_password, filesalias_disposable ' .
} 'FROM ' . dcCore::app()->prefix . My::ALIAS_TABLE_NAME . ' ' .
"WHERE blog_id = '" . dcCore::app()->con->escapeStr(dcCore::app()->blog->id) . "' " .
$this->aliases = []; "AND filesalias_url = '" . dcCore::app()->con->escapeStr($url) . "' " .
$sql = 'SELECT filesalias_url, filesalias_destination, filesalias_password, filesalias_disposable ' . 'ORDER BY filesalias_url ASC '
'FROM ' . dcCore::app()->prefix . initFilesAlias::ALIAS_TABLE_NAME . ' ' . ));
"WHERE blog_id = '" . dcCore::app()->con->escape(dcCore::app()->blog->id) . "' " .
'ORDER BY filesalias_url ASC ';
$this->aliases = dcCore::app()->con->select($sql)->rows();
return $this->aliases;
} }
public function getAlias($url) public static function updateAliases(array $aliases): void
{
$strReq = 'SELECT filesalias_url, filesalias_destination, filesalias_password, filesalias_disposable ' .
'FROM ' . dcCore::app()->prefix . initFilesAlias::ALIAS_TABLE_NAME . ' ' .
"WHERE blog_id = '" . dcCore::app()->con->escape(dcCore::app()->blog->id) . "' " .
"AND filesalias_url = '" . dcCore::app()->con->escape($url) . "' " .
'ORDER BY filesalias_url ASC ';
$rs = dcCore::app()->con->select($strReq);
return $rs;
}
public function updateAliases($aliases)
{ {
dcCore::app()->con->begin(); dcCore::app()->con->begin();
try { try {
$this->deleteAliases(); self::deleteAliases();
foreach ($aliases as $k => $v) { foreach ($aliases as $k => $v) {
if (!empty($v['filesalias_url']) && !empty($v['filesalias_destination'])) { if (!empty($v['filesalias_url']) && !empty($v['filesalias_destination'])) {
$v['filesalias_disposable'] = isset($v['filesalias_disposable']) ? true : false; $v['filesalias_disposable'] = isset($v['filesalias_disposable']) ? true : false;
$this->createAlias($v['filesalias_url'], $v['filesalias_destination'], $v['filesalias_disposable'], $v['filesalias_password']); self::createAlias($v['filesalias_url'], $v['filesalias_destination'], $v['filesalias_disposable'], $v['filesalias_password']);
} }
} }
@ -68,7 +63,7 @@ class filesAliases
} }
} }
public function createAlias($url, $destination, $disposable = 0, $password = null) public static function createAlias(string $url, string $destination, bool $disposable = false, ?string $password = null): void
{ {
if (!$url) { if (!$url) {
throw new Exception(__('File URL is empty.')); throw new Exception(__('File URL is empty.'));
@ -78,49 +73,41 @@ class filesAliases
throw new Exception(__('File destination is empty.')); throw new Exception(__('File destination is empty.'));
} }
$cur = dcCore::app()->con->openCursor(dcCore::app()->prefix . initFilesAlias::ALIAS_TABLE_NAME); $cur = dcCore::app()->con->openCursor(dcCore::app()->prefix . My::ALIAS_TABLE_NAME);
$cur->blog_id = (string) dcCore::app()->blog->id; $cur->setField('blog_id', (string) dcCore::app()->blog->id);
$cur->filesalias_url = (string) $url; $cur->setField('filesalias_url', (string) $url);
$cur->filesalias_destination = (string) $destination; $cur->setField('filesalias_destination', (string) $destination);
$cur->filesalias_password = $password; $cur->setField('filesalias_password', $password);
$cur->filesalias_disposable = abs((int) $disposable); $cur->setField('filesalias_disposable', (int) $disposable);
$cur->insert(); $cur->insert();
} }
public function deleteAliases() public static function deleteAliases(): void
{ {
dcCore::app()->con->execute( dcCore::app()->con->execute(
'DELETE FROM ' . dcCore::app()->prefix . initFilesAlias::ALIAS_TABLE_NAME . ' ' . 'DELETE FROM ' . dcCore::app()->prefix . My::ALIAS_TABLE_NAME . ' ' .
"WHERE blog_id = '" . dcCore::app()->con->escape(dcCore::app()->blog->id) . "' " "WHERE blog_id = '" . dcCore::app()->con->escapeStr(dcCore::app()->blog->id) . "' "
); );
} }
public function deleteAlias($url) public static function deleteAlias(string $url): void
{ {
dcCore::app()->con->execute( dcCore::app()->con->execute(
'DELETE FROM ' . dcCore::app()->prefix . initFilesAlias::ALIAS_TABLE_NAME . ' ' . 'DELETE FROM ' . dcCore::app()->prefix . My::ALIAS_TABLE_NAME . ' ' .
"WHERE blog_id = '" . dcCore::app()->con->escape(dcCore::app()->blog->id) . "' " . "WHERE blog_id = '" . dcCore::app()->con->escapeStr(dcCore::app()->blog->id) . "' " .
"AND filesalias_url = '" . dcCore::app()->con->escape($url) . "' " "AND filesalias_url = '" . dcCore::app()->con->escapeStr($url) . "' "
); );
} }
}
class aliasMedia extends dcMedia public static function getMediaId(string $target): int
{
public function __construct()
{
}
public function getMediaId($target)
{ {
$strReq = 'SELECT media_id ' . $strReq = 'SELECT media_id ' .
'FROM ' . dcCore::app()->prefix . dcMedia::MEDIA_TABLE_NAME . ' ' . 'FROM ' . dcCore::app()->prefix . dcMedia::MEDIA_TABLE_NAME . ' ' .
//"WHERE media_path = '" . $this->path . "' " . "WHERE media_path = '" . dcCore::app()->con->escapeStr((string) dcCore::app()->blog->settings->get('system')->get('public_path')) . "' " .
"WHERE media_path = '" . dcCore::app()->con->escape(dcCore::app()->blog->settings->system->public_path) . "' " . "AND media_file = '" . dcCore::app()->con->escapeStr($target) . "' ";
"AND media_file = '" . dcCore::app()->con->escape($target) . "' ";
$rs = dcCore::app()->con->select($strReq); $rs = dcCore::app()->con->select($strReq);
return $rs->count() ? $rs->media_id : null; return $rs->count() ? (int) $rs->f('media_id') : 0;
} }
} }