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 b66e758d981314bfd9de6dbfe0385785908a884c v1.0.0
4db108cffb1f05d2c42ff71e22d6c2330b4f45ad v1.1.0 4db108cffb1f05d2c42ff71e22d6c2330b4f45ad v1.1.0
0a90758f5df3d039490b57fad1091af22fb47524 v2.0.0

273
index.php
View File

@@ -1,23 +1,59 @@
<?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");
header('Content-Language: en, ru'); define ('SITE_ADDRESS', $config["siteaddress"]);
header('Pragma: no-cache'); define ('BASEURL', $config["baseurl"]);
header('Cache-Control: no-cache'); 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> <!DOCTYPE html>
<html lang="ru"> <html lang="ru">
@@ -54,9 +90,9 @@ function showindex($text) {
</select> </select>
</form> </form>
<?php <?php
if ($text != '') { if ($text != '') {
printf (" <p><table align=\"center\" frame=\"void\" border=\"0\"><td>%s</td></table></p>\n", $text); printf (" <p><table align=\"center\" frame=\"void\" border=\"0\"><td>%s</td></table></p>\n", $text);
} }
?> ?>
<p style="font-size: small;"> <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/> 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> </body>
</html> </html>
<?php <?php
} elseif (APP_TYPE == 'short') { } elseif (APP_TYPE == 'short') {
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
<html lang="ru"> <html lang="ru">
@@ -107,20 +143,20 @@ function showindex($text) {
</select> </select>
</form> </form>
<?php <?php
if ($text != '') { if ($text != '') {
printf (" <table style=\"margin: 0px auto;\"><td>%s</td></table>\n", $text); printf (" <table style=\"margin: 0px auto;\"><td>%s</td></table>\n", $text);
} }
?> ?>
</body> </body>
</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"');
?> ?>
#!/bin/bash #!/bin/bash
@@ -183,13 +219,13 @@ else
done | curl -F "exs=<-" <?php print SITE_ADDRESS; ?> done | curl -F "exs=<-" <?php print SITE_ADDRESS; ?>
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";');
?> ?>
#!/usr/bin/env bash #!/usr/bin/env bash
@@ -205,99 +241,70 @@ fi
exit 0 exit 0
'; ';
<?php <?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; exit;
} }
$sth->setFetchMode(PDO::FETCH_ASSOC); protected function gen_id () {
$data = $sth->fetch(); $urllen = rand (KEY_MIN, KEY_MAX);
$dbh = null; $string = '';
$a = Array(); $a = array_merge(range('A', 'Z'), range('a', 'z'));
if ($data === false) { for ($i = 0; $i < $urllen; $i++) {
return($data); $string .= $a[rand (0, count($a) - 1)];
} else {
if (isset($data['etime']) and ($data['etime'] < time())){
return false;
} }
$data = $data['paste']; return($string);
return($data); # false if no data or no key in db
} }
}
function store_data($paste) { public function query_db ($q) {
if (! file_exists(DBPATH)) { init_db(); } $dbh = new PDO("sqlite:" . DBPATH);
$dbh = new PDO("sqlite:" . DBPATH); $sth = $dbh->query("SELECT paste FROM paste WHERE id='$q'");
$q = 0;
do { if ($sth === false) {
if ($q > 100) { # assume key-value space depleted if we try more than 100 times $e = $r->errorInfo();
$q = false; self::showindex($e[2]);
break; exit;
} }
$id = gen_id();
$q++;
} while (! $dbh->query("SELECT id FROM paste WHERE id='$id'"));
if($q === false) { $sth->setFetchMode(PDO::FETCH_ASSOC);
return('Storage depleted.'); $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(); public function store_data ($paste) {
$etime = $ctime + 14515200; $dbh = new PDO("sqlite:" . DBPATH);
$q = 0;
if(isset($_POST['etime'])) { do {
switch ($_POST['etime']) { 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 case '1': # 1 hour
$etime = $ctime + 3600; $etime = $ctime + 3600;
break; break;
@@ -341,20 +348,21 @@ function store_data($paste) {
case '13': # forever case '13': # forever
$etime = "null"; $etime = "null";
break; 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 = ''; $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