From 059e64adb16b128ae19cac4a1df6cffb6d580c64 Mon Sep 17 00:00:00 2001 From: Karthik Kasturi Date: Sat, 9 Aug 2025 00:01:25 +0100 Subject: [PATCH] Auto Shortening URLs --- cfg/conf.sample.php | 5 +++++ i18n/en.json | 1 + js/privatebin.js | 17 +++++++++++++++++ lib/Configuration.php | 1 + lib/Controller.php | 1 + tpl/bootstrap.php | 10 +++++++++- tpl/bootstrap5.php | 10 +++++++++- 7 files changed, 43 insertions(+), 2 deletions(-) diff --git a/cfg/conf.sample.php b/cfg/conf.sample.php index 4b2874d6..8883716f 100644 --- a/cfg/conf.sample.php +++ b/cfg/conf.sample.php @@ -84,6 +84,11 @@ languageselection = false ; the documents encryption key. ; urlshortener = "https://shortener.example.com/api?link=" +; (optional) Whether to shorten the URL by default when a new document is created. +; If set to true, the "Shorten URL" functionality will be automatically called. +; This only works if the "urlshortener" option is set. +; shortenbydefault = false + ; (optional) Let users create a QR code for sharing the document URL with one click. ; It works both when a new document is created and when you view a document. ; qrcode = true diff --git a/i18n/en.json b/i18n/en.json index 791677ff..6649c797 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -210,6 +210,7 @@ "Encrypted note on %s": "Encrypted note on %s", "Visit this link to see the note. Giving the URL to anyone allows them to access the note, too.": "Visit this link to see the note. Giving the URL to anyone allows them to access the note, too.", "URL shortener may expose your decrypt key in URL.": "URL shortener may expose your decrypt key in URL.", + "URL shortener is enabled by default.": "URL shortener is enabled by default.", "Save document": "Save document", "Your IP is not authorized to create documents.": "Your IP is not authorized to create documents.", "Trying to shorten a URL that isn't pointing at our instance.": "Trying to shorten a URL that isn't pointing at our instance.", diff --git a/js/privatebin.js b/js/privatebin.js index 908d863e..2ee9ad7e 100644 --- a/js/privatebin.js +++ b/js/privatebin.js @@ -2120,6 +2120,21 @@ jQuery.PrivateBin = (function($) { Helper.selectText($pasteUrl[0]); }; + /** + * Checks if auto-shortening is enabled and sends the link to the shortener if it is. + * + * @name PasteStatus.checkAutoShorten + * @function + */ + me.checkAutoShorten = function() { + // check if auto-shortening is enabled + if ($shortenButton.data('autoshorten') === true) { + // if so, we send the link to the shortener + // we do not remove the button, in case shortener fails + sendToShortener(); + } + } + /** * extracts URLs from given string * @@ -5030,6 +5045,8 @@ jQuery.PrivateBin = (function($) { TopNav.hideDownloadButton(); Editor.hide(); + PasteStatus.checkAutoShorten(); + // parse and show text // (preparation already done in me.sendPaste()) PasteViewer.run(); diff --git a/lib/Configuration.php b/lib/Configuration.php index f5715801..5b9f5052 100644 --- a/lib/Configuration.php +++ b/lib/Configuration.php @@ -61,6 +61,7 @@ class Configuration 'languageselection' => false, 'languagedefault' => '', 'urlshortener' => '', + 'shortenbydefault' => false, 'qrcode' => true, 'email' => true, 'icon' => 'jdenticon', diff --git a/lib/Controller.php b/lib/Controller.php index 2a70156e..5b2ddb94 100644 --- a/lib/Controller.php +++ b/lib/Controller.php @@ -477,6 +477,7 @@ class Controller $page->assign('EXPIRE', $expire); $page->assign('EXPIREDEFAULT', $this->_conf->getKey('default', 'expire')); $page->assign('URLSHORTENER', $this->_conf->getKey('urlshortener')); + $page->assign('SHORTENBYDEFAULT', $this->_conf->getKey('shortenbydefault')); $page->assign('QRCODE', $this->_conf->getKey('qrcode')); $page->assign('EMAIL', $this->_conf->getKey('email')); $page->assign('HTTPWARNING', $this->_conf->getKey('httpwarning')); diff --git a/tpl/bootstrap.php b/tpl/bootstrap.php index f1ea895d..099b2558 100644 --- a/tpl/bootstrap.php +++ b/tpl/bootstrap.php @@ -565,12 +565,20 @@ endif; if (!empty($URLSHORTENER)) : ?>

-

-