Skip to content

Instantly share code, notes, and snippets.

@ricardoalcocer
Last active November 20, 2020 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ricardoalcocer/e577a2582fe7eb6e65a27fb905caeab6 to your computer and use it in GitHub Desktop.
Save ricardoalcocer/e577a2582fe7eb6e65a27fb905caeab6 to your computer and use it in GitHub Desktop.
iTransfer - Like we transfer but not as good
Options -Indexes
RewriteEngine On
################################################################
## ROUTES
################################################################
RewriteRule ^f/([A-Za-z0-9-\s]+)/?$ ./gf.php?f=$1 [NC,L]
<?php
error_reporting(E_ALL | E_WARNING | E_NOTICE);
ini_set('display_errors', 0);
require_once('itransfer.php');
$iTrasnfer = new iTransfer();
if (isset($_GET['f'])){
$iTrasnfer->getFile($_GET['f']);
}else{
echo "NOOP";
}
exit;
?>
<?php
class MyDB extends SQLite3{
function __construct(){
$this->open('files.db');
}
}
class iTransfer {
public function __construct() {
}
public function getFilePath($hash){
// https://www.php.net/manual/en/sqlite3.open.php
$db = new MyDB();
$db->busyTimeout = 60000;
$results = $db->query('SELECT * FROM files WHERE id='.$hash);
// while ($row = $results->fetchArray(SQLITE3_ASSOC)) {
// var_dump($row['filepath']);
// }
$row = $results->fetchArray(SQLITE3_ASSOC);
$filePath = $row['filepath'];
return $filePath;
}
public function getFile($hash){
$basepath = "./files/";
// path for this hash
$path = $this->getFilePath($hash);
if ($path != "") $path = $basepath.$path;
if (file_exists($path)) {
// track this download
$this->saveDL($path);
// send file to browser
header("Content-Description: File Transfer");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".basename($path)."\"");
header("Expires: 0");
header("Cache-Control: must-revalidate");
header("Pragma: public");
header("Content-Length: " . filesize($path));
readfile($path);
}else{
echo "NOOP";
}
exit;
}
public function saveDL($path){
$basepath = "./files/";
str_replace($basepath,"",$path);
$userAgent = $_SERVER["HTTP_USER_AGENT"];
$db = new MyDB();
$db->busyTimeout = 60000;
$statement = $db->prepare('INSERT INTO log (fileid,useragent) VALUES (:id,:useragent)');
$statement->bindValue(':id', "1");
$statement->bindValue(':useragent', $userAgent);
$result = $statement->execute();
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment