<?
#----00--00----------------------------------#
#----00---000----000000-----000000-#
#----00---0000--00----00----00---00--#
#----00---0000--00-----------00---------#
#----00---0000--000000----00000--#
#----00---000---00-----------00---------#
#----00--00-----00---00-----00---------#
#----0000-------000000----00--------#
######################################
##    W@P-MOTOR 16 - 17 DOWNLOAD CENTER MOD Def   ##
##         Дата выхода 5 февраля 2008 г.       ##
##          Авторы: XsErG, VantuZ, Def         ##
##             Связь: http://wapruk.ru                                ##
######################################


function format($name){
$f1=strrpos($name,".");
$f2=substr($name,$f1+1,999);
$fname=strtolower($f2);
return $fname;}
function dirs($name){
$f1=strrpos($name,"/");
$f2=substr($name,$f1+1,999);
$fname=strtolower($f2);
return $fname;}
function prev_dir($dir){
$d1=strrpos($dir,"/");
$d2=substr($dir,$d1,999);
$dir=str_replace($d2,"",$dir);
return $dir;}
function latin_to_utf($translite){
$eng=array("_","YA","Ya","ya","yee","YEE","YO","yo","Yo","ZH","zh","Zh","Z","z","CH","ch","Ch","SH","sh","Sh","YE","ye","Ye","YU","yu","Yu","JA","ja","Ja","A","a","B","b","V","v","G","g","D","d","E","e","I","i","J","j","K","k","L","l","M","m","N","n","O","o","P","p","R","r","S","s","T","t","U","u","F","f","H","h","W","w","x","q","Y","y","C","c");
$rus=array(" ","Я","Я","я","ые","ЫЕ","Ё","ё","Ё","Ж","ж","Ж","З","з","Ч","ч","Ч","Ш","ш","Ш","Э","э","Э","Ю","ю","Ю","Я","я","Я","А","а","Б","б","В","в","Г","г","Д","д","Е","е","И","и","Й","й","К","к","Л","л","М","м","Н","н","О","о","П","п","Р","р","С","с","Т","т","У","у","Ф","Ф","Х","х","Щ","щ","ъ","ь","Ы","ы","Ц","ц");
$translite=str_replace($eng,$rus,$translite);
return $translite;}

function utf_to_lat($transl){
$rus=array(" ","Щ","Щ","Ж","Ж","Ч","Ч","Ш","Ш","Ё","Ё","Э","Э","Ю","Ю","Я","Я","щ","ж","ч","ш","ё","э","ю","я","А","Б","В","Г","Д","Е","З","И","Й","К","Л","М","Н","О","П","Р","С","Т","У","Ф","Х","Ц","ь","ь","Ы","а","б","в","г","д","е","з","и","й","к","л","м","н","о","п","р","с","т","у","ф","х","ц","ь","ь","ы");
$eng=array("_","CSH","Csh","ZH","Zh","CH","Ch","SH","Sh","JO","Jo","JE","Je","JU","Ju","JA","Ja","csh","zh","ch","sh","jo","je","ju","ja","A","B","V","G","D","E","Z","I","J","K","L","M","N","O","P","R","S","T","U","F","H","C","&#39;","'","Y","a","b","v","g","d","e","z","i","j","k","l","m","n","o","p","r","s","t","u","f","h","c","&#39;","'","y");
$transl=str_replace($rus,$eng,$transl);
return $transl;}

function dir_name($titl,$dir){
if(file_exists("files$dir/dir.name")){
$nd=@file("files$dir/dir.name");
$titl=str_replace("/","",$titl);
foreach ($nd as $f=>$nam){
$nm = explode('::',$nd[$f]);
$nam=$nm[0];
if($nam == $titl){$id=$nm[1];}}
if($id==''){$id=latin_to_utf($titl);}}
else $id="br";
return $id;}

function show_abs($did,$name)
{
	global $ab_len;
	if (file_exists('files'.$did.'/'.$name.'.txt'))
	{
$str = file_get_contents('files'.$did.'/'.$name.'.txt');
		$str = substr($str,0,$ab_len);
		$str=trim($str);
		$str=eregi_replace("((https?|ftp)://[[:alnum:]_=/-]+(\\.[[:alnum:]_=/-]+)*(/[[:alnum:]+&amp;._=/~:%#]*(\\?[[:alnum:]?+&amp;_=/%:#]*)?)?)", "<a href='\\1'>\\1</a>", $str);
		return $str.'...';
	}
	return '';
}

function file_type($dir){
$t="$dir/type.txt";
if (file_exists($t)){
$str=file_get_contents($t);
$s1=explode("||",$str);
if($s1[0]=="java"){$tip="java";
$f=explode(":",$s1[1]);$oform=$f;}
elseif($s1[0]=="img"){$tip="img";
$f=explode(":",$s1[1]);$oform=$f;}
elseif($s1[0]=="mp3"){$tip="mp3";
$f=explode(":",$s1[1]);$oform=$f;}
elseif($s1[0]=="themes"){$tip="themes";
$f=explode(":",$s1[1]);$oform=$f;}
elseif($s1[0]=="file"){$tip="file";
$f=explode(":",$s1[1]);$oform=$f;}
elseif($s1[0]=="video"){$tip="video";
$f=explode(":",$s1[1]);$oform=$f;}
elseif($s1[0]=="all"){$tip="all";}
else {$tip="no";}}
else{$tip="no";}
return array($tip,$oform);}

function DirFilesR($dir){    
$handle = opendir($dir);
$files = Array(); 
$subfiles = Array();
while ($fid = readdir($handle)){ 

if ($fid != "." and  $fid != ".." ){ 
$format = format($fid);

if(is_dir($dir."/".$fid)){ 
$subfiles = DirFilesR($dir."/".$fid); 
$files = array_merge($files,$subfiles);}
list($tip,$oform)=file_type("$dir");
if(in_array($format,$oform)==true){
$files[] = $dir."/".$fid;
}}}      
closedir($handle); 
return $files; 
}

function dchek($str)
{	
	$str = str_replace('../','',$str);
	$str = str_replace('./','',$str);
	$str = preg_replace("/[^0-9a-z-\_\s\/]/i", "", $str);
	return $str;
}
function im_chek($message)
{	
$message=str_replace("|","I",$message); 
$message=str_replace("||","I",$message);
$message=str_replace("&","",$message);
$message=str_replace("\"","",$message);
$message=str_replace(">","",$message);
$message=str_replace("<","",$message);
$message=htmlspecialchars($message);                  
$message=str_replace("'","",$message);            
$message=str_replace("\"","",$message);  
$message=str_replace("/\\\$/","",$message);    
$message=str_replace("$","",$message);          
$message=str_replace("\\","", $message);                            
$message=str_replace("@","", $message);      
$message=str_replace("`","", $message);    
$message=str_replace("%","", $message);
$message=str_replace("^","", $message); 
$message=stripslashes(trim($message));               
return $message;  
}
function check_fulls($message){ 
$message=str_replace("|","I",$message); 
$message=str_replace("||","I",$message);
$message=str_replace("&","",$message);
$message=str_replace("\"","",$message);
$message=str_replace(">","",$message);
$message=str_replace("<","",$message);
$message=htmlspecialchars($message);                  
$message=str_replace("'","",$message);            
$message=str_replace("\"","",$message);  
$message=str_replace("/\\\$/","",$message);    
$message=str_replace("$","",$message);          
$message=str_replace("\\","", $message);                            
$message=str_replace("@","", $message);      
$message=str_replace("`","", $message);    
$message=str_replace("%","", $message);
$message=str_replace("^","", $message); 
$message=stripslashes(trim($message));               
return $message;  
}

function name_f($name)
{
	$inf = pathinfo($name,PATHINFO_EXTENSION);
	return str_replace('.'.$inf,'',$name);
}

function put_count($did)
{	
global $new_f;
global $cash;
$ttime = time();
$filtime = filemtime($did.'/count.dir');
$filtime = $filtime + (3600 * $cash);
$tot_new = 0;
$total = 0;
if($ttime >= $filtime || !is_file($did.'/count.dir')){
$new_v = (3600 * 24 * $new_f);
$dik = str_replace("files", "", $did);
$dik = str_replace("/", "", $dik);
$abs = strlen($dik);
if(is_file("baze/$dik.baz")){
	$ft = @file("baze/$dik.baz");
	$tot = count($ft);
	foreach ($ft as $i){
		$data = explode("|",$i);
		$ftime = $data[6] + $new_v;
		if($ftime >= $ttime){$tot_new ++;}
	}
$tota = $tot.'|'.$tot_new;
$fp = fopen($did.'/count.dir','w');	
fputs($fp,$tota);
fclose($fp);
@chmod($did.'/count.dir',0666);
return array($tot,$tot_new);
}else{
$odir = opendir("baze");
	while ($file = readdir($odir)){
		$fil = substr($file,0,$abs);
		if (!strcmp($fil, $dik)){
			$ft = @file("baze/$file");
			$total = count($ft);
				foreach ($ft as $i){
				$data = explode("|",$i);
				$ftime = $data[6] + $new_v;
				if($ftime >= $ttime){$tot_new ++;}
				}
			$tot += $total;
		}
	}
closedir($odir);
$tota = $tot.'|'.$tot_new;
$fp = fopen($did.'/count.dir','w');	
fputs($fp,$tota);
fclose($fp);
@chmod($did.'/count.dir',0666);
return array($tot,$tot_new);
}
}else{
$tota =	file_get_contents($did.'/count.dir');	
return explode('|',$tota);}
}

function page($all_p,$page,$p_list,$did,$s,$m = 2)
{
	$maxpages = 3;
	if ($all_p <= $page || $page < 1) $page = 1;
	$pages = $all_p / $p_list;
	$pages = ceil($pages);
	if ($pages > 1)
	{
		if ($page > 1) echo '<a href="index.php?s='.$s.'&amp;m='.$m.'&amp;m='.$m.'&amp;did='.$did.'&amp;page='.($page-1).'&amp;'.session_name().'='.session_id().'">&#171;-</a>';
		else echo "&#171;-";
		if ($page < $pages) echo '<a href="index.php?s='.$s.'&amp;m='.$m.'&amp;did='.$did.'&amp;page='.($page+1).'&amp;'.session_name().'='.session_id().'">-&#187;</a><br/>';
		else echo " -&#187;<br/>";
		if ($page == 3) echo '<a href="index.php?s='.$s.'&amp;m='.$m.'&amp;did='.$did.'&amp;page=1&amp;'.session_name().'='.session_id().'">1</a>';
		if ($page > 3) echo '<a href="index.php?s='.$s.'&amp;m='.$m.'&amp;did='.$did.'&amp;page=1&amp;'.session_name().'='.session_id().'">1</a> ... ';
		$l = $page - floor($maxpages / 2);
 		$r = $page + floor($maxpages / 2);
		if ($l < 1)
		{
			$l = 1;
			$r = $maxpages;
		}
		if (($page - 1) >= $maxpages && $page != $pages)
		{
			$ll = $l;
			echo '<a href="index.php?s='.$s.'&amp;m='.$m.'&amp;did='.$did.'&amp;page='.$ll.'&amp;'.session_name().'='.session_id().'">&#171;</a> ';
		}		
		for ($l; $l <= $r; $l ++)
		{
			if ($l > $pages) break;
			if ($l == $page) print " <b>$page</b> ";
			else echo ' <a href="index.php?s='.$s.'&amp;m='.$m.'&amp;did='.$did.'&amp;page='.$l.'&amp;'.session_name().'='.session_id().'">'.$l.'</a> ';
		}
		if ($pages - 1 > $r)
		{
			$ll = $r + 1;
			echo '<a href="index.php?s='.$s.'&amp;m='.$m.'&amp;did='.$did.'&amp;page='.$ll.'&amp;'.session_name().'='.session_id().'">&#187;</a> ';
		}
		if ($page <= $pages - 2 && $pages > 3) echo '...<a href="index.php?s='.$s.'&amp;m='.$m.'&amp;did='.$did.'&amp;page='.$pages.'&amp;'.session_name().'='.session_id().'">'.$pages.'</a> ';
		echo '<hr/>';
	}	
}

function pagen($all_p,$page,$p_list,$did)
{
	$maxpages = 3;
	if ($all_p <= $page || $page < 1) $page = 1;
	$pages = $all_p / $p_list;
	$pages = ceil($pages);
	if ($pages > 1)
	{
		if ($page > 1) echo '<a href="new.php?did='.$did.'&amp;page='.($page-1).'&amp;'.session_name().'='.session_id().'"><<Назад-</a> |';
		else echo "<<Назад- ";
		if ($page < $pages) echo '|<a href="new.php?did='.$did.'&amp;page='.($page+1).'&amp;'.session_name().'='.session_id().'"> -Вперед>></a><br/>';
		else echo " -Вперед>><br/>";
		if ($page == 3) echo '<a href="new.php?did='.$did.'&amp;page=1&amp;'.session_name().'='.session_id().'">1</a>';
		if ($page > 3) echo '<a href="new.php?did='.$did.'&amp;page=1&amp;'.session_name().'='.session_id().'">1</a> ... ';
		$l = $page - floor($maxpages / 2);
 		$r = $page + floor($maxpages / 2);
		if ($l < 1)
		{
			$l = 1;
			$r = $maxpages;
		}
		if (($page - 1) >= $maxpages && $page != $pages)
		{
			$ll = $l;
			echo '<a href="new.php?did='.$did.'&amp;page='.$ll.'&amp;'.session_name().'='.session_id().'">&#171;</a> ';
		}		
		for ($l; $l <= $r; $l ++)
		{
			if ($l > $pages) break;
			if ($l == $page) print " <b>$page</b> ";
			else echo ' <a href="new.php?did='.$did.'&amp;page='.$l.'&amp;'.session_name().'='.session_id().'">'.$l.'</a> ';
		}
		if ($pages - 1 > $r)
		{
			$ll = $r + 1;
			echo '<a href="new.php?did='.$did.'&amp;page='.$ll.'&amp;'.session_name().'='.session_id().'">&#187;</a> ';
		}
		if ($page <= $pages - 2 && $pages > 3) echo '...<a href="new.php?did='.$did.'&amp;page='.$pages.'&amp;'.session_name().'='.session_id().'">'.$pages.'</a> ';
		echo '<hr/>';
	}
	
}
function paget($all_p,$page,$p_list,$did)
{
	$maxpages = 3;
	if ($all_p <= $page || $page < 1) $page = 1;
	$pages = $all_p / $p_list;
	$pages = ceil($pages);
	if ($pages > 1)
	{
		if ($page > 1) echo '<a href="top.php?did='.$did.'&amp;page='.($page-1).'&amp;'.session_name().'='.session_id().'"><<Назад-</a> |';
		else echo "<<Назад- ";
		if ($page < $pages) echo '|<a href="top.php?did='.$did.'&amp;page='.($page+1).'&amp;'.session_name().'='.session_id().'"> -Вперед>></a><br/>';
		else echo " -Вперед>><br/>";
		if ($page == 3) echo '<a href="top.php?did='.$did.'&amp;page=1&amp;'.session_name().'='.session_id().'">1</a>';
		if ($page > 3) echo '<a href="top.php?did='.$did.'&amp;page=1&amp;'.session_name().'='.session_id().'">1</a> ... ';
		$l = $page - floor($maxpages / 2);
 		$r = $page + floor($maxpages / 2);
		if ($l < 1)
		{
			$l = 1;
			$r = $maxpages;
		}
		if (($page - 1) >= $maxpages && $page != $pages)
		{
			$ll = $l;
			echo '<a href="top.php?did='.$did.'&amp;page='.$ll.'&amp;'.session_name().'='.session_id().'">&#171;</a> ';
		}		
		for ($l; $l <= $r; $l ++)
		{
			if ($l > $pages) break;
			if ($l == $page) print " <b>$page</b> ";
			else echo ' <a href="top.php?did='.$did.'&amp;page='.$l.'&amp;'.session_name().'='.session_id().'">'.$l.'</a> ';
		}
		if ($pages - 1 > $r)
		{
			$ll = $r + 1;
			echo '<a href="top.php?did='.$did.'&amp;page='.$ll.'&amp;'.session_name().'='.session_id().'">&#187;</a> ';
		}
		if ($page <= $pages - 2 && $pages > 3) echo '...<a href="top.php?did='.$did.'&amp;page='.$pages.'&amp;'.session_name().'='.session_id().'">'.$pages.'</a> ';
		echo '<hr/>';
	}
	
}
function put_top($did)
{	
global $col_top;
global $cash;
$ttime = time();
$filtime = filemtime('files'.$did.'/top.dir');
$filtime = $filtime + (3600 * $cash);
if($ttime >= $filtime || !is_file('files'.$did.'/top.dir')){
$dik = str_replace("/", "", $did);
$abs = strlen($dik);
if(is_file("baze/$dik.baz")){
	$ft = @file("baze/$dik.baz");
	foreach ($ft as $i){
		$data = explode("|",$i);
		if($data[1] > 0){
		$count[] = $data[1];
		$files[] = "files$data[11]";
		}
	}
		arsort($count);
		$fl = array();
		foreach ($count as $k => $val)
		{
			$fl[] = $files[$k];
		}
	$fl1 = array_slice($fl,0,$col_top);
	$fl = join($fl1, "\r\n");
$fp = fopen('files'.$did.'/top.dir','w');	
fputs($fp,$fl);
fclose($fp);
@chmod('files'.$did.'/top.dir',0666);
return $fl1;
}else{
$odir = opendir("baze");
	while ($file = readdir($odir)){
		$fil = substr($file,0,$abs);
		if (!strcmp($fil, $dik)){
			$ft = @file("baze/$file");
				foreach ($ft as $i){
				$data = explode("|",$i);
					if($data[1] > 0){
					$count[] = $data[1];
					$files[] = "files$data[11]";
					}
				}
			arsort($count);
			$fl = array();
			foreach ($count as $k => $val)
			{
				$fl[] = $files[$k];
			}
		$fl1 = array_slice($fl,0,$col_top);
		$fl = join($fl1, "\r\n");
		}
	}
closedir($odir);
$fp = fopen('files'.$did.'/top.dir','w');	
fputs($fp,$fl);
fclose($fp);
@chmod('files'.$did.'/top.dir',0666);
return $fl1;
}
}else{
$files = @file('files'.$did.'/top.dir');	
return $files;}
}
function put_newf($did)
{	
global $new_f;
global $cash;
$ttime = time();
$filtime = filemtime('files'.$did.'/new.dir');
$filtime = $filtime + (3600 * $cash);
if($ttime >= $filtime || !is_file('files'.$did.'/new.dir')){
$new_v = (3600 * 24 * $new_f);
$dik = str_replace("/", "", $did);
$abs = strlen($dik);
if(is_file("baze/$dik.baz")){
	$ft = @file("baze/$dik.baz");
	$tot = count($ft);
	foreach ($ft as $i){
		$data = explode("|",$i);
		$ftime = $data[6] + $new_v;
		if($ftime >= $ttime){
		$files[] = "files$data[11]";
		$fl = join($files, "\r\n");}
	}
$fp = fopen('files'.$did.'/new.dir','w');	
fputs($fp,$fl);
fclose($fp);
@chmod('files'.$did.'/new.dir',0666);
return $files;
}else{
$odir = opendir("baze");
	while ($file = readdir($odir)){
		$fil = substr($file,0,$abs);
		if (!strcmp($fil, $dik)){
			$ft = @file("baze/$file");
			foreach ($ft as $i){
			$data = explode("|",$i);
			$ftime = $data[6] + $new_v;
			if($ftime >= $ttime){
			$files[] = "files$data[11]";
			$fl = join($files, "\r\n");}
			}
		}
	}
closedir($odir);
$fp = fopen('files'.$did.'/new.dir','w');	
fputs($fp,$fl);
fclose($fp);
@chmod('files'.$did.'/new.dir',0666);
return $files;
}
}else{
$files = @file('files'.$did.'/new.dir');	
return $files;}
}

function dir_type($did)
{		
		
		$odir = opendir("$did");
		while ($file = readdir($odir))
		{$format=format($file);
		
if ((($file!==".")&&($file!=="..")) and !is_dir("$did/$file"))
{list($tip,$oform) = file_type("$did");}
			if (is_dir($did . '/' . $file) && $file != '.' && $file != '..')
			{				
				list ($tip,$oform) = dir_type("$did/$file");
			}
		}
		closedir($odir);		
		return array($tip,$oform);	
}
function baze($did)
{
$odir = opendir($did);
while ($file = readdir($odir)){
if(ereg (".baz$", "$file")){
$baz=$baz+1;
}}
closedir($odir);
return $baz;
}
?>