2021-08-25 23:06:22 +00:00
|
|
|
<?php
|
2021-09-02 12:32:26 +00:00
|
|
|
/**
|
|
|
|
* @brief kUtRL, a plugin for Dotclear 2
|
2021-11-06 15:43:02 +00:00
|
|
|
*
|
2021-09-02 12:32:26 +00:00
|
|
|
* @package Dotclear
|
|
|
|
* @subpackage Plugin
|
2021-11-06 15:43:02 +00:00
|
|
|
*
|
2021-09-02 12:32:26 +00:00
|
|
|
* @author Jean-Christian Denis and contributors
|
2021-11-06 15:43:02 +00:00
|
|
|
*
|
2021-09-02 12:32:26 +00:00
|
|
|
* @copyright Jean-Christian Denis
|
|
|
|
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
|
|
|
|
*/
|
2021-08-26 00:08:41 +00:00
|
|
|
if (!defined('DC_CONTEXT_ADMIN')) {
|
2021-08-28 16:27:58 +00:00
|
|
|
return;
|
2021-08-26 00:08:41 +00:00
|
|
|
}
|
2021-08-25 23:06:22 +00:00
|
|
|
|
|
|
|
# Check user perms
|
|
|
|
dcPage::check('admin');
|
|
|
|
|
2021-10-26 20:27:27 +00:00
|
|
|
$header = '';
|
2021-11-06 15:43:02 +00:00
|
|
|
$part = $_REQUEST['part'] ?? 'links';
|
|
|
|
$action = $_POST['action'] ?? '';
|
2021-08-28 16:27:58 +00:00
|
|
|
|
|
|
|
# link creation
|
|
|
|
if ($part == 'link') {
|
|
|
|
$kut = kutrl::quickPlace('admin');
|
|
|
|
|
|
|
|
if (!empty($_POST['save'])) {
|
|
|
|
try {
|
|
|
|
if (null === $kut) {
|
|
|
|
throw new Exception('Unknow service');
|
|
|
|
}
|
2021-11-06 15:43:02 +00:00
|
|
|
$url = trim($core->con->escape($_POST['str']));
|
2021-08-28 16:27:58 +00:00
|
|
|
$hash = empty($_POST['custom']) ? null : $_POST['custom'];
|
2021-09-02 12:32:26 +00:00
|
|
|
|
2021-08-28 16:27:58 +00:00
|
|
|
if (empty($url)) {
|
|
|
|
throw new Exception(__('There is nothing to shorten.'));
|
|
|
|
}
|
|
|
|
if (!$kut->testService()) {
|
|
|
|
throw new Exception(__('Service is not well configured.'));
|
|
|
|
}
|
|
|
|
if (null !== $hash && !$kut->allow_custom_hash) {
|
|
|
|
throw new Exception(__('This service does not allowed custom hash.'));
|
|
|
|
}
|
|
|
|
if (!$kut->isValidUrl($url)) {
|
|
|
|
throw new Exception(__('This link is not a valid URL.'));
|
|
|
|
}
|
|
|
|
if (!$kut->isLongerUrl($url)) {
|
|
|
|
throw new Exception(__('This link is too short.'));
|
|
|
|
}
|
|
|
|
if (!$kut->isProtocolUrl($url)) {
|
|
|
|
throw new Exception(__('This type of link is not allowed.'));
|
|
|
|
}
|
|
|
|
if (!$kut->allow_external_url && !$kut->isBlogUrl($url)) {
|
|
|
|
throw new Exception(__('Short links are limited to this blog URL.'));
|
|
|
|
}
|
|
|
|
if ($kut->isServiceUrl($url)) {
|
|
|
|
throw new Exception(__('This link is already a short link.'));
|
|
|
|
}
|
|
|
|
if (null !== $hash && false !== ($rs = $kut->isKnowHash($hash))) {
|
|
|
|
throw new Exception(__('This custom short url is already taken.'));
|
|
|
|
}
|
|
|
|
if (false !== ($rs = $kut->isKnowUrl($url))) {
|
2021-11-06 15:43:02 +00:00
|
|
|
$url = $rs->url;
|
|
|
|
$new_url = $kut->url_base . $rs->hash;
|
2021-08-28 16:27:58 +00:00
|
|
|
|
|
|
|
dcPage::addSuccessNotice(sprintf(
|
|
|
|
__('Short link for %s is %s'),
|
|
|
|
'<strong>' . html::escapeHTML($url) . '</strong>',
|
|
|
|
'<a href="' . $new_url . '">' . $new_url . '</a>'
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
if (false === ($rs = $kut->hash($url, $hash))) {
|
|
|
|
if ($kut->error->flag()) {
|
|
|
|
throw new Exception($kut->error->toHTML());
|
|
|
|
}
|
2021-11-06 15:43:02 +00:00
|
|
|
|
2021-08-28 16:27:58 +00:00
|
|
|
throw new Exception(__('Failed to create short link. This could be caused by a service failure.'));
|
|
|
|
} else {
|
2021-11-06 15:43:02 +00:00
|
|
|
$url = $rs->url;
|
2021-08-28 16:27:58 +00:00
|
|
|
$new_url = $kut->url_base . $rs->hash;
|
|
|
|
|
|
|
|
dcPage::addSuccessNotice(sprintf(
|
|
|
|
__('Short link for %s is %s'),
|
|
|
|
'<strong>' . html::escapeHTML($url) . '</strong>',
|
|
|
|
'<a href="' . $new_url . '">' . $new_url . '</a>'
|
|
|
|
));
|
|
|
|
|
|
|
|
# ex: Send new url to messengers
|
|
|
|
if (!empty($rs)) {
|
2021-11-06 15:43:02 +00:00
|
|
|
$core->callBehavior('adminAfterKutrlCreate', $core, $rs, __('New short URL'));
|
2021-08-28 16:27:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$core->error->add($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-06 15:43:02 +00:00
|
|
|
# links
|
2021-10-26 20:27:27 +00:00
|
|
|
} else {
|
|
|
|
$services_combo = [];
|
2021-11-06 15:43:02 +00:00
|
|
|
foreach (kutrl::getServices($core) as $service_id => $service) {
|
|
|
|
$o = new $service($core);
|
2021-10-26 20:27:27 +00:00
|
|
|
$services_combo[__($o->name)] = $o->id;
|
|
|
|
}
|
2021-11-06 15:43:02 +00:00
|
|
|
$ext_services_combo = array_merge([__('Disabled') => ''], $services_combo);
|
|
|
|
$lst_services_combo = array_merge(['-' => ''], $services_combo);
|
2021-10-26 20:27:27 +00:00
|
|
|
|
2021-08-28 16:27:58 +00:00
|
|
|
$log = new kutrlLog($core);
|
|
|
|
|
2021-10-03 20:09:13 +00:00
|
|
|
$kUtRL_filter = new adminGenericFilter($core, 'kUtRL');
|
2021-10-09 15:18:04 +00:00
|
|
|
$kUtRL_filter->add('part', 'links');
|
|
|
|
$kUtRL_filter->add(dcAdminFilters::getPageFilter());
|
|
|
|
$kUtRL_filter->add(dcAdminFilters::getSelectFilter(
|
2021-11-06 15:43:02 +00:00
|
|
|
'urlsrv',
|
|
|
|
__('Service:'),
|
|
|
|
$lst_services_combo,
|
|
|
|
'kut_type'
|
2021-10-09 15:18:04 +00:00
|
|
|
));
|
2021-08-28 16:27:58 +00:00
|
|
|
|
2021-10-09 15:18:04 +00:00
|
|
|
$params = $kUtRL_filter->params();
|
2021-08-28 16:27:58 +00:00
|
|
|
|
|
|
|
try {
|
2021-11-06 15:43:02 +00:00
|
|
|
$list_all = $log->getLogs($params);
|
2021-08-28 16:27:58 +00:00
|
|
|
$list_counter = $log->getLogs($params, true)->f(0);
|
|
|
|
$list_current = new kutrlLinksList($core, $list_all, $list_counter);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$core->error->add($e->getMessage());
|
|
|
|
}
|
|
|
|
|
2021-11-06 15:43:02 +00:00
|
|
|
$header = $kUtRL_filter->js($core->adminurl->get('admin.plugin.kUtRL', ['part' => 'links'])) .
|
2021-10-26 20:27:27 +00:00
|
|
|
dcPage::jsLoad(dcPage::getPF('kUtRL/js/admin.js'));
|
|
|
|
|
2021-08-28 16:27:58 +00:00
|
|
|
if (!empty($_POST['deletelinks'])) {
|
|
|
|
try {
|
2021-11-06 15:43:02 +00:00
|
|
|
foreach ($_POST['entries'] as $id) {
|
2021-08-28 16:27:58 +00:00
|
|
|
$rs = $log->getLogs(['kut_id' => $id]);
|
|
|
|
if ($rs->isEmpty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (null === ($o = kutrl::quickService($rs->kut_type))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$o->remove($rs->kut_url);
|
|
|
|
}
|
2021-09-02 12:32:26 +00:00
|
|
|
|
2021-08-28 16:27:58 +00:00
|
|
|
$core->blog->triggerBlog();
|
|
|
|
|
|
|
|
dcPage::addSuccessNotice(
|
|
|
|
__('Links successfully deleted')
|
|
|
|
);
|
2021-10-09 15:18:04 +00:00
|
|
|
$core->adminurl->redirect('admin.plugin.kUtRL', $kUtRL_filter->values());
|
2021-08-28 16:27:58 +00:00
|
|
|
} catch (Exception $e) {
|
|
|
|
$core->error->add($e->getMessage());
|
2021-11-06 15:43:02 +00:00
|
|
|
}
|
2021-08-28 16:27:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# display header
|
2021-11-06 15:43:02 +00:00
|
|
|
echo
|
2021-10-26 20:27:27 +00:00
|
|
|
'<html><head><title>kUtRL, ' . __('Links shortener') . '</title>' .
|
|
|
|
$header .
|
2021-08-28 16:27:58 +00:00
|
|
|
'</head><body>';
|
|
|
|
|
|
|
|
# display link creation
|
|
|
|
if ($part == 'link') {
|
2021-11-06 15:43:02 +00:00
|
|
|
echo
|
2021-10-26 20:35:42 +00:00
|
|
|
dcPage::breadcrumb([
|
2021-11-06 15:43:02 +00:00
|
|
|
__('Plugins') => '',
|
|
|
|
__('Links shortener') => $core->adminurl->get('admin.plugin.kUtRL'),
|
|
|
|
__('New link') => ''
|
2021-10-26 20:35:42 +00:00
|
|
|
]) .
|
2021-08-28 16:27:58 +00:00
|
|
|
dcPage::notices();
|
|
|
|
|
|
|
|
if (null === $kut) {
|
|
|
|
echo '<p>' . __('You must set an admin service.') . '</p>';
|
|
|
|
} else {
|
|
|
|
echo '
|
|
|
|
<div class="fieldset">
|
|
|
|
<h4>' . sprintf(__('Shorten link using service "%s"'), $kut->name) . '</h4>
|
2021-10-26 20:27:27 +00:00
|
|
|
<form id="create-link" method="post" action="' . $core->adminurl->get('admin.plugin.kUtRL') . '">
|
2021-09-02 12:32:26 +00:00
|
|
|
|
2021-08-28 16:27:58 +00:00
|
|
|
<p><label for="str">' . __('Long link:') . '</label>' .
|
|
|
|
form::field('str', 100, 255, '') . '</p>';
|
|
|
|
|
|
|
|
if ($kut->allow_custom_hash) {
|
|
|
|
echo
|
|
|
|
'<p><label for="custom">' . __('Custom short link:') . '</label>' .
|
2021-11-06 15:43:02 +00:00
|
|
|
form::field('custom', 50, 32, '') . '</p>' .
|
2021-08-28 16:27:58 +00:00
|
|
|
'<p class="form-note">' . __('Only if you want a custom short link.') . '</p>';
|
2021-08-25 23:06:22 +00:00
|
|
|
|
2021-08-28 16:27:58 +00:00
|
|
|
if ($kut->admin_service == 'local') {
|
2021-11-06 15:43:02 +00:00
|
|
|
echo '<p class="form-note">' .
|
|
|
|
__('You can use "bob!!" if you want a semi-custom link, it starts with "bob" and "!!" will be replaced by an increment value.') .
|
2021-08-28 16:27:58 +00:00
|
|
|
'</p>';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
echo '
|
2021-11-06 15:43:02 +00:00
|
|
|
<p><input type="submit" name="save" value="' . __('Save') . '" />' .
|
|
|
|
$core->formNonce() .
|
2021-08-28 16:27:58 +00:00
|
|
|
form::hidden(['part'], 'link') . '
|
|
|
|
</p></div>
|
|
|
|
</form>';
|
|
|
|
}
|
2021-10-26 20:27:27 +00:00
|
|
|
} else {
|
2021-11-06 15:43:02 +00:00
|
|
|
echo
|
2021-10-26 20:35:42 +00:00
|
|
|
dcPage::breadcrumb([
|
2021-11-06 15:43:02 +00:00
|
|
|
__('Plugins') => '',
|
|
|
|
__('Links shortener') => ''
|
2021-10-26 20:35:42 +00:00
|
|
|
]) .
|
|
|
|
dcPage::notices() .
|
|
|
|
|
2021-11-06 15:43:02 +00:00
|
|
|
'<p class="top-add"><a class="button add" href="' .
|
|
|
|
$core->adminurl->get('admin.plugin.kUtRL', ['part' => 'link']) .
|
|
|
|
'">' . __('New Link') . '</a></p>';
|
2021-08-28 16:27:58 +00:00
|
|
|
|
2021-10-03 20:09:13 +00:00
|
|
|
$kUtRL_filter->display('admin.plugin.kUtRL', form::hidden('p', 'kUtRL') . form::hidden('part', 'links'));
|
2021-08-28 16:27:58 +00:00
|
|
|
|
2021-08-28 21:17:13 +00:00
|
|
|
$list_current->display(
|
2021-10-09 15:18:04 +00:00
|
|
|
$kUtRL_filter->value('page'),
|
2021-11-06 15:43:02 +00:00
|
|
|
$kUtRL_filter->nb,
|
2021-10-26 20:27:27 +00:00
|
|
|
'<form action="' . $core->adminurl->get('admin.plugin.kUtRL') . '" method="post" id="form-entries">
|
2021-08-28 21:17:13 +00:00
|
|
|
|
|
|
|
%s
|
|
|
|
|
|
|
|
<div class="two-cols">
|
2021-09-28 22:56:31 +00:00
|
|
|
<div class="col left">
|
|
|
|
<p class="checkboxes-helpers"></p>
|
|
|
|
</div>
|
2021-08-28 21:17:13 +00:00
|
|
|
<p class="col right">
|
2021-09-28 22:56:31 +00:00
|
|
|
<input id="do-action" type="submit" value="' . __('Delete selected short links') . '" /></p>' .
|
2021-11-06 15:43:02 +00:00
|
|
|
$core->adminurl->getHiddenFormFields('admin.plugin.kUtRL', array_merge(['deletelinks' => 1], $kUtRL_filter->values(true))) .
|
2021-08-28 21:17:13 +00:00
|
|
|
$core->formNonce() . '
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
</form>',
|
2021-10-03 20:09:13 +00:00
|
|
|
$kUtRL_filter->show()
|
2021-08-28 21:17:13 +00:00
|
|
|
);
|
2021-08-28 16:27:58 +00:00
|
|
|
}
|
2021-08-28 21:17:13 +00:00
|
|
|
|
2021-08-28 16:27:58 +00:00
|
|
|
# display footer
|
|
|
|
dcPage::helpBlock('kUtRL');
|
|
|
|
|
2021-11-06 15:43:02 +00:00
|
|
|
echo '</body></html>';
|