<?php
/*---------------------------------------------------+
| PHP-Fusion 6 Content Management System
+----------------------------------------------------+
| Copyright © 2002 - 2006 Nick Jones
| http://www.php-fusion.co.uk/
+----------------------------------------------------+
| Released under the terms & conditions of v2 of the
| GNU General Public License. For details refer to
| the included gpl.txt file or visit http://gnu.org
+----------------------------------------------------+
| Modified by PHP-Fusion SF Group
| Copyright © 2007 by SF
| http://www.rus-phpfusion.com/
+----------------------------------------------------*/

require_once "maincore.php";
require_once "subheader.php";
require_once "side_left.php";
include LOCALE.LOCALESET."downloads.php";
require_once INCLUDES."comments_include.php";
require_once INCLUDES."ratings_include.php";
include LOCALE.LOCALESET."custom_pages.php";

if (isset($download_id) && !isNum($download_id)) fallback("index.php");

if (isset($download_id) & !isset($type)) {
        $res = 0;
        if ($data = dbarray(dbquery("SELECT * FROM ".$db_prefix."downloads WHERE download_id='$download_id'"))) {
                $cdata = dbarray(dbquery("SELECT * FROM ".$db_prefix."download_cats WHERE download_cat_id='".$data['download_cat']."'"));
                if (checkgroup($cdata['download_cat_access'])) {
                        if (checkgroup($data['download_access'])) {
                        $res = 1;
                        $result = dbquery("UPDATE ".$db_prefix."downloads SET download_count=download_count+1 WHERE download_id='$download_id'");
                        redirect($data['download_url']);
                        }
                }
        }
        if ($res == 0) redirect("index.php");
} elseif (isset($type) & isset($download_id)) {
	$res = 0;
        if ($data = dbarray(dbquery("SELECT * FROM ".$db_prefix."downloads WHERE download_id='$download_id'"))) {
                $cdata = dbarray(dbquery("SELECT * FROM ".$db_prefix."download_cats WHERE download_cat_id='".$data['download_cat']."'"));
                if (checkgroup($cdata['download_cat_access'])) {
                        if (checkgroup($data['download_access'])) {
                        $res = 1;
                        $result = dbquery("UPDATE ".$db_prefix."downloads SET download_count=download_count+1 WHERE download_id='$download_id'");
                        redirect($data['mirror_url']);
                        }
                }
        }
        if ($res == 0) redirect("index.php");
}
function NomD ($nome, $id){
         global $num, $db_prefix;
         $result = dbquery("SELECT * FROM ".$db_prefix."download_cats WHERE download_cat_idc ='$nome' AND download_cat_id ".$id." ORDER BY download_cat_id");
         while ($data = dbarray($result)) {
                 $num =$num + dbcount("(download_cat)", "downloads", "download_cat='".$data['download_cat_id']."'");
                 NomD ($data['download_cat_id'],'');
         }
return $num;
}

function Put($id){
        global $sil, $db_prefix;
        $result = dbquery("SELECT * FROM ".$db_prefix."download_cats WHERE download_cat_id='$id'");
        while ($cdata = dbarray($result)){
        $sl = ($cdata['download_cat_idc']==0 ? '' : '/');
        $sil = $sl."<a class='main-fx' href='".FUSION_SELF."?cat_id=".$cdata['download_cat_id']."'>".$cdata['download_cat_name']."</a>".$sil;
        Put($cdata['download_cat_idc']);
        }
        return $sil;
}


if (!isset($cat_id)) {$cat_id=0;}
if (isset($cat_id) && !isset($page_id)) {
        $result = dbquery("SELECT * FROM ".$db_prefix."download_cats WHERE ".groupaccess('download_cat_access')."AND download_cat_idc='$cat_id' ORDER BY download_cat_id");
        $rows = dbrows($result);
        if ($rows != 0) {
                $result2 = dbquery("SELECT * FROM ".$db_prefix."download_cats WHERE download_cat_id='$cat_id'");
                $cdata = dbarray($result2);
                if (dbrows($result2)==0){$name='';}else{$name=": ".$cdata['download_cat_name'];}
                opentable($locale['D400'].$name);
                $counter = 0; $columns = 2;
                $num = 0;
                echo "<table cellpadding='0' cellspacing='0' width='100%'>\n<tr>\n";
                while ($data = dbarray($result)) {
                        if ($counter != 0 && ($counter % $columns == 0)) echo "</tr>\n<tr>\n";
                        NomD ($cat_id, "='".$data['download_cat_id']."'");
                        echo "<td align='left' valign='top' width='50%' class='tbl'>&nbsp;&nbsp;<a href='".FUSION_SELF."?cat_id=".$data['download_cat_id']."'>".$data['download_cat_name']."</a> <span class='small2'>($num)</span>";
                        $result2 = dbquery("SELECT * FROM ".$db_prefix."download_cats WHERE ".groupaccess('download_cat_access')."AND download_cat_idc='".$data['download_cat_id']."' ORDER BY download_cat_id");
                        $rows2 = dbrows($result2);
                        if ($rows2!=0){
                           $i=0;
                                 echo "<br>\n(";
                                 while ($data2 = dbarray($result2)) {
                                  $zap=($i+1==$rows2 ? '' : ', ');
                                  echo "<a href='".FUSION_SELF."?cat_id=".$data2['download_cat_id']."'>".$data2['download_cat_name']."</a>$zap";
                                  $i++;
                                 }
                                 echo ")";
                        }
                        if ($data['download_cat_description'] != "") echo "<br>\n<span class='small'>".$data['download_cat_description']."</span>";
                        echo "<br><br></td>\n" ;
                        $num = 0;
                        $counter++;
                }
                echo "</tr>\n</table>\n";
                closetable();  tablebreak();
        } else {
                if ($cat_id==0){
                opentable($locale['D400']);
                echo "<center><br>\n".$locale['430']."<br><br>\n</center>\n";
                closetable();}
        }


        if (!isNum($cat_id)) fallback(FUSION_SELF);
        $result = dbquery("SELECT * FROM ".$db_prefix."download_cats WHERE download_cat_id='$cat_id'");
        if (dbrows($result) != 0) {
                $cdata = dbarray($result);
                if (checkgroup($cdata['download_cat_access'])) {
                        Put($cat_id);

                        $rows = dbcount("(*)", "downloads", "download_cat='$cat_id'");
                        if (!isset($rowstart) || !isNum($rowstart)) $rowstart = 0;
                        if ($rows != 0) {
                                opentable($sil);
                                $result = dbquery("SELECT * FROM ".$db_prefix."downloads WHERE download_cat='$cat_id' ORDER BY ".$cdata['download_cat_sorting']." LIMIT $rowstart,10");
                                $numrows = dbrows($result); $i = 1;
                                while ($data = dbarray($result)) {
                                        if ($data['download_datestamp']+604800 > time()+($settings['timeoffset']*3600)) {
                                                $new = " <span class='small'>".$locale['410']."</span>";
                                        } else {
                                                $new = "";
                                        }
                                        echo "<table width='100%' cellpadding='0' cellspacing='1' class='tbl-border'>\n";
                                        echo "<tr>\n<td colspan='4' class='forum-caption'><table border='0' width='100%' cellspacing='0' cellpadding='0' id='table1'><tr><td class='main-fx'><b>".$data['download_title']."</b> $new</></td><td align='right' class='main-fx'><b>".$locale['414']."</b> ".showdate("%d.%m.%y", $data['download_datestamp'])."</td></tr></table></td>\n</tr>\n";
                                        if ($data['download_description'] != "") echo "<tr>\n<td colspan='4' class='tbl1'><br>".nl2br(stripslashes($data['download_description']))."<br></td>\n</tr>\n";
                                        $result2 = dbquery("SELECT * FROM ".$db_prefix."users WHERE user_id='".$data['download_user']."'");
                                        $data2 = dbarray($result2);
                                        echo "<tr>\n<td class='tbl2'><b>".$locale['412']."</b> <a href='profile.php?lookup=".$data['download_user']."' target='_self'>".$data2['user_name']."</a></b></td>\n";
                                        $contacts = dbquery("SELECT * FROM ".$db_prefix."comments WHERE comment_type='D' AND comment_item_id='".$data['download_id']."'");
                                        $col = dbrows($contacts);
                                        echo "<td class='tbl1'><b>".$locale['412']."</b> $col</td>\n<td class='tbl2'><b>".$locale['415']."</b> ".$data['download_count']."</td><td class='tbl1'><a href='downloads.php?page_id=".$data['download_id']."' target='_self'>".$locale['413']."</a></td>\n</tr>\n";
                                        echo "</table>\n";
                                        $col=0;
                                        if ($i != $numrows) { echo "<div align='center'><img src='".THEME."images/blank.gif' alt='' height='15' width='1'></div>\n"; $i++; }
                                }
                                closetable();
                                if ($rows > 5) echo "<div align='center' style='margin-top:5px;'>\n".makePageNav($rowstart,10,$rows,3,FUSION_SELF."?cat_id=$cat_id&amp;")."\n</div>\n";
                        } else {
                                echo $locale['431']."\n";
                        }
                }
        }
} 

if (isset($page_id)){
$result = dbquery("SELECT * FROM ".$db_prefix."downloads WHERE download_id='$page_id'");
if (dbrows($result) != 0) {
        $data = dbarray($result);
        opentable($data['download_title']);
        if ($data['download_description_p']==""){
                $cont=$data['download_description'];
        }else{
                $cont=$data['download_description_p'];
        }
        $result2 = dbquery("SELECT * FROM ".$db_prefix."users WHERE user_id='".$data['download_user']."'");
        $data2 = dbarray($result2);
        echo "<table width='100%' cellpadding='0' cellspacing='1' class='tbl-border'>\n";
        echo "<tr>\n<td colspan='3' class='forum-caption'><table border='0' width='100%' cellspacing='0' cellpadding='0' id='table1'><tr>\n<td class='main-fx'><b>".$locale['411']." <a href='profile.php?lookup=".$data['download_user']."' target='_self' class='main-fx'>".$data2['user_name']."</a></b></td><td align='right' class='main-fx'><b>".$locale['414']."</b> ".showdate("%d.%m.%y", $data['download_datestamp'])."</td></tr></table></td>\n</tr>\n";
        echo "<tr><td class='tbl1' colspan='2'><b>Официальный сайт:</b> <a href='http://".$data['download_os']."' target=_blank>".$data['download_os']."</a></td></tr>\n";
                if ($data['download_description'] != "") {//echo "<tr>\n<td colspan='3' class='tbl1'><br>";
        echo "<tr>\n<td colspan='3' class='tbl1'><br>".nl2br(stripslashes($cont))."<br>";
        if (checkgroup($data['download_access'])) {
                if ($data['download_access']!=3)
                echo "<div class='dow'>".$locale['D410']."</div>";
        }else{
           if ($data['download_access']!=3){
                echo "<div class='dow'>".$locale['D411']."</div>";
           }else{ echo "<div class='dow'>Для скачивания файла ваш статус должен быть VIP. За подробной информацией обратитесь к Администрации портала.</div>";
                }
        }
        //Сообщения зарегестрированым пользователям
       
        echo "</td>\n</tr>\n";
        //eval("".stripslashes($cont)." ");
       // echo "<br><br></td>\n</tr>\n";
        }
        if ($data['download_url']!=''){
        echo "<tr>\n<td class='tbl1' width='50%'><b>".$locale['415']."</b> ".$data['download_count']."</td>\n<td class='tbl2' width='50%'>";
        if (checkgroup($data['download_access'])) {
        echo "<a href='".FUSION_SELF."?cat_id=$cat_id&amp;download_id=".$data['download_id']."' target='_self'>".$locale['416']."</a> (".( $data['download_filesize'] ? $data['download_filesize'] : 'N/A' ).")";
        if ($data['mirror_url']!='') {
			echo " | <a href='".FUSION_SELF."?cat_id=$cat_id&amp;type=mirror&amp;download_id=".$data['download_id']."' target='_blank'>Зеркало</a> (".( $data['download_filesize'] ? $data['download_filesize'] : 'N/A' ).")";
        }
        }else{
        echo $locale['416']." (".$data['download_filesize'].")";
        }
        echo "</td>\n</tr>\n";
        }
        echo "</table>\n";

} else {
        opentable($locale['401']);
        echo "<center><br>\n".$locale['402']."\n<br><br></center>\n";
}
closetable();
if (dbrows($result) != 0) {
        if ($data['download_comments']) showcomments("D","downloads","download_id",$page_id,FUSION_SELF."?page_id=$page_id");
        if ($data['download_ratings']) showratings("D",$page_id,FUSION_SELF."?page_id=$page_id");
}
}

require_once "side_right.php";
require_once "footer.php";
?>