Added link shortener feature.

Now app can either handle pastes or shorten links, but not both.
This commit is contained in:
eleksir
2017-03-18 16:14:52 +03:00
parent cd72ee3124
commit fbc8445b73
3 changed files with 176 additions and 30 deletions

142
index.php
View File

@@ -1,5 +1,6 @@
<?php
$config = parse_ini_file("app.ini");
define ('SITE_ADDRESS', $config["siteaddress"]);
define ('BASEURL', $config["baseurl"]);
define ('APP_ADDRESS', substr(SITE_ADDRESS, 0, -1) . BASEURL);
@@ -7,12 +8,16 @@ define ('CDN_ADDRESS', $config["cdnaddress"]);
define ('DBPATH', $config["dbpath"]);
define ('KEY_MIN', 3);
define ('KEY_MAX', 9);
define ('APP_TYPE', $config["type"]);
unset ($config);
function showindex($text) {
header('Content-Language: en, ru');
header('Pragma: no-cache');
header('Cache-Control: no-cache');
if (APP_TYPE == 'paste') {
?>
<!DOCTYPE html>
<html lang="ru">
@@ -33,8 +38,9 @@ function showindex($text) {
<h3>Slack Paste</h3>
<form method="post" action="<?php print APP_ADDRESS; ?>" enctype="multipart/form-data">
<textarea name="exs" id="textbox" autofocus></textarea><br />
<input type="submit" value="Post it!" style="font-size:115%;" /> for
<select name="etime" style="font-size: 85%; text-align:left;">
<input type="submit" value="Post it!" style="font-size:115%;" />
for
<select name="etime" style="font-size:115%; text-align:left;">
<option value="1" style="font-size: 85%; text-align:left;">1 Hour</option>
<option value="2" style="font-size: 85%; text-align:left;">12 Hours</option>
<option value="3" style="font-size: 85%; text-align:left;">1 day</option>
@@ -61,6 +67,54 @@ function showindex($text) {
</body>
</html>
<?php
} elseif (APP_TYPE == 'short') {
?>
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8" />
<style>
* {
margin: 10px;
text-align: center;
font-size: 14pt;
}
html, body {
background-color: #adc0fc; text-align: center;
}
</style>
<title>Link Shortener</title>
</head>
<body>
<img src="<?php print CDN_ADDRESS; ?>logo.png" />
<form method="post" action="<?php print APP_ADDRESS; ?>">
Shorten this link:<br />
<input type="text" maxlength="768" name="exs" style="font-size: 110%; width:45%;" autofocus/></br />
<input type="submit" value="Submit" style="font-size: 85%;" />
for
<select name="etime" style="font-size: 85%; text-align:left;">
<option value="1" style="font-size: 85%; text-align:left;">1 Hour</option>
<option value="2" style="font-size: 85%; text-align:left;">12 Hours</option>
<option value="3" style="font-size: 85%; text-align:left;">1 day</option>
<option value="4" style="font-size: 85%; text-align:left;">1 week</option>
<option value="5" style="font-size: 85%; text-align:left;">1 Month</option>
<option value="6" style="font-size: 85%; text-align:left;">3 Months</option>
<option value="7" selected style="font-size: 85%; text-align:left;">6 Months</option>
<option value="8" style="font-size: 85%; text-align:left;">1 Year</option>
<option value="9" style="font-size: 85%; text-align:left;">2 Years</option>
<option value="10" style="font-size: 85%; text-align:left;">3 Years</option>
</select>
</form>
<?php
if ($text != '') {
printf (" <table style=\"margin: 0px auto;\"><td>%s</td></table>\n", $text);
}
?>
</body>
</html>
<?php
}
}
function eclient() {
@@ -303,23 +357,30 @@ function store_data($paste) {
return($id);
}
/*
* MAIN STARTS HERE
*/
$text = '';
if (($_SERVER['REQUEST_METHOD'] === 'GET') or ($_SERVER['REQUEST_METHOD'] === 'HEAD')) {
# substr returns empty string (boolean false) on error
$str = $_SERVER['REQUEST_URI']; $str = substr($str, mb_strlen(BASEURL, 'UTF-8'));
if ($str == 'e') {
eclient();
exit;
} elseif ($str == 'd') {
dclient();
exit;
}
if (APP_TYPE == 'paste') {
if ($str == 'e') {
eclient();
exit;
} elseif ($str == 'd') {
dclient();
exit;
}
# pastebin returns raw pate when paste' key prepended with /raw/, let's handle it
if (preg_match('/^raw\//', $str) == 1) {
$str = substr($str, mb_strlen('raw/', 'UTF-8'));
# pastebin returns raw paste when paste' key prepended with /raw/, let's handle it
if (preg_match('/^raw\//', $str) == 1) {
$str = substr($str, mb_strlen('raw/', 'UTF-8'));
}
}
if ((mb_strlen($str, 'UTF-8') >= 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);
?>
/*
* END OF MAIN
*/
?>