<?php
function exceptions_error_handler($severity, $message, $filename, $lineno) {
switch($severity) {
case E_ERROR:
throw new ErrorException($message, 0, $severity, $filename, $lineno);
break;
case E_WARNING:
echo "<b>My WARNING</b> [$severity] $message<br />\n";
break;
case E_NOTICE:
echo "<b>My NOTICE</b> [$severity] $message<br />\n";
break;
default:
echo "Unknown error type: [$severity] $message<br />\n";
break;
}
}
// set_error_handler('exceptions_error_handler');
$dir = preg_replace('/\/[^\/]*\/?$/', '', preg_replace('/\\\/', '/', dirname($_SERVER["SCRIPT_FILENAME"]))).'/';
preg_match('/.*\//', $_SERVER['SCRIPT_NAME'], $matches);
$web_prefix = substr('http://'.$_SERVER['SERVER_NAME'].$matches[0], 0, -1);
include('getid3/getid3.php');
function scandir_linux($d) {
$arr = explode("\n", `find -L "$d" -type f -name *.mp3`);
array_pop($arr);
natcasesort($arr);
print_r($arr);
return $arr;
}
/**
* Scans recursively a linux folder and builds a js structure with the info. Respects symlinks, but check if your permissions are okay.
*
* @param string $path
* @return nothing
*/
function build_linux($dir) {
global $olist, $web_prefix, $id3;
$all_files = scandir_linux($dir);
foreach ($all_files as $mkey => $f) {
$path = dirname($f);
$file = basename($f);
$f=preg_replace('/\/+/','/',$f);
$content[] = $file;
if($path != dirname($all_files[$mkey+1])) {
$web_path = substr($path,strlen('/'.trim($dir, '/')));
$a = preg_split('/\//', $web_path);
$a[0] = 'root';
$title = $artist = $album = $track = $files = '';
$key = 0;
foreach ($content as $value) {
$files .= '"'.$key.'" : "'.$web_path.'/'.$value.'",';
$fileinfo = $id3->analyze($path.'/'.$value);
$temptitle = preg_replace('/"/', '\"', (array_key_exists('id3v2', $fileinfo) && array_key_exists('title', $fileinfo['id3v2']['comments']) ? trim(implode(' ', $fileinfo['id3v2']['comments']['title'])) : $fileinfo['id3v1']['title']));
if ($temptitle == '' || !isUTF8($temptitle))
$temptitle = mb_convert_encoding($value, 'UTF-8');
$artist .= '"'.$key.'" : "'.(array_key_exists('id3v2', $fileinfo) && array_key_exists('artist', $fileinfo['id3v2']['comments']) ? trim(implode(' ', $fileinfo['id3v2']['comments']['artist'])) : $fileinfo['id3v1']['artist']).'",';
$album .= '"'.$key.'" : "'.(array_key_exists('id3v2', $fileinfo) && array_key_exists('album', $fileinfo['id3v2']['comments']) ? trim(implode(' ', $fileinfo['id3v2']['comments']['album'])) : $fileinfo['id3v1']['album']).'",';
$track .= '"'.$key.'" : "'.(array_key_exists('id3v2', $fileinfo) && array_key_exists('track', $fileinfo['id3v2']['comments']) ? trim(implode(' ', $fileinfo['id3v2']['comments']['track'])) : array_key_exists('id3v1', $fileinfo) && array_key_exists('track', $fileinfo['id3v1']) ? $fileinfo['id3v1']['track'] : '').'",';
$title .= '"'.$key++.'" : "'.$temptitle.'",';
}
$files = substr($files, 0, -1);
$artist = substr($artist, 0, -1);
$album = substr($album, 0, -1);
$title = substr($title, 0, -1);
$track = substr($track, 0, -1);
$parent = 'root';
foreach(array_slice($a, 1, -1) as $value) {
$id = preg_replace('/[^\w\d]/', '', $value);
echo "if (!$('$id')) {
new Element('DIV', { 'id': '".$id."' }).appendText(\"$value\").injectInside($('".$parent."'));
}
$('$id').className = 'node';
$('$id').>
";
$parent = $id;
}
$name = array_pop($a);
$id = preg_replace('/[^\w\d]/', '', $name);
echo "if (!$('$id')) { var el = new Element('DIV', {
'id': '".$id."',
'class': 'leaf',
'events': {
'click': leafclick
}
}).appendText(\"$name\");
el.files = new Hash({ $files });
el.track = new Hash({ $track });
el.artist = new Hash({ $artist });
el.album = new Hash({ $album });
el.titles = new Hash({ $title });
el.injectInside($('".preg_replace('/[^\w\d]/', '', array_pop($a))."')) };
";
$content = array();
}
}
}
/**
* Scans recursive a windows folder and builds a js structure with the info
*
* @param string $path
* @return nothing
*/
function build_index($path) {
global $olist, $web_prefix, $id3, $dir;
if (!is_dir($path)) return 0;
$directory = @opendir($path);
while ($file= @readdir($directory)) {
if (($file<>".")&&($file<>"..")) {
$f=$path."/".$file;
$f=preg_replace('/\/+/','/',$f);
if(is_dir($f)) {
$content = preg_grep('/\.mp3$/i', scandir($f));
if (count($content) > 0) {
$web_path = substr($f,strlen(trim($dir, '/')));
$a = preg_split('/\//', $web_path);
$a[0] = 'root';
$title = $artist = $album = $track = $files = '';
$key = 0;
foreach ($content as $value) {
$files .= '"'.$key.'" : "'.$web_path.'/'.$value.'",';
$fileinfo = $id3->analyze($f.'/'.$value);
$temptitle = preg_replace('/"/', '\"', (array_key_exists('id3v2', $fileinfo) && array_key_exists('title', $fileinfo['id3v2']['comments']) ? trim(implode(' ', $fileinfo['id3v2']['comments']['title'])) : $fileinfo['id3v1']['title']));
if ($temptitle == '' || !isUTF8($temptitle))
$temptitle = $value;
$artist .= '"'.$key.'" : "'.(array_key_exists('id3v2', $fileinfo) && array_key_exists('artist', $fileinfo['id3v2']['comments']) ? trim(implode(' ', $fileinfo['id3v2']['comments']['artist'])) : $fileinfo['id3v1']['artist']).'",';
$album .= '"'.$key.'" : "'.(array_key_exists('id3v2', $fileinfo) && array_key_exists('album', $fileinfo['id3v2']['comments']) ? trim(implode(' ', $fileinfo['id3v2']['comments']['album'])) : $fileinfo['id3v1']['album']).'",';
$track .= '"'.$key.'" : "'.(array_key_exists('id3v2', $fileinfo) && array_key_exists('track', $fileinfo['id3v2']['comments']) ? trim(implode(' ', $fileinfo['id3v2']['comments']['track'])) : array_key_exists('id3v1', $fileinfo) && array_key_exists('track', $fileinfo['id3v1']) ? $fileinfo['id3v1']['track'] : '').'",';
$title .= '"'.$key++.'" : "'.$temptitle.'",';
}
$files = substr($files, 0, -1);
$artist = substr($artist, 0, -1);
$album = substr($album, 0, -1);
$title = substr($title, 0, -1);
$track = substr($track, 0, -1);
$parent = 'root';
foreach(array_slice($a, 1, -1) as $value) {
$id = preg_replace('/[^\w\d]/', '', $value);
echo "if (!$('$id')) {
new Element('DIV', { 'id': '".$id."' }).appendText(\"$value\").injectInside($('".$parent."'));
}
$('$id').className = 'node';
$('$id').>
";
$parent = $id;
}
$name = array_pop($a);
$id = preg_replace('/[^\w\d]/', '', $name);
echo "if (!$('$id')) { var el = new Element('DIV', {
'id': '".$id."',
'class': 'leaf',
'events': {
'click': leafclick
}
}).appendText(\"$name\");
el.files = new Hash({ $files });
el.track = new Hash({ $track });
el.artist = new Hash({ $artist });
el.album = new Hash({ $album });
el.titles = new Hash({ $title });
el.injectInside($('".preg_replace('/[^\w\d]/', '', array_pop($a))."')) };
";
}
build_index($f);
}
}
}
}
/**
* Scans recursive a windows folder and builds a js structure with the info
*
* @param string $path
* @return nothing
*/
function build_json_index($path) {
global $olist, $web_prefix, $id3, $dir, $structure, $files;
if (!is_dir($path)) return 0;
$directory = @opendir($path);
while ($file= @readdir($directory)) {
if (($file<>".")&&($file<>"..")) {
$f=$path."/".$file;
$f=preg_replace('/\/+/','/',$f);
if(is_dir($f)) {
$content = preg_grep('/\.mp3$/i', scandir($f));
if (count($content) > 0) {
$web_path = mb_substr($f,strlen(trim($dir, '/')));
$a = preg_split('/\//', $web_path);
$a[0] = 'root';
$title = $artist = $album = $track = $uids = array();
$key = 0;
foreach ($content as $value) {
$uids[$key] = md5(uniqid());
$files[$uids[$key]]= $web_path.'/'.$value;
$fileinfo = $id3->analyze($f.'/'.$value);
$encoding = (array_key_exists('id3v2', $fileinfo) && array_key_exists('TIT2', $fileinfo['id3v2']) && array_key_exists('encoding', $fileinfo['id3v2']['TIT2']) ? trim($fileinfo['id3v2']['TIT2']['encoding']) : 'ISO-8859-1');
$temptitle = preg_replace('/"/', '\"', (array_key_exists('tags', $fileinfo) && array_key_exists('id3v2', $fileinfo['tags']) && array_key_exists('title', $fileinfo['tags']['id3v2']) ? trim(implode(' ', $fileinfo['tags']['id3v2']['title'])) : $fileinfo['tags']['id3v1']['title']));
if ($temptitle == '' || !isUTF8($temptitle))
$temptitle = mb_convert_encoding($value, 'UTF-8', $encoding);
$artist[$key] = (array_key_exists('tags', $fileinfo) && array_key_exists('id3v2', $fileinfo['tags']) && array_key_exists('artist', $fileinfo['tags']['id3v2']) ? trim(implode(' ', $fileinfo['tags']['id3v2']['artist'])) : array_key_exists('tags', $fileinfo) && array_key_exists('id3v1', $fileinfo['tags']) && array_key_exists('artist', $fileinfo['tags']['id3v1']) ? $fileinfo['tags']['id3v1']['artist'] : '');
$album[$key] = (array_key_exists('tags', $fileinfo) && array_key_exists('id3v2', $fileinfo['tags']) && array_key_exists('album', $fileinfo['tags']['id3v2']) ? trim(implode(' ', $fileinfo['tags']['id3v2']['album'])) : array_key_exists('tags', $fileinfo) && array_key_exists('id3v1', $fileinfo['tags']) && array_key_exists('album', $fileinfo['tags']['id3v1']) ? $fileinfo['tags']['id3v1']['album'] : '');
$track[$key] = (array_key_exists('tags', $fileinfo) && array_key_exists('id3v2', $fileinfo['tags']) && array_key_exists('track', $fileinfo['tags']['id3v2']) ? trim(implode(' ', $fileinfo['tags']['id3v2']['track'])) : array_key_exists('tags', $fileinfo) && array_key_exists('id3v1', $fileinfo['tags']) && array_key_exists('track', $fileinfo['tags']['id3v1']) ? $fileinfo['tags']['id3v1']['track'] : '');
$title[$key++] = $temptitle;
}
$parent = &$structure['root']['content'];
foreach(array_slice($a, 1, -1) as $value) {
$id = preg_replace('/[^\w\d]/', '', $value);
$parent[$id]['id'] = $id;
$parent[$id]['name'] = $value;
$parent[$id]['cls'] = 'node';
$parent[$id]['events']['click'] = 'nodeclick';
$parent[$id]['parent'] = $parent['id'];
$parent = &$parent[$id]['content'];
}
$name = array_pop($a);
$id = preg_replace('/[^\w\d]/', '', $name);
$parent[$id]['id'] = $id;
$parent[$id]['name'] = $name;
$parent[$id]['cls'] = 'leaf';
$parent[$id]['events']['click'] = 'leafclick';
$parent[$id]['uids'] = $uids;
$parent[$id]['track'] = $track;
$parent[$id]['artist'] = $artist;
$parent[$id]['album'] = $album;
$parent[$id]['title'] = $title;
$parent[$id]['parent'] = $parent['id'];
}
build_json_index($f);
}
}
}
}
/**
* Detects if a string is UTF-8 encoded.
*
* @param string $str
* @return bool
*/
function isUTF8($str) {
if ($str === mb_convert_encoding(mb_convert_encoding($str, "UTF-32", "UTF-8"), "UTF-8", "UTF-32")) {
return true;
} else {
return false;
}
}
$id3 = new getID3();
$olist = $files = array();
error_reporting( 0 );
$structure['root'] = array(
'name' => 'root',
'cls' => 'node',
'styles' => array(
'height' => 'auto',
'background' => 'url(\'image/minus.gif\') left 2px no-repeat'
),
'events' => array(
'click' => 'nodeclick'
)
);
if (getenv('OSTYPE') != "linux")
build_json_index($dir);
else build_linux($dir);
error_reporting( E_ALL );
file_put_contents('collection.files.json', json_encode($files));
file_put_contents('collection.json', json_encode($structure));
?>