blog->settings->addNamespace('kUtRL');
require_once __DIR__ . '/_widgets.php';
# Plugin menu
dcCore::app()->menu[dcAdmin::MENU_PLUGINS]->addItem(
__('Links shortener'),
dcCore::app()->adminurl->get('admin.plugin.kUtRL'),
urldecode(dcPage::getPF('kUtRL/icon.svg')),
preg_match('/' . preg_quote(dcCore::app()->adminurl->get('admin.plugin.kUtRL')) . '(&.*)?$/', $_SERVER['REQUEST_URI']),
dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([dcAuth::PERMISSION_ADMIN]), dcCore::app()->blog->id)
);
# Admin behaviors
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']);
}
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']);
# Admin behaviors class
class adminKutrl
{
public static function sortbyCombo()
{
return [
__('Date') => 'kut_dt',
__('Short link') => 'kut_hash',
__('Long link') => 'kut_url',
__('Service') => 'kut_service',
];
}
public static function antispamDashboardFavorites(dcFavorites $favs)
{
$favs->register(
'kUtRL',
[
'title' => __('Links shortener'),
'url' => dcCore::app()->adminurl->get('admin.plugin.kUtRL'),
'small-icon' => dcPage::getPF('kUtRL/icon.png'),
'large-icon' => dcPage::getPF('kUtRL/icon-b.png'),
'permissions' => dcCore::app()->auth->makePermissions([dcAuth::PERMISSION_ADMIN]),
]
);
}
public static function adminColumnsLists($cols)
{
$cols['kUtRL'] = [
__('Links shortener'),
[
'kut_hash' => [true, __('Hash')],
'kut_dt' => [true, __('Date')],
'kut_service' => [true, __('Service')],
],
];
}
public static function adminFiltersLists($sorts)
{
$sorts['kUtRL'] = [
__('Links shortener'),
self::sortbyCombo(),
'kut_dt',
'desc',
[__('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)
{
$s = dcCore::app()->blog->settings->kUtRL;
if (!$s->kutrl_active || !$s->kutrl_admin_service) {
return null;
}
if (null === ($kut = kUtRL::quickPlace('admin'))) {
return null;
}
if ($post) {
$post_url = $post->getURL();
$rs = $kut->isKnowUrl($post_url);
} else {
$post_url = '';
$rs = false;
}
$ret = '
' . __('Short link') . '
' .
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']);
}
$ret .= '
';
if ($kut->allow_custom_hash) {
$ret .= '
' .
'
';
}
} else {
$count = $rs->counter;
if ($count == 0) {
$title = __('never followed');
} elseif ($count == 1) {
$title = __('followed one time');
} else {
$title = sprintf(__('followed %s times'), $count);
}
$href = $kut->url_base . $rs->hash;
$ret .= '
' .
'
' . $href . '
';
}
$ret .= '
';
$sidebar_items['options-box']['items']['kUtRL'] = $ret;
}
public static function adminAfterPostUpdate($cur, $post_id)
{
$s = dcCore::app()->blog->settings->kUtRL;
# Create: see adminAfterPostCreate
if (!empty($_POST['kutrl_create']) || !$s->kutrl_active) {
return null;
}
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;
}
$rs = dcCore::app()->blog->getPosts(['post_id' => $post_id]);
if ($rs->isEmpty()) {
return null;
}
$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);
$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)) {
dcCore::app()->callBehavior('adminAfterKutrlCreate', $rs, $title);
}
}
}
public static function adminAfterPostCreate($cur, $post_id)
{
$s = dcCore::app()->blog->settings->kUtRL;
if (empty($_POST['kutrl_create']) || !$s->kutrl_active) {
return null;
}
if (null === ($kut = kUtRL::quickPlace('admin'))) {
return null;
}
$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;
$rs = $kut->hash($rs->getURL(), $custom);
$url = $kut->url_base . $rs->hash;
# ex: Send new url to messengers
if (!empty($rs)) {
dcCore::app()->callBehavior('adminAfterKutrlCreate', $rs, $title);
}
}
public static function adminBeforePostDelete($post_id)
{
$s = dcCore::app()->blog->settings->kUtRL;
if (!$s->kutrl_active) {
return null;
}
if (null === ($kut = kUtRL::quickPlace('admin'))) {
return null;
}
$rs = dcCore::app()->blog->getPosts(['post_id' => $post_id]);
if ($rs->isEmpty()) {
return null;
}
$kut->remove($rs->getURL());
}
public static function adminPostsActions(dcPostsActions $pa)
{
$s = dcCore::app()->blog->settings->kUtRL;
if (!$s->kutrl_active
|| !dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([dcAuth::PERMISSION_ADMIN]), dcCore::app()->blog->id)) {
return null;
}
$pa->addAction(
[__('Links shortener') => [__('Create short link') => 'kutrl_create']],
['adminKutrl', 'callbackCreate']
);
$pa->addAction(
[__('Links shortener') => [__('Delete short link') => 'kutrl_delete']],
['adminKutrl', 'callbackDelete']
);
}
public static function callbackCreate(dcPostsActions $pa, ArrayObject $post)
{
if (null === ($kut = kUtRL::quickPlace('admin'))) {
return null;
}
# No entry
$posts_ids = $pa->getIDs();
if (empty($posts_ids)) {
throw new Exception(__('No entry selected'));
}
# No right
if (!dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([dcAuth::PERMISSION_ADMIN]), dcCore::app()->blog->id)) {
throw new Exception(__('No enough right'));
}
# retrieve posts info and create hash
$posts = dcCore::app()->blog->getPosts(['post_id' => $posts_ids]);
while ($posts->fetch()) {
$kut->hash($posts->getURL());
}
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
if (!dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([dcAuth::PERMISSION_ADMIN]), dcCore::app()->blog->id)) {
throw new Exception(__('No enough right'));
}
# 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);
}
}
# Import/export behaviors for Import/export plugin
class backupKutrl
{
public static function exportSingle($exp, $blog_id)
{
$exp->export(
'kutrl',
'SELECT kut_id, blog_id, kut_service, kut_type, ' .
'kut_hash, kut_url, kut_dt, kut_password, kut_counter ' .
'FROM ' . dcCore::app()->prefix . initkUtRL::KURL_TABLE_NAME . ' ' .
"WHERE blog_id = '" . $blog_id . "' "
);
}
public static function exportFull($exp)
{
$exp->exportTable('kutrl');
}
public static function importInit($bk)
{
$bk->cur_kutrl = dcCore::app()->con->openCursor(dcCore::app()->prefix . initkUtRL::KURL_TABLE_NAME);
$bk->kutrl = new kutrlLog();
}
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);
}
}
}
public static function importFull($line, $bk)
{
if ($line->__name == 'kutrl') {
$bk->cur_kutrl->clean();
$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();
}
}
}