Convert to using htpasswd for setting a web page password

This commit is contained in:
Bill Wilson 2015-07-30 17:58:47 -05:00
parent 3ee4d62943
commit f158b72782
9 changed files with 99 additions and 177 deletions

3
.gitignore vendored
View File

@ -1,6 +1,5 @@
scripts
www/password.php
www/session
www/.htpasswd
www/FIFO
www/media
media

View File

@ -28,6 +28,8 @@ server {
# include snippets/snakeoil.conf;
root PIKRELLCAM_WWW;
auth_basic "Restricted";
auth_basic_user_file PIKRELLCAM_WWW/.htpasswd;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;

View File

@ -24,6 +24,9 @@ server {
root PIKRELLCAM_WWW;
index index.php index.html index.htm;
auth_basic "Restricted";
auth_basic_user_file PIKRELLCAM_WWW/.htpasswd;
# Make site accessible from http://localhost/
server_name localhost;

View File

@ -54,18 +54,36 @@ else
AUTOSTART=no
fi
HTPASSWD=www/.htpasswd
PASSWORD=""
echo ""
echo "Enter a password if you want a password login for the web page."
echo "Enter a blank entry if you do not want the password login."
echo "The password will be configured in: $PWD/www/password.php"
echo -n "Enter password :"
read resp
if [ "$resp" == "" ]
if [ -f $HTPASSWD ]
then
PASSWORD="unset"
echo "A web password is already set."
echo -n "Do you want to change the password (yes/no)? "
read resp
if [ "$resp" == "y" ] || [ "$resp" == "yes" ]
then
SET_PASSWORD=yes
else
PASSWORD=$resp
SET_PASSWORD=no
fi
else
SET_PASSWORD=yes
fi
if [ "$SET_PASSWORD" == "yes" ]
then
rm -f $HTPASSWD
echo "Enter a password for a web page login for user: $USER"
echo "Enter a blank entry if you do not want the password login."
echo -n "Enter password: "
read PASSWORD
fi
echo ""
@ -77,7 +95,7 @@ DEB_VERSION=`cat /etc/debian_version`
# =============== apt install needed packages ===============
#
PACKAGE_LIST=""
for PACKAGE in gpac php5 php5-common php5-fpm nginx libav-tools bc sshpass mpack imagemagick
for PACKAGE in gpac php5 php5-common php5-fpm nginx libav-tools bc sshpass mpack imagemagick apache2-utils
do
if ! dpkg-query -l $PACKAGE &> /dev/null
then
@ -90,7 +108,6 @@ then
echo "Installing packages: $PACKAGE_LIST"
echo "Running: apt-get update"
sudo apt-get update
echo "Installing packages: $PACKAGE_LIST"
sudo apt-get install -y $PACKAGE_LIST
else
echo "No packages need to be installed."
@ -269,44 +286,26 @@ fi
sudo chown $USER.www-data $fifo
sudo chmod 664 $fifo
# =============== Setup Password ===============
#
SESSION_PATH=$PWD/www/session
if [ ! -f www/password.php ]
OLD_SESSION_PATH=www/session
if [ -d $OLD_SESSION_PATH ]
then
WRITE_PASSWORD=yes
else
if ! grep -q $PASSWORD www/password.php 2>/dev/null \
|| ! grep -q $SESSION_PATH www/password.php 2>/dev/null
then
WRITE_PASSWORD=yes
fi
sudo rm -rf $OLD_SESSION_PATH
fi
if [ "$WRITE_PASSWORD" == "yes" ]
OLD_PASSWORD=www/password.php
if [ -f $OLD_PASSWORD ]
then
if [ "$PASSWORD" == "unset" ]
then
echo "Setting web page password to not require a password."
else
echo "Setting web page password file with password: $PASSWORD"
fi
cat << EOF > www/password.php
<?php
define("PASSWORD", "$PASSWORD");
define("SESSION_PATH", "$SESSION_PATH");
?>
EOF
rm -f $OLD_PASSWORD
fi
if [ ! -d $SESSION_PATH ]
if [ "$PASSWORD" != "" ]
then
mkdir $SESSION_PATH
htpasswd -bc $HTPASSWD $USER $PASSWORD
sudo chown $USER.www-data $HTPASSWD
fi
sudo chown $USER.www-data $SESSION_PATH
sudo chmod 775 $SESSION_PATH
# =============== copy scripts-dist into scripts ===============

View File

@ -1,9 +1,3 @@
<?php
if (file_exists('password.php'))
{
require_once("password_check.php");
}
?>
<?php
require_once(dirname(__FILE__) . '/config.php');
?>
@ -62,7 +56,7 @@ if (file_exists('password.php'))
<a href="media.php?dir=<?php echo VIDEO_DIR; ?>"
class="btn-control"
>Videos</a>
<a href="thumbnails.php"
<a href="thumbs.php"
class="btn-control"
>Thumbs</a>
<a href="media.php?dir=<?php echo STILL_DIR; ?>"

View File

@ -200,7 +200,7 @@ function next_select($dir, $cur_file)
<?php
if ("$media_dir" == "media/videos")
{
echo '<a href="thumbnails.php">
echo '<a href="thumbs.php">
<span style="margin-left: 16px;">Thumbs </span>';
}
?>

View File

@ -1,93 +0,0 @@
<?php
require_once("password.php");
require_once(dirname(__FILE__) . '/config.php');
session_cache_limiter('private');
session_cache_expire(86400);
ini_set('session.cookie_lifetime', 86400);
ini_set('session.gc_maxlifetime', 86400);
ini_set('session.gc_probability', 1);
ini_set('session.gc_divisor', 50);
$session_path = SESSION_PATH;
ini_set("session.save_path", $session_path);
session_start();
class passwdCheck
{
var $password = PASSWORD;
function showLoginForm()
{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<title>PiKrellCam Login</title>
<link href="js-css/pikrellcam.css" rel="stylesheet" type="text/css" />
<style>
#container
{
margin: auto;
width: 400px;
border-width:1px;
border-style:solid;
background-color: #e8e8e8;
}
</style>
</head>
<body background="images/paper1.png" style="padding-top:40px">
<div id="container">
<div id="header" style="padding:6px; text-align:center; background-color:#d0d0d0;">
<selected>
<?php echo TITLE_STRING; ?> Login
</selected>
</div>
<div>
<form style="margin:18px;" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<center>
<p>Password:
<input name="passwd" type="password" size="16" />
</p>
<p>
<input type="submit" name="submitBtn" class="btn-control" value="Login" />
</p>
</center>
</form>
</div>
</div>
</body>
<?php
}
function login()
{
if ($this->password == "unset")
$_SESSION['loggedin'] = true;
$loggedin = isset($_SESSION['loggedin']) ? $_SESSION['loggedin'] : false;
if ( (!isset($_POST['submitBtn'])) && (!($loggedin)))
{
$_SESSION['loggedin'] = false;
$this->showLoginForm();
exit();
}
else if (isset($_POST['submitBtn']))
{
$pass = isset($_POST['passwd']) ? $_POST['passwd'] : '';
if ($pass != $this->password)
{
$_SESSION['loggedin'] = false;
$this->showLoginForm();
exit();
}
else
$_SESSION['loggedin'] = true;
}
}
}
$protector = new passwdCheck();
$protector->login();
?>

View File

@ -1,36 +0,0 @@
<?php
// display JPEG images in directory in name order, with clickable links
function showGallery( $pathToVideo, $pathToThumbs )
{
$cols = 4; // how many columns on the HTML page
$output = "<html>";
$output .= "<head><title>Thumbnails</title></head>";
$output .= "<body>";
$output .= "<center><h3><a href=index.php>PiKrellCam</a></h3></center>";
$output .= "<table cellspacing=\"0\" cellpadding=\"2\" width=\"500\">";
$output .= "<tr>";
$dir = opendir( $pathToThumbs ); // open the directory
$a=array(); // hold filenames in array so we can sort them in order
while (false !== ($fname = readdir($dir))) // loop through directory
{
if (substr($fname, -4) == '.jpg') // select only *.jpg filenames
{ $a[]=$fname; } // add this filename to the array
}
closedir( $dir ); // close the directory, all done reading
$counter = 0; // count how many images total so far
sort($a); // sorts the array of filenames in-place
foreach($a as $fname){
$vname = rtrim($fname, ".th.jpg") . ".mp4"; // filename X.th.jpg becomes X.mp4
$output .= "<td valign=\"middle\" align=\"center\"><a href=\"{$pathToVideo}{$vname}\">";
$output .= "<img src=\"{$pathToThumbs}{$fname}\" border=\"0\" />";
$output .= "</a></td>";
$counter += 1;
if ( $counter % $cols == 0 ) { $output .= "</tr><tr>"; }
}
$output .= "</tr> </table> </body> </html>"; // end of HTML file
echo $output; // send out the completed HTML web page
}
showGallery("media/videos/","media/thumbs/"); // video and thumbnail directories
?>

54
www/thumbs.php Normal file
View File

@ -0,0 +1,54 @@
<?php
// display JPEG images in directory in name order, with clickable links
function showGallery( $pathToVideo, $pathToThumbs )
{
$output = "";
$dir = opendir( $pathToThumbs ); // open the directory
$a=array(); // hold filenames in array so we can sort them in order
while (false !== ($fname = readdir($dir))) // loop through directory
{ if (substr($fname, -4) == '.jpg') // select only *.jpg filenames
{ $a[]=$fname; } // add this filename to the array
}
closedir( $dir ); // close the directory, all done reading
$counter = 0; // count how many images total so far
rsort($a); // sorts the array of filenames in-place, in reverse order (newest at top)
foreach($a as $fname){
$vname = rtrim($fname, ".th.jpg") . ".mp4"; // filename X.th.jpg becomes X.mp4
$tFile = "{$pathToThumbs}{$fname}"; // full pathname of thumbnail image
$vFile = "{$pathToVideo}{$vname}"; // full pathname of video file
$vSize = round(filesize($vFile) / (1024*1024) ); // filesize in MB
$fModTime = filemtime($tFile);
$fDate = @date('m/d', $fModTime);
$fTime = @date('H:i:s', $fModTime);
$output .= "<fieldset style=\"display:inline; padding:2px; vertical-align:middle;\">";
$output .= "$fDate &nbsp;<b>$fTime</b>";
$output .= "<div style=float:right>{$vSize}M</div><br>";
$output .= "<a href=\"{$vFile}\">";
$output .= "<img src=\"{$tFile}\" border=\"0\" /></a></fieldset>";
$counter += 1;
}
$tNow = time();
$fDur = ($tNow - $fModTime) / 3600;
$hours = number_format($fDur,1); // hours with 1 decimal place
$tString = @date('Y/m/d H:i:s', $tNow);
echo "<html><head><title>Thumbnails</title></head><body>";
echo "<fieldset> ";
diskSpace($pathToVideo); // print disk usage
echo "<big><big><b> &nbsp; &nbsp;<a href=index.php>PiKrellCam</a></b></big> &nbsp; &nbsp; ";
echo "{$counter} in {$hours} hr</big></center>";
echo "<div style=float:right>{$tString}</div><hr>";
echo $output; // send out the completed HTML web page
echo "</fieldset></body> </html>"; // end of HTML file
}
function diskSpace( $vdir ){ // http://www.thecave.info/display-disk-free-space-percentage-in-php/
$df = disk_free_space($vdir); // total free
$dt = disk_total_space($vdir); // total on disk
$du = sprintf('%.0f',($dt - $df) / (1024*1024));; // total used (Mbytes)
$dp = sprintf('%.1f',(($dt - $df) / $dt) * 100); // percent used
echo "{$du} MB &nbsp;(${dp} %)";
}
showGallery("media/videos/","media/thumbs/"); // video and thumbnail directories
?>