<?php
// return: boolean | compares two strings, returns true only if $prefix appears at the beginning of $word
function isPrefix( $word, $prefix)
{
if ( strlen($word) < strlen($prefix))
{ return false; }
$word = substr($word, 0, strlen($prefix));
if ($prefix == $word)
{ return true; }
return false;
}
// return: string | takes two strings, if $prefix is a prefix of $word, then returns $word without prefix, else returns $word
function stripPrefix($word, $prefix)
{
if (isPrefix($word, $prefix))
{ return substr($word, strlen($prefix)); }
return $word;
}
// return: string | when given a full filename (a.b)returns the extention (b)
function fileExtension($file)
{ return strtolower(substr(strrchr($file, '.'), 1)); }
// return: boolean | takes a directory, returns true if it contains subdirectories
function hasSubFolders($dir)
{
// Open a known directory, and proceed to read its contents
if (is_dir($dir))
{
if ($dh = opendir($dir))
{
while (($file = readdir($dh)) !== false)
{
if(((filetype($dir .'/'. $file) == "dir") || (filetype($dir .'/'. $file) == "link")) && ($file !== ".") && ($file !== ".."))
{
return (true);
}
}
closedir($dh);
}
}
return(false);
}
//return: array of two arrays of strings | when given a root address and a subfolder path (folder/subfolder/ect), returns an array of names and array of links to each parent directory
function getBreadcrumbs ($dir)
{
$directories = explode("/", $dir);
$paths = array();
for($i = 0; $i<count($directories);$i++)
{
$tempPath = "";
for( $j=1; $j<=$i;$j++)
{
$tempPath .= "/" . $directories[$j];
}
$paths[] = $tempPath;
}
$crumbs = array($directories, $paths);
return $crumbs;
}
// return: string | when given a directory path, returns the path to the cover art it contains
function getCover($dir, $address)
{
$typesArray = array('jpg', 'jpeg');
$defaultFilename = 'cover.jpg';
$coverList = array();
// Open a known directory, and proceed to read its contents
if (is_dir($dir))
{
if ($dh = opendir($dir))
{
while (($file = readdir($dh)) !== false)
{
if(((filetype($dir .'/'. $file) == "dir") || (filetype($dir .'/'. $file) == "link")) && ($file !== ".") && ($file !== ".."))
{/*ignore folders and links*/}
else if (in_array(fileExtension($file), $typesArray))
{
if((strtolower($file) == "cover.jpg") || (strtolower($file) == "cover.jpeg"))
return ($address . stripPrefix(($dir .'/'. $file), dirname(__FILE__)));
$coverList[] = $address . stripPrefix(($dir .'/'. $file), dirname(__FILE__));
}
}
closedir($dh);
if(count($coverList) >0)
return($coverList[0]);
}
}
return(recursiveGetCover($dir, $address));
}
// return: string | when given a directory path, returns the path to the cover art it contains
function recursiveGetCover($dir, $address)
{
$typesArray = array('jpg', 'jpeg');
$defaultFilename = 'cover.jpg';
$coverList = array();
$subCoverList = array();
// Open a known directory, and proceed to read its contents
if (is_dir($dir))
{
if ($dh = opendir($dir))
{
while (($file = readdir($dh)) !== false)
{
if(((filetype($dir .'/'. $file) == "dir") || (filetype($dir .'/'. $file) == "link")) && ($file !== ".") && ($file !== ".."))
{
$temp = recursiveGetCover(($dir .'/'. $file), $address);
if($temp !== 0)
{
$subCoverList[] = $temp;
}
}
else if (in_array(fileExtension($file), $typesArray))
{
if((strtolower($file) == "cover.jpg") || (strtolower($file) == "cover.jpeg"))
return ($address . stripPrefix(($dir .'/'. $file), dirname(__FILE__)));
$coverList[] = $address . stripPrefix(($dir .'/'. $file), dirname(__FILE__));
}
}
closedir($dh);
if(count($coverList) >0)
return($coverList[0]);
else if (count($subCoverList) >0)
return($subCoverList[0]);
}
}
return(0);
}
// return: array => array => string, string, boolean, string | when given a directory path, returns an array of subfolder information
// return[0] - array of subfolder names
// return[1] - array of subfolder paths
// return[2] - array of booleans - true is subfolder has subfolders
// return[3] - array of paths to cover art (cover.jpg, first *.jpg in subfolder, default)
function listFolders($dir, $address)
{
$folderNameList = array();
$folderPathList = array();
$folderSubFolders = array();
$folderCover = array();
// Open a known directory, and proceed to read its contents
if (is_dir($dir))
{
if ($dh = opendir($dir))
{
$tempArray = array();
while (($file = readdir($dh)) !== false)
{
$tempArray[] = $file;
}
closedir($dh);
sort($tempArray);
foreach ($tempArray as $file)
{
if(((filetype($dir .'/'. $file) == "dir") || (filetype($dir .'/'. $file) == "link")) && ($file !== ".") && ($file !== ".."))
{
$folderNameList[] = $file;
$folderPathList[] = $dir .'/'. $file;
$folderSubFolders[] = hasSubFolders($dir .'/'. $file);
$folderCover[] = getCover($dir .'/'. $file, $address);
}
}
$folderList = array($folderNameList, $folderPathList, $folderSubFolders, $folderCover);
return ($folderList);
}
}
return(0);
}
// return: array => array => string, string, string | when given a directory path, returns an array of file information
// return[0] - array of file names
// return[1] - array of file paths
// return[2] - array of file extensions
function listFiles($dir, $types, $address)
{
$typesArray = explode('|', $types);
$fileNameList = array();
$filePathList = array();
$fileTypeList = array();
// Open a known directory, and proceed to read its contents
if (is_dir($dir))
{
if ($dh = opendir($dir))
{
$tempArray = array();
while (($file = readdir($dh)) !== false)
{
$tempArray[] = $file;
}
closedir($dh);
sort($tempArray);
foreach ($tempArray as $file)
{
if(((filetype($dir .'/'. $file) == "dir") || (filetype($dir .'/'. $file) == "link")) && ($file !== ".") && ($file !== ".."))
{/*ignore folders and links*/}
else if (in_array(fileExtension($file), $typesArray))
{
$fileNameList[] = $file;
$filePathList[] = urlencode($address . stripPrefix(($dir .'/'. $file), dirname(__FILE__)));
$fileTypeList[] = fileExtension($file);
}
}
$fileList = array($fileNameList, $filePathList, $fileTypeList);
return($fileList);
}
}
return(0);
}
// return: array => array => string, string, string | when given a directory path, returns an array of file information
// return[0] - array of file names
// return[1] - array of file paths
// return[2] - array of file extensions
function recursiveListFiles($dir, $types, $address)
{
$typesArray = explode('|', $types);
$fileNameList = array();
$filePathList = array();
$fileTypeList = array();
// Open a known directory, and proceed to read its contents
if (is_dir($dir))
{
if ($dh = opendir($dir))
{
$tempArray = array();
while (($file = readdir($dh)) !== false)
{
$tempArray[] = $file;
}
closedir($dh);
sort($tempArray);
foreach ($tempArray as $file)
{
if(((filetype($dir .'/'. $file) == "dir") || (filetype($dir .'/'. $file) == "link")) && ($file !== ".") && ($file !== ".."))
{
$temp = recursiveListFiles(($dir .'/'. $file), $types, $address);
if($temp !== 0)
{
$fileNameList = array_merge($fileNameList, $temp[0]);
$filePathList = array_merge($filePathList, $temp[1]);
$fileTypeList = array_merge($fileTypeList, $temp[2]);
}
}
else if (in_array(fileExtension($file), $typesArray))
{
$fileNameList[] = $file;
$filePathList[] = urlencode($address . stripPrefix(($dir .'/'. $file), dirname(__FILE__)));
$fileTypeList[] = fileExtension($file);
}
}
$fileList = array($fileNameList, $filePathList, $fileTypeList);
return($fileList);
}
}
return(0);
}
?>