<?
// Mp3CatgBuilder v1.2b
// More Infos : http://www.zrenard.com/mp3catgbuilder/
// 02-mars-2003 10:57 PM - patch for use_link_songs and use_id3 illogic
// 03-mars-2003 02:09 AM - Update for id3v2 and ogg

// Manage Compute Time of the script
function getmicrotime() { 
 list($usec, $sec) = explode(" ",microtime()); 
 return ((float)$usec + (float)$sec); 
} 
$time_start = getmicrotime();
//////////////////////////////////////
// Modify parameter to find your paradise
//////////////////////////////////////

//////////////////////////////////////
// Images Parameters
$use_picture=1; // Show picture (1) or not (0) (SLOW DISPLAY 'cause don't resize pictures)
$images_col=1; // Images links in columns (1) otherwise in Lines (0).
$use_link_images_only=1; // Link to images and don't display them (Speed up browsing). Used only if $use_picture=0
$use_main_picture=1; // Show images on the main directory. Used only if $use_picture=1
$use_link_images=1; // Link to image and display them. Used only if $use_picture=1
$max_images=3; // i.e : Maxium of 3 images (Front, Back, CD) order by name
$image_ext=array(".jpg",".gif",".png"); //Use this images Extensions (3 car ext name)
//////////////////////////////////////

//////////////////////////////////////
// Songs Parameters
$use_link_songs=1; // Direct link to song, probably used on home server.
$use_numbered=0; // Numbered lists for track.
$audio_ext=array(".mp3",".ogg"); //Use this audio Extensions (3 car ext name)
//////////////////////////////////////

//////////////////////////////////////
// ID3 Tags Parameters
$use_id3=1; // Used only if $use_link_songs=1 (SLOW TO COMPUTE and Bigger result file (All ID3tag in html))
$use_id3card=1; // Used only if $use_id3=1
//////////////////////////////////////

//////////////////////////////////////
// PlayList Parameters
// - Don't use playlist if you compute php on a command line (ie : not on a server)
$use_pls=1; // Generate link for pls (Winamp PlayList)
$use_mainpls=1; // Generate link for pls (Winamp PlayList) on main dir
//////////////////////////////////////

//////////////////////////////////////
// Lyrics Parameters
$use_lyric=1; // Retrieve lyrics from directory
$lyric_col=1; // Lyrics links in columns otherwise in Lines.
$lyric_ext=array(".htm",".txt",".html"); // 3 ou 4 car ext name allowed
$use_lyric_target="lyrics"; // "" = None, windows name otherwise
//////////////////////////////////////

//////////////////////////////////////
// General & Display Parameters
$flatlist=1; // No table in table
$show_main_title=1; // Show Directory in title mode
$show_compute_time=1; // Show compute time (and stats) at the bottom
$compute_file_size=1; // Compute and Show filesize on stats (used only if $show_compute_time=1) 
$size_value="Go"; // FileSize in Parameters (Ko, Mo or Go) if not Go is used
// Directory to process
if (!isset($dir_to_process)) $dir_to_process=".";
//////////////////////////////////////

// EOParameters

// Computed Values for Statistique
$nb_computed_audio_files=0;
$size_computed_audio_files=0;
$nb_computed_lyric_files=0;
$nb_computed_picture_files=0;

// On fly PlayList Generation
if (isset($action)) {
 if ($action=="pls") {
     header ("Content-Type: audio/mpegurl");
  echo "[playlist]\n";
    $nbf=1;
    $dir=base64_decode($dir); 
    echo $dir;
    global $audio_ext;
  $curdir=opendir($dir);
  while($curfile=readdir($curdir)){
     $retVal[count($retVal)] = $curfile;
     if(!in_array($curfile,array('.', '..'))){
    if(is_dir($dir."/".$curfile)){
       // No Dir
    }else{
         if (in_array(substr(strtolower($curfile),(strlen($curfile)-4),4),$audio_ext)) {
          $mp3file[$nbf++]=$curfile;
         }
    }
   }
  }
    $request_uri=getenv("REQUEST_URI");
  $document_root =  getenv( "DOCUMENT_ROOT");
  $server_name = getenv( "SERVER_ADDR"); // addr 'cause i use http://<my_local_ip>/ or http://<my_home_domain_on_the_internet>/   
  $server_port = getenv( "SERVER_PORT");
  $dirname = ereg_replace( "\?.*",  "", $request_uri); // strip param
  $dirname = substr($dirname,0,strrpos($dirname,"/"));
  if ($server_port=="80") $server_port=""; else $server_port=":".$server_port;
    $dir = str_replace("%2F","/",rawurlencode($dir));    
    for ($i=1;$i<$nbf;$i++) {
     $curfile = str_replace("%2F","/",rawurlencode($mp3file[$i]));
   echo "File$i=http://$server_name$server_port$dirname$dir/$curfile\n";
    }
  echo "NumberOfEntries=".($nbf-1)."\n";
    exit;
 }
}
?>
<html>
<head>
<title>Mp3 Directory : <?=$dir_to_process?></title>
<style type="text/css">
<!--
:link { text-decoration: none; color : #000000 }
:visited { text-decoration: none;color : #000000}
.tracklink:hover {text-decoration: bold; color : #009200; border : 1px solid;}
.tabTitle { color : #000000; background-color: #EEDDCC; border : 1px solid; padding: 5px;}
.tabTitleMain { color : #000000; background-color: #F5F5F5; border : 1px solid; padding: 5px;}
.tabSongs { border-top : 1px solid; border-bottom : 1px solid; border-left : 1px solid; border-right : 1px solid; border-color: #164783; color : #000000; background-color : #D5DBE4; }
.tabSongsMain { color : #000000; background-color : #FAFAFA; border : 1px solid;}
.tabImages { text-align : center; border-top : 1px solid; border-bottom : 1px solid; border-left : 1px solid; border-right : 1px solid; border-color: #164783; color : #000000; background-color : #D5DBE4; }
.tabImagesMain { text-align : center; border-top : 1px solid; border-bottom : 1px solid; border-left : 1px solid; border-right : 1px solid; color : #000000; background-color : #FAFAFA;}
.tableM { border-collapse: collapse;}
.tableS { width:100%; border-collapse: collapse; border : 1px solid; border-color: #000000;}
.tableDir { margin-left: auto; margin-right: auto; border : 1px solid; background-color :#FFB8B4 ;color :#725EA2; border-color: #333333;  text-align : center; align: center; border-collapse: collapse; font-size: 30}
.id3cardaction { font-size : 9; border : 1px solid;  color :#AAAAAA ; border-color : #AAAAAA;}
.over_id3cardaction {font-size : 9; border : 1px solid;  color :#DC6C81; border-color : #DC6C81; cursor:hand;}
.plsaction {font-size : 9; border : 1px solid;  color :#AAAAAA ; border-color : #AAAAAA;}
.over_plsaction {font-size : 9; border : 1px solid;  color :#73D940; border-color : #73D940; cursor:hand;}
.id3card { border : 1px solid; border-color: #000000; width : 175; filter:Alpha(opacity=90, style=0);}
.id3cardTitle { text-align:center; font-size : 12; font-weight : bold; font-variant : small-caps; color : #000000; background-color: #EEDDCC; border-color: #000000; border : 1px solid;}
.id3cardContent { font-size : 12; border-top : 1px solid; border-bottom : 1px solid; border-left : 1px solid; border-right : 1px solid; border-color: #164783; color : #000000; background-color : #D5DBE4; }
.id3cardTagTable {border-collapse: collapse; border:1px solid; border-color:#AAAAAA;width:95%;}
.id3cardTagHeader {text-align:center; font-size : 11; font-weight : bold; font-variant : small-caps; color : #000000; background-color: #F9E8D7;}
.id3cardTagContentT {font-size : 11; color : #000000; background-color : #F0F6FF;  border-right :1px solid; border-color:#000000}
.id3cardTagContentD {font-size : 11; color : #000000; background-color : #F0F6FF;  border-right :1px solid; border-color:#000000}
.lyricLink { text-decoration: none; text-align:center; font-size : 10; color : #423423;}
.lyricLink:hover {text-align:center; font-size : 10;text-decoration: bold; color : #009200; border : 1px solid;}
.lyricLink:link { text-decoration: none; text-align:center; font-size : 10; color : #423423;}
.lyricLink:visited { text-decoration: none; text-align:center; font-size : 10; color : #423423;}
.imageLink { text-decoration: none; text-align:center; font-size : 10; color : #423423;}
.imageLink:hover {text-align:center; font-size : 10;text-decoration: none; color : #009200;}
.imageLink:link { text-decoration: none; text-align:center; font-size : 10; color : #423423;}
.imageLink:visited { text-decoration: none; text-align:center; font-size : 10; color : #423423;}
.computetime {text-align:right; font-size : 9; border : 1px solid;  color :#99AA99 ; border-color : #99AA99;}
#mytooltips {position:absolute; visibility:hidden; z-index:200;}
-->
</style>
</head>
<body>
<DIV id=mytooltips></DIV><br>
<SCRIPT TYPE="text/javascript">
<!--
// PopUp JavaScript To Show Id3card
Xoffset=20;
Yoffset=-100;
var old,skn,iex=(document.all),yyy=-1000;
var ns4=document.layers
var ns6=document.getElementById&&!document.all
var ie4=document.all
if (ns4)
 skn=document.mytooltips
else if (ns6)
 skn=document.getElementById("mytooltips").style
else if (ie4)
 skn=document.all.mytooltips.style
if(ns4)document.captureEvents(Event.MOUSEMOVE);
else{
 skn.visibility="visible"
 skn.display="none"
}
document.onmousemove=get_mouse;
function tooltips(caption,msg){
 var content="<TABLE summary=\""+caption+"\" class=id3card><tr><TD class=id3cardTitle>"+caption+"</TD></tr><tr><TD class=id3cardContent>"+msg+"</TD></tr></TABLE>";
 yyy=Yoffset;
 if(ns4){skn.document.write(content);skn.document.close();skn.visibility="visible"}
 if(ns6){document.getElementById("mytooltips").innerHTML=content;skn.display=''}
 if(ie4){document.all("mytooltips").innerHTML=content;skn.display=''}
}
function get_mouse(e){
 var x=(ns4||ns6)?e.pageX:event.x+document.body.scrollLeft;
 skn.left=x+Xoffset;
 var y=(ns4||ns6)?e.pageY:event.y+document.body.scrollTop;
 skn.top=y+yyy;
}
function kill(){
 yyy=-1000;
 if(ns4){skn.visibility="hidden";}
 else if (ns6||ie4)
  skn.display="none"
}
//-->
</SCRIPT>
<?
set_time_limit(0); // Script could take many minute to compute so .. infinite time (dirty !)
if ($use_id3==1) {
 include_once('mp3/getid3.php'); // Use getid3 to read id3 tag (http://getid3.sourceforge.net)
 include_once('mp3/getid3.functions.php'); // Function library
 if ($use_id3card==1) {
  function id3card($mp3info,$fs) {
   // Return mp3 info and ID3 Tag V2 if present, or V1 if not.
   $retval .= $fs." Ko (".$mp3info['fileformat'].")<br>";
   if (isset($mp3info['mpeg']['audio']['layer'])) $retval .= " Layer ".$mp3info['mpeg']['audio']['layer']."<BR>";
   if (isset($mp3info['mpeg']['audio']['frequency'])) $retval .= $mp3info['mpeg']['audio']['frequency']." Hz<BR>";
   if (isset($mp3info['mpeg']['audio']['bitratemode'])) {
    if ($mp3info['mpeg']['audio']['bitratemode']=="VBR")
     $retval .= number_format($mp3info['mpeg']['audio']['VBR_bitrate'],2)." kbps (VRB)<BR>";
    else
     $retval .= $mp3info['mpeg']['audio']['bitrate']." kbps (CRB)<BR>";   
   }
   if (isset($mp3info['mpeg']['audio']['channelmode'])) $retval .= "Mode ".$mp3info['mpeg']['audio']['channelmode']."<BR>";
   if (isset($mp3info['lyrics3'])) $retval .= "Included Lyrics<BR>";
   if (isset($mp3info['comments'])) {
    $retval .= "<center><table class=id3cardTagTable>";
    if (($tmp=$mp3info['comments']['title']['0'])!="") $retval .= "<tr><td class=id3cardTagContentT>Title</td><td class=id3cardTagContentD>".$tmp."</td></tr>";
    if (($tmp=$mp3info['comments']['artist']['0'])!="") $retval .= "<tr><td class=id3cardTagContentT>Artist</td><td class=id3cardTagContentD>".$tmp."</td></tr>";
    if (($tmp=$mp3info['comments']['track']['0'])!="") $retval .= "<tr><td class=id3cardTagContentT>Track</td><td class=id3cardTagContentD>".$tmp."</td></tr>";
    if (($tmp=$mp3info['comments']['album']['0'])!="") $retval .= "<tr><td class=id3cardTagContentT>Album</td><td class=id3cardTagContentD>".$tmp."</td></tr>";
    if (($tmp=$mp3info['comments']['genre']['0'])!="") $retval .= "<tr><td class=id3cardTagContentT>Genre</td><td class=id3cardTagContentD>".$tmp."</td></tr>";
    if (($tmp=$mp3info['comments']['year']['0'])!="") $retval .= "<tr><td class=id3cardTagContentT>Year</td><td class=id3cardTagContentD>".$tmp."</td></tr>";
      $retval .="</table></center>";
   } elseif (isset($mp3info['id3v1'])) {
    $retval .= "<center><table class=id3cardTagTable><tr><td class=id3cardTagHeader colspan=2>ID3 TAG v1</td></tr>";
    if (($tmp=$mp3info['id3v1']['title'])!="") $retval .= "<tr><td class=id3cardTagContentT>Title</td><td class=id3cardTagContentD>".$tmp."</td></tr>";
    if (($tmp=$mp3info['id3v1']['artist'])!="") $retval .= "<tr><td class=id3cardTagContentT>Artist</td><td class=id3cardTagContentD>".$tmp."</td></tr>";
    if (($tmp=$mp3info['id3v1']['track'])!="") $retval .= "<tr><td class=id3cardTagContentT>Track</td><td class=id3cardTagContentD>".$tmp."</td></tr>";
    if (($tmp=$mp3info['id3v1']['album'])!="") $retval .= "<tr><td class=id3cardTagContentT>Album</td><td class=id3cardTagContentD>".$tmp."</td></tr>";
    if (($tmp=$mp3info['id3v1']['genre'])!="") $retval .= "<tr><td class=id3cardTagContentT>Genre</td><td class=id3cardTagContentD>".$tmp."</td></tr>";
    if (($tmp=$mp3info['id3v1']['year'])!="") $retval .= "<tr><td class=id3cardTagContentT>Year</td><td class=id3cardTagContentD>".$tmp."</td></tr>";
      $retval .="</table></center>";
   }
   return $retval;
  }
 } // EOFunction id3card
}
function scandir($dir,$maindir){
 //////////////////////////////////////
 // Scan a directory recursivly to extract mp3,cover, and part/lyrics etc ..
 //////////////////////////////////////
 // Global Parameters
 global $use_lyric,$use_picture,$use_main_picture,$use_link_songs,$use_link_images,$max_images,$use_id3,$use_id3card,$use_pls,$use_numbered,$use_mainpls,$flatlist,$audio_ext,$image_ext,$lyric_ext,$lyric_col,$use_lyric_target,$use_link_images_only,$images_col,$compute_file_size;
 // "Return" values for stats
 global $nb_computed_picture_files,$nb_computed_audio_files,$size_computed_audio_files,$nb_computed_lyric_files;
 $nbf=0;$nbi=0;$nbl=0;
 $dossier=opendir($dir);

 if ($flatlist==0)
  if ($dir!=$maindir) {
   echo "<table class=tableS valign=top summary=\"$dir\" border=1>\n<TR><td class=tabTitle colspan=2>".$dir;
     if ($use_pls==1) echo "&nbsp;-&nbsp;<a href=\"mp3catgbuilder.php?action=pls&dir=".base64_encode($dir)."\" class=plsaction onmouseover=\"this.className='over_plsaction';\"; onmouseout=\"this.className='plsaction';\"; >&nbsp;pls&nbsp;</a>";
     echo "</td></tr><tr><td class=tabSongs><br>";
  } else
   echo "<table class=tableM cellpading=0 cellspacing=0 width=100% valign=top summary=\"$dir\" border=0>\n<tr><td class=tabSongsMain>";
 while($fichier=readdir($dossier)){
    $retVal[count($retVal)] = $fichier;
  if(!in_array($fichier,array('.', '..'))){
   if(is_dir($dir."/".$fichier)){
      scandir($dir."/".$fichier,$maindir);
   } else {
      // Directory First ... Files folow
      if (in_array(substr(strtolower($fichier),(strlen($fichier)-4),4),$audio_ext)) {
         $onlyfile[$nbf++]=$fichier;
        }
    if ((($use_picture==1)||($use_link_images_only==1))&&(in_array(substr(strtolower($fichier),(strlen($fichier)-4),4),$image_ext))) {
         $onlyfileimg[$nbi++]=$fichier;
        }
    if (($use_lyric==1)&&((in_array(substr(strtolower($fichier),(strlen($fichier)-4),4),$lyric_ext))||(in_array(substr(strtolower($fichier),(strlen($fichier)-5),5),$lyric_ext)))) {
         $onlyfilelyr[$nbl++]=$fichier;
        }
   }
  }
 }
 $nb_computed_picture_files+=$nbi;
 $nb_computed_audio_files+=$nbf;
 $nb_computed_lyric_files+=$nbl;
 if ($nbf!=0) {
    if ($flatlist==1)
      if ($dir!=$maindir) {
    echo "<table class=tableS cellpading=0 cellspacing=0 width=100% valign=top summary=\"$dir\" border=1>\n<TR><td class=tabTitle colspan=2>".$dir;
      if ($use_pls==1) echo "&nbsp;-&nbsp;<a href=\"".$_SERVER['PHP_SELF']."?action=pls&dir=".base64_encode($dir)."\" class=plsaction onmouseover=\"this.className='over_plsaction';\"; onmouseout=\"this.className='plsaction';\"; >&nbsp;pls&nbsp;</a>";
        echo "</td></tr><tr><td class=tabSongs><br>";
   } else {
    echo "<table class=tableM cellpading=0 cellspacing=0 width=100% valign=top summary=\"$dir\" border=0>\n<TR><td class=tabTitleMain colspan=2>".$dir;
    if ($use_mainpls==1) echo "&nbsp;-&nbsp;<a href=\"".$_SERVER['PHP_SELF']."?action=pls&dir=".base64_encode($dir)."\" class=plsaction onmouseover=\"this.className='over_plsaction';\"; onmouseout=\"this.className='plsaction';\"; >&nbsp;pls&nbsp;</a>";
        echo "<tr><td class=tabSongsMain><br>";
     }
    sort($onlyfile);
    if ($use_numbered==1) echo "<OL>"; else echo "<UL>";

  // Tracks Display
    for ($i=0;$i<$nbf;$i++) {
     $fichier=$onlyfile[$i];
     $filesize=0;
     $dispfichier=substr(ucfirst($fichier),0,(strlen($fichier)-4));
   $curfichier = str_replace("%2F","/",rawurlencode($fichier));
   $curdir = str_replace("%2F","/",rawurlencode($dir));

     echo "<LI>";

     if ($use_link_songs==1)
          echo "<a class=tracklink href=\"$curdir/$curfichier\">$dispfichier</a>";
         else
        echo "$dispfichier";

     if ($use_id3==1) {
       $mp3info = GetAllFileInfo($dir."/".$fichier);
      $filesize=round($mp3info['filesize']/1024);
        $filesize=round(filesize($dir."/".$fichier)/1024);  
          echo "&nbsp;-&nbsp;";
          echo $mp3info['playtime_string'];
      if ($use_id3card==1) {
       echo "&nbsp;-&nbsp;<font class=id3cardaction onmouseover=\"this.className='over_id3cardaction';tooltips('".str_replace("'","\'",$dispfichier)."','";
           echo str_replace("\"","",str_replace("'","\'",id3card($mp3info,$filesize)));
       echo "');\"; onmouseout=\"this.className='id3cardaction';kill();\"; >&nbsp;id3&nbsp;</font>";
      }
         }

     echo "</LI>";


     if ($compute_file_size==1) {
        if ($filesize==0) $filesize=round(filesize($dir."/".$fichier)/1024);
       $size_computed_audio_files+=$filesize;
     }
  }
    if ($use_numbered==1) echo "</OL>"; else echo "</UL>";

  // Pictures Display
    if (((($use_picture==1)&&($nbi!=0))||
               (($use_link_images_only==1)&&($use_picture==0)&&($nbi!=0)))
             &&(!(($dir==$maindir)&&($use_main_picture==0)))) {
     echo "</td><td width=50% class=".(($dir==$maindir) ? "tabImagesMain" : "tabImages").">";
      for ($i=0;$i<$nbi&&$i<$max_images;$i++) {
      $fichier=$onlyfileimg[$i];
      $dispfichier=substr(ucwords($fichier),0,(strlen($fichier)-4));
      $curfichier = str_replace("%2F","/",rawurlencode($fichier));
         $curdir = str_replace("%2F","/",rawurlencode($dir));
    if ($use_picture==1)
         if ($use_link_images==1)
      echo "<a class=imageLink href=\"$curdir/$curfichier\"><img border=0 alt=\"$dispfichier\" width=160 src=\"./".$dir."/".$fichier."\"></A>\n";
         else
          echo "<img border=0 alt=\"$dispfichier\" width=160 src=\"./".$dir."/".$fichier."\">\n";
        else 
     echo "<a class=imageLink href=\"$curdir/$curfichier\">$dispfichier</A>\n";
        if ($images_col==0) echo "<br>";
     }
  }

  // Lyrics Display
  if (($use_lyric==1)&&($nbl!=0)&&(!($dir==$maindir))) {
   echo "<hr width=25%><center>";
      for ($i=0;$i<$nbl;$i++) {
      $fichier=$onlyfilelyr[$i];
        $dispfichier=substr(ucwords($fichier),0,(strlen($fichier)-4));
      $curfichier = str_replace("%2F","/",rawurlencode($fichier));
         $curdir = str_replace("%2F","/",rawurlencode($dir));
    echo "<a class=LyricLink href=\"$curdir/$curfichier\"";
        if ($use_lyric_target!="") echo " target=\"$use_lyric_target\"";
        echo ">$dispfichier</A>\n";
        if ((($i+1)!=$nbl)&&($lyric_col==1)) echo "&nbsp;-&nbsp;"; else echo "<br>";
     }
     echo "</center>";
  }
  echo "</td>";
    if ($flatlist==1)
     echo "</tr></table><br>\n";
 }
 if ($flatlist==0)
  echo "</tr></table><br>\n";
} // EOFunction scandir()

// Main
if ($show_main_title==1)
 echo "<center><table class=tableDir width=70%><tr><td>$dir_to_process</td></tr></table></center><br>";
scandir($dir_to_process,$dir_to_process);
if ($show_compute_time==1) {
 $time_end = getmicrotime();
 echo "<br><div class=computetime>";
 echo $nb_computed_audio_files." Audio files ";
 if ($compute_file_size==1)
  if ($size_value=="Ko")
   echo " (".number_format($size_computed_audio_files,2)." Ko) ";
    elseif ($size_value=="Mo")
     echo " (".number_format(($size_computed_audio_files/1024),2)." Mo) ";
 else
    echo " (".number_format((($size_computed_audio_files/1024)/1024),2)." Go) ";
 if ($use_picture==1)
  echo " - ".$nb_computed_picture_files." Pictures files ";
 if ($use_lyric==1)
  echo " - ".$nb_computed_lyric_files." Lyrics files ";
 echo " computed in ".number_format(($time_end - $time_start),2)." secondes</div>";
}
?>
</body>
</html>