void.cat/db.php
2017-07-06 01:31:13 +08:00

110 lines
2.7 KiB
PHP

<?php
include_once('config.php');
include_once('file.php');
class DB {
function __construct()
{
$this->error = null;
$this->mysqli = new mysqli(_DB_HOST, _DB_USER, _DB_PASS, _DB_DATABASE);
if ($this->mysqli->connect_errno) {
$this->error = "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
}
function __destruct()
{
if($this->error == null)
{
$this->mysqli->close();
}
}
function Exists256($hash)
{
$res = new FileUpload();
$stmt = $this->mysqli->prepare("select id, hash160, hash256, mime, path, filename, views, created, expire from files where hash256 = ? limit 1");
if($stmt)
{
$stmt->bind_param("s", $hash);
$stmt->execute();
$stmt->bind_result($res->id, $res->hash160, $res->hash256, $res->mime, $res->path, $res->filename, $res->views, $res->created, $res->expire);
$stmt->fetch();
$stmt->close();
}
return $res;
}
function GetFile($hash)
{
$res = new FileUpload();
$stmt = $this->mysqli->prepare("select id, hash160, hash256, mime, path, filename, views, created, expire from files where hash160 = ? limit 1");
if($stmt)
{
$stmt->bind_param("s", $hash);
$stmt->execute();
$stmt->bind_result($res->id, $res->hash160, $res->hash256, $res->mime, $res->path, $res->filename, $res->views, $res->created, $res->expire);
$stmt->fetch();
$stmt->close();
}
return $res;
}
function GetFiles()
{
$res = array();
$stmt = $this->mysqli->prepare("select id, hash160, hash256, mime, path, filename, views, created, expire from files");
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;
}
function InsertFile($f)
{
$stmt = $this->mysqli->prepare("insert into files(hash160, hash256, mime, path, filename) values(?,?,?,?,?)");
if($stmt)
{
$stmt->bind_param("sssss", $f->hash160, $f->hash256, $f->mime, $f->path, $f->filename);
$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 = ?");
if($stmt)
{
$stmt->bind_param("s", $hash160);
$stmt->execute();
$stmt->close();
}
}
};
?>