<?php
include "../../core.php";
if ( empty( $_REQUEST['key'] ) ) {
		exit;
}
if ( $_REQUEST['key'] != md5( SAXUE_DB_USER . SAXUE_DB_PASS . SAXUE_DB_NAME ) ) {
		exit;
}
if ( !is_numeric( $_REQUEST['aid'] ) ) {
		exit;
}
if ( !is_array( $_REQUEST['packflag'] ) || count( $_REQUEST['packflag'] ) < 1 ) {
		exit;
}
$aid = intval( $_REQUEST['aid'] );
@ignore_user_abort( @true );
@set_time_limit( 3600 );
@session_write_close( );
@ini_set( "memory_limit", "64M" );
include SAXUE_ROOT_PATH . "/model/article.php";
$article_handler = saxuearticlehandler :: getinstance( "saxuearticlehandler" );
$article = $article_handler -> get( $aid );
if ( !is_object( $article ) ) {
		exit;
} 
saxue_getconfigs( 'configs', 'article' );
		include SAXUE_ROOT_PATH . "/common/packarticle.php";
$packarticle = new saxuepackarticle( $aid );
include SAXUE_ROOT_PATH . "/model/article_chapter.php";
$chapter_handler = saxuearticlechapterhandler :: getinstance( "saxuearticlechapterhandler" );
$criteria = new criteriacompo( new criteria( "aid", $aid ) );
$criteria -> setsort( "chapterorder ASC, chapterid" );
$criteria -> setorder( "ASC" );
$res = $chapter_handler -> queryobjects( $criteria );
$i = 0;
while ( $_chapter = $chapter_handler->getobject( $res ) ) {
		$packarticle -> chapters[$i] = array( "name" => $_chapter -> getvar( "chaptername", "n" ), "href" => $_chapter -> getvar( "chapterid", "n" ) . '.txt', "isvolume" => $_chapter -> getvar( "isvolume", "n" ), "posttime" => $_chapter -> getvar( "posttime", "n" ) );
		++$i;
		if ( $_chapter -> getvar( "chapterorder", "n" ) != $i ) {
				$_chapter -> setvar( "chapterorder", $i );
				$chapter_handler -> insert( $_chapter );
		}
}
$packarticle -> isloadchapter = true;
$client = intval( $_REQUEST['client'] );
if ( $client < 0 || $client > 3 ) {
		$client = 0;
}
if ( in_array( "makecds", $_REQUEST['packflag'] ) ) {
		$packarticle -> createcds();
}
if ( in_array( "makeindex", $_REQUEST['packflag'] ) ) {
		$packarticle -> makeindex( false, $client );
}
if ( in_array( "makechapter", $_REQUEST['packflag'] ) ) {
		$_count = count( $packarticle -> chapters );
		for ( $i = 1; $i <= $_count; ++$i ) {
				$packarticle -> makechapter( $i, false, $client );
		}
}
return true;