<?php
	/*
	*by Krite (http://upcoder.net)
	*/
	define('_IN_JOHNCMS', 1);
	$headmod = 'blogs';
	require_once '../incfiles/core.php';
	if ($user_id && $user_ps)
	{
		$delarc = opendir('temp/zip');
		while ($zp = readdir($delarc))
		{
			if ($zp != '.' && $zp != '..' && $zp != 'index.php')
			{
				$mp[] = $zp;
			}
		}
		closedir($delarc);
		$totalmp = count($mp);
		for ($imp = 0; $imp < $totalmp; $imp++)
		{
			$filtime[$imp] = filemtime('temp/zip/' . $mp[$imp]);
			$tim = time();
			$ftime1 = $tim - 300;
			if ($filtime[$imp] < $ftime1)
			{
				@ unlink('temp/zip/'.$mp[$imp]);
			}
		}
		$req = mysql_query("SELECT `name`, `text` FROM `".$set['prefblog']."blogs` WHERE `id` = '" . $id . "' LIMIT 1;");
		if (mysql_num_rows($req) == 0)
		{
			$textl = 'Блоги';
			require_once('../incfiles/head.php');
			echo '<p class="rmenu">Статья не найдена</p>';
			require_once ("../incfiles/end.php");
			exit;
		}
		$res = mysql_fetch_array($req);
		
		$midlet_name = mb_substr($res['name'], 0, 10);
		$midlet_name = iconv('UTF-8', 'windows-1251', $midlet_name);

		// Записываем текст статьи
		$files = fopen('temp/'.$id.'.txt', 'w+');
		flock($files, LOCK_EX);
		$book_name = iconv('UTF-8', 'windows-1251', $res['name']);
		$book_text = iconv('UTF-8', 'windows-1251', $res['text']);
		$result = "\r\n" . $book_name . "\r\n\r\n----------\r\n\r\n" . $book_text . "\r\n\r\nDownloaded from $home";
		fputs($files, $result);
		flock($files, LOCK_UN);
		fclose($files);
		
		$files = 'temp/'.$id.'.txt';
		
		if(file_exists($files))
		{
			$tmp = 'temp/zip/'.basename($id).'.zip';
			if(!file_exists($tmp))
			{
				require_once ('../incfiles/class_pclzip.php');
				$zip = new PclZip($tmp);

				function cb($p_event, &$p_header)
				{
					$p_header['stored_filename'] = basename($p_header['filename']);
					return 1;
				}
				$zip->create($files, PCLZIP_CB_PRE_ADD, 'cb');
				chmod($tmp, 0644);
			}
			header('Location: temp/zip/'.basename($id).'.zip');
			@ unlink('temp/'.basename($id).'.txt');
		}
		else
		{
			echo 'Ошибка!';
		}
	}
	else
	{
		$textl = 'Блоги | Zip архив статьи';
		require_once('../incfiles/head.php');
		echo '<p class="rmenu">Внимание!<br />
		Скачать ZIP архив могут только зарегистрированные пользователи.</p>';
		echo '<p class="menu"><a href="index.php?act=view&amp;id=' . $id . '">К статье</a></p>';
	}

?>