<?php
header( "Content-Type:text/html;charset=utf-8" );
header( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header( "Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . " GMT" );
header( "Cache-Control: no-cache, must-revalidate" );
header( "Pramga: no-cache" );
$aid = intval( $_REQUEST['aid'] );
if ( empty( $aid ) ) {
		exit( json_encode( array( 'flag' => 0, 'msg' => '参数错误' ) ) );
}
include "../core.php";
include SAXUE_ROOT_PATH . "/model/article.php";
$article_handler = &saxuearticlehandler :: getinstance( "saxuearticlehandler" );
$article = $article_handler -> get( $aid );
if ( !$article ) {
		exit( json_encode( array( 'flag' => 0, 'msg' => '文章不存在' ) ) );
} 
switch ( trim( $_REQUEST['do'] ) ) {
		case 'fav': 
				// article fav
				if ( empty( $_SESSION['saxueUserId'] ) ) {
						exit( json_encode( array( 'flag' => 0, 'msg' => '请先登录再收藏文章' ) ) );
				} 
				$chapterid = intval( $_REQUEST['chapterid'] );
				//$sql = 'INSERT INTO ' . saxue_dbprefix( 'member_doing' ) . '(uid, aid, dotime, doing) VALUES (' . $_SESSION['saxueUserId'] . ', ' . $aid . ', ' . SAXUE_NOW_TIME . ', 2)';
				$sql = 'INSERT INTO ' . saxue_dbprefix( 'member_doing' ) . '(uid, aid, chapterid, dotime, doing) VALUES (' . $_SESSION['saxueUserId'] . ', ' . $aid . ', ' . $chapterid . ', ' . SAXUE_NOW_TIME . ', 2) ON DUPLICATE KEY UPDATE dotime=' . SAXUE_NOW_TIME . ', chapterid=' . $chapterid;
				if ( !$article_handler -> db -> query( $sql ) ) {
						exit( json_encode( array( 'flag' => 0, 'msg' => '收藏文章失败' ) ) );
				} 
				if ( $chapterid ) {
						exit( json_encode( array( 'flag' => 1, 'msg' => '加入书签成功' ) ) );
				} else {
						$favs = $article -> getvar( 'favs' ) + 1;
						$article_handler -> updatefields( 'favs=' . $favs, 'aid=' . $aid );
						exit( json_encode( array( 'flag' => 1, 'msg' => '文章已添加到您的收藏夹', 'favs' => $favs ) ) );
				}
		case 'top': 
				// article top
				$_ids = saxue_getcookie( 'saxueTopArticles' );
				$_toparticles = array();
				if ( !empty( $_ids ) ) {
						$_toparticles = explode( ',', $_ids );
				} 
				if ( in_array( $aid, $_toparticles ) ) {
						exit( json_encode( array( 'flag' => 0, 'msg' => '请不要重复推荐' ) ) );
				} 
				if ( !empty( $_SESSION['saxueUserId'] ) ) {
						$sql = 'INSERT INTO ' . saxue_dbprefix( 'member_doing' ) . '(uid, aid, dotime, doing) VALUES (' . $_SESSION['saxueUserId'] . ', ' . $aid . ', ' . SAXUE_NOW_TIME . ', 3)';
						if ( !$article_handler -> db -> query( $sql ) ) {
								exit( json_encode( array( 'flag' => 0, 'msg' => '您已推荐过该文章' ) ) );
						} 
				} 
				$tops = $article -> getvar( 'tops' ) + 1;
				$article_handler -> updatefields( 'tops=' . $tops, 'aid=' . $aid );
				if ( empty( $_ids ) ) {
						$_ids = $aid;
				} else {
						$_ids = $_ids . ',' . $aid;
				}
				saxue_setcookie( 'saxueTopArticles', $_ids, SAXUE_NOW_TIME + 31536000 );
				exit( json_encode( array( 'flag' => 1, 'msg' => '推荐成功', 'tops' => $tops ) ) );
		case 'down': 
				// article down
				if ( !empty( $_SESSION['saxueUserId'] ) ) {
						$sql = 'INSERT INTO ' . saxue_dbprefix( 'member_doing' ) . '(uid, aid, dotime, doing) VALUES (' . $_SESSION['saxueUserId'] . ', ' . $aid . ', ' . SAXUE_NOW_TIME . ', 4)';
						$article_handler -> db -> query( $sql );
				} 
				saxue_getconfigs( "configs", "article" );
				include SAXUE_ROOT_PATH . "/model/article_data.php";
				$data_handler = saxuearticledatahandler :: getinstance( "saxuearticledatahandler" );
				$data_handler -> setdatatable( $article -> getvar( 'tbnum' ) );
				$data = $data_handler -> get( $aid );
				$title = $data -> getvar( 'title' );
				$ischapter = $article -> getvar( 'ischapter' );
				$isslide = $article -> getvar( 'isslide' );
				$type = trim( $_REQUEST['type'] );
				$article_handler -> updatefields( 'downs=downs+1', 'aid=' . $aid );
				if ( $ischapter ) {
						if ( empty( $_SESSION['saxueUserId'] ) ) {
								exit( json_encode( array( 'flag' => 0, 'msg' => '请先登录再下载TXT' ) ) );
						} 
						$file = $saxueConfigs['article']['txtdir'] . saxue_getsubdir( $aid ) . '/' . $aid . '/_' . $aid . '.txt';
						if ( !is_file( $file ) || filesize( $file ) == 0 ) {
								$row = ( $article -> getvars( 'n' ) ) + ( $data -> getvars( 'n' ) );
								include SAXUE_ROOT_PATH . '/common/packarticle.php';
								$packarticle = new saxuepackarticle( $row );
								$packarticle -> maketxtfull();
						}
						if ( !is_file( $file ) ) {
								exit( 'error' );
						}
						saxue_downfile( $file );
						exit;
						header( "Content-type: application/x-octet-stream" );
						header( "Accept-Ranges: bytes" );
						$ua = $_SERVER["HTTP_USER_AGENT"];
						$encoded_title = urlencode( $title );
						$encoded_title = str_replace( "+", "%20", $encoded_title );
						if ( preg_match( "/MSIE/", $ua ) || preg_match( "/Trident\/7.0/", $ua ) ) {
								header( 'Content-Disposition: attachment; filename="' . $encoded_title . '.txt"' );
						} else if ( preg_match( "/Firefox/", $ua ) ) {
								header( 'Content-Disposition: attachment; filename*="utf8\'\'' . $title . '.txt"' );
						} else {
								header( 'Content-Disposition: attachment; filename="' . $title . '.txt"' );
						}
						$_filesize = filesize( $file );
						header( "Content-Length: " . $_filesize );
						if ( false !== stripos( $file, SAXUE_WEB_PATH ) && !empty( $_SERVER['SERVER_SOFTWARE'] ) && preg_match( "/nginx/is", $_SERVER['SERVER_SOFTWARE'] ) ) {
								header( "X-Accel-Redirect: " . str_replace( SAXUE_WEB_PATH, '', $file ) );
								return true;
						}
						$_fp = fopen( $file, "rb" );
						$_blocksize = 1024;
						$_downsize = 0;
						while ( !feof( $_fp ) && ( $_blocksize < $_filesize - $_downsize ) ) {
								echo fread( $_fp, $_blocksize );
								ob_flush( );
								flush( );
								$_downsize += $_blocksize;
						}
						if ( 0 < $_filesize - $_downsize ) {
								echo fread( $_fp, $_filesize - $_downsize );
								ob_flush( );
								flush( );
						}
						fclose( $_fp );
				} elseif ( $isslide && $type == 'slide' ) {
						if ( empty( $_SESSION['saxueUserId'] ) ) {
								exit( json_encode( array( 'flag' => 0, 'msg' => '请先登录再下载图集' ) ) );
						} 
						$file = $saxueConfigs['article']['txtdir'] . saxue_getsubdir( $aid ) . '/' . $aid . '.zip';
						if ( !is_file( $file ) || filesize( $file ) == 0 ) {
								$row = ( $article -> getvars( 'n' ) ) + ( $data -> getvars( 'n' ) );
								include SAXUE_ROOT_PATH . '/common/packarticle.php';
								$packarticle = new saxuepackarticle( $row );
								$packarticle -> makepiczip();
						}
						if ( !is_file( $file ) ) {
								exit( 'error' );
						}
						saxue_downfile( $file );
				} else {
						$file = $saxueConfigs['article']['txtdir'] . saxue_getsubdir( $aid ) . '/' . $aid . '.txt';
						if ( !file_exists( $file ) ) {
								exit( 'error' );
						}
						$ua = $_SERVER["HTTP_USER_AGENT"];
						$encoded_title = urlencode( $title );
						$encoded_title = str_replace( "+", "%20", $encoded_title );
						header( "Content-type: application/msword" );
						if ( preg_match( "/MSIE/", $ua ) || preg_match( "/Trident\/7.0/", $ua ) ) {
								header( 'Content-Disposition: attachment; filename="' . $encoded_title . '.doc"' );
						} else if ( preg_match( "/Firefox/", $ua ) ) {
								header( 'Content-Disposition: attachment; filename*="utf8\'\'' . $title . '.doc"' );
						} else {
								header( 'Content-Disposition: attachment; filename="' . $title . '.doc"' );
						}
						echo '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>' . $title . '</title></head><body style="font-size:12.0pt;line-height:150%"><p align="center"><strong>' . $title . '</strong></p>' . saxue_readfile( $file ) . '<p>本文档来自<a href="' . SAXUE_URL . '">' . SAXUE_SITE_NAME . '</a></p></body></html>';
				}
				exit;
} 
exit( json_encode( array( 'flag' => 0, 'msg' => '操作错误' ) ) );
