<?php
	include('cap.php');
	$size_img_cat=100;
	
	function foto($foto)
	{
		global $size_img_cat;
		$size_img=array();
		//Размер
		$size_img=getimagesize($foto);
		
		$w_f=$size_img[0];
		$h_f=$size_img[1];
		if($size_img[0]>$size_img_cat)
		{
			$balans=$size_img[0]/$size_img_cat;
			//                     if($balans)>=1
			//                        {
			$size_img[0]=$size_img[0]/$balans;
			$size_img[1]=$size_img[1]/$balans;
			//                        }
			
		}
		if($size_img[1]>$size_img_cat)
		{
			$balans=$size_img[1]/$size_img_cat;
			//                    if(round($balans)>=1)
			//                        {
			$size_img[0]=$size_img[0]/$balans;
			$size_img[1]=$size_img[1]/$balans;
			//                        }
		}
		
		$size_img[0]=round($size_img[0]); //sg
		$size_img[1]=round($size_img[1]); //sg
		
		echo "<img src=$foto width=$size_img[0] height=$size_img[1] class='imgb imgp'  alt='фото'>";
		// Добавил style='padding: 10px' --SG
	}
	if (!file_exists("../upload/gs_foto/db/cat.txt"))file_put_contents("../upload/gs_foto/db/cat.txt",'');
	$file=array();
	$file1=array();
	
	if(!isset($_GET['c']) || !is_int(intval($_GET['c']))) $_GET['c']=1;
	
	if(!file_exists("../upload/gs_foto/db/cat/$_GET[c]"))
	{
		$d=opendir("../upload/gs_foto/db/news");
		while(($e=readdir($d))!=false)
		{
			if($e =="." || $e ==".." || $e==".htaccess") continue;
			$file[]=$e;
		}
		closedir($d);
	}
	else
	{
		$file=file("../upload/gs_foto/db/cat/$_GET[c]");
		for($n=0;$n<count($file);$n++)$file[$n]=trim($file[$n]);
	}
	
	
	foreach($file as $line)
	{
		$text=file("../upload/gs_foto/db/news/$line");
		$text[0]=trim($text[0]);
		$file1[$text[0]]=$line;
		
	}
	
	krsort($file1,SORT_NUMERIC);
	$file=array();
	foreach($file1 as $line) $file[]=$line;
	
	//Постраничная навигация
	$topic_count_page=20;
	if(!isset($_GET['page']) || !is_numeric(@$_GET['page']) || @$_GET['page']<1 )$page=1;
	else $page=$_GET['page'];
	if($page > ceil(count($file)/ $topic_count_page))$page=1;
	$start=$page * $topic_count_page-$topic_count_page;
	$pages=ceil(count($file)/ $topic_count_page);
	if(empty($_GET['ind']) || !is_numeric(@$_GET['ind']) || @$_GET['ind']>ceil($pages/10)  || @$_GET['ind']<1)$index=1;
	else $index= $_GET['ind'];
	
	$cat=array();
	$id_cat=array();
	$cat=file("../upload/gs_foto/db/cat.txt");
	
	foreach($cat as $line)
	{
		$line=trim($line);
		$expl=explode("*",$line);
		if (!isset($expl[1])) $expl[1]='';
		$id_cat[$expl[0]]=$expl[1];
	}
	
	
	
	echo  "<table  class=tab  >
	<tr><td> <input type=button value=\"Добавить галерею\" name=add_fa id=button onclick=\"document.location.href='admin.php';\">&nbsp;&nbsp;&nbsp;
	<input type=button value=\"Категории\" name=add_cat id=button_add_cat onclick=\"return x5engine.imShowBox({ swipeImg: '../../sg_cm/sg_cm/res/imSwipe.png', closeImg: '../../sg_cm/sg_images/imClose.png', loadingImg: '../../sg_cm/sg_cm/res/imLoad.gif', startIndex: 0, media:[{type: 'iframe', url: 'admin1.php?nocap=1', width: 640, height: 400, description: ''}]}, 0, this);\">
	</td><td></td></tr>
	<tr><td>";
	if(count($cat))
	{$width="style='width: 100%'";
	}
	else $width="style='width: 95%'";
	
	if(count($file)> $topic_count_page)
    {
		//Постраничная навигаця
		echo "<table $width  align=center><tr><td>";
		
		if($index>1)echo "
		<a class='navig_passiv' href=admin2.php?c=$_GET[c]&page=".(($index-1)*10)."&ind=".($index-1)." title=назад><<</a>";
		
		for($i=$index*10-9,$p=1; $i < $pages+1; $i++,$p++)
		{
			
            if($p>10 )
			{
          	  	echo "<a class='navig_passiv'  href=admin2.php?c=$_GET[c]&page=".($i)."&ind=".($index+1)." title=далее>>></a>";
          	  	break;
			}
          	if($page==$i)
            echo "<a class='navig_activ' href=admin2.php?c=$_GET[c]&page=$i&ind=$index>$i</a>";
            else
            echo "<a class='navig_passiv'href=admin2.php?c=$_GET[c]&page=$i&ind=$index>$i</a>";
		}
		
		
		echo "</td></tr></table>";
	}
	
	for($x=$start,$y=0; $x<count($file); $x++,$y++)
	{
     	if($y==$topic_count_page)break;
		
     	$text=file("../upload/gs_foto/db/news/$file[$x]");
     	for($n=0;$n<count($text);$n++)$text[$n]=trim($text[$n]);
     	$foto=array();$line2[1]='';
        $foto=file("../upload/gs_foto/db/id_foto/$file[$x]");
		if(count($foto)){$line2=explode('*:*',$foto[0]);
		$line2[1]=trim($line2[1]);}
        echo "<table  class=tab1  $width ><tr><td class=wd20 rowspan='2'>";
		if (file_exists('../upload/gs_foto/'.$line2[1])){
			if (count($foto) && file_exists('../upload/gs_foto/'.str_replace(".","_thumb.",$line2[1])))
			foto('../upload/gs_foto/'.str_replace('.','_thumb.',$line2[1])); 
			elseif(count($foto))foto('../upload/gs_foto/'.$line2[1]);  
		else echo"&nbsp;";}
		elseif (file_exists('../../'.$line2[1])){
			if (count($foto) && file_exists('../../'.str_replace(".","_thumb.",$line2[1])))
			foto('../../'.str_replace('.','_thumb.',$line2[1])); 
			elseif(count($foto))foto('../../'.$line2[1]);  
		else echo"&nbsp;";}
		$vrus=array();$vrus_arr=array();$vrus[1]='';
		if (file_exists('../upload/gs_foto/set/'.$file[$x])){
			$vrus_arr=file('../upload/gs_foto/set/'.$file[$x]);
		$vrus=explode('--',$vrus_arr[0]); }
		else $vrus[1]='';if(!isset($vrus[1]))$vrus[1]='';
        echo"</td><td class='valtop'><span style='color:#408080;'>".date("d.m.Y H:i",$text[0])."</span><br /><b>$text[2] </b> <br />$vrus[1]
        </td><td class='valtop ar wd20'>";
        if(!array_key_exists($text[1],$id_cat))echo"Вне категорий &nbsp;";
        else echo $id_cat[$text[1]].' &nbsp;';
        echo"</td></tr><tr><td colspan=2 class=ar>
        <a class=menu_cat_p href=red.php?id=$file[$x]&op=1&c=$_GET[c]&page=$page&ind=$index>Редактировать</a>&nbsp;&nbsp;&nbsp;&nbsp;
        <a class=menu_cat_p href=# onclick=\"del('red.php?id=$file[$x]&op=2&c=$_GET[c]&page=$page&ind=$index','фотоальбом')\">Удалить</a>&nbsp;
        </td></tr></table><br />";
	}
	
	
	if(count($file)> $topic_count_page)
    {
		//Постраничная навигаця
		echo "<table $width  align=center><tr><td>";
		
		if($index>1)echo "
		<a class='navig_passiv' href=admin2.php?c=$_GET[c]&page=".(($index-1)*10)."&ind=".($index-1)." title=назад><<</a>";
		
		for($i=$index*10-9,$p=1; $i < $pages+1; $i++,$p++)
		{
			
            if($p>10 )
			{
          	  	echo "<a class='navig_passiv'  href=admin2.php?c=$_GET[c]&page=".($i)."&ind=".($index+1)." title=далее>>></a>";
          	  	break;
			}
          	if($page==$i)
            echo "<a class='navig_activ' href=admin2.php?c=$_GET[c]&page=$i&ind=$index>$i</a>";
            else
            echo "<a class='navig_passiv'href=admin2.php?c=$_GET[c]&page=$i&ind=$index>$i</a>";
		}
		
		
		echo "</td></tr></table>";
	}
	
	if(count($cat))
	{
		if($_GET['c']==1)$class="class=menu_cat_a";
		else $class="class=menu_cat_p";
		echo "</td><td class='valtop wd15'>
		<a $class href=admin2.php>Все фотоальбомы</a><br /><br />";
		foreach($cat as $line)
		{
			$line=trim($line);
			$expl=explode("*",$line);
			if($expl[0]==$_GET['c'])$class="class=menu_cat_a";
			else $class="class=menu_cat_p";
			if (!isset($expl[1])) $expl[1]='';
			echo "<a $class href=admin2.php?c=$expl[0]>$expl[1]</a><br />";
		}
		
		echo "</td></tr></table>";
		
	}
?>

</div>
</body>
</html>