<?php
header ("Access-Control-Allow-Origin: *");
set_time_limit(5);
session_start();
include'oml.ini';
?>
<!DOCTYPE html>
<html>
<head>
<title>Open Media Library</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<LINK REL="stylesheet" TYPE="text/css" HREF="oml.css">
<style type="text/css">
td {font-size:0.9rem;margin:0;padding:0}
.b {font-size:8pt;border:solid 1px grey;background-color:#555;color:#7a7;}
</style>

<script type="text/javascript" src="binaryajax.js"></script>
<script type="text/javascript" src="id3.js"></script>
<script type="text/javascript">
var path="<?=$path?>",tags;

function playertext(s) {
  var x='';
  var path2='http://localhost/mp3/';
  document.getElementById("songinfo").firstChild.data=s;
  ID3.loadTags(path2+s, function() {
	  tags = ID3.getAllTags(path2+s);
	  for (var i = 0; i < Object.getOwnPropertyNames(tags).length; i++) {
	  	var tagname=Object.getOwnPropertyNames(tags)[i];
	    document.getElementById(tagname).firstChild.data=tags[tagname];}});}
	  
function loadSong(s) {
    var player=document.getElementById('player');
    player.src=path+s;
    player.play();}
    
function togglevis(id) {
  document.getElementById(id).style.display='block';}
    

function update(fname,oid,u) {
  var x=fname+oid;
  xmlhttp=new XMLHttpRequest();
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {document.getElementById("log").innerHTML=xmlhttp.responseText;}}
  xmlhttp.open("POST","omlajax.php",true);
  xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
  var s="oid="+oid+"&fname="+fname+"&value="+u;
  xmlhttp.send(s);}
  
function edit(oid) {
  window.open('edit.php?oid='+oid, 'edit', 'toolbar=no, directories=no, location=no, status=yes, menubar=no, resizable=yes, scrollbars=yes, width=600,height=400');}  

function mb(oid) {
 window.open('mbquery2.php?oid='+oid, 'edit', 'toolbar=no, directories=no, location=no, status=yes, menubar=no, resizable=yes, scrollbars=yes, width=900,height=700');}  

</script>

</head>
<body>
<h1>Open Media Library</h1>
<table style="text-align:left;margin-left:0px;width:100%;">
<tr>
<td>
<?php



function sortline($x) {
	global $base;
  $y=substr($x,0,5);
  print chr(10).'<th><a href="'.$base.'?start=&amp;asc=1&amp;sort='.$x.'">'.$y.'</a>';}
  
function xlink($x) {print"\n<td><a href=\"".$base."?page=$x\">$x</a>";}
  


$lines=20;
$id3fields=array('artist','title','album','year','comment','genre','track');
$rankfieldsa=array('quality','speed','annoy','party','popularity','radio','lyrics','style','la','year');
if (!isset($_SESSION['show'])) {
  foreach ($rankfieldsa as $x) {$_SESSION['show'][$x]=1;}}
if (isset($_POST['show'])) {foreach($_POST['show'] as $x=>$y) {$_SESSION['show'][$x]=$y;}}  
//var_dump($_SESSION);  
array_push($rankfieldsa,'yearmb','yearall');
$rankfieldsnum=array(99,9,9,9,0);
$ranktext=array('style','comment');
$datafieldsa=array('file','bitrate','size','path','ratetime','utime');
$sort='quality';$page=0;$start='99';


//id3
$c=0;
print"<table>";
foreach($id3fields as $x) {
  print"\n<tr><td class=\"id3h\">$x<td class=\"id3d\" id=\"$x\"> ";
  if ($c>1) {print"</table>\n<td><table>";$c=0;}
  $c++;}
print'
</table>';
?>

<td>
<audio id="player" controls style="width:100%">
<source src="<?=$path?>test.mp3" type="audio/mpeg"/>    
</audio>
<br><span id="songinfo" style="background-color:#000;color:#ffa;font-size:14pt;">Player</span>
</table>

<?php

//query
$q2='';
$res = pg_query($oml, "select $sort from oml limit 1");    
$gt=($asc?'>=':'<=');
$start0=$start;
if ($start=='NULL') {$q2="where $sort is null";} 
elseif (pg_field_type($res, 0)=='varchar' and $start>='0') {
if (!$asc) {$start0=$start.'ZZ';}
$q2="where upper($sort)$gt'$start0'";}
if (!$asc) {
if ($q2) {$q2.=" and";} else {$q2=" where";}
$q2.=" $sort is not null";}   
elseif (is_numeric($start)) {$q2="where $sort$gt'$start0'";}
$qs="select oid,* from oml $q2 order by $sort ".($asc?'':' desc').",file offset ".($page*$lines)." limit $lines";
$updlog.="$qs ";       
$q=pg_query($oml,$qs);
$fa=pg_fetch_all($q);


//index page/offset/sort
$base=$_SERVER['SCRIPT_NAME'];
foreach ($_SESSION as $x=>$y) {$$x=$y;}
foreach ($_GET as $x=>$y) {$$x=$_SESSION[$x]=$y;}
if (!$page) {$page=0;}
if (!$asc) {$asc=0;}

print"\n<p>page: <b>$page</b> sort: <b>$sort</b> start: <b>$start</b>".' <span id="log" style="font-size:8pt;color:#666;"></span>';
print"\n".'<table><tr class="az"><th>start<td><a href="'.$base.'?start=">ALL</a><td><a href="'.$base.'?start=NULL">NULL</a>';
for ($x=0;$x<=9;$x++) {print"\n<td><a href=\"".$base."?start=$x\">$x</a>";}
for ($x=1;$x<=26;$x++) {$y=chr($x+64);print"\n<td><a href=\"".$base."?start=$y\">$y</a>";}

print"\n".'<tr class="az"><th>sort/offset'
	.'<td><a href="'.$base.'?asc=1">ASC</a><td><a href="'.$base.'?asc=0">DESC</a>'
	.'<td><td><a href="'.$base.'?page=0">0</a>'
	.'<td>'.($page>0?'<a href="'.$base.'?page='.($page-1).'">&lt;</a>':'')
	.'<td>'.($page<count($fa)?'<a href="'.$base.'?page='.($page+1).'">&gt;</a>':'')
	.'<td><a href="'.$base.'?page='.count($fa).'">E</a>';
	
print"</table>";
print'<br>';
print chr(13).'
<form action="'.$base.'">
start <input type="text" name="start" size="20"> order by 
<input type="text" name="order" size="20"><input type="submit">
</form><br>';


//main table
print"\n<table><tr>";
//head
foreach ($rankfieldsa as $x=>$y) {if(isset($_SESSION['show'][$y])) {sortline ($y);}}
foreach ($datafieldsa as $x=>$y) {sortline ($y);}


foreach ($fa as $c=>$f) {
  //if ($c) {
  print"\n<tr>";
  $file1=str_replace("'","\\'",$f['file']);
  $file2=str_replace("(","",$file1);
  $file2=str_replace(")","",$file2);
  foreach ($rankfieldsa as $y=>$x) {
    if ($x=='year') {$isize=4;} elseif ($x=='style') {$isize=3;} else {$isize=2;}
    if (isset($_SESSION['show'][$x])) {
      print'
    <td><input type="'.(in_array($x,$ranktext)?'text':'tel').'" name="'.$x.$f['oid'].'" size="'.$isize.'" value="'.$f[$x].'" onchange="update('."'".$x."',".$f['oid'].',this.value)">';}}
    foreach ($datafieldsa as $x) {
      if ($x=='file') {
		  $f[$x]="\n<a href=\"#\" onclick=\"edit(".$f['oid'].");\" class=\"b\">EDIT</a>
      <a href=\"#\" onclick=\"mb(".$f['oid'].");\" class=\"b\">MB</a>
			<a href=\"javascript:playertext('$file2');loadSong('$file1');\">".$f['file']."</a>";
			}
	    elseif ($x=='size') {if ($f['bitrate']) {$sec=$f['size']/$f['bitrate']*8/1000;$mi=floor($sec/60);$t=$mi.':'.(($sec%60)<10?'0':'').($sec%60);$f['size']=$t;} else {$sec=$mi=$t=$f['size']=NULL;}}		
      elseif (($x=='ratetime' or $x=='utime') and is_numeric($f[$x])) {$f[$x]=date('dmy',$f[$x]);}
      print"\n<td>$f[$x]";}}
print'</table>';

print'<br><span onclick="togglevis(\'col\')">column edit</span><div id="col" style="display:none;"><form action="'.$base.'" method="POST"><table>';
foreach($rankfieldsa as $y) {print'<tr><td><input type="radio" name="show['.$y.']" value="0"'.(isset($_SESSION['show'][$y])?'':' checked').'> off<td><input type="radio" name="show['.$y.']" value="1"'.(isset($_SESSION['show'][$y])?' checked':'').'> on<td>'.$y;}
print'</table><input type="submit"></form></div>';
?>
<br><br><a href="ranking.php" target="ranking">Ranking</a>
<br><a href="styleedit.php" target="style">Style Type Editor</a>
<br><a href="index.htm">main menu</a>
</body></html>