<?php
// 多图上传
include "../core.php";
$uid = intval( $_SESSION['saxueUserId'] );
if ( empty( $uid ) && empty( $_SESSION['saxueAdminId'] ) ) exit( '<script>parent.' . $_POST['backFunction'] . '(0, "未登录！", ' . $_POST['PicPos'] . ');</script>' );

// 文件保存目录
$save_path = saxue_uploadpath();
// 文件保存目录URL
$save_url = saxue_uploadurl();
// FTP保存目录
if ( SAXUE_ATTACHS_PATH != '' && strpos( SAXUE_ATTACHS_PATH, "\\" ) === false && SAXUE_ATTACHS_FTPURL != '' ) {
		if ( substr( SAXUE_ATTACHS_PATH, 0, 1 ) === '/' ) {
				$ftp_path = SAXUE_ATTACHS_FTPURL . SAXUE_ATTACHS_PATH;
		} else {
				$ftp_path = SAXUE_ATTACHS_FTPURL . '/' . SAXUE_ATTACHS_PATH;
		}
} else {
		$ftp_path = '';
}
// 文件路径和URL
$_filepath = $_fileurl = '';
// 文件路径
if ( !empty( $uid ) ) $_filepath = 'image/' . floor( intval( $uid ) / 1000 ) . '/' . $uid . '/' . date( "ym" );
else $_filepath = 'image/' . date( "ym" );
// 检查目录
saxue_checkdir( $save_path . '/' . $_filepath, true );

// 接收上传数据
if ( 'php' == $_POST['name'] ) {
		// PHP方式上传
		if ( empty( $_FILES['fileUploadInput']['name'] ) ) {
				exit( '<script>parent.' . $_POST['backFunction'] . '(0, "请选择要上传的图片！", ' . $_POST['PicPos'] . ');</script>' );
		} 
		if ( 0 < $_FILES['fileUploadInput']['error'] ) {
				exit( '<script>parent.' . $_POST['backFunction'] . '(0, "上传失败，可能是文件太大或者网络问题！", ' . $_POST['PicPos'] . ');</script>' );
		} 
		$file_ext = strrchr( trim( strtolower( $_FILES['fileUploadInput']['name'] ) ), "." );
		if ( !eregi( "\\.(gif|jpg|jpeg|png|bmp)\$", $file_ext ) ) {
				exit( '<script>parent.' . $_POST['backFunction'] . '(0, "对不起，您上传的文件不是有效的图片格式！", ' . $_POST['PicPos'] . ');</script>' );
				saxue_delfile( $_FILES['fileUploadInput']['tmp_name'] );
		} 
		$_filepath .= '/' . date( "dHis" ) . mt_rand( 10000, 99999 ) . '.' . $file_ext;
		if ( move_uploaded_file( $_FILES['fileUploadInput']['tmp_name'], $save_path . '/' . $_filepath ) === false ) {
				exit( '<script>parent.' . $_POST['backFunction'] . '(0, "上传失败，可能您没有文件夹的写权限", ' . $_POST['PicPos'] . ');</script>' );
		} 
} else {
		// FLASH上传
		$_filepath .= '/n_' . date( "dHis" ) . mt_rand( 10000, 99999 ) . '.jpg';
		$datastr = $GLOBALS['HTTP_RAW_POST_DATA'];
		if ( empty( $datastr ) ) $datastr = file_get_contents( 'php://input' );
		file_put_contents( $save_path . '/' . $_filepath, $datastr );
		if ( !is_file( $save_path . '/' . $_filepath ) ) {
				exit;
		} 
}
@chmod( $save_path . '/' . $_filepath, 0644 );

// 保存到远程服务器
$_fileurl = $save_url . '/' . $_filepath;
$_filein = $save_path . '/' . $_filepath;
$_savein = 'local';
if ( OSS_OPEN ) {
		// 保存文件到OSS
		include SAXUE_ROOT_PATH . '/aliyun/oss_php52/sdk.class.php';
		$ret = saxue_oss_upload( $_filepath, $save_path . '/' . $_filepath );
		if ( $ret['flag'] ) {
				$_fileurl = OSS_URL . '/' . $_filepath;
				$_filein = $_filepath;
				$_savein = 'oss';
				saxue_delfile( $save_path . '/' . $_filepath );
		} else {
				error_log( date( 'H:i:s' ) . '| swfupload: ' . $ret['msg'] . "\r\n\r\n", 3, SAXUE_ROOT_PATH . '/aliyun/oss_php52/logs/' . date( 'Ymd' ) . '.txt' );
		}
} elseif ( SAXUE_ATTACHS_FTP && !empty( $ftp_path ) ) {
		// 开启FTP上传
		$ret = saxue_copyfile( $save_path . '/' . $_filepath, $ftp_path . '/' . $_filepath, 644, true );
		if ( $ret ) {
				$_savein = 'ftp';
				$_filein = $ftp_path . '/' . $_filepath;
		}
}

// 附件插入数据库
$timeline = intval( $_REQUEST['timeline'] );
if ( !empty( $timeline ) ) {
		include SAXUE_ROOT_PATH . "/model/attachs.php";
		$attachs_handler = saxueattachshandler :: getinstance( "saxueattachshandler" );
		$attachs = $attachs_handler -> create();
		$attachs -> setvar( 'timeline', $timeline );
		$attachs -> setvar( 'aid', 0 );
		$attachs -> setvar( 'savein', $_savein );
		$attachs -> setvar( 'filepath', str_replace( '\\', '/', $_filein ) );
		$attachs -> setvar( 'fileurl', $_fileurl );
		$attachs_handler -> insert( $attachs );
}

// 返回结果
if ( 'php' == $_POST['name'] ) {
		exit( '<script>parent.' . $_POST['backFunction'] . '(1, "' . $_fileurl . '", ' . $_POST['PicPos'] . ');</script>' );
} else {
		exit( $_fileurl );
} 
