<?php
/**
 * N3Download (Nesquik Download)
 *
 * Система представляет собой скрипт загруз-центра
 * Система написана на PHP, хранящая данные в БД MYSQL
 * Высокая скорость работы скрипта, устойчивость к нагрузкам
 *
 * Автор: N3squ1k (Несквик)
 * ICQ: 551-228
 *
 * Вы не имеете право продавать скрипта
 * Вы не имеете право вылаживать скрипта
 * Все права на данный скрипт пренадлежат его автору
 *
 * Все права защищены © 2010
 */

define('ROOT', str_replace(array('\\', '//'), '/', getcwd()).'/');

if (file_exists(ROOT.'__incfiles/core.php')) {
	require_once(ROOT.'__incfiles/core.php');
} else {
	exit('Немогу подключить системный файл');
}

$row = mysql_fetch_row(mysql_query("SELECT TRIM(`path`), LOWER(RIGHT(`path`, 4)) FROM `".SQLprefixTable."files` WHERE `id`='".$id."'"));
$theme = urldecode(htmlspecialchars($row[0]));

$size = explode('*', $set['size']);

if (isset($_GET['size']) && $_GET['size'] == 1) {
	$g_preview_image_w = $size[0];
	$g_preview_image_h = $size[1];
	$name = 'cache/'.basename($theme).'_small.gif';
} else {
	$g_preview_image_w = 80;
	$g_preview_image_h = 80;
	$name = 'cache/'.basename($theme).'.gif';
}

if (file_exists($name)){
	exit(header('Location: '.$name, true, 301));
}

if ($row[1] == '.nth') {

	require_once(SYSTEM.'pclzip.lib.php');

	$nth = &new PclZip($theme);
	$content = $nth->extract(PCLZIP_OPT_BY_NAME, 'theme_descriptor.xml', PCLZIP_OPT_EXTRACT_AS_STRING);

	$teg = simplexml_load_string($content[0]['content'])->wallpaper['src'] or $teg = simplexml_load_string($content[0]['content'])->wallpaper['main_display_graphics'];
	$image = $nth->extract(PCLZIP_OPT_BY_NAME, trim($teg), PCLZIP_OPT_EXTRACT_AS_STRING);

	$im = array_reverse(explode('.', $teg));
	$im = 'imageCreateFrom'.str_ireplace('jpg','jpeg', trim($im[0]));

	file_put_contents($name, $image[0]['content']);
	$f = $im($name);

	$h = imageSY($f);
	$w = imageSX($f);

	$ratio = $w / $h;

	if ($g_preview_image_w / $g_preview_image_h > $ratio){
		$g_preview_image_w = $g_preview_image_h * $ratio;
	}
	else {
		$g_preview_image_h = $g_preview_image_w / $ratio;
	}

	$new = ImageCreateTrueColor($g_preview_image_w, $g_preview_image_h);
	ImageCopyResampled($new, $f, 0, 0, 0, 0, $g_preview_image_w, $g_preview_image_h, $w, $h);

	imageGif($new, $name, 100);
}
elseif ($row[1] == '.thm') {

	require_once(SYSTEM.'Tar.php');
	
	$thm = &new Archive_Tar($theme);
	$deskside_file = $thm->extractInString('Theme.xml');
	$load = simplexml_load_string($deskside_file)->Standby_image['Source'] or $load = simplexml_load_string($deskside_file)->Desktop_image['Source'] or $load = simplexml_load_string($deskside_file)->Desktop_image['Source'];
	$image = $thm->extractInString(trim($load));
	
	$im = array_reverse(explode('.', $load));
	$im = 'imageCreateFrom'.str_ireplace('jpg', 'jpeg', trim($im[0]));
	
	file_put_contents($name, $image);
	$f = $im($name);
	
	$h = imageSY($f);
	$w = imageSX($f);

	$ratio = $w / $h;
	if ($g_preview_image_w / $g_preview_image_h > $ratio) {
		$g_preview_image_w = $g_preview_image_h * $ratio;
	}
	else {
		$g_preview_image_h = $g_preview_image_w / $ratio;
	}


	$new = ImageCreateTrueColor($g_preview_image_w, $g_preview_image_h);
	ImageCopyResampled($new, $f, 0, 0, 0, 0, $g_preview_image_w, $g_preview_image_h, $w, $h);

	ImageGif($new, $name, 100);
}

header('Location: '.$name, true, 301);
?>