<?php
##
## this file name 'func.stream.php'
##

function check_useragent()
{
  global 
$_SERVER;

  
$winplayer 'RMA|Zinf|XMPlay|FreeAMP|RealMedia|Player';

  if(
preg_match('/xmms|Winamp/i',$_SERVER[HTTP_USER_AGENT]))
  { return 
2; }

  else if(
preg_match("/$winplayer/i",$_SERVER[HTTP_USER_AGENT]))
  { return 
1; }
}

## check for vorbis OGG file
##
function get_ftype($file)
{
  
$ftype[ogg][subtype] = 'application/x-ogg';
  
$ftype[ogg][ext] = 'ogg'// good idea
  
$ftype[mp3][subtype] = 'audio/mpeg';
  
$ftype[mp3][ext] = 'mp3';
  
$ftype[mp3][lame] = 1;

  
$tail get_ftail($file);

  return 
$ftype[$tail];
}

## for xmms/winamp useragent parsing
##
function agent_encode($str)
{
  
$str base64_encode($str);
  return 
str_replace(array('+','/'),array('_PLUS_','_DIR_'),$str);
}

function 
agent_decode($str)
{
  
$str str_replace(array('_PLUS_','_DIR_'),array('+','/'),$str);
  return 
base64_decode($str);
}

function 
get_mp3lists($path)
{
  if(
$hdle = @opendir($path))
  {
    while(
$file readdir($hdle))
    {
        if(
is_file("$path/$file") && is_readable("$path/$file"))
        {
            if(
preg_match('/\.(mp3|ogg)$/i',$file))
            { 
$mp3s[] = $file; }
            else if(
preg_match('/.(jpg|gif|jpeg|png)$/i',$file))
            { 
$imgs[] = $file; }
            else if(
preg_match('/.(txt|text)$/i',$file))
            { 
$txts[] = $file; }
        }
        else if(
is_dir("$path/$file"))
        { 
$dirs[] = $file; }

        
clearstatcache(); // flush buffer
    
}
    
closedir($hdle);

    
## patch 2003.07.18
    ##
    
if($dirssort($dirs);
    if(
$mp3ssort($mp3s);
    if(
$imgssort($imgs);
    if(
$txtssort($txts);
  }

  return array(
$dirs,$mp3s,$imgs,$txts);
}

function 
get_currlink($curr)
{
  
$subs explode('/',preg_replace(';/$;','',$curr));

  for(
$i=0$i<sizeof($subs); $i++)
  {
    
$path .= ($i>'/' '').$subs[$i];
    
$currlink['link'][] = $_SERVER[PHP_SELF].'?path='.agent_encode($path);
    
$currlink['str'][] = $subs[$i];
  }

  return 
$currlink;
}

## for real parsing
##
function get_paths($base$rpath)
{
  
$_path['rpath'] = trim($rpath); // orignal request $_GET['path'] path

  
if($_path['curr'] = agent_decode($_path['rpath']))
  {
    
$_path['subs'] = $_path['curr'].'/';
    
$_path['curr'] = '/'.$_path['curr'];
    
$_path['currlink'] = get_currlink($_path['subs']);
  }

  
$_path['real'] = str_replace('//','/',$base.$_path['curr']); // array

  
return $_path// array
}


## rewrite mp3 and get infomation
##
## $mp3 : object, new id3();
## $ogg : object, new ogg();
##
function get_cookie_userlist($_pstream$TMPL$mp3$ogg)
{
  global 
$_COOKIE;

  if(!
$_userlist $_COOKIE[_userlist]) return;

  
## patch 2003.06.18,
  ## pathc 2003.05.22
  ##
  
$_userlist str2array($_userlist); // '_lib/func.parse.php' file
  
$_userlist array_values($_userlist); // re-sort by numberic

  
$end sizeof($_userlist);
  for(
$i=0$i<$end$i++)
  {
    
$enc $_userlist[$i];
    
$dec agent_decode($_userlist[$i]);

    
$files[idx][] = $i;
    
//$files[enc][] = $enc;

    
$tail get_ftail($dec);
    
$id3 = ${$tail};
    
$id3->gettag($_pstream['mp3dir'].'/'.$dec);

    
$link $TMPL['play'].'?mp3list[]='.$enc;

    if(
$_pstream['lame'] && $tail == 'mp3')
    { 
$files[lame][] = $link.'&uselame=1'; }
    else
    {
        
$files[nolame][$i] = 1;
        
$files[lame][] = $link;
    }

    
$files[tail][] = $tail;
    
$files[link][] = $link;
    
$files[dellist][] = $TMPL['act_userlist'].'?act=del&dellist[]='.$i;
    
$files[download][] = $TMPL['act_download'].$enc.'/'.
        
preg_replace('/[\s_-]+/','_',$dec);

    
$files[str][] = $dec;
    
$files[bitrate][] = $id3->bitrate;
    
$files[mode][] = $id3->mode;
    
$files[frequency][] = $id3->frequency;
    
$files[length][] = $id3->length;
    
$files[filesize][] = $id3->filesize;
  }

  return 
$files;
}

require_once 
'_lib/func.file.php'// get_ftail()
require_once '_lib/func.parse.php'// array2str(), str2array()

?>