<?php

define('IN_PHPBB', true);
$phpbb_root_path = './';
include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.'.$phpEx);
include($phpbb_root_path . 'includes/functions_obmen.'.$phpEx);
error_reporting(0);
$userdata = session_pagestart($user_ip, PAGE_OBMEN);
init_userprefs($userdata);
$sql = "SELECT * FROM `" . UPLOAD_CONFIG_TABLE . "` WHERE `id`='1'";
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_config information', '', __LINE__, __FILE__, $sql);
	}
		$config = $db->sql_fetchrow($result);
if($config['on_off']==1)
    {
        message_die(GENERAL_MESSAGE, 'Файлобменник отключен!');
    }
$id = isset($_REQUEST['id']) ? abs(intval($_REQUEST['id'])) : false;
$u = isset($_REQUEST['u']) ? abs(intval($_REQUEST['u'])) : false;
$mod = isset($_REQUEST['mod']) ? trim($_REQUEST['mod']) : false;
switch($mod)
	{
case'catalog':
	$sql = "SELECT*FROM `" . UPLOAD_RAZD_TABLE . "` WHERE `raz_id`='".$id."'";
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_razd information', '', __LINE__, __FILE__, $sql);
	}
if(!mysql_num_rows($result))
    {
        message_die(GENERAL_MESSAGE, 'Раздел не существует!');
    }
$catalog = $db->sql_fetchrow($result);
if ($catalog['raz_login'])
    {
if ( !$userdata['session_logged_in'] )
	{
        message_die(GENERAL_MESSAGE, 'Вы не авторизированы!');	
	}
	}
	$sql = "SELECT*FROM `" . UPLOAD_CATS_TABLE . "` WHERE `cat_rid`='".$catalog['raz_id']."'";
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_razd information', '', __LINE__, __FILE__, $sql);
	}
if(!mysql_num_rows($result))
    {
	    message_die(GENERAL_MESSAGE, 'В этои разделе нет категорий!');
	}
$page_title = text_obmen($catalog['raz_name']);
define('SHOW_ONLINE', true);
include($phpbb_root_path . 'includes/page_header.'.$phpEx);
$template->set_filenames(array('catalog' => 'obmen_catalog_body.tpl'));
	$sql = "SELECT * FROM `" . UPLOAD_CATS_TABLE . "` WHERE `cat_rid`='".$catalog['raz_id']."' ORDER BY `cat_name` ASC";
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_cats information', '', __LINE__, __FILE__, $sql);
	}
while ($categories = $db->sql_fetchrow($result))
	{
		$categories_link = ($categories['cat_ero']==0 OR $catalog['raz_ero']==1) ? append_sid("obmen.$phpEx?mod=listfiles&amp;id=".$categories['cat_id']) : append_sid("obmen.$phpEx?mod=erocat&amp;id=".$categories['cat_id']);
		$categories_name = text_obmen($categories['cat_name']);
		$categories_count = $categories['cat_count'];
		$categories_size = fsize($categories['cat_size']);
		$categories_description = obmen_smiles(text_obmen($categories['cat_description']));
$template->assign_block_vars('categories_row', array(
        'CATEGORIES_LINK' => $categories_link,
		'CATEGORIES_NAME' => $categories_name,
		'CATEGORIES_COUNT' => $categories_count,
		'CATEGORIES_SIZE' => $categories_size
	));	
    if(!empty($categories['cat_description']))
	{
	    $categories_description = obmen_smiles(text_obmen($categories['cat_description']));
$template->assign_block_vars('categories_row.description', array(
	    'CATEGORIES_DESCRIPTION' => $categories_description
	));
    }
	} 

        $razdel_name = text_obmen($catalog['raz_name']);
		$upload_link = append_sid("obmen.$phpEx?mod=upload&amp;id=".$catalog['raz_id']);
$template->assign_vars(array(
	    'RAZDEL_NAME' => $razdel_name,
		'UPLOAD_LINK' => $upload_link
    ));
$template->pparse('catalog');	

break;

case'eroraz':

	$sql = "SELECT*FROM `" . UPLOAD_RAZD_TABLE . "` WHERE `raz_id`='".$id."'";
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_razd information', '', __LINE__, __FILE__, $sql);
	}
if(!mysql_num_rows($result))
    {
        message_die(GENERAL_MESSAGE, 'Раздел не существует!');
    }
$razdel = $db->sql_fetchrow($result);
if(!$razdel['raz_ero'])
    {
	    message_die(GENERAL_MESSAGE, 'Раздел не существует!');
	}
	$sql = "SELECT*FROM `" . UPLOAD_CATS_TABLE . "` WHERE `cat_rid`='".$razdel['raz_id']."'";
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_cat information', '', __LINE__, __FILE__, $sql);
	}
if(!mysql_num_rows($result))
    {
	    message_die(GENERAL_MESSAGE, 'В этом разделе нет категорий!');
	}

$page_title = text_obmen($razdel['raz_name']);
define('SHOW_ONLINE', true);
include($phpbb_root_path . 'includes/page_header.'.$phpEx);
$template->set_filenames(array('eroraz' => 'obmen_ero_body.tpl'));

        $name = text_obmen($razdel['raz_name']);
		$link = append_sid("obmen.$phpEx?mod=catalog&amp;id=".$razdel['raz_id']);
		$exit = append_sid("obmen.$phpEx");
$template->assign_vars(array(
	    'NAME' => $name,
		'LINK' => $link,
		'EXIT' => $exit
    ));
$template->pparse('eroraz');	

break;

case'erocat':

	$sql = "SELECT*FROM `" . UPLOAD_CATS_TABLE . "` WHERE `cat_id`='".$id."'";
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_cats information', '', __LINE__, __FILE__, $sql);
	}
if(!mysql_num_rows($result))
    {
        message_die(GENERAL_MESSAGE, 'Категория не существует!');
    }
$catalog = $db->sql_fetchrow($result);
if(!$catalog['cat_ero'])
    {
	    message_die(GENERAL_MESSAGE, 'Категория не существует!');
	}
	$sql = "SELECT*FROM `" . UPLOAD_FILES_TABLE . "` WHERE `file_cid`='".$catalog['cat_id']."'";
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_files information', '', __LINE__, __FILE__, $sql);
	}
if(!mysql_num_rows($result))
    {
	    message_die(GENERAL_MESSAGE, 'В этой категории нет файлов!');
	}

$page_title = text_obmen($catalog['cat_name']);
define('SHOW_ONLINE', true);
include($phpbb_root_path . 'includes/page_header.'.$phpEx);
$template->set_filenames(array('erocat' => 'obmen_ero_body.tpl'));

        $name = text_obmen($catalog['cat_name']);
		$link = append_sid("obmen.$phpEx?mod=listfiles&amp;id=".$catalog['cat_id']);
		$exit = append_sid("obmen.$phpEx?mod=catalog&amp;id=".$catalog['cat_rid']);
$template->assign_vars(array(
	    'NAME' => $name,
		'LINK' => $link,
		'EXIT' => $exit
    ));
$template->pparse('erocat');	

break;

case'upload':

if ( !$userdata['session_logged_in'] )
	{
        message_die(GENERAL_MESSAGE, 'Вы не авторизированы!');	
	}
	$sql = "SELECT*FROM `" . UPLOAD_RAZD_TABLE . "` WHERE `raz_id`='".$id."'";
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_razd information', '', __LINE__, __FILE__, $sql);
	}
if(!mysql_num_rows($result))
    {
        message_die(GENERAL_MESSAGE, 'Раздел не существует!');
    }
$razdel = $db->sql_fetchrow($result);
	$sql = "SELECT*FROM `" . UPLOAD_CATS_TABLE . "` WHERE `cat_rid`='".$razdel['raz_id']."'";
if ( !($categories = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_cats information', '', __LINE__, __FILE__, $sql);
	}
if(!mysql_num_rows($categories))
    {
	    message_die(GENERAL_MESSAGE, 'Загрузка файла невозможна, в этом разделе нет категорий!');
	}
if($config['upload_locked'])
    {
	    message_die(GENERAL_MESSAGE, 'Загрузка файла отключена администратором!');	
	}
if($razdel['raz_level']==1)
    {
if($userdata['user_level']!=ADMIN && $userdata['obmen_level']!=1)
    {
	    message_die(GENERAL_MESSAGE, 'Загружать файлы в данный раздел могут только члены администрации!');	
	}
	}
		
if(isset($_POST['submit']))
    {

        $array = explode(',',$razdel['raz_ext']);
        $ext = array();
        $i = 0;
while($i < count($array))
    {
        $ext[$i] = trim($array[$i]);
        $i++;
    }
$error = array();

	$sql = "SELECT*FROM `" . UPLOAD_CATS_TABLE . "` WHERE `cat_rid`='".$razdel['raz_id']."' AND `cat_id`='".abs(intval($_POST['cat_id']))."'";
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_cats information', '', __LINE__, __FILE__, $sql);
	}
if(!mysql_num_rows($result))
    {
        $error['cat_id'] = 'Вы хотите загрузить файл в несуществующую категорию!';
    }
if(empty($_FILES['file']['name']))
    {
        $error['nofile'] = 'Вы не выбрали файл';
    }

if($_FILES['file']['size']>($config['max_file_size']*1048576))
    {
        $error['maxfile'] = 'Размер файла превышает допустимый!';	
	}
	
if(!$error)
    {
        $size = $_FILES['file']['size'];
        $name = $_FILES['file']['name'];
        $file = strtolower($_FILES['file']['name']);
        $ext_file = ext_file($file);

if(!in_array($ext_file,$ext))
{
$error['ext'] = 'Недопустимое расширение файла! Разрешено выгружать файлы с расширением '.implode(', ', $ext);
}
$marcer_file = preg_replace('/^\/?(.*?)\/?$/', '\1', trim($board_config['server_name'])).'_';
if(preg_match("/[^0-9a-z\-\.\_]+/i", $file))
{
$file = $marcer_file.substr(md5(time()), 0, 6).'.'.$ext_file;
}

if(file_exists('upload/'.$file))
{
$file = $marcer_file.substr(md5(time()), 0, 6).'.'.$ext_file;
}

if(!$error)
{
if(!move_uploaded_file($_FILES['file']['tmp_name'], 'upload/'.$file))
{
$error['upload'] = 'Ошибка загрузки файла!';
}
}

$pfile = false;
if(!empty($_FILES['screen']['name']))
{
$pfile = strtolower($_FILES['screen']['name']);
$pext_file = ext_file($pfile);
$pext = array('jpg','gif','jpeg','png');

if(!in_array($pext_file,$pext))
{
$error['pext'] = 'Недопустимое расширение скриншота! Разрешено выгружать скриншоты с расширением '.implode(', ', $pext);
}

$pfile = 'screen_'.$file.'.'.$pext_file;

if(!$error)
{
if(!move_uploaded_file($_FILES['screen']['tmp_name'], 'screen/'.$pfile))
{
$error['screen'] = 'Ошибка загрузки скриншота';
}
}
}
}


if(!empty($error))
{
message_die(GENERAL_ERROR, implode('<br/>',$error));
}
else
{
chmod('upload/'.$file, 0777);
if($pfile)
{
chmod('screen/'.$pfile, 0777);
}

	$sql = "INSERT INTO `" . UPLOAD_FILES_TABLE . "` SET `file_text`='".mysql_real_escape_string(trim($_POST['text']))."', `file_url`='".$file."', `file_user_id`='".$userdata['user_id']."', `file_name`='".mysql_real_escape_string(trim($name))."', `file_time`='".time()."', `file_cid`='".abs(intval($_POST['cat_id']))."', `file_rid`='".$razdel['raz_id']."', `file_size`='".$size."', `file_screen`='".$pfile."'";
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_files information', '', __LINE__, __FILE__, $sql);
	}

$fid = mysql_insert_id();
$sql = "UPDATE `" . USERS_TABLE . "` SET `upload_count`=`upload_count`+1, `upload_size`=`upload_size`+".$size.", `user_points`=`user_points`+".$razdel['raz_points']." WHERE `user_id` = '".$userdata['user_id']."'";
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get users information', '', __LINE__, __FILE__, $sql);
	}

	$sql = "UPDATE `" . UPLOAD_RAZD_TABLE . "` SET `raz_count`=`raz_count`+1, `raz_size`=`raz_size`+".$size." WHERE `raz_id`='".$razdel['raz_id']."'";
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_razd information', '', __LINE__, __FILE__, $sql);
	}

	$sql = "UPDATE `" . UPLOAD_CATS_TABLE . "` SET `cat_count`=`cat_count`+1, `cat_size`=`cat_size`+".$size." WHERE `cat_id`='".abs(intval($_POST['cat_id']))."'";
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_cats information', '', __LINE__, __FILE__, $sql);
	}
		$file_link = append_sid("obmen.$phpEx?mod=file&amp;id=".$fid); 
		$upload_link = append_sid("obmen.$phpEx?mod=upload&amp;id=".$razdel['raz_id']."&amp;".rand(0,99));
        message_die(GENERAL_MESSAGE, 'Файл успешно добавлен!<br/>- <a href="'.$file_link.'">Перейти к файлу</a><br/>- <a href="'.$upload_link.'">Загрузить еще</a>');
    }
    }
$page_title = 'Загрузка файла';
define('SHOW_ONLINE', true);
include($phpbb_root_path . 'includes/page_header.'.$phpEx);
$template->set_filenames(array('upload' => 'obmen_upload_body.tpl'));
while ($cat = $db->sql_fetchrow($categories))
	{
        $cat_name = text_obmen($cat['cat_name']);
		$cat_id = $cat['cat_id'];
    $template->assign_block_vars('cat_row', array(
        'CAT_ID' => $cat_id,
		'CAT_NAME' => $cat_name,
	));	
	}
        $dext = $razdel['raz_ext'];
		$razdel_name = text_obmen($razdel['raz_name']);
		$razdel_link = append_sid("obmen.$phpEx?mod=catalog&amp;id=".$razdel['raz_id']);
		$upload_link = append_sid("obmen.$phpEx?mod=upload&amp;id=".$razdel['raz_id']);
    $template->assign_vars(array(
	    'DEXT' => $dext,
		'RAZDEL_NAME' => $razdel_name,
		'RAZDEL_LINK' => $razdel_link,
		'UPLOAD_LINK' => $upload_link
    ));
$template->pparse('upload');
break;

case 'listfiles':
	$sql = "SELECT*FROM `" . UPLOAD_CATS_TABLE . "` WHERE `cat_id`='".$id."'";
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_cats information', '', __LINE__, __FILE__, $sql);
	}
if(!mysql_num_rows($result))
    {
        message_die(GENERAL_MESSAGE, 'Категория не существует!');
    }
$catalog = $db->sql_fetchrow($result);
if ($catalog['cat_login'])
    {
if ( !$userdata['session_logged_in'] )
	{
        message_die(GENERAL_MESSAGE, 'Вы не авторизированы!');	
	}
	}
$all = mysql_result(mysql_query("SELECT COUNT(*) FROM `" . UPLOAD_FILES_TABLE . "` WHERE `file_cid`='".$catalog['cat_id']."';"),0);
if(!$all)
    {
	    message_die(GENERAL_MESSAGE, 'В этой категории нет файлов!');
	}
$page_title = text_obmen($catalog['cat_name']);
define('SHOW_ONLINE', true);
include($phpbb_root_path . 'includes/page_header.'.$phpEx);
if ( isset($HTTP_POST_VARS['start1']) )
    {
	    $start1 = abs(intval($HTTP_POST_VARS['start1']));
		$start1 = ($start1) ? $start1 : 1;
	    $start = (($start1 - 1) * $board_config['posts_per_page']);
    } 
	else 
	{
	    $start = ( isset($HTTP_GET_VARS['start']) ) ? intval($HTTP_GET_VARS['start']) : 0;
	    $start = ($start < 0) ? 0 : $start;
    }
$template->set_filenames(array('listfiles' => 'obmen_listfiles_body.tpl'));
	$sql = "SELECT*FROM `" . UPLOAD_FILES_TABLE . "` WHERE `file_cid`='".$catalog['cat_id']."' ORDER BY `file_time` DESC LIMIT " . $start . ", " . $board_config[posts_per_page];
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_files information', '', __LINE__, __FILE__, $sql);
	}

while ($file = $db->sql_fetchrow($result))
	{

        $ext = ext_file($file['file_url']);
switch($ext)
    {
case 'png':
case 'gif':
case 'jpeg':
case 'jpg':
$screen = '<img src="picture/40/40/'.$file['file_url'].'" alt="" />';
break;

case '3gp':
case 'avi':
case 'mp4':
if(class_exists('ffmpeg_movie'))
{
$screen = '<img src="video/small/'.$file['file_id'].'.gif" alt="">';
}
else
{
$screen = '<img src="images/file.gif" alt="" />';
}
break;

default:
if(!empty($file['file_screen']))
    {
        $screen = '<img src="screenshot/small/'.$file['file_screen'].'" alt="" />';
    }
    else
    {
        $screen = '<img src="images/file.gif" alt="" />';
    }
    }
        $file_link = append_sid("obmen.$phpEx?mod=file&amp;id=".$file['file_id']);
        $file_name = text_obmen($file['file_name']);
        $file_size = fsize($file['file_size']);
        $file_text = mb_substr(text_obmen($file['file_text']), 0, 60);       
$template->assign_block_vars('file_row', array(
        'FILE_LINK' => $file_link,
		'FILE_NAME' => $file_name,
		'FILE_SIZE' => $file_size,
		'FILE_TEXT' => $file_text,
		'FILE_SCREEN' => $screen
    ));		
    }
	$sql = "SELECT * FROM `" . UPLOAD_RAZD_TABLE . "` WHERE `raz_id` = '".$catalog['cat_rid']."'";
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_razd information', '', __LINE__, __FILE__, $sql);
	}
		$razdel = $db->sql_fetchrow($result);	
		$raz_link = append_sid("obmen.$phpEx?mod=catalog&amp;id=".$razdel['raz_id']);
		$raz_name = text_obmen($razdel['raz_name']);		
        $name = text_obmen($catalog['cat_name']);
		$pagination = ($all>$board_config['posts_per_page']) ? generate_pagination("obmen.$phpEx?mod=listfiles&amp;id=$catalog[cat_id]", $all, $board_config['posts_per_page'], $start) : '';
$template->assign_vars(array(
		'RAZ_LINK' => $raz_link,
		'RAZ_NAME' => $raz_name,
	    'NAME' => $name,
		'PAG' => $pagination
    ));
$template->pparse('listfiles');	

break;
case 'file':
	$sql = "SELECT*FROM `" . UPLOAD_FILES_TABLE . "` WHERE `file_id`='".$id."'";
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_files information', '', __LINE__, __FILE__, $sql);
	}
if(!mysql_num_rows($result))
    {
        message_die(GENERAL_MESSAGE, 'Файл не существует!');
    }
$last = $db->sql_fetchrow($result);
	$sql = "SELECT * FROM `" . UPLOAD_CATS_TABLE . "` WHERE `cat_id` = '".$last['file_cid']."'";
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_cats information', '', __LINE__, __FILE__, $sql);
	}
		$cat = $db->sql_fetchrow($result);	
if ($cat['cat_login'])
    {
if ( !$userdata['session_logged_in'] )
	{
        message_die(GENERAL_MESSAGE, 'Вы не авторизированы!');	
	}
	}
if ( isset($HTTP_POST_VARS['start1']) )
    {
	    $start1 = abs(intval($HTTP_POST_VARS['start1']));
		$start1 = ($start1) ? $start1 : 1;
	    $start = (($start1 - 1) * $board_config['posts_per_page']);
    } 
	else 
	{
	    $start = ( isset($HTTP_GET_VARS['start']) ) ? intval($HTTP_GET_VARS['start']) : 0;
	    $start = ($start < 0) ? 0 : $start;
    }	
$page_title = text_obmen($last['file_name']);
define('SHOW_ONLINE', true);
include($phpbb_root_path . 'includes/page_header.'.$phpEx);
$template->set_filenames(array('file' => 'obmen_file_body.tpl'));

$ext = ext_file($last['file_url']);
$jad = false;

switch($ext)
	{
case 'jar':
		$jad = '<img src="images/upload_mini.gif" alt="" /> <a href="jad/'.$last['file_id'].'/'.$last['file_url'].'.jad">Скачать JAD</a><br/>';

if(!empty($last['file_screen']))
	{
		$screen = '<img src="screenshot/normal/'.$last['file_screen'].'" alt="" />';
	}
	else
	{
		$screen = false;
	}

		$manifest = file_get_contents("zip://upload/".$last['file_url']."#META-INF/MANIFEST.MF");

if(preg_match('/(messaging|sms|send|connector\.sms)/i',strtolower($manifest)))
	{
		$sms = '<span style="color: #FF0000;"><b>Внимание:</b> файл содержит функцию отправки SMS.</span><br/>';
	}
	else
	{
		$sms = false;
	}

preg_match("|MIDlet-Vendor: (.*?)\n|",$manifest,$vendor);
		$ven = 'Разработчик: '.$vendor[1].'<br/>';

preg_match("|MIDlet-Version: (.*?)\n|",$manifest,$version);
		$ver = 'Версия: '.$version[1].'<br/>';
$template->assign_block_vars('java', array(
	    'SCREEN' => $screen,
		'SMS' => $sms,
		'VEN' => $ven,
		'VER' => $ver
	));		
break;

case 'png':
case 'gif':
case 'jpeg':
case 'jpg':
		$info = getimagesize('upload/'.$last['file_url']);
		$screen = '<img src="picture/normal/'.$last['file_url'].'" alt="" /><br/>';
		$original = '<a href="picture/'.$info[0].'/'.$info[1].'/'.$last['file_url'].'">'.$info[0].'x'.$info[1].'</a><br/>';
$template->assign_block_vars('images', array(
        'SCREEN' => $screen,
		'ORIGINAL' => $original
	));
$px = array('128|128','128|160','130|130','132|176','176|208','176|220','208|208','240|320','320|240','352|416');
$i = 0;
$c = count($px);
while($i < $c)
	{
		$arr = explode('|',$px[$i]);
		$select_size = '<a href="picture/'.$arr[0].'/'.$arr[1].'/'.$last['file_url'].'">'.$arr[0].'x'.$arr[1].'</a>'.($c!=($i+1) ? ', ' : '<br/>');
		++$i;
$template->assign_block_vars('images.select_size', array(
		'SELECT_SIZE' => $select_size
	));
	}
break;

case 'mp3':
case 'wav':
if (class_exists('ffmpeg_movie'))
{
$media = new ffmpeg_movie('upload/'.$last['file_url']);
if (intval($media->getDuration())>3599)
{
$length=intval($media->getDuration()/3600).":".date('s',fmod($media->getDuration()/60,60)).":".date('s',fmod($media->getDuration(),3600));
}
elseif (intval($media->getDuration())>59)
{
$length=intval($media->getDuration()/60).":".date('s',fmod($media->getDuration(),60));
}
else
{
$length= intval($media->getDuration())." сек";
$bitrate=ceil(($media->getBitRate())/1024).' KBPS';
}
}
else
{
$length='Неизвестно';
$bitrate='Неизвестно';
}
if(!empty($last['file_screen']))
	{
		$screen = '<img src="screenshot/normal/'.$last['file_screen'].'" alt="" /><br/>';
	}
	else
	{
		$screen = false;
	}
if(!isset($_GET['player']))
	{
		$link_player = append_sid("obmen.$phpEx?mod=file&amp;id=".$last['file_id']."&amp;player");
		$player = '<a href="'.$link_player.'">Слушать онлайн</a> (только с компа)';
	}
	else
	{
	$player = '';
	}
$template->assign_block_vars('sounds', array(
		'SCREEN' => $screen,
		'LENGTH' => $length,
		'BITRATE' => $bitrate,		
		'PLAYER' => $player
	));
if(isset($_GET['player']))
	{
		$audio_url = 'upload/'.$last['file_url'];
$template->assign_block_vars('sounds.player', array(
		'AUDIO_URL' => $audio_url
	));
	}	
	
break;

case '3gp':
case 'avi':
case 'mp4':
if(class_exists('ffmpeg_movie'))
	{
		$screen = '<img src="video/normal/'.$last['file_id'].'.gif" alt=""><br/>';
		$movie = new ffmpeg_movie('upload/'.$last['file_url']);
		$fps = $movie->getFrameRate();
		$fps = strtok($fps,'.');
		$fh = $movie->getFrameHeight();
		$fw = $movie->getFrameWidth();
		$codec = $movie->getVideoCodec();
		$mute = $movie->hasAudio();
		$duration = $movie->getDuration();
		$duration = strtok($duration,'.');
		$minutes = 0;

if($duration > 60)
	{
		$minutes = floor($movie->getDuration()/60);
		$seconds = $movie->getDuration()%60;
	}
if($minutes < 1)
	{
		$dlit = $duration.' секунд.';
	}
	else
	{
		$dlit = $minutes.' мин '.$seconds.' секунд.';
	}
		$razr = $fw.'x'.$fh;
		$codec = $codec;
if(!$mute)
	{
		$sound = 'нету';
	}
	else
	{
		$sound = 'есть';
	}
	$fps = $fps.' кадров/сек';
$template->assign_block_vars('videos', array(
        'SCREEN' => $screen,
		'DLIT' => $dlit,
        'RAZR' => $razr,
        'CODEC' => $codec,
        'SOUND' => $sound,
        'FPS' => $fps		
	));
	}
break;

default:
if(!empty($last['file_screen']))
	{
		$screen = '<img src="screenshot/normal/'.$last['file_screen'].'" alt="" /><br/>';
	}
	else
	{
		$screen = false;
	}
$template->assign_block_vars('default', array(
        'SCREEN' => $screen
	));
	}

if(!empty($last['file_text']))
	{
		$desc = obmen_smiles(text_obmen($last['file_text']));
	}
	else
	{
		$desc = 'Описание отсутствует';
	}		
	$sql = "SELECT * FROM `" . USERS_TABLE . "` WHERE `user_id` = '".$last['file_user_id']."'";
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get users information', '', __LINE__, __FILE__, $sql);
	}
if(mysql_num_rows($result))
	{	
		$user = $db->sql_fetchrow($result);
		$file_user_link = append_sid("profile.$phpEx?mode=viewprofile&amp;u=".$user['user_id']);
		$file_username = $user['username'];
	}
	else
	{
		$file_user_link = append_sid("profile.$phpEx?mode=viewprofile&amp;u=-1");
		$file_username = Anonymous;	
	}
		$time = create_date($board_config['default_dateformat'], $last['file_time'], $board_config['board_timezone']);
		$file_rating = $last['file_rating'];
		$file_load = $last['file_load'];
		$file_plus = $last['file_plus'];
		$file_minus = $last['file_minus'];
		$file_comments = $last['file_comments'];
		$rating_plus = append_sid("obmen.$phpEx?mod=rating&amp;id=".$last['file_id']."&amp;plus");
		$rating_minus = append_sid("obmen.$phpEx?mod=rating&amp;id=".$last['file_id']."&amp;minus");
		$load_link = 'loading/'.$last['file_id'].'/'.$last['file_url'];
		$file_size = fsize($last['file_size']);
		$file_abuse_link = append_sid("obmen.$phpEx?mod=abuse&amp;id=".$last['file_id']);
		$file_abuse = $last['file_abuse'];
		$cat_link = append_sid("obmen.$phpEx?mod=listfiles&amp;id=".$cat['cat_id']);
		$cat_name = text_obmen($cat['cat_name']);	
		$file_name = text_obmen($last['file_name']);
		$addcomment = append_sid("obmen.$phpEx?mod=addcomment&amp;id=".$last['file_id']);
		$pagination = ($file_comments>$board_config['posts_per_page']) ? generate_pagination("obmen.$phpEx?mod=file&amp;id=$last[file_id]", $file_comments, $board_config['posts_per_page'], $start) : '';		
$template->assign_vars(array(
	    'FILE_USER_LINK' => $file_user_link,
		'FILE_USERNAME' => $file_username,
	    'TIME' => $time,
		'FILE_RATING' => $file_rating,
	    'FILE_LOAD' => $file_load,
		'FILE_PLUS' => $file_plus,
	    'FILE_MINUS' => $file_minus,
		'RATING_PLUS' => $rating_plus,
	    'RATING_MINUS' => $rating_minus,
		'LOAD_LINK' => $load_link,	
		'FILE_SIZE' => $file_size,
		'DESC' => $desc,
		'FILE_ABUSE_LINK' => $file_abuse_link,
		'FILE_ABUSE' => $file_abuse,
		'CAT_LINK' => $cat_link,
		'CAT_NAME' => $cat_name,	
		'FILE_NAME' => $file_name,		
		'FILE_COMMENTS' => $file_comments,
		'PAG' => $pagination,
		'ADDCOMMENT' => $addcomment,
		'JAD' => $jad		
    ));
	$sql = "SELECT c.*, u.* FROM " . UPLOAD_COMMENTS_TABLE . " c, ".USERS_TABLE." u
			WHERE c.comment_fid = ".$last['file_id']." AND c.comment_user_id = u.user_id 
			ORDER BY c.comment_time DESC 
			LIMIT $start, ".$board_config['posts_per_page'];
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get information', '', __LINE__, __FILE__, $sql);
	}
		while ($comment = $db->sql_fetchrow($result))
		{
			
			$comment_id = $comment['user_id'];
			$comment_username = $comment['username'];
			$comment_profile = append_sid("profile.$phpEx?mode=viewprofile&amp;u=".$comment_id);
			$comment_time = create_date($board_config['default_dateformat'], $comment['comment_time'], $board_config['board_timezone']);
			$comment_text = obmen_smiles(text_obmen($comment['comment_text']));
	if( $userdata['user_on_off'] == 1)
	 {
		if ($comment['user_session_time'] >= (time()-$board_config['online_time']))
		{
			if ($comment['user_allow_viewonline'])
			{
				$online_status = '<span' . $online_color . '>' . $lang['Online'] . '</span>';
			}
			else if ( $is_auth['auth_mod'] || $userdata['user_id'] == $poster_id )
			{
				$online_status = '<span' . $hidden_color . '>' . $lang['Hidden'] . '</span>';
			}
			else
			{
				$online_status = '<span' . $offline_color . '>' . $lang['Offline'] . '</span>';
			}
		}
		else
		{
			$online_status = '<span' . $offline_color . '>' . $lang['Offline'] . '</span>';
		}
	 } else {
			$online_status = '';
	 }
if($num==1)
	{
		$row_class = 'row_hard';
		$num=0;
	}
	else
	{
		$row_class = 'row_easy';
		$num=1;
	}
		if($userdata['session_logged_in'])
		{
		$otv = append_sid("obmen.$phpEx?mod=addcomment&amp;id=".$last['file_id']."&amp;uid=".$comment['user_id']);
		$otv = '<a href="'.$otv.'">Отв</a>';
		}
		else
		{
		$otv = '';
		}
		if($comment_id == $userdata['user_id'] || $userdata['user_level'] == ADMIN || $userdata['obmen_level'] == 1)
		{
		$del_comments = append_sid("obmen.$phpEx?mod=delcomment&amp;id=".$comment['comment_id']);	
		$del_comments = '|<a href="'.$del_comments.'">Уд</a>';
		}
		else
		{
		$del_comments = '';
		}
		$template->assign_block_vars('comments', array(
			'COMMENT_TEXT' => $comment_text,
			'COMMENT_TIME' => $comment_time,
			'COMMENT_USERNAME' => $comment_username,
			'COMMENT_PROFILE' => $comment_profile,
		    'COMMENT_ONLINE_STATUS' => $online_status,
            'OTV' => $otv,
            'ROW_CLASS' => $row_class,			
			'DEL' => $del_comments
			));
	}
if(!$file_comments)
	{
$template->assign_block_vars('no_comments', array(
	));
	}
if($last['file_user_id'] == $userdata['user_id'] || $userdata['user_level'] == ADMIN || $userdata['obmen_level'] == 1)
		{	
	$editfile = append_sid("obmen.$phpEx?mod=editfile&amp;id=".$last['file_id']);
	$delfile = append_sid("obmen.$phpEx?mod=delfile&amp;id=".$last['file_id']);	
$template->assign_block_vars('edit_file', array(
		'EDITFILE' => $editfile,			
		'DELFILE' => $delfile
	));
		}
$template->pparse('file');	
break;

case 'addcomment':
if ( !$userdata['session_logged_in'] )
	{
        message_die(GENERAL_MESSAGE, 'Вы не авторизированы!');
	}	
	$sql = "SELECT*FROM `" . UPLOAD_FILES_TABLE . "` WHERE `file_id`='".$id."'";
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_files information', '', __LINE__, __FILE__, $sql);
	}
if(!mysql_num_rows($result))
    {
        message_die(GENERAL_MESSAGE, 'Файл не существует!');
    }
$last = $db->sql_fetchrow($result);
if(isset($_POST['submit']))
{
$sql = "SELECT * FROM `" . UPLOAD_COMMENTS_TABLE . "` WHERE `comment_user_id` = '".$userdata['user_id']."' AND `comment_fid` = '".$last['file_id']."' ORDER BY `comment_time` DESC LIMIT 1";
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Error', '', __LINE__, __FILE__, $sql);
	}
	$antiflud = $db->sql_fetchrow($result);
if ($antiflud != NULL && time()<$antiflud['comment_time']+5)
{
message_die(GENERAL_MESSAGE, 'Не стоит так часто!');
}
$msg=$_POST['comment_text'];
if (strlen2($msg)<2)
{
message_die(GENERAL_MESSAGE, 'Комментарий слишком короткий!');
}
$msg = mysql_real_escape_string(trim($msg));
$sql = "INSERT INTO `" . UPLOAD_COMMENTS_TABLE . "` (`comment_user_id`, `comment_time`, `comment_text`, `comment_fid`) values('".$userdata['user_id']."', '".time()."', '$msg', '".$last['file_id']."')";
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get  information', '', __LINE__, __FILE__, $sql);
	}
$sql = "UPDATE `" . UPLOAD_FILES_TABLE . "` SET `file_comments` = `file_comments`+1 WHERE `file_id` = '".$last['file_id']."' LIMIT 1";
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get  information', '', __LINE__, __FILE__, $sql);
	}
$sql = "UPDATE `" . USERS_TABLE . "` SET `comments_count` = `comments_count`+1 WHERE `user_id` = '".$last['file_user_id']."' LIMIT 1";
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get  information', '', __LINE__, __FILE__, $sql);
	}	
		$URL = append_sid("obmen.$phpEx?mod=file&id=".$last['file_id']);
		header ("Location: $URL");	
}
$page_title = 'Добавление комментария';
define('SHOW_ONLINE', true);
include($phpbb_root_path . 'includes/page_header.'.$phpEx);
$template->set_filenames(array('addcomment' => 'obmen_addcomment_body.tpl'));
	if(isset($_GET['uid']))
	{
		$for = intval(abs($_GET['uid']));
		$sql = "SELECT username 
				FROM ".USERS_TABLE."
				WHERE user_id = $for";
				$result = $db->sql_query($sql);
		if (!$result)
		{
			message_die(GENERAL_ERROR, 'Kiss my ass');
		}
		$for_him = $db->sql_fetchrow($result);
		if(!empty($for_him['username']))
		{
			$for_you = $for_him['username'].', ';
		}
		else
		{
			$for_you = '';
		}
	}
$file_link = append_sid("obmen.$phpEx?mod=file&id=".$last['file_id']);
$file_name = text_obmen($last['file_name']);	
$addcomments = append_sid("obmen.$phpEx?mod=addcomment&amp;id=".$last['file_id']);
	    $template->assign_vars(array(
		'FOR_YOU' => $for_you,
		'FILE_LINK' => $file_link,
		'FILE_NAME' => $file_name,
	    'ADDCOMMENTS' => $addcomments
		));
$template->pparse('addcomment');		
break;

case 'delcomment':
if ( !$userdata['session_logged_in'] )
	{
        message_die(GENERAL_MESSAGE, 'Вы не авторизированы!');
	}	
	$sql = "SELECT*FROM `" . UPLOAD_COMMENTS_TABLE . "` WHERE `comment_id`='".$id."'";
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_comment information', '', __LINE__, __FILE__, $sql);
	}
if(!mysql_num_rows($result))
    {
        message_die(GENERAL_MESSAGE, 'Комментарий не существует!');
    }
$last = $db->sql_fetchrow($result);
if($last['comment_user_id'] != $userdata['user_id'] && $userdata['user_level'] != ADMIN && $userdata['obmen_level'] != 1)
	{
        message_die(GENERAL_MESSAGE, 'Нет доступа!');
    }
if(isset($_POST['no']))
{
		$URL = append_sid("obmen.$phpEx?mod=file&id=".$last['comment_fid']);
			header ("Location: $URL");
}	
if(isset($_POST['yes']))
{
$sql = "DELETE FROM `" . UPLOAD_COMMENTS_TABLE . "` WHERE `comment_id`='".$last['comment_id']."'";
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get chat_post information', '', __LINE__, __FILE__, $sql);
	}
$sql = "UPDATE `" . UPLOAD_FILES_TABLE . "` SET `file_comments` = `file_comments`-1 WHERE `file_id` = '".$last['comment_fid']."' LIMIT 1";
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get  information', '', __LINE__, __FILE__, $sql);
	}	
$sql = "UPDATE `" . USERS_TABLE . "` SET `comments_count` = `comments_count`-1 WHERE `user_id` = '".$last['comment_user_id']."' LIMIT 1";
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get  information', '', __LINE__, __FILE__, $sql);
	}	
		$URL = append_sid("obmen.$phpEx?mod=file&id=".$last['comment_fid']);
			header ("Location: $URL");
}
$delete = append_sid("obmen.$phpEx?mod=delcomment&id=".$last['comment_id']);
message_die(GENERAL_MESSAGE, 'Вы уверены, что хотите удалить этот комментарий?<br/><form action="'.$delete.'" method="post" />
<input type="submit" name="yes" value="Да" /> <input type="submit" name="no" value="Нет" />
</form>');

break;

case 'rating':
if ( !$userdata['session_logged_in'] )
	{
        message_die(GENERAL_MESSAGE, 'Вы не авторизированы!');
	}	
	$sql = "SELECT*FROM `" . UPLOAD_FILES_TABLE . "` WHERE `file_id`='".$id."'";
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_files information', '', __LINE__, __FILE__, $sql);
	}
if(!mysql_num_rows($result))
    {
        message_die(GENERAL_MESSAGE, 'Файл не существует!');
    }
$last = $db->sql_fetchrow($result);
	$sql = "SELECT*FROM `" . UPLOAD_RATING_TABLE . "` WHERE `rating_fid`='".$last['file_id']."' AND `rating_user_id`='".$userdata['user_id']."'";
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_rating information', '', __LINE__, __FILE__, $sql);
	}
if(mysql_num_rows($result))
    {
        message_die(GENERAL_MESSAGE, 'Вы уже отдали свой голос за этот файл!');
    }
if(!isset($_GET['plus']) && !isset($_GET['minus']))
    {
        message_die(GENERAL_MESSAGE, 'Вы не выбрали тип голоса!');
    }	
if(isset($_GET['plus']))
	{
$sql = "INSERT INTO `" . UPLOAD_RATING_TABLE . "` (`rating_user_id`, `rating_fid`, `rating_type`) values('".$userdata['user_id']."', '".$last['file_id']."', '1')";
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get  information', '', __LINE__, __FILE__, $sql);
	}	
$sql = "UPDATE `" . UPLOAD_FILES_TABLE . "` SET `file_plus` = `file_plus`+1, `file_rating` = `file_rating`+1 WHERE `file_id` = '".$last['file_id']."'";
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get  information', '', __LINE__, __FILE__, $sql);
	}	
		$URL = append_sid("obmen.$phpEx?mod=file&id=".$last['file_id']);
			header ("Location: $URL");	
	}
if(isset($_GET['minus']))
	{
$sql = "INSERT INTO `" . UPLOAD_RATING_TABLE . "` (`rating_user_id`, `rating_fid`, `rating_type`) values('".$userdata['user_id']."', '".$last['file_id']."', '2')";
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get  information', '', __LINE__, __FILE__, $sql);
	}	
$sql = "UPDATE `" . UPLOAD_FILES_TABLE . "` SET `file_minus` = `file_minus`+1, `file_rating` = `file_rating`-1 WHERE `file_id` = '".$last['file_id']."'";
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get  information', '', __LINE__, __FILE__, $sql);
	}	
		$URL = append_sid("obmen.$phpEx?mod=file&id=".$last['file_id']);
			header ("Location: $URL");	
	}	
break;

case 'abuse':
if ( !$userdata['session_logged_in'] )
	{
        message_die(GENERAL_MESSAGE, 'Вы не авторизированы!');
	}	
	$sql = "SELECT*FROM `" . UPLOAD_FILES_TABLE . "` WHERE `file_id`='".$id."'";
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_files information', '', __LINE__, __FILE__, $sql);
	}
if(!mysql_num_rows($result))
    {
        message_die(GENERAL_MESSAGE, 'Файл не существует!');
    }
if($userdata['user_level'] == ADMIN || $userdata['obmen_level'] == 1)
	{
        message_die(GENERAL_MESSAGE, 'Администрация не может подавать жалобы на файлы!');
    }	
$last = $db->sql_fetchrow($result);
	$sql = "SELECT*FROM `" . UPLOAD_ABUSE_TABLE . "` WHERE `abuse_fid`='".$last['file_id']."' AND `abuse_user_id`='".$userdata['user_id']."'";
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_abuse information', '', __LINE__, __FILE__, $sql);
	}
if(mysql_num_rows($result))
    {
        message_die(GENERAL_MESSAGE, 'У этого файла уже есть жалобы от вас!');
    }
if(isset($_POST['submit']))
{
$msg=$_POST['abuse_text'];
if (strlen2($msg)<2)
{
message_die(GENERAL_MESSAGE, 'Причина слишком короткая короткий!');
}
$msg = mysql_real_escape_string(trim($msg));
$sql = "INSERT INTO `" . UPLOAD_ABUSE_TABLE . "` (`abuse_user_id`, `abuse_time`, `abuse_text`, `abuse_fid`) values('".$userdata['user_id']."', '".time()."', '$msg', '".$last['file_id']."')";
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get  information', '', __LINE__, __FILE__, $sql);
	}
$sql = "UPDATE `" . UPLOAD_FILES_TABLE . "` SET `file_abuse` = `file_abuse`+1 WHERE `file_id` = '".$last['file_id']."' LIMIT 1";
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get  information', '', __LINE__, __FILE__, $sql);
	}
        message_die(GENERAL_MESSAGE, 'Жалоба успешно добавлена и в ближайшее время будет рассмотрена Администрацией сайта!');	
}
$page_title = 'Жалоба на файл';
define('SHOW_ONLINE', true);
include($phpbb_root_path . 'includes/page_header.'.$phpEx);
$template->set_filenames(array('abuse' => 'obmen_abuse_body.tpl'));
$file_link = append_sid("obmen.$phpEx?mod=file&id=".$last['file_id']);
$file_name = text_obmen($last['file_name']);	
$addabuse = append_sid("obmen.$phpEx?mod=abuse&amp;id=".$last['file_id']);
	    $template->assign_vars(array(
		'FILE_LINK' => $file_link,
		'FILE_NAME' => $file_name,
	    'ADDABUSE' => $addabuse
		));
$template->pparse('abuse');		
break;

case 'delfile':
if ( !$userdata['session_logged_in'] )
	{
        message_die(GENERAL_MESSAGE, 'Вы не авторизированы!');
	}	
	$sql = "SELECT*FROM `" . UPLOAD_FILES_TABLE . "` WHERE `file_id`='".$id."'";
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_files information', '', __LINE__, __FILE__, $sql);
	}
if(!mysql_num_rows($result))
    {
        message_die(GENERAL_MESSAGE, 'Файл не существует!');
    }
$last = $db->sql_fetchrow($result);
if($last['file_user_id'] != $userdata['user_id'] && $userdata['user_level'] != ADMIN && $userdata['obmen_level'] != 1)
	{
        message_die(GENERAL_MESSAGE, 'Нет доступа!');
    }
	if(isset($_POST['no']))
{
	$URL = append_sid("obmen.$phpEx?mod=file&id=".$last['file_id']);
			header ("Location: $URL");	
}
if(isset($_POST['yes']))
{

unlink('upload/'.$last['file_url']);

if(!empty($last['file_screen']))
{
unlink('screen/'.$last['file_screen']);
}
	$sql = "DELETE FROM `" . UPLOAD_COMMENTS_TABLE . "` WHERE `comment_fid`='".$last['file_id']."'";
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_comments information', '', __LINE__, __FILE__, $sql);
	}
	$sql = "DELETE FROM `" . UPLOAD_ABUSE_TABLE . "` WHERE `abuse_fid`='".$last['file_id']."'";
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_abuse information', '', __LINE__, __FILE__, $sql);
	}	
	$sql = "DELETE FROM `" . UPLOAD_RATING_TABLE . "` WHERE `rating_fid`='".$last['file_id']."'";
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_rating information', '', __LINE__, __FILE__, $sql);
	}	
$sql = "UPDATE `" . USERS_TABLE . "` SET `upload_count`=`upload_count`-1, `upload_size`=`upload_size`-".$last['file_size']." WHERE `user_id` = '".$last['file_user_id']."'";
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get users information', '', __LINE__, __FILE__, $sql);
	}
$sql = "UPDATE `" . UPLOAD_CATS_TABLE . "` SET `cat_count`=`cat_count`-1, `cat_size`=`cat_size`-".$last['file_size']." WHERE `cat_id`='".$last['file_cid']."'";
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_cats information', '', __LINE__, __FILE__, $sql);
	}	

$sql = "UPDATE `" . UPLOAD_RAZD_TABLE . "` SET `raz_count`=`raz_count`-1, `raz_size`=`raz_size`-".$last['file_size']." WHERE `raz_id`='".$last['file_rid']."'";
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_razd information', '', __LINE__, __FILE__, $sql);
	}	

$sql = "DELETE FROM `" . UPLOAD_FILES_TABLE . "` WHERE `file_id`='".$last['file_id']."'";
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_files information', '', __LINE__, __FILE__, $sql);
	}	
$sql = "OPTIMIZE TABLE `" . UPLOAD_FILES_TABLE . "`";
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_razd information', '', __LINE__, __FILE__, $sql);
	}	
$sql = "OPTIMIZE TABLE `" . UPLOAD_COMMENTS_TABLE . "`";
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_comments information', '', __LINE__, __FILE__, $sql);
	}	
	        message_die(GENERAL_MESSAGE, 'Файл удален!');
	}
$delete = append_sid("obmen.$phpEx?mod=delfile&id=".$last['file_id']);
message_die(GENERAL_MESSAGE, 'Вы уверены, что хотите удалить этот файл?<br/><form action="'.$delete.'" method="post" />
<input type="submit" name="yes" value="Да" /> <input type="submit" name="no" value="Нет" />
</form>');	
break;
 case 'editfile':
 if ( !$userdata['session_logged_in'] )
	{
        message_die(GENERAL_MESSAGE, 'Вы не авторизированы!');
	}	
 	$sql = "SELECT*FROM `" . UPLOAD_FILES_TABLE . "` WHERE `file_id`='".$id."'";
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_files information', '', __LINE__, __FILE__, $sql);
	}
if(!mysql_num_rows($result))
    {
        message_die(GENERAL_MESSAGE, 'Файл не существует!');
    }
$last = $db->sql_fetchrow($result);
if($last['file_user_id'] != $userdata['user_id'] && $userdata['user_level'] != ADMIN && $userdata['obmen_level'] != 1)
	{
        message_die(GENERAL_MESSAGE, 'Нет доступа!');
    }
$sql = "SELECT*FROM " . UPLOAD_CATS_TABLE . " WHERE cat_rid=".$last['file_rid'];
	if ( !($categories = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_cats information', '', __LINE__, __FILE__, $sql);
	}	
if(isset($_POST['submit']))
{

if(empty($_POST['name']))
{
        message_die(GENERAL_MESSAGE, 'Вы не ввели назвоние файла!');
}
$cid = $last['file_cid'];
if(abs(intval($_POST['cat_id']))!=$last['file_cid'])
{
	$sql = "SELECT*FROM `" . UPLOAD_CATS_TABLE . "` WHERE `cat_id`='".abs(intval($_POST['cat_id']))."' AND `cat_rid`='".$last['file_rid']."'";
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_cats information', '', __LINE__, __FILE__, $sql);
	}
if(!mysql_num_rows($result))
    {
        message_die(GENERAL_MESSAGE, 'Вы хотите переместить файл в несуществующую категорию!');
    }
$sql = "UPDATE `" . UPLOAD_CATS_TABLE . "` SET `cat_count`=`cat_count`-1, `cat_size`=`cat_size`-".$last['file_size']." WHERE `cat_id`='".$last['file_cid']."'";
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_cats information', '', __LINE__, __FILE__, $sql);
	}
$sql = "UPDATE `" . UPLOAD_CATS_TABLE . "` SET `cat_count`=`cat_count`+1, `cat_size`=`cat_size`+".$last['file_size']." WHERE `cat_id`='".abs(intval($_POST['cat_id']))."'";
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_cats information', '', __LINE__, __FILE__, $sql);
	}
$cid = abs(intval($_POST['cat_id']));	
}


$sql = "UPDATE `" . UPLOAD_FILES_TABLE . "` SET `file_text`='".mysql_real_escape_string(trim($_POST['text']))."', `file_name`='".mysql_real_escape_string(trim($_POST['name']))."', `file_cid`='".$cid."' WHERE `file_id`='".$last['file_id']."'";
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_cats information', '', __LINE__, __FILE__, $sql);
	}

	$URL = append_sid("obmen.$phpEx?mod=file&id=".$last['file_id']);
	header ("Location: $URL");

}
$page_title = 'Редактирование файла';
define('SHOW_ONLINE', true);
include($phpbb_root_path . 'includes/page_header.'.$phpEx);
$template->set_filenames(array('editfile' => 'obmen_editfile_body.tpl'));
while ($cat = $db->sql_fetchrow($categories))
	{
        $cat_name = text_obmen_form($cat['cat_name']);
		$cat_id = $cat['cat_id'];
		$cat_selected = ($cat['cat_id']==$last['file_cid']) ? ' selected="selected"' : '';
    $template->assign_block_vars('cat_row', array(
        'CAT_ID' => $cat_id,
		'CAT_NAME' => $cat_name,
		'CAT_SELECTED' => $cat_selected
	));	
	}
        $file_name = text_obmen_form($last['file_name']);
		$file_text = text_obmen_form($last['file_text']);
		$file_link = append_sid("obmen.$phpEx?mod=file&amp;id=".$last['file_id']);
		$edit_link = append_sid("obmen.$phpEx?mod=editfile&amp;id=".$last['file_id']);
    $template->assign_vars(array(
	    'FILE_NAME' => $file_name,
		'FILE_TEXT' => $file_text,
		'FILE_LINK' => $file_link,
		'EDIT_LINK' => $edit_link
    ));
$template->pparse('editfile');
 break;

case 'viewfiles':
	$sql = "SELECT*FROM `" . USERS_TABLE . "` WHERE `user_id`='".$u."'";
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get users information', '', __LINE__, __FILE__, $sql);
	}
if(!mysql_num_rows($result))
    {
        message_die(GENERAL_MESSAGE, 'Пользователь не существует!');
    }
$user = $db->sql_fetchrow($result);
$all = mysql_result(mysql_query("SELECT COUNT(*) FROM `" . UPLOAD_FILES_TABLE . "` WHERE `file_user_id`='".$user['user_id']."';"),0);
if(!$user['upload_count'])
    {
	    message_die(GENERAL_MESSAGE, 'У пользователя '.$user['username'].' нет файлов!');
	}
$page_title = text_obmen('Файлы пользователя '.$user['username']);
define('SHOW_ONLINE', true);
include($phpbb_root_path . 'includes/page_header.'.$phpEx);
if ( isset($HTTP_POST_VARS['sort']) )
    {
	    $sort = abs(intval($HTTP_POST_VARS['sort']));
    } 
	else 
	{
	    $sort = ( isset($HTTP_GET_VARS['sort']) ) ? intval($HTTP_GET_VARS['sort']) : 0;
    }
if($sort)
{
	$sql = "SELECT*FROM `" . UPLOAD_RAZD_TABLE . "` WHERE `raz_id`='".$sort."'";
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_razd information', '', __LINE__, __FILE__, $sql);
	}
if(mysql_num_rows($result))
    {
        $and = 'AND `file_rid`='.$sort;
    }
	else
	{
		$and = '';
		$sort = 0;
	}
}
	else
	{
		$and = '';
		$sort = 0;
	}
$all = mysql_result(mysql_query("SELECT COUNT(*) FROM `" . UPLOAD_FILES_TABLE . "` WHERE `file_user_id`='".$user['user_id']."' ".$and.";"),0);	
if ( isset($HTTP_POST_VARS['start1']) )
    {
	    $start1 = abs(intval($HTTP_POST_VARS['start1']));
		$start1 = ($start1) ? $start1 : 1;
	    $start = (($start1 - 1) * $board_config['posts_per_page']);
    } 
	else 
	{
	    $start = ( isset($HTTP_GET_VARS['start']) ) ? intval($HTTP_GET_VARS['start']) : 0;
	    $start = ($start < 0) ? 0 : $start;
    }
$template->set_filenames(array('viewfiles' => 'obmen_viewfiles_body.tpl'));
if(!$all)
	{
		$template->assign_block_vars('no_files', array(
    ));		
    }
	$sql = "SELECT*FROM `" . UPLOAD_FILES_TABLE . "` WHERE `file_user_id`='".$user['user_id']."' ".$and." ORDER BY `file_time` DESC LIMIT " . $start . ", " . $board_config[posts_per_page];
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_files information', '', __LINE__, __FILE__, $sql);
	}
while ($file = $db->sql_fetchrow($result))
	{
        $ext = ext_file($file['file_url']);
switch($ext)
    {
case 'png':
case 'gif':
case 'jpeg':
case 'jpg':
$screen = '<img src="picture/40/40/'.$file['file_url'].'" alt="" />';
break;

case '3gp':
case 'avi':
case 'mp4':
if(class_exists('ffmpeg_movie'))
{
$screen = '<img src="video/small/'.$file['file_id'].'.gif" alt="">';
}
else
{
$screen = '<img src="images/file.gif" alt="" />';
}
break;

default:
if(!empty($file['file_screen']))
    {
        $screen = '<img src="screenshot/small/'.$file['file_screen'].'" alt="" />';
    }
    else
    {
        $screen = '<img src="images/file.gif" alt="" />';
    }
    }
        $file_link = append_sid("obmen.$phpEx?mod=file&amp;id=".$file['file_id']);
        $file_name = text_obmen($file['file_name']);
        $file_size = fsize($file['file_size']);
        $file_text = mb_substr(text_obmen($file['file_text']), 0, 60);       
$template->assign_block_vars('file_row', array(
        'FILE_LINK' => $file_link,
		'FILE_NAME' => $file_name,
		'FILE_SIZE' => $file_size,
		'FILE_TEXT' => $file_text,
		'FILE_SCREEN' => $screen
    ));		
    }
		$viewfiles_link = append_sid("obmen.$phpEx?mod=viewfiles&amp;u=".$user['user_id']);
		$username = text_obmen($user['username']);		
        $userprofile = append_sid("profile.$phpEx?mode=viewprofile&amp;u=".$user['user_id']);
		$upload_count = $user['upload_count'];
		$pagination = ($all>$board_config['posts_per_page']) ? generate_pagination("obmen.$phpEx?mod=viewfiles&amp;u=$user[user_id]&amp;sort=$sort", $all, $board_config['posts_per_page'], $start) : '';
$template->assign_vars(array(
		'VIEWFILES_LINK' => $viewfiles_link,
		'USERNAME' => $username,
		'USERPROFILE' => $userprofile,
	    'UPLOAD_COUNT' => $upload_count,
		'PAG' => $pagination
    ));
	$sql = "SELECT*FROM " . UPLOAD_RAZD_TABLE;
if ( !($razdel = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_razd information', '', __LINE__, __FILE__, $sql);
	}	
while ($cat = $db->sql_fetchrow($razdel))
	{
        $cat_name = text_obmen_form($cat['raz_name']);
		$cat_id = $cat['raz_id'];
		$cat_selected = ($cat_id==$sort) ? ' selected="selected"' : '';
    $template->assign_block_vars('cat_row', array(
        'CAT_ID' => $cat_id,
		'CAT_NAME' => $cat_name,
		'CAT_SELECTED' => $cat_selected
	));	
	}	
$template->pparse('viewfiles');	

break;
 case 'topusers':
$page_title = 'TOP-100 пользователей';
define('SHOW_ONLINE', true);
include($phpbb_root_path . 'includes/page_header.'.$phpEx);
if ( isset($HTTP_POST_VARS['sort']) )
    {
	    $sort = abs(intval($HTTP_POST_VARS['sort']));
    } 
	else 
	{
	    $sort = ( isset($HTTP_GET_VARS['sort']) ) ? intval($HTTP_GET_VARS['sort']) : 0;
    }
if($sort==1)
{
$order = 'upload_size';
}
elseif($sort==2)
{
$order = 'load_count';
}
elseif($sort==3)
{
$order = 'load_size';
}
elseif($sort==4)
{
$order = 'comments_count';
}
else
{
$order = 'upload_count';
}
$all = mysql_result(mysql_query("SELECT COUNT(*) FROM `" . USERS_TABLE . "` WHERE `user_id`!='-1'"),0);	
if($all>100)
{
$all = 100;
}
if ( isset($HTTP_POST_VARS['start1']) )
    {
	    $start1 = abs(intval($HTTP_POST_VARS['start1']));
		$start1 = ($start1) ? $start1 : 1;
	    $start = (($start1 - 1) * $board_config['posts_per_page']);
    } 
	else 
	{
	    $start = ( isset($HTTP_GET_VARS['start']) ) ? intval($HTTP_GET_VARS['start']) : 0;
	    $start = ($start < 0) ? 0 : $start;
    }
$template->set_filenames(array('topusers' => 'obmen_topusers_body.tpl'));
	$sql = "SELECT*FROM `" . USERS_TABLE . "` WHERE `user_id`!='-1' ORDER BY `".$order."` DESC LIMIT " . $start . ", " . $board_config[posts_per_page];
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get users information', '', __LINE__, __FILE__, $sql);
	}
$mesto = ($start) ? $start : 0;	
while ($user = $db->sql_fetchrow($result))
	{
	$mesto = $mesto + 1;
if($sort==1)
{
$stat = 'Залил '.fsize($user['upload_size']);
}
elseif($sort==2)
{
$stat = 'Скачал '.$user['load_count'].' файлов';
}
elseif($sort==3)
{
$stat = 'Скачал '.fsize($user['load_size']);
}
elseif($sort==4)
{
$stat = 'Оставил '.$user['comments_count'].' комментариев';
}
else
{
$stat = 'Залил '.$user['upload_count'].' файлов';
}      
$username = $user['username'];
$profile = append_sid("profile.$phpEx?mode=viewprofile&amp;u=".$user['user_id']);

$template->assign_block_vars('users_row', array(
        'STAT' => $stat,
		'USERNAME' => $username,
		'MESTO' => $mesto,
		'PROFILE' => $profile
    ));		
    }
		$ucount = ($sort==0) ? ' selected="selected"' : '';
		$usize = ($sort==1) ? ' selected="selected"' : '';
		$lcount = ($sort==2) ? ' selected="selected"' : '';
		$lsize = ($sort==3) ? ' selected="selected"' : '';
		$ccount = ($sort==4) ? ' selected="selected"' : '';
		$topusers = append_sid("obmen.$phpEx?mod=topusers");
		$pagination = ($all>$board_config['posts_per_page']) ? generate_pagination("obmen.$phpEx?mod=topusers&amp;sort=$sort", $all, $board_config['posts_per_page'], $start) : '';
$template->assign_vars(array(
		'UCOUNT' => $ucount,
		'USIZE' => $usize,
		'LCOUNT' => $lcount,
		'LSIZE' => $lsize,
		'CCOUNT' => $ccount,
		'TOPUSERS' => $topusers,
		'PAG' => $pagination
    ));

$template->pparse('topusers');	

break;

case 'topfilesrazd':
$page_title = 'TOP-100 файлов';
define('SHOW_ONLINE', true);
include($phpbb_root_path . 'includes/page_header.'.$phpEx);
$template->set_filenames(array('topfilesrazd' => 'obmen_topfilesrazd_body.tpl'));
	$sql = "SELECT * FROM `" . UPLOAD_RAZD_TABLE . "` ORDER BY `raz_name` ASC";
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_razd information', '', __LINE__, __FILE__, $sql);
	}
while ($razdel = $db->sql_fetchrow($result))
	{
		$razdel_name = text_obmen($razdel['raz_name']);
		$razdel_link = append_sid("obmen.$phpEx?mod=topfiles&amp;id=".$razdel['raz_id']);
		$razdel_count = $razdel['raz_count'];
    $template->assign_block_vars('razdel_row', array(
        'RAZDEL_NAME' => $razdel_name,
		'RAZDEL_LINK' => $razdel_link,
		'RAZDEL_COUNT' => $razdel_count
	));
	} 
$template->pparse('topfilesrazd');	
break;

case 'topfiles':
	$sql = "SELECT*FROM `" . UPLOAD_RAZD_TABLE . "` WHERE `raz_id`='".$id."'";
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_razd information', '', __LINE__, __FILE__, $sql);
	}
if(!mysql_num_rows($result))
    {
        message_die(GENERAL_MESSAGE, 'Раздел не существует!');
    }
$catalog = $db->sql_fetchrow($result);
if ($catalog['raz_login'])
    {
if ( !$userdata['session_logged_in'] )
	{
        message_die(GENERAL_MESSAGE, 'Раздел закрыт от неавторизированных пользователей!');	
	}
	}
if ( isset($HTTP_POST_VARS['sort']) )
    {
	    $sort = abs(intval($HTTP_POST_VARS['sort']));
    } 
	else 
	{
	    $sort = ( isset($HTTP_GET_VARS['sort']) ) ? intval($HTTP_GET_VARS['sort']) : 0;
    }
if($sort==1)
{
$order = 'file_comments';
}
elseif($sort==2)
{
$order = 'file_rating';
}
else
{
$order = 'file_load';
}
$all = mysql_result(mysql_query("SELECT COUNT(*) FROM `" . UPLOAD_FILES_TABLE . "` WHERE `file_rid`='".$catalog['raz_id']."'"),0);	
if($all>100)
{
$all = 100;
}
if ( isset($HTTP_POST_VARS['start1']) )
    {
	    $start1 = abs(intval($HTTP_POST_VARS['start1']));
		$start1 = ($start1) ? $start1 : 1;
	    $start = (($start1 - 1) * $board_config['posts_per_page']);
    } 
	else 
	{
	    $start = ( isset($HTTP_GET_VARS['start']) ) ? intval($HTTP_GET_VARS['start']) : 0;
	    $start = ($start < 0) ? 0 : $start;
    }
$page_title = 'TOP-100 файлов';
define('SHOW_ONLINE', true);
include($phpbb_root_path . 'includes/page_header.'.$phpEx);
$template->set_filenames(array('topfiles' => 'obmen_topfiles_body.tpl'));
	$sql = "SELECT*FROM `" . UPLOAD_FILES_TABLE . "` WHERE `file_rid`='".$catalog['raz_id']."' ORDER BY `".$order."` DESC LIMIT " . $start . ", " . $board_config[posts_per_page];
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_files information', '', __LINE__, __FILE__, $sql);
	}
$mesto = ($start) ? $start : 0;	
while ($file = $db->sql_fetchrow($result))
	{
$mesto = $mesto + 1;
if($sort==1)
{
$stat = 'Прокомментированн '.$file['file_comments'].' раз(а)';
}
elseif($sort==2)
{
$stat = 'Рейтинг: '.$file['file_rating'];
}
else
{
$stat = 'Скачан '.$file['file_load'].' раз(а)';
}
        $file_link = append_sid("obmen.$phpEx?mod=file&amp;id=".$file['file_id']);
        $file_name = text_obmen($file['file_name']);     
$template->assign_block_vars('file_row', array(
        'FILE_LINK' => $file_link,
		'FILE_NAME' => $file_name,
		'MESTO' => $mesto,
		'STAT' => $stat
    ));		
    }
		$fcount = ($sort==0) ? ' selected="selected"' : '';
		$fcomments = ($sort==1) ? ' selected="selected"' : '';
		$frating = ($sort==2) ? ' selected="selected"' : '';
		$razdel_name = text_obmen($catalog['raz_name']);
		$topfiles = append_sid("obmen.$phpEx?mod=topfiles&amp;id=".$catalog['raz_id']);
		$pagination = ($all>$board_config['posts_per_page']) ? generate_pagination("obmen.$phpEx?mod=topfiles&amp;sort=$sort&amp;id=$catalog[raz_id]", $all, $board_config['posts_per_page'], $start) : '';
$template->assign_vars(array(
		'FCOUNT' => $fcount,
		'FCOMMENTS' => $fcomments,
		'FRATING' => $frating,
		'RAZDEL_NAME' => $razdel_name,
		'TOPFILES' => $topfiles,
		'PAG' => $pagination
    ));	
$template->pparse('topfiles');	
break;
case 'newfiles':

$all = mysql_result(mysql_query("SELECT COUNT(*) FROM `" . UPLOAD_FILES_TABLE . "` WHERE `file_time`>'".mktime('0','0','0')."' AND `file_time`<'".mktime('23','59','59')."';"),0);

if(!$all)
	{
        message_die(GENERAL_MESSAGE, 'За сегодня файлов нет!');
    }
if ( isset($HTTP_POST_VARS['start1']) )
    {
	    $start1 = abs(intval($HTTP_POST_VARS['start1']));
		$start1 = ($start1) ? $start1 : 1;
	    $start = (($start1 - 1) * $board_config['posts_per_page']);
    } 
	else 
	{
	    $start = ( isset($HTTP_GET_VARS['start']) ) ? intval($HTTP_GET_VARS['start']) : 0;
	    $start = ($start < 0) ? 0 : $start;
    }
$template->set_filenames(array('newfiles' => 'obmen_newfiles_body.tpl'));
if(!$all)
	{
		$template->assign_block_vars('no_files', array(
    ));		
    }
$page_title = 'Новые файлы';
define('SHOW_ONLINE', true);
include($phpbb_root_path . 'includes/page_header.'.$phpEx);	
	$sql = "SELECT*FROM `" . UPLOAD_FILES_TABLE . "` WHERE `file_time`>'".mktime('0','0','0')."' AND `file_time`<'".mktime('23','59','59')."' ORDER BY `file_time` DESC LIMIT " . $start . ", " . $board_config[posts_per_page];
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_files information', '', __LINE__, __FILE__, $sql);
	}
while ($file = $db->sql_fetchrow($result))
	{
        $ext = ext_file($file['file_url']);
switch($ext)
    {
case 'png':
case 'gif':
case 'jpeg':
case 'jpg':
$screen = '<img src="picture/40/40/'.$file['file_url'].'" alt="" />';
break;

case '3gp':
case 'avi':
case 'mp4':
if(class_exists('ffmpeg_movie'))
{
$screen = '<img src="video/small/'.$file['file_id'].'.gif" alt="">';
}
else
{
$screen = '<img src="images/file.gif" alt="" />';
}
break;

default:
if(!empty($file['file_screen']))
    {
        $screen = '<img src="screenshot/small/'.$file['file_screen'].'" alt="" />';
    }
    else
    {
        $screen = '<img src="images/file.gif" alt="" />';
    }
    }
        $file_link = append_sid("obmen.$phpEx?mod=file&amp;id=".$file['file_id']);
        $file_name = text_obmen($file['file_name']);
        $file_size = fsize($file['file_size']);
        $file_text = mb_substr(text_obmen($file['file_text']), 0, 60);       
$template->assign_block_vars('file_row', array(
        'FILE_LINK' => $file_link,
		'FILE_NAME' => $file_name,
		'FILE_SIZE' => $file_size,
		'FILE_TEXT' => $file_text,
		'FILE_SCREEN' => $screen
    ));		
    }
		$viewfiles_link = append_sid("obmen.$phpEx?mod=viewfiles&amp;u=".$user['user_id']);
		$username = text_obmen($user['username']);		
        $userprofile = append_sid("profile.$phpEx?mode=viewprofile&amp;u=".$user['user_id']);
		$upload_count = $user['upload_count'];
		$pagination = ($all>$board_config['posts_per_page']) ? generate_pagination("obmen.$phpEx?mod=newfiles", $all, $board_config['posts_per_page'], $start) : '';
$template->assign_vars(array(
		'VIEWFILES_LINK' => $viewfiles_link,
		'USERNAME' => $username,
		'USERPROFILE' => $userprofile,
	    'UPLOAD_COUNT' => $upload_count,
		'PAG' => $pagination
    ));
$template->pparse('newfiles');	

break;

case 'search':
if ( isset($HTTP_POST_VARS['query']) )
    {
	    $search = mysql_real_escape_string(trim(htmlspecialchars($HTTP_POST_VARS['query'])));
    } 
	else 
	{
	    $search = ( isset($HTTP_GET_VARS['query']) ) ? urldecode(mysql_real_escape_string(trim(htmlspecialchars($HTTP_GET_VARS['query'])))) : false;
    }
if ( isset($HTTP_POST_VARS['start1']) )
    {
	    $start1 = abs(intval($HTTP_POST_VARS['start1']));
		$start1 = ($start1) ? $start1 : 1;
	    $start = (($start1 - 1) * $board_config['posts_per_page']);
    } 
	else 
	{
	    $start = ( isset($HTTP_GET_VARS['start']) ) ? intval($HTTP_GET_VARS['start']) : 0;
	    $start = ($start < 0) ? 0 : $start;
    }
if($search)
{
$all = mysql_result(mysql_query("SELECT COUNT(*) FROM `" . UPLOAD_FILES_TABLE . "` WHERE `file_name` LIKE '%".$search."%' OR `file_text` LIKE '%".$search."%';"),0);

if(!$all)
{
        message_die(GENERAL_MESSAGE, 'Ничего не найдено!');	
}
}
$page_title = 'Файлобменник';
define('SHOW_ONLINE', true);
include($phpbb_root_path . 'includes/page_header.'.$phpEx);
$template->set_filenames(array('search' => 'obmen_search_body.tpl'));
if($search)
{
	    $total_result = $all;
	    $template->assign_block_vars('result_search', array(
	    'TOTAL_RESULT' => $total_result
	));
	$sql = "SELECT*FROM `" . UPLOAD_FILES_TABLE . "` WHERE `file_name` LIKE '%".$search."%' OR `file_text` LIKE '%".$search."%' ORDER BY `file_text` DESC, `file_name` DESC LIMIT " . $start . ", " . $board_config[posts_per_page];
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_files information', '', __LINE__, __FILE__, $sql);
	}
while ($file = $db->sql_fetchrow($result))
	{
        $ext = ext_file($file['file_url']);
switch($ext)
    {
case 'png':
case 'gif':
case 'jpeg':
case 'jpg':
$screen = '<img src="picture/40/40/'.$file['file_url'].'" alt="" />';
break;

case '3gp':
case 'avi':
case 'mp4':
if(class_exists('ffmpeg_movie'))
{
$screen = '<img src="video/small/'.$file['file_id'].'.gif" alt="">';
}
else
{
$screen = '<img src="images/file.gif" alt="" />';
}
break;

default:
if(!empty($file['file_screen']))
    {
        $screen = '<img src="screenshot/small/'.$file['file_screen'].'" alt="" />';
    }
    else
    {
        $screen = '<img src="images/file.gif" alt="" />';
    }
    }
        $file_link = append_sid("obmen.$phpEx?mod=file&amp;id=".$file['file_id']);
        $file_name = text_obmen($file['file_name']);
        $file_size = fsize($file['file_size']);
        $file_text = mb_substr(text_obmen($file['file_text']), 0, 60);       
$template->assign_block_vars('result_search.file_row', array(
        'FILE_LINK' => $file_link,
		'FILE_NAME' => $file_name,
		'FILE_SIZE' => $file_size,
		'FILE_TEXT' => $file_text,
		'FILE_SCREEN' => $screen
    ));		
    }	
}
		$search1 = urlencode($search);
		$pagination = ($all>$board_config['posts_per_page']) ? generate_pagination("obmen.$phpEx?mod=search&amp;query=$search1", $all, $board_config['posts_per_page'], $start) : '';
		$search_link = append_sid("obmen.$phpEx?mod=search");
 $template->assign_vars(array(
	    'PAG' => $pagination,
		'SEARCH' => $search,
		'SEARCH_LINK' => $search_link
    ));
$template->pparse('search');
break;

case 'moderlist': 
$total = mysql_result(mysql_query("SELECT COUNT(*) FROM `" . USERS_TABLE . "` WHERE `obmen_level` = '1';"),0);
if(!$total)
{
message_die(GENERAL_MESSAGE, 'Модераторов нет!');
}
$page_title = 'Модераторы';
define('SHOW_ONLINE', true);
include($phpbb_root_path . 'includes/page_header.'.$phpEx);
if ( isset($_POST['start1']) )
{     
$start1 = abs(intval($_POST['start1']));     
$start = (($start1 - 1) *$board_config['posts_per_page']);  }
 else 
{ 
$start = ( isset($_GET['start']) ) ? intval($_GET['start']) : 0;
$start = ($start < 0) ? 0 : $start;  }
		$template->set_filenames(array('moderlist' => 'obmen_moderlist_body.tpl'));

$sql = "SELECT * FROM `" . USERS_TABLE . "` WHERE `obmen_level` = '1' LIMIT " . $start . ", " . $board_config[posts_per_page];
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Ошибка', '', __LINE__, __FILE__, $sql);
	}
		while ($user = $db->sql_fetchrow($result))
		{
		$user_link = append_sid("profile.$phpEx?mode=viewprofile&amp;u=".$user['user_id']);
$username = $user['username'];
$template->assign_block_vars('moder_row', array(
			'USER_LINK' => $user_link,
			'USERNAME' => $username
			));
	}
$pagination = generate_pagination("obmen.$phpEx?mod=moderlist", $total, $board_config['posts_per_page'], $start);
		$template->assign_vars(array( 
'PAGINATION' => $pagination
	));

 $template->pparse('moderlist'); 
break;

case 'listabuse':
if($userdata['user_level'] != ADMIN && $userdata['obmen_level'] != 1)
	{
        message_die(GENERAL_MESSAGE, 'Нет доступа!');
    }
$abuse_total = mysql_result(mysql_query("SELECT COUNT(*) FROM `" . UPLOAD_FILES_TABLE . "` WHERE `file_abuse`>'0';"),0);
if(!$abuse_total)
{
message_die(GENERAL_MESSAGE, 'Жалоб нет!');
}
if ( isset($HTTP_POST_VARS['start1']) )
    {
	    $start1 = abs(intval($HTTP_POST_VARS['start1']));
		$start1 = ($start1) ? $start1 : 1;
	    $start = (($start1 - 1) * $board_config['posts_per_page']);
    } 
	else 
	{
	    $start = ( isset($HTTP_GET_VARS['start']) ) ? intval($HTTP_GET_VARS['start']) : 0;
	    $start = ($start < 0) ? 0 : $start;
    }
$page_title = 'Список жалоб';
define('SHOW_ONLINE', true);
include($phpbb_root_path . 'includes/page_header.'.$phpEx);
$template->set_filenames(array('listabuse' => 'obmen_listabuse_body.tpl'));
	$sql = "SELECT*FROM `" . UPLOAD_FILES_TABLE . "` WHERE `file_abuse`>'0' ORDER BY `file_time` DESC LIMIT " . $start . ", " . $board_config[posts_per_page];
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_files information', '', __LINE__, __FILE__, $sql);
	}
$mesto = ($start) ? $start : 0;	
while ($file = $db->sql_fetchrow($result))
	{
		$file_abuse = $file['file_abuse'];
		$mesto = $mesto + 1;
		$abuse_link = append_sid("obmen.$phpEx?mod=viewabuse&amp;id=".$file['file_id']);
        $file_link = append_sid("obmen.$phpEx?mod=file&amp;id=".$file['file_id']);
        $file_name = text_obmen($file['file_name']);     
$template->assign_block_vars('abuse_row', array(
        'FILE_LINK' => $file_link,
		'FILE_ABUSE' => $file_abuse,
		'ABUSE_LINK' => $abuse_link,
		'FILE_NAME' => $file_name,
		'MESTO' => $mesto,
		'STAT' => $stat
    ));		
    }
		$pagination = ($abuse_total>$board_config['posts_per_page']) ? generate_pagination("obmen.$phpEx?mod=listabuse", $abuse_total, $board_config['posts_per_page'], $start) : '';
$template->assign_vars(array(
		'TOTAL' => $abuse_total,
		'PAG' => $pagination
    ));	
$template->pparse('listabuse');
break;

case 'viewabuse':
if($userdata['user_level'] != ADMIN && $userdata['obmen_level'] != 1)
	{
        message_die(GENERAL_MESSAGE, 'Нет доступа!');
    }
	$sql = "SELECT*FROM `" . UPLOAD_FILES_TABLE . "` WHERE `file_id`='".$id."'";
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_files information', '', __LINE__, __FILE__, $sql);
	}
if(!mysql_num_rows($result))
    {
        message_die(GENERAL_MESSAGE, 'Файл не существует!');
    }
$last = $db->sql_fetchrow($result);
if(!$last['file_abuse'])
	{
        message_die(GENERAL_MESSAGE, 'На этот файл нет жалоб!');
	}
$all = mysql_result(mysql_query("SELECT COUNT(*) FROM `" . UPLOAD_ABUSE_TABLE . "` WHERE `abuse_fid`='".$last['file_id']."';"),0);
if ( isset($HTTP_POST_VARS['start1']) )
    {
	    $start1 = abs(intval($HTTP_POST_VARS['start1']));
		$start1 = ($start1) ? $start1 : 1;
	    $start = (($start1 - 1) * $board_config['posts_per_page']);
    } 
	else 
	{
	    $start = ( isset($HTTP_GET_VARS['start']) ) ? intval($HTTP_GET_VARS['start']) : 0;
	    $start = ($start < 0) ? 0 : $start;
    }
$page_title = 'Список жалоб';
define('SHOW_ONLINE', true);
include($phpbb_root_path . 'includes/page_header.'.$phpEx);
$template->set_filenames(array('viewabuse' => 'obmen_viewabuse_body.tpl'));
$sql = "SELECT a.*, u.* FROM " . UPLOAD_ABUSE_TABLE . " a, ".USERS_TABLE." u
			WHERE a.abuse_fid = ".$last['file_id']." AND a.abuse_user_id = u.user_id 
			ORDER BY a.abuse_time DESC 
			LIMIT $start, ".$board_config['posts_per_page'];
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_abuse information', '', __LINE__, __FILE__, $sql);
	}
while ($abuse = $db->sql_fetchrow($result))
	{
		$username = $abuse['username'];
		$profile = append_sid("profile.$phpEx?mode=viewprofile&amp;u=".$abuse['user_id']);
		$text = obmen_smiles(text_obmen($abuse['abuse_text']));
		$time = create_date($board_config['default_dateformat'], $abuse['abuse_time'], $board_config['board_timezone']);
    $template->assign_block_vars('viewabuse', array(
		'USERNAME' => $username,
		'PROFILE' => $profile,
		'TEXT' => $text,
		'TIME' => $time
		));
	}
		$clear = append_sid("obmen.$phpEx?mod=clear&id=".$last['file_id']);
		$file_link = append_sid("obmen.$phpEx?mod=file&id=".$last['file_id']);
		$pagination = ($all>$board_config['posts_per_page']) ? generate_pagination("obmen.$phpEx?mod=viewabuse&amp;id=$last[file_id]", $abuse_total, $board_config['posts_per_page'], $start) : '';
$template->assign_vars(array(
		'FILE_NAME' => text_obmen($last['file_name']),
		'FILE_LINK' => $file_link,
		'TOTAL' => $last['file_abuse'],
		'CLEAR' => $clear,
		'PAG' => $pagination
    ));	
$template->pparse('viewabuse');	
break;

case 'clear':
if($userdata['user_level'] != ADMIN && $userdata['obmen_level'] != 1)
	{
        message_die(GENERAL_MESSAGE, 'Нет доступа!');
    }
	$sql = "SELECT*FROM `" . UPLOAD_FILES_TABLE . "` WHERE `file_id`='".$id."'";
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_files information', '', __LINE__, __FILE__, $sql);
	}
if(!mysql_num_rows($result))
    {
        message_die(GENERAL_MESSAGE, 'Файл не существует!');
    }
$last = $db->sql_fetchrow($result);
if(!$last['file_abuse'])
	{
        message_die(GENERAL_MESSAGE, 'На этот файл нет жалоб!');
	}
$sql = "DELETE FROM `" . UPLOAD_ABUSE_TABLE . "` WHERE `abuse_fid`='".$last['file_id']."'";
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_razd information', '', __LINE__, __FILE__, $sql);
	}
$sql = "UPDATE `" . UPLOAD_FILES_TABLE . "` SET `file_abuse` = '0' WHERE `file_id` = '".$last['file_id']."' LIMIT 1";
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get  information', '', __LINE__, __FILE__, $sql);
	}
	message_die(GENERAL_MESSAGE, 'Жалобы очищены!');
break;

default:
$page_title = 'Файлобменник';
define('SHOW_ONLINE', true);
include($phpbb_root_path . 'includes/page_header.'.$phpEx);
$template->set_filenames(array('body' => 'obmen_index_body.tpl'));
	$sql = "SELECT * FROM `" . UPLOAD_RAZD_TABLE . "` ORDER BY `raz_name` ASC";
if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not get upload_razd information', '', __LINE__, __FILE__, $sql);
	}
while ($razdel = $db->sql_fetchrow($result))
	{
		$razdel_link = ($razdel['raz_ero']==0) ? append_sid("obmen.$phpEx?mod=catalog&amp;id=".$razdel['raz_id']) : append_sid("obmen.$phpEx?mod=eroraz&amp;id=".$razdel['raz_id']);
		$razdel_name = text_obmen($razdel['raz_name']);
		$razdel_count = $razdel['raz_count'];
		$razdel_size = fsize($razdel['raz_size']);
    $template->assign_block_vars('razdel_row', array(
        'RAZDEL_LINK' => $razdel_link,
		'RAZDEL_NAME' => $razdel_name,
		'RAZDEL_COUNT' => $razdel_count,
		'RAZDEL_SIZE' => $razdel_size
	));
if(!empty($razdel['raz_description']))
	{
	    $razdel_description = obmen_smiles(text_obmen($razdel['raz_description']));
    $template->assign_block_vars('razdel_row.description', array(
	    'RAZDEL_DESCRIPTION' => $razdel_description
	
	));
    }
	} 
if($userdata['user_level'] == ADMIN || $userdata['obmen_level'] == 1)
	{	
    $template->assign_block_vars('level', array(
    ));
	}	
	    $yesterday = append_sid("obmen.$phpEx?mod=newfiles");
		$myfiles_link = append_sid("obmen.$phpEx?mod=viewfiles&amp;u=".$userdata['user_id']);
		$myfiles_count = $userdata['upload_count'];
		$topusers = append_sid("obmen.$phpEx?mod=topusers");
		$topfiles = append_sid("obmen.$phpEx?mod=topfilesrazd");
		$search = append_sid("obmen.$phpEx?mod=search");
		$moderlist = append_sid("obmen.$phpEx?mod=moderlist");
		$abuse = append_sid("obmen.$phpEx?mod=listabuse");
		$abuse_total = mysql_result(mysql_query("SELECT COUNT(*) FROM `" . UPLOAD_FILES_TABLE . "` WHERE `file_abuse`>'0';"),0);
        $yesterday_total = mysql_result(mysql_query("SELECT COUNT(*) FROM `" . UPLOAD_FILES_TABLE . "` WHERE `file_time`>'".mktime('0','0','0')."' AND `file_time`<'".mktime('23','59','59')."';"),0);
    $template->assign_vars(array(
	    'YESTERDAY' => $yesterday,
		'MYFILES_LINK' => $myfiles_link,
		'MYFILES_COUNT' => $myfiles_count,
		'TOPUSERS' => $topusers,
		'TOPFILES' => $topfiles,
		'SEARCH' => $search,
		'ABUSE' => $abuse,
		'ABUSE_TOTAL' => $abuse_total,		
		'MODERLIST' => $moderlist,
		'YESTERDAY_TOTAL' => $yesterday_total
    ));

$template->pparse('body');	
    }
include($phpbb_root_path . 'includes/page_tail.'.$phpEx);
?>