3 Commits

Author SHA1 Message Date
eleksir
ee4ac8a429 Added tag v2.1.0 for changeset 6db9ff3d18c2 2017-03-26 01:44:16 +03:00
eleksir
5c6fdef0b6 * Changed EOL to UNIX type
* Refactor code to respect OOP coding style, now we have interface and class here!
2017-03-26 01:43:27 +03:00
eleksir
16c3e42635 Added tag v2.0.0 for changeset 0a90758f5df3 2017-03-18 16:15:31 +03:00
2 changed files with 489 additions and 478 deletions

View File

@@ -1,2 +1,4 @@
b66e758d981314bfd9de6dbfe0385785908a884c v1.0.0 b66e758d981314bfd9de6dbfe0385785908a884c v1.0.0
4db108cffb1f05d2c42ff71e22d6c2330b4f45ad v1.1.0 4db108cffb1f05d2c42ff71e22d6c2330b4f45ad v1.1.0
0a90758f5df3d039490b57fad1091af22fb47524 v2.0.0
6db9ff3d18c27c2f7c08ed4104d6cbc05ca8bf86 v2.1.0

137
index.php
View File

@@ -1,18 +1,54 @@
<?php <?php
$config = parse_ini_file("app.ini");
define ('SITE_ADDRESS', $config["siteaddress"]); interface iPaste {
define ('BASEURL', $config["baseurl"]); public function showindex($text);
define ('APP_ADDRESS', substr(SITE_ADDRESS, 0, -1) . BASEURL); public function eclient();
define ('CDN_ADDRESS', $config["cdnaddress"]); public function dclient();
define ('DBPATH', $config["dbpath"]); public function query_db($q);
define ('KEY_MIN', 3); public function store_data($paste);
define ('KEY_MAX', 9); }
define ('APP_TYPE', $config["type"]);
unset ($config); class paste implements iPaste {
public function __construct () {
if (! file_exists('app.ini')) {
print "app.ini not found, unable to run application\n";
}
function showindex($text) { $config = parse_ini_file("app.ini");
define ('SITE_ADDRESS', $config["siteaddress"]);
define ('BASEURL', $config["baseurl"]);
define ('APP_ADDRESS', substr(SITE_ADDRESS, 0, -1) . BASEURL);
define ('CDN_ADDRESS', $config["cdnaddress"]);
define ('DBPATH', $config["dbpath"]);
define ('KEY_MIN', 3);
define ('KEY_MAX', 9);
define ('APP_TYPE', $config["type"]);
unset ($config);
if (! file_exists(dirname(DBPATH))) {
mkdir(dirname(DBPATH), 0777, true);
}
$dbh = new PDO("sqlite:" . DBPATH);
$r = $dbh->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('Content-Language: en, ru');
header('Pragma: no-cache'); header('Pragma: no-cache');
header('Cache-Control: no-cache'); header('Cache-Control: no-cache');
@@ -115,9 +151,9 @@ function showindex($text) {
</html> </html>
<?php <?php
} }
} }
function eclient() { public function eclient () {
header('Content-Language: en, ru'); header('Content-Language: en, ru');
header('Content-Type: text/plain'); header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename="e"'); header('Content-Disposition: attachment; filename="e"');
@@ -184,9 +220,9 @@ else
fi fi
<?php <?php
exit; exit;
} }
function dclient() { public function dclient () {
header('Content-Language: en, ru'); header('Content-Language: en, ru');
header('Content-Type: text/plain'); header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename="d";'); header('Content-Disposition: attachment; filename="d";');
@@ -206,56 +242,27 @@ exit 0
'; ';
<?php <?php
exit; exit;
} }
function gen_id() { protected function gen_id () {
$urllen = rand (KEY_MIN, KEY_MAX); $urllen = rand (KEY_MIN, KEY_MAX);
$string = ''; $string = '';
$a = Array(); $a = array_merge(range('A', 'Z'), range('a', 'z')); $a = Array(); $a = array_merge(range('A', 'Z'), range('a', 'z'));
for($i = 0; $i < $urllen; $i++){ for ($i = 0; $i < $urllen; $i++) {
$string .= $a[rand (0, count($a) - 1)]; $string .= $a[rand (0, count($a) - 1)];
} }
return($string); return($string);
}
function init_db() {
# we init db, eh? drop db file and re-create it!
if (file_exists(DBPATH)) { unlink(DBPATH); }
if (! file_exists(dirname(DBPATH))) { mkdir(dirname(DBPATH), 0777, true); }
$dbh = new PDO("sqlite:" . DBPATH);
$r = $dbh->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;
} }
public function query_db ($q) {
$dbh = new PDO("sqlite:" . DBPATH); $dbh = new PDO("sqlite:" . DBPATH);
$sth = $dbh->query("SELECT paste FROM paste WHERE id='$q'"); $sth = $dbh->query("SELECT paste FROM paste WHERE id='$q'");
if ($sth === false) { if ($sth === false) {
$e = $r->errorInfo(); $e = $r->errorInfo();
showindex($e[2]); self::showindex($e[2]);
exit; exit;
} }
@@ -273,10 +280,9 @@ function query_db($q) {
$data = $data['paste']; $data = $data['paste'];
return($data); # false if no data or no key in db return($data); # false if no data or no key in db
} }
} }
function store_data($paste) { public function store_data ($paste) {
if (! file_exists(DBPATH)) { init_db(); }
$dbh = new PDO("sqlite:" . DBPATH); $dbh = new PDO("sqlite:" . DBPATH);
$q = 0; $q = 0;
@@ -285,18 +291,19 @@ function store_data($paste) {
$q = false; $q = false;
break; break;
} }
$id = gen_id();
$id = self::gen_id();
$q++; $q++;
} while (! $dbh->query("SELECT id FROM paste WHERE id='$id'")); } while (! $dbh->query("SELECT id FROM paste WHERE id='$id'"));
if($q === false) { if ($q === false) {
return('Storage depleted.'); return('Storage depleted.');
} }
$ctime = time(); $ctime = time();
$etime = $ctime + 14515200; $etime = $ctime + 14515200;
if(isset($_POST['etime'])) { if (isset($_POST['etime'])) {
switch ($_POST['etime']) { switch ($_POST['etime']) {
case '1': # 1 hour case '1': # 1 hour
$etime = $ctime + 3600; $etime = $ctime + 3600;
@@ -349,12 +356,13 @@ function store_data($paste) {
if ($sth === false) { if ($sth === false) {
$e = $sth->errorInfo(); $e = $sth->errorInfo();
showindex($e[2]); self::showindex($e[2]);
exit; exit;
} }
$dbh = null; $dbh = null;
return($id); return($id);
}
} }
@@ -363,6 +371,7 @@ function store_data($paste) {
*/ */
$text = ''; $text = '';
$p = new paste;
if (($_SERVER['REQUEST_METHOD'] === 'GET') or ($_SERVER['REQUEST_METHOD'] === 'HEAD')) { if (($_SERVER['REQUEST_METHOD'] === 'GET') or ($_SERVER['REQUEST_METHOD'] === 'HEAD')) {
# substr returns empty string (boolean false) on error # substr returns empty string (boolean false) on error
@@ -370,10 +379,10 @@ if (($_SERVER['REQUEST_METHOD'] === 'GET') or ($_SERVER['REQUEST_METHOD'] === 'H
if (APP_TYPE == 'paste') { if (APP_TYPE == 'paste') {
if ($str == 'e') { if ($str == 'e') {
eclient(); $p->eclient();
exit; exit;
} elseif ($str == 'd') { } elseif ($str == 'd') {
dclient(); $p->dclient();
exit; exit;
} }
@@ -385,7 +394,7 @@ if (($_SERVER['REQUEST_METHOD'] === 'GET') or ($_SERVER['REQUEST_METHOD'] === 'H
if ((mb_strlen($str, 'UTF-8') >= KEY_MIN) and ((mb_strlen($str, 'UTF-8') <= KEY_MAX))) { 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) { if (preg_match('/^[a-z|A-Z]+$/', $str) == 1) {
$response = query_db($str); $response = $p->query_db($str);
if ($response === false) { if ($response === false) {
$text = 'No such paste.'; $text = 'No such paste.';
@@ -432,18 +441,18 @@ if (($_SERVER['REQUEST_METHOD'] === 'GET') or ($_SERVER['REQUEST_METHOD'] === 'H
(preg_match('/^opera\:/', $_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('/^about\:/', $_POST['exs']) == 1) or # i'm doubt if this really working, but anyway
(preg_match('/^skype\:/', $_POST['exs']) == 1)) { (preg_match('/^skype\:/', $_POST['exs']) == 1)) {
$text = APP_ADDRESS . store_data(base64_encode($_POST['exs'])); $text = APP_ADDRESS . $p->store_data(base64_encode($_POST['exs']));
} else { } else {
$text = 'Unable to short your link.'; $text = 'Unable to short your link.';
} }
} elseif (APP_TYPE == 'paste') { } elseif (APP_TYPE == 'paste') {
$text = APP_ADDRESS . store_data(base64_encode($_POST['exs'])); $text = APP_ADDRESS . $p->store_data(base64_encode($_POST['exs']));
} }
} elseif ((APP_TYPE == 'paste') and (isset($_POST['api_option']))) { # some pastebin API compatibility stuff } elseif ((APP_TYPE == 'paste') and (isset($_POST['api_option']))) { # some pastebin API compatibility stuff
if ($_POST['api_option'] == 'paste') { if ($_POST['api_option'] == 'paste') {
if ((isset($_POST['api_paste_code'])) and ($_POST['api_paste_code'] != '')) { if ((isset($_POST['api_paste_code'])) and ($_POST['api_paste_code'] != '')) {
header('Content-Type: text/plain; charset=utf-8'); 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'); print mb_convert_encoding(APP_ADDRESS . $p->store_data(base64_encode($_POST['api_paste_code'])), 'UTF-8', 'auto');
exit; exit;
} }
} elseif ($_POST['api_option'] == 'show_paste') { } elseif ($_POST['api_option'] == 'show_paste') {
@@ -452,7 +461,7 @@ if (($_SERVER['REQUEST_METHOD'] === 'GET') or ($_SERVER['REQUEST_METHOD'] === 'H
if ((mb_strlen($str, 'UTF-8') >= KEY_MIN) and ((mb_strlen($str, 'UTF-8') <= KEY_MAX))) { 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) { if (preg_match('/^[a-z|A-Z]+$/', $str) == 1) {
$response = query_db($str); $response = $p->query_db($str);
if ($response === false) { if ($response === false) {
header('Content-Type: text/plain; charset=utf-8'); header('Content-Type: text/plain; charset=utf-8');
@@ -470,7 +479,7 @@ if (($_SERVER['REQUEST_METHOD'] === 'GET') or ($_SERVER['REQUEST_METHOD'] === 'H
} }
} }
showindex($text); $p->showindex($text);
/* /*
* END OF MAIN * END OF MAIN