<?php
error_reporting(0);
if($_POST){
include dirname(__FILE__)."/config/config.php";
$sitein=false;
$xtem1=str_replace("\\","/",$_SERVER['DOCUMENT_ROOT']);
$xtem2=str_replace("\\","/",dirname(__FILE__));
$xtem=str_replace($xtem1,"",$xtem2);


if (file_exists(dirname(__FILE__)."/userl/".$_POST['uid'])) $sitein=true;

	if($_POST['add']=="ok"){
		if($sitein!=true){
  mkdir(dirname(__FILE__)."/userl/".$SID,0755);
 $_POST['uid']=$SID;
 
		}
		$mypl=file_get_contents(dirname(__FILE__)."/userl/".$_POST['uid']."/plist.txt");
		if($mypl) $mypl=explode("\n",$mypl); else $mypl=array(); $max=0;
		if(count($mypl)>0) foreach($mypl as $ke=>$va){
			$its=explode("{red}",trim(ltrim($va)));
			if($its[0]>$max) $max=$its[0];
		}
		if($max>0) $pos=$max+1; else $pos=1;
		$nepl=array();
		if(count($mypl)>0) foreach($mypl as $ke=>$va){
			if(list($pos1, $aid1, $oid1, $autor1, $title1, $time1) = explode("{red}",trim(ltrim($va)))){
				$nepl[]=trim(ltrim($va));
			}
		}
		$nepl[]=$pos.'{red}'.$_POST['aid'].'{red}'.$_POST['oid'].'{red}'.$_POST['autor'].'{red}'.$_POST['title'].'{red}'.$_POST['time'];
		
		$nepl6=array(); $bt=1;
		foreach($nepl as $ko=>$va){ $nepl6[$bt]=$va; $bt++;}
		$nepl=$nepl6;
		$bt=1; $nepl6=array();
if(count($nepl)>0) foreach($nepl as $ke=>$va){
			if(list($pos1, $aid1, $oid1, $autor1, $title1, $time1) = explode("{red}",trim(ltrim($va)))){
				$nepl6[]=$bt.'{red}'.$aid1.'{red}'.$oid1.'{red}'.$autor1.'{red}'.$title1.'{red}'.$time1;
				$bt++;
			}
		}
		$nepl=$nepl6;
		
		$fs=fopen(dirname(__FILE__)."/userl/".$_POST[uid]."/plist.txt","w");
		flock($fs, LOCK_EX);
		fputs($fs,implode("\n",$nepl));
		flock($fs, LOCK_UN);
		fclose($fs);
  echo "ok"; exit;
	} else if($sitein AND $_POST['del']=="ok"){
		$mypl=file_get_contents(dirname(__FILE__)."/userl/".$_POST['uid']."/plist.txt");
		if($mypl) $mypl=explode("\n",$mypl); else $mypl=array();
		$nepl=array();
		if(count($mypl)>0) foreach($mypl as $ke=>$va){
			if(list($pos1, $aid1, $oid1, $autor1, $title1, $time1) = explode("{red}",trim(ltrim($va)))){
				if($pos1!=$_POST['rid']) $nepl[]=trim(ltrim($va));
			}
		}
		$nepl6=array(); $bt=1;
		foreach($nepl as $ko=>$va){ $nepl6[$bt]=$va; $bt++;}
		$nepl=$nepl6;
		$bt=1; $nepl6=array();
if(count($nepl)>0) foreach($nepl as $ke=>$va){
			if(list($pos1, $aid1, $oid1, $autor1, $title1, $time1) = explode("{red}",trim(ltrim($va)))){
				$nepl6[]=$bt.'{red}'.$aid1.'{red}'.$oid1.'{red}'.$autor1.'{red}'.$title1.'{red}'.$time1;
				$bt++;
			}
		}
		$nepl=$nepl6;
		$fs=fopen(dirname(__FILE__)."/userl/".$_POST[uid]."/plist.txt","w");
		flock($fs, LOCK_EX);
		fputs($fs,implode("\n",$nepl));
		flock($fs, LOCK_UN);
		fclose($fs);
		
		echo "ok"; exit;
	} else if($sitein AND $_POST['sort']=="ok"){
		$mypl=file_get_contents(dirname(__FILE__)."/userl/".$_POST['uid']."/plist.txt");
		if($mypl) $mypl=explode("\n",$mypl); else $mypl=array();
		$nepl=array();
		//print_r($_POST);
		if(count($mypl)>0) foreach($mypl as $ke=>$va){
			if(list($pos1, $aid1, $oid1, $autor1, $title1, $time1) = explode("{red}",trim(ltrim($va)))){
				if(array_search($pos1,$_POST['rid'])!==FALSE) $nepl[array_search($pos1,$_POST['rid'])]=(array_search($pos1,$_POST['rid'])+1).'{red}'.$aid1.'{red}'.$oid1.'{red}'.$autor1.'{red}'.$title1.'{red}'.$time1;
			}
		}
		ksort($nepl);
		reset($nepl);
		//print_r($nepl);
		$nepl6=array(); $bt=1;
		foreach($nepl as $ko=>$va){ $nepl6[$bt]=$va; $bt++;}
		$nepl=$nepl6;
		$bt=1; $nepl6=array();
if(count($nepl)>0) foreach($nepl as $ke=>$va){
			if(list($pos1, $aid1, $oid1, $autor1, $title1, $time1) = explode("{red}",trim(ltrim($va)))){
				$nepl6[]=$bt.'{red}'.$aid1.'{red}'.$oid1.'{red}'.$autor1.'{red}'.$title1.'{red}'.$time1;
				$bt++;
			}
		}
		$nepl=$nepl6;
		
		$fs=fopen(dirname(__FILE__)."/userl/".$_POST['uid']."/plist.txt","w");
		flock($fs, LOCK_EX);
		fputs($fs,implode("\n",$nepl));
		flock($fs, LOCK_UN);
		fclose($fs);
		
		echo "ok"; exit;
	} else if($_POST['list']=="ok"){
		$jjs='plo_list_obj=new Array(); plo_list_norber=new Array(); plo_list_xftitle=new Array(); ';
		$mypl=get_list($SID);
		$xp=0;
echo '<DIV style="width:203px; position:relative; display:block;" id="example-1-3"><ul class="sortable-list">';
$mzt=1000; $xrq=0;
		if($mypl['count']>0){ foreach($mypl[item] as $ke=>$pp){
			$pp[id]=$pp[pos];
			$xp=1;
$pp[autor]=iconv('utf-8','windows-1251',$pp[autor]);
$pp[title]=iconv('utf-8','windows-1251',$pp[title]);
$pp[autor]=stripslashes($pp[autor]);
$pp[title]=stripslashes($pp[title]);


$nnm=date('i:s', (int)$pp[time]);
$tname=totranslit($pp[autor].'-'.$pp[title]);

//$tname=preg_replace("~[^a-z0-9_-]~","",$tname);
$tname=preg_replace("~([^a-z0-9_-]+)~ui","",$tname);
$tname=str_replace(array(',','\'','\"','&'),"",$tname);

$serv=get_servers(); // выбираем сервак
$idhash=md5($pp[aid]."*".$pp[oid].'_'.$pp[id]);
$hashss=md5("live_".$pp[aid].$pp[oid].'_'.$pp[id]);
if($full_list[ssoneurl]=="true") $li1="http://".$serv."/saudio/".$hashss."/".$pp[aid]."/".$tname."(".$serv.").mp3";
else $li1="http://".$serv."/audios/".$pp[aid]."/".$pp[oid]."/".$tname."(".$serv.").mp3";

//$li1=get_link($li1,8);

echo <<<BB

<li style="width:203px;height: 26px; pointer: move" class="sortable-item">

<div style="float:left; width:186px; height:24px;cursor:pointer;" onclick="gopl(this)" id="plid_{$idhash}">
	<span  id="ui-icon-play"><span class="nazpl" style="height: 24px; padding-top: 4px;">&nbsp;<b>{$pp[autor]}</b> - {$pp[title]}</span></span>
	<span  id="ui-icon-pause" style="display:none"><span class="nazpl" style="height: 22px; padding-top: 4px;background: #3782d3;">&nbsp;<b>{$pp[autor]}</b> - {$pp[title]}</span></span>
	<span style="display:none;" id="norber">http://{$serv}/audios/{$pp[aid]}/{$pp[oid]}/play.mp3</span>
	<span style="display:none;" id="xftitle">{$pp[autor]} - {$pp[title]}</span>
</div>

<div style="width:17px; height:24px;float:right;">
<div class="dd" style="width:15px; height:24px;">
<img src="/images/delete.png" id="del_mp_{$idhash}" alt="{$pp[id]}" title="Удалить" style="cursor:pointer">
</div>
</div>

</li>

BB;
$jjs.='plo_list_norber['.$xrq.']=\'http://'.$serv.'/audios/'.$pp[aid].'/'.$pp[oid].'/play.mp3\';';
$jjs.='plo_list_xftitle['.$xrq.']=\''.$pp[autor].' - '.$pp[title].'\';';
$jjs.='plo_list_obj['.$xrq.']=\''.$idhash.'\';';

$mzt++;  $xrq++;
		}
} else echo "<br><br><center><b>Добавьте добавьте треки<br> <br>в свой плейлист для <br><br>удобного прослушивания!</b></center>";
echo '</ul></div>
';
	}
	
}
?>{JAVASCRIPT}<?=$jjs?>