<?php
include('../global.php');
// Omezení na online/offline uµivatele

$DbResult = $Database->query('SELECT id FROM hosts WHERE online=1 AND block=0 AND user>0');
$Vyber = '';
$Podminka = '';
while($Row = $DbResult->fetch_array()) 
  $Vyber .= $Row['id'].',';
$Podminka .= ' AND (host IN ('.substr($Vyber,0,-1).'))';
//echo($Podminka.'<br>');

$Database->select_db('share');

// Maximální vnoření
$MaxNesting = 20;

// Najde cestu ke keřenu
function PlnaCesta($Row)
{
  global $MaxNesting, $Database;
  
  // Vyhledání cesty
  $Otec = $Row['parent'];
  $Cesta = ''; //$Row['name'];
  $i = 0;
  while(($Otec>1)&&($i<$MaxNesting))
  {
    $DbResult = $Database->query("SELECT id,name,parent FROM items WHERE id=$Otec");
    $Row = $DbResult->fetch_array();
    $Cesta = $Row['name'].'\\'.$Cesta;
    $Otec = $Row['parent'];
    $i++;
  }
  if($i >= $MaxNesting) $Cesta = '?'.'\\'.$Cesta;
  return('\\\\'.$Cesta);
}

function mime_content_type($FileName)
{
  //$FileInfo = new finfo(FILEINFO_MIME);
  //return($FileInfo->file($FileName));
  return('');
}

$Name = 'playlist.m3u';
Header('Content-type: audio/x-mpegurl');
Header('Content-Disposition: attachment; filename='.$Name);
echo("#EXTM3U\n");
$Parent = '0';
$Dir = '';
$DbResult = $Database->select('items', '*', 'ext="mp3"'.$Podminka); //.' LIMIT 0,1000');
while($Row = $DbResult->fetch_array())
{
  if($Parent != $Row['parent']) $Dir = PlnaCesta($Row); //echo('d'.PlnaCesta($Row)."\n");
  $Parent = $Row['parent'];
  echo($Dir.$Row['name'].'.'.$Row['ext']."\n");
}

?>
