ktk_mediaserve/modules/FileBrowser.php

169 lines
5.6 KiB
PHP

<?php
namespace ktk\MusicServe;
class FileBrowser {
const ROOT = "media/";
const T_DIR = 1;
const T_FILE = 2;
const F_TYPE = 0;
const F_NAME = 1;
const F_EXT = 2;
static function onInit() {
// set default options if need be
if (!MusicServe::getData('sort_by')) {
MusicServe::storeData('sort_order', SORT_ASC);
MusicServe::storeData('sort_by', "name");
}
if (!MusicServe::getData('cwd')) {
MusicServe::storeData('cwd', FileBrowser::ROOT);
self::getFiles();
self::sortFiles();
}
}
static function onRender() {
$cwd = MusicServe::getData('cwd');
echo ' <div id="ktk_MusicServe_FileBrowser">'.PHP_EOL;
echo ' <h1>',$cwd,'</h1>'.PHP_EOL;
if (MusicServe::checkModule("Playlist")) {
echo ' <form action="" method="POST">';
echo ' <input type="hidden" name="m" value="FileBrowser" />';
echo ' <input type="hidden" name="c" value="openFiles" />';
echo ' <input type="submit" name="" value="add all to playlist" />';
echo ' </form>';
}
echo ' <table>'.PHP_EOL;
echo ' <form action="" method="POST">';
echo ' <input type="hidden" name="m" value="FileBrowser" />';
echo ' <input type="hidden" name="c" value="changeSort" />';
echo ' <tr><th><input type="submit" name="p1" value="name" /></th><th><input type="submit" name="p1" value="ext" /></th></tr>'.PHP_EOL;
echo ' </form>'.PHP_EOL;
echo ' <form action="" method="POST">';
echo ' <input type="hidden" name="m" value="FileBrowser" />';
echo ' <input type="hidden" name="c" value="changeCwd" />';
echo ' <tr><td><input type="submit" name="p1" value="../"></td><td></td></tr>'.PHP_EOL;
foreach(MusicServe::getData('dirs') as $dir) {
echo ' <tr><td><input type="submit" name="p1" value="'.$dir[FileBrowser::F_NAME].'/"></td><td></td></tr>'.PHP_EOL;
}
echo ' </form>'.PHP_EOL;
echo ' <form action="" method="POST">';
echo ' <input type="hidden" name="m" value="FileBrowser" />';
echo ' <input type="hidden" name="c" value="openFile" />';
foreach(MusicServe::getData('files') as $file) {
echo ' <tr><td><input type="submit" name="p1" value="'.$file[FileBrowser::F_NAME].'"/></td><td>'.$file[FileBrowser::F_EXT].'</td></tr>'.PHP_EOL;
}
echo ' </form>'.PHP_EOL;
echo ' </table>'.PHP_EOL;
echo ' </div>'.PHP_EOL;
}
/* COMMAND FUNCTIONS */
static function changeCwd($directory) {
// NOTE: temporarily resolving any ".." to one dir up, rather than all
if (substr_count($directory, "..")) {
if (MusicServe::getData('cwd') == FileBrowser::ROOT) {
return NULL;
}
$directory = dirname(MusicServe::getData('cwd')).'/';
} else {
$directory = MusicServe::getData('cwd').$directory;
}
if (!file_exists($directory)) {
MusicServe::reportError(__CLASS__, __FUNCTION__,
"Cannot change working directory to \"'.$directory.'\", as it does not exist!\n"
);
return NULL;
}
MusicServe::storeData('cwd', $directory);
self::getFiles();
self::sortFiles();
}
static function changeSort($type) {
MusicServe::storeData('sort_by', $type);
if (($sort_order = MusicServe::getData('sort_order')) != SORT_ASC) {
MusicServe::storeData('sort_order', SORT_ASC);
} else {
MusicServe::storeData('sort_order', SORT_DESC);
}
self::sortFiles();
}
static function openFile($file) {
if (MusicServe::checkModule("Playlist")) {
Playlist::addFile(MusicServe::getData('cwd').$file);
}
}
static function openFiles() {
if (MusicServe::checkModule("Playlist")) {
foreach(MusicServe::getData('files') as $file) {
Playlist::addFile(MusicServe::getData('cwd').$file[FileBrowser::F_NAME]);
}
}
}
/* GENERAL FUNCTIONS */
static function getFiles() {
$directory = MusicServe::getData('cwd');
if (!file_exists($directory)) {
MusicServe::reportError(__CLASS__, __FUNCTION__,
"Directory \"'.$directory.'\"does not exist!\n"
);
return NULL;
}
$directory_handle = opendir($directory);
$i = 0;
while (false !== ($filename = readdir($directory_handle))) {
if ($filename[0] != '.' && $filename != "..") {
$files[$i] = array();
if (is_dir($directory.$filename)) {
$files[$i][FileBrowser::F_TYPE] = FileBrowser::T_DIR;
} else {
$files[$i][FileBrowser::F_TYPE] = FileBrowser::T_FILE;
}
$files[$i][FileBrowser::F_NAME] = $filename;
$files[$i][FileBrowser::F_EXT] = pathinfo($filename, PATHINFO_EXTENSION);
$i++;
}
}
MusicServe::storeData('dir_list', $files);
}
/* INTERNAL FUNCTIONS */
static function sortFiles() {
$sort = array();
$sort_by = MusicServe::getData('sort_by');
$sort_order = MusicServe::getData('sort_order');
$i = 0;
if ($sort_by == "ext") {
foreach (MusicServe::getData('dir_list') as $file) {
$sort[$i++] = $file[FileBrowser::F_EXT];
}
} else {
foreach (MusicServe::getData('dir_list') as $file) {
$sort[$i++] = $file[FileBrowser::F_NAME];
}
}
array_multisort($sort, $sort_order, MusicServe::getData('dir_list'));
// Wasteful, but we want directories always on top
$dirs = array();
$files = array();
foreach (MusicServe::getData('dir_list') as $file) {
if ($file[FileBrowser::F_TYPE] == FileBrowser::T_DIR) {
$dirs[] = $file;
} else {
$files[] = $file;
}
}
MusicServe::storeData('files', $files);
MusicServe::storeData('dirs', $dirs);
}
}
?>