239 lines
5.0 KiB
PHP
239 lines
5.0 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Debug output to the browser console
|
|
*/
|
|
|
|
function console_log( $data ) {
|
|
echo "<script>";
|
|
echo "console.log(". json_encode( $data ) . ")";
|
|
echo "</script";
|
|
}
|
|
/**
|
|
* List the subdirectories of a folder.
|
|
* Ignore ones marked hidden.
|
|
*/
|
|
|
|
function listFolders( $root, $hidden=false) {
|
|
|
|
$list = array();
|
|
|
|
if( !is_dir( $root ) ) {
|
|
throw new Exception("'".$root."' is not a folder!");
|
|
}
|
|
|
|
$root_content = scandir( $root );
|
|
|
|
/**
|
|
* Catch no permission, or empty folder
|
|
*/
|
|
|
|
if( empty( $root_content) ) {
|
|
return $list;
|
|
}
|
|
|
|
foreach ( $root_content as $subdir ) {
|
|
if ( ($subdir[0] != "." || $hidden ) && is_dir( $path = $root."/".$subdir) ) {
|
|
$list[] = $path;
|
|
}
|
|
}
|
|
|
|
return $list;
|
|
}
|
|
|
|
/**
|
|
* List all files in the folder.
|
|
* Ignores hidden files.
|
|
*/
|
|
|
|
function listFiles( $root, $cascade=false, $first=false ) {
|
|
$list = array();
|
|
|
|
if( !is_dir( $root ) ) {
|
|
throw new Exception("'".$root."' is not a folder.");
|
|
}
|
|
|
|
$files = scandir( $root );
|
|
|
|
/**
|
|
* Catch no permission, or empty folder
|
|
*/
|
|
|
|
if ( empty( $files ) ) {
|
|
return $list;
|
|
}
|
|
|
|
foreach ( $files as $file ) {
|
|
if( $file[0] != "." ) {
|
|
|
|
if ( is_file( $path = $root."/".$file ) ) {
|
|
if ( $first ) {
|
|
return $path;
|
|
}
|
|
|
|
$list[] = $path;
|
|
} else {
|
|
if($cascade) {
|
|
$list = array_merge( $list, listFiles( $root . "/" . $file, true));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if($first) {
|
|
return $list[0];
|
|
}
|
|
|
|
return $list;
|
|
}
|
|
|
|
/**
|
|
* Discount strip_slashes - from a file name
|
|
*/
|
|
|
|
function tidyName( $name ) {
|
|
|
|
if ( strpos( $name, "/" ) > -1 ) {
|
|
return substr( $name, strrpos( $name, "/" ) + 1);
|
|
} else {
|
|
return $name;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Check directory structure for integrity
|
|
*/
|
|
|
|
function isPathValid( $file, $path ) {
|
|
$realFile = realpath( $file );
|
|
$realDir = realpath( $path );
|
|
|
|
/**
|
|
* If the two paths match, then the path is valid
|
|
*/
|
|
if ( $realFile == $realDir ) {
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* If the file path starts with the dir path,
|
|
* then the file path is valid
|
|
*/
|
|
|
|
if ( substr( $realFile, 0, strlen( $realDir ) ) == $realDir ) {
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Otherwise, the file is not in the path, and it is invalid.
|
|
*/
|
|
return false;
|
|
|
|
}
|
|
|
|
/**
|
|
* Set up headers and send a http reply with the given file.
|
|
*/
|
|
|
|
function sendFile( $image ) {
|
|
// Expires in 2 weeks
|
|
$expires = 60*60*24*14;
|
|
|
|
$lastModified = filemtime( $image );
|
|
|
|
//$etag = md5_file( $image );
|
|
|
|
header("Last-Modified: $lastModified GMT");
|
|
header("Pragma: public");
|
|
header("Cache-Control: max-age=$expires");
|
|
//header("Etag: $etag");
|
|
header("Expires: " . gmdate('D, d M Y H:i:s', time() + $expires) . " GMT");
|
|
header("Content-type: image/jpeg");
|
|
|
|
readfile( $image );
|
|
}
|
|
|
|
/**
|
|
* Check that a file is in a given folder.
|
|
*/
|
|
|
|
function isInFolder( $file, $folder ) {
|
|
return substr( $folder, 0, strlen( $file ) ) == $file;
|
|
}
|
|
|
|
/**
|
|
* Convert an absolute path (for filesystem) to a relative path (for web server)
|
|
*/
|
|
|
|
function absoluteToRelative( $file, $folder ) {
|
|
|
|
$realFile = realpath( $file );
|
|
$realDir = realpath( $folder );
|
|
|
|
if ($realFile == $realDir ) return "";
|
|
|
|
if ( !isInFolder( $realDir, $realFile ) ) {
|
|
throw new Exception("File is not in the photos folder.");
|
|
}
|
|
|
|
return substr( $realFile, strlen( $realDir ) + 1);
|
|
}
|
|
|
|
/**
|
|
* Convert a relative path (from the file server) to an absolute path (for the filesystem)
|
|
*/
|
|
|
|
function relativeToAbsolute( $file, $folder ) {
|
|
return $folder . "/" . $file;
|
|
}
|
|
|
|
/**
|
|
* Get all of the steps needed to get to where we are, from the home page
|
|
*/
|
|
function breadcrumbs( $path ) {
|
|
|
|
$list = array();
|
|
$slash = strpos( $path, "/");
|
|
|
|
while($slash > 0) {
|
|
$list[] = substr( $path, 0, $slash );
|
|
$path = substr( $path, $slash + 1 );
|
|
$slash = strpos( $path, "/" );
|
|
}
|
|
|
|
if( $path != "" ) {
|
|
$list[] = $path;
|
|
}
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
/**
|
|
* Create a smaller copy of the image to serve as a thumbnail.
|
|
*/
|
|
|
|
function createThumbnail( $source, $thumbnailPath, $thumbnailFolder ) {
|
|
|
|
require_once("resources/libs/php/ThumbLib.inc.php");
|
|
|
|
if ( !file_exists( $thumbnailPath ) || filectime( $source ) > filectime( $thumbnailPath ) ) {
|
|
|
|
if ( !file_exists( dirname( $thumbnailPath ) ) ) {
|
|
@mkdir( dirname( $thumbnailPath ) );
|
|
}
|
|
|
|
$thumbnail = PhpThumbFactory::create( $source );
|
|
$thumbnail->resize( 200, 200 );
|
|
$thumbnail->save( $thumbnailPath );
|
|
|
|
$newFile = fopen( $thumbnailFolder . "/newImages.txt", "a+" );
|
|
fwrite( $newFile, realpath( $source ) . "\n");
|
|
|
|
$file = file( $thumbnailFolder . "/newImages.txt" );
|
|
|
|
$file = array_slice( $file, 0, 20 );
|
|
|
|
file_put_contents( $thumbnailFolder . "/newImages.txt", implode( "", $file ) );
|
|
}
|
|
} |