Files
dl/index.php

507 lines
13 KiB
PHP
Raw Normal View History

2017-02-07 20:04:34 +00:00
<?php
$FP="files/".$_SERVER['PHP_AUTH_USER']."/";
system ("mkdir -p '" . $FP . "'");
define('CHUNK_SIZE', 1024*1024); // Size (in bytes) of tiles chunk
// Read a file and display its content chunk by chunk
function readfile_chunked($filename, $retbytes = TRUE) {
$buffer = '';
$cnt =0;
// $handle = fopen($filename, 'rb');
$handle = fopen($filename, 'rb');
if ($handle === false) {
return false;
}
while (!feof($handle)) {
$buffer = fread($handle, CHUNK_SIZE);
echo $buffer;
ob_flush();
flush();
if ($retbytes) {
$cnt += strlen($buffer);
}
}
$status = fclose($handle);
if ($retbytes && $status) {
return $cnt; // return num. bytes delivered like readfile() does.
}
return $status;
}
if (isset($_GET['dlFile'])){
# doesn't work -- always gets killed (out of memory?)
header('Content-disposition: attachment; filename='. $_GET["dlFile"]);
header('Content-type: application/octet-stream');
header('Content-Length: ' . filesize($FP . $_GET["dlFile"]));
header("Pragma: no-cache");
header("Expires: 0");
readfile_chunked($FP . $_GET["dlFile"]);
#readfile($FP . $_GET["dlFile"]);
exit(0);
}
if (isset($_GET['url'])) {
system("cd '" . $FP . "';/usr/bin/wget --progress=dot '" . $_GET["url"] . "' 2>&1");
exit(0);
}
if (isset($_GET['ytdl'])) {
system("cd '" . $FP . "';/var/www/dl/ytdl.sh '" . $_GET["ytdl"] . "'");
exit(0);
}
2017-02-07 20:04:34 +00:00
if (isset($_GET['getFileList'])) {
header('Content-type: application/json');
header("Pragma: no-cache");
header("Expires: 0");
system("cd '" . $FP . "';/bin/echo -n '{\"FileList\":['; /bin/ls | /usr/bin/sort | while read x; do if [ ! -f \".\${x}.md5\" ]; then /usr/bin/md5sum \"\${x}\" | /usr/bin/cut -c1-32 > \".\${x}.md5\"; fi; /bin/echo -n '{\"file\":{\"name\":\"'\${x}'\", \"md5\":\"'`/bin/cat \".\${x}.md5\"`'\", \"size\":\"'`/usr/bin/du -m \"\${x}\" | /usr/bin/cut -f1`'\"}},'; done; /bin/echo -n '{}]}'");
exit (0);
}
if (isset($_SERVER['HTTP_X_FILENAME'])) {
// create new file if the name already exists - don't override
$origFileName=$_SERVER['HTTP_X_FILENAME'];
$fileName=$origFileName;
$i=0;
while (file_exists($FP . $fileName)) {
$fileName=$origFileName . "." . ++$i;
}
file_put_contents( $FP . $fileName, file_get_contents('php://input'));
2017-02-07 20:04:34 +00:00
echo $fileName . " uploaded";
system("cd '" . $FP . "';/usr/bin/md5sum '" . $fileName . "' | /usr/bin/cut -c1-32 > '." . $fileName . ".md5'");
exit(0);
}
if (isset($_GET['kill'])) {
unlink($FP . $_GET['kill']);
unlink($FP . "." . $_GET['kill'] . ".md5");
exit(0);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>NetSquat-Downloader
<?php
echo "[". $_SERVER['PHP_AUTH_USER'] . "]";
?>
</title>
<style>
#filedrag {
display: none;
font-weight: bold;
text-align: center;
padding: 1em 0;
margin: 1em 0;
color: #555;
border: 2px dashed #555;
border-radius: 7px;
cursor: default;
}
#filedrag.hover {
color: #f00;
border-color: #f00;
border-style: solid;
box-shadow: inset 0 3px 4px #888;
}
table.fileList {
border-collapse: collapse;
width:100%;
}
tr:nth-child(even) {
background-color: #f2f2f2;
}
tr:hover {
background-color: #bbbbbb;
}
th.fileList {
border: 2px solid black;
font-weight: bold;
background: #ffcccc;
}
td.fileList {
border: 1px solid black;
}
fieldset.upload {
background: #f2f2f2;
border: 1px solid black;
}
legend.upload {
background: #EEEEEE;
border: 1px solid black;
font-weight: bold;
}
body {
font-family:Verdana,sans-serif;
}
</style>
</head>
<body>
<span style='font-size:40px'><span style='color:#777777'>Down</span>-<span style='color:#FF0000'>loader</span></span><span style='font-size:14px'> by <a href='mailto:joe@kawomi.com'>joe@kawomi.com</a></span>
2017-02-07 20:04:34 +00:00
<br>
<br>
<fieldset class='upload'><legend class='upload'>Upload local file</legend>
<form action="" id='localFileUpload' method="POST" enctype="multipart/form-data">
<input type="file" id='fileselect' name="files[]" multiple="multiple">
<div id='filedrag'>or drop files here</div>
</form>
<div id="UploadProgressBars">
</div>
</fieldset>
<br>
<fieldset class='upload'><legend class='upload'>Download remote url</legend>
<input name="url" id="url" size="100">
<button id='btnDownload' onclick="downloader.downloadURL(document.getElementById('url').value,'url')" >Download</button>
</fieldset>
<fieldset class='upload'><legend class='upload'>Download youtube</legend>
<input name="ytdl" id="ytdl" size="100">
<button id='btnDownload' onclick="downloader.downloadURL(document.getElementById('ytdl').value,'ytdl')" >Download</button>
</fieldset>
2017-02-07 20:04:34 +00:00
<div id="DownloadProgressBars">
</div>
<hr>
<table id="fileList" class='fileList' style="font-family:monospace">
<tbody>
<tr><th class='fileList'>File</th><th class='fileList'>Size(MB)</th><th class='fileList'>MD5</th><th class='fileList'>KILL</th></tr>
</tbody>
</table>
<hr>
<div id="messages">
<p>Status Messages</p>
</div>
<!--div id="myDiv">
CONSOLE
</div-->
<script>
/*
var myDiv;
console.origLog = console.log;
console.log = function(x) {
if(!myDiv) myDiv = document.getElementById('myDiv');
if(myDiv) myDiv.innerHTML = myDiv.innerHTML + "<br>" + x;
else console.origLog('can not determine myDiv');
}
*/
var downloader = (function () {
"use strict";
function $id(id) {
return document.getElementById(id);
}
function delRequest(file){
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
fillFileList();
}
};
xhr.open("GET", "?kill=" + encodeURIComponent(file), true);
xhr.send();
}
function fillFileList(){
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
var fileData = JSON.parse(xhr.responseText);
var tFL = document.getElementById("fileList");
tFL.deleteDataRows();
var i;
for(i =0;i<fileData.FileList.length-1;i+=1) {
tFL.addFile(fileData.FileList[i].file);
}
}
};
xhr.open("GET", "?getFileList", true);
xhr.send();
}
// cancel event and hover styling
function FileDragHover(e) {
e.stopPropagation();
e.preventDefault();
e.target.className = (e.type === "dragover" ? "hover" : "");
}
function ParseFile(file) {
Output(
"<p>File information: <strong>" + file.name +
"</strong> type: <strong>" + file.type +
"</strong> size: <strong>" + file.size +
"</strong> bytes</p>"
);
}
function FileSelectHandler(e) {
FileDragHover(e);
var files = e.target.files || e.dataTransfer.files;
var i;
for (i = 0; i < files.length; i++) {
ParseFile(files[i]);
UploadFile(files[i]);
}
}
function Init() {
var fileselect = $id("fileselect");
fileselect.addEventListener("change", FileSelectHandler, false);
var filedrag = $id("filedrag");
filedrag.addEventListener("dragover", FileDragHover, false);
filedrag.addEventListener("dragleave", FileDragHover, false);
filedrag.addEventListener("drop", FileSelectHandler, false);
filedrag.style.display = "block";
fillFileList();
}
function downloadURL(url,type) {
2017-02-07 20:04:34 +00:00
var progressBarId=Math.random()*100000000000000000;
$id("DownloadProgressBars").add(progressBarId,url)
$id("DownloadProgressBars").setMax(progressBarId,100)
$id(type).value = "";
2017-02-07 20:04:34 +00:00
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
$id("DownloadProgressBars").remove(progressBarId)
fillFileList();
}
}
// TODO: The progress update only fires at the end, used to work before.. have to look sure if it's server or client...
xhr.onprogress = function(event) {
// report progress
console.log(`Loaded ${event.loaded} of ${event.total}`);
var tmpResponse=event.currentTarget.response;
var lastIndex=tmpResponse.lastIndexOf("%");
console.log(new Date(),tmpResponse,lastIndex);
if (lastIndex>0) {
var pctFinish=tmpResponse.substring(lastIndex-3,lastIndex);
//console.log(pctFinish,tmpResponse);
$id("DownloadProgressBars").setProgress(progressBarId,pctFinish);
}
};
xhr.addEventListener("progress",xhr.onprogress,false);
xhr.open("GET", "?" + type + "=" + encodeURIComponent(url), true);
2017-02-07 20:04:34 +00:00
xhr.send();
2017-02-07 20:04:34 +00:00
var timer;
timer = window.setInterval(function() {
if (xhr.readyState === 4) {
window.clearTimeout(timer);
Output('UrlDownload done <br />');
}
/* var tmpResponse=xhr.responseText;
var lastIndex=tmpResponse.lastIndexOf("%");
console.log(tmpResponse,lastIndex);
if (lastIndex>0) {
var pctFinish=tmpResponse.substring(lastIndex-3,lastIndex);
//console.log(pctFinish,tmpResponse);
$id("DownloadProgressBars").setProgress(progressBarId,pctFinish);
}
console.log(xhr.responseText); */
}, 300);
2017-02-07 20:04:34 +00:00
}
function UploadFile(file) {
var xhr = new XMLHttpRequest();
var progressBarId=Math.random()*100000000000000000;
$id("UploadProgressBars").add(progressBarId,file.name);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
Output(xhr.responseText);
fillFileList();
$id("UploadProgressBars").remove(progressBarId);
}
}
// progress bar
xhr.upload.addEventListener("progress", function(e) {
$id("UploadProgressBars").setMax(progressBarId, e.total);
$id("UploadProgressBars").setProgress(progressBarId, e.loaded);
}, false);
xhr.open("POST", $id("localFileUpload").action, true);
xhr.setRequestHeader("X_FILENAME", file.name);
xhr.send(file);
}
// output information
function Output(msg) {
var m = $id("messages");
m.innerHTML = msg + "<br>"+ m.innerHTML;
}
// call initialization file
if (window.File && window.FileList && window.FileReader) {
Init();
}
return {
$id:$id,
delRequest:delRequest,
downloadURL:downloadURL
};
}());
downloader.progressBar = (function () {
"use strict";
var min,max,current;
function $id(id){
return(downloader.$id(id));
}
function add(id,description){
var tn = document.createElement("div");
tn.setAttribute("id", "ProgressFile" + id);
tn.appendChild(document.createTextNode("Name: " + description));
this.appendChild(tn);
var newPB=document.createElement("progress");
newPB.setAttribute("id" ,"progressUL" + id);
this.appendChild(newPB);
var pt=document.createElement("span");
pt.setAttribute("id" ,"progressPct" + id);
this.appendChild(pt);
setMin(id,0);
setMax(id,100);
setProgress(id,0);
}
function setPct(id){
var pct=0;
if ((max >0)&&(current>0)) {
pct=Math.round(current/max*100);
}
$id("progressPct" + id).innerText = "["+Math.round(current/max*100)+" %]";
}
function setProgress(id,value){
current=value;
if ($id("progressUL" + id) !== null) {
$id("progressUL" + id).value = value;
setPct(id);
}
2017-02-07 20:04:34 +00:00
}
function remove(id){
$id("progressPct" + id).parentNode.removeChild($id("progressPct" + id));
$id("progressUL" + id).parentNode.removeChild($id("progressUL" + id));
$id("ProgressFile" + id).parentNode.removeChild($id("ProgressFile" + id));
}
function setMin(id,value){
min=value;
$id("progressUL" + id).min = value;
}
function setMax(id,value){
max=value
$id("progressUL" + id).max = value;
}
function bind(theDomElement) {
theDomElement.add=add;
theDomElement.setProgress=setProgress;
theDomElement.setMin=setMin;
theDomElement.setMax=setMax;
theDomElement.remove=remove;
}
return {
bind:bind
};
}());
downloader.fileBrowser = (function () {
"use strict";
function addFile(fileData){
var row;
var cell1;
var cell2;
var cell3;
var cell4;
row = this.insertRow(this.rows.length);
cell1 = row.insertCell(0);
cell1.innerHTML="<a href='<?php echo $FP ?>" + fileData.name +"'>" + fileData.name + "</a>" ;
cell1.className='fileList';
cell2 = row.insertCell(1);
cell2.innerHTML=fileData.size ;
try { // dirty trick: iOS Safari doesn't like the next line...
cell2.style='text-align:right';
} catch(e) {}
cell2.className='fileList';
cell3 = row.insertCell(2);
cell3.innerHTML=fileData.md5 ;
cell3.className='fileList';
cell4 = row.insertCell(3);
cell4.innerHTML='<button onclick="downloader.delRequest(\''+fileData.name+'\')">X</button>';
cell4.className='fileList';
}
function deleteDataRows() {
var i;
for(i = this.rows.length-1; i > 0;i-=1) {
this.deleteRow(i);
}
}
document.getElementById("fileList").addFile=addFile;
document.getElementById("fileList").deleteDataRows=deleteDataRows;
}());
window.onload = function(){
downloader.progressBar.bind(downloader.$id("UploadProgressBars"));
downloader.progressBar.bind(downloader.$id("DownloadProgressBars"));
}
</script>
</body>
</html>
<!--
vim: sw=2 ts=2 si expandtab nowrap
-->