* Changed EOL to UNIX type
* Refactor code to respect OOP coding style, now we have interface and class here!
This commit is contained in:
107
index.php
107
index.php
@@ -1,6 +1,20 @@
|
||||
<?php
|
||||
$config = parse_ini_file("app.ini");
|
||||
|
||||
interface iPaste {
|
||||
public function showindex($text);
|
||||
public function eclient();
|
||||
public function dclient();
|
||||
public function query_db($q);
|
||||
public function store_data($paste);
|
||||
}
|
||||
|
||||
class paste implements iPaste {
|
||||
public function __construct () {
|
||||
if (! file_exists('app.ini')) {
|
||||
print "app.ini not found, unable to run application\n";
|
||||
}
|
||||
|
||||
$config = parse_ini_file("app.ini");
|
||||
define ('SITE_ADDRESS', $config["siteaddress"]);
|
||||
define ('BASEURL', $config["baseurl"]);
|
||||
define ('APP_ADDRESS', substr(SITE_ADDRESS, 0, -1) . BASEURL);
|
||||
@@ -9,10 +23,32 @@ define ('DBPATH', $config["dbpath"]);
|
||||
define ('KEY_MIN', 3);
|
||||
define ('KEY_MAX', 9);
|
||||
define ('APP_TYPE', $config["type"]);
|
||||
|
||||
unset ($config);
|
||||
|
||||
function showindex($text) {
|
||||
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');
|
||||
@@ -117,7 +153,7 @@ function showindex($text) {
|
||||
}
|
||||
}
|
||||
|
||||
function eclient() {
|
||||
public function eclient () {
|
||||
header('Content-Language: en, ru');
|
||||
header('Content-Type: text/plain');
|
||||
header('Content-Disposition: attachment; filename="e"');
|
||||
@@ -186,7 +222,7 @@ fi
|
||||
exit;
|
||||
}
|
||||
|
||||
function dclient() {
|
||||
public function dclient () {
|
||||
header('Content-Language: en, ru');
|
||||
header('Content-Type: text/plain');
|
||||
header('Content-Disposition: attachment; filename="d";');
|
||||
@@ -208,7 +244,7 @@ exit 0
|
||||
exit;
|
||||
}
|
||||
|
||||
function gen_id() {
|
||||
protected function gen_id () {
|
||||
$urllen = rand (KEY_MIN, KEY_MAX);
|
||||
$string = '';
|
||||
$a = Array(); $a = array_merge(range('A', 'Z'), range('a', 'z'));
|
||||
@@ -220,42 +256,13 @@ function gen_id() {
|
||||
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);
|
||||
$sth = $dbh->query("SELECT paste FROM paste WHERE id='$q'");
|
||||
|
||||
if ($sth === false) {
|
||||
$e = $r->errorInfo();
|
||||
showindex($e[2]);
|
||||
self::showindex($e[2]);
|
||||
exit;
|
||||
}
|
||||
|
||||
@@ -275,8 +282,7 @@ function query_db($q) {
|
||||
}
|
||||
}
|
||||
|
||||
function store_data($paste) {
|
||||
if (! file_exists(DBPATH)) { init_db(); }
|
||||
public function store_data ($paste) {
|
||||
$dbh = new PDO("sqlite:" . DBPATH);
|
||||
$q = 0;
|
||||
|
||||
@@ -285,7 +291,8 @@ function store_data($paste) {
|
||||
$q = false;
|
||||
break;
|
||||
}
|
||||
$id = gen_id();
|
||||
|
||||
$id = self::gen_id();
|
||||
$q++;
|
||||
} while (! $dbh->query("SELECT id FROM paste WHERE id='$id'"));
|
||||
|
||||
@@ -349,13 +356,14 @@ function store_data($paste) {
|
||||
|
||||
if ($sth === false) {
|
||||
$e = $sth->errorInfo();
|
||||
showindex($e[2]);
|
||||
self::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
|
||||
|
||||
Reference in New Issue
Block a user