blog->settings->kUtRL; # Default values $show_filters = false; $p_url = $core->adminurl->get('admin.plugin.kUtRL'); $part = isset($_REQUEST['part']) ? $_REQUEST['part'] : 'links'; $action = isset($_POST['action']) ? $_POST['action'] : ''; # used in setting & links if (in_array($part, ['setting', 'links'])) { $services_combo = []; foreach(kutrl::getServices($core) as $service_id => $service) { $o = new $service($core); $services_combo[__($o->name)] = $o->id; } $ext_services_combo = array_merge([__('Disabled')=>''], $services_combo); $lst_services_combo = array_merge(['-'=>''], $services_combo); } # used in setting & service if (in_array($part, ['setting', 'service'])) { $img_green = 'ok'; $img_red = 'fail'; } # setting if ($part == 'setting') { $s_active = (boolean) $s->kutrl_active; $s_plugin_service = (string) $s->kutrl_plugin_service; $s_admin_service = (string) $s->kutrl_admin_service; $s_tpl_service = (string) $s->kutrl_tpl_service; $s_wiki_service = (string) $s->kutrl_wiki_service; $s_allow_external_url = (boolean) $s->kutrl_allow_external_url; $s_tpl_passive = (boolean) $s->kutrl_tpl_passive; $s_tpl_active = (boolean) $s->kutrl_tpl_active; $s_admin_entry_default = (string) $s->kutrl_admin_entry_default; if (!empty($_POST['save'])) { try { $s_active = !empty($_POST['s_active']); $s_admin_service = (string) $_POST['s_admin_service']; $s_plugin_service = (string) $_POST['s_plugin_service']; $s_tpl_service = (string) $_POST['s_tpl_service']; $s_wiki_service = (string) $_POST['s_wiki_service']; $s_allow_external_url = !empty($_POST['s_allow_external_url']); $s_tpl_passive = !empty($_POST['s_tpl_passive']); $s_tpl_active = !empty($_POST['s_tpl_active']); $s_admin_entry_default = !empty($_POST['s_admin_entry_default']); $s->put('kutrl_active', $s_active); $s->put('kutrl_plugin_service', $s_plugin_service); $s->put('kutrl_admin_service', $s_admin_service); $s->put('kutrl_tpl_service', $s_tpl_service); $s->put('kutrl_wiki_service', $s_wiki_service); $s->put('kutrl_allow_external_url', $s_allow_external_url); $s->put('kutrl_tpl_passive', $s_tpl_passive); $s->put('kutrl_tpl_active', $s_tpl_active); $s->put('kutrl_admin_entry_default', $s_admin_entry_default); $core->blog->triggerBlog(); dcPage::addSuccessNotice( __('Configuration successfully saved') ); http::redirect($p_url . '&part=setting'); } catch (Exception $e) { $core->error->add($e->getMessage()); } } } # services if ($part == 'service' && !empty($_POST['save'])) { try { foreach(kutrl::getServices($core) as $service_id => $service) { $o = new $service($core); $o->saveSettings(); } $core->blog->triggerBlog(); dcPage::addSuccessNotice( __('Configuration successfully saved') ); http::redirect($p_url . '&part=service'); } catch (Exception $e) { $core->error->add($e->getMessage()); } } # link creation if ($part == 'link') { $kut = kutrl::quickPlace('admin'); if (!empty($_POST['save'])) { try { if (null === $kut) { throw new Exception('Unknow service'); } $url = trim($core->con->escape($_POST['str'])); $hash = empty($_POST['custom']) ? null : $_POST['custom']; 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))) { $url = $rs->url; $new_url = $kut->url_base .$rs->hash; dcPage::addSuccessNotice(sprintf( __('Short link for %s is %s'), '' . html::escapeHTML($url) . '', '' . $new_url . '' )); } else { if (false === ($rs = $kut->hash($url, $hash))) { if ($kut->error->flag()) { throw new Exception($kut->error->toHTML()); } throw new Exception(__('Failed to create short link. This could be caused by a service failure.')); } else { $url = $rs->url; $new_url = $kut->url_base . $rs->hash; dcPage::addSuccessNotice(sprintf( __('Short link for %s is %s'), '' . html::escapeHTML($url) . '', '' . $new_url . '' )); # ex: Send new url to messengers if (!empty($rs)) { $core->callBehavior('adminAfterKutrlCreate', $core, $rs,__('New short URL')); } } } } catch (Exception $e) { $core->error->add($e->getMessage()); } } } # links if ($part == 'links') { $log = new kutrlLog($core); $sortby_combo = adminKutrl::sortbyCombo(); $order_combo = [__('Descending') => 'desc', __('Ascending') => 'asc']; $core->auth->user_prefs->addWorkspace('interface'); $sorts_user = @$core->auth->user_prefs->interface->sorts; $default_sortby = $sorts_user['kUtRL'][0] ?? 'kut_dt'; $default_order = $sorts_user['kUtRL'][1] ?? 'desc'; $nb_per_page = !empty($sorts_user['kUtRL'][2]) ? $sorts_user['kUtRL'][2] : 30; $sortby = !empty($_GET['sortby']) ? $_GET['sortby'] : $default_sortby; $order = !empty($_GET['order']) ? $_GET['order'] : $default_order; $urlsrv = !empty($_GET['urlsrv']) ? $_GET['urlsrv'] : ''; $page = !empty($_GET['page']) ? max(1, (integer) $_GET['page']) : 1; $show_filters = false; if (!empty($_GET['nb']) && (integer) $_GET['nb'] > 0) { if ($nb_per_page != (integer) $_GET['nb']) { $show_filters = true; } $nb_per_page = (integer) $_GET['nb']; } $params = []; $params['limit'] = [(($page-1)*$nb_per_page), $nb_per_page]; if (!in_array($sortby, $sortby_combo)) { $sortby = $default_sortby; } if (!in_array($order, $order_combo)) { $order = $default_order; } $params['order'] = $sortby . ' ' . $order; if ($urlsrv != '' && in_array($urlsrv, $lst_services_combo)) { $params['kut_type'] = $urlsrv; } if ($sortby != $default_sortby || $order != $default_order || $urlsrv != '') { $show_filters = true; } try { $list_all = $log->getLogs($params); $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()); } if (!empty($_POST['deletelinks'])) { try { foreach($_POST['entries'] as $id) { $rs = $log->getLogs(['kut_id' => $id]); if ($rs->isEmpty()) { continue; } if (null === ($o = kutrl::quickService($rs->kut_type))) { continue; } $o->remove($rs->kut_url); } $core->blog->triggerBlog(); dcPage::addSuccessNotice( __('Links successfully deleted') ); http::redirect($p_url . '&part=links&urlsrv=' . $urlsrv . '&sortby=' . $sortby . '&order=' . $order . '&nb=' . $nb_per_page . '&page=' . $page); } catch (Exception $e) { $core->error->add($e->getMessage()); } } } # display header echo 'kUtRL, ' . __('Links shortener') . ''; if ($part == 'links') { echo dcPage::jsVars(['dotclear.filter_reset_url' => $core->adminurl->get('admin.plugin.kUtRL', ['part' => 'links'])]) . dcPage::jsFilterControl($show_filters) . dcPage::jsLoad(dcPage::getPF('kUtRL/js/admin.js')); } echo ''; # display setting if ($part == 'setting') { echo dcPage::breadcrumb( [ __('Links shortener') => '', '' . __('Plugin configuration') . '' => '', __('Services configuration') => $p_url .'&part=service' ], ['hl' => false] ) . dcPage::notices() . '

' . __('Plugin configuration') . '

' . __('Back to links list') . '

' . __('Plugin activation') . '

' . __('Behaviors') . '

' . __('Not only link started with this blog URL could be shortened.') . '

' . __('If this extension is disabled and the passive mode is enabled, "kutrl" tags (like EntryKurl) will display long urls instead of nothing on templates.') . '

' . __('If the active mode is enabled, all know default template tags (like EntryURL) will display short urls instead of long ones on templates.') . '
' . __('You can disable URL shortening for a specific template tag by adding attribute disable_kutrl="1" to it . ') . '

' . __('We strongly discourage using active mode as it crashes public post form and complex url if theme is not customize for kUtRL.') . '

' . __('This can be changed on page of creation/edition of an entry.') . '

' . __('Default services') . '

' . __('Service to use in this admin page and on edit page of an entry.') . '

' . __('Service to use on third part plugins.') . '

' . __('Shorten links automatically when using template value like "EntryKutrl".') . '

' . __('Shorten links automatically found in contents using wiki synthax.') . '

' . $core->formNonce() . form::hidden(['part'], 'setting') . '

'; } # display service if ($part == 'service') { echo dcPage::breadcrumb( [ __('Links shortener') => '', __('Plugin configuration') => $p_url . '&part=setting', '' . __('Services configuration') . '' => '' ], ['hl' => false] ) . dcPage::notices() . '

' . __('Services configuration') . '

' . '

' . __('Back to links list') . '

' . '
'; foreach(kutrl::getServices($core) as $service_id => $service) { $o = new $service($core); echo '

' . $o->name . '

'; if (!empty($_POST['save'])) { echo '

' . ( $o->testService() ? $img_green . ' ' . sprintf(__('%s API is well configured and runing.'), $o->name) : $img_red . ' ' . sprintf(__('Failed to test %s API.'), $o->name) ) . '

'; //if ($o->error->flag()) { echo $o->error->toHTML(); //} } if ($o->home != '') { echo '

' . sprintf(__('Learn more about %s.'), $o->name) . '

'; } $o->settingsForm(); echo '
'; } echo '

' . $core->formNonce() . form::hidden(['part'], 'service') . '

'; } # display link creation if ($part == 'link') { echo dcPage::breadcrumb( [ __('Links shortener') => '', __('Links') => $core->adminurl->get('admin.plugin.kUtRL'), '' . __('New link') . '' => '' ], ['hl' => false] ) . dcPage::notices(); if (null === $kut) { echo '

' . __('You must set an admin service.') . '

'; } else { echo '

' . sprintf(__('Shorten link using service "%s"'), $kut->name) . '

'; } } if ($part == 'links') { echo dcPage::breadcrumb( [ __('Links shortener') => '', '' . __('Links') . '' => '', __('New link') => $core->adminurl->get('admin.plugin.kUtRL').'&part=link' ], ['hl' => false] ) . dcPage::notices(); echo '

' . __('Show filters and display options') . '

' . __('Filters') . '

' . form::combo('urlsrv', $lst_services_combo, $urlsrv) . '

' . __('Display options') . '

' . form::combo('sortby', $sortby_combo, $sortby) . '

' . form::combo('order', $order_combo, $order) . '

' . __('Show') . '

' . form::hidden('part', 'links') . form::hidden('p', 'kUtRL') . form::hidden('filters-options-id', 'kUtRL') . '

' . __('Save current options') . '


'; $list_current->display( $page, $nb_per_page, '
%s

' . $core->adminurl->getHiddenFormFields( 'admin.plugin.kUtRL',[ 'deletelinks' => 1, 'urlsrv' => $urlsrv, 'sortby' => $sortby, 'order' => $order, 'page' => $page, 'nb' => $nb_per_page, 'part' => 'links' ] ) . $core->formNonce() . '

', $show_filters ); } # display footer dcPage::helpBlock('kUtRL'); echo '

' . __('Plugin configuration') . ' - ' . __('Services configuration') . ' - kUtRL - ' . $core->plugins->moduleInfo('kUtRL', 'version') . '  ' . __('kUtRL') . '

';