From 5c6fdef0b6bdb950df1e75542dad3f6c838c2ea0 Mon Sep 17 00:00:00 2001
From: eleksir
Date: Sun, 26 Mar 2017 01:43:27 +0300
Subject: [PATCH] * Changed EOL to UNIX type * Refactor code to respect OOP
coding style, now we have interface and class here!
---
index.php | 965 +++++++++++++++++++++++++++---------------------------
1 file changed, 487 insertions(+), 478 deletions(-)
diff --git a/index.php b/index.php
index 4e94d0e..e3e05f9 100644
--- a/index.php
+++ b/index.php
@@ -1,478 +1,487 @@
-
-
-
-
-
- Slack paste.
-
-
- Slack Paste
-
-
\n", $text);
- }
-?>
-
- Console client can be found here: e thanks sprunge.us for their client :)
- More minimalist console client can be found here: d
-
- 2015
-
-
-
-
-
-
-
-
-
- Link Shortener
-
-
-
-
-
-%s | \n", $text);
- }
-?>
-
-
-
-#!/bin/bash
-
-usage() {
-description | fmt -s >&2
-}
-
-description() {
-cat << HERE
-
-DESCRIPTION
- Upload data and fetch URL from the pastebin http://p.dafuq.ru/
-
-USAGE
- $0 filename.txt
- $0 text string
- $0 < filename.txt
- piped_data | $0
-
-NOTES
---------------------------------------------------------------------------
-* INPUT METHODS *
-$0 can accept piped data, STDIN redirection [&2
- if [ "$*" ]; then
- echo Arguments present... >&2
- if [ -f "$*" ]; then
- echo Uploading the contents of "$*"... >&2
- cat "$*"
- else
- echo Uploading the text: \""$*"\"... >&2
- echo "$*"
- fi | curl -F "exs=<-"
- else
- echo No arguments found, printing USAGE and exiting. >&2
- usage
- fi
-else
- echo Using input from a pipe or STDIN redirection... >&2
- while read -r line ; do
- echo $line
- done | curl -F "exs=<-"
-fi
-
-#!/usr/bin/env bash
-
-if [[ "$1" == "--help" ]]; then
- echo "Usage: $0 < file"
- echo " echo foo | $0"
-elif [[ -f "$1" ]]; then
- curl -F "exs=<-" "" <"$1"
-else
- curl -F "exs=<-" ""
-fi
-
-exit 0
-';
-exec(
- "CREATE TABLE IF NOT EXISTS paste (
- id TEXT PRIMARY KEY,
- paste TEXT,
- time INTEGER,
- etime INTEGER
- )"
- );
-
- if($r === false) {
- $e = $r->errorInfo();
- showindex($e[2]);
- }
-
- $dbh = null;
-}
-
-function query_db($q) {
- if (! file_exists(DBPATH)) {
- init_db();
- return false;
- }
-
- $dbh = new PDO("sqlite:" . DBPATH);
- $sth = $dbh->query("SELECT paste FROM paste WHERE id='$q'");
-
- if ($sth === false) {
- $e = $r->errorInfo();
- showindex($e[2]);
- exit;
- }
-
- $sth->setFetchMode(PDO::FETCH_ASSOC);
- $data = $sth->fetch();
- $dbh = null;
-
- if ($data === false) {
- return($data);
- } else {
- if (isset($data['etime']) and ($data['etime'] < time())){
- return false;
- }
-
- $data = $data['paste'];
- return($data); # false if no data or no key in db
- }
-}
-
-function store_data($paste) {
- if (! file_exists(DBPATH)) { init_db(); }
- $dbh = new PDO("sqlite:" . DBPATH);
- $q = 0;
-
- do {
- if ($q > 100) { # assume key-value space depleted if we try more than 100 times
- $q = false;
- break;
- }
- $id = gen_id();
- $q++;
- } while (! $dbh->query("SELECT id FROM paste WHERE id='$id'"));
-
- if($q === false) {
- return('Storage depleted.');
- }
-
- $ctime = time();
- $etime = $ctime + 14515200;
-
- if(isset($_POST['etime'])) {
- switch ($_POST['etime']) {
- case '1': # 1 hour
- $etime = $ctime + 3600;
- break;
-
- case '2': # 12 hours
- $etime = $ctime + 43200;
- break;
-
- case '3': # 1 day
- $etime = $ctime + 86400;
- break;
-
- case '4': # 1 week
- $etime = $ctime + 604800;
- break;
-
- case '5': # 1 month
- $etime = $ctime + 2419200;
- break;
-
- case '6': # 3 months
- $etime = $ctime + 7257600;
- break;
-
- case '7': # 6 months
- $etime = $ctime + 14515200;
- break;
-
- case '8': # 1 year
- $etime = $ctime + 29030400;
- break;
-
- case '9': # 2 years
- $etime = $ctime + 58060800;
- break;
-
- case '10': # 3 years
- $etime = $ctime + 87091200;
- break;
-
- case '13': # forever
- $etime = "null";
- break;
- }
- }
-
- $sth = $dbh->prepare("INSERT INTO paste VALUES('$id', '$paste', $ctime, $etime)");
- $sth->execute();
-
- if ($sth === false) {
- $e = $sth->errorInfo();
- showindex($e[2]);
- exit;
- }
-
- $dbh = null;
- 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 (APP_TYPE == 'paste') {
- if ($str == 'e') {
- eclient();
- exit;
- } elseif ($str == 'd') {
- dclient();
- exit;
- }
-
- # 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))) {
- if (preg_match('/^[a-z|A-Z]+$/', $str) == 1) {
- $response = query_db($str);
-
- if ($response === false) {
- $text = 'No such paste.';
- } else {
- $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'] != '')) {
- 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');
- print mb_convert_encoding(APP_ADDRESS . store_data(base64_encode($_POST['api_paste_code'])), 'UTF-8', 'auto');
- exit;
- }
- } elseif ($_POST['api_option'] == 'show_paste') {
- if (isset($_POST['api_paste_key'])) {
- $str = $_POST['api_paste_key'];
-
- if ((mb_strlen($str, 'UTF-8') >= KEY_MIN) and ((mb_strlen($str, 'UTF-8') <= KEY_MAX))) {
- if (preg_match('/^[a-z|A-Z]+$/', $str) == 1) {
- $response = query_db($str);
-
- if ($response === false) {
- header('Content-Type: text/plain; charset=utf-8');
- print "Bad API request, invalid permission to view this paste or invalid api_paste_key";
- exit;
- } else {
- header('Content-Type: text/plain');
- print base64_decode($response);
- exit;
- }
- }
- }
- }
- }
- }
-}
-
-showindex($text);
-
-/*
- * END OF MAIN
- */
-?>
+exec(
+ "CREATE TABLE IF NOT EXISTS paste (
+ id TEXT PRIMARY KEY,
+ paste TEXT,
+ time INTEGER,
+ etime INTEGER
+ )"
+ );
+
+ if ($r === false) {
+ $e = $r->errorInfo();
+ self::showindex($e[2]);
+ }
+
+ $dbh = null;
+ }
+
+ public function showindex ($text) {
+ header('Content-Language: en, ru');
+ header('Pragma: no-cache');
+ header('Cache-Control: no-cache');
+
+ if (APP_TYPE == 'paste') {
+?>
+
+
+
+
+ Slack paste.
+
+
+ Slack Paste
+
+\n", $text);
+ }
+?>
+
+ Console client can be found here: e thanks sprunge.us for their client :)
+ More minimalist console client can be found here: d
+
+ 2015
+
+
+
+
+
+
+
+
+
+ Link Shortener
+
+
+
+
+
+%s | \n", $text);
+ }
+?>
+
+
+
+#!/bin/bash
+
+usage() {
+description | fmt -s >&2
+}
+
+description() {
+cat << HERE
+
+DESCRIPTION
+ Upload data and fetch URL from the pastebin http://p.dafuq.ru/
+
+USAGE
+ $0 filename.txt
+ $0 text string
+ $0 < filename.txt
+ piped_data | $0
+
+NOTES
+--------------------------------------------------------------------------
+* INPUT METHODS *
+$0 can accept piped data, STDIN redirection [&2
+ if [ "$*" ]; then
+ echo Arguments present... >&2
+ if [ -f "$*" ]; then
+ echo Uploading the contents of "$*"... >&2
+ cat "$*"
+ else
+ echo Uploading the text: \""$*"\"... >&2
+ echo "$*"
+ fi | curl -F "exs=<-"
+ else
+ echo No arguments found, printing USAGE and exiting. >&2
+ usage
+ fi
+else
+ echo Using input from a pipe or STDIN redirection... >&2
+ while read -r line ; do
+ echo $line
+ done | curl -F "exs=<-"
+fi
+
+#!/usr/bin/env bash
+
+if [[ "$1" == "--help" ]]; then
+ echo "Usage: $0 < file"
+ echo " echo foo | $0"
+elif [[ -f "$1" ]]; then
+ curl -F "exs=<-" "" <"$1"
+else
+ curl -F "exs=<-" ""
+fi
+
+exit 0
+';
+query("SELECT paste FROM paste WHERE id='$q'");
+
+ if ($sth === false) {
+ $e = $r->errorInfo();
+ self::showindex($e[2]);
+ exit;
+ }
+
+ $sth->setFetchMode(PDO::FETCH_ASSOC);
+ $data = $sth->fetch();
+ $dbh = null;
+
+ if ($data === false) {
+ return($data);
+ } else {
+ if (isset($data['etime']) and ($data['etime'] < time())){
+ return false;
+ }
+
+ $data = $data['paste'];
+ return($data); # false if no data or no key in db
+ }
+ }
+
+ public function store_data ($paste) {
+ $dbh = new PDO("sqlite:" . DBPATH);
+ $q = 0;
+
+ do {
+ if ($q > 100) { # assume key-value space depleted if we try more than 100 times
+ $q = false;
+ break;
+ }
+
+ $id = self::gen_id();
+ $q++;
+ } while (! $dbh->query("SELECT id FROM paste WHERE id='$id'"));
+
+ if ($q === false) {
+ return('Storage depleted.');
+ }
+
+ $ctime = time();
+ $etime = $ctime + 14515200;
+
+ if (isset($_POST['etime'])) {
+ switch ($_POST['etime']) {
+ case '1': # 1 hour
+ $etime = $ctime + 3600;
+ break;
+
+ case '2': # 12 hours
+ $etime = $ctime + 43200;
+ break;
+
+ case '3': # 1 day
+ $etime = $ctime + 86400;
+ break;
+
+ case '4': # 1 week
+ $etime = $ctime + 604800;
+ break;
+
+ case '5': # 1 month
+ $etime = $ctime + 2419200;
+ break;
+
+ case '6': # 3 months
+ $etime = $ctime + 7257600;
+ break;
+
+ case '7': # 6 months
+ $etime = $ctime + 14515200;
+ break;
+
+ case '8': # 1 year
+ $etime = $ctime + 29030400;
+ break;
+
+ case '9': # 2 years
+ $etime = $ctime + 58060800;
+ break;
+
+ case '10': # 3 years
+ $etime = $ctime + 87091200;
+ break;
+
+ case '13': # forever
+ $etime = "null";
+ break;
+ }
+ }
+
+ $sth = $dbh->prepare("INSERT INTO paste VALUES('$id', '$paste', $ctime, $etime)");
+ $sth->execute();
+
+ if ($sth === false) {
+ $e = $sth->errorInfo();
+ self::showindex($e[2]);
+ exit;
+ }
+
+ $dbh = null;
+ return($id);
+ }
+}
+
+
+/*
+ * MAIN STARTS HERE
+ */
+
+$text = '';
+$p = new paste;
+
+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 (APP_TYPE == 'paste') {
+ if ($str == 'e') {
+ $p->eclient();
+ exit;
+ } elseif ($str == 'd') {
+ $p->dclient();
+ exit;
+ }
+
+ # 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))) {
+ if (preg_match('/^[a-z|A-Z]+$/', $str) == 1) {
+ $response = $p->query_db($str);
+
+ if ($response === false) {
+ $text = 'No such paste.';
+ } else {
+ $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'] != '')) {
+ 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 . $p->store_data(base64_encode($_POST['exs']));
+ } else {
+ $text = 'Unable to short your link.';
+ }
+ } elseif (APP_TYPE == 'paste') {
+ $text = APP_ADDRESS . $p->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');
+ print mb_convert_encoding(APP_ADDRESS . $p->store_data(base64_encode($_POST['api_paste_code'])), 'UTF-8', 'auto');
+ exit;
+ }
+ } elseif ($_POST['api_option'] == 'show_paste') {
+ if (isset($_POST['api_paste_key'])) {
+ $str = $_POST['api_paste_key'];
+
+ if ((mb_strlen($str, 'UTF-8') >= KEY_MIN) and ((mb_strlen($str, 'UTF-8') <= KEY_MAX))) {
+ if (preg_match('/^[a-z|A-Z]+$/', $str) == 1) {
+ $response = $p->query_db($str);
+
+ if ($response === false) {
+ header('Content-Type: text/plain; charset=utf-8');
+ print "Bad API request, invalid permission to view this paste or invalid api_paste_key";
+ exit;
+ } else {
+ header('Content-Type: text/plain');
+ print base64_decode($response);
+ exit;
+ }
+ }
+ }
+ }
+ }
+ }
+}
+
+$p->showindex($text);
+
+/*
+ * END OF MAIN
+ */
+?>