<?
include '../config.php';
include '../func.php';

head();
aut();
ban();
////////////////////////////////////////
mysql_query("UPDATE `profile` SET `online` = 'fo' WHERE `id` = '".$id_session."'");



$id = num($_GET['cat']);
$cat = mysql_fetch_assoc(mysql_query("SELECT * FROM `fo_cat` WHERE `id` = '".$id."'"));

if ( empty($cat['id']) ) {
    
    echo head . "Обменник файлов / <b>NULL</b>" . div; 
    
    echo katalog;
    
    echo 'Каталог не найден';
    
    echo div;
    navig();
    foot();
    exit();
    
}

if ( isset($_POST['submit']) ) {
    
    ####Файл
    $file = $_FILES['file']['tmp_name'];
    $filename = $_FILES['file']['name'];
    
    ####Скрин
    $screen = $_FILES['screen']['tmp_name'];
    $screenname = $_FILES['screen']['name'];
    
    #########
    $name = html($_POST['name']);
    $info = html($_POST['info']);
    $pcat = num($_POST['pcat']);
    
    
    if ( empty($screen) ) {
        
        
        if ( empty($file) ) {
            header('Location: cat.php?cat='.$cat['id'].'&mod=emptyf'); 
            exit();
            }
        
        ini_set('memory_limit', '64M');
        $maxsize = "10280000000000";
        $maxsizesc = "10280000000000";
        
        if($cat['tip']==1)
        $extentions = array("jpg","jpeg","png","gif", "bmp");
        
        if($cat['tip']==2)
        $extentions = array("mp3","wav","amr","mp4");
        
        if($cat['tip']==3)
        $extentions = array("3gp","mp4","avi");
        
        if($cat['tip']==4)
        $extentions = array("jar");
        
        if($cat['tip']==5)
        $extentions = array("sis", "sisx");
        
        if($cat['tip']==6)
        $extentions = array("thm");
        
        if($cat['tip']==7)
        $extentions = array("sis", "sisx");
        
        if($cat['tip']==8)
        $extentions = array("rar", "zip");
        
        if($cat['tip']==9)
        $extentions = array("txt", "doc", "pdf");
        
        if($cat['tip']==10)
        $extentions = array("jpg","jpeg","png","gif", "bmp");
        
        if($cat['tip']==11)
        $extentions = array("3gp","mp4","avi");
        
        
        $extentionssc = array("jpg","jpeg","png","gif", "bmp");
        
        $size = filesize ($_FILES['file']['tmp_name']);
        $type = strtolower(substr($filename, 1+strrpos($filename,".")));
        $new_name = 'file-'.time().'.'.$type;
        
        if ( $siza > $maxsize ) { header('Location: cat.php?cat='.$cat['id'].'&razmer'); exit(); }
        if ( !in_array($type,$extentions) ) { header('Location: cat.php?cat='.$cat['id'].'&type'); exit(); }
        if ( empty($name) ) { header('Location: cat.php?cat='.$cat['id'].'&emptyn'); exit(); }
        if ( empty($info) ) { header('Location: cat.php?cat='.$cat['id'].'&emptyi'); exit(); }
        if ( strlen($info) > 500 ) { header('Location: cat.php?cat='.$cat['id'].'&info'); exit(); }
        if ( strlen($name) > 50 ) { header('Location: cat.php?cat='.$cat['id'].'&name'); exit(); }
        
        if (!copy($file, "files/".$new_name))  { header('Location: cat.php?cat='.$cat['id'].'&nofile'); exit(); }
        
        echo '<b>Result:</b><br/>';
        echo 'Файл: '.$new_name.'<br/ >';
        echo 'Название: '.$name.'<br/ >';
        echo 'Описание: '.$info.'<br/ >';
        echo 'Подкаталог: '.$pcat.'<br/ >';
        echo 'Каталог: '.$id.'<br/ >';
        mysql_query("UPDATE `profile` SET `many` = '".($many_session+5)."' WHERE `id` = '".$id_session."'");
        $result = mysql_query("INSERT INTO `fo_fil` SET `name` = '".html($name)."', `info` = '".html($info)."', `cat` = '".num($id)."', `pcat` = '".num($pcat)."', `file` = '".html($new_name)."', `screen` = '".html($new_namesc)."', `date` = '".time()."', `user` = '".$id_session."' ");
        
        $last = mysql_insert_id();
        
        if ( $result == NULL ) header('Location: cat.php?cat='.$cat['id'].'&nofile'); else header('Location: view.php?file='.$last.'&upload');
        
        echo $result;
    } else {
        
                ini_set('memory_limit', '64M');
        $maxsize = "10280000000000";
        $maxsizesc = "10280000000000";
        
        if($cat['tip']==1)
        $extentions = array("jpg","jpeg","png","gif", "bmp");
        
        if($cat['tip']==2)
        $extentions = array("mp3","wav","amr","mp4");
        
        if($cat['tip']==3)
        $extentions = array("3gp","mp4","avi");
        
        if($cat['tip']==4)
        $extentions = array("jar");
        
        if($cat['tip']==5)
        $extentions = array("sis", "sisx");
        
        if($cat['tip']==6)
        $extentions = array("thm");
        
        if($cat['tip']==7)
        $extentions = array("sis", "sisx");
        
        if($cat['tip']==8)
        $extentions = array("rar", "zip");
        
        if($cat['tip']==9)
        $extentions = array("txt", "doc", "pdf");
        
        if($cat['tip']==10)
        $extentions = array("jpg","jpeg","png","gif", "bmp");
        
        if($cat['tip']==11)
        $extentions = array("3gp","mp4","avi");
        
        
        $extentionssc = array("jpg","jpeg","png","gif", "bmp");
        
        $size = filesize ($_FILES['file']['tmp_name']);
        $type = strtolower(substr($filename, 1+strrpos($filename,".")));
        $type2 = strtolower(substr($screenname, 1+strrpos($screenname,".")));
        $new_namesc = 'screen-'.time().'.'.$type2;
        $new_name = 'file-'.time().'.'.$type;
        
        if ( $siza > $maxsize ) { header('Location: cat.php?cat='.$cat['id'].'&razmer'); exit(); }
        if ( !in_array($type,$extentions) ) { header('Location: cat.php?cat='.$cat['id'].'&type'); exit(); }
        if ( !in_array($type2,$extentionssc) ) { header('Location: cat.php?cat='.$cat['id'].'&typesc'); exit(); }
        if ( empty($name) ) { header('Location: cat.php?cat='.$cat['id'].'&emptyn'); exit(); }
        if ( empty($info) ) { header('Location: cat.php?cat='.$cat['id'].'&emptyi'); exit(); }
        if ( strlen($info) > 500 ) { header('Location: cat.php?cat='.$cat['id'].'&info'); exit(); }
        if ( strlen($name) > 50 ) { header('Location: cat.php?cat='.$cat['id'].'&name'); exit(); }
        
        if (!copy($file, "files/".$new_name))  { header('Location: cat.php?cat='.$cat['id'].'&nofile'); exit(); }
        if (!copy($screen, "screen/".$new_namesc))  { header('Location: cat.php?cat='.$cat['id'].'&noscreen'); exit(); }
        
        echo '<b>Result:</b><br/>';
        echo 'Файл: '.$new_name.'<br/ >';
        echo 'Название: '.$name.'<br/ >';
        echo 'Описание: '.$info.'<br/ >';
        echo 'Каталог: '.$pcat.'<br/ >';
        echo 'Подкаталог: '.$pcat.'<br/ >';
        echo 'Каталог: '.$id.'<br/ >';
        mysql_query("UPDATE `profile` SET `many` = '".($many_session+5)."' WHERE `id` = '".$id_session."'");
        $res = mysql_query("INSERT INTO `fo_fil` SET `name` = '".html($name)."', `info` = '".html($info)."', `cat` = '".num($id)."', `pcat` = '".num($pcat)."', `file` = '".html($new_name)."', `screen` = '".html($new_namesc)."', `date` = '".time()."', `user` = '".$id_session."' ");
        
        
        $last = mysql_insert_id();
        
        if ( $res == NULL ) header('Location: cat.php?cat='.$cat['id'].'&nofile'); else header('Location: view.php?file='.$last.'&upload');
        
        echo $res;
    }
    
    
    
}

$a = mysql_result(mysql_query("SELECT count(*) FROM `fo_pcat` WHERE `cat` = '".$id."'"), 0);
if ( $a == 0 ) {echo 'для загрузки файла требуется создать подкаталоги';  exit();}

$result = mysql_query("SELECT * FROM `fo_pcat` WHERE `cat` = '".$id."'");

echo head . "<b>Обменник файлов / ".$cat['name']." / Загрузка файла" . div . katalog;

echo '<form action="upload.php?cat='.$id.'" method="post" name="form" enctype="multipart/form-data">';


print '<b>Выберите файл:</b> [max. 25 mb]<br />
<input type="file" name="file"><br/ >';

if ( $cat['tip'] != 1 ) {
print '<b>Скриншот:</b> [max. 100 kb]<br />
<input type="file" name="screen"><br/ ><br/ >';
}


echo '<b>Подраздел:</b><br/><select name="pcat">';
while($pcat = mysql_fetch_assoc($result))
echo '<option value="'.$pcat['id'].'">'.$pcat['name'].'</option>';
echo '</select><br/><br/>';

echo '<b>Сохранить как: </b><small>[max. 50]</small><br/ > <input name="name" type="text" maxlength="50" /><br/ ><br/ >';

echo '<b>Описание: </b><small>[max. 500]</small><textarea id="tarea" name="info" cols="30" rows="5" style="width: 99%;"></textarea><br/><br/ >';
   
echo '<input name="submit" type="submit" class="button" value="Загрузить" /></form>'; 




echo div;
/////////////////////////////////////////

navig();
foot();
?>