<?php
ini_set( 'display_errors', 0 );
error_reporting( E_ALL & ~E_NOTICE & ~E_STRICT );
#ini_set( 'display_errors', 1 );
#error_reporting( E_ALL );
define( 'CUSTOMER_PAGE', true );
require_once 'libraries/utils.php';
require 'configure/config_loader.php';
require_once DIR_LIBRARIES . 'FileJobs.php';
require_once DIR_LIBRARIES . 'DataBase.php';
require_once DIR_LIBRARIES . 'FotoJobs.php';
#require_once DIR_ENGINE . 'common.php';
#require_once DIR_ENGINE . 'pages.php';
require_once DIR_ENGINE . 'files.php';
#if( $config['users_plugin_active'] == true ){
#	require_once DIR_ENGINE . 'users.php';
#}
#require_once DIR_ENGINE . 'producers.php';
#require_once DIR_ENGINE . 'banners.php';
#require_once DIR_ENGINE . 'products.php';
#require_once DIR_ENGINE . 'orders.php';
#$oDB = & DataBase::getInstance( );
#$oDB->cacheFilesIndexes( $config_db );
$oFile = & Files::getInstance( );
#$oPage = new Pages( );
#$oProduct = new Products( );
#$oOrder = new Orders( );
#var_dump( getcwd() );
$file = DIR_FILES . $_GET['file'];
function ss_mime_content_type( $filename ){
	$mime_types = array( 'txt' => 'text/plain', 'htm' => 'text/html', 'html' => 'text/html', 'php' => 'text/html', 'css' => 'text/css', 'js' => 'application/javascript', 'json' => 'application/json', 'xml' => 'application/xml', 'swf' => 'application/x-shockwave-flash', 'flv' => 'video/x-flv', 'png' => 'image/png', 'jpe' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'jpg' => 'image/jpeg', 'gif' => 'image/gif', 'bmp' => 'image/bmp', 'ico' => 'image/vnd.microsoft.icon', 'tiff' => 'image/tiff', 'tif' => 'image/tiff', 'svg' => 'image/svg+xml', 'svgz' => 'image/svg+xml', 'zip' => 'application/zip', 'rar' => 'application/x-rar-compressed', 'exe' => 'application/x-msdownload', 'msi' => 'application/x-msdownload', 'cab' => 'application/vnd.ms-cab-compressed', 'mp3' => 'audio/mpeg', 'qt' => 'video/quicktime', 'mov' => 'video/quicktime', 'pdf' => 'application/pdf', 'psd' => 'image/vnd.adobe.photoshop', 'ai' => 'application/postscript', 'eps' => 'application/postscript', 'ps' => 'application/postscript', 'doc' => 'application/msword', 'rtf' => 'application/rtf', 'xls' => 'application/vnd.ms-excel', 'ppt' => 'application/vnd.ms-powerpoint', 'odt' => 'application/vnd.oasis.opendocument.text', 'ods' => 'application/vnd.oasis.opendocument.spreadsheet', );
	$ext = strtolower( array_pop( explode( '.', $filename ) ) );
	if( array_key_exists( $ext, $mime_types ) ){
		return $mime_types[$ext];
	} elseif( function_exists( 'finfo_open' ) ){
		$finfo = finfo_open( FILEINFO_MIME );
		$mimetype = finfo_file( $finfo, $filename );
		finfo_close( $finfo );
		return $mimetype;
	} else{
		return 'application/octet-stream';
	}
}
if( is_file( $file ) ){
	header( 'Content-type: ' . ss_mime_content_type( $file ) );
	$ext = strtolower( array_pop( explode( '.', $file ) ) );
	if( $ext == 'pdf' ){
		header('Content-Disposition: attachment; filename="' . $file . '"');
	};
	readfile( $file );
	exit;
} else{
	if( preg_match( '=^([whls]?[0-9]+)/(.*)$=', $_GET['file'], $aMatches ) && is_file( DIR_FILES . $aMatches[2] ) ){
		//GENEROWANIE MINIATUREK ODPOWIEDNICH
		if( !is_dir( DIR_FILES . $aMatches[1] ) ){
			mkdir( DIR_FILES . $aMatches[1] );
		}
		$oFoto = & FotoJobs::getInstance( );
		$oFoto->createCustomThumb( DIR_FILES . $aMatches[2], DIR_FILES . $aMatches[1] . '/', $aMatches[1], $aMatches[2] );
		header( 'Content-type: ' . ss_mime_content_type( $file ) );
		readfile( $file );
		exit;
	} else{
		header( 'Content-type: text/html' );
		readfile( DIR_TEMPLATES . 'error/404-upload.html' );
	}
}
