filesAlias/index.php

159 lines
6.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_CONTEXT_ADMIN')) {
return null;
}
2022-12-04 15:32:11 +00:00
2022-12-21 23:09:55 +00:00
$o = dcCore::app()->__get('filealias');
2022-12-04 15:32:11 +00:00
$aliases = $o->getAliases();
$media = new dcMedia();
$a = new aliasMedia();
$part = $_REQUEST['part'] ?? 'list';
2022-12-04 15:32:11 +00:00
# Update aliases
if (isset($_POST['a']) && is_array($_POST['a'])) {
try {
$o->updateAliases($_POST['a']);
dcAdminNotices::addSuccessNotice(__('Aliases successfully updated.'));
2022-12-21 23:09:55 +00:00
dcCore::app()->adminurl->redirect('admin.plugin.' . basename(__DIR__));
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
}
2022-12-04 15:32:11 +00:00
}
# New alias
if (isset($_POST['filesalias_url'])) {
$url = empty($_POST['filesalias_url']) ? PallazzoTools::rand_uniqid() : $_POST['filesalias_url'];
$target = $_POST['filesalias_destination'];
$totrash = isset($_POST['filesalias_disposable']) ? true : false;
$password = empty($_POST['filesalias_password']) ? '' : $_POST['filesalias_password'];
2022-12-04 15:32:11 +00:00
if (preg_match('/^' . preg_quote($media->root_url, '/') . '/', $target)) {
$target = preg_replace('/^' . preg_quote($media->root_url, '/') . '/', '', $target);
$found = $a->getMediaId($target);
2022-12-04 15:32:11 +00:00
if (!empty($found)) {
try {
$o->createAlias($url, $target, $totrash, $password);
dcAdminNotices::addSuccessNotice(__('Alias for this media created.'));
2022-12-21 23:09:55 +00:00
dcCore::app()->adminurl->redirect('admin.plugin.' . basename(__DIR__));
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
}
} else {
dcCore::app()->error->add(__('Target is not in media manager.'));
}
} else {
$found = $a->getMediaId($target);
2022-12-04 15:32:11 +00:00
if (!empty($found)) {
try {
$o->createAlias($url, $target, $totrash, $password);
dcAdminNotices::addSuccessNotice(__('Alias for this media modified.'));
2022-12-21 23:09:55 +00:00
dcCore::app()->adminurl->redirect('admin.plugin.' . basename(__DIR__));
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
}
} else {
dcCore::app()->error->add(__('Target is not in media manager.'));
}
}
2022-12-04 15:32:11 +00:00
}
?>
<html>
<head>
<title><?php echo __('Media sharing'); ?></title>
2022-12-04 15:32:11 +00:00
</head>
<body>
<?php
if ($part == 'new') {
echo
dcPage::breadcrumb([
html::escapeHTML(dcCore::app()->blog->name) => '',
2022-12-21 23:09:55 +00:00
__('Media sharing') => dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__)),
__('New alias') => '',
]) .
dcPage::notices() .
2022-12-21 23:09:55 +00:00
'<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="' .
2022-12-21 23:09:55 +00:00
dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__), ['part' => 'new']) .
'">' . __('New alias') . '</a></p>';
2022-12-04 15:32:11 +00:00
if (empty($aliases)) {
echo '<p>' . __('No alias') . '</p>';
} else {
echo
2022-12-21 23:09:55 +00:00
'<form action="' . dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__)) . '" method="post">' .
'<div class="table-outer">' .
'<table><thead>' .
'<caption>' . __('Aliases list') . '</caption>' .
'<tr>' .
'<th class="nowrap" scope="col">' . __('Destination') . ' - <ins>' . html::escapeHTML($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">' . __('Disposable') . '</th>' .
'<th class="nowrap" scope="col">' . __('Password') . '</th>' .
'</tr></thead><body>';
2022-12-04 15:32:11 +00:00
foreach ($aliases as $k => $v) {
$url = dcCore::app()->blog->url . dcCore::app()->url->getBase('filesalias') . '/' . html::escapeHTML($v['filesalias_url']);
2022-12-04 15:32:11 +00:00
$link = '<a href="' . $url . '">' . __('link') . '</a>';
$v['filesalias_disposable'] ??= false;
echo
'<tr class="line" id="l_' . $k . '">' .
'<td>' . form::field(['a[' . $k . '][filesalias_destination]'], 40, 255, html::escapeHTML($v['filesalias_destination'])) . '</td>' .
'<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>' .
'<td class="minimal">' . form::field(['a[' . $k . '][filesalias_password]'], 10, 255, html::escapeHTML($v['filesalias_password'])) . '</td>' .
'</tr>';
}
2022-12-04 15:32:11 +00:00
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>';
}
2022-12-04 15:32:11 +00:00
}
dcPage::helpBlock('filesAlias');
?>
</body>
</html>