add expire script and discord webhook

This commit is contained in:
Kieran 2017-08-16 00:33:49 +01:00
parent 0c725bb02e
commit ce7b4120a9
6 changed files with 92 additions and 19 deletions

22
clean.php Normal file
View File

@ -0,0 +1,22 @@
<?php
include("db.php");
echo 'Cleaning files...';
$db = new DB();
$fl = $db->GetExpiredFiles();
foreach($fl as $f) {
if(unlink($f->path)) {
$db->DeleteFile($f);
echo 'Deleted file: ' . $f->filename . ' (' . $f->hash160 . ')\n';
}else{
echo 'Cant delete file ' . $f->hash160 . '\n';
}
}
if(count($fl) > 0){
$discord_data = array("content" => 'Deleted ' . count($fl) . ' expired files.');
include('discord.php');
}
?>

View File

@ -1,13 +1,15 @@
<?php
/* DB SETTINGS */
define('_DB_HOST', '127.0.0.1');
define('_DB_DATABASE', 'baba');
define('_DB_USER', 'root');
define('_DB_PASS', 'root');
/* GENERAL SETTINGS */
define('_DEFAULT_TYPE', 'application/octet-stream');
define('_SITEURL', 'http://example.com/');
define('_UPLOADDIR', '/out/');
define('_FILEPATH', dirname ( __FILE__ ) . _UPLOADDIR);
?>
<?php
/* DB SETTINGS */
define('_DB_HOST', '127.0.0.1');
define('_DB_DATABASE', 'baba');
define('_DB_USER', 'root');
define('_DB_PASS', 'root');
/* GENERAL SETTINGS */
define('_DEFAULT_TYPE', 'application/octet-stream');
define('_SITEURL', 'http://example.com/');
define('_UPLOADDIR', '/out/');
define('_FILEPATH', dirname ( __FILE__ ) . _UPLOADDIR);
define('_DISCORD_WEBHOOK', 'DISCORD_HOOK_URL');
define('_FILE_EXPIRE_TIME', 30);
?>

43
db.php
View File

@ -86,7 +86,7 @@
function InsertFile($f)
{
$stmt = $this->mysqli->prepare("insert into files(hash160, hash256, mime, path, filename) values(?,?,?,?,?)");
$stmt = $this->mysqli->prepare("insert into files(hash160, hash256, mime, path, filename, expire) values(?,?,?,?,?, DATE_ADD(NOW(), INTERVAL " . _FILE_EXPIRE_TIME . " DAY))");
if($stmt)
{
$stmt->bind_param("sssss", $f->hash160, $f->hash256, $f->mime, $f->path, $f->filename);
@ -94,10 +94,19 @@
$stmt->close();
}
}
function DeleteFile($f)
{
$stmt = $this->mysqli->prepare("delete from files where id = ?");
if($stmt)
{
$stmt->bind_param("d", $f->id);
$stmt->execute();
$stmt->close();
}
}
function AddView($hash160)
{
$stmt = $this->mysqli->prepare("update files set views = views + 1, expire = DATE_ADD(NOW(), INTERVAL 30 DAY) where hash160 = ?");
$stmt = $this->mysqli->prepare("update files set views = views + 1, expire = DATE_ADD(NOW(), INTERVAL " . _FILE_EXPIRE_TIME . " DAY) where hash160 = ?");
if($stmt)
{
$stmt->bind_param("s", $hash160);
@ -105,5 +114,33 @@
$stmt->close();
}
}
function GetExpiredFiles()
{
$res = array();
$stmt = $this->mysqli->prepare("select id, hash160, hash256, mime, path, filename, views, created, expire from files where expire < CURRENT_TIMESTAMP");
if($stmt)
{
$stmt->execute();
$stmt->bind_result($id, $hash160, $hash256, $mime, $path, $filename, $views, $created, $expire);
while($stmt->fetch()){
$nf = new FileUpload();
$nf->id = $id;
$nf->hash160 = $hash160;
$nf->hash256 = $hash256;
$nf->mime = $mime;
$nf->path = $path;
$nf->filename = $filename;
$nf->views = $views;
$nf->created = $created;
$nf->expire = $expire;
array_push($res, $nf);
}
$stmt->close();
}
return $res;
}
};
?>

10
discord.php Normal file
View File

@ -0,0 +1,10 @@
<?php
if(_DISCORD_WEBHOOK != 'DISCORD_HOOK_URL')
{
$curl = curl_init(_DISCORD_WEBHOOK);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($discord_data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_exec($curl);
}
?>

View File

@ -114,8 +114,10 @@
<div id="footer">
<a href="https://github.com/v0l/void.cat">Github</a>
| <a href="https://twitter.com/chkn10deez">Twitter</a>
| <a href="http://discord.gg/8BkxTGs">Discord</a>
| Hosting: <?php echo explode("\t", exec("du -sh " . _FILEPATH))[0]; ?>
<br/><small>Files expire in 30 days if not viewed</small>
<br/><small>Files expire in <?php echo _FILE_EXPIRE_TIME; ?> days if not viewed</small>
<br/><img src="https://void.cat/graph"/>
</div>
</div>
<script src="public/main.js"></script>

View File

@ -102,8 +102,8 @@
$db->InsertFile($f_e);
//update sitemap
//include("gensitemap.php");
$discord_data = array("content" => _SITEURL . $f_e->hash160 . '&v');
include("discord.php");
}
//close streams