<?php
	require("../__common__/__define_common.php");
	require("../__common__/include-common-all.php");
	require("../account/__logincheck.php");

	$file_name = my_GET('file').'';
	if (defined("photo_LIMITED_IMAGES_FOLDER") and photo_LIMITED_IMAGES_FOLDER <> '') {
		if ($_SESSION['システム管理者'] <> "YES" or photo_LIMITED_ADMIN_NOLIMIT <> 'YES') {
			$len = strlen(photo_LIMITED_IMAGES_FOLDER);
			if (substr($file_name, 0, $len) <> photo_LIMITED_IMAGES_FOLDER) {
				exit;
			}
		}
	}
	if ($file_name == '') {
		exit;
	}
	if (!is_ext_filename_str($file_name, VIDEO_PREVIEW_WMV) and ($_GET['enc'].'' <> 'no')) { // WMVか、「&enc=no」の場合は、この処理をスキップ
		if (mb_check_encoding($file_name, _SYSTEM_FILE_ENCODING)) {	// SWFObject経由(flv等)の場合(?)
			$file_name = myfile_DECODE($file_name);			// 何故かSHIFT-JISでファイル名が渡されている?
		}
	}
	$short_name = left($file_name, mb_strlen($file_name)-4);
	if (is_ext_filename_str($short_name, VIDEO_PREVIEW_CONVERT)) {
		$flv_file = flv_file($file_name);			// ???.mpg.flv
		$file_name = $short_name;
		$src_SJIS = myfile_ENCODE($file_name);
		$flv_SJIS = myfile_ENCODE($flv_file);
		if (!file_exists($flv_SJIS)) {
			if (rightlower($file_name,4) == '.mpg') {
				exec('ffmpeg -i "'.$src_SJIS.'" '.FFMPEG_CONVERT_OPTION_MPG.' "'.$flv_SJIS.'"');
			} elseif (rightlower($file_name,4) == '.mov') {
				exec('ffmpeg -i "'.$src_SJIS.'" '.FFMPEG_CONVERT_OPTION_MOV.' "'.$flv_SJIS.'"');
			} elseif (rightlower($file_name,5) == '.m2ts' or rightlower($file_name,4) == '.mts') {
				exec('ffmpeg -i "'.$src_SJIS.'" '.FFMPEG_CONVERT_OPTION_M2TS.' "'.$flv_SJIS.'"');
			}
		}
	}
	if ($mime_type = is_video_filename($file_name)) {
		if ($flv_file <> '') {
			$fp = fopen($flv_SJIS, "rb");
		} else {
			$fp = fopen(myfile_ENCODE($file_name), "rb");
		}
		if ($fp) {
			if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) {
				$file_name = myfile_ENCODE(substr($file_name,strrpos($file_name,'/')+1));
			} else {
				$file_name = urlencode(substr($file_name,strrpos($file_name,'/')+1));
			}
			header('Content-type: '.$mime_type.'; name='.$file_name);
			header('Content-Disposition: inline; filename='.$file_name);
			fpassthru($fp);
		}
	}
function flv_file($file_name) {		// UTF-8
	$flv_dir = up_folder_path($file_name).VIDEO_PREVIEW_CONVERT_FLV_DIR;
	if (!@opendir(myfile_ENCODE($flv_dir))) {
		@mkdir(myfile_ENCODE($flv_dir));
	}
	return ($flv_dir.basename($file_name));
}
?>
