From fbc8445b73bd782be465417907b02e074a2b8c7b Mon Sep 17 00:00:00 2001 From: eleksir Date: Sat, 18 Mar 2017 16:14:52 +0300 Subject: [PATCH] Added link shortener feature. Now app can either handle pastes or shorten links, but not both. --- README.md | 57 +++++++++++++++++++--- app.ini | 7 ++- index.php | 142 ++++++++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 176 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 356a739..c8aa3a0 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,11 @@ ## Paste. ## -This is pastebin replacement. It does not use ads, it is opensource it is relatively small. +This is pastebin replacement. It does not use ads, it is opensource, it is relatively small. And it has minimal pastebin API stub, at least pastebinit can paste to it with minimal config changes. Besides pastebinit there are 2 cli clients available on main page of this app. +And as a bonus this app can short links too! But it either shorts links **or** stores paste, not both at same time. + ### How do I get set up? ### This paste utilizes PDO SQLite driver, well, at least for now :) and mbstrings. @@ -11,7 +13,11 @@ Tested php versions are 5.6.30 and 7.0.12. Paste uses **paste/paste.sqlite** fil Tested on apache/fcgid (windows) for dev and nginx/php-fpm for prod. -Default installation assumes that paste runs as / app on site, but you can change it (BASEURL) in index.php. +Default installation assumes that paste runs as / app on site, but you can change it (BASEURL) in app.ini. + +#### Settings for paste mode #### + +edit app.ini and set correct address, base url, db path, application type (type = paste) Nginx config snippet: ``` @@ -43,7 +49,39 @@ location / { } ``` -Database initialises automatically :) +Database initializes automatically :) + +#### Settings for link shortener mode #### + +edit app.ini and set correct address, base url, db path, application type (type = short) + +Nginx config snippet: +``` +# limit links size to ~ 50k +client_max_body_size 64k; + +rewrite "^/([a-z|A-Z]*)$" /index.php?$1; + +location / { + root /var/www/htdocs/empty; + + index index.php; + location ~ \.php$ { + if ($request_method !~ ^(GET|HEAD|POST)$ ) { + return 499; + } + + root /var/www/htdocs; + fastcgi_split_path_info ^(.+\.php)(/.+)$; + #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini + fastcgi_index index.php; + include fastcgi.conf; + fastcgi_pass php; + } +} +``` + +Database initializes automatically :) ### Contribution guidelines ### @@ -52,7 +90,7 @@ Clean code, preferably with comments for some quirky places. ### PasteBinIt configuration ### default pastebin site, __~/.pastebinit.xml__: -``` +```xml http://your.site Stephane Graber @@ -61,7 +99,7 @@ default pastebin site, __~/.pastebinit.xml__: ``` and how to work with it __~/.pastebinit.d/your.site.conf__: -``` +```ini [pastebin] basename = your.site regexp = http://((([a-zA-Z0-9\-_\.]*)(your\.site))) @@ -115,4 +153,11 @@ diff -ruN pastebin.com.conf your.site.conf -page = /api/api_post.php +page = / regexp = (.*) -``` \ No newline at end of file +``` + +### TODO ### + +* Make one app that shorten links, stores paste, stores pictures (yes! image hosting). +* Configurable features (not everyone wants all features enabled). +* Before implementing picture storage, there should be made authorization. +* Sane and clear API. diff --git a/app.ini b/app.ini index 1593f62..eeec7ee 100644 --- a/app.ini +++ b/app.ini @@ -1,15 +1,14 @@ [app] -; either "paste" or "shortener" -; reserved for future releases, not used +; either "paste" or "short" type = paste ; relative path to application, as it seen in browser ; must end with '/' baseurl = / ; if you decide to put images|css on separate domain, write it here ; or leave it same as siteaddress -; for now it is only /bg_logo.png +; for now it is only /bg_logo.png for paste and /logo.png for link shortener cdnaddress = http://127.0.0.1/ ; hostname where app lives, with http(s) prefix siteaddress = http://127.0.0.1/ ; path, where db created|stored, if not exist, it will be created -dbpath = paste/paste.sqlite +dbpath = data/db.sqlite diff --git a/index.php b/index.php index 0f86a15..4e94d0e 100644 --- a/index.php +++ b/index.php @@ -1,5 +1,6 @@ @@ -33,8 +38,9 @@ function showindex($text) {

Slack Paste


- for - + for +
+ + for + +
+%s\n", $text); + } +?> + + += KEY_MIN) and ((mb_strlen($str, 'UTF-8') <= KEY_MAX))) { @@ -329,17 +390,56 @@ if (($_SERVER['REQUEST_METHOD'] === 'GET') or ($_SERVER['REQUEST_METHOD'] === 'H if ($response === false) { $text = 'No such paste.'; } else { - header('Content-Type: text/plain'); - print base64_decode($response); - exit; + $response = base64_decode($response); + + if (APP_TYPE == 'paste') { + header('Content-Type: text/plain'); + print $response; + exit; + } elseif (APP_TYPE == 'short') { + header( "Location: " . $response, true, 301 ); + } } } } } elseif ($_SERVER['REQUEST_METHOD'] === 'POST') { if (isset($_POST['exs']) and ($_POST['exs'] != '')) { - $text = APP_ADDRESS . store_data(base64_encode($_POST['exs'])); - } elseif (isset($_POST['api_option'])){ # some pastebin API compatibility stuff + if (APP_TYPE == 'short') { + # I don't like loops :) + if ((preg_match('/^https?\:/', $_POST['exs']) == 1) or + (preg_match('/^s?ftps?\:/', $_POST['exs']) == 1) or + # Looks like shorten links other than http(s) or ftp is not good idea at all, but we CAN do this too. + (preg_match('/^ldaps?\:/', $_POST['exs']) == 1) or + (preg_match('/^ircs?\:/', $_POST['exs']) == 1) or + (preg_match('/^xmpp\:/', $_POST['exs']) == 1) or + (preg_match('/^mailto\:/', $_POST['exs']) == 1) or + (preg_match('/^rtmp\:/', $_POST['exs']) == 1) or + (preg_match('/^rtsp\:/', $_POST['exs']) == 1) or + (preg_match('/^rtp\:/', $_POST['exs']) == 1) or + (preg_match('/^soutcast\:/', $_POST['exs']) == 1) or + (preg_match('/^icecast\:/', $_POST['exs']) == 1) or + (preg_match('/^magnet\:/', $_POST['exs']) == 1) or + (preg_match('/^ed2k\:/', $_POST['exs']) == 1) or + (preg_match('/^ssh\:/', $_POST['exs']) == 1) or + (preg_match('/^svn\:/', $_POST['exs']) == 1) or + (preg_match('/^hg\:/', $_POST['exs']) == 1) or + (preg_match('/^git\:/', $_POST['exs']) == 1) or + (preg_match('/^smb\:/', $_POST['exs']) == 1) or + (preg_match('/^ipp\:/', $_POST['exs']) == 1) or + (preg_match('/^chrome\:/', $_POST['exs']) == 1) or # i'm doubt if this really working, but anyway + (preg_match('/^vivaldi\:/', $_POST['exs']) == 1) or # i'm doubt if this really working, but anyway + (preg_match('/^opera\:/', $_POST['exs']) == 1) or # i'm doubt if this really working, but anyway + (preg_match('/^about\:/', $_POST['exs']) == 1) or # i'm doubt if this really working, but anyway + (preg_match('/^skype\:/', $_POST['exs']) == 1)) { + $text = APP_ADDRESS . store_data(base64_encode($_POST['exs'])); + } else { + $text = 'Unable to short your link.'; + } + } elseif (APP_TYPE == 'paste') { + $text = APP_ADDRESS . store_data(base64_encode($_POST['exs'])); + } + } elseif ((APP_TYPE == 'paste') and (isset($_POST['api_option']))) { # some pastebin API compatibility stuff if ($_POST['api_option'] == 'paste') { if ((isset($_POST['api_paste_code'])) and ($_POST['api_paste_code'] != '')) { header('Content-Type: text/plain; charset=utf-8'); @@ -368,9 +468,11 @@ if (($_SERVER['REQUEST_METHOD'] === 'GET') or ($_SERVER['REQUEST_METHOD'] === 'H } } } -} else { - # ??? } showindex($text); -?> \ No newline at end of file + +/* + * END OF MAIN + */ +?>