Password protection added with session

Functions freeSpace and destFolderExists moved to utilities.php
This commit is contained in:
p1rox 2014-03-21 21:15:45 +01:00
parent 99a98660d9
commit 30f2477d34
5 changed files with 138 additions and 64 deletions

View file

@ -1,8 +1,22 @@
<?php // Config file
<?php
// Variables :
$mainPage = "index.php"; //Rename it only if you change index.php to downloader.php for example
$folder = "videos/"; // -> with "/" <- at the end. Directory where you videos are downloaded
$listPage = 'list.php';
/*** Config file ***/
//Rename it only if you change index.php to downloader.php for example
$mainPage = "index.php";
// -> with "/" <- at the end. Directory where you videos are downloaded
$folder = "videos/";
//Rename it only if you change list.php to myvideos.php for example
$listPage = "list.php";
// Enable password to access the panel
// 1 -> enable 0 -> disable
$security = 1;
// PHP::md5(); You can use md5.php to generate an other one
// default : root
$secretPassword = "63a9f0ea7bb98050796b649e85481845";
?>

View file

@ -1,5 +1,12 @@
<?php
require_once("config.php");
require_once("sessions.php");
require_once("utilities.php");
if(isset($_POST['passwd']) && !empty($_POST['passwd'])) startSession($_POST['passwd']);
if(isset($_GET['logout']) && $_GET['logout'] == 1) endSession();
?>
<!DOCTYPE html>
<?php require_once("config.php"); ?>
<html>
<head>
<meta charset="utf-8">
@ -27,7 +34,7 @@
<div class="container">
<h1>Download</h1>
<?php
if(isset($_GET['url']) && !empty($_GET['url']))
if(isset($_GET['url']) && !empty($_GET['url']) && $_SESSION['logged'] == 1)
{
$url = $_GET['url'];
$cmd = 'youtube-dl -o ' . escapeshellarg('./'.$folder.'%(title)s-%(uploader)s.%(ext)s') . ' ' . escapeshellarg($url) . ' 2>&1';
@ -48,7 +55,8 @@
echo '</div>';
}
}
else{?>
elseif(isset($_SESSION['logged']) && $_SESSION['logged'] == 1)
{ ?>
<form class="form-horizontal" action="<?php echo $mainPage; ?>">
<fieldset>
<div class="form-group">
@ -70,7 +78,7 @@
<div class="panel panel-info">
<div class="panel-heading"><h3 class="panel-title">Info</h3></div>
<div class="panel-body">
<p>Free space : <?php if(file_exists($folder)){ freeSpace(disk_free_space("./".$folder));} else {echo "Folder not found";} ?></b></p>
<p>Free space : <?php if(file_exists($folder)){ human_filesize(disk_free_space("./".$folder),1);} else {echo "Folder not found";} ?></b></p>
<p>Download folder : <?php echo $folder ;?></p>
</div>
</div>
@ -91,6 +99,22 @@
</div>
<?php
}
else{ ?>
<form class="form-horizontal" action="<?php echo $mainPage; ?>" method="POST" >
<fieldset>
<legend>You need to login first</legend>
<div class="form-group">
<div class="col-lg-4"></div>
<div class="col-lg-4">
<input class="form-control" id="passwd" name="passwd" placeholder="Password" type="password">
</div>
<div class="col-lg-4"></div>
</div>
</fieldset>
</form>
<?php
}
if(isset($_SESSION['logged']) && $_SESSION['logged'] == 1) echo '<p><a href="index.php?logout=1">Logout</a></p>';
?>
</div><!-- End container -->
<footer>
@ -100,30 +124,4 @@
</div>
</footer>
</body>
</html>
<?php
function freeSpace($Bytes)
{
$Type = array("", "Ko", "Mo", "Go", "To");
$Index = 0;
while($Bytes >= 1024)
{
$Bytes /= 1024;
$Index++;
}
return(round($Bytes) . " " . $Type[$Index]);
}
function destFolderExists($destFolder)
{
if(!file_exists($destFolder))
{
echo '<div class="alert alert-danger">
<strong>Error : </strong> Destination folder doesn\'t exist or is not found here.
</div>';
}
}
?>
</html>

View file

@ -1,5 +1,11 @@
<!DOCTYPE html>
<?php require_once("config.php"); ?>
<?php
require_once("config.php");
require_once("sessions.php");
require_once("utilities.php");
if(isset($_GET['logout']) && $_GET['logout'] == 1) endSession();
?>
<html>
<head>
<meta charset="utf-8">
@ -17,7 +23,7 @@
</button>
<a class="navbar-brand" href="<?php echo $mainPage; ?>">Youtube-dl WebUI</a>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav">
<li><a href="<?php echo $mainPage; ?>">Download</a></li>
<li class="active"><a href="<?php echo $listPage; ?>">List of videos</a></li>
@ -26,7 +32,8 @@
</div>
<div class="container">
<?php
if(isset($_SESSION['logged']) && $_SESSION['logged'] == 1)
{
if(isset($_GET['fileToDel']))
{
$fileToDel = $_GET['fileToDel'];
@ -63,9 +70,8 @@
<strong>Error : </strong> Destination folder doesn\'t exist or is not found here.
</div>';
}
else{
?>
<h2>List of available videos :</h2>
else { ?>
<h2>List of available videos :</h2>
<table class="table table-striped table-hover ">
<thead>
<tr>
@ -77,31 +83,32 @@
<tbody>
<tr>
<?php
foreach(glob($folder."*") as $file)
{
$filename = str_replace($folder, "", $file); // Need to fix accent problem with something like this : utf8_encode
echo "<tr>"; //New line
echo "<td height=\"30px\"><a href=\"$folder$filename\">$filename</a></td>"; //1st col
echo "<td>".human_filesize(filesize($folder.$filename))."</td>"; //2nd col
echo "<td><a href=\"".$listPage."?fileToDel=$filename\" class=\"text-danger\">Delete</a></td>"; //3rd col
echo "</tr>"; //End line
foreach(glob($folder."*") as $file)
{
$filename = str_replace($folder, "", $file); // Need to fix accent problem with something like this : utf8_encode
echo "<tr>"; //New line
echo "<td height=\"30px\"><a href=\"$folder$filename\">$filename</a></td>"; //1st col
echo "<td>".human_filesize(filesize($folder.$filename))."</td>"; //2nd col
echo "<td><a href=\"".$listPage."?fileToDel=$filename\" class=\"text-danger\">Delete</a></td>"; //3rd col
echo "</tr>"; //End line
}
}
}
?>
}
else {
echo '<div class="alert alert-danger"><strong>Access denied :</strong> You must sign in before !</div>';
} ?>
</tr>
</tbody>
</table>
<br/>
<?php if(!isset($_GET['fileToDel'])) echo "<a href=".$mainPage.">Back to download page</a>"; ?>
</div>
<?php if(!isset($_GET['fileToDel'])) echo "<a href=".$mainPage.">Back to download page</a>"; ?>
</div><!-- End container -->
<br>
<footer>
<div class="well text-center">
<p><a href="https://github.com/p1rox/Youtube-dl-WebUI" target="_blank">Fork me on Github</a></p>
<p>Created by <a href="https://twitter.com/p1rox" target="_blank">@p1rox</a> - Web Site : <a href="http://p1rox.fr" target="_blank">p1rox.fr</a></p>
</div>
</footer>
</body>
</html>
<?php
function human_filesize($bytes, $decimals = 0)
{
$sz = 'BKMGTP';
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor];
}
?>
</html>

33
sessions.php Normal file
View file

@ -0,0 +1,33 @@
<?php
// Start session
session_start();
// Include config.php
require_once("config.php");
function startSession($pass)
{
$pass = htmlentities($pass);
global $security;
if($security == 1)
{
if(passwordMatch($pass)) $_SESSION['logged'] = 1;
else $_SESSION['logged'] = 0;
} else {
$_SESSION['logged'] = 1;
}
}
function passwordMatch($pass)
{
global $secretPassword;
if(md5($pass) == $secretPassword) return 1;
else return 0;
}
function endSession()
{
global $security;
if($security == 1) session_destroy();
}
?>

22
utilities.php Normal file
View file

@ -0,0 +1,22 @@
<?php
// Test if destination folder exists
function destFolderExists($destFolder)
{
if(!file_exists($destFolder))
{
echo '<div class="alert alert-danger">
<strong>Error : </strong> Destination folder doesn\'t exist or is not found here.
</div>';
}
}
// Convert bytes to a more user-friendly value
function human_filesize($bytes, $decimals = 0)
{
$sz = 'BKMGTP';
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor];
}
?>