kUtRL/src/Backend.php

381 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-12-22 14:37:09 +00:00
dcCore::app()->blog->settings->addNamespace(basename(__DIR__));
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-12-22 14:37:09 +00:00
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']),
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-12-22 20:18:49 +00:00
if (dcCore::app()->blog->settings->get(basename(__DIR__))->get('active')) {
2022-12-22 14:37:09 +00:00
dcCore::app()->addBehavior('adminDashboardFavoritesV2', ['adminKutrl', 'antispamDashboardFavoritesV2']);
dcCore::app()->addBehavior('adminColumnsListsV2', ['adminKutrl', 'adminColumnsListsV2']);
dcCore::app()->addBehavior('adminFiltersListsV2', ['adminKutrl', 'adminFiltersListsV2']);
2022-11-20 16:15:36 +00:00
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-12-22 14:37:09 +00:00
dcCore::app()->addBehavior('exportFullV2', ['backupKutrl', 'exportFullV2']);
dcCore::app()->addBehavior('exportSingleV2', ['backupKutrl', 'exportSingleV2']);
dcCore::app()->addBehavior('importInitV2', ['backupKutrl', 'importInitV2']);
dcCore::app()->addBehavior('importSingleV2', ['backupKutrl', 'importSingleV2']);
dcCore::app()->addBehavior('importFullV2', ['backupKutrl', 'importFullV2']);
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-12-22 14:37:09 +00:00
public static function antispamDashboardFavoritesV2(dcFavorites $favs)
{
$favs->register(
2021-11-06 15:43:02 +00:00
'kUtRL',
[
'title' => __('Links shortener'),
2022-12-22 14:37:09 +00:00
'url' => dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__)),
'small-icon' => dcPage::getPF(basename(__DIR__) . '/icon.png'),
'large-icon' => dcPage::getPF(basename(__DIR__) . '/icon-b.png'),
2022-12-01 22:22:10 +00:00
'permissions' => dcCore::app()->auth->makePermissions([dcAuth::PERMISSION_ADMIN]),
]
);
}
2022-12-22 14:37:09 +00:00
public static function adminColumnsListsV2($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-12-22 14:37:09 +00:00
public static function adminFiltersListsV2($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()
{
2022-12-22 14:37:09 +00:00
return dcPage::jsModuleLoad(basename(__DIR__) . '/js/posts.js');
}
public static function adminPostFormItems($main_items, $sidebar_items, $post)
{
2022-12-22 14:37:09 +00:00
$s = dcCore::app()->blog->settings->get(basename(__DIR__));
2022-12-22 20:18:49 +00:00
if (!$s->get('active') || !$s->get('active')) {
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) {
2022-12-22 20:18:49 +00:00
if (empty($_POST['kutrl_old_post_url']) && $s->get('admin_entry_default')) {
$chk = true;
} else {
$chk = !empty($_POST['kutrl_create']);
}
2021-11-06 15:43:02 +00:00
$ret .= '<p><label class="classic">' .
2022-12-22 14:37:09 +00:00
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:') . ' ' .
2022-12-22 14:37:09 +00:00
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">' .
2022-12-22 14:37:09 +00:00
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)
{
# Create: see adminAfterPostCreate
2022-12-22 20:18:49 +00:00
if (!empty($_POST['kutrl_create']) || !dcCore::app()->blog->settings->get(basename(__DIR__))->get('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);
2022-12-22 14:37:09 +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-12-22 20:18:49 +00:00
if (empty($_POST['kutrl_create']) || !dcCore::app()->blog->settings->get(basename(__DIR__))->get('active')) {
return null;
}
2022-12-22 14:37:09 +00:00
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-12-22 20:18:49 +00:00
if (!dcCore::app()->blog->settings->get(basename(__DIR__))->get('active')) {
return null;
}
2022-12-22 14:37:09 +00:00
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-12-22 20:18:49 +00:00
if (!dcCore::app()->blog->settings->get(basename(__DIR__))->get('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
# 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-12-22 14:37:09 +00:00
if (null === ($kut = kUtRL::quickPlace('admin'))) {
return null;
}
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
2022-12-22 14:37:09 +00:00
if (null === ($kut = kUtRL::quickPlace('admin'))) {
return null;
}
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-12-22 14:37:09 +00:00
public static function exportSingleV2($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-12-22 14:37:09 +00:00
public static function exportFullV2($exp)
{
$exp->exportTable('kutrl');
}
2022-12-22 14:37:09 +00:00
public static function importInitV2($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-12-22 14:37:09 +00:00
public static function importSingleV2($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-12-22 14:37:09 +00:00
public static function importFullV2($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
}