'http://friend.lan/music'
);
// max age to be new, in seconds.
$newage = 30*24*60*60;
// filters to pick out the zik and pictures
$zikfilter = '/\.([Mm][Pp]3|[oO][gG][gG]|[wW][aA][vV])$/';
$picfilter = '/\.([j][pP][eE]?[gG]|[pP][nN][gG]|[gG][iI][fF])$/';
$cssfilter = '/\.([cC][sS][sS]$/';
// filter for the remote host listing
$httplistfilter = '/(.*?)(\/?)<\/a>/';
function var_get($n,$d) {
if(array_key_exists($n,$_GET)) return $_GET[$n];
else return $d;
}
function clean_path($path) {
// kill starting /
// /../
// ./
// starting .
// trailling /
return preg_replace(array('/^\//', '/\/\.\./', '/\.\//','/^\.+/','/\/$/'),'',$path);
}
function make_title($name) {
return preg_replace('/\.[a-zA-Z0-9]*$/','',$name);
}
function encodepath($p) {
$l = explode('/',$p);
$e = array();
foreach($l as $i)
array_push($e,rawurlencode($i));
return implode('/',$e);
}
function print_pls($list,$shuffle) {
global $baseurl;
if($shuffle) shuffle($list);
echo "[playlist]\n";
echo 'numberofentries=' . count($list) . "\n";
$n = 1;
foreach($list as $f) {
echo 'File' . $n . '=' . $baseurl . '/' . encodepath($f) . "\n";
echo 'Title' . $n . '=' . make_title(basename($f)) . "\n";
echo 'Length' . $n . "=-1\n";
$n += 1;
}
echo "Version=2\n\n";
}
function make_file_list($path,$matches,$recurse) {
global $basepath;
if(!is_dir($basepath . '/' . $path)) return array($path);
$dd = opendir($basepath . '/' . $path);
if($dd == false) {
return 0;
}
if($recurse) $dir = array();
$zik = array();
while(($file = readdir($dd)) == true) {
if($file[0] == ".") continue;
if(is_dir($basepath . '/' . $path . '/' . $file)) {
if($recurse) array_push($dir, $path . '/' . $file);
continue;
}
foreach($matches as $m) {
if(preg_match($m,$file)) {
array_push($zik, $path . '/' . $file);
break;
}
}
}
asort($zik);
if($recurse) {
asort($dir);
foreach($dir as $d)
$zik = array_merge($zik,make_file_list($d,$matches,1));
}
return $zik;
}
function make_lists($path) {
global $basepath,$zikfilter,$picfilter;
$dd = opendir($basepath . '/' . $path);
if($dd == false) return 0;
$dir = array();
$pic = array();
$zik = array();
while(($file = readdir($dd)) == true) {
if($file[0] == ".") continue;
if(is_dir($basepath . '/' . $path . '/' . $file))
array_push($dir,$file);
elseif(preg_match($zikfilter,$file))
array_push($zik,$file);
elseif(preg_match($picfilter,$file))
array_push($pic,$file);
}
closedir($dd);
return array('dir' => $dir, 'pic' => $pic, 'zik' => $zik);
}
function make_http_list($url) {
global $zikfilter,$picfilter,$httplistfilter;
$lines = file($url);
$dir = array();
$pic = array();
$zik = array();
# print "Got from" . utf8_encode(htmlentities($url)) . ":
\n" . utf8_encode(htmlentities(implode("",$lines))) . "\n\n";
foreach($lines as $l) {
if(!preg_match($httplistfilter,$l,$matches)) continue;
$f = rawurldecode($matches[1]);
if($matches[3] != '')
array_push($dir,$matches[2]);
else if(preg_match($zikfilter,$f))
array_push($zik,$matches[1]);
elseif(preg_match($picfilter,$f))
array_push($pic,$f);
}
return array('dir' => $dir, 'pic' => $pic, 'zik' => $zik);
}
function make_http_file_list($url,$path,$matches,$recurse) {
global $httplistfilter;
foreach($matches as $m) {
if(preg_match($m,$path))
return array($path);
}
// Otherwise estimate it's a dir listing
$lines = file($url . '/' . encodepath($path) . '/');
if($recurse) $dir = array();
$zik = array();
foreach($lines as $l) {
if(!preg_match($httplistfilter,$l,$res)) continue;
$f = rawurldecode($res[1]);
if($res[3] != '') {
if($recurse) array_push($dir, $path . '/' . $f);
continue;
}
foreach($matches as $m) {
if(preg_match($m,$f)) {
array_push($zik, $path . '/' . $f);
break;
}
}
}
asort($zik);
if($recurse) {
asort($dir);
foreach($dir as $d)
$zik = array_merge($zik,make_http_file_list($url,$d,$matches,1));
}
return $zik;
}
function make_links($path,$href,$back,$css) {
global $title;
if($path == "") {
echo $title;
return;
}
if($href) {
$p = explode('/',$path);
$c = count($p);
$cp = ''; $cb = '';
if($back != '') {
$b = explode('/',$back);
if(count($b) != $c) $b = 0;
} else $b = 0;
echo '' . $title . ': ';
for($i = 0 ; $i < $c-1 ; $i += 1) {
if($cp == '') $cp = $p[0];
else $cp = $cp . '/' . $p[$i];
if($cb != '') $opt = '&back=' . $cb
. '&css=' . $css
. '#' . $b[$i+1];
echo ''
. utf8_encode(htmlentities($p[$i])) . ' / ';
if($cb != '') $cb = $cb . '/' . $b[$i+1];
}
echo utf8_encode(htmlentities($p[$i]));
} else
echo $title . ': ' . utf8_encode(htmlentities($path));
}
function make_url($cmd,$base,$path,$opt) {
global $scripturl,$host;
if($base != "") $p = $base . '/' . $path;
else $p = $path;
if($host != '')
return $scripturl . '?host=' . $host
. '&' . $cmd . '='. encodepath($p) . $opt;
return $scripturl . '?' . $cmd . '='. encodepath($p) . $opt;
}
function make_data_url($base,$path) {
global $baseurl;
if($base != "") $p = $base . '/' . $path;
else $p = $path;
return $baseurl . '/' . encodepath($p);
}
function get_dir_age($dir) {
$dd = opendir($dir);
if($dd == false) {
echo "Failed to open directory $path\n";
return 0;
}
$ages = array();
while(($file = readdir($dd)) == true) {
if($file[0] == ".") continue;
if(is_dir($dir . '/' . $file)) {
$a = get_dir_age($dir . '/' . $file);
if($a > 0) array_push($ages,$a);
} else
array_push($ages,filemtime($dir . '/' . $file));
}
if(!count($ages)) return 0;
sort($ages);
return $ages[0];
}
function make_new($base,$path) {
global $basepath,$newage;
if($base != "") $p = $base . '/' . $path;
else $p = $path;
$p = $basepath . '/' . $p;
if(is_dir($p)) {
$a = get_dir_age($p);
if($a == 0 || time() - $a > $newage) return '';
return ' new since ' . date("F d",$a) . "\n";
}
if(!file_exists($p)) return '';
$mtime = filemtime($p);
$d = time() - $mtime;
if($d > $newage) return '';
return ' new since ' . date("F d",$mtime) . "\n";
}
function print_pics($path,$list) {
if(count($list) == 0) return;
asort($list);
reset($list);
?>