<?php

class JieqiJar
{
	public $bookinfo = array('id' => 0, 'title' => 'jar book', 'author' => 'author', 'sort' => 'default', 'publisher' => '', 'seller' => 'DIY_GENERATED', 'corver' => '', 'copyright' . "\0" . '' => Generated by JIEQI CMS);
	public $chapters = array();
	public $charset = JIEQI_SYSTEM_CHARSET;
	public $jar_fd;
	public $chaptercount = 0;
	public $articlelen = 0;
	public $chaptitlelen = 0;

	public function __construct()
	{
		$this->charset = strtoupper($this->charset);
	}

	public function setcharset($charset)
	{
		$this->charset = strtoupper($charset);
	}

	public function setinfo($infoary = array())
	{
		foreach ($this->bookinfo as $k => $v ) {
			if (isset($infoary[$k])) {
				$this->bookinfo[$k] = $infoary[$k];
			}

			if (($k != 'id') && ($this->charset != 'UTF-8')) {
				$this->bookinfo[$k] = iconv($this->charset, 'UTF-8//IGNORE', $this->bookinfo[$k]);
			}
		}
	}

	public function addchapter($title, $content)
	{
		if ($this->charset != 'UTF-8') {
			$title = iconv($this->charset, 'UTF-8//IGNORE', $title);
		}

		if ($this->charset != 'UCS') {
			$content = iconv($this->charset, 'UCS-2LE//IGNORE', $content);
		}

		$this->chapters[$this->chaptercount] = array('title' => $title, 'content' => $content);
		$this->chaptercount++;
		$this->chaptitlelen += strlen($title);
		$this->articlelen += strlen($content);
	}

	public function makejar($jarfile = '', $jadfile = '')
	{
		$zip = new JieqiZip();
		$zip->zipstart($jarfile);
		$zip->zipadd('a.class', jieqi_readfile(JIEQI_ROOT_PATH . '/lib/compress/jar/a.class'));
		$zip->zipadd('b.class', jieqi_readfile(JIEQI_ROOT_PATH . '/lib/compress/jar/b.class'));
		$zip->zipadd('c.class', jieqi_readfile(JIEQI_ROOT_PATH . '/lib/compress/jar/c.class'));
		$zip->zipadd('d.class', jieqi_readfile(JIEQI_ROOT_PATH . '/lib/compress/jar/d.class'));
		$zip->zipadd('e.class', jieqi_readfile(JIEQI_ROOT_PATH . '/lib/compress/jar/e.class'));
		$zip->zipadd('f.class', jieqi_readfile(JIEQI_ROOT_PATH . '/lib/compress/jar/f.class'));
		$zip->zipadd('g.class', jieqi_readfile(JIEQI_ROOT_PATH . '/lib/compress/jar/g.class'));
		$zip->zipadd('h.class', jieqi_readfile(JIEQI_ROOT_PATH . '/lib/compress/jar/h.class'));
		$zip->zipadd('i.class', jieqi_readfile(JIEQI_ROOT_PATH . '/lib/compress/jar/i.class'));
		$zip->zipadd('j.class', jieqi_readfile(JIEQI_ROOT_PATH . '/lib/compress/jar/j.class'));
		$zip->zipadd('k.class', jieqi_readfile(JIEQI_ROOT_PATH . '/lib/compress/jar/k.class'));
		$zip->zipadd('l.class', jieqi_readfile(JIEQI_ROOT_PATH . '/lib/compress/jar/l.class'));
		$zip->zipadd('m.class', jieqi_readfile(JIEQI_ROOT_PATH . '/lib/compress/jar/m.class'));
		$zip->zipadd('n.class', jieqi_readfile(JIEQI_ROOT_PATH . '/lib/compress/jar/n.class'));
		$zip->zipadd('o.class', jieqi_readfile(JIEQI_ROOT_PATH . '/lib/compress/jar/o.class'));
		$zip->zipadd('JavaBook.class', jieqi_readfile(JIEQI_ROOT_PATH . '/lib/compress/jar/JavaBook.class'));
		$zip->zipadd('icon.png', jieqi_readfile(JIEQI_ROOT_PATH . '/lib/compress/jar/icon.png'));
		$maininest = 'Manifest-Version: 1.0' . "\r\n" . 'Ant-Version: Apache Ant 1.7.0' . "\r\n" . 'MicroEdition-Configuration: CLDC-1.0' . "\r\n" . 'MIDlet-Name: ' . $this->bookinfo['title'] . "\r\n" . 'MIDlet-Version: 1.0' . "\r\n" . 'MIDlet-Vendor: JIEQI CMS (www.jieqi.com)' . "\r\n" . 'MIDlet-1: ' . $this->bookinfo['title'] . ', /icon.png, JavaBook' . "\r\n" . 'MicroEdition-Profile: MIDP-1.0' . "\r\n" . 'Created-By: 10.0-b19 (Sun Microsystems Inc.)' . "\r\n";
		$zip->zipadd('META-INF/MANIFEST.MF', $maininest);
		$index = '';
		$index .= chr(0) . chr(1) . chr(48);
		$index .= $this->dechexs(strlen($this->bookinfo['title']), 2) . $this->bookinfo['title'];
		$chaptercount = strval(count($this->chapters));
		$index .= $this->dechexs(strlen($chaptercount), 2) . $chaptercount;
		$i = 1;

		foreach ($this->chapters as $k => $v ) {
			$zip->zipadd($i, $v['content']);
			$tmpstr = $i . ',' . strlen($v['content']) . ',' . $v['title'];
			$index .= $this->dechexs(strlen($tmpstr), 2) . $tmpstr;
			$i++;
		}

		$tmpvar = '';
		$tmpvar .= (strtoupper(JIEQI_SYSTEM_CHARSET) != 'UTF-8' ? iconv(strtoupper(JIEQI_SYSTEM_CHARSET), 'UTF-8//IGNORE', '书名：') : '书名：');
		$tmpvar .= $this->bookinfo['title'] . "\r\n";
		$tmpvar .= (strtoupper(JIEQI_SYSTEM_CHARSET) != 'UTF-8' ? iconv(strtoupper(JIEQI_SYSTEM_CHARSET), 'UTF-8//IGNORE', '作者：') : '作者：');
		$tmpvar .= $this->bookinfo['author'] . "\r\n";
		$tmpvar .= (strtoupper(JIEQI_SYSTEM_CHARSET) != 'UTF-8' ? iconv(strtoupper(JIEQI_SYSTEM_CHARSET), 'UTF-8//IGNORE', '制作：') : '制作：');
		$tmpvar .= $this->bookinfo['publisher'] . "\r\n";

		if (defined('JIEQI_MAIN_URL')) {
			$tmpvar .= (strtoupper(JIEQI_SYSTEM_CHARSET) != 'UTF-8' ? iconv(strtoupper(JIEQI_SYSTEM_CHARSET), 'UTF-8//IGNORE', '网址：') : '网址：');
			$tmpvar .= JIEQI_MAIN_URL . "\r\n";
		}

		$tmpvar = substr($tmpvar, 0, -2);
		$index .= $this->dechexs(strlen($tmpvar), 4) . $tmpvar;
		$zip->zipadd('0', $index);
		$zip->zipend();
		$filesize = filesize($jarfile);
		$jadstr = 'Manifest-Version: 1.0' . "\r\n" . 'Ant-Version: Apache Ant 1.7.0' . "\r\n" . 'MIDlet-1: ' . $this->bookinfo['title'] . ', /icon.png, JavaBook' . "\r\n" . 'MIDlet-Jar-Size: ' . $filesize . "\r\n" . 'MIDlet-Jar-URL: ' . basename($jarfile) . "\r\n" . 'MIDlet-Name: ' . $this->bookinfo['title'] . "\r\n" . 'MIDlet-Vendor: JIEQI CMS (www.jieqi.com)' . "\r\n" . 'MIDlet-Version: 1.0' . "\r\n" . 'MicroEdition-Configuration: CLDC-1.0' . "\r\n" . 'MicroEdition-Profile: MIDP-1.0' . "\r\n";

		if (empty($jadfile)) {
			$i = strrpos($jarfile, '.');

			if (0 < $i) {
				$jadfile = substr($jarfile, 0, $i);
			}

			$jadfile .= '.jad';
		}

		jieqi_writefile($jadfile, $jadstr);
	}

	public function dechexs($num, $bytes)
	{
		$retstr = '';
		$bytes = $bytes * 2;
		$tmpvar = substr(sprintf('%0' . $bytes . 's', dechex($num)), -$bytes);

		for ($i = 0; $i < $bytes; $i += 2) {
			$retstr .= chr(hexdec(substr($tmpvar, $i, 2)));
		}

		return $retstr;
	}
}

include_once (JIEQI_ROOT_PATH . '/lib/compress/zip.php');
define('JIEQI_JAR_DEFAULT_SIZE', 23);
define('JIEQI_JAR_COMPRESS_RATE', 0.72);

