2 Commits

Author SHA1 Message Date
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 488 additions and 478 deletions

View File

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

273
index.php
View File

@@ -1,23 +1,59 @@
<?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);
define ('CDN_ADDRESS', $config["cdnaddress"]);
define ('DBPATH', $config["dbpath"]);
define ('KEY_MIN', 3);
define ('KEY_MAX', 9);
define ('APP_TYPE', $config["type"]);
interface iPaste {
public function showindex($text);
public function eclient();
public function dclient();
public function query_db($q);
public function store_data($paste);
}
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) {
header('Content-Language: en, ru');
header('Pragma: no-cache');
header('Cache-Control: no-cache');
$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 (APP_TYPE == 'paste') {
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('Pragma: no-cache');
header('Cache-Control: no-cache');
if (APP_TYPE == 'paste') {
?>
<!DOCTYPE html>
<html lang="ru">
@@ -54,9 +90,9 @@ function showindex($text) {
</select>
</form>
<?php
if ($text != '') {
printf (" <p><table align=\"center\" frame=\"void\" border=\"0\"><td>%s</td></table></p>\n", $text);
}
if ($text != '') {
printf (" <p><table align=\"center\" frame=\"void\" border=\"0\"><td>%s</td></table></p>\n", $text);
}
?>
<p style="font-size: small;">
Console client can be found here: <a href="<?php print APP_ADDRESS; ?>e"><?php print APP_ADDRESS; ?>e</a>&nbsp; &nbsp; thanks <a href="http://sprunge.us">sprunge.us</a> for their client :)<br/>
@@ -67,7 +103,7 @@ function showindex($text) {
</body>
</html>
<?php
} elseif (APP_TYPE == 'short') {
} elseif (APP_TYPE == 'short') {
?>
<!DOCTYPE html>
<html lang="ru">
@@ -107,20 +143,20 @@ function showindex($text) {
</select>
</form>
<?php
if ($text != '') {
printf (" <table style=\"margin: 0px auto;\"><td>%s</td></table>\n", $text);
}
if ($text != '') {
printf (" <table style=\"margin: 0px auto;\"><td>%s</td></table>\n", $text);
}
?>
</body>
</html>
<?php
}
}
}
function eclient() {
header('Content-Language: en, ru');
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename="e"');
public function eclient () {
header('Content-Language: en, ru');
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename="e"');
?>
#!/bin/bash
@@ -183,13 +219,13 @@ else
done | curl -F "exs=<-" <?php print SITE_ADDRESS; ?>
fi
<?php
exit;
}
exit;
}
function dclient() {
header('Content-Language: en, ru');
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename="d";');
public function dclient () {
header('Content-Language: en, ru');
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename="d";');
?>
#!/usr/bin/env bash
@@ -205,99 +241,70 @@ fi
exit 0
';
<?php
exit;
}
function gen_id() {
$urllen = rand (KEY_MIN, KEY_MAX);
$string = '';
$a = Array(); $a = array_merge(range('A', 'Z'), range('a', 'z'));
for($i = 0; $i < $urllen; $i++){
$string .= $a[rand (0, count($a) - 1)];
}
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;
}
$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;
protected function gen_id () {
$urllen = rand (KEY_MIN, KEY_MAX);
$string = '';
$a = Array(); $a = array_merge(range('A', 'Z'), range('a', 'z'));
if ($data === false) {
return($data);
} else {
if (isset($data['etime']) and ($data['etime'] < time())){
return false;
for ($i = 0; $i < $urllen; $i++) {
$string .= $a[rand (0, count($a) - 1)];
}
$data = $data['paste'];
return($data); # false if no data or no key in db
return($string);
}
}
function store_data($paste) {
if (! file_exists(DBPATH)) { init_db(); }
$dbh = new PDO("sqlite:" . DBPATH);
$q = 0;
public function query_db ($q) {
$dbh = new PDO("sqlite:" . DBPATH);
$sth = $dbh->query("SELECT paste FROM paste WHERE id='$q'");
do {
if ($q > 100) { # assume key-value space depleted if we try more than 100 times
$q = false;
break;
if ($sth === false) {
$e = $r->errorInfo();
self::showindex($e[2]);
exit;
}
$id = gen_id();
$q++;
} while (! $dbh->query("SELECT id FROM paste WHERE id='$id'"));
if($q === false) {
return('Storage depleted.');
$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
}
}
$ctime = time();
$etime = $ctime + 14515200;
public function store_data ($paste) {
$dbh = new PDO("sqlite:" . DBPATH);
$q = 0;
if(isset($_POST['etime'])) {
switch ($_POST['etime']) {
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;
@@ -341,20 +348,21 @@ function store_data($paste) {
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);
}
$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);
}
@@ -363,6 +371,7 @@ function store_data($paste) {
*/
$text = '';
$p = new paste;
if (($_SERVER['REQUEST_METHOD'] === 'GET') or ($_SERVER['REQUEST_METHOD'] === 'HEAD')) {
# 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 ($str == 'e') {
eclient();
$p->eclient();
exit;
} elseif ($str == 'd') {
dclient();
$p->dclient();
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 (preg_match('/^[a-z|A-Z]+$/', $str) == 1) {
$response = query_db($str);
$response = $p->query_db($str);
if ($response === false) {
$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('/^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']));
$text = APP_ADDRESS . $p->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']));
$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 . 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;
}
} 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 (preg_match('/^[a-z|A-Z]+$/', $str) == 1) {
$response = query_db($str);
$response = $p->query_db($str);
if ($response === false) {
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