kUtRL/_admin.php

383 lines
13 KiB
PHP
Raw Normal View History

2021-08-25 23:06:22 +00:00
<?php
/**
* @brief kUtRL, a plugin for Dotclear 2
2021-11-06 15:43:02 +00:00
*
* @package Dotclear
* @subpackage Plugin
2021-11-06 15:43:02 +00:00
*
* @author Jean-Christian Denis and contributors
2021-11-06 15:43:02 +00:00
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('DC_CONTEXT_ADMIN')) {
return null;
}
2021-08-25 23:06:22 +00:00
2022-11-20 16:15:36 +00:00
dcCore::app()->blog->settings->addNamespace('kUtRL');
2022-11-20 16:15:36 +00:00
require_once __DIR__ . '/_widgets.php';
2021-08-25 23:06:22 +00:00
# Plugin menu
2022-11-20 16:15:36 +00:00
dcCore::app()->menu[dcAdmin::MENU_PLUGINS]->addItem(
__('Links shortener'),
2022-11-20 16:15:36 +00:00
dcCore::app()->adminurl->get('admin.plugin.kUtRL'),
2022-12-01 22:22:10 +00:00
urldecode(dcPage::getPF('kUtRL/icon.svg')),
2022-11-20 16:15:36 +00:00
preg_match('/' . preg_quote(dcCore::app()->adminurl->get('admin.plugin.kUtRL')) . '(&.*)?$/', $_SERVER['REQUEST_URI']),
2022-12-01 22:22:10 +00:00
dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([dcAuth::PERMISSION_ADMIN]), dcCore::app()->blog->id)
);
2021-08-25 23:06:22 +00:00
# Admin behaviors
2022-11-20 16:15:36 +00:00
if (dcCore::app()->blog->settings->kUtRL->kutrl_active) {
dcCore::app()->addBehavior('adminDashboardFavoritesV2', ['adminKutrl', 'antispamDashboardFavorites']);
dcCore::app()->addBehavior('adminColumnsListsV2', ['adminKutrl', 'adminColumnsLists']);
dcCore::app()->addBehavior('adminFiltersListsV2', ['adminKutrl', 'adminFiltersLists']);
dcCore::app()->addBehavior('adminPostHeaders', ['adminKutrl', 'adminPostHeaders']);
dcCore::app()->addBehavior('adminPostFormItems', ['adminKutrl', 'adminPostFormItems']);
dcCore::app()->addBehavior('adminAfterPostUpdate', ['adminKutrl', 'adminAfterPostUpdate']); // update existing short url
dcCore::app()->addBehavior('adminAfterPostUpdate', ['adminKutrl', 'adminAfterPostCreate']); // create new short url
dcCore::app()->addBehavior('adminAfterPostCreate', ['adminKutrl', 'adminAfterPostCreate']);
dcCore::app()->addBehavior('adminBeforePostDelete', ['adminKutrl', 'adminBeforePostDelete']);
dcCore::app()->addBehavior('adminPostsActions', ['adminKutrl', 'adminPostsActions']);
2021-08-25 23:06:22 +00:00
}
2022-11-20 16:15:36 +00:00
dcCore::app()->addBehavior('exportFullV2', ['backupKutrl', 'exportFull']);
dcCore::app()->addBehavior('exportSingleV2', ['backupKutrl', 'exportSingle']);
dcCore::app()->addBehavior('importInitV2', ['backupKutrl', 'importInit']);
dcCore::app()->addBehavior('importSingleV2', ['backupKutrl', 'importSingle']);
dcCore::app()->addBehavior('importFullV2', ['backupKutrl', 'importFull']);
2021-08-25 23:06:22 +00:00
# Admin behaviors class
class adminKutrl
{
public static function sortbyCombo()
{
return [
2021-10-26 21:18:12 +00:00
__('Date') => 'kut_dt',
__('Short link') => 'kut_hash',
__('Long link') => 'kut_url',
2022-11-20 16:15:36 +00:00
__('Service') => 'kut_service',
];
}
2022-11-20 16:15:36 +00:00
public static function antispamDashboardFavorites(dcFavorites $favs)
{
$favs->register(
2021-11-06 15:43:02 +00:00
'kUtRL',
[
'title' => __('Links shortener'),
2022-11-20 16:15:36 +00:00
'url' => dcCore::app()->adminurl->get('admin.plugin.kUtRL'),
'small-icon' => dcPage::getPF('kUtRL/icon.png'),
'large-icon' => dcPage::getPF('kUtRL/icon-b.png'),
2022-12-01 22:22:10 +00:00
'permissions' => dcCore::app()->auth->makePermissions([dcAuth::PERMISSION_ADMIN]),
]
);
}
2022-11-20 16:15:36 +00:00
public static function adminColumnsLists($cols)
{
$cols['kUtRL'] = [
2021-10-26 21:18:12 +00:00
__('Links shortener'),
[
'kut_hash' => [true, __('Hash')],
'kut_dt' => [true, __('Date')],
2022-11-20 16:15:36 +00:00
'kut_service' => [true, __('Service')],
],
];
}
2022-11-20 16:15:36 +00:00
public static function adminFiltersLists($sorts)
{
$sorts['kUtRL'] = [
2021-10-26 21:18:12 +00:00
__('Links shortener'),
self::sortbyCombo(),
'kut_dt',
'desc',
2022-11-20 16:15:36 +00:00
[__('links per page'), 30],
];
}
public static function adminPostHeaders()
{
return dcPage::jsLoad(dcPage::getPF('kUtRL/js/posts.js'));
}
public static function adminPostFormItems($main_items, $sidebar_items, $post)
{
2022-11-20 16:15:36 +00:00
$s = dcCore::app()->blog->settings->kUtRL;
if (!$s->kutrl_active || !$s->kutrl_admin_service) {
return null;
}
2022-11-20 16:15:36 +00:00
if (null === ($kut = kUtRL::quickPlace('admin'))) {
return null;
}
if ($post) {
$post_url = $post->getURL();
2021-11-06 15:43:02 +00:00
$rs = $kut->isKnowUrl($post_url);
} else {
$post_url = '';
2021-11-06 15:43:02 +00:00
$rs = false;
}
2021-11-06 15:43:02 +00:00
$ret = '<div id="kUtRL"><h5>' . __('Short link') . '</h5>' .
form::hidden(['kutrl_old_post_url'], $post_url);
if (!$rs) {
if (empty($_POST['kutrl_old_post_url']) && $s->kutrl_admin_entry_default) {
$chk = true;
} else {
$chk = !empty($_POST['kutrl_create']);
}
2021-11-06 15:43:02 +00:00
$ret .= '<p><label class="classic">' .
form::checkbox('kutrl_create', 1, $chk, '', 3) . ' ' .
__('Create short link') . '</label></p>';
if ($kut->allow_custom_hash) {
2021-11-06 15:43:02 +00:00
$ret .= '<p class="classic">' .
'<label for="custom">' . __('Custom short link:') . ' ' .
form::field('kutrl_create_custom', 32, 32, '', 3) .
'</label></p>';
}
} else {
$count = $rs->counter;
if ($count == 0) {
$title = __('never followed');
} elseif ($count == 1) {
$title = __('followed one time');
} else {
2021-11-06 15:43:02 +00:00
$title = sprintf(__('followed %s times'), $count);
}
$href = $kut->url_base . $rs->hash;
2021-11-06 15:43:02 +00:00
$ret .= '<p><label class="classic">' .
form::checkbox('kutrl_delete', 1, !empty($_POST['kutrl_delete']), '', 3) . ' ' .
__('Delete short link') . '</label></p>' .
'<p><a href="' . $href . '" ' . 'title="' . $title . '">' . $href . '</a></p>';
}
$ret .= '</div>';
$sidebar_items['options-box']['items']['kUtRL'] = $ret;
}
public static function adminAfterPostUpdate($cur, $post_id)
{
2022-11-20 16:15:36 +00:00
$s = dcCore::app()->blog->settings->kUtRL;
# Create: see adminAfterPostCreate
if (!empty($_POST['kutrl_create']) || !$s->kutrl_active) {
return null;
}
2022-11-20 16:15:36 +00:00
if (null === ($kut = kUtRL::quickPlace('admin'))) {
return null;
}
if (empty($_POST['kutrl_old_post_url'])) {
return null;
}
$old_post_url = $_POST['kutrl_old_post_url'];
if (!($rs = $kut->isKnowUrl($old_post_url))) {
return null;
}
2022-11-20 16:15:36 +00:00
$rs = dcCore::app()->blog->getPosts(['post_id' => $post_id]);
if ($rs->isEmpty()) {
return null;
}
2021-11-06 15:43:02 +00:00
$title = html::escapeHTML($rs->post_title);
$new_post_url = $rs->getURL();
# Delete
if (!empty($_POST['kutrl_delete'])) {
$kut->remove($old_post_url);
# Update
} else {
if ($old_post_url == $new_post_url) {
return null;
}
$kut->remove($old_post_url);
2021-11-06 15:43:02 +00:00
$rs = $kut->hash($new_post_url, $custom); // better to update (not yet implemented)
$url = $kut->url_base . $rs->hash;
# ex: Send new url to messengers
if (!empty($rs)) {
2022-11-20 16:15:36 +00:00
dcCore::app()->callBehavior('adminAfterKutrlCreate', $rs, $title);
}
}
}
public static function adminAfterPostCreate($cur, $post_id)
{
2022-11-20 16:15:36 +00:00
$s = dcCore::app()->blog->settings->kUtRL;
if (empty($_POST['kutrl_create']) || !$s->kutrl_active) {
return null;
}
2022-11-20 16:15:36 +00:00
if (null === ($kut = kUtRL::quickPlace('admin'))) {
return null;
}
2022-11-20 16:15:36 +00:00
$rs = dcCore::app()->blog->getPosts(['post_id' => $post_id]);
if ($rs->isEmpty()) {
return null;
}
$title = html::escapeHTML($rs->post_title);
$custom = !empty($_POST['kutrl_create_custom']) && $kut->allow_custom_hash ?
$_POST['kutrl_create_custom'] : null;
2021-11-06 15:43:02 +00:00
$rs = $kut->hash($rs->getURL(), $custom);
$url = $kut->url_base . $rs->hash;
# ex: Send new url to messengers
if (!empty($rs)) {
2022-11-20 16:15:36 +00:00
dcCore::app()->callBehavior('adminAfterKutrlCreate', $rs, $title);
}
}
public static function adminBeforePostDelete($post_id)
{
2022-11-20 16:15:36 +00:00
$s = dcCore::app()->blog->settings->kUtRL;
if (!$s->kutrl_active) {
return null;
}
2022-11-20 16:15:36 +00:00
if (null === ($kut = kUtRL::quickPlace('admin'))) {
return null;
}
2022-11-20 16:15:36 +00:00
$rs = dcCore::app()->blog->getPosts(['post_id' => $post_id]);
if ($rs->isEmpty()) {
return null;
}
$kut->remove($rs->getURL());
}
2022-11-20 16:15:36 +00:00
public static function adminPostsActions(dcPostsActions $pa)
{
2022-11-20 16:15:36 +00:00
$s = dcCore::app()->blog->settings->kUtRL;
2021-11-06 15:43:02 +00:00
if (!$s->kutrl_active
2022-12-01 22:22:10 +00:00
|| !dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([dcAuth::PERMISSION_ADMIN]), dcCore::app()->blog->id)) {
return null;
}
2022-11-20 16:15:36 +00:00
$pa->addAction(
[__('Links shortener') => [__('Create short link') => 'kutrl_create']],
['adminKutrl', 'callbackCreate']
);
$pa->addAction(
[__('Links shortener') => [__('Delete short link') => 'kutrl_delete']],
['adminKutrl', 'callbackDelete']
);
}
2022-11-20 16:15:36 +00:00
public static function callbackCreate(dcPostsActions $pa, ArrayObject $post)
{
2022-11-20 16:15:36 +00:00
if (null === ($kut = kUtRL::quickPlace('admin'))) {
return null;
}
2022-11-20 16:15:36 +00:00
# No entry
$posts_ids = $pa->getIDs();
if (empty($posts_ids)) {
throw new Exception(__('No entry selected'));
}
2022-11-20 16:15:36 +00:00
# No right
2022-12-01 22:22:10 +00:00
if (!dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([dcAuth::PERMISSION_ADMIN]), dcCore::app()->blog->id)) {
2022-11-20 16:15:36 +00:00
throw new Exception(__('No enough right'));
}
2022-11-20 16:15:36 +00:00
# retrieve posts info and create hash
$posts = dcCore::app()->blog->getPosts(['post_id' => $posts_ids]);
while ($posts->fetch()) {
2022-11-20 16:15:36 +00:00
$kut->hash($posts->getURL());
}
2022-11-20 16:15:36 +00:00
dcAdminNotices::addSuccessNotice(__('Posts short links have been created.'));
$pa->redirect(true);
}
public static function callbackDelete(dcPostsActions $pa, ArrayObject $post)
{
# No entry
$posts_ids = $pa->getIDs();
if (empty($posts_ids)) {
throw new Exception(__('No entry selected'));
}
# No right
2022-12-01 22:22:10 +00:00
if (!dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([dcAuth::PERMISSION_ADMIN]), dcCore::app()->blog->id)) {
2022-11-20 16:15:36 +00:00
throw new Exception(__('No enough right'));
}
2022-11-20 16:15:36 +00:00
# retrieve posts info and create hash
$posts = dcCore::app()->blog->getPosts(['post_id' => $posts_ids]);
while ($posts->fetch()) {
$kut->remove($posts->getURL());
}
dcAdminNotices::addSuccessNotice(__('Posts short links have been created.'));
$pa->redirect(true);
}
2021-08-25 23:06:22 +00:00
}
# Import/export behaviors for Import/export plugin
class backupKutrl
{
2022-11-20 16:15:36 +00:00
public static function exportSingle($exp, $blog_id)
{
2021-11-06 15:43:02 +00:00
$exp->export(
'kutrl',
'SELECT kut_id, blog_id, kut_service, kut_type, ' .
'kut_hash, kut_url, kut_dt, kut_password, kut_counter ' .
2022-12-01 22:22:10 +00:00
'FROM ' . dcCore::app()->prefix . initkUtRL::KURL_TABLE_NAME . ' ' .
"WHERE blog_id = '" . $blog_id . "' "
);
}
2022-11-20 16:15:36 +00:00
public static function exportFull($exp)
{
$exp->exportTable('kutrl');
}
2022-11-20 16:15:36 +00:00
public static function importInit($bk)
{
2022-12-01 22:22:10 +00:00
$bk->cur_kutrl = dcCore::app()->con->openCursor(dcCore::app()->prefix . initkUtRL::KURL_TABLE_NAME);
2022-11-20 16:15:36 +00:00
$bk->kutrl = new kutrlLog();
}
2022-11-20 16:15:36 +00:00
public static function importSingle($line, $bk)
{
if ($line->__name == 'kutrl') {
# Do nothing if str/type exists !
if (false === $bk->kutrl->select($line->kut_url, $line->kut_hash, $line->kut_type, $line->kut_service)) {
$bk->kutrl->insert($line->kut_url, $line->kut_hash, $line->kut_type, $line->kut_service);
}
}
}
2022-11-20 16:15:36 +00:00
public static function importFull($line, $bk)
{
if ($line->__name == 'kutrl') {
$bk->cur_kutrl->clean();
2021-11-06 15:43:02 +00:00
$bk->cur_kutrl->kut_id = (int) $line->kut_id;
$bk->cur_kutrl->blog_id = (string) $line->blog_id;
$bk->cur_kutrl->kut_service = (string) $line->kut_service;
$bk->cur_kutrl->kut_type = (string) $line->kut_type;
$bk->cur_kutrl->kut_hash = (string) $line->kut_hash;
$bk->cur_kutrl->kut_url = (string) $line->kut_url;
$bk->cur_kutrl->kut_dt = (string) $line->miniurl_dt;
$bk->cur_kutrl->kut_counter = (int) $line->kut_counter;
$bk->cur_kutrl->kut_password = (string) $line->kut_password;
$bk->cur_kutrl->insert();
}
}
2021-11-06 15:43:02 +00:00
}