<?php

include './core.php';

if( !isset( $user ) || !$user->can_access( 'files' ) ) access_error();

if( isset( $_REQUEST['fileID'] ) )
{
    $file = $MySQL->query_first( "SELECT * FROM files WHERE fileID='".$_REQUEST['fileID']."'" );

    switch( $file['extension'] )
    {
        case 'zip':
            $ctype="application/zip";
            break;
        case 'exe':
            $ctype="application/octet-stream";
            break;
        default:
            $ctype="application/force-download";
        	break;
    }

    header( 'Pragma: no-cache' );
    header( 'Cache-Control: no-store, no-cache, must-revalidate' );
    header( 'Cache-Control: post-check=0, pre-check=0', false );
    header( 'Expires: 0' );
    header( 'Content-Type: '.$ctype );
    $user_agent = strtolower( $_SERVER["HTTP_USER_AGENT"] );
    if ( ( is_integer ( strpos( $user_agent, "msie" ) ) ) && ( is_integer ( strpos( $user_agent, "win" ) ) ) )
    {
       header( "Content-Disposition: filename=".$file['filename'].".".$file['extension'].";" );
    }
    else
    {
       header( "Content-Disposition: attachment; filename=".$file['filename'].".".$file['extension'].";" );
    }
    header( "Content-Length: ".filesize( $settings['files_dir'].$file['filename'].".".$file['extension'] ) );

    readfile( $settings['files_dir'].$file['filename'].".".$file['extension'] );

    die();
}

$result = $MySQL->query( "SELECT * FROM files ORDER BY filename" );
$files = array();
while( $file = $MySQL->fetch_array( $result ) )
{
    $filename = combine_path( $settings['files_dir'], $file['filename'].".".$file['extension'] );
    $size = round( @filesize( $filename ) /1024, 2 );

    if( $size > 1024 )
        $size = round( $size/1024, 2 )." MB";
    else
        $size .= " KB";

    $time = @filemtime( $filename );
    $files[] = array( $time, $file, $size );
}

sort($files);
$files = array_reverse($files);

$Smarty->assign( 'files', $files );

$page['content'] = $Smarty->fetch( 'files.tpl' );

$Smarty->assign( 'page', $page );
$Smarty->display( 'main.tpl' );

?>