<?php
	/*
	*by Krite (http://upcoder.net)
	*/
	define('_IN_JOHNCMS', 1);
	$headmod = 'blogs';
	require_once '../incfiles/core.php';
	$textl = 'Блоги | Скачать Java книгу';
	require_once('../incfiles/head.php');
	if ($user_id && $user_ps)
	{
		$delarc = opendir('temp/jar');
		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/jar/' . $mp[$imp]);
			$tim = time();
			$ftime1 = $tim - 300;
			if ($filtime[$imp] < $ftime1)
			{
				@ unlink('temp/'.$mp[$imp]);
			}
		}
		$req = mysql_query("SELECT `name`, `text`, `id` FROM `".$set['prefblog']."blogs` WHERE `id` = '" . $id . "' LIMIT 1;");
		if (mysql_num_rows($req) == 0)
		{
			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))
		{
			$nm = array_reverse(explode('.', basename($files)));
			$nm = $nm[1];
			$tmp = 'temp/jar/'.basename($id).'.jar';
			if(!file_exists($tmp))
			{
				$f = file_get_contents($files);
				if(iconv('UTF-8', 'UTF-8', $f) == $f)
				{
					$f = iconv('UTF-8', 'Windows-1251//TRANSLIT', $f);
				}
				@ copy('book.zip', $tmp);
				@ copy('temp/props.ini', 'temp/jar/props.ini');
				@ copy('temp/MANIFEST.MF', 'temp/jar/MANIFEST.MF');
				$arr = str_split($f, 25600);
				$all = sizeof($arr);
				$ar = @ file('temp/jar/props.ini');

				function encode($str)
				{
					for($i=0, $ln=strlen($str); $i<$ln; $i++)
					{
						$st .= $str[$i].chr(0);
					}
					return $st;
				}
				$ar[] = chr(0).chr(10).chr(0).encode('J/textfile.txt.label=1');
				for($i=1; $i<$all; $i++)
				{
					$ar[] = chr(10).chr(0).encode('J/textfile'.$i.'.txt.label='.($i+1));
				}
				$ar[] = chr(10);

				file_put_contents('temp/jar/props.ini', $ar);
				file_put_contents('temp/jar/MANIFEST.MF','
Manifest-Version: 1.0
MicroEdition-Configuration: CLDC-1.0
MicroEdition-Profile: MIDP-1.0
MIDlet-Name: '.$nm.'
MIDlet-Vendor: Reader UPcoder.Net
MIDlet-1: '.$nm.', /icon.png, br.BookReader
MIDlet-Version: 1.6
MIDlet-Info-URL: http://'.$_SERVER['HTTP_HOST'].'
MIDlet-Delete-Confirm: GoodBye =)');
				require_once ('../incfiles/class_pclzip.php');
				$zip = new PclZip(dirname(__FILE__).'/'.$tmp);
				//echo 'ERROR : '.$zip->errorInfo(true);
				$zip->add(dirname(__FILE__).'/temp/jar/props.ini', PCLZIP_OPT_REMOVE_ALL_PATH);
				//echo 'ERROR : '.$zip->errorInfo(true);

				$zip->add(dirname(__FILE__).'/temp/jar/MANIFEST.MF',PCLZIP_OPT_REMOVE_ALL_PATH,PCLZIP_OPT_ADD_PATH,'META-INF');
				//echo 'ERROR : '.$zip->errorInfo(true);

				file_put_contents('temp/jar/textfile.txt', $arr[0]);

				$zip->add(dirname(__FILE__).'/temp/jar/textfile.txt', PCLZIP_OPT_REMOVE_ALL_PATH);
				//echo 'ERROR : '.$zip->errorInfo(true);

				@ unlink('temp/jar/textfile.txt');

				for($i=1; $i<$all; $i++)
				{
					file_put_contents('temp/jar/textfile'.$i.'.txt', $arr[$i]);

					$zip->add(dirname(__FILE__).'/temp/jar/textfile'.$i.'.txt', PCLZIP_OPT_REMOVE_ALL_PATH);
					//echo 'ERROR : '.$zip->errorInfo(true);
					unlink('temp/textfile'.$i.'.txt');
				}
				@ unlink('temp/jar/MANIFEST.MF');
				@ unlink('temp/jar/props.ini');
				chmod($tmp, 0644);
			}
			//header('Location: temp/'.basename($id).'.jar');
		}
		@ unlink('temp/'.basename($id).'.txt');
		
		echo '<div class="phdr">На этой странице Вы можете скачать Java (MIDP-2) книгу с нужной вам статьей.</div>';
		echo '<div class="list1">Название: ' . $res['name'] . '<br />';
		echo 'Скачать: <a href="temp/jar/' . $id . '.jar">JAR</a></div>';
		echo '<p class="menu"><a href="index.php?act=view&amp;id=' . $id . '">К статье</a></p>';
	}
	else
	{
		echo '<p class="rmenu">Внимание!<br />
		Скачать книгу в Java формате могут только зарегистрированные пользователи.</p>';
		echo '<p class="menu"><a href="index.php?act=view&amp;id=' . $id . '">К статье</a></p>';
	}
	require_once ('../incfiles/end.php');
?>